15
15
#include "nx_azure_iot_provisioning_client.h"
16
16
17
17
#include "azure_iot_nx_client.h"
18
- #include "nx_azure_iot_pnp_helpers.h"
19
18
20
19
#include "azure_config.h"
21
20
#include "azure_device_x509_cert_config.h"
@@ -57,7 +56,7 @@ static TX_EVENT_FLAGS_GROUP azure_iot_flags;
57
56
58
57
static int32_t telemetry_interval = 10 ;
59
58
60
- static UINT append_device_info_properties (NX_AZURE_IOT_JSON_WRITER * json_writer , VOID * context )
59
+ static UINT append_device_info_properties (NX_AZURE_IOT_JSON_WRITER * json_writer )
61
60
{
62
61
if (nx_azure_iot_json_writer_append_property_with_string_value (json_writer ,
63
62
(UCHAR * )DEVICE_INFO_MANUFACTURER_PROPERTY_NAME ,
@@ -106,7 +105,7 @@ static UINT append_device_info_properties(NX_AZURE_IOT_JSON_WRITER* json_writer,
106
105
return NX_AZURE_IOT_SUCCESS ;
107
106
}
108
107
109
- static UINT append_device_telemetry (NX_AZURE_IOT_JSON_WRITER * json_writer , VOID * context )
108
+ static UINT append_device_telemetry (NX_AZURE_IOT_JSON_WRITER * json_writer )
110
109
{
111
110
lps22hb_t lps22hb_data = lps22hb_data_read ();
112
111
hts221_data_t hts221_data = hts221_data_read ();
@@ -127,7 +126,7 @@ static UINT append_device_telemetry(NX_AZURE_IOT_JSON_WRITER* json_writer, VOID*
127
126
return NX_AZURE_IOT_SUCCESS ;
128
127
}
129
128
130
- static UINT append_device_telemetry_magnetometer (NX_AZURE_IOT_JSON_WRITER * json_writer , VOID * context )
129
+ static UINT append_device_telemetry_magnetometer (NX_AZURE_IOT_JSON_WRITER * json_writer )
131
130
{
132
131
lis2mdl_data_t lis2mdl_data = lis2mdl_data_read ();
133
132
@@ -153,7 +152,7 @@ static UINT append_device_telemetry_magnetometer(NX_AZURE_IOT_JSON_WRITER* json_
153
152
return NX_AZURE_IOT_SUCCESS ;
154
153
}
155
154
156
- static UINT append_device_telemetry_accelerometer (NX_AZURE_IOT_JSON_WRITER * json_writer , VOID * context )
155
+ static UINT append_device_telemetry_accelerometer (NX_AZURE_IOT_JSON_WRITER * json_writer )
157
156
{
158
157
lsm6dsl_data_t lsm6dsl_data = lsm6dsl_data_read ();
159
158
@@ -179,7 +178,7 @@ static UINT append_device_telemetry_accelerometer(NX_AZURE_IOT_JSON_WRITER* json
179
178
return NX_AZURE_IOT_SUCCESS ;
180
179
}
181
180
182
- static UINT append_device_telemetry_gyroscope (NX_AZURE_IOT_JSON_WRITER * json_writer , VOID * context )
181
+ static UINT append_device_telemetry_gyroscope (NX_AZURE_IOT_JSON_WRITER * json_writer )
183
182
{
184
183
lsm6dsl_data_t lsm6dsl_data = lsm6dsl_data_read ();
185
184
@@ -209,77 +208,83 @@ static void set_led_state(bool level)
209
208
{
210
209
if (level )
211
210
{
212
- printf ("LED is turned ON\r\n" );
211
+ printf ("\tLED is turned ON\r\n" );
213
212
HAL_GPIO_WritePin (GPIOC , GPIO_PIN_13 , GPIO_PIN_SET );
214
213
}
215
214
else
216
215
{
217
- printf ("LED is turned OFF\r\n" );
216
+ printf ("\tLED is turned OFF\r\n" );
218
217
HAL_GPIO_WritePin (GPIOC , GPIO_PIN_13 , GPIO_PIN_RESET );
219
218
}
220
219
}
221
220
222
- static void direct_method_cb (AZURE_IOT_NX_CONTEXT * nx_context ,
221
+ static void command_received_cb (AZURE_IOT_NX_CONTEXT * nx_context_ptr ,
223
222
const UCHAR * method ,
224
223
USHORT method_length ,
225
224
UCHAR * payload ,
226
225
USHORT payload_length ,
227
- VOID * context ,
226
+ VOID * context_ptr ,
228
227
USHORT context_length )
229
228
{
230
229
UINT status ;
231
- UINT http_status = 501 ;
232
- CHAR * http_response = "{}" ;
233
230
234
231
if (strncmp ((CHAR * )method , SET_LED_STATE_COMMAND , method_length ) == 0 )
235
232
{
236
233
bool arg = (strncmp ((CHAR * )payload , "true" , payload_length ) == 0 );
237
234
set_led_state (arg );
238
235
239
- azure_iot_nx_client_publish_bool_property (& azure_iot_nx_client , LED_STATE_PROPERTY , arg );
236
+ if ((status = nx_azure_iot_hub_client_command_message_response (
237
+ & nx_context_ptr -> iothub_client , 200 , context_ptr , context_length , NULL , 0 , NX_WAIT_FOREVER )))
238
+ {
239
+ printf ("Direct method response failed! (0x%08x)\r\n" , status );
240
+ return ;
241
+ }
240
242
241
- http_status = 200 ;
243
+ azure_iot_nx_client_publish_bool_property ( & azure_iot_nx_client , LED_STATE_PROPERTY , arg ) ;
242
244
}
243
-
244
245
else if (strncmp ((CHAR * )method , SET_DISPLAY_TEXT_COMMAND , method_length ) == 0 )
245
246
{
246
247
// drop the first and last character to remove the quotes
247
248
screen_printn ((CHAR * )payload + 1 , payload_length - 2 , L0 );
248
-
249
- http_status = 200 ;
249
+ if ((status = nx_azure_iot_hub_client_command_message_response (
250
+ & nx_context_ptr -> iothub_client , 200 , context_ptr , context_length , NULL , 0 , NX_WAIT_FOREVER )))
251
+ {
252
+ printf ("Direct method response failed! (0x%08x)\r\n" , status );
253
+ return ;
254
+ }
250
255
}
251
-
252
- if ((status = nx_azure_iot_hub_client_direct_method_message_response (& nx_context -> iothub_client ,
253
- http_status ,
254
- context ,
255
- context_length ,
256
- (UCHAR * )http_response ,
257
- strlen (http_response ),
258
- NX_WAIT_FOREVER )))
256
+ else
259
257
{
260
- printf ("Direct method response failed! (0x%08x)\r\n" , status );
261
- return ;
258
+ printf ("Direct method is not for this device\r\n" );
259
+
260
+ if ((status = nx_azure_iot_hub_client_command_message_response (
261
+ & nx_context_ptr -> iothub_client , 501 , context_ptr , context_length , NULL , 0 , NX_WAIT_FOREVER )))
262
+ {
263
+ printf ("Direct method response failed! (0x%08x)\r\n" , status );
264
+ return ;
265
+ }
262
266
}
263
267
}
264
268
265
- static void device_twin_desired_property_cb (UCHAR * component_name ,
269
+ static void writable_property_received_cb (AZURE_IOT_NX_CONTEXT * nx_context ,
270
+ const UCHAR * component_name ,
266
271
UINT component_name_len ,
267
272
UCHAR * property_name ,
268
273
UINT property_name_len ,
269
- NX_AZURE_IOT_JSON_READER property_value_reader ,
270
- UINT version ,
271
- VOID * userContextCallback )
274
+ NX_AZURE_IOT_JSON_READER * json_reader_ptr ,
275
+ UINT version )
272
276
{
273
277
UINT status ;
274
- AZURE_IOT_NX_CONTEXT * nx_context = (AZURE_IOT_NX_CONTEXT * )userContextCallback ;
275
278
276
279
if (strncmp ((CHAR * )property_name , TELEMETRY_INTERVAL_PROPERTY , property_name_len ) == 0 )
277
280
{
278
- status = nx_azure_iot_json_reader_token_int32_get (& property_value_reader , & telemetry_interval );
281
+ status = nx_azure_iot_json_reader_token_int32_get (json_reader_ptr , & telemetry_interval );
279
282
if (status == NX_AZURE_IOT_SUCCESS )
280
283
{
284
+ printf ("\tUpdating %s to %ld\r\n" , TELEMETRY_INTERVAL_PROPERTY , telemetry_interval );
285
+
281
286
// Confirm reception back to hub
282
- azure_nx_client_respond_int_writeable_property (
287
+ azure_nx_client_respond_int_writable_property (
283
288
nx_context , TELEMETRY_INTERVAL_PROPERTY , telemetry_interval , 200 , version );
284
289
285
290
// Set a telemetry event so we pick up the change immediately
@@ -288,17 +293,23 @@ static void device_twin_desired_property_cb(UCHAR* component_name,
288
293
}
289
294
}
290
295
291
- static void device_twin_property_cb (UCHAR * component_name ,
296
+ static void property_received_cb (AZURE_IOT_NX_CONTEXT * context ,
297
+ const UCHAR * component_name ,
292
298
UINT component_name_len ,
293
299
UCHAR * property_name ,
294
300
UINT property_name_len ,
295
- NX_AZURE_IOT_JSON_READER property_value_reader ,
296
- UINT version ,
297
- VOID * userContextCallback )
301
+ NX_AZURE_IOT_JSON_READER * json_reader_ptr ,
302
+ UINT version )
298
303
{
304
+ UINT status ;
305
+
299
306
if (strncmp ((CHAR * )property_name , TELEMETRY_INTERVAL_PROPERTY , property_name_len ) == 0 )
300
307
{
301
- nx_azure_iot_json_reader_token_int32_get (& property_value_reader , & telemetry_interval );
308
+ status = nx_azure_iot_json_reader_token_int32_get (json_reader_ptr , & telemetry_interval );
309
+ if (status == NX_AZURE_IOT_SUCCESS )
310
+ {
311
+ printf ("\tUpdating %s to %ld\r\n" , TELEMETRY_INTERVAL_PROPERTY , telemetry_interval );
312
+ }
302
313
}
303
314
}
304
315
@@ -350,25 +361,25 @@ UINT azure_iot_nx_client_entry(
350
361
}
351
362
352
363
// Register the callbacks
353
- azure_iot_nx_client_register_direct_method (& azure_iot_nx_client , direct_method_cb );
354
- azure_iot_nx_client_register_device_twin_desired_prop (& azure_iot_nx_client , device_twin_desired_property_cb );
355
- azure_iot_nx_client_register_device_twin_prop (& azure_iot_nx_client , device_twin_property_cb );
364
+ azure_iot_nx_client_register_command_callback (& azure_iot_nx_client , command_received_cb );
365
+ azure_iot_nx_client_register_writable_property_callback (& azure_iot_nx_client , writable_property_received_cb );
366
+ azure_iot_nx_client_register_property_callback (& azure_iot_nx_client , property_received_cb );
356
367
357
368
if ((status = azure_iot_nx_client_connect (& azure_iot_nx_client )))
358
369
{
359
370
printf ("ERROR: failed to connect nx client (0x%08x)\r\n" , status );
360
371
return status ;
361
372
}
362
373
363
- // Request the device twin for writeable property update
364
- if ((status = azure_iot_nx_client_device_twin_request_and_wait (& azure_iot_nx_client )))
374
+ // Request the device twin for writable property update
375
+ if ((status = azure_iot_nx_client_properties_request_and_wait (& azure_iot_nx_client )))
365
376
{
366
- printf ("ERROR: azure_iot_nx_client_device_twin_request_and_wait failed (0x%08x)\r\n" , status );
377
+ printf ("ERROR: azure_iot_nx_client_properties_request_and_wait failed (0x%08x)\r\n" , status );
367
378
return status ;
368
379
}
369
380
370
381
// Send out property updates
371
- azure_iot_nx_client_publish_int_writeable_property (
382
+ azure_iot_nx_client_publish_int_writable_property (
372
383
& azure_iot_nx_client , TELEMETRY_INTERVAL_PROPERTY , telemetry_interval );
373
384
azure_iot_nx_client_publish_bool_property (& azure_iot_nx_client , LED_STATE_PROPERTY , false);
374
385
azure_iot_nx_client_publish_properties (
@@ -385,19 +396,20 @@ UINT azure_iot_nx_client_entry(
385
396
switch (telemetry_state )
386
397
{
387
398
case TELEMETRY_STATE_DEFAULT :
388
- azure_iot_nx_client_publish_telemetry (& azure_iot_nx_client , append_device_telemetry );
399
+ azure_iot_nx_client_publish_telemetry (& azure_iot_nx_client , NULL , append_device_telemetry );
389
400
break ;
390
401
391
402
case TELEMETRY_STATE_MAGNETOMETER :
392
- azure_iot_nx_client_publish_telemetry (& azure_iot_nx_client , append_device_telemetry_magnetometer );
403
+ azure_iot_nx_client_publish_telemetry (& azure_iot_nx_client , NULL , append_device_telemetry_magnetometer );
393
404
break ;
394
405
395
406
case TELEMETRY_STATE_ACCELEROMETER :
396
- azure_iot_nx_client_publish_telemetry (& azure_iot_nx_client , append_device_telemetry_accelerometer );
407
+ azure_iot_nx_client_publish_telemetry (
408
+ & azure_iot_nx_client , NULL , append_device_telemetry_accelerometer );
397
409
break ;
398
410
399
411
case TELEMETRY_STATE_GYROSCOPE :
400
- azure_iot_nx_client_publish_telemetry (& azure_iot_nx_client , append_device_telemetry_gyroscope );
412
+ azure_iot_nx_client_publish_telemetry (& azure_iot_nx_client , NULL , append_device_telemetry_gyroscope );
401
413
break ;
402
414
403
415
default :
0 commit comments