source: azure_iot_hub_f767zi/trunk/azure_iot_sdk/serializer/src/datamarshaller.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

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