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 Feb 24 14:00:43 2017 -0800
Revision:
60:41648c4e7036
Parent:
57:4524910c6445
Child:
61:8b85a4e797cf
1.1.8

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 0:e393db310d89 29 }IOTHUB_MESSAGE_HANDLE_DATA;
AzureIoTClient 0:e393db310d89 30
AzureIoTClient 0:e393db310d89 31 static bool ContainsOnlyUsAscii(const char* asciiValue)
AzureIoTClient 0:e393db310d89 32 {
AzureIoTClient 21:3c90c2262ce4 33 bool result = true;
AzureIoTClient 0:e393db310d89 34 const char* iterator = asciiValue;
AzureIoTClient 0:e393db310d89 35 while (iterator != NULL && *iterator != '\0')
AzureIoTClient 0:e393db310d89 36 {
AzureIoTClient 0:e393db310d89 37 // Allow only printable ascii char
AzureIoTClient 0:e393db310d89 38 if (*iterator < ' ' || *iterator > '~')
AzureIoTClient 0:e393db310d89 39 {
AzureIoTClient 0:e393db310d89 40 result = false;
AzureIoTClient 0:e393db310d89 41 break;
AzureIoTClient 0:e393db310d89 42 }
AzureIoTClient 0:e393db310d89 43 iterator++;
AzureIoTClient 0:e393db310d89 44 }
AzureIoTClient 0:e393db310d89 45 return result;
AzureIoTClient 0:e393db310d89 46 }
AzureIoTClient 0:e393db310d89 47
AzureIoTClient 0:e393db310d89 48 /* 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 49 static int ValidateAsciiCharactersFilter(const char* mapKey, const char* mapValue)
AzureIoTClient 0:e393db310d89 50 {
AzureIoTClient 0:e393db310d89 51 int result;
AzureIoTClient 0:e393db310d89 52 if (!ContainsOnlyUsAscii(mapKey) || !ContainsOnlyUsAscii(mapValue) )
AzureIoTClient 0:e393db310d89 53 {
AzureIoTClient 60:41648c4e7036 54 result = __FAILURE__;
AzureIoTClient 0:e393db310d89 55 }
AzureIoTClient 0:e393db310d89 56 else
AzureIoTClient 0:e393db310d89 57 {
AzureIoTClient 0:e393db310d89 58 result = 0;
AzureIoTClient 0:e393db310d89 59 }
AzureIoTClient 0:e393db310d89 60 return result;
AzureIoTClient 0:e393db310d89 61 }
AzureIoTClient 0:e393db310d89 62
AzureIoTClient 0:e393db310d89 63 IOTHUB_MESSAGE_HANDLE IoTHubMessage_CreateFromByteArray(const unsigned char* byteArray, size_t size)
AzureIoTClient 0:e393db310d89 64 {
AzureIoTClient 0:e393db310d89 65 IOTHUB_MESSAGE_HANDLE_DATA* result;
AzureIoTClient 0:e393db310d89 66 result = malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
AzureIoTClient 0:e393db310d89 67 if (result == NULL)
AzureIoTClient 0:e393db310d89 68 {
AzureIoTClient 39:2719651a5bee 69 LogError("unable to malloc");
AzureIoTClient 0:e393db310d89 70 /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
AzureIoTClient 0:e393db310d89 71 /*let it go through*/
AzureIoTClient 0:e393db310d89 72 }
AzureIoTClient 0:e393db310d89 73 else
AzureIoTClient 0:e393db310d89 74 {
AzureIoTClient 0:e393db310d89 75 const unsigned char* source;
AzureIoTClient 0:e393db310d89 76 unsigned char temp = 0x00;
AzureIoTClient 0:e393db310d89 77 if (size != 0)
AzureIoTClient 0:e393db310d89 78 {
AzureIoTClient 0:e393db310d89 79 /*Codes_SRS_IOTHUBMESSAGE_06_002: [If size is NOT zero then byteArray MUST NOT be NULL*/
AzureIoTClient 0:e393db310d89 80 if (byteArray == NULL)
AzureIoTClient 0:e393db310d89 81 {
AzureIoTClient 39:2719651a5bee 82 LogError("Attempted to create a Hub Message from a NULL pointer!");
AzureIoTClient 0:e393db310d89 83 free(result);
AzureIoTClient 0:e393db310d89 84 result = NULL;
AzureIoTClient 0:e393db310d89 85 source = NULL;
AzureIoTClient 0:e393db310d89 86 }
AzureIoTClient 0:e393db310d89 87 else
AzureIoTClient 0:e393db310d89 88 {
AzureIoTClient 0:e393db310d89 89 source = byteArray;
AzureIoTClient 0:e393db310d89 90 }
AzureIoTClient 0:e393db310d89 91 }
AzureIoTClient 0:e393db310d89 92 else
AzureIoTClient 0:e393db310d89 93 {
AzureIoTClient 0:e393db310d89 94 /*Codes_SRS_IOTHUBMESSAGE_06_001: [If size is zero then byteArray may be NULL.]*/
AzureIoTClient 0:e393db310d89 95 source = &temp;
AzureIoTClient 0:e393db310d89 96 }
AzureIoTClient 0:e393db310d89 97 if (result != NULL)
AzureIoTClient 0:e393db310d89 98 {
AzureIoTClient 0:e393db310d89 99 /*Codes_SRS_IOTHUBMESSAGE_02_022: [IoTHubMessage_CreateFromByteArray shall call BUFFER_create passing byteArray and size as parameters.] */
AzureIoTClient 0:e393db310d89 100 if ((result->value.byteArray = BUFFER_create(source, size)) == NULL)
AzureIoTClient 0:e393db310d89 101 {
AzureIoTClient 39:2719651a5bee 102 LogError("BUFFER_create failed");
AzureIoTClient 0:e393db310d89 103 /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
AzureIoTClient 0:e393db310d89 104 free(result);
AzureIoTClient 0:e393db310d89 105 result = NULL;
AzureIoTClient 0:e393db310d89 106 }
AzureIoTClient 0:e393db310d89 107 /*Codes_SRS_IOTHUBMESSAGE_02_023: [IoTHubMessage_CreateFromByteArray shall call Map_Create to create the message properties.] */
AzureIoTClient 0:e393db310d89 108 else if ((result->properties = Map_Create(ValidateAsciiCharactersFilter)) == NULL)
AzureIoTClient 0:e393db310d89 109 {
AzureIoTClient 39:2719651a5bee 110 LogError("Map_Create failed");
AzureIoTClient 0:e393db310d89 111 /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
AzureIoTClient 0:e393db310d89 112 BUFFER_delete(result->value.byteArray);
AzureIoTClient 0:e393db310d89 113 free(result);
AzureIoTClient 0:e393db310d89 114 result = NULL;
AzureIoTClient 0:e393db310d89 115 }
AzureIoTClient 0:e393db310d89 116 else
AzureIoTClient 0:e393db310d89 117 {
AzureIoTClient 0:e393db310d89 118 /*Codes_SRS_IOTHUBMESSAGE_02_025: [Otherwise, IoTHubMessage_CreateFromByteArray shall return a non-NULL handle.] */
AzureIoTClient 0:e393db310d89 119 /*Codes_SRS_IOTHUBMESSAGE_02_026: [The type of the new message shall be IOTHUBMESSAGE_BYTEARRAY.] */
AzureIoTClient 0:e393db310d89 120 result->contentType = IOTHUBMESSAGE_BYTEARRAY;
AzureIoTClient 18:1e9adb15c645 121 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 122 result->correlationId = NULL;
AzureIoTClient 0:e393db310d89 123 /*all is fine, return result*/
AzureIoTClient 0:e393db310d89 124 }
AzureIoTClient 0:e393db310d89 125 }
AzureIoTClient 0:e393db310d89 126 }
AzureIoTClient 0:e393db310d89 127 return result;
AzureIoTClient 0:e393db310d89 128 }
AzureIoTClient 0:e393db310d89 129 IOTHUB_MESSAGE_HANDLE IoTHubMessage_CreateFromString(const char* source)
AzureIoTClient 0:e393db310d89 130 {
AzureIoTClient 0:e393db310d89 131 IOTHUB_MESSAGE_HANDLE_DATA* result;
AzureIoTClient 0:e393db310d89 132 result = malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
AzureIoTClient 0:e393db310d89 133 if (result == NULL)
AzureIoTClient 0:e393db310d89 134 {
AzureIoTClient 39:2719651a5bee 135 LogError("malloc failed");
AzureIoTClient 0:e393db310d89 136 /*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 137 /*let it go through*/
AzureIoTClient 0:e393db310d89 138 }
AzureIoTClient 0:e393db310d89 139 else
AzureIoTClient 0:e393db310d89 140 {
AzureIoTClient 0:e393db310d89 141 /*Codes_SRS_IOTHUBMESSAGE_02_027: [IoTHubMessage_CreateFromString shall call STRING_construct passing source as parameter.] */
AzureIoTClient 0:e393db310d89 142 if ((result->value.string = STRING_construct(source)) == NULL)
AzureIoTClient 0:e393db310d89 143 {
AzureIoTClient 39:2719651a5bee 144 LogError("STRING_construct failed");
AzureIoTClient 0:e393db310d89 145 /*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 146 free(result);
AzureIoTClient 0:e393db310d89 147 result = NULL;
AzureIoTClient 0:e393db310d89 148 }
AzureIoTClient 0:e393db310d89 149 /*Codes_SRS_IOTHUBMESSAGE_02_028: [IoTHubMessage_CreateFromString shall call Map_Create to create the message properties.] */
AzureIoTClient 0:e393db310d89 150 else if ((result->properties = Map_Create(ValidateAsciiCharactersFilter)) == NULL)
AzureIoTClient 0:e393db310d89 151 {
AzureIoTClient 39:2719651a5bee 152 LogError("Map_Create failed");
AzureIoTClient 0:e393db310d89 153 /*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 154 STRING_delete(result->value.string);
AzureIoTClient 0:e393db310d89 155 free(result);
AzureIoTClient 0:e393db310d89 156 result = NULL;
AzureIoTClient 0:e393db310d89 157 }
AzureIoTClient 0:e393db310d89 158 else
AzureIoTClient 0:e393db310d89 159 {
AzureIoTClient 0:e393db310d89 160 /*Codes_SRS_IOTHUBMESSAGE_02_031: [Otherwise, IoTHubMessage_CreateFromString shall return a non-NULL handle.] */
AzureIoTClient 0:e393db310d89 161 /*Codes_SRS_IOTHUBMESSAGE_02_032: [The type of the new message shall be IOTHUBMESSAGE_STRING.] */
AzureIoTClient 0:e393db310d89 162 result->contentType = IOTHUBMESSAGE_STRING;
AzureIoTClient 18:1e9adb15c645 163 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 164 result->correlationId = NULL;
AzureIoTClient 0:e393db310d89 165 }
AzureIoTClient 0:e393db310d89 166 }
AzureIoTClient 0:e393db310d89 167 return result;
AzureIoTClient 0:e393db310d89 168 }
AzureIoTClient 0:e393db310d89 169
AzureIoTClient 0:e393db310d89 170 /*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 171 IOTHUB_MESSAGE_HANDLE IoTHubMessage_Clone(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 0:e393db310d89 172 {
AzureIoTClient 0:e393db310d89 173 IOTHUB_MESSAGE_HANDLE_DATA* result;
AzureIoTClient 0:e393db310d89 174 const IOTHUB_MESSAGE_HANDLE_DATA* source = (const IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
AzureIoTClient 0:e393db310d89 175 /* Codes_SRS_IOTHUBMESSAGE_03_005: [IoTHubMessage_Clone shall return NULL if iotHubMessageHandle is NULL.] */
AzureIoTClient 0:e393db310d89 176 if (source == NULL)
AzureIoTClient 0:e393db310d89 177 {
AzureIoTClient 0:e393db310d89 178 result = NULL;
AzureIoTClient 39:2719651a5bee 179 LogError("iotHubMessageHandle parameter cannot be NULL for IoTHubMessage_Clone");
AzureIoTClient 0:e393db310d89 180 }
AzureIoTClient 0:e393db310d89 181 else
AzureIoTClient 0:e393db310d89 182 {
AzureIoTClient 0:e393db310d89 183 result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
AzureIoTClient 0:e393db310d89 184 /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
AzureIoTClient 0:e393db310d89 185 if (result == NULL)
AzureIoTClient 0:e393db310d89 186 {
AzureIoTClient 0:e393db310d89 187 /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
AzureIoTClient 0:e393db310d89 188 /*do nothing and return as is*/
AzureIoTClient 39:2719651a5bee 189 LogError("unable to malloc");
AzureIoTClient 0:e393db310d89 190 }
AzureIoTClient 0:e393db310d89 191 else
AzureIoTClient 0:e393db310d89 192 {
AzureIoTClient 18:1e9adb15c645 193 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 194 result->correlationId = NULL;
AzureIoTClient 18:1e9adb15c645 195 if (source->messageId != NULL && mallocAndStrcpy_s(&result->messageId, source->messageId) != 0)
AzureIoTClient 18:1e9adb15c645 196 {
AzureIoTClient 39:2719651a5bee 197 LogError("unable to Copy messageId");
AzureIoTClient 18:1e9adb15c645 198 free(result);
AzureIoTClient 18:1e9adb15c645 199 result = NULL;
AzureIoTClient 18:1e9adb15c645 200 }
AzureIoTClient 18:1e9adb15c645 201 else if (source->correlationId != NULL && mallocAndStrcpy_s(&result->correlationId, source->correlationId) != 0)
AzureIoTClient 18:1e9adb15c645 202 {
AzureIoTClient 39:2719651a5bee 203 LogError("unable to Copy correlationId");
AzureIoTClient 18:1e9adb15c645 204 if (result->messageId != NULL)
AzureIoTClient 18:1e9adb15c645 205 {
AzureIoTClient 18:1e9adb15c645 206 free(result->messageId);
AzureIoTClient 18:1e9adb15c645 207 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 208 }
AzureIoTClient 18:1e9adb15c645 209 free(result);
AzureIoTClient 18:1e9adb15c645 210 result = NULL;
AzureIoTClient 18:1e9adb15c645 211 }
AzureIoTClient 18:1e9adb15c645 212 else if (source->contentType == IOTHUBMESSAGE_BYTEARRAY)
AzureIoTClient 0:e393db310d89 213 {
AzureIoTClient 0:e393db310d89 214 /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone to content by a call to BUFFER_clone] */
AzureIoTClient 0:e393db310d89 215 if ((result->value.byteArray = BUFFER_clone(source->value.byteArray)) == NULL)
AzureIoTClient 0:e393db310d89 216 {
AzureIoTClient 0:e393db310d89 217 /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
AzureIoTClient 39:2719651a5bee 218 LogError("unable to BUFFER_clone");
AzureIoTClient 18:1e9adb15c645 219 if (result->messageId)
AzureIoTClient 18:1e9adb15c645 220 {
AzureIoTClient 18:1e9adb15c645 221 free(result->messageId);
AzureIoTClient 18:1e9adb15c645 222 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 223 }
AzureIoTClient 18:1e9adb15c645 224 if (result->correlationId != NULL)
AzureIoTClient 18:1e9adb15c645 225 {
AzureIoTClient 18:1e9adb15c645 226 free(result->correlationId);
AzureIoTClient 18:1e9adb15c645 227 result->correlationId = NULL;
AzureIoTClient 18:1e9adb15c645 228 }
AzureIoTClient 0:e393db310d89 229 free(result);
AzureIoTClient 0:e393db310d89 230 result = NULL;
AzureIoTClient 0:e393db310d89 231 }
AzureIoTClient 0:e393db310d89 232 /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */
AzureIoTClient 0:e393db310d89 233 else if ((result->properties = Map_Clone(source->properties)) == NULL)
AzureIoTClient 0:e393db310d89 234 {
AzureIoTClient 0:e393db310d89 235 /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
AzureIoTClient 39:2719651a5bee 236 LogError("unable to Map_Clone");
AzureIoTClient 0:e393db310d89 237 BUFFER_delete(result->value.byteArray);
AzureIoTClient 18:1e9adb15c645 238 if (result->messageId)
AzureIoTClient 18:1e9adb15c645 239 {
AzureIoTClient 18:1e9adb15c645 240 free(result->messageId);
AzureIoTClient 18:1e9adb15c645 241 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 242 }
AzureIoTClient 18:1e9adb15c645 243 if (result->correlationId != NULL)
AzureIoTClient 18:1e9adb15c645 244 {
AzureIoTClient 18:1e9adb15c645 245 free(result->correlationId);
AzureIoTClient 18:1e9adb15c645 246 result->correlationId = NULL;
AzureIoTClient 18:1e9adb15c645 247 }
AzureIoTClient 0:e393db310d89 248 free(result);
AzureIoTClient 0:e393db310d89 249 result = NULL;
AzureIoTClient 0:e393db310d89 250 }
AzureIoTClient 0:e393db310d89 251 else
AzureIoTClient 0:e393db310d89 252 {
AzureIoTClient 0:e393db310d89 253 result->contentType = IOTHUBMESSAGE_BYTEARRAY;
AzureIoTClient 0:e393db310d89 254 /*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 255 /*return as is, this is a good result*/
AzureIoTClient 0:e393db310d89 256 }
AzureIoTClient 0:e393db310d89 257 }
AzureIoTClient 0:e393db310d89 258 else /*can only be STRING*/
AzureIoTClient 0:e393db310d89 259 {
AzureIoTClient 0:e393db310d89 260 /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone the content by a call to BUFFER_clone or STRING_clone] */
AzureIoTClient 0:e393db310d89 261 if ((result->value.string = STRING_clone(source->value.string)) == NULL)
AzureIoTClient 0:e393db310d89 262 {
AzureIoTClient 0:e393db310d89 263 /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
AzureIoTClient 18:1e9adb15c645 264 if (result->messageId)
AzureIoTClient 18:1e9adb15c645 265 {
AzureIoTClient 18:1e9adb15c645 266 free(result->messageId);
AzureIoTClient 18:1e9adb15c645 267 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 268 }
AzureIoTClient 18:1e9adb15c645 269 if (result->correlationId != NULL)
AzureIoTClient 18:1e9adb15c645 270 {
AzureIoTClient 18:1e9adb15c645 271 free(result->correlationId);
AzureIoTClient 18:1e9adb15c645 272 result->correlationId = NULL;
AzureIoTClient 18:1e9adb15c645 273 }
AzureIoTClient 0:e393db310d89 274 free(result);
AzureIoTClient 0:e393db310d89 275 result = NULL;
AzureIoTClient 39:2719651a5bee 276 LogError("failed to STRING_clone");
AzureIoTClient 0:e393db310d89 277 }
AzureIoTClient 0:e393db310d89 278 /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */
AzureIoTClient 0:e393db310d89 279 else if ((result->properties = Map_Clone(source->properties)) == NULL)
AzureIoTClient 0:e393db310d89 280 {
AzureIoTClient 0:e393db310d89 281 /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
AzureIoTClient 39:2719651a5bee 282 LogError("unable to Map_Clone");
AzureIoTClient 0:e393db310d89 283 STRING_delete(result->value.string);
AzureIoTClient 18:1e9adb15c645 284 if (result->messageId)
AzureIoTClient 18:1e9adb15c645 285 {
AzureIoTClient 18:1e9adb15c645 286 free(result->messageId);
AzureIoTClient 18:1e9adb15c645 287 result->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 288 }
AzureIoTClient 18:1e9adb15c645 289 if (result->correlationId != NULL)
AzureIoTClient 18:1e9adb15c645 290 {
AzureIoTClient 18:1e9adb15c645 291 free(result->correlationId);
AzureIoTClient 18:1e9adb15c645 292 result->correlationId = NULL;
AzureIoTClient 18:1e9adb15c645 293 }
AzureIoTClient 0:e393db310d89 294 free(result);
AzureIoTClient 0:e393db310d89 295 result = NULL;
AzureIoTClient 0:e393db310d89 296 }
AzureIoTClient 0:e393db310d89 297 else
AzureIoTClient 0:e393db310d89 298 {
AzureIoTClient 0:e393db310d89 299 result->contentType = IOTHUBMESSAGE_STRING;
AzureIoTClient 0:e393db310d89 300 /*all is fine*/
AzureIoTClient 0:e393db310d89 301 }
AzureIoTClient 0:e393db310d89 302 }
AzureIoTClient 0:e393db310d89 303 }
AzureIoTClient 0:e393db310d89 304 }
AzureIoTClient 0:e393db310d89 305 return result;
AzureIoTClient 0:e393db310d89 306 }
AzureIoTClient 0:e393db310d89 307
AzureIoTClient 0:e393db310d89 308 IOTHUB_MESSAGE_RESULT IoTHubMessage_GetByteArray(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const unsigned char** buffer, size_t* size)
AzureIoTClient 0:e393db310d89 309 {
AzureIoTClient 0:e393db310d89 310 IOTHUB_MESSAGE_RESULT result;
AzureIoTClient 0:e393db310d89 311 if (
AzureIoTClient 0:e393db310d89 312 (iotHubMessageHandle == NULL) ||
AzureIoTClient 0:e393db310d89 313 (buffer == NULL) ||
AzureIoTClient 0:e393db310d89 314 (size == NULL)
AzureIoTClient 0:e393db310d89 315 )
AzureIoTClient 0:e393db310d89 316 {
AzureIoTClient 0:e393db310d89 317 /*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 318 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 319 result = IOTHUB_MESSAGE_INVALID_ARG;
AzureIoTClient 0:e393db310d89 320 }
AzureIoTClient 0:e393db310d89 321 else
AzureIoTClient 0:e393db310d89 322 {
AzureIoTClient 0:e393db310d89 323 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 0:e393db310d89 324 if (handleData->contentType != IOTHUBMESSAGE_BYTEARRAY)
AzureIoTClient 0:e393db310d89 325 {
AzureIoTClient 0:e393db310d89 326 /*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 327 result = IOTHUB_MESSAGE_INVALID_ARG;
AzureIoTClient 39:2719651a5bee 328 LogError("invalid type of message %s", ENUM_TO_STRING(IOTHUBMESSAGE_CONTENT_TYPE, handleData->contentType));
AzureIoTClient 0:e393db310d89 329 }
AzureIoTClient 0:e393db310d89 330 else
AzureIoTClient 0:e393db310d89 331 {
AzureIoTClient 0:e393db310d89 332 /*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 333 *buffer = BUFFER_u_char(handleData->value.byteArray);
AzureIoTClient 0:e393db310d89 334 /*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 335 *size = BUFFER_length(handleData->value.byteArray);
AzureIoTClient 0:e393db310d89 336 result = IOTHUB_MESSAGE_OK;
AzureIoTClient 0:e393db310d89 337 }
AzureIoTClient 0:e393db310d89 338 }
AzureIoTClient 0:e393db310d89 339 return result;
AzureIoTClient 0:e393db310d89 340 }
AzureIoTClient 0:e393db310d89 341
AzureIoTClient 0:e393db310d89 342 const char* IoTHubMessage_GetString(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 0:e393db310d89 343 {
AzureIoTClient 0:e393db310d89 344 const char* result;
AzureIoTClient 0:e393db310d89 345 if (iotHubMessageHandle == NULL)
AzureIoTClient 0:e393db310d89 346 {
AzureIoTClient 0:e393db310d89 347 /*Codes_SRS_IOTHUBMESSAGE_02_016: [If any parameter is NULL then IoTHubMessage_GetString shall return NULL.] */
AzureIoTClient 0:e393db310d89 348 result = NULL;
AzureIoTClient 0:e393db310d89 349 }
AzureIoTClient 0:e393db310d89 350 else
AzureIoTClient 0:e393db310d89 351 {
AzureIoTClient 0:e393db310d89 352 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 0:e393db310d89 353 if (handleData->contentType != IOTHUBMESSAGE_STRING)
AzureIoTClient 0:e393db310d89 354 {
AzureIoTClient 0:e393db310d89 355 /*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 356 result = NULL;
AzureIoTClient 0:e393db310d89 357 }
AzureIoTClient 0:e393db310d89 358 else
AzureIoTClient 0:e393db310d89 359 {
AzureIoTClient 0:e393db310d89 360 /*Codes_SRS_IOTHUBMESSAGE_02_018: [IoTHubMessage_GetStringData shall return the currently stored null terminated string.] */
AzureIoTClient 0:e393db310d89 361 result = STRING_c_str(handleData->value.string);
AzureIoTClient 0:e393db310d89 362 }
AzureIoTClient 0:e393db310d89 363 }
AzureIoTClient 0:e393db310d89 364 return result;
AzureIoTClient 0:e393db310d89 365 }
AzureIoTClient 0:e393db310d89 366
AzureIoTClient 0:e393db310d89 367 IOTHUBMESSAGE_CONTENT_TYPE IoTHubMessage_GetContentType(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 0:e393db310d89 368 {
AzureIoTClient 0:e393db310d89 369 IOTHUBMESSAGE_CONTENT_TYPE result;
AzureIoTClient 0:e393db310d89 370 /*Codes_SRS_IOTHUBMESSAGE_02_008: [If any parameter is NULL then IoTHubMessage_GetContentType shall return IOTHUBMESSAGE_UNKNOWN.] */
AzureIoTClient 0:e393db310d89 371 if (iotHubMessageHandle == NULL)
AzureIoTClient 0:e393db310d89 372 {
AzureIoTClient 0:e393db310d89 373 result = IOTHUBMESSAGE_UNKNOWN;
AzureIoTClient 0:e393db310d89 374 }
AzureIoTClient 0:e393db310d89 375 else
AzureIoTClient 0:e393db310d89 376 {
AzureIoTClient 0:e393db310d89 377 /*Codes_SRS_IOTHUBMESSAGE_02_009: [Otherwise IoTHubMessage_GetContentType shall return the type of the message.] */
AzureIoTClient 0:e393db310d89 378 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 0:e393db310d89 379 result = handleData->contentType;
AzureIoTClient 0:e393db310d89 380 }
AzureIoTClient 0:e393db310d89 381 return result;
AzureIoTClient 0:e393db310d89 382 }
AzureIoTClient 0:e393db310d89 383
AzureIoTClient 0:e393db310d89 384 MAP_HANDLE IoTHubMessage_Properties(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 0:e393db310d89 385 {
AzureIoTClient 0:e393db310d89 386 MAP_HANDLE result;
AzureIoTClient 0:e393db310d89 387 /*Codes_SRS_IOTHUBMESSAGE_02_001: [If iotHubMessageHandle is NULL then IoTHubMessage_Properties shall return NULL.]*/
AzureIoTClient 0:e393db310d89 388 if (iotHubMessageHandle == NULL)
AzureIoTClient 0:e393db310d89 389 {
AzureIoTClient 39:2719651a5bee 390 LogError("invalid arg (NULL) passed to IoTHubMessage_Properties");
AzureIoTClient 18:1e9adb15c645 391 result = NULL;
AzureIoTClient 0:e393db310d89 392 }
AzureIoTClient 0:e393db310d89 393 else
AzureIoTClient 0:e393db310d89 394 {
AzureIoTClient 0:e393db310d89 395 /*Codes_SRS_IOTHUBMESSAGE_02_002: [Otherwise, for any non-NULL iotHubMessageHandle it shall return a non-NULL MAP_HANDLE.]*/
AzureIoTClient 0:e393db310d89 396 IOTHUB_MESSAGE_HANDLE_DATA* handleData = (IOTHUB_MESSAGE_HANDLE_DATA*)iotHubMessageHandle;
AzureIoTClient 0:e393db310d89 397 result = handleData->properties;
AzureIoTClient 0:e393db310d89 398 }
AzureIoTClient 0:e393db310d89 399 return result;
AzureIoTClient 0:e393db310d89 400 }
AzureIoTClient 0:e393db310d89 401
AzureIoTClient 18:1e9adb15c645 402 const char* IoTHubMessage_GetCorrelationId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 18:1e9adb15c645 403 {
AzureIoTClient 18:1e9adb15c645 404 const char* result;
AzureIoTClient 18:1e9adb15c645 405 /* Codes_SRS_IOTHUBMESSAGE_07_016: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_GetCorrelationId shall return a NULL value.] */
AzureIoTClient 18:1e9adb15c645 406 if (iotHubMessageHandle == NULL)
AzureIoTClient 18:1e9adb15c645 407 {
AzureIoTClient 39:2719651a5bee 408 LogError("invalid arg (NULL) passed to IoTHubMessage_GetCorrelationId");
AzureIoTClient 18:1e9adb15c645 409 result = NULL;
AzureIoTClient 18:1e9adb15c645 410 }
AzureIoTClient 18:1e9adb15c645 411 else
AzureIoTClient 18:1e9adb15c645 412 {
AzureIoTClient 18:1e9adb15c645 413 /* Codes_SRS_IOTHUBMESSAGE_07_017: [IoTHubMessage_GetCorrelationId shall return the correlationId as a const char*.] */
AzureIoTClient 18:1e9adb15c645 414 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 18:1e9adb15c645 415 result = handleData->correlationId;
AzureIoTClient 18:1e9adb15c645 416 }
AzureIoTClient 18:1e9adb15c645 417 return result;
AzureIoTClient 18:1e9adb15c645 418 }
AzureIoTClient 18:1e9adb15c645 419
AzureIoTClient 18:1e9adb15c645 420 IOTHUB_MESSAGE_RESULT IoTHubMessage_SetCorrelationId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* correlationId)
AzureIoTClient 18:1e9adb15c645 421 {
AzureIoTClient 18:1e9adb15c645 422 IOTHUB_MESSAGE_RESULT result;
AzureIoTClient 18:1e9adb15c645 423 /* 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 424 if (iotHubMessageHandle == NULL || correlationId == NULL)
AzureIoTClient 18:1e9adb15c645 425 {
AzureIoTClient 39:2719651a5bee 426 LogError("invalid arg (NULL) passed to IoTHubMessage_SetCorrelationId");
AzureIoTClient 18:1e9adb15c645 427 result = IOTHUB_MESSAGE_INVALID_ARG;
AzureIoTClient 18:1e9adb15c645 428 }
AzureIoTClient 18:1e9adb15c645 429 else
AzureIoTClient 18:1e9adb15c645 430 {
AzureIoTClient 18:1e9adb15c645 431 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 18:1e9adb15c645 432 /* 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 433 if (handleData->correlationId != NULL)
AzureIoTClient 18:1e9adb15c645 434 {
AzureIoTClient 18:1e9adb15c645 435 free(handleData->correlationId);
AzureIoTClient 18:1e9adb15c645 436 }
AzureIoTClient 18:1e9adb15c645 437
AzureIoTClient 18:1e9adb15c645 438 if (mallocAndStrcpy_s(&handleData->correlationId, correlationId) != 0)
AzureIoTClient 18:1e9adb15c645 439 {
AzureIoTClient 18:1e9adb15c645 440 /* 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 441 result = IOTHUB_MESSAGE_ERROR;
AzureIoTClient 18:1e9adb15c645 442 }
AzureIoTClient 18:1e9adb15c645 443 else
AzureIoTClient 18:1e9adb15c645 444 {
AzureIoTClient 18:1e9adb15c645 445 /* Codes_SRS_IOTHUBMESSAGE_07_021: [IoTHubMessage_SetCorrelationId finishes successfully it shall return IOTHUB_MESSAGE_OK.] */
AzureIoTClient 18:1e9adb15c645 446 result = IOTHUB_MESSAGE_OK;
AzureIoTClient 18:1e9adb15c645 447 }
AzureIoTClient 18:1e9adb15c645 448 }
AzureIoTClient 18:1e9adb15c645 449 return result;
AzureIoTClient 18:1e9adb15c645 450 }
AzureIoTClient 18:1e9adb15c645 451
AzureIoTClient 18:1e9adb15c645 452 IOTHUB_MESSAGE_RESULT IoTHubMessage_SetMessageId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* messageId)
AzureIoTClient 18:1e9adb15c645 453 {
AzureIoTClient 18:1e9adb15c645 454 IOTHUB_MESSAGE_RESULT result;
AzureIoTClient 18:1e9adb15c645 455 /* 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 456 if (iotHubMessageHandle == NULL || messageId == NULL)
AzureIoTClient 18:1e9adb15c645 457 {
AzureIoTClient 39:2719651a5bee 458 LogError("invalid arg (NULL) passed to IoTHubMessage_SetMessageId");
AzureIoTClient 18:1e9adb15c645 459 result = IOTHUB_MESSAGE_INVALID_ARG;
AzureIoTClient 18:1e9adb15c645 460 }
AzureIoTClient 18:1e9adb15c645 461 else
AzureIoTClient 18:1e9adb15c645 462 {
AzureIoTClient 18:1e9adb15c645 463 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 18:1e9adb15c645 464 /* 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 465 if (handleData->messageId != NULL)
AzureIoTClient 18:1e9adb15c645 466 {
AzureIoTClient 18:1e9adb15c645 467 free(handleData->messageId);
AzureIoTClient 18:1e9adb15c645 468 }
AzureIoTClient 18:1e9adb15c645 469
AzureIoTClient 18:1e9adb15c645 470 /* 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 471 if (mallocAndStrcpy_s(&handleData->messageId, messageId) != 0)
AzureIoTClient 18:1e9adb15c645 472 {
AzureIoTClient 18:1e9adb15c645 473 result = IOTHUB_MESSAGE_ERROR;
AzureIoTClient 18:1e9adb15c645 474 }
AzureIoTClient 18:1e9adb15c645 475 else
AzureIoTClient 18:1e9adb15c645 476 {
AzureIoTClient 18:1e9adb15c645 477 result = IOTHUB_MESSAGE_OK;
AzureIoTClient 18:1e9adb15c645 478 }
AzureIoTClient 18:1e9adb15c645 479 }
AzureIoTClient 18:1e9adb15c645 480 return result;
AzureIoTClient 18:1e9adb15c645 481 }
AzureIoTClient 18:1e9adb15c645 482
AzureIoTClient 18:1e9adb15c645 483 const char* IoTHubMessage_GetMessageId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 18:1e9adb15c645 484 {
AzureIoTClient 18:1e9adb15c645 485 const char* result;
AzureIoTClient 18:1e9adb15c645 486 /* Codes_SRS_IOTHUBMESSAGE_07_010: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_MessageId shall return a NULL value.] */
AzureIoTClient 18:1e9adb15c645 487 if (iotHubMessageHandle == NULL)
AzureIoTClient 18:1e9adb15c645 488 {
AzureIoTClient 39:2719651a5bee 489 LogError("invalid arg (NULL) passed to IoTHubMessage_GetMessageId");
AzureIoTClient 18:1e9adb15c645 490 result = NULL;
AzureIoTClient 18:1e9adb15c645 491 }
AzureIoTClient 18:1e9adb15c645 492 else
AzureIoTClient 18:1e9adb15c645 493 {
AzureIoTClient 18:1e9adb15c645 494 /* Codes_SRS_IOTHUBMESSAGE_07_011: [IoTHubMessage_MessageId shall return the messageId as a const char*.] */
AzureIoTClient 18:1e9adb15c645 495 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 18:1e9adb15c645 496 result = handleData->messageId;
AzureIoTClient 18:1e9adb15c645 497 }
AzureIoTClient 18:1e9adb15c645 498 return result;
AzureIoTClient 18:1e9adb15c645 499 }
AzureIoTClient 18:1e9adb15c645 500
AzureIoTClient 0:e393db310d89 501 void IoTHubMessage_Destroy(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
AzureIoTClient 0:e393db310d89 502 {
AzureIoTClient 0:e393db310d89 503 /*Codes_SRS_IOTHUBMESSAGE_01_004: [If iotHubMessageHandle is NULL, IoTHubMessage_Destroy shall do nothing.] */
AzureIoTClient 0:e393db310d89 504 if (iotHubMessageHandle != NULL)
AzureIoTClient 0:e393db310d89 505 {
AzureIoTClient 0:e393db310d89 506 /*Codes_SRS_IOTHUBMESSAGE_01_003: [IoTHubMessage_Destroy shall free all resources associated with iotHubMessageHandle.] */
AzureIoTClient 0:e393db310d89 507 IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
AzureIoTClient 0:e393db310d89 508 if (handleData->contentType == IOTHUBMESSAGE_BYTEARRAY)
AzureIoTClient 0:e393db310d89 509 {
AzureIoTClient 0:e393db310d89 510 BUFFER_delete(handleData->value.byteArray);
AzureIoTClient 0:e393db310d89 511 }
AzureIoTClient 0:e393db310d89 512 else
AzureIoTClient 0:e393db310d89 513 {
AzureIoTClient 0:e393db310d89 514 /*can only be STRING*/
AzureIoTClient 0:e393db310d89 515 STRING_delete(handleData->value.string);
AzureIoTClient 0:e393db310d89 516 }
AzureIoTClient 0:e393db310d89 517 Map_Destroy(handleData->properties);
AzureIoTClient 18:1e9adb15c645 518 free(handleData->messageId);
AzureIoTClient 18:1e9adb15c645 519 handleData->messageId = NULL;
AzureIoTClient 18:1e9adb15c645 520 free(handleData->correlationId);
AzureIoTClient 18:1e9adb15c645 521 handleData->correlationId = NULL;
AzureIoTClient 0:e393db310d89 522 free(handleData);
AzureIoTClient 0:e393db310d89 523 }
AzureIoTClient 53:1e5a1ca1f274 524 }