1 | // Copyright (c) Microsoft. All rights reserved.
|
---|
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
---|
3 |
|
---|
4 | #include <stdlib.h>
|
---|
5 | #include "azure_c_shared_utility/optimize_size.h"
|
---|
6 | #include "azure_c_shared_utility/gballoc.h"
|
---|
7 | #include "azure_c_shared_utility/xlogging.h"
|
---|
8 | #include "azure_c_shared_utility/buffer_.h"
|
---|
9 |
|
---|
10 | #include "iothub_message.h"
|
---|
11 |
|
---|
12 | MU_DEFINE_ENUM_STRINGS_WITHOUT_INVALID(IOTHUB_MESSAGE_RESULT, IOTHUB_MESSAGE_RESULT_VALUES);
|
---|
13 | MU_DEFINE_ENUM_STRINGS_WITHOUT_INVALID(IOTHUBMESSAGE_CONTENT_TYPE, IOTHUBMESSAGE_CONTENT_TYPE_VALUES);
|
---|
14 |
|
---|
15 | static const char* SECURITY_CLIENT_JSON_ENCODING = "application/json";
|
---|
16 |
|
---|
17 | typedef struct IOTHUB_MESSAGE_HANDLE_DATA_TAG
|
---|
18 | {
|
---|
19 | IOTHUBMESSAGE_CONTENT_TYPE contentType;
|
---|
20 | union
|
---|
21 | {
|
---|
22 | BUFFER_HANDLE byteArray;
|
---|
23 | STRING_HANDLE string;
|
---|
24 | } value;
|
---|
25 | MAP_HANDLE properties;
|
---|
26 | char* messageId;
|
---|
27 | char* correlationId;
|
---|
28 | char* userDefinedContentType;
|
---|
29 | char* contentEncoding;
|
---|
30 | char* outputName;
|
---|
31 | char* inputName;
|
---|
32 | char* connectionModuleId;
|
---|
33 | char* connectionDeviceId;
|
---|
34 | IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE diagnosticData;
|
---|
35 | bool is_security_message;
|
---|
36 | }IOTHUB_MESSAGE_HANDLE_DATA;
|
---|
37 |
|
---|
38 | static bool ContainsOnlyUsAscii(const char* asciiValue)
|
---|
39 | {
|
---|
40 | bool result = true;
|
---|
41 | const char* iterator = asciiValue;
|
---|
42 | while (iterator != NULL && *iterator != '\0')
|
---|
43 | {
|
---|
44 | // Allow only printable ascii char
|
---|
45 | if (*iterator < ' ' || *iterator > '~')
|
---|
46 | {
|
---|
47 | result = false;
|
---|
48 | break;
|
---|
49 | }
|
---|
50 | iterator++;
|
---|
51 | }
|
---|
52 | return result;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /* Codes_SRS_IOTHUBMESSAGE_07_008: [ValidateAsciiCharactersFilter shall loop through the mapKey and mapValue strings to ensure that they only contain valid US-Ascii characters Ascii value 32 - 126.] */
|
---|
56 | static int ValidateAsciiCharactersFilter(const char* mapKey, const char* mapValue)
|
---|
57 | {
|
---|
58 | int result;
|
---|
59 | if (!ContainsOnlyUsAscii(mapKey) || !ContainsOnlyUsAscii(mapValue))
|
---|
60 | {
|
---|
61 | result = MU_FAILURE;
|
---|
62 | }
|
---|
63 | else
|
---|
64 | {
|
---|
65 | result = 0;
|
---|
66 | }
|
---|
67 | return result;
|
---|
68 | }
|
---|
69 |
|
---|
70 | static void DestroyDiagnosticPropertyData(IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE diagnosticHandle)
|
---|
71 | {
|
---|
72 | if (diagnosticHandle != NULL)
|
---|
73 | {
|
---|
74 | free(diagnosticHandle->diagnosticId);
|
---|
75 | free(diagnosticHandle->diagnosticCreationTimeUtc);
|
---|
76 | }
|
---|
77 | free(diagnosticHandle);
|
---|
78 | }
|
---|
79 |
|
---|
80 | static void DestroyMessageData(IOTHUB_MESSAGE_HANDLE_DATA* handleData)
|
---|
81 | {
|
---|
82 | if (handleData->contentType == IOTHUBMESSAGE_BYTEARRAY)
|
---|
83 | {
|
---|
84 | BUFFER_delete(handleData->value.byteArray);
|
---|
85 | }
|
---|
86 | else if (handleData->contentType == IOTHUBMESSAGE_STRING)
|
---|
87 | {
|
---|
88 | STRING_delete(handleData->value.string);
|
---|
89 | }
|
---|
90 |
|
---|
91 | Map_Destroy(handleData->properties);
|
---|
92 | free(handleData->messageId);
|
---|
93 | handleData->messageId = NULL;
|
---|
94 | free(handleData->correlationId);
|
---|
95 | handleData->correlationId = NULL;
|
---|
96 | free(handleData->userDefinedContentType);
|
---|
97 | free(handleData->contentEncoding);
|
---|
98 | DestroyDiagnosticPropertyData(handleData->diagnosticData);
|
---|
99 | free(handleData->outputName);
|
---|
100 | free(handleData->inputName);
|
---|
101 | free(handleData->connectionModuleId);
|
---|
102 | free(handleData->connectionDeviceId);
|
---|
103 | free(handleData);
|
---|
104 | }
|
---|
105 |
|
---|
106 | static int set_content_encoding(IOTHUB_MESSAGE_HANDLE_DATA* handleData, const char* encoding)
|
---|
107 | {
|
---|
108 | int result;
|
---|
109 | char* tmp_encoding;
|
---|
110 |
|
---|
111 | if (mallocAndStrcpy_s(&tmp_encoding, encoding) != 0)
|
---|
112 | {
|
---|
113 | LogError("Failed saving a copy of contentEncoding");
|
---|
114 | // Codes_SRS_IOTHUBMESSAGE_09_008: [If the allocation or the copying of `contentEncoding` fails, then IoTHubMessage_SetContentEncodingSystemProperty shall return IOTHUB_MESSAGE_ERROR.]
|
---|
115 | result = MU_FAILURE;
|
---|
116 | }
|
---|
117 | else
|
---|
118 | {
|
---|
119 | // Codes_SRS_IOTHUBMESSAGE_09_007: [If the IOTHUB_MESSAGE_HANDLE `contentEncoding` is not NULL it shall be deallocated.]
|
---|
120 | if (handleData->contentEncoding != NULL)
|
---|
121 | {
|
---|
122 | free(handleData->contentEncoding);
|
---|
123 | }
|
---|
124 | handleData->contentEncoding = tmp_encoding;
|
---|
125 | result = 0;
|
---|
126 | }
|
---|
127 | return result;
|
---|
128 | }
|
---|
129 |
|
---|
130 | static IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE CloneDiagnosticPropertyData(const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* source)
|
---|
131 | {
|
---|
132 | IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE result = NULL;
|
---|
133 | if (source == NULL)
|
---|
134 | {
|
---|
135 | LogError("Invalid argument - source is NULL");
|
---|
136 | }
|
---|
137 | else
|
---|
138 | {
|
---|
139 | result = (IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE)malloc(sizeof(IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA));
|
---|
140 | if (result == NULL)
|
---|
141 | {
|
---|
142 | LogError("malloc failed");
|
---|
143 | }
|
---|
144 | else
|
---|
145 | {
|
---|
146 | result->diagnosticCreationTimeUtc = NULL;
|
---|
147 | result->diagnosticId = NULL;
|
---|
148 | if (source->diagnosticCreationTimeUtc != NULL && mallocAndStrcpy_s(&result->diagnosticCreationTimeUtc, source->diagnosticCreationTimeUtc) != 0)
|
---|
149 | {
|
---|
150 | LogError("mallocAndStrcpy_s for diagnosticCreationTimeUtc failed");
|
---|
151 | free(result);
|
---|
152 | result = NULL;
|
---|
153 | }
|
---|
154 | else if (source->diagnosticId != NULL && mallocAndStrcpy_s(&result->diagnosticId, source->diagnosticId) != 0)
|
---|
155 | {
|
---|
156 | LogError("mallocAndStrcpy_s for diagnosticId failed");
|
---|
157 | free(result->diagnosticCreationTimeUtc);
|
---|
158 | free(result);
|
---|
159 | result = NULL;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | return result;
|
---|
164 | }
|
---|
165 |
|
---|
166 | IOTHUB_MESSAGE_HANDLE IoTHubMessage_CreateFromByteArray(const unsigned char* byteArray, size_t size)
|
---|
167 | {
|
---|
168 | IOTHUB_MESSAGE_HANDLE_DATA* result;
|
---|
169 | if ((byteArray == NULL) && (size != 0))
|
---|
170 | {
|
---|
171 | LogError("Invalid argument - byteArray is NULL");
|
---|
172 | result = NULL;
|
---|
173 | }
|
---|
174 | else
|
---|
175 | {
|
---|
176 | result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
|
---|
177 | if (result == NULL)
|
---|
178 | {
|
---|
179 | LogError("unable to malloc");
|
---|
180 | /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
|
---|
181 | /*let it go through*/
|
---|
182 | }
|
---|
183 | else
|
---|
184 | {
|
---|
185 | const unsigned char* source;
|
---|
186 | unsigned char temp = 0x00;
|
---|
187 |
|
---|
188 | memset(result, 0, sizeof(*result));
|
---|
189 | /*Codes_SRS_IOTHUBMESSAGE_02_026: [The type of the new message shall be IOTHUBMESSAGE_BYTEARRAY.] */
|
---|
190 | result->contentType = IOTHUBMESSAGE_BYTEARRAY;
|
---|
191 |
|
---|
192 | if (size != 0)
|
---|
193 | {
|
---|
194 | /*Codes_SRS_IOTHUBMESSAGE_06_002: [If size is NOT zero then byteArray MUST NOT be NULL*/
|
---|
195 | if (byteArray == NULL)
|
---|
196 | {
|
---|
197 | LogError("Attempted to create a Hub Message from a NULL pointer!");
|
---|
198 | DestroyMessageData(result);
|
---|
199 | result = NULL;
|
---|
200 | source = NULL;
|
---|
201 | }
|
---|
202 | else
|
---|
203 | {
|
---|
204 | source = byteArray;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | else
|
---|
208 | {
|
---|
209 | /*Codes_SRS_IOTHUBMESSAGE_06_001: [If size is zero then byteArray may be NULL.]*/
|
---|
210 | source = &temp;
|
---|
211 | }
|
---|
212 | if (result != NULL)
|
---|
213 | {
|
---|
214 | /*Codes_SRS_IOTHUBMESSAGE_02_022: [IoTHubMessage_CreateFromByteArray shall call BUFFER_create passing byteArray and size as parameters.] */
|
---|
215 | if ((result->value.byteArray = BUFFER_create(source, size)) == NULL)
|
---|
216 | {
|
---|
217 | LogError("BUFFER_create failed");
|
---|
218 | /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
|
---|
219 | DestroyMessageData(result);
|
---|
220 | result = NULL;
|
---|
221 | }
|
---|
222 | /*Codes_SRS_IOTHUBMESSAGE_02_023: [IoTHubMessage_CreateFromByteArray shall call Map_Create to create the message properties.] */
|
---|
223 | else if ((result->properties = Map_Create(ValidateAsciiCharactersFilter)) == NULL)
|
---|
224 | {
|
---|
225 | LogError("Map_Create for properties failed");
|
---|
226 | /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
|
---|
227 | DestroyMessageData(result);
|
---|
228 | result = NULL;
|
---|
229 | }
|
---|
230 | /*Codes_SRS_IOTHUBMESSAGE_02_025: [Otherwise, IoTHubMessage_CreateFromByteArray shall return a non-NULL handle.] */
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 | return result;
|
---|
235 | }
|
---|
236 |
|
---|
237 | IOTHUB_MESSAGE_HANDLE IoTHubMessage_CreateFromString(const char* source)
|
---|
238 | {
|
---|
239 | IOTHUB_MESSAGE_HANDLE_DATA* result;
|
---|
240 | if (source == NULL)
|
---|
241 | {
|
---|
242 | LogError("Invalid argument - source is NULL");
|
---|
243 | result = NULL;
|
---|
244 | }
|
---|
245 | else
|
---|
246 | {
|
---|
247 | result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
|
---|
248 | if (result == NULL)
|
---|
249 | {
|
---|
250 | LogError("malloc failed");
|
---|
251 | /*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
|
---|
252 | /*let it go through*/
|
---|
253 | }
|
---|
254 | else
|
---|
255 | {
|
---|
256 | memset(result, 0, sizeof(*result));
|
---|
257 | /*Codes_SRS_IOTHUBMESSAGE_02_032: [The type of the new message shall be IOTHUBMESSAGE_STRING.] */
|
---|
258 | result->contentType = IOTHUBMESSAGE_STRING;
|
---|
259 |
|
---|
260 | /*Codes_SRS_IOTHUBMESSAGE_02_027: [IoTHubMessage_CreateFromString shall call STRING_construct passing source as parameter.] */
|
---|
261 | if ((result->value.string = STRING_construct(source)) == NULL)
|
---|
262 | {
|
---|
263 | LogError("STRING_construct failed");
|
---|
264 | /*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
|
---|
265 | DestroyMessageData(result);
|
---|
266 | result = NULL;
|
---|
267 | }
|
---|
268 | /*Codes_SRS_IOTHUBMESSAGE_02_028: [IoTHubMessage_CreateFromString shall call Map_Create to create the message properties.] */
|
---|
269 | else if ((result->properties = Map_Create(ValidateAsciiCharactersFilter)) == NULL)
|
---|
270 | {
|
---|
271 | LogError("Map_Create for properties failed");
|
---|
272 | /*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
|
---|
273 | DestroyMessageData(result);
|
---|
274 | result = NULL;
|
---|
275 | }
|
---|
276 | /*Codes_SRS_IOTHUBMESSAGE_02_031: [Otherwise, IoTHubMessage_CreateFromString shall return a non-NULL handle.] */
|
---|
277 | }
|
---|
278 | }
|
---|
279 | return result;
|
---|
280 | }
|
---|
281 |
|
---|
282 | /*Codes_SRS_IOTHUBMESSAGE_03_001: [IoTHubMessage_Clone shall create a new IoT hub message with data content identical to that of the iotHubMessageHandle parameter.]*/
|
---|
283 | IOTHUB_MESSAGE_HANDLE IoTHubMessage_Clone(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
284 | {
|
---|
285 | IOTHUB_MESSAGE_HANDLE_DATA* result;
|
---|
286 | const IOTHUB_MESSAGE_HANDLE_DATA* source = (const IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
|
---|
287 | /* Codes_SRS_IOTHUBMESSAGE_03_005: [IoTHubMessage_Clone shall return NULL if iotHubMessageHandle is NULL.] */
|
---|
288 | if (source == NULL)
|
---|
289 | {
|
---|
290 | result = NULL;
|
---|
291 | LogError("iotHubMessageHandle parameter cannot be NULL for IoTHubMessage_Clone");
|
---|
292 | }
|
---|
293 | else
|
---|
294 | {
|
---|
295 | result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
|
---|
296 | /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
|
---|
297 | if (result == NULL)
|
---|
298 | {
|
---|
299 | /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
|
---|
300 | /*do nothing and return as is*/
|
---|
301 | LogError("unable to malloc");
|
---|
302 | }
|
---|
303 | else
|
---|
304 | {
|
---|
305 | memset(result, 0, sizeof(*result));
|
---|
306 | result->contentType = source->contentType;
|
---|
307 | result->is_security_message = source->is_security_message;
|
---|
308 |
|
---|
309 | if (source->messageId != NULL && mallocAndStrcpy_s(&result->messageId, source->messageId) != 0)
|
---|
310 | {
|
---|
311 | LogError("unable to Copy messageId");
|
---|
312 | DestroyMessageData(result);
|
---|
313 | result = NULL;
|
---|
314 | }
|
---|
315 | else if (source->correlationId != NULL && mallocAndStrcpy_s(&result->correlationId, source->correlationId) != 0)
|
---|
316 | {
|
---|
317 | LogError("unable to Copy correlationId");
|
---|
318 | DestroyMessageData(result);
|
---|
319 | result = NULL;
|
---|
320 | }
|
---|
321 | else if (source->userDefinedContentType != NULL && mallocAndStrcpy_s(&result->userDefinedContentType, source->userDefinedContentType) != 0)
|
---|
322 | {
|
---|
323 | LogError("unable to copy contentType");
|
---|
324 | DestroyMessageData(result);
|
---|
325 | result = NULL;
|
---|
326 | }
|
---|
327 | else if (source->contentEncoding != NULL && mallocAndStrcpy_s(&result->contentEncoding, source->contentEncoding) != 0)
|
---|
328 | {
|
---|
329 | LogError("unable to copy contentEncoding");
|
---|
330 | DestroyMessageData(result);
|
---|
331 | result = NULL;
|
---|
332 | }
|
---|
333 | else if (source->diagnosticData != NULL && (result->diagnosticData = CloneDiagnosticPropertyData(source->diagnosticData)) == NULL)
|
---|
334 | {
|
---|
335 | LogError("unable to copy CloneDiagnosticPropertyData");
|
---|
336 | DestroyMessageData(result);
|
---|
337 | result = NULL;
|
---|
338 | }
|
---|
339 | else if (source->outputName != NULL && mallocAndStrcpy_s(&result->outputName, source->outputName) != 0)
|
---|
340 | {
|
---|
341 | LogError("unable to copy outputName");
|
---|
342 | DestroyMessageData(result);
|
---|
343 | result = NULL;
|
---|
344 | }
|
---|
345 | else if (source->inputName != NULL && mallocAndStrcpy_s(&result->inputName, source->inputName) != 0)
|
---|
346 | {
|
---|
347 | LogError("unable to copy inputName");
|
---|
348 | DestroyMessageData(result);
|
---|
349 | result = NULL;
|
---|
350 | }
|
---|
351 | else if (source->connectionModuleId != NULL && mallocAndStrcpy_s(&result->connectionModuleId, source->connectionModuleId) != 0)
|
---|
352 | {
|
---|
353 | LogError("unable to copy inputName");
|
---|
354 | DestroyMessageData(result);
|
---|
355 | result = NULL;
|
---|
356 | }
|
---|
357 | else if (source->connectionDeviceId != NULL && mallocAndStrcpy_s(&result->connectionDeviceId, source->connectionDeviceId) != 0)
|
---|
358 | {
|
---|
359 | LogError("unable to copy inputName");
|
---|
360 | DestroyMessageData(result);
|
---|
361 | result = NULL;
|
---|
362 | }
|
---|
363 | else if (source->contentType == IOTHUBMESSAGE_BYTEARRAY)
|
---|
364 | {
|
---|
365 | /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone to content by a call to BUFFER_clone] */
|
---|
366 | if ((result->value.byteArray = BUFFER_clone(source->value.byteArray)) == NULL)
|
---|
367 | {
|
---|
368 | /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
|
---|
369 | LogError("unable to BUFFER_clone");
|
---|
370 | DestroyMessageData(result);
|
---|
371 | result = NULL;
|
---|
372 | }
|
---|
373 | /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */
|
---|
374 | else if ((result->properties = Map_Clone(source->properties)) == NULL)
|
---|
375 | {
|
---|
376 | /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
|
---|
377 | LogError("unable to Map_Clone");
|
---|
378 | DestroyMessageData(result);
|
---|
379 | result = NULL;
|
---|
380 | }
|
---|
381 | /*Codes_SRS_IOTHUBMESSAGE_03_002: [IoTHubMessage_Clone shall return upon success a non-NULL handle to the newly created IoT hub message.]*/
|
---|
382 | }
|
---|
383 | else /*can only be STRING*/
|
---|
384 | {
|
---|
385 | /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone the content by a call to BUFFER_clone or STRING_clone] */
|
---|
386 | if ((result->value.string = STRING_clone(source->value.string)) == NULL)
|
---|
387 | {
|
---|
388 | /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
|
---|
389 | LogError("failed to STRING_clone");
|
---|
390 | DestroyMessageData(result);
|
---|
391 | result = NULL;
|
---|
392 | }
|
---|
393 | /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */
|
---|
394 | else if ((result->properties = Map_Clone(source->properties)) == NULL)
|
---|
395 | {
|
---|
396 | /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
|
---|
397 | LogError("unable to Map_Clone");
|
---|
398 | DestroyMessageData(result);
|
---|
399 | result = NULL;
|
---|
400 | }
|
---|
401 | }
|
---|
402 | }
|
---|
403 | }
|
---|
404 | return result;
|
---|
405 | }
|
---|
406 |
|
---|
407 | IOTHUB_MESSAGE_RESULT IoTHubMessage_GetByteArray(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const unsigned char** buffer, size_t* size)
|
---|
408 | {
|
---|
409 | IOTHUB_MESSAGE_RESULT result;
|
---|
410 | if (
|
---|
411 | (iotHubMessageHandle == NULL) ||
|
---|
412 | (buffer == NULL) ||
|
---|
413 | (size == NULL)
|
---|
414 | )
|
---|
415 | {
|
---|
416 | /*Codes_SRS_IOTHUBMESSAGE_01_014: [If any of the arguments passed to IoTHubMessage_GetByteArray is NULL IoTHubMessage_GetByteArray shall return IOTHUBMESSAGE_INVALID_ARG.] */
|
---|
417 | LogError("invalid parameter (NULL) to IoTHubMessage_GetByteArray IOTHUB_MESSAGE_HANDLE iotHubMessageHandle=%p, const unsigned char** buffer=%p, size_t* size=%p", iotHubMessageHandle, buffer, size);
|
---|
418 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
419 | }
|
---|
420 | else
|
---|
421 | {
|
---|
422 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
|
---|
423 | if (handleData->contentType != IOTHUBMESSAGE_BYTEARRAY)
|
---|
424 | {
|
---|
425 | /*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.] */
|
---|
426 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
427 | LogError("invalid type of message %s", MU_ENUM_TO_STRING(IOTHUBMESSAGE_CONTENT_TYPE, handleData->contentType));
|
---|
428 | }
|
---|
429 | else
|
---|
430 | {
|
---|
431 | /*Codes_SRS_IOTHUBMESSAGE_01_011: [The pointer shall be obtained by using BUFFER_u_char and it shall be copied in the buffer argument.]*/
|
---|
432 | *buffer = BUFFER_u_char(handleData->value.byteArray);
|
---|
433 | /*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.]*/
|
---|
434 | *size = BUFFER_length(handleData->value.byteArray);
|
---|
435 | result = IOTHUB_MESSAGE_OK;
|
---|
436 | }
|
---|
437 | }
|
---|
438 | return result;
|
---|
439 | }
|
---|
440 |
|
---|
441 | const char* IoTHubMessage_GetString(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
442 | {
|
---|
443 | const char* result;
|
---|
444 | if (iotHubMessageHandle == NULL)
|
---|
445 | {
|
---|
446 | /*Codes_SRS_IOTHUBMESSAGE_02_016: [If any parameter is NULL then IoTHubMessage_GetString shall return NULL.] */
|
---|
447 | result = NULL;
|
---|
448 | }
|
---|
449 | else
|
---|
450 | {
|
---|
451 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
|
---|
452 | if (handleData->contentType != IOTHUBMESSAGE_STRING)
|
---|
453 | {
|
---|
454 | /*Codes_SRS_IOTHUBMESSAGE_02_017: [IoTHubMessage_GetString shall return NULL if the iotHubMessageHandle does not refer to a IOTHUBMESSAGE of type STRING.] */
|
---|
455 | result = NULL;
|
---|
456 | }
|
---|
457 | else
|
---|
458 | {
|
---|
459 | /*Codes_SRS_IOTHUBMESSAGE_02_018: [IoTHubMessage_GetStringData shall return the currently stored null terminated string.] */
|
---|
460 | result = STRING_c_str(handleData->value.string);
|
---|
461 | }
|
---|
462 | }
|
---|
463 | return result;
|
---|
464 | }
|
---|
465 |
|
---|
466 | IOTHUBMESSAGE_CONTENT_TYPE IoTHubMessage_GetContentType(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
467 | {
|
---|
468 | IOTHUBMESSAGE_CONTENT_TYPE result;
|
---|
469 | /*Codes_SRS_IOTHUBMESSAGE_02_008: [If any parameter is NULL then IoTHubMessage_GetContentType shall return IOTHUBMESSAGE_UNKNOWN.] */
|
---|
470 | if (iotHubMessageHandle == NULL)
|
---|
471 | {
|
---|
472 | result = IOTHUBMESSAGE_UNKNOWN;
|
---|
473 | }
|
---|
474 | else
|
---|
475 | {
|
---|
476 | /*Codes_SRS_IOTHUBMESSAGE_02_009: [Otherwise IoTHubMessage_GetContentType shall return the type of the message.] */
|
---|
477 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
|
---|
478 | result = handleData->contentType;
|
---|
479 | }
|
---|
480 | return result;
|
---|
481 | }
|
---|
482 |
|
---|
483 | MAP_HANDLE IoTHubMessage_Properties(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
484 | {
|
---|
485 | MAP_HANDLE result;
|
---|
486 | /*Codes_SRS_IOTHUBMESSAGE_02_001: [If iotHubMessageHandle is NULL then IoTHubMessage_Properties shall return NULL.]*/
|
---|
487 | if (iotHubMessageHandle == NULL)
|
---|
488 | {
|
---|
489 | LogError("invalid arg (NULL) passed to IoTHubMessage_Properties");
|
---|
490 | result = NULL;
|
---|
491 | }
|
---|
492 | else
|
---|
493 | {
|
---|
494 | /*Codes_SRS_IOTHUBMESSAGE_02_002: [Otherwise, for any non-NULL iotHubMessageHandle it shall return a non-NULL MAP_HANDLE.]*/
|
---|
495 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
|
---|
496 | result = handleData->properties;
|
---|
497 | }
|
---|
498 | return result;
|
---|
499 | }
|
---|
500 |
|
---|
501 | IOTHUB_MESSAGE_RESULT IoTHubMessage_SetProperty(IOTHUB_MESSAGE_HANDLE msg_handle, const char* key, const char* value)
|
---|
502 | {
|
---|
503 | IOTHUB_MESSAGE_RESULT result;
|
---|
504 | if (msg_handle == NULL || key == NULL || value == NULL)
|
---|
505 | {
|
---|
506 | LogError("invalid parameter (NULL) to IoTHubMessage_SetProperty iotHubMessageHandle=%p, key=%p, value=%p", msg_handle, key, value);
|
---|
507 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
508 | }
|
---|
509 | else
|
---|
510 | {
|
---|
511 | if (Map_AddOrUpdate(msg_handle->properties, key, value) != MAP_OK)
|
---|
512 | {
|
---|
513 | LogError("Failure adding property to internal map");
|
---|
514 | result = IOTHUB_MESSAGE_ERROR;
|
---|
515 | }
|
---|
516 | else
|
---|
517 | {
|
---|
518 | result = IOTHUB_MESSAGE_OK;
|
---|
519 | }
|
---|
520 | }
|
---|
521 | return result;
|
---|
522 | }
|
---|
523 |
|
---|
524 | const char* IoTHubMessage_GetProperty(IOTHUB_MESSAGE_HANDLE msg_handle, const char* key)
|
---|
525 | {
|
---|
526 | const char* result;
|
---|
527 | if (msg_handle == NULL || key == NULL)
|
---|
528 | {
|
---|
529 | LogError("invalid parameter (NULL) to IoTHubMessage_GetProperty iotHubMessageHandle=%p, key=%p", msg_handle, key);
|
---|
530 | result = NULL;
|
---|
531 | }
|
---|
532 | else
|
---|
533 | {
|
---|
534 | bool key_exists = false;
|
---|
535 | // The return value is not neccessary, just check the key_exist variable
|
---|
536 | if ((Map_ContainsKey(msg_handle->properties, key, &key_exists) == MAP_OK) && key_exists)
|
---|
537 | {
|
---|
538 | result = Map_GetValueFromKey(msg_handle->properties, key);
|
---|
539 | }
|
---|
540 | else
|
---|
541 | {
|
---|
542 | result = NULL;
|
---|
543 | }
|
---|
544 | }
|
---|
545 | return result;
|
---|
546 | }
|
---|
547 |
|
---|
548 | const char* IoTHubMessage_GetCorrelationId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
549 | {
|
---|
550 | const char* result;
|
---|
551 | /* Codes_SRS_IOTHUBMESSAGE_07_016: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_GetCorrelationId shall return a NULL value.] */
|
---|
552 | if (iotHubMessageHandle == NULL)
|
---|
553 | {
|
---|
554 | LogError("invalid arg (NULL) passed to IoTHubMessage_GetCorrelationId");
|
---|
555 | result = NULL;
|
---|
556 | }
|
---|
557 | else
|
---|
558 | {
|
---|
559 | /* Codes_SRS_IOTHUBMESSAGE_07_017: [IoTHubMessage_GetCorrelationId shall return the correlationId as a const char*.] */
|
---|
560 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
|
---|
561 | result = handleData->correlationId;
|
---|
562 | }
|
---|
563 | return result;
|
---|
564 | }
|
---|
565 |
|
---|
566 | IOTHUB_MESSAGE_RESULT IoTHubMessage_SetCorrelationId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* correlationId)
|
---|
567 | {
|
---|
568 | IOTHUB_MESSAGE_RESULT result;
|
---|
569 | /* Codes_SRS_IOTHUBMESSAGE_07_018: [if any of the parameters are NULL then IoTHubMessage_SetCorrelationId shall return a IOTHUB_MESSAGE_INVALID_ARG value.]*/
|
---|
570 | if (iotHubMessageHandle == NULL || correlationId == NULL)
|
---|
571 | {
|
---|
572 | LogError("invalid arg (NULL) passed to IoTHubMessage_SetCorrelationId");
|
---|
573 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
574 | }
|
---|
575 | else
|
---|
576 | {
|
---|
577 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
|
---|
578 | /* Codes_SRS_IOTHUBMESSAGE_07_019: [If the IOTHUB_MESSAGE_HANDLE correlationId is not NULL, then the IOTHUB_MESSAGE_HANDLE correlationId will be deallocated.] */
|
---|
579 | if (handleData->correlationId != NULL)
|
---|
580 | {
|
---|
581 | free(handleData->correlationId);
|
---|
582 | handleData->correlationId = NULL;
|
---|
583 | }
|
---|
584 |
|
---|
585 | if (mallocAndStrcpy_s(&handleData->correlationId, correlationId) != 0)
|
---|
586 | {
|
---|
587 | /* Codes_SRS_IOTHUBMESSAGE_07_020: [If the allocation or the copying of the correlationId fails, then IoTHubMessage_SetCorrelationId shall return IOTHUB_MESSAGE_ERROR.] */
|
---|
588 | result = IOTHUB_MESSAGE_ERROR;
|
---|
589 | }
|
---|
590 | else
|
---|
591 | {
|
---|
592 | /* Codes_SRS_IOTHUBMESSAGE_07_021: [IoTHubMessage_SetCorrelationId finishes successfully it shall return IOTHUB_MESSAGE_OK.] */
|
---|
593 | result = IOTHUB_MESSAGE_OK;
|
---|
594 | }
|
---|
595 | }
|
---|
596 | return result;
|
---|
597 | }
|
---|
598 |
|
---|
599 | IOTHUB_MESSAGE_RESULT IoTHubMessage_SetMessageId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* messageId)
|
---|
600 | {
|
---|
601 | IOTHUB_MESSAGE_RESULT result;
|
---|
602 | /* Codes_SRS_IOTHUBMESSAGE_07_012: [if any of the parameters are NULL then IoTHubMessage_SetMessageId shall return a IOTHUB_MESSAGE_INVALID_ARG value.] */
|
---|
603 | if (iotHubMessageHandle == NULL || messageId == NULL)
|
---|
604 | {
|
---|
605 | LogError("invalid arg (NULL) passed to IoTHubMessage_SetMessageId");
|
---|
606 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
607 | }
|
---|
608 | else
|
---|
609 | {
|
---|
610 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
|
---|
611 | /* Codes_SRS_IOTHUBMESSAGE_07_013: [If the IOTHUB_MESSAGE_HANDLE messageId is not NULL, then the IOTHUB_MESSAGE_HANDLE messageId will be freed] */
|
---|
612 | if (handleData->messageId != NULL)
|
---|
613 | {
|
---|
614 | free(handleData->messageId);
|
---|
615 | handleData->messageId = NULL;
|
---|
616 | }
|
---|
617 |
|
---|
618 | /* Codes_SRS_IOTHUBMESSAGE_07_014: [If the allocation or the copying of the messageId fails, then IoTHubMessage_SetMessageId shall return IOTHUB_MESSAGE_ERROR.] */
|
---|
619 | if (mallocAndStrcpy_s(&handleData->messageId, messageId) != 0)
|
---|
620 | {
|
---|
621 | result = IOTHUB_MESSAGE_ERROR;
|
---|
622 | }
|
---|
623 | else
|
---|
624 | {
|
---|
625 | result = IOTHUB_MESSAGE_OK;
|
---|
626 | }
|
---|
627 | }
|
---|
628 | return result;
|
---|
629 | }
|
---|
630 |
|
---|
631 | const char* IoTHubMessage_GetMessageId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
632 | {
|
---|
633 | const char* result;
|
---|
634 | /* Codes_SRS_IOTHUBMESSAGE_07_010: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_MessageId shall return a NULL value.] */
|
---|
635 | if (iotHubMessageHandle == NULL)
|
---|
636 | {
|
---|
637 | LogError("invalid arg (NULL) passed to IoTHubMessage_GetMessageId");
|
---|
638 | result = NULL;
|
---|
639 | }
|
---|
640 | else
|
---|
641 | {
|
---|
642 | /* Codes_SRS_IOTHUBMESSAGE_07_011: [IoTHubMessage_MessageId shall return the messageId as a const char*.] */
|
---|
643 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
|
---|
644 | result = handleData->messageId;
|
---|
645 | }
|
---|
646 | return result;
|
---|
647 | }
|
---|
648 |
|
---|
649 | IOTHUB_MESSAGE_RESULT IoTHubMessage_SetContentTypeSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* contentType)
|
---|
650 | {
|
---|
651 | IOTHUB_MESSAGE_RESULT result;
|
---|
652 |
|
---|
653 | // Codes_SRS_IOTHUBMESSAGE_09_001: [If any of the parameters are NULL then IoTHubMessage_SetContentTypeSystemProperty shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
|
---|
654 | if (iotHubMessageHandle == NULL || contentType == NULL)
|
---|
655 | {
|
---|
656 | LogError("Invalid argument (iotHubMessageHandle=%p, contentType=%p)", iotHubMessageHandle, contentType);
|
---|
657 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
658 | }
|
---|
659 | else
|
---|
660 | {
|
---|
661 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
|
---|
662 |
|
---|
663 | // Codes_SRS_IOTHUBMESSAGE_09_002: [If the IOTHUB_MESSAGE_HANDLE `contentType` is not NULL it shall be deallocated.]
|
---|
664 | if (handleData->userDefinedContentType != NULL)
|
---|
665 | {
|
---|
666 | free(handleData->userDefinedContentType);
|
---|
667 | handleData->userDefinedContentType = NULL;
|
---|
668 | }
|
---|
669 |
|
---|
670 | if (mallocAndStrcpy_s(&handleData->userDefinedContentType, contentType) != 0)
|
---|
671 | {
|
---|
672 | LogError("Failed saving a copy of contentType");
|
---|
673 | // Codes_SRS_IOTHUBMESSAGE_09_003: [If the allocation or the copying of `contentType` fails, then IoTHubMessage_SetContentTypeSystemProperty shall return IOTHUB_MESSAGE_ERROR.]
|
---|
674 | result = IOTHUB_MESSAGE_ERROR;
|
---|
675 | }
|
---|
676 | else
|
---|
677 | {
|
---|
678 | // Codes_SRS_IOTHUBMESSAGE_09_004: [If IoTHubMessage_SetContentTypeSystemProperty finishes successfully it shall return IOTHUB_MESSAGE_OK.]
|
---|
679 | result = IOTHUB_MESSAGE_OK;
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | return result;
|
---|
684 | }
|
---|
685 |
|
---|
686 | const char* IoTHubMessage_GetContentTypeSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
687 | {
|
---|
688 | const char* result;
|
---|
689 |
|
---|
690 | // Codes_SRS_IOTHUBMESSAGE_09_005: [If any of the parameters are NULL then IoTHubMessage_GetContentTypeSystemProperty shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
|
---|
691 | if (iotHubMessageHandle == NULL)
|
---|
692 | {
|
---|
693 | LogError("Invalid argument (iotHubMessageHandle is NULL)");
|
---|
694 | result = NULL;
|
---|
695 | }
|
---|
696 | else
|
---|
697 | {
|
---|
698 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
|
---|
699 |
|
---|
700 | // Codes_SRS_IOTHUBMESSAGE_09_006: [IoTHubMessage_GetContentTypeSystemProperty shall return the `contentType` as a const char* ]
|
---|
701 | result = (const char*)handleData->userDefinedContentType;
|
---|
702 | }
|
---|
703 |
|
---|
704 | return result;
|
---|
705 | }
|
---|
706 |
|
---|
707 | IOTHUB_MESSAGE_RESULT IoTHubMessage_SetContentEncodingSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* contentEncoding)
|
---|
708 | {
|
---|
709 | IOTHUB_MESSAGE_RESULT result;
|
---|
710 |
|
---|
711 | // Codes_SRS_IOTHUBMESSAGE_09_006: [If any of the parameters are NULL then IoTHubMessage_SetContentEncodingSystemProperty shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
|
---|
712 | if (iotHubMessageHandle == NULL || contentEncoding == NULL)
|
---|
713 | {
|
---|
714 | LogError("Invalid argument (iotHubMessageHandle=%p, contentEncoding=%p)", iotHubMessageHandle, contentEncoding);
|
---|
715 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
716 | }
|
---|
717 | else
|
---|
718 | {
|
---|
719 | if (set_content_encoding(iotHubMessageHandle, contentEncoding) != 0)
|
---|
720 | {
|
---|
721 | LogError("Failed saving a copy of contentEncoding");
|
---|
722 | // Codes_SRS_IOTHUBMESSAGE_09_008: [If the allocation or the copying of `contentEncoding` fails, then IoTHubMessage_SetContentEncodingSystemProperty shall return IOTHUB_MESSAGE_ERROR.]
|
---|
723 | result = IOTHUB_MESSAGE_ERROR;
|
---|
724 | }
|
---|
725 | else
|
---|
726 | {
|
---|
727 | // Codes_SRS_IOTHUBMESSAGE_09_009: [If IoTHubMessage_SetContentEncodingSystemProperty finishes successfully it shall return IOTHUB_MESSAGE_OK.]
|
---|
728 | result = IOTHUB_MESSAGE_OK;
|
---|
729 | }
|
---|
730 | }
|
---|
731 | return result;
|
---|
732 | }
|
---|
733 |
|
---|
734 | const char* IoTHubMessage_GetContentEncodingSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
735 | {
|
---|
736 | const char* result;
|
---|
737 |
|
---|
738 | // Codes_SRS_IOTHUBMESSAGE_09_010: [If any of the parameters are NULL then IoTHubMessage_GetContentEncodingSystemProperty shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
|
---|
739 | if (iotHubMessageHandle == NULL)
|
---|
740 | {
|
---|
741 | LogError("Invalid argument (iotHubMessageHandle is NULL)");
|
---|
742 | result = NULL;
|
---|
743 | }
|
---|
744 | else
|
---|
745 | {
|
---|
746 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
|
---|
747 |
|
---|
748 | // Codes_SRS_IOTHUBMESSAGE_09_011: [IoTHubMessage_GetContentEncodingSystemProperty shall return the `contentEncoding` as a const char* ]
|
---|
749 | result = (const char*)handleData->contentEncoding;
|
---|
750 | }
|
---|
751 |
|
---|
752 | return result;
|
---|
753 | }
|
---|
754 |
|
---|
755 | const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* IoTHubMessage_GetDiagnosticPropertyData(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
756 | {
|
---|
757 | const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* result;
|
---|
758 | // Codes_SRS_IOTHUBMESSAGE_10_001: [If any of the parameters are NULL then IoTHubMessage_GetDiagnosticPropertyData shall return a NULL value.]
|
---|
759 | if (iotHubMessageHandle == NULL)
|
---|
760 | {
|
---|
761 | LogError("Invalid argument (iotHubMessageHandle is NULL)");
|
---|
762 | result = NULL;
|
---|
763 | }
|
---|
764 | else
|
---|
765 | {
|
---|
766 | /* Codes_SRS_IOTHUBMESSAGE_10_002: [IoTHubMessage_GetDiagnosticPropertyData shall return the diagnosticData as a const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA*.] */
|
---|
767 | result = iotHubMessageHandle->diagnosticData;
|
---|
768 | }
|
---|
769 | return result;
|
---|
770 | }
|
---|
771 |
|
---|
772 | IOTHUB_MESSAGE_RESULT IoTHubMessage_SetDiagnosticPropertyData(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* diagnosticData)
|
---|
773 | {
|
---|
774 | IOTHUB_MESSAGE_RESULT result;
|
---|
775 | // Codes_SRS_IOTHUBMESSAGE_10_003: [If any of the parameters are NULL then IoTHubMessage_SetDiagnosticId shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
|
---|
776 | if (iotHubMessageHandle == NULL ||
|
---|
777 | diagnosticData == NULL ||
|
---|
778 | diagnosticData->diagnosticCreationTimeUtc == NULL ||
|
---|
779 | diagnosticData->diagnosticId == NULL)
|
---|
780 | {
|
---|
781 | LogError("Invalid argument (iotHubMessageHandle=%p, diagnosticData=%p, diagnosticData->diagnosticId=%p, diagnosticData->diagnosticCreationTimeUtc=%p)",
|
---|
782 | iotHubMessageHandle, diagnosticData,
|
---|
783 | diagnosticData == NULL ? NULL : diagnosticData->diagnosticId,
|
---|
784 | diagnosticData == NULL ? NULL : diagnosticData->diagnosticCreationTimeUtc);
|
---|
785 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
786 | }
|
---|
787 | else
|
---|
788 | {
|
---|
789 | // Codes_SRS_IOTHUBMESSAGE_10_004: [If the IOTHUB_MESSAGE_HANDLE `diagnosticData` is not NULL it shall be deallocated.]
|
---|
790 | if (iotHubMessageHandle->diagnosticData != NULL)
|
---|
791 | {
|
---|
792 | DestroyDiagnosticPropertyData(iotHubMessageHandle->diagnosticData);
|
---|
793 | iotHubMessageHandle->diagnosticData = NULL;
|
---|
794 | }
|
---|
795 |
|
---|
796 | // Codes_SRS_IOTHUBMESSAGE_10_005: [If the allocation or the copying of `diagnosticData` fails, then IoTHubMessage_SetDiagnosticPropertyData shall return IOTHUB_MESSAGE_ERROR.]
|
---|
797 | if ((iotHubMessageHandle->diagnosticData = CloneDiagnosticPropertyData(diagnosticData)) == NULL)
|
---|
798 | {
|
---|
799 | LogError("Failed saving a copy of diagnosticData");
|
---|
800 | result = IOTHUB_MESSAGE_ERROR;
|
---|
801 | }
|
---|
802 | else
|
---|
803 | {
|
---|
804 | // Codes_SRS_IOTHUBMESSAGE_10_006: [If IoTHubMessage_SetDiagnosticPropertyData finishes successfully it shall return IOTHUB_MESSAGE_OK.]
|
---|
805 | result = IOTHUB_MESSAGE_OK;
|
---|
806 | }
|
---|
807 | }
|
---|
808 | return result;
|
---|
809 | }
|
---|
810 |
|
---|
811 |
|
---|
812 | const char* IoTHubMessage_GetOutputName(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
813 | {
|
---|
814 | const char* result;
|
---|
815 | // Codes_SRS_IOTHUBMESSAGE_31_034: [If the iotHubMessageHandle parameter is NULL then IoTHubMessage_GetOutputName shall return a NULL value.]
|
---|
816 | if (iotHubMessageHandle == NULL)
|
---|
817 | {
|
---|
818 | LogError("Invalid argument (iotHubMessageHandle is NULL)");
|
---|
819 | result = NULL;
|
---|
820 | }
|
---|
821 | else
|
---|
822 | {
|
---|
823 | // Codes_SRS_IOTHUBMESSAGE_31_035: [IoTHubMessage_GetOutputName shall return the OutputName as a const char*.]
|
---|
824 | result = iotHubMessageHandle->outputName;
|
---|
825 | }
|
---|
826 | return result;
|
---|
827 | }
|
---|
828 |
|
---|
829 | IOTHUB_MESSAGE_RESULT IoTHubMessage_SetOutputName(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* outputName)
|
---|
830 | {
|
---|
831 | IOTHUB_MESSAGE_RESULT result;
|
---|
832 |
|
---|
833 | // Codes_SRS_IOTHUBMESSAGE_31_036: [If any of the parameters are NULL then IoTHubMessage_SetOutputName shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
|
---|
834 | if ((iotHubMessageHandle == NULL) || (outputName == NULL))
|
---|
835 | {
|
---|
836 | LogError("Invalid argument (iotHubMessageHandle=%p, outputName=%p)", iotHubMessageHandle, outputName);
|
---|
837 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
838 | }
|
---|
839 | else
|
---|
840 | {
|
---|
841 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
|
---|
842 |
|
---|
843 | // Codes_SRS_IOTHUBMESSAGE_31_037: [If the IOTHUB_MESSAGE_HANDLE OutputName is not NULL, then the IOTHUB_MESSAGE_HANDLE OutputName will be deallocated.]
|
---|
844 | if (handleData->outputName != NULL)
|
---|
845 | {
|
---|
846 | free(handleData->outputName);
|
---|
847 | handleData->outputName = NULL;
|
---|
848 | }
|
---|
849 |
|
---|
850 | if (mallocAndStrcpy_s(&handleData->outputName, outputName) != 0)
|
---|
851 | {
|
---|
852 | // Codes_SRS_IOTHUBMESSAGE_31_038: [If the allocation or the copying of the OutputName fails, then IoTHubMessage_SetOutputName shall return IOTHUB_MESSAGE_ERROR.]
|
---|
853 | LogError("Failed saving a copy of outputName");
|
---|
854 | result = IOTHUB_MESSAGE_ERROR;
|
---|
855 | }
|
---|
856 | else
|
---|
857 | {
|
---|
858 | // Codes_SRS_IOTHUBMESSAGE_31_039: [IoTHubMessage_SetOutputName finishes successfully it shall return IOTHUB_MESSAGE_OK.]
|
---|
859 | result = IOTHUB_MESSAGE_OK;
|
---|
860 | }
|
---|
861 |
|
---|
862 | }
|
---|
863 |
|
---|
864 | return result;
|
---|
865 | }
|
---|
866 |
|
---|
867 | const char* IoTHubMessage_GetInputName(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
868 | {
|
---|
869 | const char* result;
|
---|
870 | // Codes_SRS_IOTHUBMESSAGE_31_040: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_GetInputName shall return a NULL value.]
|
---|
871 | if (iotHubMessageHandle == NULL)
|
---|
872 | {
|
---|
873 | LogError("Invalid argument (iotHubMessageHandle is NULL)");
|
---|
874 | result = NULL;
|
---|
875 | }
|
---|
876 | else
|
---|
877 | {
|
---|
878 | // Codes_SRS_IOTHUBMESSAGE_31_041: [IoTHubMessage_GetInputName shall return the InputName as a const char*.]
|
---|
879 | result = iotHubMessageHandle->inputName;
|
---|
880 | }
|
---|
881 | return result;
|
---|
882 | }
|
---|
883 |
|
---|
884 | IOTHUB_MESSAGE_RESULT IoTHubMessage_SetInputName(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* inputName)
|
---|
885 | {
|
---|
886 | IOTHUB_MESSAGE_RESULT result;
|
---|
887 |
|
---|
888 | // Codes_SRS_IOTHUBMESSAGE_31_042: [if any of the parameters are NULL then IoTHubMessage_SetInputName shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
|
---|
889 | if ((iotHubMessageHandle == NULL) || (inputName == NULL))
|
---|
890 | {
|
---|
891 | LogError("Invalid argument (iotHubMessageHandle=%p, inputName=%p)", iotHubMessageHandle, inputName);
|
---|
892 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
893 | }
|
---|
894 | else
|
---|
895 | {
|
---|
896 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
|
---|
897 |
|
---|
898 | // Codes_SRS_IOTHUBMESSAGE_31_043: [If the IOTHUB_MESSAGE_HANDLE InputName is not NULL, then the IOTHUB_MESSAGE_HANDLE InputName will be deallocated.]
|
---|
899 | if (handleData->inputName != NULL)
|
---|
900 | {
|
---|
901 | free(handleData->inputName);
|
---|
902 | handleData->inputName = NULL;
|
---|
903 | }
|
---|
904 |
|
---|
905 | if (mallocAndStrcpy_s(&handleData->inputName, inputName) != 0)
|
---|
906 | {
|
---|
907 | // Codes_SRS_IOTHUBMESSAGE_31_044: [If the allocation or the copying of the InputName fails, then IoTHubMessage_SetInputName shall return IOTHUB_MESSAGE_ERROR.]
|
---|
908 | LogError("Failed saving a copy of inputName");
|
---|
909 | result = IOTHUB_MESSAGE_ERROR;
|
---|
910 | }
|
---|
911 | else
|
---|
912 | {
|
---|
913 | // Codes_SRS_IOTHUBMESSAGE_31_045: [IoTHubMessage_SetInputName finishes successfully it shall return IOTHUB_MESSAGE_OK.]
|
---|
914 | result = IOTHUB_MESSAGE_OK;
|
---|
915 | }
|
---|
916 |
|
---|
917 | }
|
---|
918 |
|
---|
919 | return result;
|
---|
920 | }
|
---|
921 |
|
---|
922 |
|
---|
923 | const char* IoTHubMessage_GetConnectionModuleId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
924 | {
|
---|
925 | const char* result;
|
---|
926 | // Codes_SRS_IOTHUBMESSAGE_31_046: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_GetConnectionModuleId shall return a NULL value.]
|
---|
927 | if (iotHubMessageHandle == NULL)
|
---|
928 | {
|
---|
929 | LogError("Invalid argument (iotHubMessageHandle is NULL)");
|
---|
930 | result = NULL;
|
---|
931 | }
|
---|
932 | else
|
---|
933 | {
|
---|
934 | // Codes_SRS_IOTHUBMESSAGE_31_047: [IoTHubMessage_GetConnectionModuleId shall return the ConnectionModuleId as a const char*.]
|
---|
935 | result = iotHubMessageHandle->connectionModuleId;
|
---|
936 | }
|
---|
937 | return result;
|
---|
938 | }
|
---|
939 |
|
---|
940 | IOTHUB_MESSAGE_RESULT IoTHubMessage_SetConnectionModuleId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* connectionModuleId)
|
---|
941 | {
|
---|
942 | IOTHUB_MESSAGE_RESULT result;
|
---|
943 |
|
---|
944 | // Codes_SRS_IOTHUBMESSAGE_31_048: [if any of the parameters are NULL then IoTHubMessage_SetConnectionModuleId shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
|
---|
945 | if ((iotHubMessageHandle == NULL) || (connectionModuleId == NULL))
|
---|
946 | {
|
---|
947 | LogError("Invalid argument (iotHubMessageHandle=%p, connectionModuleId=%p)", iotHubMessageHandle, connectionModuleId);
|
---|
948 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
949 | }
|
---|
950 | else
|
---|
951 | {
|
---|
952 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
|
---|
953 |
|
---|
954 | // Codes_SRS_IOTHUBMESSAGE_31_049: [If the IOTHUB_MESSAGE_HANDLE ConnectionModuleId is not NULL, then the IOTHUB_MESSAGE_HANDLE ConnectionModuleId will be deallocated.]
|
---|
955 | if (handleData->connectionModuleId != NULL)
|
---|
956 | {
|
---|
957 | free(handleData->connectionModuleId);
|
---|
958 | handleData->connectionModuleId = NULL;
|
---|
959 | }
|
---|
960 |
|
---|
961 | if (mallocAndStrcpy_s(&handleData->connectionModuleId, connectionModuleId) != 0)
|
---|
962 | {
|
---|
963 | // Codes_SRS_IOTHUBMESSAGE_31_050: [If the allocation or the copying of the ConnectionModuleId fails, then IoTHubMessage_SetConnectionModuleId shall return IOTHUB_MESSAGE_ERROR.]
|
---|
964 | LogError("Failed saving a copy of connectionModuleId");
|
---|
965 | result = IOTHUB_MESSAGE_ERROR;
|
---|
966 | }
|
---|
967 | else
|
---|
968 | {
|
---|
969 | // Codes_SRS_IOTHUBMESSAGE_31_051: [IoTHubMessage_SetConnectionModuleId finishes successfully it shall return IOTHUB_MESSAGE_OK.]
|
---|
970 | result = IOTHUB_MESSAGE_OK;
|
---|
971 | }
|
---|
972 |
|
---|
973 | }
|
---|
974 |
|
---|
975 | return result;
|
---|
976 | }
|
---|
977 |
|
---|
978 |
|
---|
979 | const char* IoTHubMessage_GetConnectionDeviceId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
980 | {
|
---|
981 | const char* result;
|
---|
982 | // Codes_SRS_IOTHUBMESSAGE_31_052: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_GetConnectionDeviceId shall return a NULL value.]
|
---|
983 | if (iotHubMessageHandle == NULL)
|
---|
984 | {
|
---|
985 | LogError("Invalid argument (iotHubMessageHandle is NULL)");
|
---|
986 | result = NULL;
|
---|
987 | }
|
---|
988 | else
|
---|
989 | {
|
---|
990 | // Codes_SRS_IOTHUBMESSAGE_31_053: [IoTHubMessage_GetConnectionDeviceId shall return the ConnectionDeviceId as a const char*.]
|
---|
991 | result = iotHubMessageHandle->connectionDeviceId;
|
---|
992 | }
|
---|
993 | return result;
|
---|
994 | }
|
---|
995 |
|
---|
996 | IOTHUB_MESSAGE_RESULT IoTHubMessage_SetConnectionDeviceId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* connectionDeviceId)
|
---|
997 | {
|
---|
998 | IOTHUB_MESSAGE_RESULT result;
|
---|
999 |
|
---|
1000 | // Codes_SRS_IOTHUBMESSAGE_31_054: [if any of the parameters are NULL then IoTHubMessage_SetConnectionDeviceId shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
|
---|
1001 | if ((iotHubMessageHandle == NULL) || (connectionDeviceId == NULL))
|
---|
1002 | {
|
---|
1003 | LogError("Invalid argument (iotHubMessageHandle=%p, connectionDeviceId=%p)", iotHubMessageHandle, connectionDeviceId);
|
---|
1004 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
1005 | }
|
---|
1006 | else
|
---|
1007 | {
|
---|
1008 | IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
|
---|
1009 |
|
---|
1010 | // Codes_SRS_IOTHUBMESSAGE_31_055: [If the IOTHUB_MESSAGE_HANDLE ConnectionDeviceId is not NULL, then the IOTHUB_MESSAGE_HANDLE ConnectionDeviceId will be deallocated.]
|
---|
1011 | if (handleData->connectionDeviceId != NULL)
|
---|
1012 | {
|
---|
1013 | free(handleData->connectionDeviceId);
|
---|
1014 | handleData->connectionDeviceId = NULL;
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | if (mallocAndStrcpy_s(&handleData->connectionDeviceId, connectionDeviceId) != 0)
|
---|
1018 | {
|
---|
1019 | // Codes_SRS_IOTHUBMESSAGE_31_056: [If the allocation or the copying of the ConnectionDeviceId fails, then IoTHubMessage_SetConnectionDeviceId shall return IOTHUB_MESSAGE_ERROR.]
|
---|
1020 | LogError("Failed saving a copy of connectionDeviceId");
|
---|
1021 | result = IOTHUB_MESSAGE_ERROR;
|
---|
1022 | }
|
---|
1023 | else
|
---|
1024 | {
|
---|
1025 | // Codes_SRS_IOTHUBMESSAGE_31_057: [IoTHubMessage_SetConnectionDeviceId finishes successfully it shall return IOTHUB_MESSAGE_OK.]
|
---|
1026 | result = IOTHUB_MESSAGE_OK;
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | return result;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | IOTHUB_MESSAGE_RESULT IoTHubMessage_SetAsSecurityMessage(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
1035 | {
|
---|
1036 | IOTHUB_MESSAGE_RESULT result;
|
---|
1037 | if (iotHubMessageHandle == NULL)
|
---|
1038 | {
|
---|
1039 | LogError("Invalid argument (iotHubMessageHandle is NULL)");
|
---|
1040 | result = IOTHUB_MESSAGE_INVALID_ARG;
|
---|
1041 | }
|
---|
1042 | else
|
---|
1043 | {
|
---|
1044 | if (set_content_encoding(iotHubMessageHandle, SECURITY_CLIENT_JSON_ENCODING) != 0)
|
---|
1045 | {
|
---|
1046 | LogError("Failure setting security message content encoding");
|
---|
1047 | result = IOTHUB_MESSAGE_ERROR;
|
---|
1048 | }
|
---|
1049 | else
|
---|
1050 | {
|
---|
1051 | iotHubMessageHandle->is_security_message = true;
|
---|
1052 | result = IOTHUB_MESSAGE_OK;
|
---|
1053 | }
|
---|
1054 | }
|
---|
1055 | return result;
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | bool IoTHubMessage_IsSecurityMessage(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
1059 | {
|
---|
1060 | bool result;
|
---|
1061 | if (iotHubMessageHandle == NULL)
|
---|
1062 | {
|
---|
1063 | LogError("Invalid argument (iotHubMessageHandle is NULL)");
|
---|
1064 | result = false;
|
---|
1065 | }
|
---|
1066 | else
|
---|
1067 | {
|
---|
1068 | result = iotHubMessageHandle->is_security_message;
|
---|
1069 | }
|
---|
1070 | return result;
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | void IoTHubMessage_Destroy(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
|
---|
1074 | {
|
---|
1075 | /*Codes_SRS_IOTHUBMESSAGE_01_004: [If iotHubMessageHandle is NULL, IoTHubMessage_Destroy shall do nothing.] */
|
---|
1076 | if (iotHubMessageHandle != NULL)
|
---|
1077 | {
|
---|
1078 | /*Codes_SRS_IOTHUBMESSAGE_01_003: [IoTHubMessage_Destroy shall free all resources associated with iotHubMessageHandle.] */
|
---|
1079 | DestroyMessageData((IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle);
|
---|
1080 | }
|
---|
1081 | }
|
---|