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:
Wed Nov 16 21:38:26 2016 -0800
Revision:
17:fa1bba4c6053
Parent:
13:16e88f0cfa5f
Child:
21:6d3dea1abd9c
1.0.10

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