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

Committer:
AzureIoTClient
Date:
Tue Sep 11 11:14:37 2018 -0700
Revision:
36:7d12a5386197
Parent:
33:30999a4089f1
1.2.9

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 0:1f9b2707ec7d 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 0:1f9b2707ec7d 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 0:1f9b2707ec7d 3
AzureIoTClient 0:1f9b2707ec7d 4 #include <stdlib.h> /*for free*/
Azure.IoT Build 10:c2aee3965a83 5 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 0:1f9b2707ec7d 6
AzureIoTClient 0:1f9b2707ec7d 7 #include <stdbool.h>
AzureIoTClient 0:1f9b2707ec7d 8 #include "datamarshaller.h"
Azure.IoT Build 10:c2aee3965a83 9 #include "azure_c_shared_utility/crt_abstractions.h"
AzureIoTClient 0:1f9b2707ec7d 10 #include "schema.h"
AzureIoTClient 0:1f9b2707ec7d 11 #include "jsonencoder.h"
AzureIoTClient 0:1f9b2707ec7d 12 #include "agenttypesystem.h"
Azure.IoT Build 13:16e88f0cfa5f 13 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 17:fa1bba4c6053 14 #include "parson.h"
AzureIoTClient 17:fa1bba4c6053 15 #include "azure_c_shared_utility/vector.h"
AzureIoTClient 0:1f9b2707ec7d 16
AzureIoTClient 0:1f9b2707ec7d 17 DEFINE_ENUM_STRINGS(DATA_MARSHALLER_RESULT, DATA_MARSHALLER_RESULT_VALUES);
AzureIoTClient 0:1f9b2707ec7d 18
AzureIoTClient 0:1f9b2707ec7d 19 #define LOG_DATA_MARSHALLER_ERROR \
AzureIoTClient 11:b1327861f5e0 20 LogError("(result = %s)", ENUM_TO_STRING(DATA_MARSHALLER_RESULT, result));
AzureIoTClient 0:1f9b2707ec7d 21
AzureIoTClient 17:fa1bba4c6053 22 typedef struct DATA_MARSHALLER_HANDLE_DATA_TAG
AzureIoTClient 0:1f9b2707ec7d 23 {
AzureIoTClient 0:1f9b2707ec7d 24 SCHEMA_MODEL_TYPE_HANDLE ModelHandle;
AzureIoTClient 0:1f9b2707ec7d 25 bool IncludePropertyPath;
AzureIoTClient 17:fa1bba4c6053 26 } DATA_MARSHALLER_HANDLE_DATA;
AzureIoTClient 0:1f9b2707ec7d 27
AzureIoTClient 0:1f9b2707ec7d 28 static int NoCloneFunction(void** destination, const void* source)
AzureIoTClient 0:1f9b2707ec7d 29 {
AzureIoTClient 0:1f9b2707ec7d 30 *destination = (void*)source;
AzureIoTClient 0:1f9b2707ec7d 31 return 0;
AzureIoTClient 0:1f9b2707ec7d 32 }
AzureIoTClient 0:1f9b2707ec7d 33
AzureIoTClient 0:1f9b2707ec7d 34 static void NoFreeFunction(void* value)
AzureIoTClient 0:1f9b2707ec7d 35 {
AzureIoTClient 0:1f9b2707ec7d 36 (void)value;
AzureIoTClient 0:1f9b2707ec7d 37 }
AzureIoTClient 0:1f9b2707ec7d 38
AzureIoTClient 0:1f9b2707ec7d 39 DATA_MARSHALLER_HANDLE DataMarshaller_Create(SCHEMA_MODEL_TYPE_HANDLE modelHandle, bool includePropertyPath)
AzureIoTClient 0:1f9b2707ec7d 40 {
AzureIoTClient 17:fa1bba4c6053 41 DATA_MARSHALLER_HANDLE_DATA* result;
AzureIoTClient 0:1f9b2707ec7d 42
AzureIoTClient 0:1f9b2707ec7d 43 /*Codes_SRS_DATA_MARSHALLER_99_019:[ DataMarshaller_Create shall return NULL if any argument is NULL.]*/
AzureIoTClient 36:7d12a5386197 44 if (modelHandle == NULL)
AzureIoTClient 0:1f9b2707ec7d 45 {
AzureIoTClient 0:1f9b2707ec7d 46 result = NULL;
AzureIoTClient 11:b1327861f5e0 47 LogError("(result = %s)", ENUM_TO_STRING(DATA_MARSHALLER_RESULT, DATA_MARSHALLER_INVALID_ARG));
AzureIoTClient 0:1f9b2707ec7d 48 }
AzureIoTClient 17:fa1bba4c6053 49 else if ((result = (DATA_MARSHALLER_HANDLE_DATA*)malloc(sizeof(DATA_MARSHALLER_HANDLE_DATA))) == NULL)
AzureIoTClient 0:1f9b2707ec7d 50 {
AzureIoTClient 0:1f9b2707ec7d 51 /* Codes_SRS_DATA_MARSHALLER_99_048:[On any other errors not explicitly specified, DataMarshaller_Create shall return NULL.] */
AzureIoTClient 0:1f9b2707ec7d 52 result = NULL;
AzureIoTClient 11:b1327861f5e0 53 LogError("(result = %s)", ENUM_TO_STRING(DATA_MARSHALLER_RESULT, DATA_MARSHALLER_ERROR));
AzureIoTClient 0:1f9b2707ec7d 54 }
AzureIoTClient 0:1f9b2707ec7d 55 else
AzureIoTClient 0:1f9b2707ec7d 56 {
AzureIoTClient 0:1f9b2707ec7d 57 /*everything ok*/
AzureIoTClient 0:1f9b2707ec7d 58 /*Codes_SRS_DATA_MARSHALLER_99_018:[ DataMarshaller_Create shall create a new DataMarshaller instance and on success it shall return a non NULL handle.]*/
AzureIoTClient 17:fa1bba4c6053 59 result->ModelHandle = modelHandle;
AzureIoTClient 17:fa1bba4c6053 60 result->IncludePropertyPath = includePropertyPath;
AzureIoTClient 0:1f9b2707ec7d 61 }
AzureIoTClient 0:1f9b2707ec7d 62 return result;
AzureIoTClient 0:1f9b2707ec7d 63 }
AzureIoTClient 0:1f9b2707ec7d 64
AzureIoTClient 0:1f9b2707ec7d 65 void DataMarshaller_Destroy(DATA_MARSHALLER_HANDLE dataMarshallerHandle)
AzureIoTClient 0:1f9b2707ec7d 66 {
AzureIoTClient 0:1f9b2707ec7d 67 /* Codes_SRS_DATA_MARSHALLER_99_024:[ When called with a NULL handle, DataMarshaller_Destroy shall do nothing.] */
AzureIoTClient 0:1f9b2707ec7d 68 if (dataMarshallerHandle != NULL)
AzureIoTClient 0:1f9b2707ec7d 69 {
AzureIoTClient 0:1f9b2707ec7d 70 /* Codes_SRS_DATA_MARSHALLER_99_022:[ DataMarshaller_Destroy shall free all resources associated with the dataMarshallerHandle argument.] */
AzureIoTClient 17:fa1bba4c6053 71 DATA_MARSHALLER_HANDLE_DATA* dataMarshallerInstance = (DATA_MARSHALLER_HANDLE_DATA*)dataMarshallerHandle;
AzureIoTClient 0:1f9b2707ec7d 72 free(dataMarshallerInstance);
AzureIoTClient 0:1f9b2707ec7d 73 }
AzureIoTClient 0:1f9b2707ec7d 74 }
AzureIoTClient 0:1f9b2707ec7d 75
AzureIoTClient 0:1f9b2707ec7d 76 DATA_MARSHALLER_RESULT DataMarshaller_SendData(DATA_MARSHALLER_HANDLE dataMarshallerHandle, size_t valueCount, const DATA_MARSHALLER_VALUE* values, unsigned char** destination, size_t* destinationSize)
AzureIoTClient 0:1f9b2707ec7d 77 {
AzureIoTClient 17:fa1bba4c6053 78 DATA_MARSHALLER_HANDLE_DATA* dataMarshallerInstance = (DATA_MARSHALLER_HANDLE_DATA*)dataMarshallerHandle;
AzureIoTClient 0:1f9b2707ec7d 79 DATA_MARSHALLER_RESULT result;
AzureIoTClient 0:1f9b2707ec7d 80 MULTITREE_HANDLE treeHandle;
AzureIoTClient 0:1f9b2707ec7d 81
AzureIoTClient 0:1f9b2707ec7d 82 /* Codes_SRS_DATA_MARSHALLER_99_034:[All argument checks shall be performed before calling any other modules.] */
AzureIoTClient 0:1f9b2707ec7d 83 /* Codes_SRS_DATA_MARSHALLER_99_004:[ DATA_MARSHALLER_INVALID_ARG shall be returned when the function has detected an invalid parameter (NULL) being passed to the function.] */
AzureIoTClient 0:1f9b2707ec7d 84 if ((values == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 85 (dataMarshallerHandle == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 86 (destination == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 87 (destinationSize == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 88 /* Codes_SRS_DATA_MARSHALLER_99_033:[ DATA_MARSHALLER_INVALID_ARG shall be returned if the valueCount is zero.] */
AzureIoTClient 0:1f9b2707ec7d 89 (valueCount == 0))
AzureIoTClient 0:1f9b2707ec7d 90 {
AzureIoTClient 0:1f9b2707ec7d 91 result = DATA_MARSHALLER_INVALID_ARG;
AzureIoTClient 0:1f9b2707ec7d 92 LOG_DATA_MARSHALLER_ERROR
AzureIoTClient 0:1f9b2707ec7d 93 }
AzureIoTClient 0:1f9b2707ec7d 94 else
AzureIoTClient 0:1f9b2707ec7d 95 {
AzureIoTClient 0:1f9b2707ec7d 96 size_t i;
AzureIoTClient 17:fa1bba4c6053 97 bool includePropertyPath = dataMarshallerInstance->IncludePropertyPath;
AzureIoTClient 0:1f9b2707ec7d 98 /* VS complains wrongly that result is not initialized */
AzureIoTClient 0:1f9b2707ec7d 99 result = DATA_MARSHALLER_ERROR;
AzureIoTClient 0:1f9b2707ec7d 100
AzureIoTClient 0:1f9b2707ec7d 101 for (i = 0; i < valueCount; i++)
AzureIoTClient 0:1f9b2707ec7d 102 {
AzureIoTClient 0:1f9b2707ec7d 103 if ((values[i].PropertyPath == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 104 (values[i].Value == NULL))
AzureIoTClient 0:1f9b2707ec7d 105 {
AzureIoTClient 0:1f9b2707ec7d 106 /*Codes_SRS_DATA_MARSHALLER_99_007:[ DATA_MARSHALLER_INVALID_MODEL_PROPERTY shall be returned when any of the items in values contain invalid data]*/
AzureIoTClient 0:1f9b2707ec7d 107 result = DATA_MARSHALLER_INVALID_MODEL_PROPERTY;
AzureIoTClient 0:1f9b2707ec7d 108 LOG_DATA_MARSHALLER_ERROR
AzureIoTClient 0:1f9b2707ec7d 109 break;
AzureIoTClient 0:1f9b2707ec7d 110 }
AzureIoTClient 0:1f9b2707ec7d 111
AzureIoTClient 0:1f9b2707ec7d 112 if ((!dataMarshallerInstance->IncludePropertyPath) &&
AzureIoTClient 0:1f9b2707ec7d 113 (values[i].Value->type == EDM_COMPLEX_TYPE_TYPE) &&
AzureIoTClient 0:1f9b2707ec7d 114 (valueCount > 1))
AzureIoTClient 0:1f9b2707ec7d 115 {
AzureIoTClient 0:1f9b2707ec7d 116 /* Codes_SRS_DATAMARSHALLER_01_002: [If the includePropertyPath argument passed to DataMarshaller_Create was false and the number of values passed to SendData is greater than 1 and at least one of them is a struct, DataMarshaller_SendData shall fallback to including the complete property path in the output JSON.] */
AzureIoTClient 0:1f9b2707ec7d 117 includePropertyPath = true;
AzureIoTClient 0:1f9b2707ec7d 118 }
AzureIoTClient 0:1f9b2707ec7d 119 }
AzureIoTClient 0:1f9b2707ec7d 120
AzureIoTClient 0:1f9b2707ec7d 121 if (i == valueCount)
AzureIoTClient 0:1f9b2707ec7d 122 {
AzureIoTClient 0:1f9b2707ec7d 123 /* Codes_SRS_DATA_MARSHALLER_99_037:[DataMarshaller shall store as MultiTree the data to be encoded by the JSONEncoder module.] */
AzureIoTClient 0:1f9b2707ec7d 124 if ((treeHandle = MultiTree_Create(NoCloneFunction, NoFreeFunction)) == NULL)
AzureIoTClient 0:1f9b2707ec7d 125 {
AzureIoTClient 0:1f9b2707ec7d 126 /* Codes_SRS_DATA_MARSHALLER_99_035:[DATA_MARSHALLER_MULTITREE_ERROR shall be returned in case any MultiTree API call fails.] */
AzureIoTClient 0:1f9b2707ec7d 127 result = DATA_MARSHALLER_MULTITREE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 128 LOG_DATA_MARSHALLER_ERROR
AzureIoTClient 0:1f9b2707ec7d 129 }
AzureIoTClient 0:1f9b2707ec7d 130 else
AzureIoTClient 0:1f9b2707ec7d 131 {
AzureIoTClient 0:1f9b2707ec7d 132 size_t j;
AzureIoTClient 0:1f9b2707ec7d 133 result = DATA_MARSHALLER_OK; /* addressing warning in VS compiler */
AzureIoTClient 0:1f9b2707ec7d 134 /* Codes_SRS_DATA_MARSHALLER_99_038:[For each pair in the values argument, a string : value pair shall exist in the JSON object in the form of propertyName : value.] */
AzureIoTClient 0:1f9b2707ec7d 135 for (j = 0; j < valueCount; j++)
AzureIoTClient 0:1f9b2707ec7d 136 {
AzureIoTClient 0:1f9b2707ec7d 137 if ((includePropertyPath == false) && (values[j].Value->type == EDM_COMPLEX_TYPE_TYPE))
AzureIoTClient 0:1f9b2707ec7d 138 {
AzureIoTClient 0:1f9b2707ec7d 139 size_t k;
AzureIoTClient 0:1f9b2707ec7d 140
AzureIoTClient 0:1f9b2707ec7d 141 /* Codes_SRS_DATAMARSHALLER_01_001: [If the includePropertyPath argument passed to DataMarshaller_Create was false and only one struct is being sent, the relative path of the value passed to DataMarshaller_SendData - including property name - shall be ignored and the value shall be placed at JSON root.] */
AzureIoTClient 0:1f9b2707ec7d 142 for (k = 0; k < values[j].Value->value.edmComplexType.nMembers; k++)
AzureIoTClient 0:1f9b2707ec7d 143 {
AzureIoTClient 0:1f9b2707ec7d 144 /* Codes_SRS_DATAMARSHALLER_01_004: [In this case the members of the struct shall be added as leafs into the MultiTree, each leaf having the name of the struct member.] */
AzureIoTClient 0:1f9b2707ec7d 145 if (MultiTree_AddLeaf(treeHandle, values[j].Value->value.edmComplexType.fields[k].fieldName, (void*)values[j].Value->value.edmComplexType.fields[k].value) != MULTITREE_OK)
AzureIoTClient 0:1f9b2707ec7d 146 {
AzureIoTClient 0:1f9b2707ec7d 147 break;
AzureIoTClient 0:1f9b2707ec7d 148 }
AzureIoTClient 0:1f9b2707ec7d 149 }
AzureIoTClient 0:1f9b2707ec7d 150
AzureIoTClient 0:1f9b2707ec7d 151 if (k < values[j].Value->value.edmComplexType.nMembers)
AzureIoTClient 0:1f9b2707ec7d 152 {
AzureIoTClient 0:1f9b2707ec7d 153 /* Codes_SRS_DATA_MARSHALLER_99_035:[DATA_MARSHALLER_MULTITREE_ERROR shall be returned in case any MultiTree API call fails.] */
AzureIoTClient 0:1f9b2707ec7d 154 result = DATA_MARSHALLER_MULTITREE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 155 LOG_DATA_MARSHALLER_ERROR
AzureIoTClient 0:1f9b2707ec7d 156 break;
AzureIoTClient 0:1f9b2707ec7d 157 }
AzureIoTClient 0:1f9b2707ec7d 158 }
AzureIoTClient 0:1f9b2707ec7d 159 else
AzureIoTClient 0:1f9b2707ec7d 160 {
AzureIoTClient 0:1f9b2707ec7d 161 /* Codes_SRS_DATA_MARSHALLER_99_039:[ If the includePropertyPath argument passed to DataMarshaller_Create was true each property shall be placed in the appropriate position in the JSON according to its path in the model.] */
AzureIoTClient 0:1f9b2707ec7d 162 if (MultiTree_AddLeaf(treeHandle, values[j].PropertyPath, (void*)values[j].Value) != MULTITREE_OK)
AzureIoTClient 0:1f9b2707ec7d 163 {
AzureIoTClient 0:1f9b2707ec7d 164 /* Codes_SRS_DATA_MARSHALLER_99_035:[DATA_MARSHALLER_MULTITREE_ERROR shall be returned in case any MultiTree API call fails.] */
AzureIoTClient 0:1f9b2707ec7d 165 result = DATA_MARSHALLER_MULTITREE_ERROR;
AzureIoTClient 0:1f9b2707ec7d 166 LOG_DATA_MARSHALLER_ERROR
AzureIoTClient 0:1f9b2707ec7d 167 break;
AzureIoTClient 0:1f9b2707ec7d 168 }
AzureIoTClient 0:1f9b2707ec7d 169 }
AzureIoTClient 0:1f9b2707ec7d 170
AzureIoTClient 0:1f9b2707ec7d 171 }
AzureIoTClient 0:1f9b2707ec7d 172
AzureIoTClient 0:1f9b2707ec7d 173 if (j == valueCount)
AzureIoTClient 0:1f9b2707ec7d 174 {
AzureIoTClient 0:1f9b2707ec7d 175 STRING_HANDLE payload = STRING_new();
AzureIoTClient 0:1f9b2707ec7d 176 if (payload == NULL)
AzureIoTClient 0:1f9b2707ec7d 177 {
AzureIoTClient 0:1f9b2707ec7d 178 result = DATA_MARSHALLER_ERROR;
AzureIoTClient 0:1f9b2707ec7d 179 LOG_DATA_MARSHALLER_ERROR
AzureIoTClient 0:1f9b2707ec7d 180 }
AzureIoTClient 0:1f9b2707ec7d 181 else
AzureIoTClient 0:1f9b2707ec7d 182 {
AzureIoTClient 0:1f9b2707ec7d 183 if (JSONEncoder_EncodeTree(treeHandle, payload, (JSON_ENCODER_TOSTRING_FUNC)AgentDataTypes_ToString) != JSON_ENCODER_OK)
AzureIoTClient 0:1f9b2707ec7d 184 {
AzureIoTClient 0:1f9b2707ec7d 185 /* Codes_SRS_DATA_MARSHALLER_99_027:[ DATA_MARSHALLER_JSON_ENCODER_ERROR shall be returned when JSONEncoder returns an error code.] */
AzureIoTClient 0:1f9b2707ec7d 186 result = DATA_MARSHALLER_JSON_ENCODER_ERROR;
AzureIoTClient 0:1f9b2707ec7d 187 LOG_DATA_MARSHALLER_ERROR
AzureIoTClient 0:1f9b2707ec7d 188 }
AzureIoTClient 0:1f9b2707ec7d 189 else
AzureIoTClient 0:1f9b2707ec7d 190 {
AzureIoTClient 0:1f9b2707ec7d 191 /*Codes_SRS_DATAMARSHALLER_02_007: [DataMarshaller_SendData shall copy in the output parameters *destination, *destinationSize the content and the content length of the encoded JSON tree.] */
AzureIoTClient 0:1f9b2707ec7d 192 size_t resultSize = STRING_length(payload);
AzureIoTClient 0:1f9b2707ec7d 193 unsigned char* temp = malloc(resultSize);
AzureIoTClient 0:1f9b2707ec7d 194 if (temp == NULL)
AzureIoTClient 0:1f9b2707ec7d 195 {
AzureIoTClient 0:1f9b2707ec7d 196 /*Codes_SRS_DATA_MARSHALLER_99_015:[ DATA_MARSHALLER_ERROR shall be returned in all the other error cases not explicitly defined here.]*/
AzureIoTClient 0:1f9b2707ec7d 197 result = DATA_MARSHALLER_ERROR;
AzureIoTClient 0:1f9b2707ec7d 198 LOG_DATA_MARSHALLER_ERROR;
AzureIoTClient 0:1f9b2707ec7d 199 }
AzureIoTClient 0:1f9b2707ec7d 200 else
AzureIoTClient 0:1f9b2707ec7d 201 {
AzureIoTClient 22:422d94bd3c18 202 (void)memcpy(temp, STRING_c_str(payload), resultSize);
AzureIoTClient 0:1f9b2707ec7d 203 *destination = temp;
AzureIoTClient 0:1f9b2707ec7d 204 *destinationSize = resultSize;
AzureIoTClient 0:1f9b2707ec7d 205 result = DATA_MARSHALLER_OK;
AzureIoTClient 0:1f9b2707ec7d 206 }
AzureIoTClient 0:1f9b2707ec7d 207 }
AzureIoTClient 0:1f9b2707ec7d 208 STRING_delete(payload);
AzureIoTClient 0:1f9b2707ec7d 209 }
AzureIoTClient 0:1f9b2707ec7d 210 } /* if (j==valueCount)*/
AzureIoTClient 0:1f9b2707ec7d 211 MultiTree_Destroy(treeHandle);
AzureIoTClient 0:1f9b2707ec7d 212 } /* MultiTree_Create */
AzureIoTClient 0:1f9b2707ec7d 213 }
AzureIoTClient 0:1f9b2707ec7d 214 }
AzureIoTClient 0:1f9b2707ec7d 215
AzureIoTClient 0:1f9b2707ec7d 216 return result;
AzureIoTClient 0:1f9b2707ec7d 217 }
AzureIoTClient 17:fa1bba4c6053 218
AzureIoTClient 17:fa1bba4c6053 219
AzureIoTClient 17:fa1bba4c6053 220 DATA_MARSHALLER_RESULT DataMarshaller_SendData_ReportedProperties(DATA_MARSHALLER_HANDLE dataMarshallerHandle, VECTOR_HANDLE values, unsigned char** destination, size_t* destinationSize)
AzureIoTClient 17:fa1bba4c6053 221 {
AzureIoTClient 17:fa1bba4c6053 222 DATA_MARSHALLER_RESULT result;
AzureIoTClient 17:fa1bba4c6053 223 /*Codes_SRS_DATA_MARSHALLER_02_021: [ If argument dataMarshallerHandle is NULL then DataMarshaller_SendData_ReportedProperties shall fail and return DATA_MARSHALLER_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 224 /*Codes_SRS_DATA_MARSHALLER_02_008: [ If argument values is NULL then DataMarshaller_SendData_ReportedProperties shall fail and return DATA_MARSHALLER_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 225 /*Codes_SRS_DATA_MARSHALLER_02_009: [ If argument destination NULL then DataMarshaller_SendData_ReportedProperties shall fail and return DATA_MARSHALLER_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 226 /*Codes_SRS_DATA_MARSHALLER_02_010: [ If argument destinationSize NULL then DataMarshaller_SendData_ReportedProperties shall fail and return DATA_MARSHALLER_INVALID_ARG. ]*/
AzureIoTClient 17:fa1bba4c6053 227 if (
AzureIoTClient 17:fa1bba4c6053 228 (dataMarshallerHandle == NULL) ||
AzureIoTClient 17:fa1bba4c6053 229 (values == NULL) ||
AzureIoTClient 17:fa1bba4c6053 230 (destination == NULL) ||
AzureIoTClient 17:fa1bba4c6053 231 (destinationSize == NULL)
AzureIoTClient 17:fa1bba4c6053 232 )
AzureIoTClient 17:fa1bba4c6053 233 {
AzureIoTClient 17:fa1bba4c6053 234 LogError("invalid argument DATA_MARSHALLER_HANDLE dataMarshallerHandle=%p, VECTOR_HANDLE values=%p, unsigned char** destination=%p, size_t* destinationSize=%p",
AzureIoTClient 17:fa1bba4c6053 235 dataMarshallerHandle,
AzureIoTClient 17:fa1bba4c6053 236 values,
AzureIoTClient 17:fa1bba4c6053 237 destination,
AzureIoTClient 17:fa1bba4c6053 238 destinationSize);
AzureIoTClient 17:fa1bba4c6053 239 result = DATA_MARSHALLER_INVALID_ARG;
AzureIoTClient 17:fa1bba4c6053 240 }
AzureIoTClient 17:fa1bba4c6053 241 else
AzureIoTClient 17:fa1bba4c6053 242 {
AzureIoTClient 17:fa1bba4c6053 243 /*Codes_SRS_DATA_MARSHALLER_02_012: [ DataMarshaller_SendData_ReportedProperties shall create an empty JSON_Value. ]*/
AzureIoTClient 17:fa1bba4c6053 244 JSON_Value* json = json_value_init_object();
AzureIoTClient 17:fa1bba4c6053 245 if (json == NULL)
AzureIoTClient 17:fa1bba4c6053 246 {
AzureIoTClient 17:fa1bba4c6053 247 /*Codes_SRS_DATA_MARSHALLER_02_019: [ If any failure occurs, DataMarshaller_SendData_ReportedProperties shall fail and return DATA_MARSHALLER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 248 LogError("failure calling json_value_init_object");
AzureIoTClient 17:fa1bba4c6053 249 result = DATA_MARSHALLER_ERROR;
AzureIoTClient 17:fa1bba4c6053 250 }
AzureIoTClient 17:fa1bba4c6053 251 else
AzureIoTClient 17:fa1bba4c6053 252 {
AzureIoTClient 17:fa1bba4c6053 253 /*Codes_SRS_DATA_MARSHALLER_02_013: [ DataMarshaller_SendData_ReportedProperties shall get the object behind the JSON_Value by calling json_object. ]*/
AzureIoTClient 17:fa1bba4c6053 254 JSON_Object* jsonObject = json_object(json);
AzureIoTClient 17:fa1bba4c6053 255 if (jsonObject == NULL)
AzureIoTClient 17:fa1bba4c6053 256 {
AzureIoTClient 17:fa1bba4c6053 257 /*Codes_SRS_DATA_MARSHALLER_02_019: [ If any failure occurs, DataMarshaller_SendData_ReportedProperties shall fail and return DATA_MARSHALLER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 258 LogError("failure calling json_object");
AzureIoTClient 17:fa1bba4c6053 259 result = DATA_MARSHALLER_ERROR;
AzureIoTClient 17:fa1bba4c6053 260 }
AzureIoTClient 17:fa1bba4c6053 261 else
AzureIoTClient 17:fa1bba4c6053 262 {
AzureIoTClient 17:fa1bba4c6053 263 size_t nReportedProperties = VECTOR_size(values), nProcessedProperties = 0;
AzureIoTClient 17:fa1bba4c6053 264
AzureIoTClient 17:fa1bba4c6053 265 for (size_t i = 0;i < nReportedProperties; i++)
AzureIoTClient 17:fa1bba4c6053 266 {
AzureIoTClient 17:fa1bba4c6053 267 DATA_MARSHALLER_VALUE* v = *(DATA_MARSHALLER_VALUE**)VECTOR_element(values, i);
AzureIoTClient 17:fa1bba4c6053 268 STRING_HANDLE s = STRING_new();
AzureIoTClient 17:fa1bba4c6053 269 if (s == NULL)
AzureIoTClient 17:fa1bba4c6053 270 {
AzureIoTClient 17:fa1bba4c6053 271 /*Codes_SRS_DATA_MARSHALLER_02_019: [ If any failure occurs, DataMarshaller_SendData_ReportedProperties shall fail and return DATA_MARSHALLER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 272 LogError("failure calling STRING_new");
AzureIoTClient 17:fa1bba4c6053 273 i = nReportedProperties;/*forces loop to break, result is set in the "if" following this for*/
AzureIoTClient 17:fa1bba4c6053 274 }
AzureIoTClient 17:fa1bba4c6053 275 else
AzureIoTClient 17:fa1bba4c6053 276 {
AzureIoTClient 17:fa1bba4c6053 277 /*Codes_SRS_DATA_MARSHALLER_02_014: [ For every reported property, DataMarshaller_SendData_ReportedProperties shall get the reported property's JSON value (as string) by calling AgentDataTypes_ToString. ]*/
AzureIoTClient 17:fa1bba4c6053 278 if (AgentDataTypes_ToString(s, v->Value) != AGENT_DATA_TYPES_OK)
AzureIoTClient 17:fa1bba4c6053 279 {
AzureIoTClient 17:fa1bba4c6053 280 /*Codes_SRS_DATA_MARSHALLER_02_019: [ If any failure occurs, DataMarshaller_SendData_ReportedProperties shall fail and return DATA_MARSHALLER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 281 LogError("failure calling AgentDataTypes_ToString");
AzureIoTClient 17:fa1bba4c6053 282 i = nReportedProperties;/*forces loop to break, result is set in the "if" following this for*/
AzureIoTClient 17:fa1bba4c6053 283 }
AzureIoTClient 17:fa1bba4c6053 284 else
AzureIoTClient 17:fa1bba4c6053 285 {
AzureIoTClient 17:fa1bba4c6053 286 /*Codes_SRS_DATA_MARSHALLER_02_015: [ DataMarshaller_SendData_ReportedProperties shall import the JSON value (as string) by calling json_parse_string. ]*/
AzureIoTClient 17:fa1bba4c6053 287 JSON_Value * rightSide = json_parse_string(STRING_c_str(s));
AzureIoTClient 17:fa1bba4c6053 288 if (rightSide == NULL)
AzureIoTClient 17:fa1bba4c6053 289 {
AzureIoTClient 17:fa1bba4c6053 290 /*Codes_SRS_DATA_MARSHALLER_02_019: [ If any failure occurs, DataMarshaller_SendData_ReportedProperties shall fail and return DATA_MARSHALLER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 291 LogError("failure calling json_parse_string");
AzureIoTClient 17:fa1bba4c6053 292 i = nReportedProperties;/*forces loop to break, result is set in the "if" following this for*/
AzureIoTClient 17:fa1bba4c6053 293 }
AzureIoTClient 17:fa1bba4c6053 294 else
AzureIoTClient 17:fa1bba4c6053 295 {
AzureIoTClient 17:fa1bba4c6053 296 char* leftSide;
AzureIoTClient 17:fa1bba4c6053 297 if (mallocAndStrcpy_s(&leftSide, v->PropertyPath) != 0)
AzureIoTClient 17:fa1bba4c6053 298 {
AzureIoTClient 17:fa1bba4c6053 299 /*Codes_SRS_DATA_MARSHALLER_02_019: [ If any failure occurs, DataMarshaller_SendData_ReportedProperties shall fail and return DATA_MARSHALLER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 300 LogError("failure calling mallocAndStrcpy_s");
AzureIoTClient 17:fa1bba4c6053 301 json_value_free(rightSide);
AzureIoTClient 17:fa1bba4c6053 302 i = nReportedProperties;/*forces loop to break, result is set in the "if" following this for*/
AzureIoTClient 17:fa1bba4c6053 303 }
AzureIoTClient 17:fa1bba4c6053 304 else
AzureIoTClient 17:fa1bba4c6053 305 {
AzureIoTClient 17:fa1bba4c6053 306 /*Codes_SRS_DATA_MARSHALLER_02_016: [ DataMarshaller_SendData_ReportedProperties shall replace all the occurences of / with . in the reported property paths. ]*/
AzureIoTClient 17:fa1bba4c6053 307 char *whereIsSlash;
AzureIoTClient 17:fa1bba4c6053 308 while ((whereIsSlash = strchr(leftSide, '/')) != NULL)
AzureIoTClient 17:fa1bba4c6053 309 {
AzureIoTClient 17:fa1bba4c6053 310 *whereIsSlash = '.';
AzureIoTClient 17:fa1bba4c6053 311 }
AzureIoTClient 17:fa1bba4c6053 312
AzureIoTClient 17:fa1bba4c6053 313 /*Codes_SRS_DATA_MARSHALLER_02_017: [ DataMarshaller_SendData_ReportedProperties shall use json_object_dotset_value passing the reported property path and the imported json value. ]*/
AzureIoTClient 17:fa1bba4c6053 314 /*Codes_SRS_DATA_MARSHALLER_02_011: [ DataMarshaller_SendData_ReportedProperties shall ignore the value of includePropertyPath and shall consider it to be true. ]*/
AzureIoTClient 17:fa1bba4c6053 315 if (json_object_dotset_value(jsonObject, leftSide, rightSide) != JSONSuccess)
AzureIoTClient 17:fa1bba4c6053 316 {
AzureIoTClient 17:fa1bba4c6053 317 /*Codes_SRS_DATA_MARSHALLER_02_019: [ If any failure occurs, DataMarshaller_SendData_ReportedProperties shall fail and return DATA_MARSHALLER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 318 LogError("failure calling json_object_dotset_value");
AzureIoTClient 17:fa1bba4c6053 319 json_value_free(rightSide);
AzureIoTClient 17:fa1bba4c6053 320 i = nReportedProperties;/*forces loop to break, result is set in the "if" following this for*/
AzureIoTClient 17:fa1bba4c6053 321 }
AzureIoTClient 17:fa1bba4c6053 322 else
AzureIoTClient 17:fa1bba4c6053 323 {
AzureIoTClient 17:fa1bba4c6053 324 /*all is fine with this property... */
AzureIoTClient 17:fa1bba4c6053 325 nProcessedProperties++;
AzureIoTClient 17:fa1bba4c6053 326 }
AzureIoTClient 17:fa1bba4c6053 327 free(leftSide);
AzureIoTClient 17:fa1bba4c6053 328 }
AzureIoTClient 17:fa1bba4c6053 329 }
AzureIoTClient 17:fa1bba4c6053 330 }
AzureIoTClient 17:fa1bba4c6053 331 STRING_delete(s);
AzureIoTClient 17:fa1bba4c6053 332 }
AzureIoTClient 17:fa1bba4c6053 333 }
AzureIoTClient 17:fa1bba4c6053 334
AzureIoTClient 17:fa1bba4c6053 335 if (nProcessedProperties != nReportedProperties)
AzureIoTClient 17:fa1bba4c6053 336 {
AzureIoTClient 17:fa1bba4c6053 337 result = DATA_MARSHALLER_ERROR;
AzureIoTClient 17:fa1bba4c6053 338 /*all properties have NOT been processed*/
AzureIoTClient 17:fa1bba4c6053 339 /*return result as is*/
AzureIoTClient 17:fa1bba4c6053 340 }
AzureIoTClient 17:fa1bba4c6053 341 else
AzureIoTClient 17:fa1bba4c6053 342 {
AzureIoTClient 17:fa1bba4c6053 343 /*Codes_SRS_DATA_MARSHALLER_02_018: [ DataMarshaller_SendData_ReportedProperties shall use json_serialize_to_string_pretty to produce the output JSON string that fills out parameters destination and destionationSize. ]*/
AzureIoTClient 17:fa1bba4c6053 344 char* temp = json_serialize_to_string_pretty(json);
AzureIoTClient 17:fa1bba4c6053 345 if (temp == NULL)
AzureIoTClient 17:fa1bba4c6053 346 {
AzureIoTClient 17:fa1bba4c6053 347 /*Codes_SRS_DATA_MARSHALLER_02_019: [ If any failure occurs, DataMarshaller_SendData_ReportedProperties shall fail and return DATA_MARSHALLER_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 348 LogError("failure calling json_serialize_to_string_pretty ");
AzureIoTClient 17:fa1bba4c6053 349 result = DATA_MARSHALLER_ERROR;
AzureIoTClient 17:fa1bba4c6053 350 }
AzureIoTClient 17:fa1bba4c6053 351 else
AzureIoTClient 17:fa1bba4c6053 352 {
AzureIoTClient 17:fa1bba4c6053 353 /*Codes_SRS_DATA_MARSHALLER_02_020: [ Otherwise DataMarshaller_SendData_ReportedProperties shall succeed and return DATA_MARSHALLER_OK. ]*/
AzureIoTClient 17:fa1bba4c6053 354 *destination = (unsigned char*)temp;
AzureIoTClient 17:fa1bba4c6053 355 *destinationSize = strlen(temp);
AzureIoTClient 17:fa1bba4c6053 356 result = DATA_MARSHALLER_OK;
AzureIoTClient 17:fa1bba4c6053 357 /*all is fine... */
AzureIoTClient 17:fa1bba4c6053 358 }
AzureIoTClient 17:fa1bba4c6053 359 }
AzureIoTClient 17:fa1bba4c6053 360 }
AzureIoTClient 17:fa1bba4c6053 361 json_value_free(json);
AzureIoTClient 17:fa1bba4c6053 362 }
AzureIoTClient 17:fa1bba4c6053 363 }
AzureIoTClient 17:fa1bba4c6053 364 return result;
AzureIoTClient 17:fa1bba4c6053 365 }