A modelling and serializer library for Microsoft Azure IoTHub client applications

Dependents:   sht15_remote_monitoring f767zi_mqtt remote_monitoring simplesample_amqp ... more

This library implements a serializer library to be used in projects involving Microsoft Azure IoT Hub connectivity. The code is replicated from https://github.com/Azure/azure-iot-sdks

Revision:
36:7d12a5386197
Parent:
24:49c36a13bed1
--- a/schema.c	Thu Jul 12 18:10:30 2018 -0700
+++ b/schema.c	Tue Sep 11 11:14:37 2018 -0700
@@ -134,7 +134,7 @@
     if (action != NULL)
     {
         size_t j;
-        
+
         for (j = 0; j < action->ArgumentCount; j++)
         {
             DestroyActionArgument(action->ArgumentHandles[j]);
@@ -328,15 +328,22 @@
                 /* If possible, reduce the memory of over allocation */
                 if (result != SCHEMA_OK)
                 {
-                    SCHEMA_PROPERTY_HANDLE* oldProperties = (SCHEMA_PROPERTY_HANDLE*)realloc(modelType->Properties, sizeof(SCHEMA_PROPERTY_HANDLE) * modelType->PropertyCount);
-                    if (oldProperties == NULL)
+                    if (modelType->PropertyCount > 0)
                     {
-                        result = SCHEMA_ERROR;
-                        LogError("(result = %s)", ENUM_TO_STRING(SCHEMA_RESULT, result));
+                        SCHEMA_PROPERTY_HANDLE *oldProperties = (SCHEMA_PROPERTY_HANDLE *)realloc(modelType->Properties, sizeof(SCHEMA_PROPERTY_HANDLE) * modelType->PropertyCount);
+                        if (oldProperties == NULL)
+                        {
+                            result = SCHEMA_ERROR;
+                            LogError("(result = %s)", ENUM_TO_STRING(SCHEMA_RESULT, result));
+                        }
+                        else
+                        {
+                            modelType->Properties = oldProperties;
+                        }
                     }
                     else
                     {
-                        modelType->Properties = oldProperties;
+                        modelType->Properties = NULL;
                     }
                 }
             }
@@ -431,7 +438,7 @@
         SCHEMA_HANDLE* handle = (g_schemas==NULL)?NULL:(SCHEMA_HANDLE*)VECTOR_find_if(g_schemas, (PREDICATE_FUNCTION)SchemaNamespacesMatch, schemaNamespace);
         if (handle != NULL)
         {
-            /* Codes_SRS_SCHEMA_99_148: [Schema_GetSchemaByNamespace shall search all active schemas and return the schema with the 
+            /* Codes_SRS_SCHEMA_99_148: [Schema_GetSchemaByNamespace shall search all active schemas and return the schema with the
                namespace given by the schemaNamespace argument.] */
             result = *handle;
         }
@@ -918,14 +925,21 @@
                     /* If possible, reduce the memory of over allocation */
                     if (result == NULL)
                     {
-                        SCHEMA_ACTION_HANDLE* oldActions = (SCHEMA_ACTION_HANDLE*)realloc(modelType->Actions, sizeof(SCHEMA_ACTION_HANDLE) * modelType->ActionCount);
-                        if (oldActions == NULL)
+                        if (modelType->ActionCount > 0)
                         {
-                            LogError("(Error code:%s)", ENUM_TO_STRING(SCHEMA_RESULT, SCHEMA_ERROR));
+                            SCHEMA_ACTION_HANDLE *oldActions = (SCHEMA_ACTION_HANDLE *)realloc(modelType->Actions, sizeof(SCHEMA_ACTION_HANDLE) * modelType->ActionCount);
+                            if (oldActions == NULL)
+                            {
+                                LogError("(Error code:%s)", ENUM_TO_STRING(SCHEMA_RESULT, SCHEMA_ERROR));
+                            }
+                            else
+                            {
+                                modelType->Actions = oldActions;
+                            }
                         }
                         else
                         {
-                            modelType->Actions = oldActions;
+                            modelType->Actions = NULL;
                         }
                     }
                 }
