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

Revision:
78:74a8d3068204
Parent:
77:e4e36df9caee
Child:
88:248736be106e
--- a/iothub_message.c	Sat Oct 21 20:11:49 2017 +0000
+++ b/iothub_message.c	Fri Nov 03 13:18:25 2017 -0700
@@ -73,6 +73,28 @@
     free(diagnosticHandle);
 }
 
+static void DestroyMessageData(IOTHUB_MESSAGE_HANDLE_DATA* handleData)
+{
+    if (handleData->contentType == IOTHUBMESSAGE_BYTEARRAY)
+    {
+        BUFFER_delete(handleData->value.byteArray);
+    }
+    else if (handleData->contentType == IOTHUBMESSAGE_STRING)
+    {
+        STRING_delete(handleData->value.string);
+    }
+
+    Map_Destroy(handleData->properties);
+    free(handleData->messageId);
+    handleData->messageId = NULL;
+    free(handleData->correlationId);
+    handleData->correlationId = NULL;
+    free(handleData->userDefinedContentType);
+    free(handleData->contentEncoding);
+    DestroyDiagnosticPropertyData(handleData->diagnosticData);
+    free(handleData);
+}
+
 static IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE CloneDiagnosticPropertyData(const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* source)
 {
     IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE result = NULL;
@@ -130,13 +152,18 @@
         {
             const unsigned char* source;
             unsigned char temp = 0x00;
+
+            memset(result, 0, sizeof(*result));
+            /*Codes_SRS_IOTHUBMESSAGE_02_026: [The type of the new message shall be IOTHUBMESSAGE_BYTEARRAY.] */
+            result->contentType = IOTHUBMESSAGE_BYTEARRAY;
+
             if (size != 0)
             {
                 /*Codes_SRS_IOTHUBMESSAGE_06_002: [If size is NOT zero then byteArray MUST NOT be NULL*/
                 if (byteArray == NULL)
                 {
                     LogError("Attempted to create a Hub Message from a NULL pointer!");
-                    free(result);
+                    DestroyMessageData(result);
                     result = NULL;
                     source = NULL;
                 }
@@ -157,7 +184,7 @@
                 {
                     LogError("BUFFER_create failed");
                     /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
-                    free(result);
+                    DestroyMessageData(result);
                     result = NULL;
                 }
                 /*Codes_SRS_IOTHUBMESSAGE_02_023: [IoTHubMessage_CreateFromByteArray shall call Map_Create to create the message properties.] */
@@ -165,22 +192,10 @@
                 {
                     LogError("Map_Create for properties failed");
                     /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
-                    BUFFER_delete(result->value.byteArray);
-                    free(result);
+                    DestroyMessageData(result);
                     result = NULL;
                 }
-                else
-                {
-                    /*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;
-                    result->userDefinedContentType = NULL;
-                    result->contentEncoding = NULL;
-                    result->diagnosticData = NULL;
-                    /*all is fine, return result*/
-                }
+                /*Codes_SRS_IOTHUBMESSAGE_02_025: [Otherwise, IoTHubMessage_CreateFromByteArray shall return a non-NULL handle.] */
             }
         }
     }
