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:
77:e4e36df9caee
Parent:
74:ea0021abecf7
Child:
78:74a8d3068204
--- a/iothub_message.c	Mon Sep 25 13:37:53 2017 -0700
+++ b/iothub_message.c	Sat Oct 21 20:11:49 2017 +0000
@@ -18,7 +18,7 @@
 typedef struct IOTHUB_MESSAGE_HANDLE_DATA_TAG
 {
     IOTHUBMESSAGE_CONTENT_TYPE contentType;
-    union 
+    union
     {
         BUFFER_HANDLE byteArray;
         STRING_HANDLE string;
@@ -28,6 +28,7 @@
     char* correlationId;
     char* userDefinedContentType;
     char* contentEncoding;
+    IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE diagnosticData;
 }IOTHUB_MESSAGE_HANDLE_DATA;
 
 static bool ContainsOnlyUsAscii(const char* asciiValue)
@@ -51,7 +52,7 @@
 static int ValidateAsciiCharactersFilter(const char* mapKey, const char* mapValue)
 {
     int result;
-    if (!ContainsOnlyUsAscii(mapKey) || !ContainsOnlyUsAscii(mapValue) )
+    if (!ContainsOnlyUsAscii(mapKey) || !ContainsOnlyUsAscii(mapValue))
     {
         result = __FAILURE__;
     }
@@ -62,6 +63,52 @@
     return result;
 }
 
+static void DestroyDiagnosticPropertyData(IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE diagnosticHandle)
+{
+    if (diagnosticHandle != NULL)
+    {
+        free(diagnosticHandle->diagnosticId);
+        free(diagnosticHandle->diagnosticCreationTimeUtc);
+    }
+    free(diagnosticHandle);
+}
+
+static IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE CloneDiagnosticPropertyData(const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* source)
+{
+    IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE result = NULL;
+    if (source == NULL)
+    {
+        LogError("Invalid argument - source is NULL");
+    }
+    else
+    {
+        result = (IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE)malloc(sizeof(IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA));
+        if (result == NULL)
+        {
+            LogError("malloc failed");
+        }
+        else
+        {
+            result->diagnosticCreationTimeUtc = NULL;
+            result->diagnosticId = NULL;
+            if (source->diagnosticCreationTimeUtc != NULL && mallocAndStrcpy_s(&result->diagnosticCreationTimeUtc, source->diagnosticCreationTimeUtc) != 0)
+            {
+                LogError("mallocAndStrcpy_s for diagnosticCreationTimeUtc failed");
+                free(result);
+                result = NULL;
+            }
+            else if (source->diagnosticId != NULL && mallocAndStrcpy_s(&result->diagnosticId, source->diagnosticId) != 0)
+            {
+                LogError("mallocAndStrcpy_s for diagnosticId failed");
+                free(result->diagnosticCreationTimeUtc);
+                free(result);
+                result = NULL;
+            }
+        }
+    }
+    return result;
+}
+
 IOTHUB_MESSAGE_HANDLE IoTHubMessage_CreateFromByteArray(const unsigned char* byteArray, size_t size)
 {
     IOTHUB_MESSAGE_HANDLE_DATA* result;
@@ -116,7 +163,7 @@
                 /*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");
+                    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);
@@ -131,6 +178,7 @@
                     result->correlationId = NULL;
                     result->userDefinedContentType = NULL;
                     result->contentEncoding = NULL;
+                    result->diagnosticData = NULL;
                     /*all is fine, return result*/
                 }
             }
@@ -169,7 +217,7 @@
             /*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");
+                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);
@@ -184,6 +232,7 @@
                 result->correlationId = NULL;
                 result->userDefinedContentType = NULL;
                 result->contentEncoding = NULL;
+                result->diagnosticData = NULL;
             }
         }
     }
@@ -215,8 +264,10 @@
         {
             result->messageId = NULL;
             result->correlationId = NULL;
+            result->contentEncoding = NULL;
             result->userDefinedContentType = NULL;
-            result->contentEncoding = NULL;
+            result->properties = NULL;
+            result->diagnosticData = NULL;
 
             if (source->messageId != NULL && mallocAndStrcpy_s(&result->messageId, source->messageId) != 0)
             {
@@ -266,6 +317,16 @@
                 free(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);
+                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] */
@@ -283,6 +344,7 @@
                         free(result->correlationId);
                         result->correlationId = NULL;
                     }
+                    DestroyDiagnosticPropertyData(result->diagnosticData);
                     free(result);
                     result = NULL;
                 }
