@@ -11,10 +11,23 @@ That does not mean that HTTP only works with the _LL APIs.
11
11
Simply changing the using the convenience layer (functions not having _LL)
12
12
and removing calls to _DoWork will yield the same results. */
13
13
14
+ #ifdef ARDUINO
14
15
#include "AzureIoTHub.h"
15
- #include "iot_logging.h"
16
+ #else
17
+ #include "azure_c_shared_utility/threadapi.h"
18
+ #include "azure_c_shared_utility/platform.h"
19
+ #include "serializer.h"
20
+ #include "iothub_client_ll.h"
21
+ #include "iothubtransporthttp.h"
22
+ #endif
16
23
17
- static const char * connectionString = "HostName=[host].azure-devices.net;DeviceId=[device];SharedAccessKey=[key]" ;
24
+ #ifdef MBED_BUILD_TIMESTAMP
25
+ #include "certs.h"
26
+ #endif // MBED_BUILD_TIMESTAMP
27
+
28
+ /*String containing Hostname, Device Id & Device Key in the format: */
29
+ /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
30
+ static const char * connectionString = "[device connection string]" ;
18
31
19
32
// Define the Model
20
33
BEGIN_NAMESPACE (WeatherStation );
@@ -29,36 +42,35 @@ WITH_ACTION(SetAirResistance, int, Position)
29
42
30
43
END_NAMESPACE (WeatherStation );
31
44
32
- DEFINE_ENUM_STRINGS (IOTHUB_CLIENT_CONFIRMATION_RESULT , IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES )
33
45
34
46
EXECUTE_COMMAND_RESULT TurnFanOn (ContosoAnemometer * device )
35
47
{
36
48
(void )device ;
37
- LogInfo ("Turning fan on.\r\n" );
49
+ ( void ) printf ("Turning fan on.\r\n" );
38
50
return EXECUTE_COMMAND_SUCCESS ;
39
51
}
40
52
41
53
EXECUTE_COMMAND_RESULT TurnFanOff (ContosoAnemometer * device )
42
54
{
43
55
(void )device ;
44
- LogInfo ("Turning fan off.\r\n" );
56
+ ( void ) printf ("Turning fan off.\r\n" );
45
57
return EXECUTE_COMMAND_SUCCESS ;
46
58
}
47
59
48
60
EXECUTE_COMMAND_RESULT SetAirResistance (ContosoAnemometer * device , int Position )
49
61
{
50
62
(void )device ;
51
- LogInfo ("Setting Air Resistance Position to %d.\r\n" , Position );
63
+ ( void ) printf ("Setting Air Resistance Position to %d.\r\n" , Position );
52
64
return EXECUTE_COMMAND_SUCCESS ;
53
65
}
54
66
55
67
void sendCallback (IOTHUB_CLIENT_CONFIRMATION_RESULT result , void * userContextCallback )
56
68
{
57
- int messageTrackingId = (intptr_t )userContextCallback ;
69
+ unsigned int messageTrackingId = (unsigned int )( uintptr_t )userContextCallback ;
58
70
59
- LogInfo ( "Message Id: %d Received.\r\n" , messageTrackingId );
71
+ ( void ) printf ( "Message Id: %u Received.\r\n" , messageTrackingId );
60
72
61
- LogInfo ("Result Call Back Called! Result is: %s \r\n" , ENUM_TO_STRING (IOTHUB_CLIENT_CONFIRMATION_RESULT , result ));
73
+ ( void ) printf ("Result Call Back Called! Result is: %s \r\n" , ENUM_TO_STRING (IOTHUB_CLIENT_CONFIRMATION_RESULT , result ));
62
74
}
63
75
64
76
static void sendMessage (IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle , const unsigned char * buffer , size_t size )
@@ -67,17 +79,17 @@ static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsign
67
79
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray (buffer , size );
68
80
if (messageHandle == NULL )
69
81
{
70
- LogInfo ("unable to create a new IoTHubMessage\r\n" );
82
+ printf ("unable to create a new IoTHubMessage\r\n" );
71
83
}
72
84
else
73
85
{
74
86
if (IoTHubClient_LL_SendEventAsync (iotHubClientHandle , messageHandle , sendCallback , (void * )(uintptr_t )messageTrackingId ) != IOTHUB_CLIENT_OK )
75
87
{
76
- LogInfo ("failed to hand over the message to IoTHubClient" );
88
+ printf ("failed to hand over the message to IoTHubClient" );
77
89
}
78
90
else
79
91
{
80
- LogInfo ("IoTHubClient accepted the message for delivery\r\n" );
92
+ printf ("IoTHubClient accepted the message for delivery\r\n" );
81
93
}
82
94
IoTHubMessage_Destroy (messageHandle );
83
95
}
@@ -93,7 +105,7 @@ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE mess
93
105
size_t size ;
94
106
if (IoTHubMessage_GetByteArray (message , & buffer , & size ) != IOTHUB_MESSAGE_OK )
95
107
{
96
- LogInfo ("unable to IoTHubMessage_GetByteArray\r\n" );
108
+ printf ("unable to IoTHubMessage_GetByteArray\r\n" );
97
109
result = EXECUTE_COMMAND_ERROR ;
98
110
}
99
111
else
@@ -102,14 +114,16 @@ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE mess
102
114
char * temp = malloc (size + 1 );
103
115
if (temp == NULL )
104
116
{
105
- LogInfo ("failed to malloc\r\n" );
117
+ printf ("failed to malloc\r\n" );
106
118
result = EXECUTE_COMMAND_ERROR ;
107
119
}
108
120
else
109
121
{
122
+ EXECUTE_COMMAND_RESULT executeCommandResult ;
123
+
110
124
memcpy (temp , buffer , size );
111
125
temp [size ] = '\0' ;
112
- EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND (userContextCallback , temp );
126
+ executeCommandResult = EXECUTE_COMMAND (userContextCallback , temp );
113
127
result =
114
128
(executeCommandResult == EXECUTE_COMMAND_ERROR ) ? IOTHUBMESSAGE_ABANDONED :
115
129
(executeCommandResult == EXECUTE_COMMAND_SUCCESS ) ? IOTHUBMESSAGE_ACCEPTED :
@@ -122,94 +136,110 @@ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE mess
122
136
123
137
void simplesample_http_run (void )
124
138
{
125
- if (serializer_init ( NULL ) != SERIALIZER_OK )
139
+ if (platform_init ( ) != 0 )
126
140
{
127
- LogInfo ("Failed on serializer_init \r\n" );
141
+ printf ("Failed to initialize the platform. \r\n" );
128
142
}
129
143
else
130
144
{
131
- IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString (connectionString , HTTP_Protocol );
132
- srand ((unsigned int )time (NULL ));
133
- int avgWindSpeed = 10 ;
134
-
135
- if (iotHubClientHandle == NULL )
145
+ if (serializer_init (NULL ) != SERIALIZER_OK )
136
146
{
137
- LogInfo ( "Failed on IoTHubClient_LL_Create \r\n" );
147
+ ( void ) printf ( "Failed on serializer_init \r\n" );
138
148
}
139
149
else
140
150
{
141
- unsigned int minimumPollingTime = 9 ; /*because it can poll "after 9 seconds" polls will happen effectively at ~10 seconds*/
142
- if (IoTHubClient_LL_SetOption (iotHubClientHandle , "MinimumPollingTime" , & minimumPollingTime ) != IOTHUB_CLIENT_OK )
143
- {
144
- LogInfo ("failure to set option \"MinimumPollingTime\"\r\n" );
145
- }
151
+ IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString (connectionString , HTTP_Protocol );
152
+ int avgWindSpeed = 10 ;
146
153
147
- #ifdef MBED_BUILD_TIMESTAMP
148
- // For mbed add the certificate information
149
- if (IoTHubClient_LL_SetOption (iotHubClientHandle , "TrustedCerts" , certificates ) != IOTHUB_CLIENT_OK )
150
- {
151
- LogInfo ("failure to set option \"TrustedCerts\"\r\n" );
152
- }
153
- #endif // MBED_BUILD_TIMESTAMP
154
+ srand ((unsigned int )time (NULL ));
154
155
155
- ContosoAnemometer * myWeather = CREATE_MODEL_INSTANCE (WeatherStation , ContosoAnemometer );
156
- if (myWeather == NULL )
156
+ if (iotHubClientHandle == NULL )
157
157
{
158
- LogInfo ( "Failed on CREATE_MODEL_INSTANCE \r\n" );
158
+ ( void ) printf ( "Failed on IoTHubClient_LL_Create \r\n" );
159
159
}
160
160
else
161
161
{
162
- if (IoTHubClient_LL_SetMessageCallback (iotHubClientHandle , IoTHubMessage , myWeather ) != IOTHUB_CLIENT_OK )
162
+ // Because it can poll "after 9 seconds" polls will happen
163
+ // effectively at ~10 seconds.
164
+ // Note that for scalabilty, the default value of minimumPollingTime
165
+ // is 25 minutes. For more information, see:
166
+ // https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging
167
+ unsigned int minimumPollingTime = 9 ;
168
+ ContosoAnemometer * myWeather ;
169
+
170
+ if (IoTHubClient_LL_SetOption (iotHubClientHandle , "MinimumPollingTime" , & minimumPollingTime ) != IOTHUB_CLIENT_OK )
163
171
{
164
- LogInfo ("unable to IoTHubClient_SetMessageCallback\r\n" );
172
+ printf ("failure to set option \"MinimumPollingTime\"\r\n" );
173
+ }
174
+
175
+ #ifdef MBED_BUILD_TIMESTAMP
176
+ // For mbed add the certificate information
177
+ if (IoTHubClient_LL_SetOption (iotHubClientHandle , "TrustedCerts" , certificates ) != IOTHUB_CLIENT_OK )
178
+ {
179
+ (void )printf ("failure to set option \"TrustedCerts\"\r\n" );
180
+ }
181
+ #endif // MBED_BUILD_TIMESTAMP
182
+
183
+ myWeather = CREATE_MODEL_INSTANCE (WeatherStation , ContosoAnemometer );
184
+ if (myWeather == NULL )
185
+ {
186
+ (void )printf ("Failed on CREATE_MODEL_INSTANCE\r\n" );
165
187
}
166
188
else
167
189
{
168
- myWeather -> DeviceId = "myFirstDevice" ;
169
- myWeather -> WindSpeed = avgWindSpeed + (rand () % 4 + 2 );
190
+ if (IoTHubClient_LL_SetMessageCallback (iotHubClientHandle , IoTHubMessage , myWeather ) != IOTHUB_CLIENT_OK )
170
191
{
171
- unsigned char * destination ;
172
- size_t destinationSize ;
173
- if (SERIALIZE (& destination , & destinationSize , myWeather -> DeviceId , myWeather -> WindSpeed ) != IOT_AGENT_OK )
174
- {
175
- LogInfo ("Failed to serialize\r\n" );
176
- }
177
- else
192
+ printf ("unable to IoTHubClient_SetMessageCallback\r\n" );
193
+ }
194
+ else
195
+ {
196
+ myWeather -> DeviceId = "myFirstDevice" ;
197
+ myWeather -> WindSpeed = avgWindSpeed + (rand () % 4 + 2 );
178
198
{
179
- IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray (destination , destinationSize );
180
- if (messageHandle == NULL )
199
+ unsigned char * destination ;
200
+ size_t destinationSize ;
201
+ if (SERIALIZE (& destination , & destinationSize , myWeather -> DeviceId , myWeather -> WindSpeed ) != IOT_AGENT_OK )
181
202
{
182
- LogInfo ( "unable to create a new IoTHubMessage \r\n" );
203
+ ( void ) printf ( "Failed to serialize \r\n" );
183
204
}
184
205
else
185
206
{
186
- if (IoTHubClient_LL_SendEventAsync (iotHubClientHandle , messageHandle , sendCallback , (void * )1 ) != IOTHUB_CLIENT_OK )
207
+ IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray (destination , destinationSize );
208
+ if (messageHandle == NULL )
187
209
{
188
- LogInfo ( "failed to hand over the message to IoTHubClient " );
210
+ printf ( "unable to create a new IoTHubMessage\r\n " );
189
211
}
190
212
else
191
213
{
192
- LogInfo ("IoTHubClient accepted the message for delivery\r\n" );
214
+ if (IoTHubClient_LL_SendEventAsync (iotHubClientHandle , messageHandle , sendCallback , (void * )1 ) != IOTHUB_CLIENT_OK )
215
+ {
216
+ printf ("failed to hand over the message to IoTHubClient" );
217
+ }
218
+ else
219
+ {
220
+ printf ("IoTHubClient accepted the message for delivery\r\n" );
221
+ }
222
+
223
+ IoTHubMessage_Destroy (messageHandle );
193
224
}
194
-
195
- IoTHubMessage_Destroy (messageHandle );
225
+ free (destination );
196
226
}
197
- free (destination );
198
227
}
199
- }
200
228
201
- /* wait for commands */
202
- while (1 )
203
- {
204
- IoTHubClient_LL_DoWork (iotHubClientHandle );
205
- ThreadAPI_Sleep (100 );
229
+ /* wait for commands */
230
+ while (1 )
231
+ {
232
+ IoTHubClient_LL_DoWork (iotHubClientHandle );
233
+ ThreadAPI_Sleep (100 );
234
+ }
206
235
}
207
- }
208
236
209
- DESTROY_MODEL_INSTANCE (myWeather );
237
+ DESTROY_MODEL_INSTANCE (myWeather );
238
+ }
239
+ IoTHubClient_LL_Destroy (iotHubClientHandle );
210
240
}
211
- IoTHubClient_LL_Destroy ( iotHubClientHandle );
241
+ serializer_deinit ( );
212
242
}
213
- serializer_deinit ();
243
+ platform_deinit ();
214
244
}
215
245
}
0 commit comments