@@ -56,115 +56,6 @@ static TX_EVENT_FLAGS_GROUP azure_iot_flags;
56
56
57
57
static int32_t telemetry_interval = 10 ;
58
58
59
- static void set_led_state (bool level )
60
- {
61
- if (level )
62
- {
63
- printf ("LED is turned ON\r\n" );
64
- HAL_GPIO_WritePin (GPIOC , GPIO_PIN_13 , GPIO_PIN_SET );
65
- }
66
- else
67
- {
68
- printf ("LED is turned OFF\r\n" );
69
- HAL_GPIO_WritePin (GPIOC , GPIO_PIN_13 , GPIO_PIN_RESET );
70
- }
71
- }
72
-
73
- static void direct_method_cb (AZURE_IOT_NX_CONTEXT * nx_context ,
74
- const UCHAR * method ,
75
- USHORT method_length ,
76
- UCHAR * payload ,
77
- USHORT payload_length ,
78
- VOID * context ,
79
- USHORT context_length )
80
- {
81
- UINT status ;
82
- UINT http_status = 501 ;
83
- CHAR * http_response = "{}" ;
84
-
85
- if (strncmp ((CHAR * )method , SET_LED_STATE_COMMAND , method_length ) == 0 )
86
- {
87
- bool arg = (strncmp ((CHAR * )payload , "true" , payload_length ) == 0 );
88
- set_led_state (arg );
89
-
90
- azure_iot_nx_client_publish_bool_property (& azure_iot_nx_client , LED_STATE_PROPERTY , arg );
91
-
92
- http_status = 200 ;
93
- }
94
-
95
- else if (strncmp ((CHAR * )method , SET_DISPLAY_TEXT_COMMAND , method_length ) == 0 )
96
- {
97
- // drop the first and last character to remove the quotes
98
- screen_printn ((CHAR * )payload + 1 , payload_length - 2 , L0 );
99
-
100
- http_status = 200 ;
101
- }
102
-
103
- if ((status = nx_azure_iot_hub_client_direct_method_message_response (& nx_context -> iothub_client ,
104
- http_status ,
105
- context ,
106
- context_length ,
107
- (UCHAR * )http_response ,
108
- strlen (http_response ),
109
- NX_WAIT_FOREVER )))
110
- {
111
- printf ("Direct method response failed! (0x%08x)\r\n" , status );
112
- return ;
113
- }
114
- }
115
-
116
- static void device_twin_desired_property_cb (UCHAR * component_name ,
117
- UINT component_name_len ,
118
- UCHAR * property_name ,
119
- UINT property_name_len ,
120
- NX_AZURE_IOT_JSON_READER property_value_reader ,
121
- UINT version ,
122
- VOID * userContextCallback )
123
- {
124
- UINT status ;
125
- AZURE_IOT_NX_CONTEXT * nx_context = (AZURE_IOT_NX_CONTEXT * )userContextCallback ;
126
-
127
- if (strncmp ((CHAR * )property_name , TELEMETRY_INTERVAL_PROPERTY , property_name_len ) == 0 )
128
- {
129
- status = nx_azure_iot_json_reader_token_int32_get (& property_value_reader , & telemetry_interval );
130
- if (status == NX_AZURE_IOT_SUCCESS )
131
- {
132
- // Set a telemetry event so we pick up the change immediately
133
- tx_event_flags_set (& azure_iot_flags , TELEMETRY_INTERVAL_EVENT , TX_OR );
134
-
135
- // Confirm reception back to hub
136
- azure_nx_client_respond_int_writeable_property (
137
- nx_context , TELEMETRY_INTERVAL_PROPERTY , telemetry_interval , 200 , version );
138
- }
139
- }
140
- }
141
-
142
- static void device_twin_property_cb (UCHAR * component_name ,
143
- UINT component_name_len ,
144
- UCHAR * property_name ,
145
- UINT property_name_len ,
146
- NX_AZURE_IOT_JSON_READER property_value_reader ,
147
- UINT version ,
148
- VOID * userContextCallback )
149
- {
150
- UINT status ;
151
- AZURE_IOT_NX_CONTEXT * nx_context = (AZURE_IOT_NX_CONTEXT * )userContextCallback ;
152
-
153
- if (strncmp ((CHAR * )property_name , TELEMETRY_INTERVAL_PROPERTY , property_name_len ) == 0 )
154
- {
155
- status = nx_azure_iot_json_reader_token_int32_get (& property_value_reader , & telemetry_interval );
156
- if (status == NX_AZURE_IOT_SUCCESS )
157
- {
158
- // Set a telemetry event so we pick up the change immediately
159
- tx_event_flags_set (& azure_iot_flags , TELEMETRY_INTERVAL_EVENT , TX_OR );
160
- }
161
- }
162
-
163
- // Confirm reception back to hub
164
- azure_nx_client_respond_int_writeable_property (
165
- nx_context , TELEMETRY_INTERVAL_PROPERTY , telemetry_interval , 200 , version );
166
- }
167
-
168
59
static UINT append_device_info_properties (NX_AZURE_IOT_JSON_WRITER * json_writer , VOID * context )
169
60
{
170
61
if (nx_azure_iot_json_writer_append_property_with_string_value (json_writer ,
@@ -321,6 +212,111 @@ static UINT append_device_telemetry_gyroscope(NX_AZURE_IOT_JSON_WRITER* json_wri
321
212
return NX_AZURE_IOT_SUCCESS ;
322
213
}
323
214
215
+ static void set_led_state (bool level )
216
+ {
217
+ if (level )
218
+ {
219
+ printf ("LED is turned ON\r\n" );
220
+ HAL_GPIO_WritePin (GPIOC , GPIO_PIN_13 , GPIO_PIN_SET );
221
+ }
222
+ else
223
+ {
224
+ printf ("LED is turned OFF\r\n" );
225
+ HAL_GPIO_WritePin (GPIOC , GPIO_PIN_13 , GPIO_PIN_RESET );
226
+ }
227
+ }
228
+
229
+ static void direct_method_cb (AZURE_IOT_NX_CONTEXT * nx_context ,
230
+ const UCHAR * method ,
231
+ USHORT method_length ,
232
+ UCHAR * payload ,
233
+ USHORT payload_length ,
234
+ VOID * context ,
235
+ USHORT context_length )
236
+ {
237
+ UINT status ;
238
+ UINT http_status = 501 ;
239
+ CHAR * http_response = "{}" ;
240
+
241
+ if (strncmp ((CHAR * )method , SET_LED_STATE_COMMAND , method_length ) == 0 )
242
+ {
243
+ bool arg = (strncmp ((CHAR * )payload , "true" , payload_length ) == 0 );
244
+ set_led_state (arg );
245
+
246
+ azure_iot_nx_client_publish_bool_property (& azure_iot_nx_client , LED_STATE_PROPERTY , arg );
247
+
248
+ http_status = 200 ;
249
+ }
250
+
251
+ else if (strncmp ((CHAR * )method , SET_DISPLAY_TEXT_COMMAND , method_length ) == 0 )
252
+ {
253
+ // drop the first and last character to remove the quotes
254
+ screen_printn ((CHAR * )payload + 1 , payload_length - 2 , L0 );
255
+
256
+ http_status = 200 ;
257
+ }
258
+
259
+ if ((status = nx_azure_iot_hub_client_direct_method_message_response (& nx_context -> iothub_client ,
260
+ http_status ,
261
+ context ,
262
+ context_length ,
263
+ (UCHAR * )http_response ,
264
+ strlen (http_response ),
265
+ NX_WAIT_FOREVER )))
266
+ {
267
+ printf ("Direct method response failed! (0x%08x)\r\n" , status );
268
+ return ;
269
+ }
270
+ }
271
+
272
+ static void device_twin_desired_property_cb (UCHAR * component_name ,
273
+ UINT component_name_len ,
274
+ UCHAR * property_name ,
275
+ UINT property_name_len ,
276
+ NX_AZURE_IOT_JSON_READER property_value_reader ,
277
+ UINT version ,
278
+ VOID * userContextCallback )
279
+ {
280
+ UINT status ;
281
+ AZURE_IOT_NX_CONTEXT * nx_context = (AZURE_IOT_NX_CONTEXT * )userContextCallback ;
282
+
283
+ if (strncmp ((CHAR * )property_name , TELEMETRY_INTERVAL_PROPERTY , property_name_len ) == 0 )
284
+ {
285
+ status = nx_azure_iot_json_reader_token_int32_get (& property_value_reader , & telemetry_interval );
286
+ if (status == NX_AZURE_IOT_SUCCESS )
287
+ {
288
+ // Confirm reception back to hub
289
+ azure_nx_client_respond_int_writeable_property (
290
+ nx_context , TELEMETRY_INTERVAL_PROPERTY , telemetry_interval , 200 , version );
291
+
292
+ // Set a telemetry event so we pick up the change immediately
293
+ tx_event_flags_set (& azure_iot_flags , TELEMETRY_INTERVAL_EVENT , TX_OR );
294
+ }
295
+ }
296
+ }
297
+
298
+ static void device_twin_property_cb (UCHAR * component_name ,
299
+ UINT component_name_len ,
300
+ UCHAR * property_name ,
301
+ UINT property_name_len ,
302
+ NX_AZURE_IOT_JSON_READER property_value_reader ,
303
+ UINT version ,
304
+ VOID * userContextCallback )
305
+ {
306
+ if (strncmp ((CHAR * )property_name , TELEMETRY_INTERVAL_PROPERTY , property_name_len ) == 0 )
307
+ {
308
+ nx_azure_iot_json_reader_token_int32_get (& property_value_reader , & telemetry_interval );
309
+ }
310
+ }
311
+
312
+ static void device_twin_received_cb (AZURE_IOT_NX_CONTEXT * nx_context )
313
+ {
314
+ azure_iot_nx_client_publish_int_writeable_property (nx_context , TELEMETRY_INTERVAL_PROPERTY , telemetry_interval );
315
+ azure_iot_nx_client_publish_bool_property (& azure_iot_nx_client , LED_STATE_PROPERTY , false);
316
+ azure_iot_nx_client_publish_properties (
317
+ & azure_iot_nx_client , DEVICE_INFO_COMPONENT_NAME , append_device_info_properties );
318
+ }
319
+
324
320
UINT azure_iot_nx_client_entry (
325
321
NX_IP * ip_ptr , NX_PACKET_POOL * pool_ptr , NX_DNS * dns_ptr , UINT (* unix_time_callback )(ULONG * unix_time ))
326
322
{
@@ -330,17 +326,17 @@ UINT azure_iot_nx_client_entry(
330
326
331
327
if ((status = tx_event_flags_create (& azure_iot_flags , "Azure IoT flags" )))
332
328
{
333
- printf ("FAIL: Unable to create nx_client event flags (0x%04x )\r\n" , status );
329
+ printf ("FAIL: Unable to create nx_client event flags (0x%08x )\r\n" , status );
334
330
return status ;
335
331
}
336
332
337
- status = azure_iot_nx_client_create (
338
- & azure_iot_nx_client , ip_ptr , pool_ptr , dns_ptr , unix_time_callback , IOT_MODEL_ID );
333
+ status =
334
+ azure_iot_nx_client_create ( & azure_iot_nx_client , ip_ptr , pool_ptr , dns_ptr , unix_time_callback , IOT_MODEL_ID );
339
335
if (status != NX_SUCCESS )
340
336
{
341
337
printf ("ERROR: azure_iot_nx_client_create failed (0x%08x)\r\n" , status );
342
338
return status ;
343
- }
339
+ }
344
340
345
341
#ifdef ENABLE_X509
346
342
status = azure_iot_nx_client_cert_set (& azure_iot_nx_client ,
@@ -372,6 +368,7 @@ UINT azure_iot_nx_client_entry(
372
368
azure_iot_nx_client_register_direct_method (& azure_iot_nx_client , direct_method_cb );
373
369
azure_iot_nx_client_register_device_twin_desired_prop (& azure_iot_nx_client , device_twin_desired_property_cb );
374
370
azure_iot_nx_client_register_device_twin_prop (& azure_iot_nx_client , device_twin_property_cb );
371
+ azure_iot_nx_client_register_device_twin_received (& azure_iot_nx_client , device_twin_received_cb );
375
372
376
373
if ((status = azure_iot_nx_client_connect (& azure_iot_nx_client )))
377
374
{
@@ -387,13 +384,9 @@ UINT azure_iot_nx_client_entry(
387
384
return status ;
388
385
}
389
386
390
- // Send properties
391
- azure_iot_nx_client_publish_bool_property (& azure_iot_nx_client , LED_STATE_PROPERTY , false);
392
- azure_iot_nx_client_publish_properties (
393
- & azure_iot_nx_client , DEVICE_INFO_COMPONENT_NAME , append_device_info_properties );
394
-
395
387
printf ("\r\nStarting Main loop\r\n" );
396
388
screen_print ("Azure IoT" , L0 );
389
+
397
390
while (true)
398
391
{
399
392
tx_event_flags_get (
0 commit comments