-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
342 lines (294 loc) · 11.5 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <stdio.h>
#include <stdlib.h>
#include "iothub_module_client_ll.h"
#include "iothub_client_options.h"
#include "iothub_message.h"
#include "azure_c_shared_utility/threadapi.h"
#include "azure_c_shared_utility/crt_abstractions.h"
#include "azure_c_shared_utility/platform.h"
#include "azure_c_shared_utility/shared_util_options.h"
#include "iothubtransportmqtt.h"
#include "iothub.h"
#include "time.h"
#include "parson.h"
static double temperatureThreshold = 25;
typedef struct MESSAGE_INSTANCE_TAG
{
IOTHUB_MESSAGE_HANDLE messageHandle;
size_t messageTrackingId; // For tracking the messages within the user callback.
}
MESSAGE_INSTANCE;
size_t messagesReceivedByInput1Queue = 0;
// SendConfirmationCallback is invoked when the message that was forwarded on from 'InputQueue1Callback'
// pipeline function is confirmed.
static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
{
// The context corresponds to which message# we were at when we sent.
MESSAGE_INSTANCE* messageInstance = (MESSAGE_INSTANCE*)userContextCallback;
printf("Confirmation[%zu] received for message with result = %d\r\n", messageInstance->messageTrackingId, result);
IoTHubMessage_Destroy(messageInstance->messageHandle);
free(messageInstance);
}
// Allocates a context for callback and clones the message
// NOTE: The message MUST be cloned at this stage. InputQueue1Callback's caller always frees the message
// so we need to pass down a new copy.
static MESSAGE_INSTANCE* CreateMessageInstance(IOTHUB_MESSAGE_HANDLE message)
{
MESSAGE_INSTANCE* messageInstance = (MESSAGE_INSTANCE*)malloc(sizeof(MESSAGE_INSTANCE));
/*
if (NULL == messageInstance)
{
printf("Failed allocating 'MESSAGE_INSTANCE' for pipelined message\r\n");
}
else
{
memset(messageInstance, 0, sizeof(*messageInstance));
if ((messageInstance->messageHandle = IoTHubMessage_Clone(message)) == NULL)
{
free(messageInstance);
messageInstance = NULL;
}
else
{
messageInstance->messageTrackingId = messagesReceivedByInput1Queue;
}
}*/
if ((messageInstance->messageHandle = IoTHubMessage_Clone(message)) == NULL)
{
free(messageInstance);
messageInstance = NULL;
}
else
{
messageInstance->messageTrackingId = messagesReceivedByInput1Queue;
MAP_HANDLE propMap = IoTHubMessage_Properties(messageInstance->messageHandle);
if (Map_AddOrUpdate(propMap, "MessageType", "Alert") != MAP_OK)
{
printf("ERROR: Map_AddOrUpdate Failed!\r\n");
}
}
return messageInstance;
}
static void moduleTwinCallback(DEVICE_TWIN_UPDATE_STATE update_state, const unsigned char* payLoad, size_t size, void* userContextCallback)
{
printf("\r\nTwin callback called with (state=%s, size=%zu):\r\n%s\r\n",
MU_ENUM_TO_STRING(DEVICE_TWIN_UPDATE_STATE, update_state), size, payLoad);
JSON_Value *root_value = json_parse_string(payLoad);
JSON_Object *root_object = json_value_get_object(root_value);
if (json_object_dotget_value(root_object, "desired.TemperatureThreshold") != NULL) {
temperatureThreshold = json_object_dotget_number(root_object, "desired.TemperatureThreshold");
}
if (json_object_get_value(root_object, "TemperatureThreshold") != NULL) {
temperatureThreshold = json_object_get_number(root_object, "TemperatureThreshold");
}
}
/*
static IOTHUBMESSAGE_DISPOSITION_RESULT InputQueue1Callback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
{
IOTHUBMESSAGE_DISPOSITION_RESULT result;
IOTHUB_CLIENT_RESULT clientResult;
IOTHUB_MODULE_CLIENT_LL_HANDLE iotHubModuleClientHandle = (IOTHUB_MODULE_CLIENT_LL_HANDLE)userContextCallback;
unsigned const char* messageBody;
size_t contentSize;
if (IoTHubMessage_GetByteArray(message, &messageBody, &contentSize) != IOTHUB_MESSAGE_OK)
{
messageBody = "<null>";
}
printf("Received Message [%zu]\r\n Data: [%s]\r\n",
messagesReceivedByInput1Queue, messageBody);
// This message should be sent to next stop in the pipeline, namely "output1". What happens at "outpu1" is determined
// by the configuration of the Edge routing table setup.
MESSAGE_INSTANCE *messageInstance = CreateMessageInstance(message);
if (NULL == messageInstance)
{
result = IOTHUBMESSAGE_ABANDONED;
}
else
{
printf("Sending message (%zu) to the next stage in pipeline\n", messagesReceivedByInput1Queue);
clientResult = IoTHubModuleClient_LL_SendEventToOutputAsync(iotHubModuleClientHandle, messageInstance->messageHandle, "output1", SendConfirmationCallback, (void *)messageInstance);
if (clientResult != IOTHUB_CLIENT_OK)
{
IoTHubMessage_Destroy(messageInstance->messageHandle);
free(messageInstance);
printf("IoTHubModuleClient_LL_SendEventToOutputAsync failed on sending msg#=%zu, err=%d\n", messagesReceivedByInput1Queue, clientResult);
result = IOTHUBMESSAGE_ABANDONED;
}
else
{
result = IOTHUBMESSAGE_ACCEPTED;
}
}
messagesReceivedByInput1Queue++;
return result;
}*/
static IOTHUBMESSAGE_DISPOSITION_RESULT InputQueue1Callback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
{
IOTHUBMESSAGE_DISPOSITION_RESULT result;
IOTHUB_CLIENT_RESULT clientResult;
IOTHUB_MODULE_CLIENT_LL_HANDLE iotHubModuleClientHandle = (IOTHUB_MODULE_CLIENT_LL_HANDLE)userContextCallback;
unsigned const char* messageBody;
size_t contentSize;
if (IoTHubMessage_GetByteArray(message, &messageBody, &contentSize) != IOTHUB_MESSAGE_OK)
{
messageBody = "<null>";
}
printf("Received Message [%zu]\r\n Data: [%s]\r\n",
messagesReceivedByInput1Queue, messageBody);
// Check if the message reports temperatures higher than the threshold
JSON_Value *root_value = json_parse_string(messageBody);
JSON_Object *root_object = json_value_get_object(root_value);
double temperature;
if (json_object_dotget_value(root_object, "machine.temperature") != NULL && (temperature = json_object_dotget_number(root_object, "machine.temperature")) > temperatureThreshold)
{
printf("Machine temperature %f exceeds threshold %f\r\n", temperature, temperatureThreshold);
// This message should be sent to next stop in the pipeline, namely "output1". What happens at "outpu1" is determined
// by the configuration of the Edge routing table setup.
MESSAGE_INSTANCE *messageInstance = CreateMessageInstance(message);
if (NULL == messageInstance)
{
result = IOTHUBMESSAGE_ABANDONED;
}
else
{
printf("Sending message (%zu) to the next stage in pipeline\n", messagesReceivedByInput1Queue);
clientResult = IoTHubModuleClient_LL_SendEventToOutputAsync(iotHubModuleClientHandle, messageInstance->messageHandle, "output1", SendConfirmationCallback, (void *)messageInstance);
if (clientResult != IOTHUB_CLIENT_OK)
{
IoTHubMessage_Destroy(messageInstance->messageHandle);
free(messageInstance);
printf("IoTHubModuleClient_LL_SendEventToOutputAsync failed on sending msg#=%zu, err=%d\n", messagesReceivedByInput1Queue, clientResult);
result = IOTHUBMESSAGE_ABANDONED;
}
else
{
result = IOTHUBMESSAGE_ACCEPTED;
}
}
}
else
{
printf("Not sending message (%zu) to the next stage in pipeline.\r\n", messagesReceivedByInput1Queue);
result = IOTHUBMESSAGE_ACCEPTED;
}
messagesReceivedByInput1Queue++;
return result;
}
static IOTHUB_MODULE_CLIENT_LL_HANDLE InitializeConnection()
{
IOTHUB_MODULE_CLIENT_LL_HANDLE iotHubModuleClientHandle;
if (IoTHub_Init() != 0)
{
printf("Failed to initialize the platform.\r\n");
iotHubModuleClientHandle = NULL;
}
else if ((iotHubModuleClientHandle = IoTHubModuleClient_LL_CreateFromEnvironment(MQTT_Protocol)) == NULL)
{
printf("ERROR: IoTHubModuleClient_LL_CreateFromEnvironment failed\r\n");
}
else
{
// Uncomment the following lines to enable verbose logging.
// bool traceOn = true;
// IoTHubModuleClient_LL_SetOption(iotHubModuleClientHandle, OPTION_LOG_TRACE, &trace);
}
return iotHubModuleClientHandle;
}
static void DeInitializeConnection(IOTHUB_MODULE_CLIENT_LL_HANDLE iotHubModuleClientHandle)
{
if (iotHubModuleClientHandle != NULL)
{
IoTHubModuleClient_LL_Destroy(iotHubModuleClientHandle);
}
IoTHub_Deinit();
}
/*
static int SetupCallbacksForModule(IOTHUB_MODULE_CLIENT_LL_HANDLE iotHubModuleClientHandle)
{
int ret;
if (IoTHubModuleClient_LL_SetInputMessageCallback(iotHubModuleClientHandle, "input1", InputQueue1Callback, (void*)iotHubModuleClientHandle) != IOTHUB_CLIENT_OK)
{
printf("ERROR: IoTHubModuleClient_LL_SetInputMessageCallback(\"input1\")..........FAILED!\r\n");
ret = 1;
}
else
{
ret = 0;
}
return ret;
}*/
static int deviceMethodCallback(const char* method_name, const unsigned char* payload, size_t size, unsigned char** response, size_t* response_size, void* userContextCallback)
{
(void)userContextCallback;
(void)payload;
(void)size;
int result;
if (strcmp("getCarVIN", method_name) == 0)
{
const char deviceMethodResponse[] = "{ \"Response\": \"1HGCM82633A004352\" }";
*response_size = sizeof(deviceMethodResponse)-1;
*response = malloc(*response_size);
(void)memcpy(*response, deviceMethodResponse, *response_size);
result = 200;
}
else
{
// All other entries are ignored.
const char deviceMethodResponse[] = "{ }";
*response_size = sizeof(deviceMethodResponse)-1;
*response = malloc(*response_size);
(void)memcpy(*response, deviceMethodResponse, *response_size);
result = -1;
}
return result;
}
static int SetupCallbacksForModule(IOTHUB_MODULE_CLIENT_LL_HANDLE iotHubModuleClientHandle)
{
int ret;
if (IoTHubModuleClient_LL_SetInputMessageCallback(iotHubModuleClientHandle, "input1", InputQueue1Callback, (void*)iotHubModuleClientHandle) != IOTHUB_CLIENT_OK)
{
printf("ERROR: IoTHubModuleClient_LL_SetInputMessageCallback(\"input1\")..........FAILED!\r\n");
ret = MU_FAILURE;
}
else if (IoTHubModuleClient_LL_SetModuleTwinCallback(iotHubModuleClientHandle, moduleTwinCallback, (void*)iotHubModuleClientHandle) != IOTHUB_CLIENT_OK)
{
printf("ERROR: IoTHubModuleClient_LL_SetModuleTwinCallback(default)..........FAILED!\r\n");
ret = MU_FAILURE;
}
else if(IoTHubModuleClient_LL_SetModuleMethodCallback(iotHubModuleClientHandle,deviceMethodCallback,NULL) != IOTHUB_CLIENT_OK)
{
printf("ERROR: IoTHubModuleClient_LL_SetModuleMethodCallback(default)..........FAILED!\r\n");
ret = MU_FAILURE;
}
else
{
ret = 0;
}
return ret;
}
void iothub_module()
{
IOTHUB_MODULE_CLIENT_LL_HANDLE iotHubModuleClientHandle;
srand((unsigned int)time(NULL));
char report_value[1024] = "{\"lastOilChangeDate\":\"2016\",\"maker\":{\"makerName\":\"Fabrikam\",\"style\":\"sedan\",\"year\":2014},\"state\":{\"reported_maxSpeed\":100,\"softwareVersion\":1,\"vanityPlate\":\"1I1\"}}";
if ((iotHubModuleClientHandle = InitializeConnection()) != NULL && SetupCallbacksForModule(iotHubModuleClientHandle) == 0)
{
// The receiver just loops constantly waiting for messages.
printf("Waiting for incoming messages.\r\n");
while (true)
{
printf("do_work.....................\n");
IoTHubModuleClient_LL_DoWork(iotHubModuleClientHandle,report_value,strlen(report_value),);
ThreadAPI_Sleep(100);
}
}
IoTHubModuleClient_SendReportedState(iotHubModuleClientHandle,);
DeInitializeConnection(iotHubModuleClientHandle);
}
int main(void)
{
iothub_module();
return 0;
}