forked from eclipse-threadx/getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure_iot_nx_client.c
1200 lines (1011 loc) · 41 KB
/
azure_iot_nx_client.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) Microsoft Corporation.
Licensed under the MIT License. */
#include "azure_iot_nx_client.h"
#include <stdio.h>
#include "nx_azure_iot_hub_client.h"
#include "nx_azure_iot_hub_client_properties.h"
#include "azure_iot_cert.h"
#include "azure_iot_ciphersuites.h"
#include "azure_iot_connect.h"
#define NX_AZURE_IOT_THREAD_PRIORITY 4
// Incoming events from the middleware
#define HUB_ALL_EVENTS 0xFF
#define HUB_CONNECT_EVENT 0x01
#define HUB_DISCONNECT_EVENT 0x02
#define HUB_COMMAND_RECEIVE_EVENT 0x04
#define HUB_PROPERTIES_RECEIVE_EVENT 0x08
#define HUB_WRITABLE_PROPERTIES_RECEIVE_EVENT 0x10
#define HUB_PROPERTIES_COMPLETE_EVENT 0x20
#define HUB_PERIODIC_TIMER_EVENT 0x40
#define AZURE_IOT_DPS_ENDPOINT "global.azure-devices-provisioning.net"
#define MODULE_ID ""
#define DPS_PAYLOAD "{\"modelId\":\"%s\"}"
// Connection timeouts in threadx ticks
#define HUB_CONNECT_TIMEOUT_TICKS (10 * TX_TIMER_TICKS_PER_SECOND)
#define DPS_REGISTER_TIMEOUT_TICKS (30 * TX_TIMER_TICKS_PER_SECOND)
#define DPS_PAYLOAD_SIZE (15 + 128)
#define TELEMETRY_BUFFER_SIZE 256
#define PROPERTIES_BUFFER_SIZE 128
// define static strings for content type and -encoding on message property bag
static const UCHAR content_type_property[] = "$.ct";
static const UCHAR content_encoding_property[] = "$.ce";
static const UCHAR content_type_json[] = "application%2Fjson";
static const UCHAR content_encoding_utf8[] = "utf-8";
static UCHAR telemetry_buffer[TELEMETRY_BUFFER_SIZE];
static UCHAR properties_buffer[PROPERTIES_BUFFER_SIZE];
static VOID printf_packet(CHAR* prepend, NX_PACKET* packet_ptr)
{
printf("%s", prepend);
while (packet_ptr != NX_NULL)
{
printf("%.*s", (INT)(packet_ptr->nx_packet_length), (CHAR*)packet_ptr->nx_packet_prepend_ptr);
packet_ptr = packet_ptr->nx_packet_next;
}
printf("\r\n");
}
static VOID connection_status_callback(NX_AZURE_IOT_HUB_CLIENT* hub_client_ptr, UINT status)
{
// :HACK: This callback doesn't allow us to provide context, pinch it from the command message callback args
AZURE_IOT_NX_CONTEXT* nx_context = hub_client_ptr->nx_azure_iot_hub_client_command_message.message_callback_args;
if (status == NX_SUCCESS)
{
tx_event_flags_set(&nx_context->events, HUB_CONNECT_EVENT, TX_OR);
}
else
{
tx_event_flags_set(&nx_context->events, HUB_DISCONNECT_EVENT, TX_OR);
}
// update the connection status in the connect workflow
connection_status_set(nx_context, status);
}
static VOID message_receive_command(NX_AZURE_IOT_HUB_CLIENT* hub_client_ptr, VOID* context)
{
AZURE_IOT_NX_CONTEXT* nx_context = (AZURE_IOT_NX_CONTEXT*)context;
tx_event_flags_set(&nx_context->events, HUB_COMMAND_RECEIVE_EVENT, TX_OR);
}
static VOID message_receive_callback_properties(NX_AZURE_IOT_HUB_CLIENT* hub_client_ptr, VOID* context)
{
AZURE_IOT_NX_CONTEXT* nx_context = (AZURE_IOT_NX_CONTEXT*)context;
tx_event_flags_set(&nx_context->events, HUB_PROPERTIES_RECEIVE_EVENT, TX_OR);
}
static VOID message_receive_callback_writable_property(NX_AZURE_IOT_HUB_CLIENT* hub_client_ptr, VOID* context)
{
AZURE_IOT_NX_CONTEXT* nx_context = (AZURE_IOT_NX_CONTEXT*)context;
tx_event_flags_set(&nx_context->events, HUB_WRITABLE_PROPERTIES_RECEIVE_EVENT, TX_OR);
}
static VOID periodic_timer_entry(ULONG context)
{
AZURE_IOT_NX_CONTEXT* nx_context = (AZURE_IOT_NX_CONTEXT*)context;
tx_event_flags_set(&nx_context->events, HUB_PERIODIC_TIMER_EVENT, TX_OR);
}
static UINT iot_hub_initialize(AZURE_IOT_NX_CONTEXT* nx_context)
{
UINT status;
// Initialize IoT Hub client.
if ((status = nx_azure_iot_hub_client_initialize(&nx_context->iothub_client,
&nx_context->nx_azure_iot,
(UCHAR*)nx_context->azure_iot_hub_hostname,
nx_context->azure_iot_hub_hostname_len,
(UCHAR*)nx_context->azure_iot_hub_device_id,
nx_context->azure_iot_hub_device_id_len,
(UCHAR*)MODULE_ID,
sizeof(MODULE_ID) - 1,
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
(UCHAR*)nx_context->nx_azure_iot_tls_metadata_buffer,
sizeof(nx_context->nx_azure_iot_tls_metadata_buffer),
&nx_context->root_ca_cert)))
{
printf("Error: on nx_azure_iot_hub_client_initialize (0x%08x)\r\n", status);
return status;
}
// Set credentials
if (nx_context->azure_iot_auth_mode == AZURE_IOT_AUTH_MODE_SAS)
{
// Symmetric (SAS) Key
if ((status = nx_azure_iot_hub_client_symmetric_key_set(&nx_context->iothub_client,
(UCHAR*)nx_context->azure_iot_device_sas_key,
nx_context->azure_iot_device_sas_key_len)))
{
printf("Error: failed on nx_azure_iot_hub_client_symmetric_key_set (0x%08x)\r\n", status);
}
}
else if (nx_context->azure_iot_auth_mode == AZURE_IOT_AUTH_MODE_CERT)
{
// X509 Certificate
if ((status = nx_azure_iot_hub_client_device_cert_set(
&nx_context->iothub_client, &nx_context->device_certificate)))
{
printf("Error: failed on nx_azure_iot_hub_client_device_cert_set!: error code = 0x%08x\r\n", status);
}
}
if (status != NX_AZURE_IOT_SUCCESS)
{
printf("Failed to set auth credentials\r\n");
}
// Add more CA certificates
else if ((status =
nx_azure_iot_hub_client_trusted_cert_add(&nx_context->iothub_client, &nx_context->root_ca_cert_2)))
{
printf("Failed on nx_azure_iot_hub_client_trusted_cert_add!: error code = 0x%08x\r\n", status);
}
else if ((status =
nx_azure_iot_hub_client_trusted_cert_add(&nx_context->iothub_client, &nx_context->root_ca_cert_3)))
{
printf("Failed on nx_azure_iot_hub_client_trusted_cert_add!: error code = 0x%08x\r\n", status);
}
// Set Model id
else if ((status = nx_azure_iot_hub_client_model_id_set(&nx_context->iothub_client,
(UCHAR*)nx_context->azure_iot_model_id,
nx_context->azure_iot_model_id_len)))
{
printf("Error: nx_azure_iot_hub_client_model_id_set (0x%08x)\r\n", status);
}
// Set connection status callback
else if ((status = nx_azure_iot_hub_client_connection_status_callback_set(
&nx_context->iothub_client, connection_status_callback)))
{
printf("Error: failed on connection_status_callback (0x%08x)\r\n", status);
}
// Enable commands
else if ((status = nx_azure_iot_hub_client_command_enable(&nx_context->iothub_client)))
{
printf("Error: command receive enable failed (0x%08x)\r\n", status);
}
// Enable properties
else if ((status = nx_azure_iot_hub_client_properties_enable(&nx_context->iothub_client)))
{
printf("Failed on nx_azure_iot_hub_client_properties_enable!: error code = 0x%08x\r\n", status);
}
// Set properties callback
else if ((status = nx_azure_iot_hub_client_receive_callback_set(&nx_context->iothub_client,
NX_AZURE_IOT_HUB_PROPERTIES,
message_receive_callback_properties,
(VOID*)nx_context)))
{
printf("Error: device twin callback set (0x%08x)\r\n", status);
}
// Set command callback
else if ((status = nx_azure_iot_hub_client_receive_callback_set(
&nx_context->iothub_client, NX_AZURE_IOT_HUB_COMMAND, message_receive_command, (VOID*)nx_context)))
{
printf("Error: device method callback set (0x%08x)\r\n", status);
}
// Set the writable property callback
else if ((status = nx_azure_iot_hub_client_receive_callback_set(&nx_context->iothub_client,
NX_AZURE_IOT_HUB_WRITABLE_PROPERTIES,
message_receive_callback_writable_property,
(VOID*)nx_context)))
{
printf("Error: device twin desired property callback set (0x%08x)\r\n", status);
}
// Register the pnp components for receiving
for (int i = 0; i < nx_context->azure_iot_component_count; ++i)
{
if ((status = nx_azure_iot_hub_client_component_add(&nx_context->iothub_client,
(UCHAR*)nx_context->azure_iot_components[i],
strlen(nx_context->azure_iot_components[i]))))
{
printf("ERROR: nx_azure_iot_hub_client_component_add failed (0x%08x)\r\n", status);
break;
}
}
if (status != NX_AZURE_IOT_SUCCESS)
{
nx_azure_iot_hub_client_deinitialize(&nx_context->iothub_client);
}
return status;
}
static UINT dps_initialize(AZURE_IOT_NX_CONTEXT* nx_context)
{
UINT status;
CHAR payload[DPS_PAYLOAD_SIZE];
if (nx_context == NULL)
{
printf("ERROR: context is NULL\r\n");
return NX_PTR_ERROR;
}
// Return error if empty credentials
if (nx_context->azure_iot_dps_id_scope_len == 0 || nx_context->azure_iot_dps_registration_id_len == 0)
{
printf("ERROR: azure_iot_nx_client_dps_entry incorrect parameters\r\n");
return NX_PTR_ERROR;
}
printf("\r\nInitializing Azure IoT DPS client\r\n");
printf("\tDPS endpoint: %s\r\n", AZURE_IOT_DPS_ENDPOINT);
printf("\tDPS ID scope: %.*s\r\n", nx_context->azure_iot_dps_id_scope_len, nx_context->azure_iot_dps_id_scope);
printf("\tRegistration ID: %.*s\r\n",
nx_context->azure_iot_dps_registration_id_len,
nx_context->azure_iot_dps_registration_id);
// Initialise the length of the return buffers
nx_context->azure_iot_hub_hostname_len = sizeof(nx_context->azure_iot_hub_hostname);
nx_context->azure_iot_hub_device_id_len = sizeof(nx_context->azure_iot_hub_device_id);
if (snprintf(payload, sizeof(payload), DPS_PAYLOAD, nx_context->azure_iot_model_id) > DPS_PAYLOAD_SIZE - 1)
{
printf("ERROR: insufficient buffer size to create DPS payload\r\n");
return NX_SIZE_ERROR;
}
// Initialize IoT provisioning client
if ((status = nx_azure_iot_provisioning_client_initialize(&nx_context->dps_client,
&nx_context->nx_azure_iot,
(UCHAR*)AZURE_IOT_DPS_ENDPOINT,
strlen(AZURE_IOT_DPS_ENDPOINT),
(UCHAR*)nx_context->azure_iot_dps_id_scope,
nx_context->azure_iot_dps_id_scope_len,
(UCHAR*)nx_context->azure_iot_dps_registration_id,
nx_context->azure_iot_dps_registration_id_len,
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
(UCHAR*)nx_context->nx_azure_iot_tls_metadata_buffer,
sizeof(nx_context->nx_azure_iot_tls_metadata_buffer),
&nx_context->root_ca_cert)))
{
printf("ERROR: nx_azure_iot_provisioning_client_initialize (0x%08x)\r\n", status);
return status;
}
// Add more CA certificates
else if ((status = nx_azure_iot_provisioning_client_trusted_cert_add(
&nx_context->dps_client, &nx_context->root_ca_cert_2)))
{
printf("ERROR: nx_azure_iot_provisioning_client_trusted_cert_add!: error code = 0x%08x\r\n", status);
}
else if ((status = nx_azure_iot_provisioning_client_trusted_cert_add(
&nx_context->dps_client, &nx_context->root_ca_cert_3)))
{
printf("ERROR: nx_azure_iot_provisioning_client_trusted_cert_add!: error code = 0x%08x\r\n", status);
}
else
{
switch (nx_context->azure_iot_auth_mode)
{
// Symmetric (SAS) Key
case AZURE_IOT_AUTH_MODE_SAS:
if ((status = nx_azure_iot_provisioning_client_symmetric_key_set(&nx_context->dps_client,
(UCHAR*)nx_context->azure_iot_device_sas_key,
nx_context->azure_iot_device_sas_key_len)))
{
printf("ERROR: nx_azure_iot_provisioning_client_symmetric_key_set (0x%08x)\r\n", status);
}
break;
// X509 Certificate
case AZURE_IOT_AUTH_MODE_CERT:
if ((status = nx_azure_iot_provisioning_client_device_cert_set(
&nx_context->dps_client, &nx_context->device_certificate)))
{
printf("ERROR: nx_azure_iot_provisioning_client_device_cert_set (0x%08x)\r\n", status);
}
break;
}
}
if (status != NX_AZURE_IOT_SUCCESS)
{
printf("ERROR: failed to set initialize DPS\r\n");
}
// Set the payload containing the model Id
else if ((status = nx_azure_iot_provisioning_client_registration_payload_set(
&nx_context->dps_client, (UCHAR*)payload, strlen(payload))))
{
printf("ERROR: nx_azure_iot_provisioning_client_registration_payload_set (0x%08x\r\n", status);
}
else if ((status = nx_azure_iot_provisioning_client_register(&nx_context->dps_client, DPS_REGISTER_TIMEOUT_TICKS)))
{
printf("\tERROR: nx_azure_iot_provisioning_client_register (0x%08x)\r\n", status);
}
// Stash IoT Hub Device info
else if ((status = nx_azure_iot_provisioning_client_iothub_device_info_get(&nx_context->dps_client,
(UCHAR*)nx_context->azure_iot_hub_hostname,
&nx_context->azure_iot_hub_hostname_len,
(UCHAR*)nx_context->azure_iot_hub_device_id,
&nx_context->azure_iot_hub_device_id_len)))
{
printf("ERROR: nx_azure_iot_provisioning_client_iothub_device_info_get (0x%08x)\r\n", status);
}
// Destroy Provisioning Client
nx_azure_iot_provisioning_client_deinitialize(&nx_context->dps_client);
if (status != NX_SUCCESS)
{
return status;
}
printf("SUCCESS: Azure IoT DPS client initialized\r\n");
return iot_hub_initialize(nx_context);
}
static VOID process_connect(AZURE_IOT_NX_CONTEXT* nx_context)
{
UINT status;
// Request the client properties
if ((status = nx_azure_iot_hub_client_properties_request(&nx_context->iothub_client, NX_WAIT_FOREVER)))
{
printf("ERROR: failed to request properties (0x%08x)\r\n", status);
}
// Start the periodic timer
if ((status = tx_timer_activate(&nx_context->periodic_timer)))
{
printf("ERROR: tx_timer_activate (0x%08x)\r\n", status);
}
}
static VOID process_disconnect(AZURE_IOT_NX_CONTEXT* nx_context)
{
UINT status;
printf("Disconnected from IoT Hub\r\n");
// Stop the periodic timer
if ((status = tx_timer_deactivate(&nx_context->periodic_timer)))
{
printf("ERROR: tx_timer_deactivate (0x%08x)\r\n", status);
}
}
static VOID process_properties_complete(AZURE_IOT_NX_CONTEXT* nx_context)
{
if (nx_context->properties_complete_cb)
{
nx_context->properties_complete_cb(nx_context);
}
}
static VOID process_command(AZURE_IOT_NX_CONTEXT* nx_context)
{
UINT status;
const UCHAR* component_name_ptr;
USHORT component_name_length;
const UCHAR* command_name_ptr;
USHORT command_name_length;
VOID* context_ptr;
USHORT context_length;
UCHAR* payload_ptr;
USHORT payload_length;
NX_PACKET* packet_ptr;
while ((status = nx_azure_iot_hub_client_command_message_receive(&nx_context->iothub_client,
&component_name_ptr,
&component_name_length,
&command_name_ptr,
&command_name_length,
&context_ptr,
&context_length,
&packet_ptr,
NX_NO_WAIT)) == NX_AZURE_IOT_SUCCESS)
{
printf("Received command: %.*s\r\n", (INT)command_name_length, (CHAR*)command_name_ptr);
printf_packet("\tPayload: ", packet_ptr);
payload_ptr = packet_ptr->nx_packet_prepend_ptr;
payload_length = packet_ptr->nx_packet_append_ptr - packet_ptr->nx_packet_prepend_ptr;
if (nx_context->command_received_cb)
{
nx_context->command_received_cb(nx_context,
component_name_ptr,
component_name_length,
command_name_ptr,
command_name_length,
payload_ptr,
payload_length,
context_ptr,
context_length);
}
// Release the received packet, as ownership was passed to the application from the middleware
nx_packet_release(packet_ptr);
}
// If we failed for anything other than no packet, then report error
if (status != NX_AZURE_IOT_NO_PACKET)
{
printf("Error: Command receive failed (0x%08x)\r\n", status);
return;
}
}
static UINT process_properties_shared(AZURE_IOT_NX_CONTEXT* nx_context,
NX_PACKET* packet_ptr,
UINT message_type,
UINT property_type,
UCHAR* scratch_buffer,
UINT scratch_buffer_len,
func_ptr_property_received property_received_cb)
{
UINT status;
const UCHAR* component_name_ptr;
USHORT component_name_length = 0;
UINT property_name_length;
ULONG properties_version;
NX_AZURE_IOT_JSON_READER json_reader;
if ((status = nx_azure_iot_json_reader_init(&json_reader, packet_ptr)))
{
printf("Error: failed to initialize json reader (0x%08x)\r\n", status);
nx_packet_release(packet_ptr);
return status;
}
// Get the version
if ((status = nx_azure_iot_hub_client_properties_version_get(
&nx_context->iothub_client, &json_reader, message_type, &properties_version)))
{
printf("Error: Properties version get failed (0x%08x)\r\n", status);
nx_packet_release(packet_ptr);
return status;
}
// reinitialize the json reader after reading the version to reset
if ((status = nx_azure_iot_json_reader_init(&json_reader, packet_ptr)))
{
printf("Error: failed to initialize json reader (0x%08x)\r\n", status);
nx_packet_release(packet_ptr);
return status;
}
while ((status = nx_azure_iot_hub_client_properties_component_property_next_get(&nx_context->iothub_client,
&json_reader,
message_type,
property_type,
&component_name_ptr,
&component_name_length)) == NX_AZURE_IOT_SUCCESS)
{
if (nx_azure_iot_json_reader_token_string_get(
&json_reader, scratch_buffer, scratch_buffer_len, &property_name_length))
{
printf("Failed to get string property value\r\n");
return NX_NOT_SUCCESSFUL;
}
nx_azure_iot_json_reader_next_token(&json_reader);
property_received_cb(nx_context,
component_name_ptr,
component_name_length,
scratch_buffer,
property_name_length,
&json_reader,
properties_version);
// If we are still looking at the value, then skip over it (including if it has children)
if (nx_azure_iot_json_reader_token_type(&json_reader) == NX_AZURE_IOT_READER_TOKEN_BEGIN_OBJECT)
{
nx_azure_iot_json_reader_skip_children(&json_reader);
}
nx_azure_iot_json_reader_next_token(&json_reader);
}
return NX_AZURE_IOT_SUCCESS;
}
static VOID process_properties(AZURE_IOT_NX_CONTEXT* nx_context)
{
UINT status;
NX_PACKET* packet_ptr;
if ((status = nx_azure_iot_hub_client_properties_receive(&nx_context->iothub_client, &packet_ptr, NX_WAIT_FOREVER)))
{
printf("ERROR: nx_azure_iot_hub_client_properties_receive failed (0x%08x)\r\n", status);
return;
}
printf_packet("Receive properties: ", packet_ptr);
if (nx_context->property_received_cb)
{
// Parse the writable properties from the device twin receive receive message
if ((status = process_properties_shared(nx_context,
packet_ptr,
NX_AZURE_IOT_HUB_PROPERTIES,
NX_AZURE_IOT_HUB_CLIENT_PROPERTY_WRITABLE,
properties_buffer,
sizeof(properties_buffer),
nx_context->property_received_cb)))
{
printf("Error: failed to parse properties (0x%08x)\r\n", status);
}
}
// Release the received packet, as ownership was passed to the application from the middleware
nx_packet_release(packet_ptr);
// Send event to notify device twin received
tx_event_flags_set(&nx_context->events, HUB_PROPERTIES_COMPLETE_EVENT, TX_OR);
}
static VOID process_writable_properties(AZURE_IOT_NX_CONTEXT* nx_context)
{
UINT status;
NX_PACKET* packet_ptr;
if ((status = nx_azure_iot_hub_client_writable_properties_receive(
&nx_context->iothub_client, &packet_ptr, NX_WAIT_FOREVER)))
{
printf("ERROR: nx_azure_iot_hub_client_writable_properties_receive (0x%08x)\r\n", status);
return;
}
printf_packet("Receive properties: ", packet_ptr);
if (nx_context->writable_property_received_cb)
{
// Parse the writable properties from the writable receive message
if ((status = process_properties_shared(nx_context,
packet_ptr,
NX_AZURE_IOT_HUB_WRITABLE_PROPERTIES,
NX_AZURE_IOT_HUB_CLIENT_PROPERTY_WRITABLE,
properties_buffer,
sizeof(properties_buffer),
nx_context->writable_property_received_cb)))
{
printf("ERROR: failed to parse properties (0x%08x)\r\n", status);
}
}
// Release the received packet, as ownership was passed to the application from the middleware
nx_packet_release(packet_ptr);
}
static VOID process_timer_event(AZURE_IOT_NX_CONTEXT* nx_context)
{
if (nx_context->timer_cb)
{
nx_context->timer_cb(nx_context);
}
}
UINT azure_nx_client_periodic_interval_set(AZURE_IOT_NX_CONTEXT* nx_context, INT interval)
{
UINT status;
UINT active;
UINT ticks = interval * TX_TIMER_TICKS_PER_SECOND;
if ((status = tx_timer_info_get(&nx_context->periodic_timer, NULL, &active, NULL, NULL, NULL)))
{
printf("ERROR: tx_timer_deactivate (0x%08x)\r\n", status);
return status;
}
if (active == TX_TRUE && (status = tx_timer_deactivate(&nx_context->periodic_timer)))
{
printf("ERROR: tx_timer_deactivate (0x%08x)\r\n", status);
}
else if ((status = tx_timer_change(&nx_context->periodic_timer, ticks, ticks)))
{
printf("ERROR: tx_timer_change (0x%08x)\r\n", status);
}
else if (active == TX_TRUE && (status = tx_timer_activate(&nx_context->periodic_timer)))
{
printf("ERROR: tx_timer_activate (0x%08x)\r\n", status);
}
return status;
}
UINT azure_iot_nx_client_publish_telemetry(AZURE_IOT_NX_CONTEXT* context_ptr,
CHAR* component_name_ptr,
UINT (*append_properties)(NX_AZURE_IOT_JSON_WRITER* json_builder_ptr))
{
UINT status;
UINT telemetry_length;
NX_PACKET* packet_ptr;
NX_AZURE_IOT_JSON_WRITER json_writer;
if ((status = nx_azure_iot_hub_client_telemetry_message_create(
&context_ptr->iothub_client, &packet_ptr, NX_WAIT_FOREVER)))
{
printf("Error: nx_azure_iot_hub_client_telemetry_message_create failed (0x%08x)\r\n", status);
}
if (component_name_ptr != NX_NULL)
{
printf("appending component name: %s\r\n", component_name_ptr);
if ((status = nx_azure_iot_hub_client_telemetry_component_set(
packet_ptr, (UCHAR*)component_name_ptr, strlen(component_name_ptr), NX_WAIT_FOREVER)))
{
printf("Error: nx_azure_iot_hub_client_telemetry_component_set failed (0x%08x)\r\n", status);
nx_azure_iot_hub_client_telemetry_message_delete(packet_ptr);
return status;
}
}
if ((status = nx_azure_iot_json_writer_with_buffer_init(&json_writer, telemetry_buffer, sizeof(telemetry_buffer))))
{
printf("Error: Failed to initialize json writer (0x%08x)\r\n", status);
nx_azure_iot_hub_client_telemetry_message_delete(packet_ptr);
return status;
}
if ((status = nx_azure_iot_json_writer_append_begin_object(&json_writer)) ||
(status = append_properties(&json_writer)) ||
(status = nx_azure_iot_json_writer_append_end_object(&json_writer)))
{
printf("Error: Failed to build telemetry (0x%08x)\r\n", status);
nx_azure_iot_hub_client_telemetry_message_delete(packet_ptr);
return status;
}
// set the ContentType property on the message to "application/json" (url-encoded)
if ((status = nx_azure_iot_hub_client_telemetry_property_add(packet_ptr,
content_type_property,
sizeof(content_type_property) - 1,
content_type_json,
sizeof(content_type_json) - 1,
NX_WAIT_FOREVER)))
{
printf("Error: Cant set ContentType message property (0x%08X)\r\n", status);
nx_azure_iot_hub_client_telemetry_message_delete(packet_ptr);
return status;
}
// set the ContentEncoding property on the message to "utf-8"
if ((status = nx_azure_iot_hub_client_telemetry_property_add(packet_ptr,
content_encoding_property,
sizeof(content_encoding_property) - 1,
content_encoding_utf8,
sizeof(content_encoding_utf8) - 1,
NX_WAIT_FOREVER)))
{
printf("Error: Cant set ContentEncoding message property (0x%08X)\r\n", status);
nx_azure_iot_hub_client_telemetry_message_delete(packet_ptr);
return status;
}
telemetry_length = nx_azure_iot_json_writer_get_bytes_used(&json_writer);
if ((status = nx_azure_iot_hub_client_telemetry_send(
&context_ptr->iothub_client, packet_ptr, telemetry_buffer, telemetry_length, NX_WAIT_FOREVER)))
{
printf("Error: Telemetry message send failed (0x%08x)\r\n", status);
nx_azure_iot_hub_client_telemetry_message_delete(packet_ptr);
return status;
}
printf("Telemetry message sent: %.*s.\r\n", telemetry_length, telemetry_buffer);
return status;
}
static UINT reported_properties_begin(AZURE_IOT_NX_CONTEXT* context_ptr,
NX_AZURE_IOT_JSON_WRITER* json_writer,
NX_PACKET** packet_ptr,
CHAR* component_name_ptr)
{
UINT status;
if ((status = nx_azure_iot_hub_client_reported_properties_create(
&context_ptr->iothub_client, packet_ptr, NX_WAIT_FOREVER)))
{
printf("Error: Failed create reported properties (0x%08x)\r\n", status);
}
else if ((status = nx_azure_iot_json_writer_init(json_writer, *packet_ptr, NX_WAIT_FOREVER)))
{
printf("Error: Failed to initialize json writer (0x%08x)\r\n", status);
}
else if ((status = nx_azure_iot_json_writer_append_begin_object(json_writer)))
{
printf("Error: Failed to append object begin (0x%08x)\r\n", status);
}
else if (component_name_ptr != NX_NULL &&
(status = nx_azure_iot_hub_client_reported_properties_component_begin(
&context_ptr->iothub_client, json_writer, (UCHAR*)component_name_ptr, strlen(component_name_ptr))))
{
printf("Error: Failed to append component begin (0x%08x)\r\n", status);
}
return status;
}
static UINT reported_properties_end(AZURE_IOT_NX_CONTEXT* nx_context,
NX_AZURE_IOT_JSON_WRITER* json_writer,
NX_PACKET** packet_ptr,
CHAR* component_name_ptr)
{
UINT status;
UINT response_status = 0;
if ((component_name_ptr != NX_NULL && (status = nx_azure_iot_hub_client_reported_properties_component_end(
&nx_context->iothub_client, json_writer))))
{
printf("Error: Failed to append component end (0x%08x)\r\n", status);
return status;
}
if ((status = nx_azure_iot_json_writer_append_end_object(json_writer)))
{
printf("Error: Failed to append object end (0x%08x)\r\n", status);
return status;
}
printf_packet("Sending property: ", *packet_ptr);
if ((status = nx_azure_iot_hub_client_reported_properties_send(
&nx_context->iothub_client, *packet_ptr, NX_NULL, &response_status, NX_NULL, 5 * NX_IP_PERIODIC_RATE)))
{
printf("Error: nx_azure_iot_hub_client_reported_properties_send failed (0x%08x)\r\n", status);
return status;
}
else if ((response_status < 200) || (response_status >= 300))
{
printf("Error: Property sent response status failed (%d)\r\n", response_status);
return NX_NOT_SUCCESSFUL;
}
return NX_SUCCESS;
}
UINT azure_iot_nx_client_publish_properties(AZURE_IOT_NX_CONTEXT* nx_context,
CHAR* component_name_ptr,
UINT (*append_properties)(NX_AZURE_IOT_JSON_WRITER* json_writer_ptr))
{
UINT status;
NX_PACKET* packet_ptr;
NX_AZURE_IOT_JSON_WRITER json_writer;
if ((status = reported_properties_begin(nx_context, &json_writer, &packet_ptr, component_name_ptr)) ||
(status = append_properties(&json_writer)) ||
(status = reported_properties_end(nx_context, &json_writer, &packet_ptr, component_name_ptr)))
{
printf("ERROR: azure_iot_nx_client_publish_properties (0x%08x)", status);
nx_packet_release(packet_ptr);
}
return status;
}
UINT azure_iot_nx_client_publish_bool_property(
AZURE_IOT_NX_CONTEXT* nx_context, CHAR* component_name_ptr, CHAR* property_ptr, bool value)
{
UINT status;
NX_AZURE_IOT_JSON_WRITER json_writer;
NX_PACKET* packet_ptr;
if ((status = reported_properties_begin(nx_context, &json_writer, &packet_ptr, component_name_ptr)) ||
(status = nx_azure_iot_json_writer_append_property_with_bool_value(
&json_writer, (const UCHAR*)property_ptr, strlen(property_ptr), value)) ||
(status = reported_properties_end(nx_context, &json_writer, &packet_ptr, component_name_ptr)))
{
printf("ERROR: azure_iot_nx_client_publish_bool_property (0x%08x)", status);
nx_packet_release(packet_ptr);
}
return status;
}
UINT azure_nx_client_respond_int_writable_property(AZURE_IOT_NX_CONTEXT* nx_context,
CHAR* component_name_ptr,
CHAR* property_ptr,
INT value,
INT http_status,
INT version)
{
UINT status;
NX_AZURE_IOT_JSON_WRITER json_writer;
NX_PACKET* packet_ptr;
if ((status = reported_properties_begin(nx_context, &json_writer, &packet_ptr, component_name_ptr)) ||
(status = nx_azure_iot_hub_client_reported_properties_status_begin(&nx_context->iothub_client,
&json_writer,
(const UCHAR*)property_ptr,
strlen(property_ptr),
http_status,
version,
NULL,
0)) ||
(status = nx_azure_iot_json_writer_append_int32(&json_writer, value)) ||
(status = nx_azure_iot_hub_client_reported_properties_status_end(&nx_context->iothub_client, &json_writer)) ||
(status = reported_properties_end(nx_context, &json_writer, &packet_ptr, component_name_ptr)))
{
printf("ERROR: azure_nx_client_respond_int_writable_property (0x%08x)", status);
nx_packet_release(packet_ptr);
}
return status;
}
UINT azure_iot_nx_client_publish_int_writable_property(
AZURE_IOT_NX_CONTEXT* nx_context, CHAR* component_ptr, CHAR* property_ptr, UINT value)
{
// Pass in a version of 1, as we a reporting the writable property, not responding to a server request
return azure_nx_client_respond_int_writable_property(nx_context, component_ptr, property_ptr, value, 200, 1);
}
UINT azure_iot_nx_client_register_command_callback(AZURE_IOT_NX_CONTEXT* nx_context, func_ptr_command_received callback)
{
if (nx_context == NULL || nx_context->command_received_cb != NULL)
{
return NX_PTR_ERROR;
}
nx_context->command_received_cb = callback;
return NX_SUCCESS;
}
UINT azure_iot_nx_client_register_writable_property_callback(
AZURE_IOT_NX_CONTEXT* nx_context, func_ptr_writable_property_received callback)
{
if (nx_context == NULL || nx_context->writable_property_received_cb != NULL)
{
return NX_PTR_ERROR;
}
nx_context->writable_property_received_cb = callback;
return NX_SUCCESS;
}
UINT azure_iot_nx_client_register_property_callback(
AZURE_IOT_NX_CONTEXT* nx_context, func_ptr_property_received callback)
{
if (nx_context == NULL || nx_context->property_received_cb != NULL)
{
return NX_PTR_ERROR;
}
nx_context->property_received_cb = callback;
return NX_SUCCESS;
}
UINT azure_iot_nx_client_register_properties_complete_callback(
AZURE_IOT_NX_CONTEXT* nx_context, func_ptr_properties_complete callback)
{
if (nx_context == NULL || nx_context->properties_complete_cb != NULL)
{
return NX_PTR_ERROR;
}
nx_context->properties_complete_cb = callback;
return NX_SUCCESS;
}
UINT azure_iot_nx_client_register_timer_callback(
AZURE_IOT_NX_CONTEXT* nx_context, func_ptr_timer callback, int32_t interval)
{
if (nx_context == NULL || nx_context->timer_cb != NULL)
{
return NX_PTR_ERROR;
}
azure_nx_client_periodic_interval_set(nx_context, interval);
nx_context->timer_cb = callback;
return NX_SUCCESS;
}
UINT azure_iot_nx_client_add_component(AZURE_IOT_NX_CONTEXT* nx_context, CHAR* component_name)
{
if (nx_context == NULL || component_name == NULL)
{
return NX_PTR_ERROR;
}
if (nx_context->azure_iot_component_count >= NX_AZURE_IOT_HUB_CLIENT_MAX_COMPONENT_LIST)
{
return NX_AZURE_IOT_INSUFFICIENT_BUFFER_SPACE;
}
nx_context->azure_iot_components[nx_context->azure_iot_component_count] = component_name;
nx_context->azure_iot_component_count++;
return NX_SUCCESS;
}
UINT azure_iot_nx_client_sas_set(AZURE_IOT_NX_CONTEXT* context, CHAR* device_sas_key)
{
if (device_sas_key[0] == 0)
{
printf("Error: azure_iot_nx_client_sas_set device_sas_key is null\r\n");
return NX_PTR_ERROR;
}
context->azure_iot_auth_mode = AZURE_IOT_AUTH_MODE_SAS;
context->azure_iot_device_sas_key = device_sas_key;
context->azure_iot_device_sas_key_len = strlen(device_sas_key);
return NX_SUCCESS;
}
UINT azure_iot_nx_client_cert_set(AZURE_IOT_NX_CONTEXT* nx_context,
UCHAR* device_x509_cert,
UINT device_x509_cert_len,
UCHAR* device_x509_key,
UINT device_x509_key_len)
{
UINT status;
if (device_x509_cert_len == 0 || device_x509_key_len == 0)
{
printf("ERROR: azure_iot_nx_client_cert_set cert/key is null\r\n");
return NX_PTR_ERROR;
}
nx_context->azure_iot_auth_mode = AZURE_IOT_AUTH_MODE_CERT;
// Create the device certificate
if ((status = nx_secure_x509_certificate_initialize(&nx_context->device_certificate,
(UCHAR*)device_x509_cert,
(USHORT)device_x509_cert_len,
NX_NULL,
0,
(UCHAR*)device_x509_key,