generated from gfurtadoalmeida/esp32-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_adu.c
403 lines (333 loc) · 17.2 KB
/
example_adu.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "examples/adu/example_adu.h"
#include "esp32_iot_azure/azure_iot_hub.h"
#include "esp32_iot_azure/azure_iot_adu.h"
#include "esp32_iot_azure/azure_iot_adu_workflow.h"
#include "esp32_iot_azure/extension/azure_iot_hub_extension.h"
#include "esp32_iot_azure/extension/azure_iot_hub_client_properties_extension.h"
#include "esp32_iot_azure/extension/azure_iot_json_writer_extension.h"
#include "esp32_iot_azure/extension/azure_iot_json_reader_extension.h"
#include "dtdl/temperaturecontroller.h"
typedef struct
{
azure_iot_hub_context_t *iot_hub;
azure_adu_context_t *adu;
azure_adu_workflow_t *adu_workflow;
buffer_t scratch_buffer;
temperature_controller_status_t device_status;
uint8_t display_brightness;
bool restart_command_called;
} example_context_t;
static const char TAG_EX_ADU[] = "EX_IOT_ADU";
static example_context_t EXAMPLE_CONTEXT = {
.adu = NULL,
.iot_hub = NULL,
.scratch_buffer = BUFFER_WITH_FIXED_LENGTH(700),
.device_status = TEMP_CONTROLLER_STATUS_NORMAL,
.display_brightness = 50,
.restart_command_called = false};
;
static void callback_cloud_properties_subscription(const AzureIoTHubClientPropertiesResponse_t *message, void *callback_context);
static AzureIoTResult_t device_report_initial_state(example_context_t *context);
static AzureIoTResult_t device_change_state(example_context_t *context, const AzureIoTHubClientPropertiesResponse_t *message, uint32_t *version);
static AzureIoTResult_t device_report_state_changed(example_context_t *context, uint32_t version);
bool example_adu_run(const buffer_t *iot_hub_hostname,
const buffer_t *device_id,
const buffer_t *device_symmetric_key)
{
bool success = false;
AzureIoTHubClientOptions_t *iot_client_options = NULL;
AzureIoTADUClientOptions_t *adu_client_options = NULL;
AzureIoTADUClientDeviceProperties_t adu_client_device_properties = {0};
buffer_t iot_hub_mqtt_buffer = {
.length = 4096,
.buffer = (uint8_t *)malloc(4096)};
buffer_t adu_op_buffer = {
.length = 3072,
.buffer = (uint8_t *)malloc(3072)};
buffer_t adu_down_buffer = {
.length = 4096 + ADU_WORKFLOW_DOWNLOAD_BUFFER_EXTRA_BYTES,
.buffer = (uint8_t *)malloc(4096 + ADU_WORKFLOW_DOWNLOAD_BUFFER_EXTRA_BYTES)};
azure_iot_hub_context_t *iot = azure_iot_hub_create(&iot_hub_mqtt_buffer);
azure_adu_context_t *adu = azure_adu_create(iot);
azure_adu_workflow_t *adu_workflow = azure_adu_workflow_create(adu, &adu_op_buffer);
example_context_t *example_context = &EXAMPLE_CONTEXT;
example_context->adu = adu;
example_context->adu_workflow = adu_workflow;
example_context->iot_hub = iot;
if (azure_iot_hub_options_init(iot, &iot_client_options) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure initializing IoT Hub options");
goto CLEAN_UP;
}
iot_client_options->pxComponentList = (AzureIoTHubClientComponent_t[3]){
azureiothubCREATE_COMPONENT(AZ_IOT_ADU_CLIENT_PROPERTIES_COMPONENT_NAME),
azureiothubCREATE_COMPONENT(TEMP_CTRL_CMP_DISPLAY_NAME),
azureiothubCREATE_COMPONENT(TEMP_CTRL_CMP_THERMOSTAT_NAME)};
iot_client_options->ulComponentListLength = 3;
if (azure_iot_hub_init(iot,
iot_hub_hostname->buffer,
(uint16_t)iot_hub_hostname->length,
device_id->buffer,
(uint16_t)device_id->length) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure initializing IoT Hub");
goto CLEAN_UP;
}
if (azure_iot_hub_auth_set_symmetric_key(iot, device_symmetric_key->buffer, device_symmetric_key->length) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure setting IoT Hub symmetric key");
goto CLEAN_UP;
}
if (azure_adu_options_init(adu, &adu_client_options) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure initializing Device Update options");
goto CLEAN_UP;
}
if (azure_adu_init(adu) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure initializing Device Update");
goto CLEAN_UP;
}
if (azure_adu_device_properties_init(&adu_client_device_properties) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure initializing Device Update device properties");
goto CLEAN_UP;
}
if (azure_iot_hub_connect(iot) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure connecting to IoT Hub");
goto CLEAN_UP;
}
if (azure_iot_hub_subscribe_properties(iot, &callback_cloud_properties_subscription, example_context) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure subscribing to properties");
goto CLEAN_UP;
}
if (azure_adu_workflow_init(adu_workflow, &adu_client_device_properties) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure initializing Device Update Workflow");
goto CLEAN_UP;
}
if (azure_iot_hub_request_properties_async(iot) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure requesting device properties document");
goto CLEAN_UP;
}
if (device_report_initial_state(example_context) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure reporting initial state");
goto CLEAN_UP;
}
buffer_t telemetry_payload = BUFFER_FROM_LITERAL("{\"temp\":25}");
while (true)
{
if (azure_iot_hub_send_telemetry_from_component(iot,
(uint8_t *)TEMP_CTRL_CMP_THERMOSTAT_PRP_TLY_TEMPERATURE_NAME,
sizeof_l(TEMP_CTRL_CMP_THERMOSTAT_PRP_TLY_TEMPERATURE_NAME),
telemetry_payload.buffer,
telemetry_payload.length,
eAzureIoTHubMessageQoS1,
NULL) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure sending telemetry");
}
if (azure_iot_hub_process_loop(iot) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure processing loop");
}
if (azure_adu_workflow_has_update(adu_workflow) && azure_adu_workflow_accept_update(adu_workflow, &adu_down_buffer, 4096, NULL, NULL) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure updating");
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
azure_iot_hub_unsubscribe_properties(iot);
success = true;
CLEAN_UP:
azure_adu_workflow_free(adu_workflow);
azure_adu_free(adu);
azure_iot_hub_disconnect(iot);
azure_iot_hub_deinit(iot);
azure_iot_hub_free(iot);
free(iot_hub_mqtt_buffer.buffer);
free(adu_op_buffer.buffer);
return success;
}
//
// SUBSCRIPTION CALLBACKS
//
static void callback_cloud_properties_subscription(const AzureIoTHubClientPropertiesResponse_t *message, void *callback_context)
{
example_context_t *context = (example_context_t *)callback_context;
uint32_t version = 0;
ESP_LOGI(TAG_EX_ADU, "message status: %d", message->xMessageStatus);
ESP_LOGI(TAG_EX_ADU, "message length: %lu", message->ulPayloadLength);
switch (message->xMessageType)
{
case eAzureIoTHubPropertiesRequestedMessage:
ESP_LOGI(TAG_EX_ADU, "azure_iot_hub_request_properties_async response = property document (desired + reported) sent by the server");
if (device_change_state(context, message, &version) == eAzureIoTSuccess)
{
ESP_LOGI(TAG_EX_ADU, "device state changed");
}
else
{
ESP_LOGE(TAG_EX_ADU, "failure changing device state");
}
break;
case eAzureIoTHubPropertiesWritablePropertyMessage:
ESP_LOGI(TAG_EX_ADU, "server wants to change a device property");
if (device_change_state(context, message, &version) == eAzureIoTSuccess)
{
ESP_LOGI(TAG_EX_ADU, "device state changed");
if (device_report_state_changed(context, version) == eAzureIoTSuccess)
{
ESP_LOGI(TAG_EX_ADU, "state change reported");
}
else
{
ESP_LOGE(TAG_EX_ADU, "failure reporting state change");
}
}
else
{
ESP_LOGE(TAG_EX_ADU, "failure changing device state");
}
break;
case eAzureIoTHubPropertiesReportedResponseMessage:
ESP_LOGI(TAG_EX_ADU, "azure_iot_hub_send_properties_reported response = properties value stored at the server or error.");
if (message->xMessageStatus != eAzureIoTStatusNoContent)
{
ESP_LOGW(TAG_EX_ADU, "failure reporting properties: %.*s", (int)message->ulPayloadLength, (const char *)message->pvMessagePayload);
}
break;
default:
ESP_LOGE(TAG_EX_ADU, "unknown property message");
}
}
//
// PROPERTY HANDLE
//
static AzureIoTResult_t device_report_initial_state(example_context_t *context)
{
AzureIoTJSONWriter_t json_writer;
AzureIoTHubClient_t *iot_client = azure_iot_hub_get_iot_client(context->iot_hub);
AZ_CHECK_BEGIN()
AZ_CHECK(AzureIoTJSONWriter_Init(&json_writer, context->scratch_buffer.buffer, context->scratch_buffer.length))
AZ_CHECK(AzureIoTJSONWriter_AppendBeginObject(&json_writer))
AZ_CHECK(AzureIoTJSONWriter_AppendPropertyWithInt32Value(&json_writer,
(uint8_t *)TEMP_CTRL_PRP_DEVICE_STATUS_NAME,
sizeof_l(TEMP_CTRL_PRP_DEVICE_STATUS_NAME),
context->device_status))
AZ_CHECK(AzureIoTHubClientProperties_BuilderBeginComponent(iot_client,
&json_writer,
(uint8_t *)TEMP_CTRL_CMP_DISPLAY_NAME,
sizeof_l(TEMP_CTRL_CMP_DISPLAY_NAME)))
AZ_CHECK(AzureIoTJSONWriter_AppendPropertyWithInt32Value(&json_writer,
(uint8_t *)TEMP_CTRL_CMP_DISPLAY_PRP_BRIGHTNESS_NAME,
sizeof_l(TEMP_CTRL_CMP_DISPLAY_PRP_BRIGHTNESS_NAME),
context->display_brightness))
AZ_CHECK(AzureIoTHubClientProperties_BuilderEndComponent(iot_client, &json_writer))
AZ_CHECK(AzureIoTJSONWriter_AppendEndObject(&json_writer))
int32_t payload_length = AzureIoTJSONWriter_GetBytesUsed(&json_writer);
AZ_CHECK(azure_iot_hub_send_properties_reported(context->iot_hub, context->scratch_buffer.buffer, payload_length, NULL))
AZ_CHECK_RETURN_LAST()
}
static AzureIoTResult_t device_change_state(example_context_t *context,
const AzureIoTHubClientPropertiesResponse_t *message,
uint32_t *version)
{
AzureIoTJSONReader_t json_reader;
AzureIoTHubClient_t *iot_client = azure_iot_hub_get_iot_client(context->iot_hub);
AZ_CHECK_BEGIN()
AZ_CHECK(AzureIoTJSONReader_Init(&json_reader, message->pvMessagePayload, message->ulPayloadLength))
if (AzureIoTHubClientProperties_GetPropertiesVersion(iot_client, &json_reader, message->xMessageType, version) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure getting property version");
}
else
{
const uint8_t *component_name = NULL;
uint32_t component_name_length = 0;
ESP_LOGI(TAG_EX_ADU, "desired property version: %lu", *version);
// Reset JSON reader to the beginning.
AZ_CHECK(AzureIoTJSONReader_Init(&json_reader, message->pvMessagePayload, message->ulPayloadLength))
while (AzureIoTHubClientProperties_GetNextComponentProperty(iot_client,
&json_reader,
message->xMessageType,
eAzureIoTHubClientPropertyWritable,
&component_name,
&component_name_length) == eAzureIoTSuccess)
{
// We're expecting properties from a component.
// We have to skip over the root property and value to continue iterating.
if (component_name_length == 0)
{
ESP_LOGW(TAG_EX_ADU, "unknown root property (we expect a component): %.*s", (int)component_name_length, component_name);
AZ_CHECK(AzureIoTJSONReader_SkipPropertyAndValue(&json_reader))
continue;
}
if (azure_adu_is_adu_component(context->adu, component_name, component_name_length))
{
AZ_CHECK(azure_adu_workflow_process_update_request(context->adu_workflow,
&json_reader,
*version))
continue;
}
// We're expecting a "display" component.
// We have to skip over the root property and value to continue iterating.
if (strncasecmp(TEMP_CTRL_CMP_DISPLAY_NAME, (const char *)component_name, sizeof_l(TEMP_CTRL_CMP_DISPLAY_NAME)) != 0)
{
ESP_LOGW(TAG_EX_ADU, "unknown component: %.*s", (int)component_name_length, component_name);
AZ_CHECK(AzureIoTJSONReader_SkipPropertyAndValue(&json_reader))
continue;
}
if (AzureIoTJSONReader_TokenIsTextEqual(&json_reader,
(uint8_t *)TEMP_CTRL_CMP_DISPLAY_PRP_BRIGHTNESS_NAME,
sizeof_l(TEMP_CTRL_CMP_DISPLAY_PRP_BRIGHTNESS_NAME)))
{
AZ_CHECK(AzureIoTJSONReader_NextToken(&json_reader))
AZ_CHECK(AzureIoTJSONReader_GetTokenUInt8(&json_reader, &context->display_brightness))
AZ_CHECK(AzureIoTJSONReader_NextToken(&json_reader))
ESP_LOGI(TAG_EX_ADU, "display.brightness received: %d", context->display_brightness);
}
}
}
AZ_CHECK_RETURN_LAST()
}
static AzureIoTResult_t device_report_state_changed(example_context_t *context, uint32_t version)
{
AzureIoTJSONWriter_t json_writer;
AzureIoTHubClient_t *iot_client = azure_iot_hub_get_iot_client(context->iot_hub);
AZ_CHECK_BEGIN()
AZ_CHECK(AzureIoTJSONWriter_Init(&json_writer, context->scratch_buffer.buffer, context->scratch_buffer.length))
AZ_CHECK(AzureIoTJSONWriter_AppendBeginObject(&json_writer))
AZ_CHECK(AzureIoTHubClientProperties_BuilderBeginComponent(iot_client,
&json_writer,
(uint8_t *)TEMP_CTRL_CMP_DISPLAY_NAME,
sizeof_l(TEMP_CTRL_CMP_DISPLAY_NAME)))
AZ_CHECK(AzureIoTHubClientProperties_BuilderBeginResponseStatus(iot_client,
&json_writer,
(uint8_t *)TEMP_CTRL_CMP_DISPLAY_PRP_BRIGHTNESS_NAME,
sizeof_l(TEMP_CTRL_CMP_DISPLAY_PRP_BRIGHTNESS_NAME),
200,
version,
(uint8_t *)"success",
sizeof("success") - 1))
AZ_CHECK(AzureIoTJSONWriter_AppendInt32(&json_writer, context->display_brightness))
AZ_CHECK(AzureIoTHubClientProperties_BuilderEndResponseStatus(iot_client, &json_writer))
AZ_CHECK(AzureIoTHubClientProperties_BuilderEndComponent(iot_client, &json_writer))
AZ_CHECK(AzureIoTJSONWriter_AppendEndObject(&json_writer))
int32_t payload_length = AzureIoTJSONWriter_GetBytesUsed(&json_writer);
if ((AZ_CHECK_RESULT_VAR = azure_iot_hub_send_properties_reported(context->iot_hub,
context->scratch_buffer.buffer,
payload_length,
NULL)) != eAzureIoTSuccess)
{
ESP_LOGE(TAG_EX_ADU, "failure reporting properties: 0x%08x", AZ_CHECK_RESULT_VAR);
}
AZ_CHECK_RETURN_LAST()
}