@@ -1066,7 +1080,11 @@
             }
             else
             {
+                action->ArgumentHandles = newArguments;
+
                 SCHEMA_ACTION_ARGUMENT_HANDLE_DATA* newActionArgument;
+
+                action->ArgumentHandles = newArguments;
                 if ((newActionArgument = (SCHEMA_ACTION_ARGUMENT_HANDLE_DATA*)malloc(sizeof(SCHEMA_ACTION_ARGUMENT_HANDLE_DATA))) == NULL)
                 {
                     /* Codes_SRS_SCHEMA_99_112: [On any other error, Schema_ AddModelActionArgumet shall return SCHEMA_ERROR.] */
@@ -1092,7 +1110,6 @@
                     }
                     else
                     {
-                        action->ArgumentHandles = newArguments;
                         /* Codes_SRS_SCHEMA_99_119: [Schema_AddModelActionArgument shall preserve the order of the action arguments according to the order in which they were added, starting with index 0 for the first added argument.] */
                         action->ArgumentHandles[action->ArgumentCount] = newActionArgument;
                         action->ArgumentCount++;
@@ -1105,14 +1122,21 @@
                 /* If possible, reduce the memory of over allocation */
                 if (result == SCHEMA_ERROR)
                 {
-                    SCHEMA_ACTION_ARGUMENT_HANDLE* oldArguments = (SCHEMA_ACTION_ARGUMENT_HANDLE*)realloc(action->ArgumentHandles, sizeof(SCHEMA_ACTION_ARGUMENT_HANDLE) * action->ArgumentCount);
-                    if (oldArguments == NULL)
+                    if (action->ArgumentCount > 0)
                     {
-                        LogError("(result = %s)", ENUM_TO_STRING(SCHEMA_RESULT, SCHEMA_ERROR));
+                        SCHEMA_ACTION_ARGUMENT_HANDLE *oldArguments = (SCHEMA_ACTION_ARGUMENT_HANDLE *)realloc(action->ArgumentHandles, sizeof(SCHEMA_ACTION_ARGUMENT_HANDLE) * action->ArgumentCount);
+                        if (oldArguments == NULL)
+                        {
+                            LogError("(result = %s)", ENUM_TO_STRING(SCHEMA_RESULT, SCHEMA_ERROR));
+                        }
+                        else
+                        {
+                            action->ArgumentHandles = oldArguments;
+                        }
                     }
                     else
                     {
-                        action->ArgumentHandles = oldArguments;
+                        action->ArgumentHandles = NULL;
                     }
                 }
             }
@@ -1807,15 +1831,22 @@
                 /* If possible, reduce the memory of over allocation */
                 if (result == NULL)
                 {
-                    SCHEMA_STRUCT_TYPE_HANDLE* oldStructTypes = (SCHEMA_STRUCT_TYPE_HANDLE*)realloc(schema->StructTypes, sizeof(SCHEMA_STRUCT_TYPE_HANDLE) * schema->StructTypeCount);
-                    if (oldStructTypes == NULL)
+                    if (schema->StructTypeCount > 0)
                     {
-                        result = NULL;
-                        LogError("(Error code:%s)", ENUM_TO_STRING(SCHEMA_RESULT, SCHEMA_ERROR));
+                        SCHEMA_STRUCT_TYPE_HANDLE *oldStructTypes = (SCHEMA_STRUCT_TYPE_HANDLE *)realloc(schema->StructTypes, sizeof(SCHEMA_STRUCT_TYPE_HANDLE) * schema->StructTypeCount);
+                        if (oldStructTypes == NULL)
+                        {
+                            result = NULL;
+                            LogError("(Error code:%s)", ENUM_TO_STRING(SCHEMA_RESULT, SCHEMA_ERROR));
+                        }
+                        else
+                        {
+                            schema->StructTypes = oldStructTypes;
+                        }
                     }
                     else
                     {
-                        schema->StructTypes = oldStructTypes;
+                        schema->StructTypes = NULL;
                     }
                 }
             }
@@ -1840,7 +1871,7 @@
         /* Codes_SRS_SCHEMA_99_135: [Schema_GetStructTypeName shall return the name of a struct type identified by the structTypeHandle argument.] */
         result = ((SCHEMA_STRUCT_TYPE_HANDLE_DATA*)structTypeHandle)->Name;
     }
-    
+
     return result;
 }
 
@@ -2011,15 +2042,22 @@
                 /* If possible, reduce the memory of over allocation */
                 if (result != SCHEMA_OK)
                 {
-                    SCHEMA_PROPERTY_HANDLE* oldProperties = (SCHEMA_PROPERTY_HANDLE*)realloc(structType->Properties, sizeof(SCHEMA_PROPERTY_HANDLE) * structType->PropertyCount);
-                    if (oldProperties == NULL)
+                    if (structType->PropertyCount > 0)
                     {
-                        result = SCHEMA_ERROR;
-                        LogError("(result = %s)", ENUM_TO_STRING(SCHEMA_RESULT, result));
+                        SCHEMA_PROPERTY_HANDLE *oldProperties = (SCHEMA_PROPERTY_HANDLE *)realloc(structType->Properties, sizeof(SCHEMA_PROPERTY_HANDLE) * structType->PropertyCount);
+                        if (oldProperties == NULL)
+                        {
+                            result = SCHEMA_ERROR;
+                            LogError("(result = %s)", ENUM_TO_STRING(SCHEMA_RESULT, result));
+                        }
+                        else
+                        {
+                            structType->Properties = oldProperties;
+                        }
                     }
                     else
                     {
-                        structType->Properties = oldProperties;
+                        structType->Properties = NULL;
                     }
                 }
             }
@@ -2235,7 +2273,7 @@
     else
     {
 
-        /* Codes_SRS_SCHEMA_99_126: [Schema_GetModelByIndex shall return a non-NULL SCHEMA_MODEL_TYPE_HANDLE corresponding to the model identified by schemaHandle and matching the index number provided by the index argument.] */ 
+        /* Codes_SRS_SCHEMA_99_126: [Schema_GetModelByIndex shall return a non-NULL SCHEMA_MODEL_TYPE_HANDLE corresponding to the model identified by schemaHandle and matching the index number provided by the index argument.] */
         /* Codes_SRS_SCHEMA_99_127: [The index argument is zero based, and the order in which models were added shall be the index in which they will be retrieved.] */
         result = schema->ModelTypes[index];
     }
@@ -2294,7 +2332,7 @@
         else
         {
             /*Codes_SRS_SCHEMA_99_164: [If the function succeeds, then the return value shall be SCHEMA_OK.]*/
-            
+
             result = SCHEMA_OK;
         }
     }
@@ -2459,7 +2497,7 @@
 SCHEMA_MODEL_TYPE_HANDLE Schema_GetModelModelyByIndex(SCHEMA_MODEL_TYPE_HANDLE modelTypeHandle, size_t index)
 {
     SCHEMA_MODEL_TYPE_HANDLE result;
-    if (modelTypeHandle == NULL) 
+    if (modelTypeHandle == NULL)
     {
         /*Codes_SRS_SCHEMA_99_173: [Schema_GetModelModelyByIndex shall return NULL in the cases when it cannot provide the handle.]*/
         result = NULL;