corrected version (with typedef struct IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA* IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE;) included in the sources

Dependents:   STM32F746_iothub_client_sample_mqtt

Fork of iothub_client by Azure IoT

Committer:
Azure.IoT Build
Date:
Fri Apr 08 13:24:33 2016 -0700
Revision:
38:a05929a75111
Parent:
21:3c90c2262ce4
Child:
39:2719651a5bee
1.0.4

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