1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // Defining _POSIX_C_SOURCE macro with 200112L (or greater) as value
22 // causes header files to expose definitions
23 // corresponding to the POSIX.1-2001 base
24 // specification (excluding the XSI extension).
25 // For POSIX.1-2001 base specification,
26 // Refer http://pubs.opengroup.org/onlinepubs/009695399/
27 // Required for strok_r
28 #define _POSIX_C_SOURCE 200112L
30 #include "ocpayloadcbor.h"
33 #include "oic_string.h"
34 #include "oic_malloc.h"
35 #include "ocstackinternal.h"
36 #include "ocpayload.h"
38 #include "oic_string.h"
39 #include "payload_logging.h"
40 #include "rdpayload.h"
42 #define TAG "OCPayloadParse"
44 static OCStackResult OCParseDiscoveryPayload(OCPayload** outPayload, CborValue* arrayVal);
45 static OCStackResult OCParseDevicePayload(OCPayload** outPayload, CborValue* arrayVal);
46 static OCStackResult OCParsePlatformPayload(OCPayload** outPayload, CborValue* arrayVal);
47 static bool OCParseSingleRepPayload(OCRepPayload** outPayload, CborValue* repParent);
48 static OCStackResult OCParseRepPayload(OCPayload** outPayload, CborValue* arrayVal);
49 static OCStackResult OCParsePresencePayload(OCPayload** outPayload, CborValue* arrayVal);
50 static OCStackResult OCParseSecurityPayload(OCPayload** outPayload, CborValue* arrayVal);
52 OCStackResult OCParsePayload(OCPayload** outPayload, OCPayloadType payloadType,
53 const uint8_t* payload, size_t payloadSize)
59 OC_LOG_V(INFO, TAG, "CBOR Parsing size: %zu", payloadSize);
60 if((err = cbor_parser_init(payload, payloadSize, 0, &parser, &rootValue)) != false)
62 OC_LOG_V(ERROR, TAG, "CBOR Parser init failed: %d", err);
63 return OC_STACK_ERROR;
66 OCStackResult result = OC_STACK_ERROR;
69 case PAYLOAD_TYPE_DISCOVERY:
70 result = OCParseDiscoveryPayload(outPayload, &rootValue);
72 case PAYLOAD_TYPE_DEVICE:
73 result = OCParseDevicePayload(outPayload, &rootValue);
75 case PAYLOAD_TYPE_PLATFORM:
76 result = OCParsePlatformPayload(outPayload, &rootValue);
78 case PAYLOAD_TYPE_REPRESENTATION:
79 result = OCParseRepPayload(outPayload, &rootValue);
81 case PAYLOAD_TYPE_PRESENCE:
82 result = OCParsePresencePayload(outPayload, &rootValue);
84 case PAYLOAD_TYPE_SECURITY:
85 result = OCParseSecurityPayload(outPayload, &rootValue);
88 result = OCRDCborToPayload(&rootValue, outPayload);
91 OC_LOG_V(ERROR, TAG, "ParsePayload Type default: %d", payloadType);
92 result = OC_STACK_ERROR;
96 if(result != OC_STACK_OK)
98 OC_LOG_V(INFO, TAG, "Finished parse payload, result is %d", result);
104 void OCFreeOCStringLL(OCStringLL* ll);
106 static OCStackResult OCParseSecurityPayload(OCPayload** outPayload, CborValue* rootValue)
110 return OC_STACK_INVALID_PARAM;
114 char * securityData = NULL;
116 err = err || cbor_value_enter_container(rootValue, &map);
118 if(cbor_value_is_map(&map))
121 err = err || cbor_value_dup_text_string(&map, &securityData, &len, NULL);
125 OC_LOG(ERROR, TAG, "Cbor main value not a map");
126 return OC_STACK_MALFORMED_RESPONSE;
129 err = err || cbor_value_leave_container(rootValue, &map);
133 OC_LOG(ERROR, TAG, "Cbor in error condition");
134 OICFree(securityData);
135 return OC_STACK_MALFORMED_RESPONSE;
138 *outPayload = (OCPayload*)OCSecurityPayloadCreate(securityData);
139 OICFree(securityData);
145 static char* InPlaceStringTrim(char* str)
147 while (str[0] == ' ')
152 size_t lastchar = strlen(str);
154 while (str[lastchar] == ' ')
156 str[lastchar] = '\0';
163 static OCStackResult OCParseDiscoveryPayload(OCPayload** outPayload, CborValue* rootValue)
167 return OC_STACK_INVALID_PARAM;
171 OCResourcePayload* resource = NULL;
173 OCDiscoveryPayload* out = OCDiscoveryPayloadCreate();
176 return OC_STACK_NO_MEMORY;
180 err = err || cbor_value_enter_container(rootValue, &arrayVal);
182 if (cbor_value_is_array(&arrayVal))
184 OCLinksPayload *linksPayload = NULL;
185 OCTagsPayload *tagsPayload = NULL;
186 while (cbor_value_is_container(&arrayVal))
190 CborValue colResources;
191 CborError cborFindResult = cbor_value_enter_container(&arrayVal, &colResources);
192 if (CborNoError != cborFindResult)
197 if (OC_STACK_OK != OCTagsCborToPayload(&colResources, &tagsPayload))
199 OC_LOG(ERROR, TAG, "Tags cbor parsing failed.");
200 OCFreeTagsResource(tagsPayload);
204 if (OC_STACK_OK != OCLinksCborToPayload(&colResources, &linksPayload))
206 OC_LOG(ERROR, TAG, "Links cbor parsing failed.");
207 OCFreeTagsResource(tagsPayload);
208 OCFreeLinksResource(linksPayload);
212 if (OC_STACK_OK != OCDiscoveryCollectionPayloadAddResource(out, tagsPayload, linksPayload))
214 OC_LOG(ERROR, TAG, "Memory allocation failed");
215 OCFreeLinksResource(linksPayload);
216 OCFreeTagsResource(tagsPayload);
217 OCDiscoveryPayloadDestroy(out);
218 return OC_STACK_NO_MEMORY;
220 if (CborNoError != cbor_value_advance(&arrayVal))
222 OC_LOG(ERROR, TAG, "Cbor value advanced failed.");
227 if (cbor_value_is_map(&arrayVal))
229 size_t resourceCount = 0;
230 while (cbor_value_is_map(&arrayVal))
232 resource = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
235 OC_LOG(ERROR, TAG, "Memory allocation failed");
236 OCDiscoveryPayloadDestroy(out);
237 return OC_STACK_NO_MEMORY;
241 err = cbor_value_map_find_value(&arrayVal, OC_RSRVD_DEVICE_ID, &curVal);
242 if (CborNoError != err)
244 OC_LOG(ERROR, TAG, "Cbor find value failed.");
248 err = cbor_value_dup_byte_string(&curVal, &(resource->sid), &len, NULL);
249 if (CborNoError != err)
251 OC_LOG(ERROR, TAG, "Cbor di finding failed.");
257 err = cbor_value_map_find_value(&arrayVal, OC_RSRVD_LINKS, &linkArray);
258 if (CborNoError != err)
260 OC_LOG(ERROR, TAG, "Cbor links finding failed.");
264 err = cbor_value_enter_container(&linkArray, &linkMap);
265 if (CborNoError != err)
267 OC_LOG(ERROR, TAG, "Cbor entering map failed.");
271 err = cbor_value_map_find_value(&linkMap, OC_RSRVD_HREF, &curVal);
272 if (CborNoError != err)
274 OC_LOG(ERROR, TAG, "Cbor finding href type failed.");
277 err = cbor_value_dup_text_string(&curVal, &(resource->uri), &len, NULL);
278 if (CborNoError != err)
280 OC_LOG(ERROR, TAG, "Cbor finding href value failed.");
285 err = cbor_value_map_find_value(&linkMap, OC_RSRVD_RESOURCE_TYPE, &rtVal);
286 if (CborNoError != err)
288 OC_LOG(ERROR, TAG, "Cbor finding rt type failed.");
291 if (cbor_value_is_text_string(&rtVal))
295 err = cbor_value_dup_text_string(&rtVal, &input, &len, NULL);
296 if (CborNoError != err)
298 OC_LOG(ERROR, TAG, "Cbor finding rt value failed.");
303 char* curPtr = strtok_r(input, " ", &savePtr);
307 char* trimmed = InPlaceStringTrim(curPtr);
308 if (trimmed[0] !='\0')
310 if (!OCResourcePayloadAddResourceType(resource, trimmed))
312 OICFree(resource->uri);
313 OICFree(resource->sid);
314 OCFreeOCStringLL(resource->types);
316 OCDiscoveryPayloadDestroy(out);
317 return OC_STACK_NO_MEMORY;
320 curPtr = strtok_r(NULL, " ", &savePtr);
328 err = cbor_value_map_find_value(&linkMap, OC_RSRVD_INTERFACE, &ifVal);
329 if (CborNoError != err)
331 OC_LOG(ERROR, TAG, "Cbor finding if type failed.");
334 if (!err && cbor_value_is_text_string(&ifVal))
338 err = cbor_value_dup_text_string(&ifVal, &input, &len, NULL);
339 if (CborNoError != err)
341 OC_LOG(ERROR, TAG, "Cbor finding if value failed.");
346 char* curPtr = strtok_r(input, " ", &savePtr);
350 char* trimmed = InPlaceStringTrim(curPtr);
351 if (trimmed[0] !='\0')
353 if (!OCResourcePayloadAddInterface(resource, trimmed))
355 OICFree(resource->uri);
356 OICFree(resource->sid);
357 OCFreeOCStringLL(resource->types);
359 OCDiscoveryPayloadDestroy(out);
360 return OC_STACK_NO_MEMORY;
363 curPtr = strtok_r(NULL, " ", &savePtr);
371 err = cbor_value_map_find_value(&linkMap, OC_RSRVD_POLICY, &policyMap);
372 if (CborNoError != err)
374 OC_LOG(ERROR, TAG, "Cbor finding policy type failed.");
379 err = cbor_value_map_find_value(&policyMap, OC_RSRVD_BITMAP, &val);
380 if (CborNoError != err)
382 OC_LOG(ERROR, TAG, "Cbor finding bitmap type failed.");
386 err = cbor_value_get_uint64(&val, &temp);
387 if (CborNoError != err)
389 OC_LOG(ERROR, TAG, "Cbor finding bitmap value failed.");
392 resource->bitmap = (uint8_t)temp;
394 err = cbor_value_map_find_value(&policyMap, OC_RSRVD_SECURE, &val);
395 if (CborNoError != err)
397 OC_LOG(ERROR, TAG, "Cbor finding secure type failed.");
400 if(cbor_value_is_valid(&val))
402 err = cbor_value_get_boolean(&val, &(resource->secure));
403 if (CborNoError != err)
405 OC_LOG(ERROR, TAG, "Cbor finding secure value failed.");
410 err = cbor_value_map_find_value(&policyMap, OC_RSRVD_HOSTING_PORT,
412 if (CborNoError != err)
414 OC_LOG(ERROR, TAG, "Cbor finding port type failed.");
417 if(cbor_value_is_valid(&port))
419 err = cbor_value_get_uint64(&port, &temp);
420 if (CborNoError != err)
422 OC_LOG(ERROR, TAG, "Cbor finding port value failed.");
425 resource->port = (uint16_t)temp;
430 err = cbor_value_advance(&arrayVal);
431 if (CborNoError != err)
433 OC_LOG(ERROR, TAG, "Cbor advance value failed.");
437 OCDiscoveryPayloadAddNewResource(out, resource);
440 err = err || cbor_value_leave_container(rootValue, &arrayVal);
442 *outPayload = (OCPayload*)out;
446 OICFree(resource->uri);
447 OICFree(resource->sid);
448 OCFreeOCStringLL(resource->types);
449 OCFreeOCStringLL(resource->interfaces);
451 OCDiscoveryPayloadDestroy(out);
452 return OC_STACK_MALFORMED_RESPONSE;
455 OCDiscoveryCollectionPayloadDestroy(out);
456 return OC_STACK_MALFORMED_RESPONSE;
459 static OCStackResult OCParseDevicePayload(OCPayload** outPayload, CborValue* rootValue)
463 return OC_STACK_INVALID_PARAM;
468 if(cbor_value_is_map(rootValue))
472 char* specVer = NULL;
477 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_DEVICE_ID, &repVal);
479 if(cbor_value_is_valid(&repVal))
481 err = err || cbor_value_dup_byte_string(&repVal, &sid, &len, NULL);
484 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_DEVICE_NAME, &repVal);
485 if(cbor_value_is_valid(&repVal))
487 err = err || cbor_value_dup_text_string(&repVal, &dname, &len, NULL);
489 // Device Spec Version
490 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_SPEC_VERSION, &repVal);
491 if(cbor_value_is_valid(&repVal))
493 err = err || cbor_value_dup_text_string(&repVal, &specVer, &len, NULL);
496 // Data Model Version
497 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_DATA_MODEL_VERSION, &repVal);
498 if (cbor_value_is_valid(&repVal))
500 err = err || cbor_value_dup_text_string(&repVal, &dmVer, &len, NULL);
503 err = err || cbor_value_advance(rootValue);
511 OC_LOG_V(ERROR, TAG, "CBOR in error condition %d", err);
512 return OC_STACK_MALFORMED_RESPONSE;
515 *outPayload = (OCPayload*)OCDevicePayloadCreate(sid, dname, specVer, dmVer);
523 return OC_STACK_NO_MEMORY;
530 OC_LOG(ERROR, TAG, "Root device node was not a map");
531 return OC_STACK_MALFORMED_RESPONSE;
536 static OCStackResult OCParsePlatformPayload(OCPayload** outPayload, CborValue* rootValue)
540 return OC_STACK_INVALID_PARAM;
545 if(cbor_value_is_map(rootValue))
547 OCPlatformInfo info = {0};
552 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_PLATFORM_ID, &repVal);
553 if(cbor_value_is_valid(&repVal))
555 err = err || cbor_value_dup_text_string(&repVal, &(info.platformID), &len, NULL);
559 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_MFG_NAME, &repVal);
560 if(cbor_value_is_valid(&repVal))
562 err = err || cbor_value_dup_text_string(&repVal, &(info.manufacturerName), &len, NULL);
566 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_MFG_URL, &repVal);
567 if(cbor_value_is_valid(&repVal))
569 err = err || cbor_value_dup_text_string(&repVal, &(info.manufacturerUrl), &len, NULL);
573 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_MODEL_NUM, &repVal);
574 if(cbor_value_is_valid(&repVal))
576 err = err || cbor_value_dup_text_string(&repVal, &(info.modelNumber), &len, NULL);
580 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_MFG_DATE, &repVal);
581 if(cbor_value_is_valid(&repVal))
583 err = err || cbor_value_dup_text_string(&repVal, &(info.dateOfManufacture), &len,
588 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_PLATFORM_VERSION, &repVal);
589 if(cbor_value_is_valid(&repVal))
591 err = err || cbor_value_dup_text_string(&repVal, &(info.platformVersion), &len, NULL);
595 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_OS_VERSION, &repVal);
596 if(cbor_value_is_valid(&repVal))
598 err = err || cbor_value_dup_text_string(&repVal, &(info.operatingSystemVersion),
603 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_HARDWARE_VERSION, &repVal);
604 if(cbor_value_is_valid(&repVal))
606 err = err || cbor_value_dup_text_string(&repVal, &(info.hardwareVersion), &len,
611 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_FIRMWARE_VERSION, &repVal);
612 if(cbor_value_is_valid(&repVal))
614 err = err || cbor_value_dup_text_string(&repVal, &(info.firmwareVersion), &len,
619 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_SUPPORT_URL, &repVal);
620 if(cbor_value_is_valid(&repVal))
622 err = err || cbor_value_dup_text_string(&repVal, &(info.supportUrl), &len, NULL);
626 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_SYSTEM_TIME, &repVal);
627 if(cbor_value_is_valid(&repVal))
629 err = err || cbor_value_dup_text_string(&repVal, &(info.systemTime), &len, NULL);
632 err = err || cbor_value_advance(rootValue);
636 OICFree(info.dateOfManufacture);
637 OICFree(info.firmwareVersion);
638 OICFree(info.hardwareVersion);
639 OICFree(info.manufacturerName);
640 OICFree(info.manufacturerUrl);
641 OICFree(info.modelNumber);
642 OICFree(info.operatingSystemVersion);
643 OICFree(info.platformID);
644 OICFree(info.platformVersion);
645 OICFree(info.supportUrl);
646 OICFree(info.systemTime);
647 OC_LOG(ERROR, TAG, "CBOR error In ParsePlatformPayload");
648 return OC_STACK_MALFORMED_RESPONSE;
651 *outPayload = (OCPayload*)OCPlatformPayloadCreateAsOwner(&info);
655 return OC_STACK_NO_MEMORY;
662 OC_LOG(ERROR, TAG, "Root device node was not a map");
663 return OC_STACK_MALFORMED_RESPONSE;
667 static OCRepPayloadPropType DecodeCborType(CborType type)
672 return OCREP_PROP_NULL;
673 case CborIntegerType:
674 return OCREP_PROP_INT;
676 return OCREP_PROP_DOUBLE;
677 case CborBooleanType:
678 return OCREP_PROP_BOOL;
679 case CborTextStringType:
680 return OCREP_PROP_STRING;
681 case CborByteStringType:
682 return OCREP_PROP_BYTE_STRING;
684 return OCREP_PROP_OBJECT;
686 return OCREP_PROP_ARRAY;
688 return OCREP_PROP_NULL;
691 static bool OCParseArrayFindDimensionsAndType(const CborValue* parent, size_t dimensions[MAX_REP_ARRAY_DEPTH],
692 OCRepPayloadPropType* type)
695 CborValue insideArray;
696 *type = OCREP_PROP_NULL;
697 dimensions[0] = dimensions[1] = dimensions[2] = 0;
699 err = err || cbor_value_enter_container(parent, &insideArray);
701 while (cbor_value_is_valid(&insideArray))
703 OCRepPayloadPropType tempType = DecodeCborType(cbor_value_get_type(&insideArray));
705 if (tempType == OCREP_PROP_ARRAY)
707 size_t subdim[MAX_REP_ARRAY_DEPTH];
708 tempType = OCREP_PROP_NULL;
709 err = err || OCParseArrayFindDimensionsAndType(&insideArray, subdim, &tempType);
713 OC_LOG(ERROR, TAG, "Parse array helper, sub-array too deep");
716 dimensions[1] = dimensions[1] >= subdim[0] ? dimensions[1] : subdim[0];
717 dimensions[2] = dimensions[2] >= subdim[1] ? dimensions[2] : subdim[1];
719 if (*type != OCREP_PROP_NULL && tempType != OCREP_PROP_NULL
720 && *type != tempType)
722 OC_LOG(ERROR, TAG, "Array parse failed, mixed arrays not allowed (subtype)");
725 else if (*type == OCREP_PROP_NULL)
727 // We don't know the type of this array yet, so the assignment is OK
731 else if (*type == OCREP_PROP_NULL)
733 // We don't know the type of this array yet, so the assignment is OK
736 // tempType is allowed to be NULL, since it might now know the answer yet
737 else if (tempType != OCREP_PROP_NULL && *type != tempType)
739 // this is an invalid situation!
740 OC_LOG(ERROR, TAG, "Array parse failed, mixed arrays not allowed");
745 cbor_value_advance(&insideArray);
751 static size_t getAllocSize(OCRepPayloadPropType type)
756 return sizeof (int64_t);
757 case OCREP_PROP_DOUBLE:
758 return sizeof (double);
759 case OCREP_PROP_BOOL:
760 return sizeof (bool);
761 case OCREP_PROP_STRING:
762 return sizeof (char*);
763 case OCREP_PROP_BYTE_STRING:
764 return sizeof (OCByteString);
765 case OCREP_PROP_OBJECT:
766 return sizeof (OCRepPayload*);
772 static size_t arrayStep(size_t dimensions[MAX_REP_ARRAY_DEPTH], size_t elementNum)
775 (dimensions[1] == 0 ? 1 : dimensions[1]) *
776 (dimensions[2] == 0 ? 1 : dimensions[2]) *
780 static bool OCParseArrayFillArray(const CborValue* parent, size_t dimensions[MAX_REP_ARRAY_DEPTH],
781 OCRepPayloadPropType type, void* targetArray)
784 CborValue insideArray;
786 err = err || cbor_value_enter_container(parent, &insideArray);
789 char* tempStr = NULL;
790 OCByteString ocByteStr = { .bytes = NULL, .len = 0};
792 OCRepPayload* tempPl = NULL;
794 size_t newdim[MAX_REP_ARRAY_DEPTH];
795 newdim[0] = dimensions[1];
796 newdim[1] = dimensions[2];
799 while (!err && i < dimensions[0] && cbor_value_is_valid(&insideArray))
801 bool noAdvance = false;
802 if (cbor_value_get_type(&insideArray) != CborNullType)
807 if (dimensions[1] == 0)
809 err = err || cbor_value_get_int64(&insideArray,
810 &(((int64_t*)targetArray)[i]));
814 err = err || OCParseArrayFillArray(&insideArray, newdim,
816 &(((int64_t*)targetArray)[arrayStep(dimensions, i)])
820 case OCREP_PROP_DOUBLE:
821 if (dimensions[1] == 0)
823 err = err || cbor_value_get_double(&insideArray,
824 &(((double*)targetArray)[i]));
828 err = err || OCParseArrayFillArray(&insideArray, newdim,
830 &(((double*)targetArray)[arrayStep(dimensions, i)])
834 case OCREP_PROP_BOOL:
835 if (dimensions[1] == 0)
837 err = err || cbor_value_get_boolean(&insideArray,
838 &(((bool*)targetArray)[i]));
842 err = err || OCParseArrayFillArray(&insideArray, newdim,
844 &(((bool*)targetArray)[arrayStep(dimensions, i)])
848 case OCREP_PROP_STRING:
849 if (dimensions[1] == 0)
851 err = err || cbor_value_dup_text_string(&insideArray,
852 &tempStr, &tempLen, NULL);
853 ((char**)targetArray)[i] = tempStr;
858 err = err || OCParseArrayFillArray(&insideArray, newdim,
860 &(((char**)targetArray)[arrayStep(dimensions, i)])
864 case OCREP_PROP_BYTE_STRING:
865 if (dimensions[1] == 0)
867 err = err || cbor_value_dup_byte_string(&insideArray,
868 &(ocByteStr.bytes), &(ocByteStr.len), NULL);
869 ((OCByteString*)targetArray)[i] = ocByteStr;
873 err = err || OCParseArrayFillArray(&insideArray, newdim,
875 &(((OCByteString*)targetArray)[arrayStep(dimensions, i)])
879 case OCREP_PROP_OBJECT:
880 if (dimensions[1] == 0)
882 err = err || OCParseSingleRepPayload(&tempPl, &insideArray);
883 ((OCRepPayload**)targetArray)[i] = tempPl;
889 err = err || OCParseArrayFillArray(&insideArray, newdim,
891 &(((OCRepPayload**)targetArray)[arrayStep(dimensions, i)])
896 OC_LOG(ERROR, TAG, "Invalid Array type in Parse Array");
902 if (!noAdvance && cbor_value_is_valid(&insideArray))
904 err = err || cbor_value_advance(&insideArray);
911 static bool OCParseArray(OCRepPayload* out, const char* name, CborValue* container)
913 OCRepPayloadPropType type;
914 size_t dimensions[MAX_REP_ARRAY_DEPTH];
915 bool err = OCParseArrayFindDimensionsAndType(container, dimensions, &type);
919 OC_LOG(ERROR, TAG, "Array details weren't clear");
923 if (type == OCREP_PROP_NULL)
925 err = err || OCRepPayloadSetNull(out, name);
926 err = err || cbor_value_advance(container);
930 size_t dimTotal = calcDimTotal(dimensions);
931 size_t allocSize = getAllocSize(type);
932 void* arr = OICCalloc(dimTotal, allocSize);
936 OC_LOG(ERROR, TAG, "Array Parse allocation failed");
940 err = err || OCParseArrayFillArray(container, dimensions, type, arr);
945 if (err || !OCRepPayloadSetIntArrayAsOwner(out, name, (int64_t*)arr, dimensions))
951 case OCREP_PROP_DOUBLE:
952 if (err || !OCRepPayloadSetDoubleArrayAsOwner(out, name, (double*)arr, dimensions))
958 case OCREP_PROP_BOOL:
959 if (err || !OCRepPayloadSetBoolArrayAsOwner(out, name, (bool*)arr, dimensions))
965 case OCREP_PROP_STRING:
966 if (err || !OCRepPayloadSetStringArrayAsOwner(out, name, (char**)arr, dimensions))
968 for(size_t i = 0; i < dimTotal; ++i)
970 OICFree(((char**)arr)[i]);
976 case OCREP_PROP_BYTE_STRING:
977 if (err || !OCRepPayloadSetByteStringArrayAsOwner(out, name, (OCByteString*)arr, dimensions))
979 for (size_t i = 0; i < dimTotal; ++i)
981 OICFree(((OCByteString*)arr)[i].bytes);
987 case OCREP_PROP_OBJECT:
988 if (err || !OCRepPayloadSetPropObjectArrayAsOwner(out, name, (OCRepPayload**)arr, dimensions))
990 for(size_t i = 0; i < dimTotal; ++i)
992 OCRepPayloadDestroy(((OCRepPayload**)arr)[i]);
999 OC_LOG(ERROR, TAG, "Invalid Array type in Parse Array");
1007 static bool OCParseSingleRepPayload(OCRepPayload** outPayload, CborValue *objMap)
1016 if (cbor_value_is_map(objMap))
1020 *outPayload = OCRepPayloadCreate();
1023 return CborErrorOutOfMemory;
1027 OCRepPayload* curPayload = *outPayload;
1031 err = err || cbor_value_enter_container(objMap, &repMap);
1033 while(!err && cbor_value_is_valid(&repMap))
1036 err = err || cbor_value_dup_text_string(&repMap, &name, &len, NULL);
1037 err = err || cbor_value_advance(&repMap);
1039 CborType type = cbor_value_get_type(&repMap);
1043 err = !OCRepPayloadSetNull(curPayload, name);
1045 case CborIntegerType:
1048 err = err || cbor_value_get_int64(&repMap, &intval);
1051 err = !OCRepPayloadSetPropInt(curPayload, name, intval);
1055 case CborDoubleType:
1057 double doubleval = 0;
1058 err = err || cbor_value_get_double(&repMap, &doubleval);
1061 err = !OCRepPayloadSetPropDouble(curPayload, name, doubleval);
1065 case CborBooleanType:
1067 bool boolval = false;
1068 err = err || cbor_value_get_boolean(&repMap, &boolval);
1071 err = !OCRepPayloadSetPropBool(curPayload, name, boolval);
1075 case CborTextStringType:
1077 char* strval = NULL;
1078 err = err || cbor_value_dup_text_string(&repMap, &strval, &len, NULL);
1081 err = !OCRepPayloadSetPropStringAsOwner(curPayload, name, strval);
1085 case CborByteStringType:
1087 uint8_t* bytestrval = NULL;
1088 err = err || cbor_value_dup_byte_string(&repMap, &bytestrval, &len, NULL);
1091 OCByteString tmp = {.bytes = bytestrval, .len = len};
1092 err = !OCRepPayloadSetPropByteStringAsOwner(curPayload, name, &tmp);
1098 OCRepPayload *pl = NULL;
1099 err = err || OCParseSingleRepPayload(&pl, &repMap);
1102 err = !OCRepPayloadSetPropObjectAsOwner(curPayload, name, pl);
1107 err = err || OCParseArray(curPayload, name, &repMap);
1110 OC_LOG_V(ERROR, TAG, "Parsing rep property, unknown type %d", repMap.type);
1114 if (type != CborMapType && cbor_value_is_valid(&repMap))
1116 err = err || cbor_value_advance(&repMap);
1121 err = err || cbor_value_leave_container(objMap, &repMap);
1125 OCRepPayloadDestroy(*outPayload);
1133 static OCStackResult OCParseRepPayload(OCPayload** outPayload, CborValue* rootMap)
1137 return OC_STACK_INVALID_PARAM;
1140 OCRepPayload *rootPayload = NULL;
1141 OCRepPayload *curPayload = NULL;
1142 OCRepPayload *temp = OCRepPayloadCreate();
1145 return OC_STACK_NO_MEMORY;
1151 err = err || cbor_value_map_find_value(rootMap, OC_RSRVD_RESOURCE_TYPE, &curVal);
1152 if(cbor_value_is_text_string(&curVal))
1155 err = err || cbor_value_dup_text_string(&curVal, &allRt, &len, NULL);
1159 char* curPtr = strtok_r(allRt, " ", &savePtr);
1162 char* trimmed = InPlaceStringTrim(curPtr);
1163 if (trimmed[0] != '\0')
1165 OCRepPayloadAddResourceType(temp, curPtr);
1167 curPtr = strtok_r(NULL, " ", &savePtr);
1173 err = err || cbor_value_map_find_value(rootMap, OC_RSRVD_INTERFACE, &curVal);
1174 if(cbor_value_is_text_string(&curVal))
1177 err = err || cbor_value_dup_text_string(&curVal, &allIf, &len, NULL);
1181 char* curPtr = strtok_r(allIf, " ", &savePtr);
1184 char* trimmed = InPlaceStringTrim(curPtr);
1185 if (trimmed[0] != '\0')
1187 OCRepPayloadAddInterface(temp, curPtr);
1189 curPtr = strtok_r(NULL, " ", &savePtr);
1195 while (!err && cbor_value_is_map(rootMap))
1197 err = err || OCParseSingleRepPayload(&temp, rootMap);
1199 if(rootPayload == NULL)
1206 curPayload->next = temp;
1207 curPayload = curPayload->next;
1210 if (cbor_value_is_valid(rootMap))
1212 err = err || cbor_value_advance(rootMap);
1217 OCRepPayloadDestroy(rootPayload);
1218 OC_LOG(ERROR, TAG, "CBOR error in ParseRepPayload");
1219 return OC_STACK_MALFORMED_RESPONSE;
1223 *outPayload = (OCPayload*)rootPayload;
1228 static OCStackResult OCParsePresencePayload(OCPayload** outPayload, CborValue* rootValue)
1232 return OC_STACK_INVALID_PARAM;
1237 if(cbor_value_is_map(rootValue))
1239 uint64_t seqNum = 0;
1240 uint64_t maxAge = 0;
1241 OCPresenceTrigger trigger = OC_PRESENCE_TRIGGER_CREATE;
1242 char* tempStr = NULL;
1247 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_NONCE, &curVal);
1248 err = err || cbor_value_get_uint64(&curVal, &seqNum);
1251 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_TTL, &curVal);
1252 err = err || cbor_value_get_uint64(&curVal, &maxAge);
1255 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_TRIGGER, &curVal);
1256 err = err || cbor_value_dup_text_string(&curVal, &tempStr, &len, NULL);
1257 trigger = convertTriggerStringToEnum(tempStr);
1261 // Resource type name
1262 err = err || cbor_value_map_find_value(rootValue, OC_RSRVD_RESOURCE_TYPE, &curVal);
1263 if(cbor_value_is_valid(&curVal))
1265 err = err || cbor_value_dup_text_string(&curVal, &tempStr, &len, NULL);
1268 err = err || cbor_value_advance(rootValue);
1272 *outPayload = (OCPayload*)OCPresencePayloadCreate(seqNum, maxAge, trigger, tempStr);
1278 OCPayloadDestroy(*outPayload);
1279 OC_LOG(ERROR, TAG, "CBOR error Parse Presence Payload");
1280 return OC_STACK_MALFORMED_RESPONSE;
1285 return OC_STACK_NO_MEMORY;
1292 OC_LOG(ERROR, TAG, "Root presence node was not a map");
1293 return OC_STACK_MALFORMED_RESPONSE;