@@ -206,12 +221,16 @@
         }
         else
         {
+            memset(result, 0, sizeof(*result));
+            /*Codes_SRS_IOTHUBMESSAGE_02_032: [The type of the new message shall be IOTHUBMESSAGE_STRING.] */
+            result->contentType = IOTHUBMESSAGE_STRING;
+            
             /*Codes_SRS_IOTHUBMESSAGE_02_027: [IoTHubMessage_CreateFromString shall call STRING_construct passing source as parameter.] */
             if ((result->value.string = STRING_construct(source)) == NULL)
             {
                 LogError("STRING_construct failed");
                 /*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
-                free(result);
+                DestroyMessageData(result);
                 result = NULL;
             }
             /*Codes_SRS_IOTHUBMESSAGE_02_028: [IoTHubMessage_CreateFromString shall call Map_Create to create the message properties.] */
@@ -219,21 +238,10 @@
             {
                 LogError("Map_Create for properties failed");
                 /*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
-                STRING_delete(result->value.string);
-                free(result);
+                DestroyMessageData(result);
                 result = NULL;
             }
-            else
-            {
-                /*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;
-                result->userDefinedContentType = NULL;
-                result->contentEncoding = NULL;
-                result->diagnosticData = NULL;
-            }
+            /*Codes_SRS_IOTHUBMESSAGE_02_031: [Otherwise, IoTHubMessage_CreateFromString shall return a non-NULL handle.] */
         }
     }
     return result;
@@ -262,69 +270,37 @@
         }
         else
         {
-            result->messageId = NULL;
-            result->correlationId = NULL;
-            result->contentEncoding = NULL;
-            result->userDefinedContentType = NULL;
-            result->properties = NULL;
-            result->diagnosticData = NULL;
+            memset(result, 0, sizeof(*result));
+            result->contentType = source->contentType;
 
             if (source->messageId != NULL && mallocAndStrcpy_s(&result->messageId, source->messageId) != 0)
             {
                 LogError("unable to Copy messageId");
-                free(result);
+                DestroyMessageData(result);
                 result = NULL;
             }
             else if (source->correlationId != NULL && mallocAndStrcpy_s(&result->correlationId, source->correlationId) != 0)
             {
                 LogError("unable to Copy correlationId");
-                if (result->messageId != NULL)
-                {
-                    free(result->messageId);
-                }
-                free(result);
+                DestroyMessageData(result);
                 result = NULL;
             }
             else if (source->userDefinedContentType != NULL && mallocAndStrcpy_s(&result->userDefinedContentType, source->userDefinedContentType) != 0)
             {
                 LogError("unable to copy contentType");
-                if (result->messageId != NULL)
-                {
-                    free(result->messageId);
-                }
-                if (result->correlationId != NULL)
-                {
-                    free(result->correlationId);
-                }
-                free(result);
+                DestroyMessageData(result);
                 result = NULL;
             }
             else if (source->contentEncoding != NULL && mallocAndStrcpy_s(&result->contentEncoding, source->contentEncoding) != 0)
             {
                 LogError("unable to copy contentEncoding");
-                if (result->messageId != NULL)
-                {
-                    free(result->messageId);
-                }
-                if (result->correlationId != NULL)
-                {
-                    free(result->correlationId);
-                }
-                if (result->userDefinedContentType != NULL)
-                {
-                    free(result->userDefinedContentType);
-                }
-                free(result);
+                DestroyMessageData(result);
                 result = NULL;
             }
             else if (source->diagnosticData != NULL && (result->diagnosticData = CloneDiagnosticPropertyData(source->diagnosticData)) == NULL)
             {
                 LogError("unable to CloneDiagnosticPropertyData");
-                free(result->messageId);
-                free(result->correlationId);
-                free(result->userDefinedContentType);
-                free(result->contentEncoding);
-                free(result);
+                DestroyMessageData(result);
                 result = NULL;
             }
             else if (source->contentType == IOTHUBMESSAGE_BYTEARRAY)
@@ -334,18 +310,7 @@
                 {
                     /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
                     LogError("unable to BUFFER_clone");
-                    if (result->messageId)
-                    {
-                        free(result->messageId);
-                        result->messageId = NULL;
-                    }
-                    if (result->correlationId != NULL)
-                    {
-                        free(result->correlationId);
-                        result->correlationId = NULL;
-                    }
-                    DestroyDiagnosticPropertyData(result->diagnosticData);
-                    free(result);
+                    DestroyMessageData(result);
                     result = NULL;
                 }
                 /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */
@@ -353,27 +318,10 @@
                 {
                     /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
                     LogError("unable to Map_Clone");
-                    BUFFER_delete(result->value.byteArray);
-                    if (result->messageId)
-                    {
-                        free(result->messageId);
-                        result->messageId = NULL;
-                    }
-                    if (result->correlationId != NULL)
-                    {
-                        free(result->correlationId);
-                        result->correlationId = NULL;
-                    }
-                    DestroyDiagnosticPropertyData(result->diagnosticData);
-                    free(result);
+                    DestroyMessageData(result);
                     result = NULL;
                 }
-                else
-                {
-                    result->contentType = IOTHUBMESSAGE_BYTEARRAY;
-                    /*Codes_SRS_IOTHUBMESSAGE_03_002: [IoTHubMessage_Clone shall return upon success a non-NULL handle to the newly created IoT hub message.]*/
-                    /*return as is, this is a good result*/
-                }
+                /*Codes_SRS_IOTHUBMESSAGE_03_002: [IoTHubMessage_Clone shall return upon success a non-NULL handle to the newly created IoT hub message.]*/
             }
             else /*can only be STRING*/
             {
@@ -381,46 +329,18 @@
                 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;
-                    }
-                    DestroyDiagnosticPropertyData(result->diagnosticData);
-                    free(result);
+                    LogError("failed to STRING_clone");
+                    DestroyMessageData(result);
                     result = NULL;
-                    LogError("failed to STRING_clone");
                 }
                 /*Codes_SRS_IOTHUBMESSAGE_02_005: [IoTHubMessage_Clone shall clone the properties map by using Map_Clone.] */
                 else if ((result->properties = Map_Clone(source->properties)) == NULL)
                 {
                     /*Codes_SRS_IOTHUBMESSAGE_03_004: [IoTHubMessage_Clone shall return NULL if it fails for any reason.]*/
                     LogError("unable to Map_Clone");
-                    STRING_delete(result->value.string);
-                    if (result->messageId)
-                    {
-                        free(result->messageId);
-                        result->messageId = NULL;
-                    }
-                    if (result->correlationId != NULL)
-                    {
-                        free(result->correlationId);
-                        result->correlationId = NULL;
-                    }
-                    DestroyDiagnosticPropertyData(result->diagnosticData);
-                    free(result);
+                    DestroyMessageData(result);
                     result = NULL;
                 }
-                else
-                {
-                    result->contentType = IOTHUBMESSAGE_STRING;
-                    /*all is fine*/
-                }
             }
         }
     }
@@ -800,27 +720,6 @@
     if (iotHubMessageHandle != NULL)
     {
         /*Codes_SRS_IOTHUBMESSAGE_01_003: [IoTHubMessage_Destroy shall free all resources associated with iotHubMessageHandle.]  */
-        IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
-        if (handleData->contentType == IOTHUBMESSAGE_BYTEARRAY)
-        {
-            BUFFER_delete(handleData->value.byteArray);
-        }
-        else if (handleData->contentType == IOTHUBMESSAGE_STRING)
-        {
-            STRING_delete(handleData->value.string);
-        }
-        else
-        {
-            LogError("Unknown contentType in IoTHubMessage");
-        }
-        Map_Destroy(handleData->properties);
-        free(handleData->messageId);
-        handleData->messageId = NULL;
-        free(handleData->correlationId);
-        handleData->correlationId = NULL;
-        free(handleData->userDefinedContentType);
-        free(handleData->contentEncoding);
-        DestroyDiagnosticPropertyData(handleData->diagnosticData);
-        free(handleData);
+        DestroyMessageData((IOTHUB_MESSAGE_HANDLE_DATA* )iotHubMessageHandle);
     }
 }