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:
61:8b85a4e797cf
Parent:
60:41648c4e7036
Child:
74:ea0021abecf7
diff -r 41648c4e7036 -r 8b85a4e797cf iothub_message.c
--- a/iothub_message.c	Fri Feb 24 14:00:43 2017 -0800
+++ b/iothub_message.c	Fri Mar 10 11:47:36 2017 -0800
@@ -63,109 +63,126 @@
 IOTHUB_MESSAGE_HANDLE IoTHubMessage_CreateFromByteArray(const unsigned char* byteArray, size_t size)
 {
     IOTHUB_MESSAGE_HANDLE_DATA* result;
-    result = malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
-    if (result == NULL)
+    if ((byteArray == NULL) && (size != 0))
     {
-        LogError("unable to malloc");
-        /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
-        /*let it go through*/
+        LogError("Invalid argument - byteArray is NULL");
+        result = NULL;
     }
     else
     {
-        const unsigned char* source;
-        unsigned char temp = 0x00;
-        if (size != 0)
+        result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
+        if (result == NULL)
+        {
+            LogError("unable to malloc");
+            /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
+            /*let it go through*/
+        }
+        else
         {
-            /*Codes_SRS_IOTHUBMESSAGE_06_002: [If size is NOT zero then byteArray MUST NOT be NULL*/
-            if (byteArray == NULL)
+            const unsigned char* source;
+            unsigned char temp = 0x00;
+            if (size != 0)
             {
-                LogError("Attempted to create a Hub Message from a NULL pointer!");
-                free(result);
-                result = NULL;
-                source = NULL;
+                /*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);
+                    result = NULL;
+                    source = NULL;
+                }
+                else
+                {
+                    source = byteArray;
+                }
             }
             else
             {
-                source = byteArray;
+                /*Codes_SRS_IOTHUBMESSAGE_06_001: [If size is zero then byteArray may be NULL.]*/
+                source = &temp;
             }
+            if (result != NULL)
+            {
+                /*Codes_SRS_IOTHUBMESSAGE_02_022: [IoTHubMessage_CreateFromByteArray shall call BUFFER_create passing byteArray and size as parameters.] */
+                if ((result->value.byteArray = BUFFER_create(source, size)) == NULL)
+                {
+                    LogError("BUFFER_create failed");
+                    /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
+                    free(result);
+                    result = NULL;
+                }
+                /*Codes_SRS_IOTHUBMESSAGE_02_023: [IoTHubMessage_CreateFromByteArray shall call Map_Create to create the message properties.] */
+                else if ((result->properties = Map_Create(ValidateAsciiCharactersFilter)) == NULL)
+                {
+                    LogError("Map_Create failed");
+                    /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
+                    BUFFER_delete(result->value.byteArray);
+                    free(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;
+                    /*all is fine, return result*/
+                }
+            }
+        }
+    }
+    return result;
+}
+
+IOTHUB_MESSAGE_HANDLE IoTHubMessage_CreateFromString(const char* source)
+{
+    IOTHUB_MESSAGE_HANDLE_DATA* result;
+    if (source == NULL)
+    {
+        LogError("Invalid argument - source is NULL");
+        result = NULL;
+    }
+    else
+    {
+        result = (IOTHUB_MESSAGE_HANDLE_DATA*)malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
+        if (result == NULL)
+        {
+            LogError("malloc failed");
+            /*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
+            /*let it go through*/
         }
         else
         {
-            /*Codes_SRS_IOTHUBMESSAGE_06_001: [If size is zero then byteArray may be NULL.]*/
-            source = &temp;
-        }
-        if (result != NULL)
-        {
-            /*Codes_SRS_IOTHUBMESSAGE_02_022: [IoTHubMessage_CreateFromByteArray shall call BUFFER_create passing byteArray and size as parameters.] */
-            if ((result->value.byteArray = BUFFER_create(source, size)) == NULL)
+            /*Codes_SRS_IOTHUBMESSAGE_02_027: [IoTHubMessage_CreateFromString shall call STRING_construct passing source as parameter.] */
+            if ((result->value.string = STRING_construct(source)) == NULL)
             {
-                LogError("BUFFER_create failed");
-                /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return 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);
                 result = NULL;
             }
-            /*Codes_SRS_IOTHUBMESSAGE_02_023: [IoTHubMessage_CreateFromByteArray shall call Map_Create to create the message properties.] */
+            /*Codes_SRS_IOTHUBMESSAGE_02_028: [IoTHubMessage_CreateFromString shall call Map_Create to create the message properties.] */
             else if ((result->properties = Map_Create(ValidateAsciiCharactersFilter)) == NULL)
             {
                 LogError("Map_Create failed");
-                /*Codes_SRS_IOTHUBMESSAGE_02_024: [If there are any errors then IoTHubMessage_CreateFromByteArray shall return NULL.] */
-                BUFFER_delete(result->value.byteArray);
+                /*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);
                 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;
+                /*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;
-                /*all is fine, return result*/
             }
         }
     }
     return result;
 }
-IOTHUB_MESSAGE_HANDLE IoTHubMessage_CreateFromString(const char* source)
-{
-    IOTHUB_MESSAGE_HANDLE_DATA* result;
-    result = malloc(sizeof(IOTHUB_MESSAGE_HANDLE_DATA));
-    if (result == NULL)
-    {
-        LogError("malloc failed");
-        /*Codes_SRS_IOTHUBMESSAGE_02_029: [If there are any encountered in the execution of IoTHubMessage_CreateFromString then IoTHubMessage_CreateFromString shall return NULL.] */
-        /*let it go through*/
-    }
-    else
-    {
-        /*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);
-            result = NULL;
-        }
-        /*Codes_SRS_IOTHUBMESSAGE_02_028: [IoTHubMessage_CreateFromString shall call Map_Create to create the message properties.] */
-        else if ((result->properties = Map_Create(ValidateAsciiCharactersFilter)) == NULL)
-        {
-            LogError("Map_Create 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);
-            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;
-        }
-    }
-    return result;
-}
 
 /*Codes_SRS_IOTHUBMESSAGE_03_001: [IoTHubMessage_Clone shall create a new IoT hub message with data content identical to that of the iotHubMessageHandle parameter.]*/
 IOTHUB_MESSAGE_HANDLE IoTHubMessage_Clone(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
@@ -509,10 +526,13 @@
         {
             BUFFER_delete(handleData->value.byteArray);
         }
+        else if (handleData->contentType == IOTHUBMESSAGE_STRING)
+        {
+            STRING_delete(handleData->value.string);
+        }
         else
         {
-            /*can only be STRING*/
-            STRING_delete(handleData->value.string);
+            LogError("Unknown contentType in IoTHubMessage");
         }
         Map_Destroy(handleData->properties);
         free(handleData->messageId);