Skip to content

Commit c1e8515

Browse files
authored
✨ The client app can receiving device twin message (Azure-Samples#4)
* Fix readme * fix mirrot * Update main.c * Update main.c * twins * parse the interval value * I can change the interval of sending message * the receiving message is only the desired part * Update LogInfor * change printf to log * Update config.h
1 parent 6fb4a74 commit c1e8515

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
*.gch
99
*.pch
1010

11+
# vim temp
12+
*.swp
13+
14+
#cmake file
15+
CMakeCache.txt
16+
CMakeFiles/
17+
Makefile
18+
app
19+
cmake_install.cmake
20+
1121
# Libraries
1222
*.lib
1323
*.a

main.c

+55-19
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <iothub_client_options.h>
1414
#include <iothub_message.h>
1515
#include <iothubtransportmqtt.h>
16-
16+
#include <jsondecoder.h>
1717
#include "./config.h"
1818
#include "./wiring.h"
1919

@@ -23,6 +23,8 @@ const char *notFound = "\"No method found\"";
2323
static bool messagePending = false;
2424
static bool sendingMessage = true;
2525

26+
static int interval = INTERVAL;
27+
2628
static void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void *userContextCallback)
2729
{
2830
if (IOTHUB_CLIENT_CONFIRMATION_OK == result)
@@ -31,7 +33,7 @@ static void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void *userCon
3133
}
3234
else
3335
{
34-
(void)printf("Failed to send message to Azure IoT Hub\r\n");
36+
LogError("Failed to send message to Azure IoT Hub");
3537
}
3638

3739
messagePending = false;
@@ -42,21 +44,21 @@ static void sendMessages(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, char *buffe
4244
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, strlen(buffer));
4345
if (messageHandle == NULL)
4446
{
45-
(void)printf("Unable to create a new IoTHubMessage\r\n");
47+
LogError("Unable to create a new IoTHubMessage");
4648
}
4749
else
4850
{
4951
MAP_HANDLE properties = IoTHubMessage_Properties(messageHandle);
5052
Map_Add(properties, "temperatureAlert", (temperatureAlert > 0) ? "true" : "false");
51-
(void)printf("Sending message: %s\r\n", buffer);
53+
LogInfo("Sending message: %s", buffer);
5254
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, NULL) != IOTHUB_CLIENT_OK)
5355
{
54-
(void)printf("Failed to send message to Azure IoT Hub\r\n");
56+
LogError("Failed to send message to Azure IoT Hub");
5557
}
5658
else
5759
{
5860
messagePending = true;
59-
(void)printf("Message sent to Azure IoT Hub\r\n");
61+
LogInfo("Message sent to Azure IoT Hub");
6062
}
6163

6264
IoTHubMessage_Destroy(messageHandle);
@@ -100,7 +102,7 @@ int deviceMethodCallback(
100102
size_t *response_size,
101103
void *userContextCallback)
102104
{
103-
(void)printf("Try to invoke method %s\r\n", methodName);
105+
LogInfo("Try to invoke method %s\r\n", methodName);
104106
const char *responseMessage = onSuccess;
105107
int result = 200;
106108

@@ -114,7 +116,7 @@ int deviceMethodCallback(
114116
}
115117
else
116118
{
117-
(void)printf("No method %s found\r\n", methodName);
119+
LogError("No method %s found\r\n", methodName);
118120
responseMessage = notFound;
119121
result = 404;
120122
}
@@ -126,6 +128,39 @@ int deviceMethodCallback(
126128
return result;
127129
}
128130

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+
129164
IOTHUBMESSAGE_DISPOSITION_RESULT receiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void *userContextCallback)
130165
{
131166
const unsigned char *buffer = NULL;
@@ -147,7 +182,7 @@ IOTHUBMESSAGE_DISPOSITION_RESULT receiveMessageCallback(IOTHUB_MESSAGE_HANDLE me
147182
strncpy(temp, buffer, size);
148183
temp[size] = '\0';
149184

150-
printf("Receiving message: %s\r\n", temp);
185+
(void)printf("Receiving message: %s\r\n", temp);
151186
free(temp);
152187

153188
return IOTHUBMESSAGE_ACCEPTED;
@@ -163,7 +198,7 @@ static char *readFile(char *fileName)
163198

164199
if (fp == NULL)
165200
{
166-
(void)printf("ERROR: File %s doesn't exist!\n", fileName);
201+
LogError("ERROR: File %s doesn't exist!", fileName);
167202
return NULL;
168203
}
169204

@@ -177,7 +212,7 @@ static char *readFile(char *fileName)
177212
if (buffer == NULL)
178213
{
179214
fclose(fp);
180-
(void)printf("ERROR: Failed to allocate memory.\n");
215+
LogError("ERROR: Failed to allocate memory.");
181216
return NULL;
182217
}
183218

@@ -186,7 +221,7 @@ static char *readFile(char *fileName)
186221
{
187222
fclose(fp);
188223
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);
190225
return NULL;
191226
}
192227

@@ -212,7 +247,7 @@ static bool setX509Certificate(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, char
212247
IoTHubClient_LL_SetOption(iotHubClientHandle, OPTION_X509_CERT, x509certificate) != IOTHUB_CLIENT_OK ||
213248
IoTHubClient_LL_SetOption(iotHubClientHandle, OPTION_X509_PRIVATE_KEY, x509privatekey) != IOTHUB_CLIENT_OK)
214249
{
215-
(void)printf("ERROR: Failed to set options for x509.\n");
250+
LogError("Failed to set options for x509.");
216251
return false;
217252
}
218253

@@ -226,7 +261,7 @@ int main(int argc, char *argv[])
226261
{
227262
if (argc < 2)
228263
{
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]);
230265
return 1;
231266
}
232267

@@ -237,7 +272,7 @@ int main(int argc, char *argv[])
237272

238273
if (device_id_src == NULL)
239274
{
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");
241276
return 1;
242277
}
243278
snprintf(device_id, sizeof(device_id), "%s", device_id_src);
@@ -247,13 +282,13 @@ int main(int argc, char *argv[])
247282

248283
if (platform_init() != 0)
249284
{
250-
(void)printf("Failed to initialize the platform.\r\n");
285+
LogError("Failed to initialize the platform.");
251286
}
252287
else
253288
{
254289
if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(argv[1], MQTT_Protocol)) == NULL)
255290
{
256-
(void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
291+
LogError("iotHubClientHandle is NULL!");
257292
}
258293
else
259294
{
@@ -269,6 +304,7 @@ int main(int argc, char *argv[])
269304
// set C2D and device method callback
270305
IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, receiveMessageCallback, NULL);
271306
IoTHubClient_LL_SetDeviceMethodCallback(iotHubClientHandle, deviceMethodCallback, NULL);
307+
IoTHubClient_LL_SetDeviceTwinCallback(iotHubClientHandle, twinCallback, NULL);
272308

273309
int count = 0;
274310
while (true)
@@ -286,10 +322,10 @@ int main(int argc, char *argv[])
286322
}
287323
else
288324
{
289-
(void)printf("Failed to read message\r\n");
325+
LogError("Failed to read message");
290326
}
291327
}
292-
delay(INTERVAL);
328+
delay(interval);
293329
}
294330
IoTHubClient_LL_DoWork(iotHubClientHandle);
295331
}

0 commit comments

Comments
 (0)