@@ -302,6 +364,7 @@
                         free(result->correlationId);
                         result->correlationId = NULL;
                     }
+                    DestroyDiagnosticPropertyData(result->diagnosticData);
                     free(result);
                     result = NULL;
                 }
@@ -328,6 +391,7 @@
                         free(result->correlationId);
                         result->correlationId = NULL;
                     }
+                    DestroyDiagnosticPropertyData(result->diagnosticData);
                     free(result);
                     result = NULL;
                     LogError("failed to STRING_clone");
@@ -348,6 +412,7 @@
                         free(result->correlationId);
                         result->correlationId = NULL;
                     }
+                    DestroyDiagnosticPropertyData(result->diagnosticData);
                     free(result);
                     result = NULL;
                 }
@@ -673,6 +738,62 @@
     return result;
 }
 
+const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* IoTHubMessage_GetDiagnosticPropertyData(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
+{
+    const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* result;
+    // Codes_SRS_IOTHUBMESSAGE_10_001: [If any of the parameters are NULL then IoTHubMessage_GetDiagnosticPropertyData shall return a NULL value.] 
+    if (iotHubMessageHandle == NULL)
+    {
+        LogError("Invalid argument (iotHubMessageHandle is NULL)");
+        result = NULL;
+    }
+    else
+    {
+        /* Codes_SRS_IOTHUBMESSAGE_10_002: [IoTHubMessage_GetDiagnosticPropertyData shall return the diagnosticData as a const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA*.] */
+        result = iotHubMessageHandle->diagnosticData;
+    }
+    return result;
+}
+
+IOTHUB_MESSAGE_RESULT IoTHubMessage_SetDiagnosticPropertyData(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* diagnosticData)
+{
+    IOTHUB_MESSAGE_RESULT result;
+    // Codes_SRS_IOTHUBMESSAGE_10_003: [If any of the parameters are NULL then IoTHubMessage_SetDiagnosticId shall return a IOTHUB_MESSAGE_INVALID_ARG value.] 
+    if (iotHubMessageHandle == NULL || 
+        diagnosticData == NULL ||
+        diagnosticData->diagnosticCreationTimeUtc == NULL ||
+        diagnosticData->diagnosticId == NULL)
+    {
+        LogError("Invalid argument (iotHubMessageHandle=%p, diagnosticData=%p, diagnosticData->diagnosticId=%p, diagnosticData->diagnosticCreationTimeUtc=%p)", 
+            iotHubMessageHandle, diagnosticData, 
+            diagnosticData == NULL ? NULL : diagnosticData->diagnosticId,
+            diagnosticData == NULL ? NULL : diagnosticData->diagnosticCreationTimeUtc);
+        result = IOTHUB_MESSAGE_INVALID_ARG;
+    }
+    else
+    {
+        // Codes_SRS_IOTHUBMESSAGE_10_004: [If the IOTHUB_MESSAGE_HANDLE `diagnosticData` is not NULL it shall be deallocated.] 
+        if (iotHubMessageHandle->diagnosticData != NULL)
+        {
+            DestroyDiagnosticPropertyData(iotHubMessageHandle->diagnosticData);
+            iotHubMessageHandle->diagnosticData = NULL;
+        }
+
+        // Codes_SRS_IOTHUBMESSAGE_10_005: [If the allocation or the copying of `diagnosticData` fails, then IoTHubMessage_SetDiagnosticPropertyData shall return IOTHUB_MESSAGE_ERROR.]
+        if ((iotHubMessageHandle->diagnosticData = CloneDiagnosticPropertyData(diagnosticData)) == NULL)
+        {
+            LogError("Failed saving a copy of diagnosticData");
+            result = IOTHUB_MESSAGE_ERROR;
+        }
+        else
+        {
+            // Codes_SRS_IOTHUBMESSAGE_10_006: [If IoTHubMessage_SetDiagnosticPropertyData finishes successfully it shall return IOTHUB_MESSAGE_OK.]
+            result = IOTHUB_MESSAGE_OK;
+        }
+    }
+    return result;
+}
+
 void IoTHubMessage_Destroy(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle)
 {
     /*Codes_SRS_IOTHUBMESSAGE_01_004: [If iotHubMessageHandle is NULL, IoTHubMessage_Destroy shall do nothing.] */
@@ -699,6 +820,7 @@
         handleData->correlationId = NULL;
         free(handleData->userDefinedContentType);
         free(handleData->contentEncoding);
+        DestroyDiagnosticPropertyData(handleData->diagnosticData);
         free(handleData);
     }
 }