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 #include "ocpayloadcbor.h"
24 #include "oic_malloc.h"
25 #include "ocstackinternal.h"
26 #include "ocpayload.h"
29 #define TAG PCF("OCPayloadParse")
31 static OCStackResult OCParseDiscoveryPayload(OCPayload** outPayload, CborValue* arrayVal);
32 static OCStackResult OCParseDevicePayload(OCPayload** outPayload, CborValue* arrayVal);
33 static OCStackResult OCParsePlatformPayload(OCPayload** outPayload, CborValue* arrayVal);
34 static bool OCParseSingleRepPayload(OCRepPayload** outPayload, CborValue* repParent);
35 static OCStackResult OCParseRepPayload(OCPayload** outPayload, CborValue* arrayVal);
36 static OCStackResult OCParsePresencePayload(OCPayload** outPayload, CborValue* arrayVal);
37 static OCStackResult OCParseSecurityPayload(OCPayload** outPayload, CborValue* arrayVal);
39 OCStackResult OCParsePayload(OCPayload** outPayload, const uint8_t* payload, size_t payloadSize)
45 OC_LOG_V(INFO, TAG, "CBOR Parsing size: %d", payloadSize);
46 if((err = cbor_parser_init(payload, payloadSize, 0, &parser, &rootValue)) != false)
48 OC_LOG_V(ERROR, TAG, "CBOR Parser init failed: %d", err);
49 return OC_STACK_ERROR;
52 if(!cbor_value_is_array(&rootValue))
54 OC_LOG_V(ERROR, TAG, "CBOR payload root object is not an array :%x", rootValue.type);
55 return OC_STACK_MALFORMED_RESPONSE;
60 err = err || cbor_value_enter_container(&rootValue, &arrayValue);
63 err = err || cbor_value_get_int(&arrayValue, &payloadType);
64 err = err || cbor_value_advance_fixed(&arrayValue);
68 OC_LOG_V(ERROR, TAG, "CBOR payload parse failed :%d", err);
69 return OC_STACK_MALFORMED_RESPONSE;
72 OCStackResult result = OC_STACK_ERROR;
75 case PAYLOAD_TYPE_DISCOVERY:
76 result = OCParseDiscoveryPayload(outPayload, &arrayValue);
78 case PAYLOAD_TYPE_DEVICE:
79 result = OCParseDevicePayload(outPayload, &arrayValue);
81 case PAYLOAD_TYPE_PLATFORM:
82 result = OCParsePlatformPayload(outPayload, &arrayValue);
84 case PAYLOAD_TYPE_REPRESENTATION:
85 result = OCParseRepPayload(outPayload, &arrayValue);
87 case PAYLOAD_TYPE_PRESENCE:
88 result = OCParsePresencePayload(outPayload, &arrayValue);
90 case PAYLOAD_TYPE_SECURITY:
91 result = OCParseSecurityPayload(outPayload, &arrayValue);
95 result = OCRDCborToPayload(&arrayValue, outPayload);
99 OC_LOG_V(ERROR, TAG, "ParsePayload Type default: %d", payloadType);
100 result = OC_STACK_ERROR;
104 if(result == OC_STACK_OK)
106 err = err || cbor_value_leave_container(&rootValue, &arrayValue);
107 if(err != CborNoError)
109 return OC_STACK_MALFORMED_RESPONSE;
114 OC_LOG_V(INFO, TAG, "Finished parse payload, result is %d", result);
120 void OCFreeOCStringLL(OCStringLL* ll);
122 static OCStackResult OCParseSecurityPayload(OCPayload** outPayload, CborValue* arrayVal)
125 char * securityData = NULL;
127 if(cbor_value_is_map(arrayVal))
130 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_REPRESENTATION, &curVal);
132 if(cbor_value_is_valid(&curVal))
135 err = err || cbor_value_dup_text_string(&curVal, &securityData, &len, NULL);
140 OC_LOG_V(ERROR, TAG, PCF("Cbor main value not a map"));
141 return OC_STACK_MALFORMED_RESPONSE;
144 err = err || cbor_value_advance(arrayVal);
148 OC_LOG_V(ERROR, TAG, "Cbor in error condition");
149 OICFree(securityData);
150 return OC_STACK_MALFORMED_RESPONSE;
153 *outPayload = (OCPayload*)OCSecurityPayloadCreate(securityData);
154 OICFree(securityData);
160 static OCStackResult OCParseDiscoveryPayload(OCPayload** outPayload, CborValue* arrayVal)
164 OCDiscoveryPayload* out = OCDiscoveryPayloadCreate();
168 return OC_STACK_NO_MEMORY;
171 size_t resourceCount = 0;
173 cbor_value_is_map(arrayVal))
175 OCResourcePayload* resource = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
178 OC_LOG_V(ERROR, TAG, "Memory allocation failed");
179 OCDiscoveryPayloadDestroy(out);
180 return OC_STACK_NO_MEMORY;
185 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_HREF, &curVal);
187 err = err || cbor_value_dup_text_string(&curVal, &(resource->uri), &len, NULL);
190 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_SERVER_INSTANCE_ID, &curVal);
191 err = err || cbor_value_dup_byte_string(&curVal, &(resource->sid), &len, NULL);
195 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_PROPERTY, &curVal);
198 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_RESOURCE_TYPE, &rtArray);
201 err = err || cbor_value_enter_container(&rtArray, &rtVal);
203 OCStringLL* llPtr = NULL;
204 while(!err && cbor_value_is_text_string(&rtVal))
206 if(resource->types == NULL)
208 resource->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
209 llPtr = resource->types;
212 OC_LOG(ERROR, TAG, PCF("Memory allocation failed"));
213 OICFree(resource->uri);
214 OICFree(resource->sid);
216 OCDiscoveryPayloadDestroy(out);
217 return OC_STACK_NO_MEMORY;
222 llPtr->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
226 OC_LOG(ERROR, TAG, PCF("Memory allocation failed"));
227 OICFree(resource->uri);
228 OICFree(resource->sid);
229 OCFreeOCStringLL(resource->types);
231 OCDiscoveryPayloadDestroy(out);
232 return OC_STACK_NO_MEMORY;
237 OC_LOG(ERROR, TAG, PCF("Unknown state in resource type copying"));
238 OICFree(resource->uri);
239 OICFree(resource->sid);
240 OCFreeOCStringLL(resource->types);
242 OCDiscoveryPayloadDestroy(out);
243 return OC_STACK_NO_MEMORY;
246 err = err || cbor_value_dup_text_string(&rtVal, &(llPtr->value), &len, NULL);
247 err = err || cbor_value_advance(&rtVal);
250 err = err || cbor_value_leave_container(&rtArray, &rtVal);
254 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_INTERFACE, &ifArray);
256 err = err || cbor_value_enter_container(&ifArray, &ifVal);
259 while(!err && cbor_value_is_text_string(&ifVal))
261 if(resource->interfaces == NULL)
263 resource->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
264 llPtr = resource->interfaces;
267 OC_LOG_V(ERROR, TAG, "Memory allocation failed");
268 OICFree(resource->uri);
269 OICFree(resource->sid);
270 OCFreeOCStringLL(resource->types);
272 OCDiscoveryPayloadDestroy(out);
273 return OC_STACK_NO_MEMORY;
278 llPtr->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
282 OC_LOG_V(ERROR, TAG, "Memory allocation failed");
283 OICFree(resource->uri);
284 OICFree(resource->sid);
285 OCFreeOCStringLL(resource->types);
286 OCFreeOCStringLL(resource->interfaces);
288 OCDiscoveryPayloadDestroy(out);
289 return OC_STACK_NO_MEMORY;
294 OC_LOG(ERROR, TAG, PCF("Unknown state in resource interfaces copying"));
295 OICFree(resource->uri);
296 OICFree(resource->sid);
297 OCFreeOCStringLL(resource->types);
299 OCDiscoveryPayloadDestroy(out);
300 return OC_STACK_NO_MEMORY;
303 err = err || cbor_value_dup_text_string(&ifVal, &(llPtr->value), &len, NULL);
304 err = err || cbor_value_advance(&ifVal);
306 err = err || cbor_value_leave_container(&ifArray, &ifVal);
311 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_POLICY, &policyMap);
315 err = err || cbor_value_map_find_value(&policyMap, OC_RSRVD_BITMAP, &val);
317 err = err || cbor_value_get_uint64(&val, &temp);
318 resource->bitmap = (uint8_t)temp;
320 err = err || cbor_value_map_find_value(&policyMap, OC_RSRVD_SECURE, &val);
321 if(cbor_value_is_valid(&val))
323 err = err || cbor_value_get_boolean(&val, &(resource->secure));
326 err = err || cbor_value_map_find_value(&policyMap, OC_RSRVD_HOSTING_PORT,
328 if(cbor_value_is_valid(&port))
330 err = err || cbor_value_get_uint64(&port, &temp);
331 resource->port = (uint16_t)temp;
337 err = err || cbor_value_advance(arrayVal);
340 OICFree(resource->uri);
341 OICFree(resource->sid);
342 OCFreeOCStringLL(resource->types);
343 OCFreeOCStringLL(resource->interfaces);
345 OCDiscoveryPayloadDestroy(out);
346 OC_LOG_V(ERROR, TAG, "CBOR in error condition", err);
347 return OC_STACK_MALFORMED_RESPONSE;
350 OCDiscoveryPayloadAddNewResource(out, resource);
355 OCDiscoveryPayloadDestroy(out);
356 return OC_STACK_MALFORMED_RESPONSE;
360 *outPayload = (OCPayload*)out;
365 static OCStackResult OCParseDevicePayload(OCPayload** outPayload, CborValue* arrayVal)
369 if(cbor_value_is_map(arrayVal))
374 char* specVer = NULL;
377 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_HREF, &curVal);
379 err = err || cbor_value_dup_text_string(&curVal, &uri, &len, NULL);
383 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_REPRESENTATION, &curVal);
387 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_DEVICE_ID, &repVal);
388 err = err || cbor_value_dup_byte_string(&repVal, &sid, &len, NULL);
390 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_DEVICE_NAME, &repVal);
391 err = err || cbor_value_dup_text_string(&repVal, &dname, &len, NULL);
392 // Device Spec Version
393 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_SPEC_VERSION, &repVal);
394 err = err || cbor_value_dup_text_string(&repVal, &specVer, &len, NULL);
395 // Data Model Version
396 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_DATA_MODEL_VERSION, &repVal);
397 err = err || cbor_value_dup_text_string(&repVal, &dmVer, &len, NULL);
401 err = err || cbor_value_advance(arrayVal);
410 OC_LOG_V(ERROR, TAG, "CBOR in error condition %d", err);
411 return OC_STACK_MALFORMED_RESPONSE;
414 *outPayload = (OCPayload*)OCDevicePayloadCreate(uri, sid, dname, specVer, dmVer);
423 return OC_STACK_NO_MEMORY;
430 OC_LOG(ERROR, TAG, PCF("Root device node was not a map"));
431 return OC_STACK_MALFORMED_RESPONSE;
436 static OCStackResult OCParsePlatformPayload(OCPayload** outPayload, CborValue* arrayVal)
440 if(cbor_value_is_map(arrayVal))
443 OCPlatformInfo info = {0};
445 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_HREF, &curVal);
447 err = err || cbor_value_dup_text_string(&curVal, &uri, &len, NULL);
451 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_REPRESENTATION, &curVal);
455 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_PLATFORM_ID, &repVal);
456 err = err || cbor_value_dup_text_string(&repVal, &(info.platformID), &len, NULL);
459 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_MFG_NAME, &repVal);
460 err = err || cbor_value_dup_text_string(&repVal, &(info.manufacturerName), &len, NULL);
463 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_MFG_URL, &repVal);
464 if(cbor_value_is_valid(&repVal))
466 err = err || cbor_value_dup_text_string(&repVal, &(info.manufacturerUrl), &len, NULL);
470 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_MODEL_NUM, &repVal);
471 if(cbor_value_is_valid(&repVal))
473 err = err || cbor_value_dup_text_string(&repVal, &(info.modelNumber), &len, NULL);
477 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_MFG_DATE, &repVal);
478 if(cbor_value_is_valid(&repVal))
480 err = err || cbor_value_dup_text_string(&repVal, &(info.dateOfManufacture), &len,
485 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_PLATFORM_VERSION, &repVal);
486 if(cbor_value_is_valid(&repVal))
488 err = err || cbor_value_dup_text_string(&repVal, &(info.platformVersion), &len,
493 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_OS_VERSION, &repVal);
494 if(cbor_value_is_valid(&repVal))
496 err = err || cbor_value_dup_text_string(&repVal, &(info.operatingSystemVersion),
501 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_HARDWARE_VERSION, &repVal);
502 if(cbor_value_is_valid(&repVal))
504 err = err || cbor_value_dup_text_string(&repVal, &(info.hardwareVersion), &len,
509 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_FIRMWARE_VERSION, &repVal);
510 if(cbor_value_is_valid(&repVal))
512 err = err || cbor_value_dup_text_string(&repVal, &(info.firmwareVersion), &len,
517 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_SUPPORT_URL, &repVal);
518 if(cbor_value_is_valid(&repVal))
520 err = err || cbor_value_dup_text_string(&repVal, &(info.supportUrl), &len, NULL);
524 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_SYSTEM_TIME, &repVal);
525 if(cbor_value_is_valid(&repVal))
527 err = err || cbor_value_dup_text_string(&repVal, &(info.systemTime), &len, NULL);
531 err = err || cbor_value_advance(arrayVal);
535 OICFree(info.dateOfManufacture);
536 OICFree(info.firmwareVersion);
537 OICFree(info.hardwareVersion);
538 OICFree(info.manufacturerName);
539 OICFree(info.manufacturerUrl);
540 OICFree(info.modelNumber);
541 OICFree(info.operatingSystemVersion);
542 OICFree(info.platformID);
543 OICFree(info.platformVersion);
544 OICFree(info.supportUrl);
545 OICFree(info.systemTime);
546 OC_LOG(ERROR, TAG, PCF("CBOR error In ParsePlatformPayload"));
547 return OC_STACK_MALFORMED_RESPONSE;
550 *outPayload = (OCPayload*)OCPlatformPayloadCreateAsOwner(uri, &info);
554 return OC_STACK_NO_MEMORY;
561 OC_LOG(ERROR, TAG, PCF("Root device node was not a map"));
562 return OC_STACK_MALFORMED_RESPONSE;
566 static bool OCParseArray(OCRepPayload* out, const char* name, CborValue* container)
568 CborValue insideArray;
570 uint64_t tempInt = 0;
571 OCRepPayloadPropType type;
572 size_t dimensions[MAX_REP_ARRAY_DEPTH];
573 err = err || cbor_value_enter_container(container, &insideArray);
575 err = err || cbor_value_get_uint64(&insideArray, &tempInt);
576 err = err || cbor_value_advance_fixed(&insideArray);
577 type = (OCRepPayloadPropType)tempInt;
579 for(int i = 0; i < MAX_REP_ARRAY_DEPTH; ++ i)
581 err = err || cbor_value_get_uint64(&insideArray, &tempInt);
582 err = err || cbor_value_advance_fixed(&insideArray);
583 dimensions[i] = tempInt;
586 size_t dimTotal = calcDimTotal(dimensions);
595 arr = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
598 for(size_t i = 0; i < dimTotal && !err; ++i)
600 err = err || cbor_value_get_int64(&insideArray, &(((int64_t*)arr)[i]));
601 err = err || cbor_value_advance_fixed(&insideArray);
603 if(err || !OCRepPayloadSetIntArrayAsOwner(out, name, (int64_t*)arr, dimensions))
614 case OCREP_PROP_DOUBLE:
615 arr = (double*)OICMalloc(dimTotal * sizeof(double));
618 for(size_t i = 0; i < dimTotal && !err; ++i)
620 err = err || cbor_value_get_double(&insideArray, &(((double*)arr)[i]));
621 err = err || cbor_value_advance_fixed(&insideArray);
623 if(err || !OCRepPayloadSetDoubleArrayAsOwner(out, name, (double*)arr, dimensions))
634 case OCREP_PROP_BOOL:
635 arr = (bool*)OICMalloc(dimTotal * sizeof(bool));
638 for(size_t i = 0; i < dimTotal && !err; ++i)
640 err = err || cbor_value_get_boolean(&insideArray, &(((bool*)arr)[i]));
641 err = err || cbor_value_advance_fixed(&insideArray);
643 if(err && !OCRepPayloadSetBoolArrayAsOwner(out, name, (bool*)arr, dimensions))
654 case OCREP_PROP_STRING:
655 arr = (char**)OICMalloc(dimTotal * sizeof(char*));
658 for(size_t i = 0; i < dimTotal && !err; ++i)
660 err = err || cbor_value_dup_text_string(&insideArray, &tempStr,
662 err = err || cbor_value_advance(&insideArray);
663 ((char**)arr)[i] = tempStr;
665 if(err || !OCRepPayloadSetStringArrayAsOwner(out, name, (char**)arr, dimensions))
676 case OCREP_PROP_OBJECT:
677 arr = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
680 for(size_t i = 0; i < dimTotal && !err; ++i)
683 err = err || OCParseSingleRepPayload(&pl, &insideArray);
684 err = err || cbor_value_advance(&insideArray);
685 ((OCRepPayload**)arr)[i] = pl;
687 if(err || !OCRepPayloadSetPropObjectArrayAsOwner(out, name,
688 (OCRepPayload**)arr, dimensions))
700 OC_LOG(ERROR, TAG, "Invalid Array type in Parse Array");
708 static bool OCParseSingleRepPayload(OCRepPayload** outPayload, CborValue* repParent)
710 *outPayload = OCRepPayloadCreate();
711 OCRepPayload* curPayload = *outPayload;
715 return CborErrorOutOfMemory;
720 err = err || cbor_value_map_find_value(repParent, OC_RSRVD_HREF, &curVal);
721 if(cbor_value_is_valid(&curVal))
723 err = err || cbor_value_dup_text_string(&curVal, &curPayload->uri, &len,
727 err = err || cbor_value_map_find_value(repParent, OC_RSRVD_PROPERTY, &curVal);
728 if(cbor_value_is_valid(&curVal))
730 CborValue insidePropArray = {0};
731 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_RESOURCE_TYPE,
734 if(cbor_value_is_array(&insidePropArray))
737 err = err || cbor_value_enter_container(&insidePropArray, &rtArray);
739 while(!err && cbor_value_is_valid(&rtArray))
742 err = err || cbor_value_dup_text_string(&rtArray, &curRt, &len, NULL);
743 err = err || cbor_value_advance(&rtArray);
744 OCRepPayloadAddResourceTypeAsOwner(curPayload, curRt);
747 err = err || cbor_value_leave_container(&insidePropArray, &rtArray);
750 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_INTERFACE, &insidePropArray);
752 if(cbor_value_is_array(&insidePropArray))
755 err = err || cbor_value_enter_container(&insidePropArray, &ifArray);
757 while(!err && cbor_value_is_valid(&ifArray))
760 err = err || cbor_value_dup_text_string(&ifArray, &curIf, &len, NULL);
761 err = err || cbor_value_advance(&ifArray);
762 OCRepPayloadAddInterfaceAsOwner(curPayload, curIf);
765 err = err || cbor_value_leave_container(&insidePropArray, &ifArray);
768 err = err || cbor_value_map_find_value(repParent, OC_RSRVD_REPRESENTATION, &curVal);
769 if(cbor_value_is_map(&curVal))
772 err = err || cbor_value_enter_container(&curVal, &repMap);
774 while(!err && cbor_value_is_valid(&repMap))
777 err = err || cbor_value_dup_text_string(&repMap, &name, &len, NULL);
779 err = err || cbor_value_advance(&repMap);
782 bool boolval = false;
784 double doubleval = 0;
787 switch(cbor_value_get_type(&repMap))
790 err = !OCRepPayloadSetNull(curPayload, name);
792 case CborIntegerType:
793 err = err || cbor_value_get_int64(&repMap, &intval);
796 err = !OCRepPayloadSetPropInt(curPayload, name, intval);
800 err = err || cbor_value_get_double(&repMap, &doubleval);
803 err = !OCRepPayloadSetPropDouble(curPayload, name, doubleval);
806 case CborBooleanType:
807 err = err || cbor_value_get_boolean(&repMap, &boolval);
810 err = !OCRepPayloadSetPropBool(curPayload, name, boolval);
813 case CborTextStringType:
814 err = err || cbor_value_dup_text_string(&repMap, &strval, &len, NULL);
817 err = !OCRepPayloadSetPropStringAsOwner(curPayload, name, strval);
821 err = err || OCParseSingleRepPayload(&pl, &repMap);
824 err = !OCRepPayloadSetPropObjectAsOwner(curPayload, name, pl);
828 err = err || OCParseArray(curPayload, name, &repMap);
831 OC_LOG_V(ERROR, TAG, "Parsing rep property, unknown type %d", repMap.type);
835 err = err || cbor_value_advance(&repMap);
838 err = err || cbor_value_leave_container(&curVal, &repMap);
843 OCRepPayloadDestroy(*outPayload);
849 static OCStackResult OCParseRepPayload(OCPayload** outPayload, CborValue* arrayVal)
853 OCRepPayload* rootPayload = NULL;
854 OCRepPayload* curPayload = NULL;
855 OCRepPayload* temp = NULL;
856 while(!err && cbor_value_is_map(arrayVal))
858 err = err || OCParseSingleRepPayload(&temp, arrayVal);
860 if(rootPayload == NULL)
867 curPayload->next = temp;
868 curPayload = curPayload->next;
872 err = err || cbor_value_advance(arrayVal);
875 OCRepPayloadDestroy(rootPayload);
876 OC_LOG_V(ERROR, TAG, PCF("CBOR error in ParseRepPayload"));
877 return OC_STACK_MALFORMED_RESPONSE;
881 *outPayload = (OCPayload*)rootPayload;
886 static OCStackResult OCParsePresencePayload(OCPayload** outPayload, CborValue* arrayVal)
889 if(cbor_value_is_map(arrayVal))
893 OCPresenceTrigger trigger = OC_PRESENCE_TRIGGER_CREATE;
894 char* tempStr = NULL;
899 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_NONCE, &curVal);
900 err = err || cbor_value_get_uint64(&curVal, &seqNum);
903 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_TTL, &curVal);
904 err = err || cbor_value_get_uint64(&curVal, &maxAge);
907 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_TRIGGER, &curVal);
908 err = err || cbor_value_dup_text_string(&curVal, &tempStr, &len, NULL);
909 trigger = convertTriggerStringToEnum(tempStr);
913 // Resource type name
914 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_RESOURCE_TYPE, &curVal);
915 if(cbor_value_is_valid(&curVal))
917 err = err || cbor_value_dup_text_string(&curVal, &tempStr, &len, NULL);
920 err = err || cbor_value_advance(arrayVal);
924 *outPayload = (OCPayload*)OCPresencePayloadCreate(seqNum, maxAge, trigger, tempStr);
930 OCPayloadDestroy(*outPayload);
931 OC_LOG_V(ERROR, TAG, PCF("CBOR error Parse Presence Payload"));
932 return OC_STACK_MALFORMED_RESPONSE;
937 return OC_STACK_NO_MEMORY;
944 OC_LOG(ERROR, TAG, PCF("Root presence node was not a map"));
945 return OC_STACK_MALFORMED_RESPONSE;