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
Diff: iothub_message.c
- Revision:
- 18:1e9adb15c645
- Parent:
- 0:e393db310d89
- Child:
- 21:3c90c2262ce4
--- a/iothub_message.c Fri Oct 23 01:37:22 2015 +0000 +++ b/iothub_message.c Mon Nov 02 19:21:21 2015 -0800 @@ -26,6 +26,8 @@ STRING_HANDLE string; } value; MAP_HANDLE properties; + char* messageId; + char* correlationId; }IOTHUB_MESSAGE_HANDLE_DATA; static bool ContainsOnlyUsAscii(const char* asciiValue) @@ -118,6 +120,8 @@ /*Codes_SRS_IOTHUBMESSAGE_02_025: [Otherwise, IoTHubMessage_CreateFromByteArray shall return a non-NULL handle.] */ /*Codes_SRS_IOTHUBMESSAGE_02_026: [The type of the new message shall be IOTHUBMESSAGE_BYTEARRAY.] */ result->contentType = IOTHUBMESSAGE_BYTEARRAY; + result->messageId = NULL; + result->correlationId = NULL; /*all is fine, return result*/ } } @@ -158,6 +162,8 @@ /*Codes_SRS_IOTHUBMESSAGE_02_031: [Otherwise, IoTHubMessage_CreateFromString shall return a non-NULL handle.] */ /*Codes_SRS_IOTHUBMESSAGE_02_032: [The type of the new message shall be IOTHUBMESSAGE_STRING.] */ result->contentType = IOTHUBMESSAGE_STRING; + result->messageId = NULL; + result->correlationId = NULL; } } return result; @@ -186,13 +192,42 @@ } else { - if (source->contentType == IOTHUBMESSAGE_BYTEARRAY) + result->messageId = NULL; + result->correlationId = NULL; + if (source->messageId != NULL && mallocAndStrcpy_s(&result->messageId, source->messageId) != 0) + { + LogError("unable to Copy messageId\r\n"); + free(result); + result = NULL; + } + else if (source->correlationId != NULL && mallocAndStrcpy_s(&result->correlationId, source->correlationId) != 0) + { + LogError("unable to Copy correlationId\r\n"); + if (result->messageId != NULL) + { + free(result->messageId); + result->messageId = NULL; + } + free(result); + result = NULL; + } + else if (source->contentType == IOTHUBMESSAGE_BYTEARRAY) { /*Codes_SRS_IOTHUBMESSAGE_02_006: [IoTHubMessage_Clone shall clone to content by a call to BUFFER_clone] */ if ((result->value.byteArray = BUFFER_clone(source->value.byteArray)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to BUFFER_clone\r\n"); + if (result->messageId) + { + free(result->messageId); + result->messageId = NULL; + } + if (result->correlationId != NULL) + { + free(result->correlationId); + result->correlationId = NULL; + } free(result); result = NULL; } @@ -202,6 +237,16 @@ /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to Map_Clone\r\n"); BUFFER_delete(result->value.byteArray); + if (result->messageId) + { + free(result->messageId); + result->messageId = NULL; + } + if (result->correlationId != NULL) + { + free(result->correlationId); + result->correlationId = NULL; + } free(result); result = NULL; } @@ -218,6 +263,16 @@ if ((result->value.string = STRING_clone(source->value.string)) == NULL) { /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ + if (result->messageId) + { + free(result->messageId); + result->messageId = NULL; + } + if (result->correlationId != NULL) + { + free(result->correlationId); + result->correlationId = NULL; + } free(result); result = NULL; LogError("failed to STRING_clone\r\n"); @@ -228,6 +283,16 @@ /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/ LogError("unable to Map_Clone\r\n"); STRING_delete(result->value.string); + if (result->messageId) + { + free(result->messageId); + result->messageId = NULL; + } + if (result->correlationId != NULL) + { + free(result->correlationId); + result->correlationId = NULL; + } free(result); result = NULL; } @@ -324,8 +389,8 @@ /*Codes_SRS_IOTHUBMESSAGE_02_001: [If iotHubMessageHandle is NULL then IoTHubMessage_Properties shall return NULL.]*/ if (iotHubMessageHandle == NULL) { - LogError("invalid arg (NULL) passed to IoTHubMessage_Properties\r\n") - result = NULL; + LogError("invalid arg (NULL) passed to IoTHubMessage_Properties\r\n"); + result = NULL; } else { @@ -336,6 +401,105 @@ return result; } +const char* IoTHubMessage_GetCorrelationId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle) +{ + const char* result; + /* Codes_SRS_IOTHUBMESSAGE_07_016: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_GetCorrelationId shall return a NULL value.] */ + if (iotHubMessageHandle == NULL) + { + LogError("invalid arg (NULL) passed to IoTHubMessage_GetCorrelationId\r\n"); + result = NULL; + } + else + { + /* Codes_SRS_IOTHUBMESSAGE_07_017: [IoTHubMessage_GetCorrelationId shall return the correlationId as a const char*.] */ + IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle; + result = handleData->correlationId; + } + return result; +} + +IOTHUB_MESSAGE_RESULT IoTHubMessage_SetCorrelationId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* correlationId) +{ + IOTHUB_MESSAGE_RESULT result; + /* Codes_SRS_IOTHUBMESSAGE_07_018: [if any of the parameters are NULL then IoTHubMessage_SetCorrelationId shall return a IOTHUB_MESSAGE_INVALID_ARG value.]*/ + if (iotHubMessageHandle == NULL || correlationId == NULL) + { + LogError("invalid arg (NULL) passed to IoTHubMessage_SetCorrelationId\r\n"); + result = IOTHUB_MESSAGE_INVALID_ARG; + } + else + { + IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle; + /* Codes_SRS_IOTHUBMESSAGE_07_019: [If the IOTHUB_MESSAGE_HANDLE correlationId is not NULL, then the IOTHUB_MESSAGE_HANDLE correlationId will be deallocated.] */ + if (handleData->correlationId != NULL) + { + free(handleData->correlationId); + } + + if (mallocAndStrcpy_s(&handleData->correlationId, correlationId) != 0) + { + /* Codes_SRS_IOTHUBMESSAGE_07_020: [If the allocation or the copying of the correlationId fails, then IoTHubMessage_SetCorrelationId shall return IOTHUB_MESSAGE_ERROR.] */ + result = IOTHUB_MESSAGE_ERROR; + } + else + { + /* Codes_SRS_IOTHUBMESSAGE_07_021: [IoTHubMessage_SetCorrelationId finishes successfully it shall return IOTHUB_MESSAGE_OK.] */ + result = IOTHUB_MESSAGE_OK; + } + } + return result; +} + +IOTHUB_MESSAGE_RESULT IoTHubMessage_SetMessageId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* messageId) +{ + IOTHUB_MESSAGE_RESULT result; + /* Codes_SRS_IOTHUBMESSAGE_07_012: [if any of the parameters are NULL then IoTHubMessage_SetMessageId shall return a IOTHUB_MESSAGE_INVALID_ARG value.] */ + if (iotHubMessageHandle == NULL || messageId == NULL) + { + LogError("invalid arg (NULL) passed to IoTHubMessage_SetMessageId\r\n"); + result = IOTHUB_MESSAGE_INVALID_ARG; + } + else + { + IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle; + /* Codes_SRS_IOTHUBMESSAGE_07_013: [If the IOTHUB_MESSAGE_HANDLE messageId is not NULL, then the IOTHUB_MESSAGE_HANDLE messageId will be freed] */ + if (handleData->messageId != NULL) + { + free(handleData->messageId); + } + + /* Codes_SRS_IOTHUBMESSAGE_07_014: [If the allocation or the copying of the messageId fails, then IoTHubMessage_SetMessageId shall return IOTHUB_MESSAGE_ERROR.] */ + if (mallocAndStrcpy_s(&handleData->messageId, messageId) != 0) + { + result = IOTHUB_MESSAGE_ERROR; + } + else + { + result = IOTHUB_MESSAGE_OK; + } + } + return result; +} + +const char* IoTHubMessage_GetMessageId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle) +{ + const char* result; + /* Codes_SRS_IOTHUBMESSAGE_07_010: [if the iotHubMessageHandle parameter is NULL then IoTHubMessage_MessageId shall return a NULL value.] */ + if (iotHubMessageHandle == NULL) + { + LogError("invalid arg (NULL) passed to IoTHubMessage_GetMessageId\r\n"); + result = NULL; + } + else + { + /* Codes_SRS_IOTHUBMESSAGE_07_011: [IoTHubMessage_MessageId shall return the messageId as a const char*.] */ + IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle; + result = handleData->messageId; + } + return result; +} + void IoTHubMessage_Destroy(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle) { /*Codes_SRS_IOTHUBMESSAGE_01_004: [If iotHubMessageHandle is NULL, IoTHubMessage_Destroy shall do nothing.] */ @@ -353,6 +517,10 @@ STRING_delete(handleData->value.string); } Map_Destroy(handleData->properties); + free(handleData->messageId); + handleData->messageId = NULL; + free(handleData->correlationId); + handleData->correlationId = NULL; free(handleData); } } \ No newline at end of file