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