13
13
#include <iothub_client_options.h>
14
14
#include <iothub_message.h>
15
15
#include <iothubtransportmqtt.h>
16
-
16
+ #include <jsondecoder.h>
17
17
#include "./config.h"
18
18
#include "./wiring.h"
19
19
@@ -23,6 +23,8 @@ const char *notFound = "\"No method found\"";
23
23
static bool messagePending = false;
24
24
static bool sendingMessage = true;
25
25
26
+ static int interval = INTERVAL ;
27
+
26
28
static void sendCallback (IOTHUB_CLIENT_CONFIRMATION_RESULT result , void * userContextCallback )
27
29
{
28
30
if (IOTHUB_CLIENT_CONFIRMATION_OK == result )
@@ -31,7 +33,7 @@ static void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void *userCon
31
33
}
32
34
else
33
35
{
34
- ( void ) printf ( "Failed to send message to Azure IoT Hub\r\n " );
36
+ LogError ( "Failed to send message to Azure IoT Hub" );
35
37
}
36
38
37
39
messagePending = false;
@@ -42,21 +44,21 @@ static void sendMessages(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, char *buffe
42
44
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray (buffer , strlen (buffer ));
43
45
if (messageHandle == NULL )
44
46
{
45
- ( void ) printf ( "Unable to create a new IoTHubMessage\r\n " );
47
+ LogError ( "Unable to create a new IoTHubMessage" );
46
48
}
47
49
else
48
50
{
49
51
MAP_HANDLE properties = IoTHubMessage_Properties (messageHandle );
50
52
Map_Add (properties , "temperatureAlert" , (temperatureAlert > 0 ) ? "true" : "false" );
51
- ( void ) printf ( "Sending message: %s\r\n " , buffer );
53
+ LogInfo ( "Sending message: %s" , buffer );
52
54
if (IoTHubClient_LL_SendEventAsync (iotHubClientHandle , messageHandle , sendCallback , NULL ) != IOTHUB_CLIENT_OK )
53
55
{
54
- ( void ) printf ( "Failed to send message to Azure IoT Hub\r\n " );
56
+ LogError ( "Failed to send message to Azure IoT Hub" );
55
57
}
56
58
else
57
59
{
58
60
messagePending = true;
59
- ( void ) printf ( "Message sent to Azure IoT Hub\r\n " );
61
+ LogInfo ( "Message sent to Azure IoT Hub" );
60
62
}
61
63
62
64
IoTHubMessage_Destroy (messageHandle );
@@ -100,7 +102,7 @@ int deviceMethodCallback(
100
102
size_t * response_size ,
101
103
void * userContextCallback )
102
104
{
103
- ( void ) printf ("Try to invoke method %s\r\n" , methodName );
105
+ LogInfo ("Try to invoke method %s\r\n" , methodName );
104
106
const char * responseMessage = onSuccess ;
105
107
int result = 200 ;
106
108
@@ -114,7 +116,7 @@ int deviceMethodCallback(
114
116
}
115
117
else
116
118
{
117
- ( void ) printf ("No method %s found\r\n" , methodName );
119
+ LogError ("No method %s found\r\n" , methodName );
118
120
responseMessage = notFound ;
119
121
result = 404 ;
120
122
}
@@ -126,6 +128,39 @@ int deviceMethodCallback(
126
128
return result ;
127
129
}
128
130
131
+ void twinCallback (
132
+ DEVICE_TWIN_UPDATE_STATE updateState ,
133
+ const unsigned char * payLoad ,
134
+ size_t size ,
135
+ void * userContextCallback )
136
+ {
137
+ char * temp = (char * )malloc (size + 1 );
138
+ for (int i = 0 ; i < size ; i ++ )
139
+ {
140
+ temp [i ] = (char )(payLoad [i ]);
141
+ }
142
+ temp [size ] = '\0' ;
143
+ MULTITREE_HANDLE tree = NULL ;
144
+
145
+ if (JSON_DECODER_OK == JSONDecoder_JSON_To_MultiTree (temp , & tree ))
146
+ {
147
+ MULTITREE_HANDLE child = NULL ;
148
+
149
+ if (MULTITREE_OK != MultiTree_GetChildByName (tree , "desired" , & child ))
150
+ {
151
+ LogInfo ("This device twin message contains desired message only" );
152
+ child = tree ;
153
+ }
154
+ const void * value = NULL ;
155
+ if (MULTITREE_OK == MultiTree_GetLeafValue (child , "interval" , & value ))
156
+ {
157
+ interval = atoi ((const char * )value );
158
+ }
159
+ }
160
+ MultiTree_Destroy (tree );
161
+ free (temp );
162
+ }
163
+
129
164
IOTHUBMESSAGE_DISPOSITION_RESULT receiveMessageCallback (IOTHUB_MESSAGE_HANDLE message , void * userContextCallback )
130
165
{
131
166
const unsigned char * buffer = NULL ;
@@ -147,7 +182,7 @@ IOTHUBMESSAGE_DISPOSITION_RESULT receiveMessageCallback(IOTHUB_MESSAGE_HANDLE me
147
182
strncpy (temp , buffer , size );
148
183
temp [size ] = '\0' ;
149
184
150
- printf ("Receiving message: %s\r\n" , temp );
185
+ ( void ) printf ("Receiving message: %s\r\n" , temp );
151
186
free (temp );
152
187
153
188
return IOTHUBMESSAGE_ACCEPTED ;
@@ -163,7 +198,7 @@ static char *readFile(char *fileName)
163
198
164
199
if (fp == NULL )
165
200
{
166
- ( void ) printf ( "ERROR: File %s doesn't exist!\n " , fileName );
201
+ LogError ( "ERROR: File %s doesn't exist!" , fileName );
167
202
return NULL ;
168
203
}
169
204
@@ -177,7 +212,7 @@ static char *readFile(char *fileName)
177
212
if (buffer == NULL )
178
213
{
179
214
fclose (fp );
180
- ( void ) printf ( "ERROR: Failed to allocate memory.\n " );
215
+ LogError ( "ERROR: Failed to allocate memory." );
181
216
return NULL ;
182
217
}
183
218
@@ -186,7 +221,7 @@ static char *readFile(char *fileName)
186
221
{
187
222
fclose (fp );
188
223
free (buffer );
189
- ( void ) printf ( "ERROR: Failed to read the file %s into memory.\n " , fileName );
224
+ LogError ( "ERROR: Failed to read the file %s into memory." , fileName );
190
225
return NULL ;
191
226
}
192
227
@@ -212,7 +247,7 @@ static bool setX509Certificate(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, char
212
247
IoTHubClient_LL_SetOption (iotHubClientHandle , OPTION_X509_CERT , x509certificate ) != IOTHUB_CLIENT_OK ||
213
248
IoTHubClient_LL_SetOption (iotHubClientHandle , OPTION_X509_PRIVATE_KEY , x509privatekey ) != IOTHUB_CLIENT_OK )
214
249
{
215
- ( void ) printf ( "ERROR: Failed to set options for x509.\n " );
250
+ LogError ( " Failed to set options for x509." );
216
251
return false;
217
252
}
218
253
@@ -226,7 +261,7 @@ int main(int argc, char *argv[])
226
261
{
227
262
if (argc < 2 )
228
263
{
229
- ( void ) printf ( "Usage: %s <IoT hub device connection string>\r\n " , argv [0 ]);
264
+ LogError ( "Usage: %s <IoT hub device connection string>" , argv [0 ]);
230
265
return 1 ;
231
266
}
232
267
@@ -237,7 +272,7 @@ int main(int argc, char *argv[])
237
272
238
273
if (device_id_src == NULL )
239
274
{
240
- ( void ) printf ( "ERROR: Cannot parse device id from IoT device connection string\n " );
275
+ LogError ( " Cannot parse device id from IoT device connection string" );
241
276
return 1 ;
242
277
}
243
278
snprintf (device_id , sizeof (device_id ), "%s" , device_id_src );
@@ -247,13 +282,13 @@ int main(int argc, char *argv[])
247
282
248
283
if (platform_init () != 0 )
249
284
{
250
- ( void ) printf ( "Failed to initialize the platform.\r\n " );
285
+ LogError ( "Failed to initialize the platform." );
251
286
}
252
287
else
253
288
{
254
289
if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString (argv [1 ], MQTT_Protocol )) == NULL )
255
290
{
256
- ( void ) printf ( "ERROR: iotHubClientHandle is NULL!\r\n " );
291
+ LogError ( " iotHubClientHandle is NULL!" );
257
292
}
258
293
else
259
294
{
@@ -269,6 +304,7 @@ int main(int argc, char *argv[])
269
304
// set C2D and device method callback
270
305
IoTHubClient_LL_SetMessageCallback (iotHubClientHandle , receiveMessageCallback , NULL );
271
306
IoTHubClient_LL_SetDeviceMethodCallback (iotHubClientHandle , deviceMethodCallback , NULL );
307
+ IoTHubClient_LL_SetDeviceTwinCallback (iotHubClientHandle , twinCallback , NULL );
272
308
273
309
int count = 0 ;
274
310
while (true)
@@ -286,10 +322,10 @@ int main(int argc, char *argv[])
286
322
}
287
323
else
288
324
{
289
- ( void ) printf ( "Failed to read message\r\n " );
325
+ LogError ( "Failed to read message" );
290
326
}
291
327
}
292
- delay (INTERVAL );
328
+ delay (interval );
293
329
}
294
330
IoTHubClient_LL_DoWork (iotHubClientHandle );
295
331
}
0 commit comments