Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

This library implements the Microsoft Azure IoTHub client library. The code is replicated from https://github.com/Azure/azure-iot-sdks

Committer:
AzureIoTClient
Date:
Fri Aug 25 11:22:43 2017 -0700
Revision:
74:ea0021abecf7
Parent:
61:8b85a4e797cf
Child:
77:e4e36df9caee
1.1.22

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 0:e393db310d89 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 0:e393db310d89 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 0:e393db310d89 3
AzureIoTClient 0:e393db310d89 4 #include <stdlib.h>
AzureIoTClient 60:41648c4e7036 5 #include "azure_c_shared_utility/optimize_size.h"
Azure.IoT Build 38:a05929a75111 6 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 45:54c11b1b1407 7 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 38:a05929a75111 8 #include "azure_c_shared_utility/buffer_.h"
AzureIoTClient 0:e393db310d89 9
AzureIoTClient 0:e393db310d89 10 #include "iothub_message.h"
AzureIoTClient 0:e393db310d89 11
AzureIoTClient 0:e393db310d89 12 DEFINE_ENUM_STRINGS(IOTHUB_MESSAGE_RESULT, IOTHUB_MESSAGE_RESULT_VALUES);
AzureIoTClient 0:e393db310d89 13 DEFINE_ENUM_STRINGS(IOTHUBMESSAGE_CONTENT_TYPE, IOTHUBMESSAGE_CONTENT_TYPE_VALUES);
AzureIoTClient 0:e393db310d89 14
AzureIoTClient 0:e393db310d89 15 #define LOG_IOTHUB_MESSAGE_ERROR() \
AzureIoTClient 39:2719651a5bee 16 LogError("(result = %s)", ENUM_TO_STRING(IOTHUB_MESSAGE_RESULT, result));
AzureIoTClient 0:e393db310d89 17
AzureIoTClient 0:e393db310d89 18 typedef struct IOTHUB_MESSAGE_HANDLE_DATA_TAG
AzureIoTClient 0:e393db310d89 19 {
AzureIoTClient 0:e393db310d89 20 IOTHUBMESSAGE_CONTENT_TYPE contentType;
AzureIoTClient 0:e393db310d89 21 union
AzureIoTClient 0:e393db310d89 22 {
AzureIoTClient 0:e393db310d89 23 BUFFER_HANDLE byteArray;
AzureIoTClient 0:e393db310d89 24 STRING_HANDLE string;
AzureIoTClient 0:e393db310d89 25 } value;
AzureIoTClient 0:e393db310d89 26 MAP_HANDLE properties;
AzureIoTClient 18:1e9adb15c645 27 char* messageId;
AzureIoTClient 18:1e9adb15c645 28 char* correlationId;
AzureIoTClient 74:ea0021abecf7 29 char* userDefinedContentType;
AzureIoTClient 74:ea0021abecf7 30 char* contentEncoding;
AzureIoTClient 0:e393db310d89 31 }IOTHUB_MESSAGE_HANDLE_DATA;
AzureIoTClient 0:e393db310d89 32
AzureIoTClient 0:e393db310d89 33 static bool ContainsOnlyUsAscii(const char* asciiValue)
AzureIoTClient 0:e393db310d89 34 {
AzureIoTClient 21:3c90c2262ce4 35 bool result = true;
AzureIoTClient 0:e393db310d89 36 const char* iterator = asciiValue;
AzureIoTClient 0:e393db310d89 37 while (iterator != NULL && *iterator != '\0')
AzureIoTClient 0:e393db310d89 38 {
AzureIoTClient 0:e393db310d89 39 // Allow only printable ascii char
AzureIoTClient 0:e393db310d89 40 if (*iterator < ' ' || *iterator > '~')
AzureIoTClient 0:e393db310d89 41 {
AzureIoTClient 0:e393db310d89 42 result = false;
AzureIoTClient 0:e393db310d89 43 break;
AzureIoTClient 0:e393db310d89 44 }
AzureIoTClient 0:e393db310d89 45 iterator++;
AzureIoTClient 0:e393db310d89 46 }
AzureIoTClient 0:e393db310d89 47 return result;
AzureIoTClient 0:e393db310d89 48 }
AzureIoTClient 0:e393db310d89 49
AzureIoTClient 0:e393db310d89 50 /* 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.] */
AzureIoTClient 0:e393db310d89 51 static int ValidateAsciiCharactersFilter(const char* mapKey, const char* mapValue)
AzureIoTClient 0:e393db310d89 52 {
AzureIoTClient 0:e393db310d89 53 int result;
AzureIoTClient 0:e393db310d89 54 if (!ContainsOnlyUsAscii(mapKey) || !ContainsOnlyUsAscii(mapValue) )
AzureIoTClient 0:e393db310d89 55 {
AzureIoTClient 60:41648c4e7036 56 result = __FAILURE__;
AzureIoTClient 0:e393db310d89 57 }
AzureIoTClient 0:e393db310d89 58 else
AzureIoTClient 0:e393db310d89 59 {
AzureIoTClient 0:e393db310d89 60 result = 0;
AzureIoTClient 0:e393db310d89 61 }
AzureIoTClient 0:e393db310d89 62 return result;
AzureIoTClient 0:e393db310d89 63 }
AzureIoTClient 0:e393db310d89 64
AzureIoTClient 0:e393db310d89 65 IOTHUB_MESSAGE_HANDLE IoTHubMessage_CreateFromByteArray(const unsigned char* byteArray, size_t size)
AzureIoTClient 0:e393db310d89 66 {
AzureIoTClient 0:e393db310d89 67 IOTHUB_MESSAGE_HANDLE_DATA* result;
AzureIoTClient 61:8b85a4e797cf 68 if ((byteArray == NULL) && (size != 0))
AzureIoTClient 0:e393db310d89 69 {
AzureIoTClient 61:8b85a4e797cf 70 LogError("Invalid argument - byteArray is NULL");
AzureIoTClient 61:8b85a4e797cf 71 result = NULL;
AzureIoTClient 0:e393db310d89 72 }
AzureIoTClient 0:e393db310d89 73 else
AzureIoTClient 0:e393db310d89 74 {
AzureIoTClient 61:8b85a4e797cf 75 result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
AzureIoTClient 61:8b85a4e797cf 76 if (result == NULL)
AzureIoTClient 61:8b85a4e797cf 77 {
AzureIoTClient 61:8b85a4e797cf 78 LogError("unable to malloc");
AzureIoTClient 61:8b85a4e797cf 79 /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
AzureIoTClient 61:8b85a4e797cf 80 /*let it go through*/
AzureIoTClient 61:8b85a4e797cf 81 }
AzureIoTClient 61:8b85a4e797cf 82 else
AzureIoTClient 0:e393db310d89 83 {
AzureIoTClient 61:8b85a4e797cf 84 const unsigned char* source;
AzureIoTClient 61:8b85a4e797cf 85 unsigned char temp = 0x00;
AzureIoTClient 61:8b85a4e797cf 86 if (size != 0)
AzureIoTClient 0:e393db310d89 87 {
AzureIoTClient 61:8b85a4e797cf 88 /*Codes_SRS_IOTHUBMESSAGE_06_002: [If size is NOT zero then byteArray MUST NOT be NULL*/
AzureIoTClient 61:8b85a4e797cf 89 if (byteArray == NULL)
AzureIoTClient 61:8b85a4e797cf 90 {
AzureIoTClient 61:8b85a4e797cf 91 LogError("Attempted to create a Hub Message from a NULL pointer!");
AzureIoTClient 61:8b85a4e797cf 92 free(result);
AzureIoTClient 61:8b85a4e797cf 93 result = NULL;
AzureIoTClient 61:8b85a4e797cf 94 source = NULL;
AzureIoTClient 61:8b85a4e797cf 95 }
AzureIoTClient 61:8b85a4e797cf 96 else
AzureIoTClient 61:8b85a4e797cf 97 {
AzureIoTClient 61:8b85a4e797cf 98 source = byteArray;
AzureIoTClient 61:8b85a4e797cf 99 }
AzureIoTClient 0:e393db310d89 100 }
AzureIoTClient 0:e393db310d89 101 else
AzureIoTClient 0:e393db310d89 102 {
AzureIoTClient 61:8b85a4e797cf 103 /*Codes_SRS_IOTHUBMESSAGE_06_001: [If size is zero then byteArray may be NULL.]*/
AzureIoTClient 61:8b85a4e797cf 104 source = &temp;
AzureIoTClient 0:e393db310d89 105 }
AzureIoTClient 61:8b85a4e797cf 106 if (result != NULL)
AzureIoTClient 61:8b85a4e797cf 107 {
AzureIoTClient 61:8b85a4e797cf 108 /*Codes_SRS_IOTHUBMESSAGE_02_022: [IoTHubMessage_CreateFromByteArray shall call BUFFER_create passing byteArray and size as parameters.] */
AzureIoTClient 61:8b85a4e797cf 109 if ((result->value.byteArray = BUFFER_create(source, size)) == NULL)
AzureIoTClient 61:8b85a4e797cf 110 {
AzureIoTClient 61:8b85a4e797cf 111 LogError("BUFFER_create failed");
AzureIoTClient 61:8b85a4e797cf 112 /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
AzureIoTClient 61:8b85a4e797cf 113 free(result);
AzureIoTClient 61:8b85a4e797cf 114 result = NULL;
AzureIoTClient 61:8b85a4e797cf 115 }
AzureIoTClient 61:8b85a4e797cf 116 /*Codes_SRS_IOTHUBMESSAGE_02_023: [IoTHubMessage_CreateFromByteArray shall call Map_Create to create the message properties.] */
AzureIoTClient 61:8b85a4e797cf 117 else if ((result->properties = Map_Create(ValidateAsciiCharactersFilter)) == NULL)
AzureIoTClient 61:8b85a4e797cf 118 {
AzureIoTClient 61:8b85a4e797cf 119 LogError("Map_Create failed");
AzureIoTClient 61:8b85a4e797cf 120 /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
AzureIoTClient 61:8b85a4e797cf 121 BUFFER_delete(result->value.byteArray);
AzureIoTClient 61:8b85a4e797cf 122 free(result);
AzureIoTClient 61:8b85a4e797cf 123 result = NULL;
AzureIoTClient 61:8b85a4e797cf 124 }
AzureIoTClient 61:8b85a4e797cf 125 else
AzureIoTClient 61:8b85a4e797cf 126 {
AzureIoTClient 61:8b85a4e797cf 127 /*Codes_SRS_IOTHUBMESSAGE_02_025: [Otherwise, IoTHubMessage_CreateFromByteArray shall return a non-NULL handle.] */
AzureIoTClient 61:8b85a4e797cf 128 /*Codes_SRS_IOTHUBMESSAGE_02_026: [The type of the new message shall be IOTHUBMESSAGE_BYTEARRAY.] */
AzureIoTClient 61:8b85a4e797cf 129 result->contentType = IOTHUBMESSAGE_BYTEARRAY;
AzureIoTClient 61:8b85a4e797cf 130 result->messageId = NULL;
AzureIoTClient 61:8b85a4e797cf 131 result->correlationId = NULL;
AzureIoTClient 74:ea0021abecf7 132 result->userDefinedContentType = NULL;
AzureIoTClient 74:ea0021abecf7 133 result->contentEncoding = NULL;
AzureIoTClient 61:8b85a4e797cf 134 /*all is fine, return result*/
AzureIoTClient 61:8b85a4e797cf 135 }
AzureIoTClient 61:8b85a4e797cf 136 }
AzureIoTClient 61:8b85a4e797cf 137 }
AzureIoTClient 61:8b85a4e797cf 138 }
AzureIoTClient 61:8b85a4e797cf 139 return result;
AzureIoTClient 61:8b85a4e797cf 140 }
AzureIoTClient 61:8b85a4e797cf 141
AzureIoTClient 61:8b85a4e797cf 142 IOTHUB_MESSAGE_HANDLE IoTHubMessage_CreateFromString(const char* source)
AzureIoTClient 61:8b85a4e797cf 143 {
AzureIoTClient 61:8b85a4e797cf 144 IOTHUB_MESSAGE_HANDLE_DATA* result;
AzureIoTClient 61:8b85a4e797cf 145 if (source == NULL)
AzureIoTClient 61:8b85a4e797cf 146 {
AzureIoTClient 61:8b85a4e797cf 147 LogError("Invalid argument - source is NULL");
AzureIoTClient 61:8b85a4e797cf 148 result = NULL;
AzureIoTClient 61:8b85a4e797cf 149 }
AzureIoTClient 61:8b85a4e797cf 150 else
AzureIoTClient 61:8b85a4e797cf 151 {
AzureIoTClient 61:8b85a4e797cf 152 result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
AzureIoTClient 61:8b85a4e797cf 153 if (result == NULL)
AzureIoTClient 61:8b85a4e797cf 154 {
AzureIoTClient 61:8b85a4e797cf 155 LogError("malloc failed");
AzureIoTClient 61:8b85a4e797cf 156 /*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
AzureIoTClient 61:8b85a4e797cf 157 /*let it go through*/
AzureIoTClient 0:e393db310d89 158 }
AzureIoTClient 0:e393db310d89 159 else
AzureIoTClient 0:e393db310d89 160 {
AzureIoTClient 61:8b85a4e797cf 161 /*Codes_SRS_IOTHUBMESSAGE_02_027: [IoTHubMessage_CreateFromString shall call STRING_construct passing source as parameter.] */
AzureIoTClient 61:8b85a4e797cf 162 if ((result->value.string = STRING_construct(source)) == NULL)
AzureIoTClient 0:e393db310d89 163 {
AzureIoTClient 61:8b85a4e797cf 164 LogError("STRING_construct failed");
AzureIoTClient 61:8b85a4e797cf 165 /*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
AzureIoTClient 0:e393db310d89 166 free(result);
AzureIoTClient 0:e393db310d89 167 result = NULL;
AzureIoTClient 0:e393db310d89 168 }
AzureIoTClient 61:8b85a4e797cf 169 /*Codes_SRS_IOTHUBMESSAGE_02_028: [IoTHubMessage_CreateFromString shall call Map_Create to create the message properties.] */
AzureIoTClient 0:e393db310d89 170 else if ((result->properties = Map_Create(ValidateAsciiCharactersFilter)) == NULL)
AzureIoTClient 0:e393db310d89 171 {
AzureIoTClient 39:2719651a5bee 172 LogError("Map_Create failed");
AzureIoTClient 61:8b85a4e797cf 173 /*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
AzureIoTClient 61:8b85a4e797cf 174 STRING_delete(result->value.string);
AzureIoTClient 0:e393db310d89 175 free(result);
AzureIoTClient 0:e393db310d89 176 result = NULL;
AzureIoTClient 0:e393db310d89 177 }
AzureIoTClient 0:e393db310d89 178 else
AzureIoTClient 0:e393db310d89 179 {
AzureIoTClient 61:8b85a4e797cf 180 /*Codes_SRS_IOTHUBMESSAGE_02_031: [Otherwise, IoTHubMessage_CreateFromString shall return a non-NULL handle.] */
AzureIoTClient 61:8b85a4e797cf 181 /*Codes_SRS_IOTHUBMESSAGE_02_032: [The type of the new message shall be IOTHUBMESSAGE_STRING.] */
AzureIoTClient 61:8b85a4e797cf 182 result->contentType = IOTHUBMESSAGE_STRING;
AzureIoTClient 18:1e9adb15c645 183 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 184 result->correlationId = NULL;
AzureIoTClient 74:ea0021abecf7 185 result->userDefinedContentType = NULL;
AzureIoTClient 74:ea0021abecf7 186 result->contentEncoding = NULL;
AzureIoTClient 0:e393db310d89 187 }
AzureIoTClient 0:e393db310d89 188 }
AzureIoTClient 0:e393db310d89 189 }
AzureIoTClient 0:e393db310d89 190 return result;
AzureIoTClient 0:e393db310d89 191 }
AzureIoTClient 0:e393db310d89 192
AzureIoTClient 0:e393db310d89 193 /*Codes_SRS_IOTHUBMESSAGE_03_001: [IoTHubMessage_Clone shall create a new IoT hub message with data content identical to that of the iotHubMessageHandle parameter.]*/
AzureIoTClient 0:e393db310d89 194 IOTHUB_MESSAGE_HANDLE IoTHubMessage_Clone(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 0:e393db310d89 195 {
AzureIoTClient 0:e393db310d89 196 IOTHUB_MESSAGE_HANDLE_DATA* result;
AzureIoTClient 0:e393db310d89 197 const IOTHUB_MESSAGE_HANDLE_DATA* source = (const IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
AzureIoTClient 0:e393db310d89 198 /* Codes_SRS_IOTHUBMESSAGE_03_005: [IoTHubMessage_Clone shall return NULL if iotHubMessageHandle is NULL.] */
AzureIoTClient 0:e393db310d89 199 if (source == NULL)
AzureIoTClient 0:e393db310d89 200 {
AzureIoTClient 0:e393db310d89 201 result = NULL;
AzureIoTClient 39:2719651a5bee 202 LogError("iotHubMessageHandle parameter cannot be NULL for IoTHubMessage_Clone");
AzureIoTClient 0:e393db310d89 203 }
AzureIoTClient 0:e393db310d89 204 else
AzureIoTClient 0:e393db310d89 205 {
AzureIoTClient 0:e393db310d89 206 result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
AzureIoTClient 0:e393db310d89 207 /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
AzureIoTClient 0:e393db310d89 208 if (result == NULL)
AzureIoTClient 0:e393db310d89 209 {
AzureIoTClient 0:e393db310d89 210 /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
AzureIoTClient 0:e393db310d89 211 /*do nothing and return as is*/
AzureIoTClient 39:2719651a5bee 212 LogError("unable to malloc");
AzureIoTClient 0:e393db310d89 213 }
AzureIoTClient 0:e393db310d89 214 else
AzureIoTClient 0:e393db310d89 215 {
AzureIoTClient 18:1e9adb15c645 216 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 217 result->correlationId = NULL;
AzureIoTClient 74:ea0021abecf7 218 result->userDefinedContentType = NULL;
AzureIoTClient 74:ea0021abecf7 219 result->contentEncoding = NULL;
AzureIoTClient 74:ea0021abecf7 220
AzureIoTClient 18:1e9adb15c645 221 if (source->messageId != NULL && mallocAndStrcpy_s(&result->messageId, source->messageId) != 0)
AzureIoTClient 18:1e9adb15c645 222 {
AzureIoTClient 39:2719651a5bee 223 LogError("unable to Copy messageId");
AzureIoTClient 18:1e9adb15c645 224 free(result);
AzureIoTClient 18:1e9adb15c645 225 result = NULL;
AzureIoTClient 18:1e9adb15c645 226 }
AzureIoTClient 18:1e9adb15c645 227 else if (source->correlationId != NULL && mallocAndStrcpy_s(&result->correlationId, source->correlationId) != 0)
AzureIoTClient 18:1e9adb15c645 228 {
AzureIoTClient 39:2719651a5bee 229 LogError("unable to Copy correlationId");
AzureIoTClient 18:1e9adb15c645 230 if (result->messageId != NULL)
AzureIoTClient 18:1e9adb15c645 231 {
AzureIoTClient 18:1e9adb15c645 232 free(result->messageId);
AzureIoTClient 74:ea0021abecf7 233 }
AzureIoTClient 74:ea0021abecf7 234 free(result);
AzureIoTClient 74:ea0021abecf7 235 result = NULL;
AzureIoTClient 74:ea0021abecf7 236 }
AzureIoTClient 74:ea0021abecf7 237 else if (source->userDefinedContentType != NULL && mallocAndStrcpy_s(&result->userDefinedContentType, source->userDefinedContentType) != 0)
AzureIoTClient 74:ea0021abecf7 238 {
AzureIoTClient 74:ea0021abecf7 239 LogError("unable to copy contentType");
AzureIoTClient 74:ea0021abecf7 240 if (result->messageId != NULL)
AzureIoTClient 74:ea0021abecf7 241 {
AzureIoTClient 74:ea0021abecf7 242 free(result->messageId);
AzureIoTClient 74:ea0021abecf7 243 }
AzureIoTClient 74:ea0021abecf7 244 if (result->correlationId != NULL)
AzureIoTClient 74:ea0021abecf7 245 {
AzureIoTClient 74:ea0021abecf7 246 free(result->correlationId);
AzureIoTClient 74:ea0021abecf7 247 }
AzureIoTClient 74:ea0021abecf7 248 free(result);
AzureIoTClient 74:ea0021abecf7 249 result = NULL;
AzureIoTClient 74:ea0021abecf7 250 }
AzureIoTClient 74:ea0021abecf7 251 else if (source->contentEncoding != NULL && mallocAndStrcpy_s(&result->contentEncoding, source->contentEncoding) != 0)
AzureIoTClient 74:ea0021abecf7 252 {
AzureIoTClient 74:ea0021abecf7 253 LogError("unable to copy contentEncoding");
AzureIoTClient 74:ea0021abecf7 254 if (result->messageId != NULL)
AzureIoTClient 74:ea0021abecf7 255 {
AzureIoTClient 74:ea0021abecf7 256 free(result->messageId);
AzureIoTClient 74:ea0021abecf7 257 }
AzureIoTClient 74:ea0021abecf7 258 if (result->correlationId != NULL)
AzureIoTClient 74:ea0021abecf7 259 {
AzureIoTClient 74:ea0021abecf7 260 free(result->correlationId);
AzureIoTClient 74:ea0021abecf7 261 }
AzureIoTClient 74:ea0021abecf7 262 if (result->userDefinedContentType != NULL)
AzureIoTClient 74:ea0021abecf7 263 {
AzureIoTClient 74:ea0021abecf7 264 free(result->userDefinedContentType);
AzureIoTClient 18:1e9adb15c645 265 }
AzureIoTClient 18:1e9adb15c645 266 free(result);
AzureIoTClient 18:1e9adb15c645 267 result = NULL;
AzureIoTClient 18:1e9adb15c645 268 }
AzureIoTClient 18:1e9adb15c645 269 else if (source->contentType == IOTHUBMESSAGE_BYTEARRAY)
AzureIoTClient 0:e393db310d89 270 {
AzureIoTClient 0:e393db310d89 271 /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone to content by a call to BUFFER_clone] */
AzureIoTClient 0:e393db310d89 272 if ((result->value.byteArray = BUFFER_clone(source->value.byteArray)) == NULL)
AzureIoTClient 0:e393db310d89 273 {
AzureIoTClient 0:e393db310d89 274 /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
AzureIoTClient 39:2719651a5bee 275 LogError("unable to BUFFER_clone");
AzureIoTClient 18:1e9adb15c645 276 if (result->messageId)
AzureIoTClient 18:1e9adb15c645 277 {
AzureIoTClient 18:1e9adb15c645 278 free(result->messageId);
AzureIoTClient 18:1e9adb15c645 279 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 280 }
AzureIoTClient 18:1e9adb15c645 281 if (result->correlationId != NULL)
AzureIoTClient 18:1e9adb15c645 282 {
AzureIoTClient 18:1e9adb15c645 283 free(result->correlationId);
AzureIoTClient 18:1e9adb15c645 284 result->correlationId = NULL;
AzureIoTClient 18:1e9adb15c645 285 }
AzureIoTClient 0:e393db310d89 286 free(result);
AzureIoTClient 0:e393db310d89 287 result = NULL;
AzureIoTClient 0:e393db310d89 288 }
AzureIoTClient 0:e393db310d89 289 /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */
AzureIoTClient 0:e393db310d89 290 else if ((result->properties = Map_Clone(source->properties)) == NULL)
AzureIoTClient 0:e393db310d89 291 {
AzureIoTClient 0:e393db310d89 292 /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
AzureIoTClient 39:2719651a5bee 293 LogError("unable to Map_Clone");
AzureIoTClient 0:e393db310d89 294 BUFFER_delete(result->value.byteArray);
AzureIoTClient 18:1e9adb15c645 295 if (result->messageId)
AzureIoTClient 18:1e9adb15c645 296 {
AzureIoTClient 18:1e9adb15c645 297 free(result->messageId);
AzureIoTClient 18:1e9adb15c645 298 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 299 }
AzureIoTClient 18:1e9adb15c645 300 if (result->correlationId != NULL)
AzureIoTClient 18:1e9adb15c645 301 {
AzureIoTClient 18:1e9adb15c645 302 free(result->correlationId);
AzureIoTClient 18:1e9adb15c645 303 result->correlationId = NULL;
AzureIoTClient 18:1e9adb15c645 304 }
AzureIoTClient 0:e393db310d89 305 free(result);
AzureIoTClient 0:e393db310d89 306 result = NULL;
AzureIoTClient 0:e393db310d89 307 }
AzureIoTClient 0:e393db310d89 308 else
AzureIoTClient 0:e393db310d89 309 {
AzureIoTClient 0:e393db310d89 310 result->contentType = IOTHUBMESSAGE_BYTEARRAY;
AzureIoTClient 0:e393db310d89 311 /*Codes_SRS_IOTHUBMESSAGE_03_002: [IoTHubMessage_Clone shall return upon success a non-NULL handle to the newly created IoT hub message.]*/
AzureIoTClient 0:e393db310d89 312 /*return as is, this is a good result*/
AzureIoTClient 0:e393db310d89 313 }
AzureIoTClient 0:e393db310d89 314 }
AzureIoTClient 0:e393db310d89 315 else /*can only be STRING*/
AzureIoTClient 0:e393db310d89 316 {
AzureIoTClient 0:e393db310d89 317 /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone the content by a call to BUFFER_clone or STRING_clone] */
AzureIoTClient 0:e393db310d89 318 if ((result->value.string = STRING_clone(source->value.string)) == NULL)
AzureIoTClient 0:e393db310d89 319 {
AzureIoTClient 0:e393db310d89 320 /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
AzureIoTClient 18:1e9adb15c645 321 if (result->messageId)
AzureIoTClient 18:1e9adb15c645 322 {
AzureIoTClient 18:1e9adb15c645 323 free(result->messageId);
AzureIoTClient 18:1e9adb15c645 324 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 325 }
AzureIoTClient 18:1e9adb15c645 326 if (result->correlationId != NULL)
AzureIoTClient 18:1e9adb15c645 327 {
AzureIoTClient 18:1e9adb15c645 328 free(result->correlationId);
AzureIoTClient 18:1e9adb15c645 329 result->correlationId = NULL;
AzureIoTClient 18:1e9adb15c645 330 }
AzureIoTClient 0:e393db310d89 331 free(result);
AzureIoTClient 0:e393db310d89 332 result = NULL;
AzureIoTClient 39:2719651a5bee 333 LogError("failed to STRING_clone");
AzureIoTClient 0:e393db310d89 334 }
AzureIoTClient 0:e393db310d89 335 /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */
AzureIoTClient 0:e393db310d89 336 else if ((result->properties = Map_Clone(source->properties)) == NULL)
AzureIoTClient 0:e393db310d89 337 {
AzureIoTClient 0:e393db310d89 338 /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
AzureIoTClient 39:2719651a5bee 339 LogError("unable to Map_Clone");
AzureIoTClient 0:e393db310d89 340 STRING_delete(result->value.string);
AzureIoTClient 18:1e9adb15c645 341 if (result->messageId)
AzureIoTClient 18:1e9adb15c645 342 {
AzureIoTClient 18:1e9adb15c645 343 free(result->messageId);
AzureIoTClient 18:1e9adb15c645 344 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 345 }
AzureIoTClient 18:1e9adb15c645 346 if (result->correlationId != NULL)
AzureIoTClient 18:1e9adb15c645 347 {
AzureIoTClient 18:1e9adb15c645 348 free(result->correlationId);
AzureIoTClient 18:1e9adb15c645 349 result->correlationId = NULL;
AzureIoTClient 18:1e9adb15c645 350 }
AzureIoTClient 0:e393db310d89 351 free(result);
AzureIoTClient 0:e393db310d89 352 result = NULL;
AzureIoTClient 0:e393db310d89 353 }
AzureIoTClient 0:e393db310d89 354 else
AzureIoTClient 0:e393db310d89 355 {
AzureIoTClient 0:e393db310d89 356 result->contentType = IOTHUBMESSAGE_STRING;
AzureIoTClient 0:e393db310d89 357 /*all is fine*/
AzureIoTClient 0:e393db310d89 358 }
AzureIoTClient 0:e393db310d89 359 }
AzureIoTClient 0:e393db310d89 360 }
AzureIoTClient 0:e393db310d89 361 }
AzureIoTClient 0:e393db310d89 362 return result;
AzureIoTClient 0:e393db310d89 363 }
AzureIoTClient 0:e393db310d89 364
AzureIoTClient 0:e393db310d89 365 IOTHUB_MESSAGE_RESULT IoTHubMessage_GetByteArray(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const unsigned char** buffer, size_t* size)
AzureIoTClient 0:e393db310d89 366 {
AzureIoTClient 0:e393db310d89 367 IOTHUB_MESSAGE_RESULT result;
AzureIoTClient 0:e393db310d89 368 if (
AzureIoTClient 0:e393db310d89 369 (iotHubMessageHandle == NULL) ||
AzureIoTClient 0:e393db310d89 370 (buffer == NULL) ||
AzureIoTClient 0:e393db310d89 371 (size == NULL)
AzureIoTClient 0:e393db310d89 372 )
AzureIoTClient 0:e393db310d89 373 {
AzureIoTClient 0:e393db310d89 374 /*Codes_SRS_IOTHUBMESSAGE_01_014: [If any of the arguments passed to IoTHubMessage_GetByteArray is NULL IoTHubMessage_GetByteArray shall return IOTHUBMESSAGE_INVALID_ARG.] */
AzureIoTClient 39:2719651a5bee 375 LogError("invalid parameter (NULL) to IoTHubMessage_GetByteArray IOTHUB_MESSAGE_HANDLE iotHubMessageHandle=%p, const unsigned char** buffer=%p, size_t* size=%p", iotHubMessageHandle, buffer, size);
AzureIoTClient 0:e393db310d89 376 result = IOTHUB_MESSAGE_INVALID_ARG;
AzureIoTClient 0:e393db310d89 377 }
AzureIoTClient 0:e393db310d89 378 else
AzureIoTClient 0:e393db310d89 379 {
AzureIoTClient 0:e393db310d89 380 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 0:e393db310d89 381 if (handleData->contentType != IOTHUBMESSAGE_BYTEARRAY)
AzureIoTClient 0:e393db310d89 382 {
AzureIoTClient 0:e393db310d89 383 /*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.] */
AzureIoTClient 0:e393db310d89 384 result = IOTHUB_MESSAGE_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 385 LogError("invalid type of message %s", ENUM_TO_STRING(IOTHUBMESSAGE_CONTENT_TYPE, handleData->contentType));
AzureIoTClient 0:e393db310d89 386 }
AzureIoTClient 0:e393db310d89 387 else
AzureIoTClient 0:e393db310d89 388 {
AzureIoTClient 0:e393db310d89 389 /*Codes_SRS_IOTHUBMESSAGE_01_011: [The pointer shall be obtained by using BUFFER_u_char and it shall be copied in the buffer argument.]*/
AzureIoTClient 0:e393db310d89 390 *buffer = BUFFER_u_char(handleData->value.byteArray);
AzureIoTClient 0:e393db310d89 391 /*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.]*/
AzureIoTClient 0:e393db310d89 392 *size = BUFFER_length(handleData->value.byteArray);
AzureIoTClient 0:e393db310d89 393 result = IOTHUB_MESSAGE_OK;
AzureIoTClient 0:e393db310d89 394 }
AzureIoTClient 0:e393db310d89 395 }
AzureIoTClient 0:e393db310d89 396 return result;
AzureIoTClient 0:e393db310d89 397 }
AzureIoTClient 0:e393db310d89 398
AzureIoTClient 0:e393db310d89 399 const char* IoTHubMessage_GetString(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 0:e393db310d89 400 {
AzureIoTClient 0:e393db310d89 401 const char* result;
AzureIoTClient 0:e393db310d89 402 if (iotHubMessageHandle == NULL)
AzureIoTClient 0:e393db310d89 403 {
AzureIoTClient 0:e393db310d89 404 /*Codes_SRS_IOTHUBMESSAGE_02_016: [If any parameter is NULL then IoTHubMessage_GetString shall return NULL.] */
AzureIoTClient 0:e393db310d89 405 result = NULL;
AzureIoTClient 0:e393db310d89 406 }
AzureIoTClient 0:e393db310d89 407 else
AzureIoTClient 0:e393db310d89 408 {
AzureIoTClient 0:e393db310d89 409 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 0:e393db310d89 410 if (handleData->contentType != IOTHUBMESSAGE_STRING)
AzureIoTClient 0:e393db310d89 411 {
AzureIoTClient 0:e393db310d89 412 /*Codes_SRS_IOTHUBMESSAGE_02_017: [IoTHubMessage_GetString shall return NULL if the iotHubMessageHandle does not refer to a IOTHUBMESSAGE of type STRING.] */
AzureIoTClient 0:e393db310d89 413 result = NULL;
AzureIoTClient 0:e393db310d89 414 }
AzureIoTClient 0:e393db310d89 415 else
AzureIoTClient 0:e393db310d89 416 {
AzureIoTClient 0:e393db310d89 417 /*Codes_SRS_IOTHUBMESSAGE_02_018: [IoTHubMessage_GetStringData shall return the currently stored null terminated string.] */
AzureIoTClient 0:e393db310d89 418 result = STRING_c_str(handleData->value.string);
AzureIoTClient 0:e393db310d89 419 }
AzureIoTClient 0:e393db310d89 420 }
AzureIoTClient 0:e393db310d89 421 return result;
AzureIoTClient 0:e393db310d89 422 }
AzureIoTClient 0:e393db310d89 423
AzureIoTClient 0:e393db310d89 424 IOTHUBMESSAGE_CONTENT_TYPE IoTHubMessage_GetContentType(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 0:e393db310d89 425 {
AzureIoTClient 0:e393db310d89 426 IOTHUBMESSAGE_CONTENT_TYPE result;
AzureIoTClient 0:e393db310d89 427 /*Codes_SRS_IOTHUBMESSAGE_02_008: [If any parameter is NULL then IoTHubMessage_GetContentType shall return IOTHUBMESSAGE_UNKNOWN.] */
AzureIoTClient 0:e393db310d89 428 if (iotHubMessageHandle == NULL)
AzureIoTClient 0:e393db310d89 429 {
AzureIoTClient 0:e393db310d89 430 result = IOTHUBMESSAGE_UNKNOWN;
AzureIoTClient 0:e393db310d89 431 }
AzureIoTClient 0:e393db310d89 432 else
AzureIoTClient 0:e393db310d89 433 {
AzureIoTClient 0:e393db310d89 434 /*Codes_SRS_IOTHUBMESSAGE_02_009: [Otherwise IoTHubMessage_GetContentType shall return the type of the message.] */
AzureIoTClient 0:e393db310d89 435 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 0:e393db310d89 436 result = handleData->contentType;
AzureIoTClient 0:e393db310d89 437 }
AzureIoTClient 0:e393db310d89 438 return result;
AzureIoTClient 0:e393db310d89 439 }
AzureIoTClient 0:e393db310d89 440
AzureIoTClient 0:e393db310d89 441 MAP_HANDLE IoTHubMessage_Properties(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 0:e393db310d89 442 {
AzureIoTClient 0:e393db310d89 443 MAP_HANDLE result;
AzureIoTClient 0:e393db310d89 444 /*Codes_SRS_IOTHUBMESSAGE_02_001: [If iotHubMessageHandle is NULL then IoTHubMessage_Properties shall return NULL.]*/
AzureIoTClient 0:e393db310d89 445 if (iotHubMessageHandle == NULL)
AzureIoTClient 0:e393db310d89 446 {
AzureIoTClient 39:2719651a5bee 447 LogError("invalid arg (NULL) passed to IoTHubMessage_Properties");
AzureIoTClient 18:1e9adb15c645 448 result = NULL;
AzureIoTClient 0:e393db310d89 449 }
AzureIoTClient 0:e393db310d89 450 else
AzureIoTClient 0:e393db310d89 451 {
AzureIoTClient 0:e393db310d89 452 /*Codes_SRS_IOTHUBMESSAGE_02_002: [Otherwise, for any non-NULL iotHubMessageHandle it shall return a non-NULL MAP_HANDLE.]*/
AzureIoTClient 0:e393db310d89 453 IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
AzureIoTClient 0:e393db310d89 454 result = handleData->properties;
AzureIoTClient 0:e393db310d89 455 }
AzureIoTClient 0:e393db310d89 456 return result;
AzureIoTClient 0:e393db310d89 457 }
AzureIoTClient 0:e393db310d89 458
AzureIoTClient 18:1e9adb15c645 459 const char* IoTHubMessage_GetCorrelationId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 18:1e9adb15c645 460 {
AzureIoTClient 18:1e9adb15c645 461 const char* result;
AzureIoTClient 18:1e9adb15c645 462 /* Codes_SRS_IOTHUBMESSAGE_07_016: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_GetCorrelationId shall return a NULL value.] */
AzureIoTClient 18:1e9adb15c645 463 if (iotHubMessageHandle == NULL)
AzureIoTClient 18:1e9adb15c645 464 {
AzureIoTClient 39:2719651a5bee 465 LogError("invalid arg (NULL) passed to IoTHubMessage_GetCorrelationId");
AzureIoTClient 18:1e9adb15c645 466 result = NULL;
AzureIoTClient 18:1e9adb15c645 467 }
AzureIoTClient 18:1e9adb15c645 468 else
AzureIoTClient 18:1e9adb15c645 469 {
AzureIoTClient 18:1e9adb15c645 470 /* Codes_SRS_IOTHUBMESSAGE_07_017: [IoTHubMessage_GetCorrelationId shall return the correlationId as a const char*.] */
AzureIoTClient 18:1e9adb15c645 471 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 18:1e9adb15c645 472 result = handleData->correlationId;
AzureIoTClient 18:1e9adb15c645 473 }
AzureIoTClient 18:1e9adb15c645 474 return result;
AzureIoTClient 18:1e9adb15c645 475 }
AzureIoTClient 18:1e9adb15c645 476
AzureIoTClient 18:1e9adb15c645 477 IOTHUB_MESSAGE_RESULT IoTHubMessage_SetCorrelationId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* correlationId)
AzureIoTClient 18:1e9adb15c645 478 {
AzureIoTClient 18:1e9adb15c645 479 IOTHUB_MESSAGE_RESULT result;
AzureIoTClient 18:1e9adb15c645 480 /* Codes_SRS_IOTHUBMESSAGE_07_018: [if any of the parameters are NULL then IoTHubMessage_SetCorrelationId shall return a IOTHUB_MESSAGE_INVALID_ARG value.]*/
AzureIoTClient 18:1e9adb15c645 481 if (iotHubMessageHandle == NULL || correlationId == NULL)
AzureIoTClient 18:1e9adb15c645 482 {
AzureIoTClient 39:2719651a5bee 483 LogError("invalid arg (NULL) passed to IoTHubMessage_SetCorrelationId");
AzureIoTClient 18:1e9adb15c645 484 result = IOTHUB_MESSAGE_INVALID_ARG;
AzureIoTClient 18:1e9adb15c645 485 }
AzureIoTClient 18:1e9adb15c645 486 else
AzureIoTClient 18:1e9adb15c645 487 {
AzureIoTClient 18:1e9adb15c645 488 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 18:1e9adb15c645 489 /* Codes_SRS_IOTHUBMESSAGE_07_019: [If the IOTHUB_MESSAGE_HANDLE correlationId is not NULL, then the IOTHUB_MESSAGE_HANDLE correlationId will be deallocated.] */
AzureIoTClient 18:1e9adb15c645 490 if (handleData->correlationId != NULL)
AzureIoTClient 18:1e9adb15c645 491 {
AzureIoTClient 18:1e9adb15c645 492 free(handleData->correlationId);
AzureIoTClient 74:ea0021abecf7 493 handleData->correlationId = NULL;
AzureIoTClient 18:1e9adb15c645 494 }
AzureIoTClient 18:1e9adb15c645 495
AzureIoTClient 18:1e9adb15c645 496 if (mallocAndStrcpy_s(&handleData->correlationId, correlationId) != 0)
AzureIoTClient 18:1e9adb15c645 497 {
AzureIoTClient 18:1e9adb15c645 498 /* Codes_SRS_IOTHUBMESSAGE_07_020: [If the allocation or the copying of the correlationId fails, then IoTHubMessage_SetCorrelationId shall return IOTHUB_MESSAGE_ERROR.] */
AzureIoTClient 18:1e9adb15c645 499 result = IOTHUB_MESSAGE_ERROR;
AzureIoTClient 18:1e9adb15c645 500 }
AzureIoTClient 18:1e9adb15c645 501 else
AzureIoTClient 18:1e9adb15c645 502 {
AzureIoTClient 18:1e9adb15c645 503 /* Codes_SRS_IOTHUBMESSAGE_07_021: [IoTHubMessage_SetCorrelationId finishes successfully it shall return IOTHUB_MESSAGE_OK.] */
AzureIoTClient 18:1e9adb15c645 504 result = IOTHUB_MESSAGE_OK;
AzureIoTClient 18:1e9adb15c645 505 }
AzureIoTClient 18:1e9adb15c645 506 }
AzureIoTClient 18:1e9adb15c645 507 return result;
AzureIoTClient 18:1e9adb15c645 508 }
AzureIoTClient 18:1e9adb15c645 509
AzureIoTClient 18:1e9adb15c645 510 IOTHUB_MESSAGE_RESULT IoTHubMessage_SetMessageId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* messageId)
AzureIoTClient 18:1e9adb15c645 511 {
AzureIoTClient 18:1e9adb15c645 512 IOTHUB_MESSAGE_RESULT result;
AzureIoTClient 18:1e9adb15c645 513 /* Codes_SRS_IOTHUBMESSAGE_07_012: [if any of the parameters are NULL then IoTHubMessage_SetMessageId shall return a IOTHUB_MESSAGE_INVALID_ARG value.] */
AzureIoTClient 18:1e9adb15c645 514 if (iotHubMessageHandle == NULL || messageId == NULL)
AzureIoTClient 18:1e9adb15c645 515 {
AzureIoTClient 39:2719651a5bee 516 LogError("invalid arg (NULL) passed to IoTHubMessage_SetMessageId");
AzureIoTClient 18:1e9adb15c645 517 result = IOTHUB_MESSAGE_INVALID_ARG;
AzureIoTClient 18:1e9adb15c645 518 }
AzureIoTClient 18:1e9adb15c645 519 else
AzureIoTClient 18:1e9adb15c645 520 {
AzureIoTClient 18:1e9adb15c645 521 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 18:1e9adb15c645 522 /* Codes_SRS_IOTHUBMESSAGE_07_013: [If the IOTHUB_MESSAGE_HANDLE messageId is not NULL, then the IOTHUB_MESSAGE_HANDLE messageId will be freed] */
AzureIoTClient 18:1e9adb15c645 523 if (handleData->messageId != NULL)
AzureIoTClient 18:1e9adb15c645 524 {
AzureIoTClient 18:1e9adb15c645 525 free(handleData->messageId);
AzureIoTClient 74:ea0021abecf7 526 handleData->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 527 }
AzureIoTClient 18:1e9adb15c645 528
AzureIoTClient 18:1e9adb15c645 529 /* Codes_SRS_IOTHUBMESSAGE_07_014: [If the allocation or the copying of the messageId fails, then IoTHubMessage_SetMessageId shall return IOTHUB_MESSAGE_ERROR.] */
AzureIoTClient 18:1e9adb15c645 530 if (mallocAndStrcpy_s(&handleData->messageId, messageId) != 0)
AzureIoTClient 18:1e9adb15c645 531 {
AzureIoTClient 18:1e9adb15c645 532 result = IOTHUB_MESSAGE_ERROR;
AzureIoTClient 18:1e9adb15c645 533 }
AzureIoTClient 18:1e9adb15c645 534 else
AzureIoTClient 18:1e9adb15c645 535 {
AzureIoTClient 18:1e9adb15c645 536 result = IOTHUB_MESSAGE_OK;
AzureIoTClient 18:1e9adb15c645 537 }
AzureIoTClient 18:1e9adb15c645 538 }
AzureIoTClient 18:1e9adb15c645 539 return result;
AzureIoTClient 18:1e9adb15c645 540 }
AzureIoTClient 18:1e9adb15c645 541
AzureIoTClient 18:1e9adb15c645 542 const char* IoTHubMessage_GetMessageId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 18:1e9adb15c645 543 {
AzureIoTClient 18:1e9adb15c645 544 const char* result;
AzureIoTClient 18:1e9adb15c645 545 /* Codes_SRS_IOTHUBMESSAGE_07_010: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_MessageId shall return a NULL value.] */
AzureIoTClient 18:1e9adb15c645 546 if (iotHubMessageHandle == NULL)
AzureIoTClient 18:1e9adb15c645 547 {
AzureIoTClient 39:2719651a5bee 548 LogError("invalid arg (NULL) passed to IoTHubMessage_GetMessageId");
AzureIoTClient 18:1e9adb15c645 549 result = NULL;
AzureIoTClient 18:1e9adb15c645 550 }
AzureIoTClient 18:1e9adb15c645 551 else
AzureIoTClient 18:1e9adb15c645 552 {
AzureIoTClient 18:1e9adb15c645 553 /* Codes_SRS_IOTHUBMESSAGE_07_011: [IoTHubMessage_MessageId shall return the messageId as a const char*.] */
AzureIoTClient 18:1e9adb15c645 554 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 18:1e9adb15c645 555 result = handleData->messageId;
AzureIoTClient 18:1e9adb15c645 556 }
AzureIoTClient 18:1e9adb15c645 557 return result;
AzureIoTClient 18:1e9adb15c645 558 }
AzureIoTClient 18:1e9adb15c645 559
AzureIoTClient 74:ea0021abecf7 560 IOTHUB_MESSAGE_RESULT IoTHubMessage_SetContentTypeSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* contentType)
AzureIoTClient 74:ea0021abecf7 561 {
AzureIoTClient 74:ea0021abecf7 562 IOTHUB_MESSAGE_RESULT result;
AzureIoTClient 74:ea0021abecf7 563
AzureIoTClient 74:ea0021abecf7 564 // Codes_SRS_IOTHUBMESSAGE_09_001: [If any of the parameters are NULL then IoTHubMessage_SetContentTypeSystemProperty shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
AzureIoTClient 74:ea0021abecf7 565 if (iotHubMessageHandle == NULL || contentType == NULL)
AzureIoTClient 74:ea0021abecf7 566 {
AzureIoTClient 74:ea0021abecf7 567 LogError("Invalid argument (iotHubMessageHandle=%p, contentType=%p)", iotHubMessageHandle, contentType);
AzureIoTClient 74:ea0021abecf7 568 result = IOTHUB_MESSAGE_INVALID_ARG;
AzureIoTClient 74:ea0021abecf7 569 }
AzureIoTClient 74:ea0021abecf7 570 else
AzureIoTClient 74:ea0021abecf7 571 {
AzureIoTClient 74:ea0021abecf7 572 IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
AzureIoTClient 74:ea0021abecf7 573
AzureIoTClient 74:ea0021abecf7 574 // Codes_SRS_IOTHUBMESSAGE_09_002: [If the IOTHUB_MESSAGE_HANDLE `contentType` is not NULL it shall be deallocated.]
AzureIoTClient 74:ea0021abecf7 575 if (handleData->userDefinedContentType != NULL)
AzureIoTClient 74:ea0021abecf7 576 {
AzureIoTClient 74:ea0021abecf7 577 free(handleData->userDefinedContentType);
AzureIoTClient 74:ea0021abecf7 578 handleData->userDefinedContentType = NULL;
AzureIoTClient 74:ea0021abecf7 579 }
AzureIoTClient 74:ea0021abecf7 580
AzureIoTClient 74:ea0021abecf7 581 if (mallocAndStrcpy_s(&handleData->userDefinedContentType, contentType) != 0)
AzureIoTClient 74:ea0021abecf7 582 {
AzureIoTClient 74:ea0021abecf7 583 LogError("Failed saving a copy of contentType");
AzureIoTClient 74:ea0021abecf7 584 // Codes_SRS_IOTHUBMESSAGE_09_003: [If the allocation or the copying of `contentType` fails, then IoTHubMessage_SetContentTypeSystemProperty shall return IOTHUB_MESSAGE_ERROR.]
AzureIoTClient 74:ea0021abecf7 585 result = IOTHUB_MESSAGE_ERROR;
AzureIoTClient 74:ea0021abecf7 586 }
AzureIoTClient 74:ea0021abecf7 587 else
AzureIoTClient 74:ea0021abecf7 588 {
AzureIoTClient 74:ea0021abecf7 589 // Codes_SRS_IOTHUBMESSAGE_09_004: [If IoTHubMessage_SetContentTypeSystemProperty finishes successfully it shall return IOTHUB_MESSAGE_OK.]
AzureIoTClient 74:ea0021abecf7 590 result = IOTHUB_MESSAGE_OK;
AzureIoTClient 74:ea0021abecf7 591 }
AzureIoTClient 74:ea0021abecf7 592 }
AzureIoTClient 74:ea0021abecf7 593
AzureIoTClient 74:ea0021abecf7 594 return result;
AzureIoTClient 74:ea0021abecf7 595 }
AzureIoTClient 74:ea0021abecf7 596
AzureIoTClient 74:ea0021abecf7 597 const char* IoTHubMessage_GetContentTypeSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 74:ea0021abecf7 598 {
AzureIoTClient 74:ea0021abecf7 599 const char* result;
AzureIoTClient 74:ea0021abecf7 600
AzureIoTClient 74:ea0021abecf7 601 // Codes_SRS_IOTHUBMESSAGE_09_005: [If any of the parameters are NULL then IoTHubMessage_GetContentTypeSystemProperty shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
AzureIoTClient 74:ea0021abecf7 602 if (iotHubMessageHandle == NULL)
AzureIoTClient 74:ea0021abecf7 603 {
AzureIoTClient 74:ea0021abecf7 604 LogError("Invalid argument (iotHubMessageHandle is NULL)");
AzureIoTClient 74:ea0021abecf7 605 result = NULL;
AzureIoTClient 74:ea0021abecf7 606 }
AzureIoTClient 74:ea0021abecf7 607 else
AzureIoTClient 74:ea0021abecf7 608 {
AzureIoTClient 74:ea0021abecf7 609 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 74:ea0021abecf7 610
AzureIoTClient 74:ea0021abecf7 611 // Codes_SRS_IOTHUBMESSAGE_09_006: [IoTHubMessage_GetContentTypeSystemProperty shall return the `contentType` as a const char* ]
AzureIoTClient 74:ea0021abecf7 612 result = (const char*)handleData->userDefinedContentType;
AzureIoTClient 74:ea0021abecf7 613 }
AzureIoTClient 74:ea0021abecf7 614
AzureIoTClient 74:ea0021abecf7 615 return result;
AzureIoTClient 74:ea0021abecf7 616 }
AzureIoTClient 74:ea0021abecf7 617
AzureIoTClient 74:ea0021abecf7 618 IOTHUB_MESSAGE_RESULT IoTHubMessage_SetContentEncodingSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* contentEncoding)
AzureIoTClient 74:ea0021abecf7 619 {
AzureIoTClient 74:ea0021abecf7 620 IOTHUB_MESSAGE_RESULT result;
AzureIoTClient 74:ea0021abecf7 621
AzureIoTClient 74:ea0021abecf7 622 // Codes_SRS_IOTHUBMESSAGE_09_006: [If any of the parameters are NULL then IoTHubMessage_SetContentEncodingSystemProperty shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
AzureIoTClient 74:ea0021abecf7 623 if (iotHubMessageHandle == NULL || contentEncoding == NULL)
AzureIoTClient 74:ea0021abecf7 624 {
AzureIoTClient 74:ea0021abecf7 625 LogError("Invalid argument (iotHubMessageHandle=%p, contentEncoding=%p)", iotHubMessageHandle, contentEncoding);
AzureIoTClient 74:ea0021abecf7 626 result = IOTHUB_MESSAGE_INVALID_ARG;
AzureIoTClient 74:ea0021abecf7 627 }
AzureIoTClient 74:ea0021abecf7 628 else
AzureIoTClient 74:ea0021abecf7 629 {
AzureIoTClient 74:ea0021abecf7 630 IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
AzureIoTClient 74:ea0021abecf7 631
AzureIoTClient 74:ea0021abecf7 632 // Codes_SRS_IOTHUBMESSAGE_09_007: [If the IOTHUB_MESSAGE_HANDLE `contentEncoding` is not NULL it shall be deallocated.]
AzureIoTClient 74:ea0021abecf7 633 if (handleData->contentEncoding != NULL)
AzureIoTClient 74:ea0021abecf7 634 {
AzureIoTClient 74:ea0021abecf7 635 free(handleData->contentEncoding);
AzureIoTClient 74:ea0021abecf7 636 handleData->contentEncoding = NULL;
AzureIoTClient 74:ea0021abecf7 637 }
AzureIoTClient 74:ea0021abecf7 638
AzureIoTClient 74:ea0021abecf7 639 if (mallocAndStrcpy_s(&handleData->contentEncoding, contentEncoding) != 0)
AzureIoTClient 74:ea0021abecf7 640 {
AzureIoTClient 74:ea0021abecf7 641 LogError("Failed saving a copy of contentEncoding");
AzureIoTClient 74:ea0021abecf7 642 // Codes_SRS_IOTHUBMESSAGE_09_008: [If the allocation or the copying of `contentEncoding` fails, then IoTHubMessage_SetContentEncodingSystemProperty shall return IOTHUB_MESSAGE_ERROR.]
AzureIoTClient 74:ea0021abecf7 643 result = IOTHUB_MESSAGE_ERROR;
AzureIoTClient 74:ea0021abecf7 644 }
AzureIoTClient 74:ea0021abecf7 645 else
AzureIoTClient 74:ea0021abecf7 646 {
AzureIoTClient 74:ea0021abecf7 647 // Codes_SRS_IOTHUBMESSAGE_09_009: [If IoTHubMessage_SetContentEncodingSystemProperty finishes successfully it shall return IOTHUB_MESSAGE_OK.]
AzureIoTClient 74:ea0021abecf7 648 result = IOTHUB_MESSAGE_OK;
AzureIoTClient 74:ea0021abecf7 649 }
AzureIoTClient 74:ea0021abecf7 650 }
AzureIoTClient 74:ea0021abecf7 651
AzureIoTClient 74:ea0021abecf7 652 return result;
AzureIoTClient 74:ea0021abecf7 653 }
AzureIoTClient 74:ea0021abecf7 654
AzureIoTClient 74:ea0021abecf7 655 const char* IoTHubMessage_GetContentEncodingSystemProperty(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 74:ea0021abecf7 656 {
AzureIoTClient 74:ea0021abecf7 657 const char* result;
AzureIoTClient 74:ea0021abecf7 658
AzureIoTClient 74:ea0021abecf7 659 // Codes_SRS_IOTHUBMESSAGE_09_010: [If any of the parameters are NULL then IoTHubMessage_GetContentEncodingSystemProperty shall return a IOTHUB_MESSAGE_INVALID_ARG value.]
AzureIoTClient 74:ea0021abecf7 660 if (iotHubMessageHandle == NULL)
AzureIoTClient 74:ea0021abecf7 661 {
AzureIoTClient 74:ea0021abecf7 662 LogError("Invalid argument (iotHubMessageHandle is NULL)");
AzureIoTClient 74:ea0021abecf7 663 result = NULL;
AzureIoTClient 74:ea0021abecf7 664 }
AzureIoTClient 74:ea0021abecf7 665 else
AzureIoTClient 74:ea0021abecf7 666 {
AzureIoTClient 74:ea0021abecf7 667 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 74:ea0021abecf7 668
AzureIoTClient 74:ea0021abecf7 669 // Codes_SRS_IOTHUBMESSAGE_09_011: [IoTHubMessage_GetContentEncodingSystemProperty shall return the `contentEncoding` as a const char* ]
AzureIoTClient 74:ea0021abecf7 670 result = (const char*)handleData->contentEncoding;
AzureIoTClient 74:ea0021abecf7 671 }
AzureIoTClient 74:ea0021abecf7 672
AzureIoTClient 74:ea0021abecf7 673 return result;
AzureIoTClient 74:ea0021abecf7 674 }
AzureIoTClient 74:ea0021abecf7 675
AzureIoTClient 0:e393db310d89 676 void IoTHubMessage_Destroy(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 0:e393db310d89 677 {
AzureIoTClient 0:e393db310d89 678 /*Codes_SRS_IOTHUBMESSAGE_01_004: [If iotHubMessageHandle is NULL, IoTHubMessage_Destroy shall do nothing.] */
AzureIoTClient 0:e393db310d89 679 if (iotHubMessageHandle != NULL)
AzureIoTClient 0:e393db310d89 680 {
AzureIoTClient 0:e393db310d89 681 /*Codes_SRS_IOTHUBMESSAGE_01_003: [IoTHubMessage_Destroy shall free all resources associated with iotHubMessageHandle.] */
AzureIoTClient 0:e393db310d89 682 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 0:e393db310d89 683 if (handleData->contentType == IOTHUBMESSAGE_BYTEARRAY)
AzureIoTClient 0:e393db310d89 684 {
AzureIoTClient 0:e393db310d89 685 BUFFER_delete(handleData->value.byteArray);
AzureIoTClient 0:e393db310d89 686 }
AzureIoTClient 61:8b85a4e797cf 687 else if (handleData->contentType == IOTHUBMESSAGE_STRING)
AzureIoTClient 61:8b85a4e797cf 688 {
AzureIoTClient 61:8b85a4e797cf 689 STRING_delete(handleData->value.string);
AzureIoTClient 61:8b85a4e797cf 690 }
AzureIoTClient 0:e393db310d89 691 else
AzureIoTClient 0:e393db310d89 692 {
AzureIoTClient 61:8b85a4e797cf 693 LogError("Unknown contentType in IoTHubMessage");
AzureIoTClient 0:e393db310d89 694 }
AzureIoTClient 0:e393db310d89 695 Map_Destroy(handleData->properties);
AzureIoTClient 18:1e9adb15c645 696 free(handleData->messageId);
AzureIoTClient 18:1e9adb15c645 697 handleData->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 698 free(handleData->correlationId);
AzureIoTClient 18:1e9adb15c645 699 handleData->correlationId = NULL;
AzureIoTClient 74:ea0021abecf7 700 free(handleData->userDefinedContentType);
AzureIoTClient 74:ea0021abecf7 701 free(handleData->contentEncoding);
AzureIoTClient 0:e393db310d89 702 free(handleData);
AzureIoTClient 0:e393db310d89 703 }
AzureIoTClient 53:1e5a1ca1f274 704 }