This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
/
iothub_message.c
1236 lines (1135 loc) · 47.7 KB
/
iothub_message.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. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <stdlib.h>
#include "azure_c_shared_utility/optimize_size.h"
#include "azure_c_shared_utility/gballoc.h"
#include "azure_c_shared_utility/xlogging.h"
#include "azure_c_shared_utility/buffer_.h"
#include "iothub_message.h"
MU_DEFINE_ENUM_STRINGS_WITHOUT_INVALID(IOTHUB_MESSAGE_RESULT, IOTHUB_MESSAGE_RESULT_VALUES);
MU_DEFINE_ENUM_STRINGS_WITHOUT_INVALID(IOTHUBMESSAGE_CONTENT_TYPE, IOTHUBMESSAGE_CONTENT_TYPE_VALUES);
static const char* SECURITY_CLIENT_JSON_ENCODING = "application/json";
typedef struct IOTHUB_MESSAGE_HANDLE_DATA_TAG
{
IOTHUBMESSAGE_CONTENT_TYPE contentType;
union
{
BUFFER_HANDLE byteArray;
STRING_HANDLE string;
} value;
MAP_HANDLE properties;
char* messageId;
char* correlationId;
char* userDefinedContentType;
char* contentEncoding;
char* outputName;
char* inputName;
char* connectionModuleId;
char* connectionDeviceId;
IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE diagnosticData;
bool is_security_message;
char* creationTimeUtc;
char* userId;
}IOTHUB_MESSAGE_HANDLE_DATA;
static bool ContainsValidUsAscii(const char* asciiValue)
{
bool result = true;
const char* iterator = asciiValue;
while (iterator != NULL && *iterator != '\0')
{
// Union of all allowed ASCII characters for the different protocols (HTTPS, MQTT, AMQP) is [dec 32, dec 126].
if (*iterator < ' ' || *iterator > '~')
{
result = false;
break;
}
iterator++;
}
return result;
}
/* Codes_SRS_IOTHUBMESSAGE_07_008: [ValidateAsciiCharactersFilter shall loop through the mapKey and mapValue strings to ensure that they only contain valid US-Ascii characters.*/
static int ValidateAsciiCharactersFilter(const char* mapKey, const char* mapValue)
{
int result;
if (!ContainsValidUsAscii(mapKey) || !ContainsValidUsAscii(mapValue))
{
result = MU_FAILURE;
}
else
{
result = 0;
}
return result;
}
static void DestroyDiagnosticPropertyData(IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE diagnosticHandle)
{
if (diagnosticHandle != NULL)
{
free(diagnosticHandle->diagnosticId);
free(diagnosticHandle->diagnosticCreationTimeUtc);
}
free(diagnosticHandle);
}
static void DestroyMessageData(IOTHUB_MESSAGE_HANDLE_DATA* handleData)
{
if (handleData->contentType == IOTHUBMESSAGE_BYTEARRAY)
{
BUFFER_delete(handleData->value.byteArray);
}
else if (handleData->contentType == IOTHUBMESSAGE_STRING)
{
STRING_delete(handleData->value.string);
}
Map_Destroy(handleData->properties);
free(handleData->messageId);
handleData->messageId = NULL;
free(handleData->correlationId);
handleData->correlationId = NULL;
free(handleData->userDefinedContentType);
free(handleData->contentEncoding);
DestroyDiagnosticPropertyData(handleData->diagnosticData);
free(handleData->outputName);
free(handleData->inputName);
free(handleData->connectionModuleId);
free(handleData->connectionDeviceId);
free(handleData->creationTimeUtc);
free(handleData->userId);
free(handleData);
}
static int set_content_encoding(IOTHUB_MESSAGE_HANDLE_DATA* handleData, const char* encoding)
{
int result;
char* tmp_encoding;
if (mallocAndStrcpy_s(&tmp_encoding, encoding) != 0)
{
LogError("Failed saving a copy of contentEncoding");
// Codes_SRS_IOTHUBMESSAGE_09_008: [If the allocation or the copying of `contentEncoding` fails, then IoTHubMessage_SetContentEncodingSystemProperty shall return IOTHUB_MESSAGE_ERROR.]
result = MU_FAILURE;
}
else
{
// Codes_SRS_IOTHUBMESSAGE_09_007: [If the IOTHUB_MESSAGE_HANDLE `contentEncoding` is not NULL it shall be deallocated.]
if (handleData->contentEncoding != NULL)
{
free(handleData->contentEncoding);
}
handleData->contentEncoding = tmp_encoding;
result = 0;
}
return result;
}
static int set_message_creation_time(IOTHUB_MESSAGE_HANDLE_DATA* handleData, const char* messageCreationTimeUtc)
{
int result;
char* tmp_message_creation_time;
if (handleData->creationTimeUtc != NULL)
{
free(handleData->creationTimeUtc);
handleData->creationTimeUtc = NULL;
}
if (mallocAndStrcpy_s(&tmp_message_creation_time, messageCreationTimeUtc) != 0)
{
LogError("Failed saving a copy of messageCreationTimeUtc");
result = MU_FAILURE;
}
else
{
handleData->creationTimeUtc = tmp_message_creation_time;
result = 0;
}
return result;
}
static int set_message_user_id(IOTHUB_MESSAGE_HANDLE_DATA* handleData, const char* userId)
{
int result;
char* tmp_message_user_id;
if (handleData->userId != NULL)
{
free(handleData->userId);
handleData->userId = NULL;
}
if (mallocAndStrcpy_s(&tmp_message_user_id, userId) != 0)
{
LogError("Failed saving a copy of userId");
result = MU_FAILURE;
}
else
{
handleData->userId = tmp_message_user_id;
result = 0;
}
return result;
}
static IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE CloneDiagnosticPropertyData(const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* source)
{
IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE result = NULL;
if (source == NULL)
{
LogError("Invalid argument - source is NULL");
}
else
{
result = (IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE)malloc(sizeof(IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA));
if (result == NULL)
{
LogError("malloc failed");
}
else
{
result->diagnosticCreationTimeUtc = NULL;
result->diagnosticId = NULL;
if (source->diagnosticCreationTimeUtc != NULL && mallocAndStrcpy_s(&result->diagnosticCreationTimeUtc, source->diagnosticCreationTimeUtc) != 0)
{
LogError("mallocAndStrcpy_s for diagnosticCreationTimeUtc failed");
free(result);
result = NULL;
}
else if (source->diagnosticId != NULL && mallocAndStrcpy_s(&result->diagnosticId, source->diagnosticId) != 0)
{
LogError("mallocAndStrcpy_s for diagnosticId failed");
free(result->diagnosticCreationTimeUtc);
free(result);
result = NULL;
}
}
}
return result;
}
IOTHUB_MESSAGE_HANDLE IoTHubMessage_CreateFromByteArray(const unsigned char* byteArray, size_t size)
{
IOTHUB_MESSAGE_HANDLE_DATA* result;
if ((byteArray == NULL) && (size != 0))
{
LogError("Invalid argument - byteArray is NULL");
result = NULL;
}
else
{
result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
if (result == NULL)
{
LogError("unable to malloc");
/*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
/*let it go through*/
}
else
{
const unsigned char* source;
unsigned char temp = 0x00;
memset(result, 0, sizeof(*result));
/*Codes_SRS_IOTHUBMESSAGE_02_026: [The type of the new message shall be IOTHUBMESSAGE_BYTEARRAY.] */
result->contentType = IOTHUBMESSAGE_BYTEARRAY;
if (size != 0)
{
/*Codes_SRS_IOTHUBMESSAGE_06_002: [If size is NOT zero then byteArray MUST NOT be NULL*/
if (byteArray == NULL)
{
LogError("Attempted to create a Hub Message from a NULL pointer!");
DestroyMessageData(result);
result = NULL;
source = NULL;
}
else
{
source = byteArray;
}
}
else
{
/*Codes_SRS_IOTHUBMESSAGE_06_001: [If size is zero then byteArray may be NULL.]*/
source = &temp;
}
if (result != NULL)
{
/*Codes_SRS_IOTHUBMESSAGE_02_022: [IoTHubMessage_CreateFromByteArray shall call BUFFER_create passing byteArray and size as parameters.] */
if ((result->value.byteArray = BUFFER_create(source, size)) == NULL)
{
LogError("BUFFER_create failed");
/*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
DestroyMessageData(result);
result = NULL;
}
/*Codes_SRS_IOTHUBMESSAGE_02_023: [IoTHubMessage_CreateFromByteArray shall call Map_Create to create the message properties.] */
else if ((result->properties = Map_Create(ValidateAsciiCharactersFilter)) == NULL)
{
LogError("Map_Create for properties failed");
/*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
DestroyMessageData(result);
result = NULL;
}
/*Codes_SRS_IOTHUBMESSAGE_02_025: [Otherwise, IoTHubMessage_CreateFromByteArray shall return a non-NULL handle.] */
}
}
}
return result;
}
IOTHUB_MESSAGE_HANDLE IoTHubMessage_CreateFromString(const char* source)
{
IOTHUB_MESSAGE_HANDLE_DATA* result;
if (source == NULL)
{
LogError("Invalid argument - source is NULL");
result = NULL;
}
else
{
result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
if (result == NULL)
{
LogError("malloc failed");
/*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
/*let it go through*/
}
else
{
memset(result, 0, sizeof(*result));
/*Codes_SRS_IOTHUBMESSAGE_02_032: [The type of the new message shall be IOTHUBMESSAGE_STRING.] */
result->contentType = IOTHUBMESSAGE_STRING;
/*Codes_SRS_IOTHUBMESSAGE_02_027: [IoTHubMessage_CreateFromString shall call STRING_construct passing source as parameter.] */
if ((result->value.string = STRING_construct(source)) == NULL)
{
LogError("STRING_construct failed");
/*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
DestroyMessageData(result);
result = NULL;
}
/*Codes_SRS_IOTHUBMESSAGE_02_028: [IoTHubMessage_CreateFromString shall call Map_Create to create the message properties.] */
else if ((result->properties = Map_Create(ValidateAsciiCharactersFilter)) == NULL)
{
LogError("Map_Create for properties failed");
/*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
DestroyMessageData(result);
result = NULL;
}
/*Codes_SRS_IOTHUBMESSAGE_02_031: [Otherwise, IoTHubMessage_CreateFromString shall return a non-NULL handle.] */
}
}
return result;
}
/*Codes_SRS_IOTHUBMESSAGE_03_001: [IoTHubMessage_Clone shall create a new IoT hub message with data content identical to that of the iotHubMessageHandle parameter.]*/
IOTHUB_MESSAGE_HANDLE IoTHubMessage_Clone(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
{
IOTHUB_MESSAGE_HANDLE_DATA* result;
const IOTHUB_MESSAGE_HANDLE_DATA* source = (const IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
/* Codes_SRS_IOTHUBMESSAGE_03_005: [IoTHubMessage_Clone shall return NULL if iotHubMessageHandle is NULL.] */
if (source == NULL)
{
result = NULL;
LogError("iotHubMessageHandle parameter cannot be NULL for IoTHubMessage_Clone");
}
else
{
result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
/*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
if (result == NULL)
{
/*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
/*do nothing and return as is*/
LogError("unable to malloc");
}
else
{
memset(result, 0, sizeof(*result));
result->contentType = source->contentType;
result->is_security_message = source->is_security_message;
if (source->messageId != NULL && mallocAndStrcpy_s(&result->messageId, source->messageId) != 0)
{
LogError("unable to Copy messageId");
DestroyMessageData(result);
result = NULL;
}
else if (source->correlationId != NULL && mallocAndStrcpy_s(&result->correlationId, source->correlationId) != 0)
{
LogError("unable to Copy correlationId");
DestroyMessageData(result);
result = NULL;
}
else if (source->userDefinedContentType != NULL && mallocAndStrcpy_s(&result->userDefinedContentType, source->userDefinedContentType) != 0)
{
LogError("unable to copy contentType");
DestroyMessageData(result);
result = NULL;
}
else if (source->contentEncoding != NULL && mallocAndStrcpy_s(&result->contentEncoding, source->contentEncoding) != 0)
{
LogError("unable to copy contentEncoding");
DestroyMessageData(result);
result = NULL;
}
else if (source->diagnosticData != NULL && (result->diagnosticData = CloneDiagnosticPropertyData(source->diagnosticData)) == NULL)
{
LogError("unable to copy CloneDiagnosticPropertyData");
DestroyMessageData(result);
result = NULL;
}
else if (source->outputName != NULL && mallocAndStrcpy_s(&result->outputName, source->outputName) != 0)
{
LogError("unable to copy outputName");
DestroyMessageData(result);
result = NULL;
}
else if (source->inputName != NULL && mallocAndStrcpy_s(&result->inputName, source->inputName) != 0)
{
LogError("unable to copy inputName");
DestroyMessageData(result);
result = NULL;
}
else if (source->creationTimeUtc != NULL && mallocAndStrcpy_s(&result->creationTimeUtc, source->creationTimeUtc) != 0)
{
LogError("unable to copy creationTimeUtc");
DestroyMessageData(result);
result = NULL;
}
else if (source->userId != NULL && mallocAndStrcpy_s(&result->userId, source->userId) != 0)
{
LogError("unable to copy userId");
DestroyMessageData(result);
result = NULL;
}
else if (source->connectionModuleId != NULL && mallocAndStrcpy_s(&result->connectionModuleId, source->connectionModuleId) != 0)
{
LogError("unable to copy inputName");
DestroyMessageData(result);
result = NULL;
}
else if (source->connectionDeviceId != NULL && mallocAndStrcpy_s(&result->connectionDeviceId, source->connectionDeviceId) != 0)
{
LogError("unable to copy inputName");
DestroyMessageData(result);
result = NULL;
}
else if (source->contentType == IOTHUBMESSAGE_BYTEARRAY)
{
/*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone to content by a call to BUFFER_clone] */
if ((result->value.byteArray = BUFFER_clone(source->value.byteArray)) == NULL)
{
/*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
LogError("unable to BUFFER_clone");
DestroyMessageData(result);
result = NULL;
}
/*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */
else if ((result->properties = Map_Clone(source->properties)) == NULL)
{
/*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
LogError("unable to Map_Clone");
DestroyMessageData(result);
result = NULL;
}
/*Codes_SRS_IOTHUBMESSAGE_03_002: [IoTHubMessage_Clone shall return upon success a non-NULL handle to the newly created IoT hub message.]*/
}
else /*can only be STRING*/
{
/*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone the content by a call to BUFFER_clone or STRING_clone] */
if ((result->value.string = STRING_clone(source->value.string)) == NULL)
{
/*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
LogError("failed to STRING_clone");
DestroyMessageData(result);
result = NULL;
}
/*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */
else if ((result->properties = Map_Clone(source->properties)) == NULL)
{
/*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
LogError("unable to Map_Clone");
DestroyMessageData(result);
result = NULL;
}
}
}
}
return result;
}
IOTHUB_MESSAGE_RESULT IoTHubMessage_GetByteArray(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const unsigned char** buffer, size_t* size)
{
IOTHUB_MESSAGE_RESULT result;
if (
(iotHubMessageHandle == NULL) ||
(buffer == NULL) ||
(size == NULL)
)
{
/*Codes_SRS_IOTHUBMESSAGE_01_014: [If any of the arguments passed to IoTHubMessage_GetByteArray is NULL IoTHubMessage_GetByteArray shall return IOTHUBMESSAGE_INVALID_ARG.] */
LogError("invalid parameter (NULL) to IoTHubMessage_GetByteArray IOTHUB_MESSAGE_HANDLE iotHubMessageHandle=%p, const unsigned char** buffer=%p, size_t* size=%p", iotHubMessageHandle, buffer, size);
result = IOTHUB_MESSAGE_INVALID_ARG;
}
else
{
IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
if (handleData->contentType != IOTHUBMESSAGE_BYTEARRAY)
{
/*Codes_SRS_IOTHUBMESSAGE_02_021: [If iotHubMessageHandle is not a iothubmessage containing BYTEARRAY data, then IoTHubMessage_GetData shall write in *buffer NULL and shall set *size to 0.] */
result = IOTHUB_MESSAGE_INVALID_ARG;
LogError("invalid type of message %s", MU_ENUM_TO_STRING(IOTHUBMESSAGE_CONTENT_TYPE, handleData->contentType));
}
else
{
/*Codes_SRS_IOTHUBMESSAGE_01_011: [The pointer shall be obtained by using BUFFER_u_char and it shall be copied in the buffer argument.]*/
*buffer = BUFFER_u_char(handleData->value.byteArray);
/*Codes_SRS_IOTHUBMESSAGE_01_012: [The size of the associated data shall be obtained by using BUFFER_length and it shall be copied to the size argument.]*/
*size = BUFFER_length(handleData->value.byteArray);
result = IOTHUB_MESSAGE_OK;
}
}
return result;
}
const char* IoTHubMessage_GetString(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
{
const char* result;
if (iotHubMessageHandle == NULL)
{
/*Codes_SRS_IOTHUBMESSAGE_02_016: [If any parameter is NULL then IoTHubMessage_GetString shall return NULL.] */
result = NULL;
}
else
{
IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
if (handleData->contentType != IOTHUBMESSAGE_STRING)
{
/*Codes_SRS_IOTHUBMESSAGE_02_017: [IoTHubMessage_GetString shall return NULL if the iotHubMessageHandle does not refer to a IOTHUBMESSAGE of type STRING.] */
result = NULL;
}
else
{
/*Codes_SRS_IOTHUBMESSAGE_02_018: [IoTHubMessage_GetStringData shall return the currently stored null terminated string.] */
result = STRING_c_str(handleData->value.string);
}
}
return result;
}
IOTHUBMESSAGE_CONTENT_TYPE IoTHubMessage_GetContentType(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
{
IOTHUBMESSAGE_CONTENT_TYPE result;
/*Codes_SRS_IOTHUBMESSAGE_02_008: [If any parameter is NULL then IoTHubMessage_GetContentType shall return IOTHUBMESSAGE_UNKNOWN.] */
if (iotHubMessageHandle == NULL)
{
result = IOTHUBMESSAGE_UNKNOWN;
}
else
{
/*Codes_SRS_IOTHUBMESSAGE_02_009: [Otherwise IoTHubMessage_GetContentType shall return the type of the message.] */
IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
result = handleData->contentType;
}
return result;
}
MAP_HANDLE IoTHubMessage_Properties(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
{
MAP_HANDLE result;
/*Codes_SRS_IOTHUBMESSAGE_02_001: [If iotHubMessageHandle is NULL then IoTHubMessage_Properties shall return NULL.]*/
if (iotHubMessageHandle == NULL)
{
LogError("invalid arg (NULL) passed to IoTHubMessage_Properties");
result = NULL;
}
else
{
/*Codes_SRS_IOTHUBMESSAGE_02_002: [Otherwise, for any non-NULL iotHubMessageHandle it shall return a non-NULL MAP_HANDLE.]*/
IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
result = handleData->properties;
}
return result;
}
IOTHUB_MESSAGE_RESULT IoTHubMessage_SetProperty(IOTHUB_MESSAGE_HANDLE msg_handle, const char* key, const char* value)
{
IOTHUB_MESSAGE_RESULT result;
if (msg_handle == NULL || key == NULL || value == NULL)
{
LogError("invalid parameter (NULL) to IoTHubMessage_SetProperty iotHubMessageHandle=%p, key=%p, value=%p", msg_handle, key, value);
result = IOTHUB_MESSAGE_INVALID_ARG;
}
else
{
MAP_RESULT map_result = Map_AddOrUpdate(msg_handle->properties, key, value);
if (map_result == MAP_FILTER_REJECT)
{
LogError("Failure validating property as ASCII");
result = IOTHUB_MESSAGE_INVALID_TYPE;
}
else if (map_result != MAP_OK)
{
LogError("Failure adding property to internal map");
result = IOTHUB_MESSAGE_ERROR;
}
else
{
result = IOTHUB_MESSAGE_OK;
}
}
return result;
}
const char* IoTHubMessage_GetProperty(IOTHUB_MESSAGE_HANDLE msg_handle, const char* key)
{
const char* result;
if (msg_handle == NULL || key == NULL)
{
LogError("invalid parameter (NULL) to IoTHubMessage_GetProperty iotHubMessageHandle=%p, key=%p", msg_handle, key);
result = NULL;
}
else
{
bool key_exists = false;
// The return value is not neccessary, just check the key_exist variable
if ((Map_ContainsKey(msg_handle->properties, key, &key_exists) == MAP_OK) && key_exists)
{
result = Map_GetValueFromKey(msg_handle->properties, key);
}
else
{
result = NULL;
}
}
return result;
}
const char* IoTHubMessage_GetCorrelationId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
{
const char* result;
/* Codes_SRS_IOTHUBMESSAGE_07_016: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_GetCorrelationId shall return a NULL value.] */
if (iotHubMessageHandle == NULL)
{
LogError("invalid arg (NULL) passed to IoTHubMessage_GetCorrelationId");
result = NULL;
}
else
{
/* Codes_SRS_IOTHUBMESSAGE_07_017: [IoTHubMessage_GetCorrelationId shall return the correlationId as a const char*.] */
IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
result = handleData->correlationId;
}
return result;
}
IOTHUB_MESSAGE_RESULT IoTHubMessage_SetCorrelationId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* correlationId)
{
IOTHUB_MESSAGE_RESULT result;
/* Codes_SRS_IOTHUBMESSAGE_07_018: [if any of the parameters are NULL then IoTHubMessage_SetCorrelationId shall return a IOTHUB_MESSAGE_INVALID_ARG value.]*/
if (iotHubMessageHandle == NULL || correlationId == NULL)
{
LogError("invalid arg (NULL) passed to IoTHubMessage_SetCorrelationId");
result = IOTHUB_MESSAGE_INVALID_ARG;
}
else
{
IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
/* Codes_SRS_IOTHUBMESSAGE_07_019: [If the IOTHUB_MESSAGE_HANDLE correlationId is not NULL, then the IOTHUB_MESSAGE_HANDLE correlationId will be deallocated.] */
if (handleData->correlationId != NULL)
{
free(handleData->correlationId);
handleData->correlationId = NULL;
}
if (mallocAndStrcpy_s(&handleData->correlationId, correlationId) != 0)
{
/* Codes_SRS_IOTHUBMESSAGE_07_020: [If the allocation or the copying of the correlationId fails, then IoTHubMessage_SetCorrelationId shall return IOTHUB_MESSAGE_ERROR.] */
result = IOTHUB_MESSAGE_ERROR;
}
else
{
/* Codes_SRS_IOTHUBMESSAGE_07_021: [IoTHubMessage_SetCorrelationId finishes successfully it shall return IOTHUB_MESSAGE_OK.] */
result = IOTHUB_MESSAGE_OK;
}
}
return result;
}
IOTHUB_MESSAGE_RESULT IoTHubMessage_SetMessageId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* messageId)
{
IOTHUB_MESSAGE_RESULT result;
/* Codes_SRS_IOTHUBMESSAGE_07_012: [if any of the parameters are NULL then IoTHubMessage_SetMessageId shall return a IOTHUB_MESSAGE_INVALID_ARG value.] */
if (iotHubMessageHandle == NULL || messageId == NULL)
{
LogError("invalid arg (NULL) passed to IoTHubMessage_SetMessageId");
result = IOTHUB_MESSAGE_INVALID_ARG;
}
else
{
IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
/* Codes_SRS_IOTHUBMESSAGE_07_013: [If the IOTHUB_MESSAGE_HANDLE messageId is not NULL, then the IOTHUB_MESSAGE_HANDLE messageId will be freed] */
if (handleData->messageId != NULL)
{
free(handleData->messageId);
handleData->messageId = NULL;
}
/* Codes_SRS_IOTHUBMESSAGE_07_014: [If the allocation or the copying of the messageId fails, then IoTHubMessage_SetMessageId shall return IOTHUB_MESSAGE_ERROR.] */
if (mallocAndStrcpy_s(&handleData->messageId, messageId) != 0)
{
result = IOTHUB_MESSAGE_ERROR;
}
else
{
result = IOTHUB_MESSAGE_OK;
}
}
return result;
}
const char* IoTHubMessage_GetMessageId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
{
const char* result;
/* Codes_SRS_IOTHUBMESSAGE_07_010: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_MessageId shall return a NULL value.] */
if (iotHubMessageHandle == NULL)
{
LogError("invalid arg (NULL) passed to IoTHubMessage_GetMessageId");
result = NULL;
}
else
{
/* Codes_SRS_IOTHUBMESSAGE_07_011: [IoTHubMessage_MessageId shall return the messageId as a const char*.] */
IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
result = handleData->messageId;
}
return result;
}
IOTHUB_MESSAGE_RESULT IoTHubMessage_SetContentTypeSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* contentType)
{
IOTHUB_MESSAGE_RESULT result;
// Codes_SRS_IOTHUBMESSAGE_09_001: [If any of the parameters are NULL then IoTHubMessage_SetContentTypeSystemProperty shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
if (iotHubMessageHandle == NULL || contentType == NULL)
{
LogError("Invalid argument (iotHubMessageHandle=%p, contentType=%p)", iotHubMessageHandle, contentType);
result = IOTHUB_MESSAGE_INVALID_ARG;
}
else
{
IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
// Codes_SRS_IOTHUBMESSAGE_09_002: [If the IOTHUB_MESSAGE_HANDLE `contentType` is not NULL it shall be deallocated.]
if (handleData->userDefinedContentType != NULL)
{
free(handleData->userDefinedContentType);
handleData->userDefinedContentType = NULL;
}
if (mallocAndStrcpy_s(&handleData->userDefinedContentType, contentType) != 0)
{
LogError("Failed saving a copy of contentType");
// Codes_SRS_IOTHUBMESSAGE_09_003: [If the allocation or the copying of `contentType` fails, then IoTHubMessage_SetContentTypeSystemProperty shall return IOTHUB_MESSAGE_ERROR.]
result = IOTHUB_MESSAGE_ERROR;
}
else
{
// Codes_SRS_IOTHUBMESSAGE_09_004: [If IoTHubMessage_SetContentTypeSystemProperty finishes successfully it shall return IOTHUB_MESSAGE_OK.]
result = IOTHUB_MESSAGE_OK;
}
}
return result;
}
const char* IoTHubMessage_GetContentTypeSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
{
const char* result;
// Codes_SRS_IOTHUBMESSAGE_09_005: [If any of the parameters are NULL then IoTHubMessage_GetContentTypeSystemProperty shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
if (iotHubMessageHandle == NULL)
{
LogError("Invalid argument (iotHubMessageHandle is NULL)");
result = NULL;
}
else
{
IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
// Codes_SRS_IOTHUBMESSAGE_09_006: [IoTHubMessage_GetContentTypeSystemProperty shall return the `contentType` as a const char* ]
result = (const char*)handleData->userDefinedContentType;
}
return result;
}
IOTHUB_MESSAGE_RESULT IoTHubMessage_SetContentEncodingSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* contentEncoding)
{
IOTHUB_MESSAGE_RESULT result;
// Codes_SRS_IOTHUBMESSAGE_09_006: [If any of the parameters are NULL then IoTHubMessage_SetContentEncodingSystemProperty shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
if (iotHubMessageHandle == NULL || contentEncoding == NULL)
{
LogError("Invalid argument (iotHubMessageHandle=%p, contentEncoding=%p)", iotHubMessageHandle, contentEncoding);
result = IOTHUB_MESSAGE_INVALID_ARG;
}
else
{
if (set_content_encoding(iotHubMessageHandle, contentEncoding) != 0)
{
LogError("Failed saving a copy of contentEncoding");
// Codes_SRS_IOTHUBMESSAGE_09_008: [If the allocation or the copying of `contentEncoding` fails, then IoTHubMessage_SetContentEncodingSystemProperty shall return IOTHUB_MESSAGE_ERROR.]
result = IOTHUB_MESSAGE_ERROR;
}
else
{
// Codes_SRS_IOTHUBMESSAGE_09_009: [If IoTHubMessage_SetContentEncodingSystemProperty finishes successfully it shall return IOTHUB_MESSAGE_OK.]
result = IOTHUB_MESSAGE_OK;
}
}
return result;
}
const char* IoTHubMessage_GetContentEncodingSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
{
const char* result;
// Codes_SRS_IOTHUBMESSAGE_09_010: [If any of the parameters are NULL then IoTHubMessage_GetContentEncodingSystemProperty shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
if (iotHubMessageHandle == NULL)
{
LogError("Invalid argument (iotHubMessageHandle is NULL)");
result = NULL;
}
else
{
IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
// Codes_SRS_IOTHUBMESSAGE_09_011: [IoTHubMessage_GetContentEncodingSystemProperty shall return the `contentEncoding` as a const char* ]
result = (const char*)handleData->contentEncoding;
}
return result;
}
IOTHUB_MESSAGE_RESULT IoTHubMessage_SetMessageCreationTimeUtcSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* creationTimeUtc)
{
IOTHUB_MESSAGE_RESULT result;
if (iotHubMessageHandle == NULL || creationTimeUtc == NULL)
{
LogError("Invalid argument (iotHubMessageHandle=%p, creationTimeUtc=%p)", iotHubMessageHandle, creationTimeUtc);
result = IOTHUB_MESSAGE_INVALID_ARG;
}
else
{
if (set_message_creation_time(iotHubMessageHandle, creationTimeUtc) != 0)
{
LogError("Failed saving a copy of creationTimeUtc");
result = IOTHUB_MESSAGE_ERROR;
}
else
{
result = IOTHUB_MESSAGE_OK;
}
}
return result;
}
const char* IoTHubMessage_GetMessageCreationTimeUtcSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
{
const char* result;
if (iotHubMessageHandle == NULL)
{
LogError("Invalid argument (iotHubMessageHandle is NULL)");
result = NULL;
}
else
{
IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
result = (const char*)handleData->creationTimeUtc;
}
return result;
}
IOTHUB_MESSAGE_RESULT IoTHubMessage_SetMessageUserIdSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* userId)
{
IOTHUB_MESSAGE_RESULT result;
if (iotHubMessageHandle == NULL || userId == NULL)
{
LogError("Invalid argument (iotHubMessageHandle=%p, userId=%p)", iotHubMessageHandle, userId);
result = IOTHUB_MESSAGE_INVALID_ARG;
}
else
{
if (set_message_user_id(iotHubMessageHandle, userId) != 0)
{
LogError("Failed saving a copy of userId");
result = IOTHUB_MESSAGE_ERROR;
}
else
{
result = IOTHUB_MESSAGE_OK;
}
}
return result;
}
const char* IoTHubMessage_GetMessageUserIdSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
{
const char* result;
if (iotHubMessageHandle == NULL)
{
LogError("Invalid argument (iotHubMessageHandle is NULL)");
result = NULL;
}
else
{
IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
result = (const char*)handleData->userId;
}
return result;
}
const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* IoTHubMessage_GetDiagnosticPropertyData(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
{
const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* result;
// Codes_SRS_IOTHUBMESSAGE_10_001: [If any of the parameters are NULL then IoTHubMessage_GetDiagnosticPropertyData shall return a NULL value.]
if (iotHubMessageHandle == NULL)
{
LogError("Invalid argument (iotHubMessageHandle is NULL)");
result = NULL;
}
else
{
/* Codes_SRS_IOTHUBMESSAGE_10_002: [IoTHubMessage_GetDiagnosticPropertyData shall return the diagnosticData as a const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA*.] */
result = iotHubMessageHandle->diagnosticData;
}
return result;
}
IOTHUB_MESSAGE_RESULT IoTHubMessage_SetDiagnosticPropertyData(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* diagnosticData)
{
IOTHUB_MESSAGE_RESULT result;
// Codes_SRS_IOTHUBMESSAGE_10_003: [If any of the parameters are NULL then IoTHubMessage_SetDiagnosticId shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
if (iotHubMessageHandle == NULL ||
diagnosticData == NULL ||
diagnosticData->diagnosticCreationTimeUtc == NULL ||
diagnosticData->diagnosticId == NULL)
{
LogError("Invalid argument (iotHubMessageHandle=%p, diagnosticData=%p, diagnosticData->diagnosticId=%p, diagnosticData->diagnosticCreationTimeUtc=%p)",
iotHubMessageHandle, diagnosticData,
diagnosticData == NULL ? NULL : diagnosticData->diagnosticId,
diagnosticData == NULL ? NULL : diagnosticData->diagnosticCreationTimeUtc);
result = IOTHUB_MESSAGE_INVALID_ARG;
}
else
{
// Codes_SRS_IOTHUBMESSAGE_10_004: [If the IOTHUB_MESSAGE_HANDLE `diagnosticData` is not NULL it shall be deallocated.]
if (iotHubMessageHandle->diagnosticData != NULL)
{
DestroyDiagnosticPropertyData(iotHubMessageHandle->diagnosticData);
iotHubMessageHandle->diagnosticData = NULL;
}
// Codes_SRS_IOTHUBMESSAGE_10_005: [If the allocation or the copying of `diagnosticData` fails, then IoTHubMessage_SetDiagnosticPropertyData shall return IOTHUB_MESSAGE_ERROR.]
if ((iotHubMessageHandle->diagnosticData = CloneDiagnosticPropertyData(diagnosticData)) == NULL)
{
LogError("Failed saving a copy of diagnosticData");
result = IOTHUB_MESSAGE_ERROR;
}
else
{
// Codes_SRS_IOTHUBMESSAGE_10_006: [If IoTHubMessage_SetDiagnosticPropertyData finishes successfully it shall return IOTHUB_MESSAGE_OK.]
result = IOTHUB_MESSAGE_OK;
}
}
return result;
}
const char* IoTHubMessage_GetOutputName(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
{
const char* result;
// Codes_SRS_IOTHUBMESSAGE_31_034: [If the iotHubMessageHandle parameter is NULL then IoTHubMessage_GetOutputName shall return a NULL value.]
if (iotHubMessageHandle == NULL)
{
LogError("Invalid argument (iotHubMessageHandle is NULL)");
result = NULL;
}
else
{
// Codes_SRS_IOTHUBMESSAGE_31_035: [IoTHubMessage_GetOutputName shall return the OutputName as a const char*.]
result = iotHubMessageHandle->outputName;
}
return result;
}
IOTHUB_MESSAGE_RESULT IoTHubMessage_SetOutputName(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* outputName)
{
IOTHUB_MESSAGE_RESULT result;
// Codes_SRS_IOTHUBMESSAGE_31_036: [If any of the parameters are NULL then IoTHubMessage_SetOutputName shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
if ((iotHubMessageHandle == NULL) || (outputName == NULL))
{
LogError("Invalid argument (iotHubMessageHandle=%p, outputName=%p)", iotHubMessageHandle, outputName);
result = IOTHUB_MESSAGE_INVALID_ARG;
}
else
{
IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
// Codes_SRS_IOTHUBMESSAGE_31_037: [If the IOTHUB_MESSAGE_HANDLE OutputName is not NULL, then the IOTHUB_MESSAGE_HANDLE OutputName will be deallocated.]
if (handleData->outputName != NULL)
{