-
Notifications
You must be signed in to change notification settings - Fork 7
/
iothub_client_sample_mqtt.c
286 lines (251 loc) · 11.5 KB
/
iothub_client_sample_mqtt.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
// 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_client.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 "iothubtransportmqtt.h"
#ifdef MBED_BUILD_TIMESTAMP
#include "certs.h"
#endif // MBED_BUILD_TIMESTAMP
/*String containing Hostname, Device Id & Device Key in the format: */
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>" */
static const char* connectionString = "[azure connectionString]";
static int callbackCounter;
static char msgText[1024];
static char propText[1024];
static bool g_continueRunning;
static bool g_resend;
#define MESSAGE_COUNT 500
#define DOWORK_LOOP_NUM 3
typedef struct EVENT_INSTANCE_TAG
{
IOTHUB_MESSAGE_HANDLE messageHandle;
size_t messageTrackingId; // For tracking the messages within the user callback.
} EVENT_INSTANCE;
static unsigned char* bytearray_to_str(const unsigned char *buffer, size_t len)
{
unsigned char* ret = (unsigned char*)malloc(len+1);
memcpy(ret, buffer, len);
ret[len] = '\0';
return ret;
}
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
{
int* counter = (int*)userContextCallback;
const char* buffer;
size_t size;
if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK)
{
(void)printf("unable to retrieve the message data\r\n");
}
else
{
unsigned char* message_string = bytearray_to_str(buffer, size);
(void)printf("IoTHubMessage_GetByteArray received message: \"%s\" \n", message_string);
free(message_string);
// If we receive the word 'quit' then we stop running
if (size == (strlen("quit") * sizeof(char)) && memcmp(buffer, "quit", size) == 0)
{
g_continueRunning = false;
}
}
// Retrieve properties from the message
MAP_HANDLE mapProperties = IoTHubMessage_Properties(message);
if (mapProperties != NULL)
{
const char*const* keys;
const char*const* values;
size_t propertyCount = 0;
if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK)
{
if (propertyCount > 0)
{
size_t index = 0;
for (index = 0; index < propertyCount; index++)
{
//(void)printf("\tKey: %s Value: %s\r\n", keys[index], values[index]);
}
//(void)printf("\r\n");
}
}
}
/* Some device specific action code goes here... */
(*counter)++;
return IOTHUBMESSAGE_ACCEPTED;
}
static int DeviceMethodCallback(const char* method_name, const unsigned char* payload, size_t size, unsigned char** response, size_t* resp_size, void* userContextCallback)
{
(void)userContextCallback;
printf("\r\nDevice Method called\r\n");
printf("Device Method name: %s\r\n", method_name);
printf("Device Method payload: %.*s\r\n", (int)size, (const char*)payload);
int status = 200;
char* RESPONSE_STRING = "{ \"Response\": \"This is the response from the device\" }";
printf("\r\nResponse status: %d\r\n", status);
printf("Response payload: %s\r\n\r\n", RESPONSE_STRING);
*resp_size = strlen(RESPONSE_STRING);
if ((*response = malloc(*resp_size)) == NULL)
{
status = -1;
}
else
{
memcpy(*response, RESPONSE_STRING, *resp_size);
}
//g_continueRunning = false;
return status;
}
static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
{
EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
size_t id = eventInstance->messageTrackingId;
(void)printf("Confirmation[%d] received for message tracking id = %d with result = %s\r\n", callbackCounter, (int)id, ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
/* Some device specific action code goes here... */
if (result != IOTHUB_CLIENT_CONFIRMATION_OK)
{
g_resend = true;
(void)printf("g_resend is true \n");
}
else
{
g_resend = false;
callbackCounter++;
IoTHubMessage_Destroy(eventInstance->messageHandle);
(void)printf("messageHandle destroyed \n");
}
}
void iothub_client_sample_mqtt_run(void)
{
//(void)printf("size after iothub_client_sample_mqtt_run starts: %d \n", system_get_free_heap_size());
IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle;
EVENT_INSTANCE messages[MESSAGE_COUNT];
g_continueRunning = true;
g_resend = false;
//srand((unsigned int)time(NULL));
double avgWindSpeed = 10.0;
callbackCounter = 0;
int receiveContext = 0;
//(void)printf("size before platform_init: %d \n", system_get_free_heap_size());
if (platform_init() != 0)
{
(void)printf("Failed to initialize the platform.\r\n");
}
else
{
//(void)printf("size before IoTHubClient_LL_CreateFromConnectionString: %d \n", system_get_free_heap_size());
if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol)) == NULL)
{
(void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
}
else
{
bool traceOn = true;
//(void)printf("size before IoTHubClient_LL_SetOption: %d \n", system_get_free_heap_size());
IoTHubClient_LL_SetOption(iotHubClientHandle, "logtrace", &traceOn);
#ifdef MBED_BUILD_TIMESTAMP
// For mbed add the certificate information
if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
{
printf("failure to set option \"TrustedCerts\"\r\n");
}
#endif // MBED_BUILD_TIMESTAMP
/* Setting Message call back, so we can receive Commands. */
//(void)printf("size before IoTHubClient_LL_SetMessageCallback: %d \n", system_get_free_heap_size());
if (IoTHubClient_LL_SetDeviceMethodCallback(iotHubClientHandle, DeviceMethodCallback, &receiveContext) != IOTHUB_CLIENT_OK)
{
(void)printf("ERROR: IoTHubClient_LL_SetDeviceMethodCallback..........FAILED!\r\n");
}
if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK)
{
(void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n");
}
else
{
//(void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n");
/* Now that we are ready to receive commands, let's send some messages */
size_t iterator = 0;
int n = 0;
do
{
if (iterator < MESSAGE_COUNT && ((iterator<= callbackCounter) || g_resend == true))
{
if (g_resend == false)
{
int index = 0;
index = n*500 + (int)iterator;
sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"myESP8266Device_%d\",\"windSpeed\":%.2f}", index, avgWindSpeed + (rand() % 4 + 2));
//(void)printf("size before IoTHubMessage_CreateFromByteArray: %d \n", system_get_free_heap_size());
if ((messages[iterator].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
{
(void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
}
else
{
messages[iterator].messageTrackingId = iterator;
//(void)printf("size before IoTHubMessage_Properties: %d \n", system_get_free_heap_size());
MAP_HANDLE propMap = IoTHubMessage_Properties(messages[iterator].messageHandle);
(void)sprintf_s(propText, sizeof(propText), "PropMsg_%zu", iterator);
if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
{
(void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
}
//(void)printf("free heap size before IoTHubClient_LL_SendEventAsync: %d \n", system_get_free_heap_size());
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator].messageHandle, SendConfirmationCallback, &messages[iterator]) != IOTHUB_CLIENT_OK)
{
(void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
}
else
{
(void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", (int)iterator);
}
}
iterator++;
}
else
{
(void)printf("message [%d] needs to be resent to IoT Hub.\r\n", (int)(iterator-1));
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator-1].messageHandle, SendConfirmationCallback, &messages[iterator-1]) != IOTHUB_CLIENT_OK)
{
(void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
}
else
{
(void)printf("IoTHubClient_LL_SendEventAsync accepted to resend message [%d] for transmission to IoT Hub.\r\n", (int)(iterator-1));
}
g_resend = false;
}
}
IoTHubClient_LL_DoWork(iotHubClientHandle);
//(void)printf("rita: IoTHubClient_LL_DoWork finished!\r\n");
ThreadAPI_Sleep(1);
if (callbackCounter>=MESSAGE_COUNT){
//break;
n++;
printf("%dth round \n", n);
callbackCounter = 0;
iterator = 0;
}
} while (g_continueRunning);
(void)printf("iothub_client_sample_mqtt has gotten quit message, call DoWork %d more time to complete final sending...\r\n", DOWORK_LOOP_NUM);
size_t index = 0;
for (index = 0; index < DOWORK_LOOP_NUM; index++)
{
IoTHubClient_LL_DoWork(iotHubClientHandle);
ThreadAPI_Sleep(1);
}
}
IoTHubClient_LL_Destroy(iotHubClientHandle);
}
platform_deinit();
}
}
int main(void)
{
iothub_client_sample_mqtt_run();
return 0;
}