1515#include "nx_azure_iot_provisioning_client.h"
1616
1717#include "azure_iot_nx_client.h"
18- #include "nx_azure_iot_pnp_helpers.h"
1918
2019#include "azure_config.h"
2120#include "azure_device_x509_cert_config.h"
@@ -57,7 +56,7 @@ static TX_EVENT_FLAGS_GROUP azure_iot_flags;
5756
5857static int32_t telemetry_interval = 10 ;
5958
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 )
6160{
6261 if (nx_azure_iot_json_writer_append_property_with_string_value (json_writer ,
6362 (UCHAR * )DEVICE_INFO_MANUFACTURER_PROPERTY_NAME ,
@@ -106,7 +105,7 @@ static UINT append_device_info_properties(NX_AZURE_IOT_JSON_WRITER* json_writer,
106105 return NX_AZURE_IOT_SUCCESS ;
107106}
108107
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 )
110109{
111110 lps22hb_t lps22hb_data = lps22hb_data_read ();
112111 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*
127126 return NX_AZURE_IOT_SUCCESS ;
128127}
129128
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 )
131130{
132131 lis2mdl_data_t lis2mdl_data = lis2mdl_data_read ();
133132
@@ -153,7 +152,7 @@ static UINT append_device_telemetry_magnetometer(NX_AZURE_IOT_JSON_WRITER* json_
153152 return NX_AZURE_IOT_SUCCESS ;
154153}
155154
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 )
157156{
158157 lsm6dsl_data_t lsm6dsl_data = lsm6dsl_data_read ();
159158
@@ -179,7 +178,7 @@ static UINT append_device_telemetry_accelerometer(NX_AZURE_IOT_JSON_WRITER* json
179178 return NX_AZURE_IOT_SUCCESS ;
180179}
181180
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 )
183182{
184183 lsm6dsl_data_t lsm6dsl_data = lsm6dsl_data_read ();
185184
@@ -209,77 +208,83 @@ static void set_led_state(bool level)
209208{
210209 if (level )
211210 {
212- printf ("LED is turned ON\r\n" );
211+ printf ("\tLED is turned ON\r\n" );
213212 HAL_GPIO_WritePin (GPIOC , GPIO_PIN_13 , GPIO_PIN_SET );
214213 }
215214 else
216215 {
217- printf ("LED is turned OFF\r\n" );
216+ printf ("\tLED is turned OFF\r\n" );
218217 HAL_GPIO_WritePin (GPIOC , GPIO_PIN_13 , GPIO_PIN_RESET );
219218 }
220219}
221220
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 ,
223222 const UCHAR * method ,
224223 USHORT method_length ,
225224 UCHAR * payload ,
226225 USHORT payload_length ,
227- VOID * context ,
226+ VOID * context_ptr ,
228227 USHORT context_length )
229228{
230229 UINT status ;
231- UINT http_status = 501 ;
232- CHAR * http_response = "{}" ;
233230
234231 if (strncmp ((CHAR * )method , SET_LED_STATE_COMMAND , method_length ) == 0 )
235232 {
236233 bool arg = (strncmp ((CHAR * )payload , "true" , payload_length ) == 0 );
237234 set_led_state (arg );
238235
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+ }
240242
241- http_status = 200 ;
243+ azure_iot_nx_client_publish_bool_property ( & azure_iot_nx_client , LED_STATE_PROPERTY , arg ) ;
242244 }
243-
244245 else if (strncmp ((CHAR * )method , SET_DISPLAY_TEXT_COMMAND , method_length ) == 0 )
245246 {
246247 // drop the first and last character to remove the quotes
247248 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+ }
250255 }
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
259257 {
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+ }
262266 }
263267}
264268
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 ,
266271 UINT component_name_len ,
267272 UCHAR * property_name ,
268273 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 )
272276{
273277 UINT status ;
274- AZURE_IOT_NX_CONTEXT * nx_context = (AZURE_IOT_NX_CONTEXT * )userContextCallback ;
275278
276279 if (strncmp ((CHAR * )property_name , TELEMETRY_INTERVAL_PROPERTY , property_name_len ) == 0 )
277280 {
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 );
279282 if (status == NX_AZURE_IOT_SUCCESS )
280283 {
284+ printf ("\tUpdating %s to %ld\r\n" , TELEMETRY_INTERVAL_PROPERTY , telemetry_interval );
285+
281286 // Confirm reception back to hub
282- azure_nx_client_respond_int_writeable_property (
287+ azure_nx_client_respond_int_writable_property (
283288 nx_context , TELEMETRY_INTERVAL_PROPERTY , telemetry_interval , 200 , version );
284289
285290 // 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,
288293 }
289294}
290295
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 ,
292298 UINT component_name_len ,
293299 UCHAR * property_name ,
294300 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 )
298303{
304+ UINT status ;
305+
299306 if (strncmp ((CHAR * )property_name , TELEMETRY_INTERVAL_PROPERTY , property_name_len ) == 0 )
300307 {
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+ }
302313 }
303314}
304315
@@ -350,25 +361,25 @@ UINT azure_iot_nx_client_entry(
350361 }
351362
352363 // 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 );
356367
357368 if ((status = azure_iot_nx_client_connect (& azure_iot_nx_client )))
358369 {
359370 printf ("ERROR: failed to connect nx client (0x%08x)\r\n" , status );
360371 return status ;
361372 }
362373
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 )))
365376 {
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 );
367378 return status ;
368379 }
369380
370381 // Send out property updates
371- azure_iot_nx_client_publish_int_writeable_property (
382+ azure_iot_nx_client_publish_int_writable_property (
372383 & azure_iot_nx_client , TELEMETRY_INTERVAL_PROPERTY , telemetry_interval );
373384 azure_iot_nx_client_publish_bool_property (& azure_iot_nx_client , LED_STATE_PROPERTY , false);
374385 azure_iot_nx_client_publish_properties (
@@ -385,19 +396,20 @@ UINT azure_iot_nx_client_entry(
385396 switch (telemetry_state )
386397 {
387398 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 );
389400 break ;
390401
391402 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 );
393404 break ;
394405
395406 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 );
397409 break ;
398410
399411 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 );
401413 break ;
402414
403415 default :
0 commit comments