static int64_t OCConvertPlatformPayload(OCPlatformPayload* payload, uint8_t* outPayload,
size_t* size);
static int64_t OCConvertRepPayload(OCRepPayload* payload, uint8_t* outPayload, size_t* size);
+static int64_t OCConvertRepMap(CborEncoder *map, const OCRepPayload* payload);
static int64_t OCConvertPresencePayload(OCPresencePayload* payload, uint8_t* outPayload,
size_t* size);
static int64_t OCConvertSecurityPayload(OCSecurityPayload* payload, uint8_t* outPayload,
}
else
{
- err = OCConvertSingleRepPayload(array, valArray->objArray[index]);
+ err = OCConvertRepMap(array, valArray->objArray[index]);
}
break;
case OCREP_PROP_ARRAY:
return err;
}
-static int64_t OCConvertSingleRepPayload(CborEncoder* parent, const OCRepPayload* payload)
+static int64_t OCConvertRepMap(CborEncoder *map, const OCRepPayload* payload)
{
int64_t err = 0;
- CborEncoder map;
- err = err | cbor_encoder_create_map(parent, &map, CborIndefiniteLength);
-
- // Uri
- err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_HREF,
- sizeof(OC_RSRVD_HREF) - 1,
- payload->uri);
-
- // Prop Map
- // resource types, interfaces
- if(payload->types || payload->interfaces)
- {
- OC_LOG(INFO, TAG, "Payload has types or interfaces");
- err = err | cbor_encode_text_string(&map,
- OC_RSRVD_PROPERTY,
- sizeof(OC_RSRVD_PROPERTY) - 1);
- CborEncoder propMap;
- err = err | cbor_encoder_create_map(&map, &propMap, CborIndefiniteLength);
-
- if (payload->types)
- {
- char* joinedTypes = OCStringLLJoin(payload->types);
- if (joinedTypes)
- {
- err = err | cbor_encode_text_string(&propMap, OC_RSRVD_RESOURCE_TYPE,
- sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
- err = err | cbor_encode_text_string(&propMap, joinedTypes,
- strlen(joinedTypes));
- OICFree(joinedTypes);
- }
- else
- {
- return OC_STACK_NO_MEMORY;
- }
- }
- if (payload->interfaces)
- {
- char* joinedInterfaces = OCStringLLJoin(payload->interfaces);
- if (joinedInterfaces)
- {
- err = err | cbor_encode_text_string(&propMap, OC_RSRVD_INTERFACE,
- sizeof(OC_RSRVD_INTERFACE) - 1);
- err = err | cbor_encode_text_string(&propMap, joinedInterfaces,
- strlen(joinedInterfaces));
- OICFree(joinedInterfaces);
- }
- else
- {
- return OC_STACK_NO_MEMORY;
- }
- }
- err = err | cbor_encoder_close_container(&map, &propMap);
- }
+ CborEncoder repMap;
+ err = err | cbor_encoder_create_map(map, &repMap, CborIndefiniteLength);
+ err = err | OCConvertSingleRepPayload(&repMap, payload);
+ err = err | cbor_encoder_close_container(map, &repMap);
+ return err;
+}
- // Rep Map
+static int64_t OCConvertSingleRepPayload(CborEncoder* repMap, const OCRepPayload* payload)
+{
+ int64_t err = 0;
+ OCRepPayloadValue* value = payload->values;
+ while(value)
{
- CborEncoder repMap;
- err = err | cbor_encode_text_string(&map,
- OC_RSRVD_REPRESENTATION,
- sizeof(OC_RSRVD_REPRESENTATION) - 1);
- err = err | cbor_encoder_create_map(&map, &repMap, CborIndefiniteLength);
- OCRepPayloadValue* value = payload->values;
- while(value)
+ err = err | cbor_encode_text_string(repMap,
+ value->name,
+ strlen(value->name));
+ switch(value->type)
{
- err = err | cbor_encode_text_string(&repMap,
- value->name,
- strlen(value->name));
- switch(value->type)
- {
- case OCREP_PROP_NULL:
- err = err | cbor_encode_null(&repMap);
- break;
- case OCREP_PROP_INT:
- err = err | cbor_encode_int(&repMap,
- value->i);
- break;
- case OCREP_PROP_DOUBLE:
- err = err | cbor_encode_double(&repMap,
- value->d);
- break;
- case OCREP_PROP_BOOL:
- err = err | cbor_encode_boolean(&repMap,
- value->b);
- break;
- case OCREP_PROP_STRING:
- err = err | cbor_encode_text_string(&repMap,
- value->str, strlen(value->str));
- break;
- case OCREP_PROP_BYTE_STRING:
- err = err | cbor_encode_byte_string(&repMap,
- value->ocByteStr.bytes, value->ocByteStr.len);
- break;
- case OCREP_PROP_OBJECT:
- err = err | OCConvertSingleRepPayload(&repMap, value->obj);
- break;
- case OCREP_PROP_ARRAY:
- err = err | OCConvertArray(&repMap, &value->arr);
- break;
- default:
- OC_LOG_V(ERROR, TAG, "Invalid Prop type: %d",
- value->type);
- break;
- }
- value = value->next;
+ case OCREP_PROP_NULL:
+ err = err | cbor_encode_null(repMap);
+ break;
+ case OCREP_PROP_INT:
+ err = err | cbor_encode_int(repMap,
+ value->i);
+ break;
+ case OCREP_PROP_DOUBLE:
+ err = err | cbor_encode_double(repMap,
+ value->d);
+ break;
+ case OCREP_PROP_BOOL:
+ err = err | cbor_encode_boolean(repMap,
+ value->b);
+ break;
+ case OCREP_PROP_STRING:
+ err = err | cbor_encode_text_string(repMap,
+ value->str, strlen(value->str));
+ break;
+ case OCREP_PROP_BYTE_STRING:
+ err = err | cbor_encode_byte_string(repMap,
+ value->ocByteStr.bytes, value->ocByteStr.len);
+ break;
+ case OCREP_PROP_OBJECT:
+ err = err | OCConvertRepMap(repMap, value->obj);
+ break;
+ case OCREP_PROP_ARRAY:
+ err = err | OCConvertArray(repMap, &value->arr);
+ break;
+ default:
+ OC_LOG_V(ERROR, TAG, "Invalid Prop type: %d",
+ value->type);
+ break;
}
-
- err = err | cbor_encoder_close_container(&map, &repMap);
+ value = value->next;
}
- // Close Map
- err = err | cbor_encoder_close_container(parent, &map);
-
return err;
}
int64_t err = 0;
cbor_encoder_init(&encoder, outPayload, *size, 0);
- CborEncoder rootArray;
- err = err | cbor_encoder_create_array(&encoder, &rootArray, CborIndefiniteLength);
+ CborEncoder rootMap;
+ err = err | cbor_encoder_create_map(&encoder, &rootMap, CborIndefiniteLength);
+
+ if (payload->types)
+ {
+ OC_LOG(INFO, TAG, "Payload has types or interfaces");
+ char* joinedTypes = OCStringLLJoin(payload->types);
+ if (joinedTypes)
+ {
+ err = err | cbor_encode_text_string(&rootMap, OC_RSRVD_RESOURCE_TYPE,
+ sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
+ err = err | cbor_encode_text_string(&rootMap, joinedTypes,
+ strlen(joinedTypes));
+ OICFree(joinedTypes);
+ }
+ else
+ {
+ return OC_STACK_NO_MEMORY;
+ }
+ }
+ if (payload->interfaces)
+ {
+ char* joinedInterfaces = OCStringLLJoin(payload->interfaces);
+ if (joinedInterfaces)
+ {
+ err = err | cbor_encode_text_string(&rootMap, OC_RSRVD_INTERFACE,
+ sizeof(OC_RSRVD_INTERFACE) - 1);
+ err = err | cbor_encode_text_string(&rootMap, joinedInterfaces,
+ strlen(joinedInterfaces));
+ OICFree(joinedInterfaces);
+ }
+ else
+ {
+ return OC_STACK_NO_MEMORY;
+ }
+ }
while(payload != NULL && (err == 0 || err == CborErrorOutOfMemory))
{
- err = err | OCConvertSingleRepPayload(&rootArray, payload);
+ err = err | OCConvertSingleRepPayload(&rootMap, payload);
payload = payload->next;
}
// Close main array
- err = err | cbor_encoder_close_container(&encoder, &rootArray);
+ err = err | cbor_encoder_close_container(&encoder, &rootMap);
return checkError(err, &encoder, outPayload, size);
}
return OC_STACK_ERROR;
}
- if(!cbor_value_is_array(&rootValue))
- {
- OC_LOG_V(ERROR, TAG, "CBOR payload root object is not an array :%x", rootValue.type);
- return OC_STACK_MALFORMED_RESPONSE;
- }
-
- CborValue arrayValue;
- // enter the array
- err = err || cbor_value_enter_container(&rootValue, &arrayValue);
-
- if(err)
- {
- OC_LOG_V(ERROR, TAG, "CBOR payload parse failed :%d", err);
- return OC_STACK_MALFORMED_RESPONSE;
- }
-
OCStackResult result = OC_STACK_ERROR;
switch(payloadType)
{
case PAYLOAD_TYPE_DISCOVERY:
- result = OCParseDiscoveryPayload(outPayload, &arrayValue);
+ result = OCParseDiscoveryPayload(outPayload, &rootValue);
break;
case PAYLOAD_TYPE_DEVICE:
- result = OCParseDevicePayload(outPayload, &arrayValue);
+ result = OCParseDevicePayload(outPayload, &rootValue);
break;
case PAYLOAD_TYPE_PLATFORM:
- result = OCParsePlatformPayload(outPayload, &arrayValue);
+ result = OCParsePlatformPayload(outPayload, &rootValue);
break;
case PAYLOAD_TYPE_REPRESENTATION:
- result = OCParseRepPayload(outPayload, &arrayValue);
+ result = OCParseRepPayload(outPayload, &rootValue);
break;
case PAYLOAD_TYPE_PRESENCE:
- result = OCParsePresencePayload(outPayload, &arrayValue);
+ result = OCParsePresencePayload(outPayload, &rootValue);
break;
case PAYLOAD_TYPE_SECURITY:
- result = OCParseSecurityPayload(outPayload, &arrayValue);
+ result = OCParseSecurityPayload(outPayload, &rootValue);
break;
case PAYLOAD_TYPE_RD:
- result = OCRDCborToPayload(&arrayValue, outPayload);
+ result = OCRDCborToPayload(&rootValue, outPayload);
break;
default:
OC_LOG_V(ERROR, TAG, "ParsePayload Type default: %d", payloadType);
break;
}
- if(result == OC_STACK_OK)
- {
- err = err || cbor_value_leave_container(&rootValue, &arrayValue);
- if(err != CborNoError)
- {
- return OC_STACK_MALFORMED_RESPONSE;
- }
- }
- else
+ if(result != OC_STACK_OK)
{
OC_LOG_V(INFO, TAG, "Finished parse payload, result is %d", result);
}
void OCFreeOCStringLL(OCStringLL* ll);
-static OCStackResult OCParseSecurityPayload(OCPayload** outPayload, CborValue* arrayVal)
+static OCStackResult OCParseSecurityPayload(OCPayload** outPayload, CborValue* rootValue)
{
if (!outPayload)
{
bool err = false;
char * securityData = NULL;
+ CborValue arrayVal;
+ err = err || cbor_value_enter_container(rootValue, &arrayVal);
- if(cbor_value_is_map(arrayVal))
+ if(cbor_value_is_map(&arrayVal))
{
CborValue curVal;
- err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_REPRESENTATION, &curVal);
+ err = err || cbor_value_map_find_value(&arrayVal, OC_RSRVD_REPRESENTATION, &curVal);
if(cbor_value_is_valid(&curVal))
{
return OC_STACK_MALFORMED_RESPONSE;
}
- err = err || cbor_value_advance(arrayVal);
+ err = err || cbor_value_advance(&arrayVal);
+ err = err || cbor_value_leave_container(rootValue, &arrayVal);
if(err)
{
return str;
}
-static OCStackResult OCParseDiscoveryPayload(OCPayload** outPayload, CborValue* arrayVal)
+static OCStackResult OCParseDiscoveryPayload(OCPayload** outPayload, CborValue* rootValue)
{
if (!outPayload)
{
return OC_STACK_NO_MEMORY;
}
- if (cbor_value_is_array(arrayVal))
+ CborValue arrayVal;
+ err = err || cbor_value_enter_container(rootValue, &arrayVal);
+
+ if (cbor_value_is_array(&arrayVal))
{
OCLinksPayload *linksPayload = NULL;
OCTagsPayload *tagsPayload = NULL;
- while (cbor_value_is_container(arrayVal))
+ while (cbor_value_is_container(&arrayVal))
{
linksPayload = NULL;
tagsPayload = NULL;
CborValue colResources;
- CborError cborFindResult = cbor_value_enter_container(arrayVal, &colResources);
+ CborError cborFindResult = cbor_value_enter_container(&arrayVal, &colResources);
if (CborNoError != cborFindResult)
{
goto cbor_error;
OCDiscoveryPayloadDestroy(out);
return OC_STACK_NO_MEMORY;
}
- if (CborNoError != cbor_value_advance(arrayVal))
+ if (CborNoError != cbor_value_advance(&arrayVal))
{
OC_LOG(ERROR, TAG, "Cbor value advanced failed.");
goto cbor_error;
}
}
}
- if (cbor_value_is_map(arrayVal))
+ if (cbor_value_is_map(&arrayVal))
{
size_t resourceCount = 0;
- while (cbor_value_is_map(arrayVal))
+ while (cbor_value_is_map(&arrayVal))
{
resource = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
if(!resource)
}
CborValue curVal;
// DI
- err = cbor_value_map_find_value(arrayVal, OC_RSRVD_DEVICE_ID, &curVal);
+ err = cbor_value_map_find_value(&arrayVal, OC_RSRVD_DEVICE_ID, &curVal);
if (CborNoError != err)
{
OC_LOG(ERROR, TAG, "Cbor find value failed.");
// Links TAG
{
CborValue linkArray;
- err = cbor_value_map_find_value(arrayVal, OC_RSRVD_LINKS, &linkArray);
+ err = cbor_value_map_find_value(&arrayVal, OC_RSRVD_LINKS, &linkArray);
if (CborNoError != err)
{
OC_LOG(ERROR, TAG, "Cbor links finding failed.");
}
}
}
- err = cbor_value_advance(arrayVal);
+ err = cbor_value_advance(&arrayVal);
if (CborNoError != err)
{
OC_LOG(ERROR, TAG, "Cbor advance value failed.");
OCDiscoveryPayloadAddNewResource(out, resource);
}
}
+ err = err || cbor_value_leave_container(rootValue, &arrayVal);
*outPayload = (OCPayload*)out;
return OC_STACK_OK;
return OC_STACK_MALFORMED_RESPONSE;
}
-static OCStackResult OCParseDevicePayload(OCPayload** outPayload, CborValue* arrayVal)
+static OCStackResult OCParseDevicePayload(OCPayload** outPayload, CborValue* rootValue)
{
if (!outPayload)
{
bool err = false;
- if(cbor_value_is_map(arrayVal))
+ CborValue arrayVal;
+ err = err || cbor_value_enter_container(rootValue, &arrayVal);
+
+ if(cbor_value_is_map(&arrayVal))
{
char* uri = NULL;
uint8_t* sid = NULL;
char* specVer = NULL;
char* dmVer = NULL;
CborValue curVal;
- err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_HREF, &curVal);
+ err = err || cbor_value_map_find_value(&arrayVal, OC_RSRVD_HREF, &curVal);
size_t len;
err = err || cbor_value_dup_text_string(&curVal, &uri, &len, NULL);
// Representation
{
- err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_REPRESENTATION, &curVal);
+ err = err || cbor_value_map_find_value(&arrayVal, OC_RSRVD_REPRESENTATION, &curVal);
CborValue repVal;
// Device ID
}
}
- err = err || cbor_value_advance(arrayVal);
+ err = err || cbor_value_advance(&arrayVal);
+ err = err || cbor_value_leave_container(rootValue, &arrayVal);
if(err)
{
}
-static OCStackResult OCParsePlatformPayload(OCPayload** outPayload, CborValue* arrayVal)
+static OCStackResult OCParsePlatformPayload(OCPayload** outPayload, CborValue* rootValue)
{
if (!outPayload)
{
bool err = false;
- if(cbor_value_is_map(arrayVal))
+ CborValue arrayVal;
+ err = err || cbor_value_enter_container(rootValue, &arrayVal);
+
+ if(cbor_value_is_map(&arrayVal))
{
char* uri = NULL;
OCPlatformInfo info = {0};
CborValue curVal;
- err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_HREF, &curVal);
+ err = err || cbor_value_map_find_value(&arrayVal, OC_RSRVD_HREF, &curVal);
size_t len;
err = err || cbor_value_dup_text_string(&curVal, &uri, &len, NULL);
// Representation
{
- err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_REPRESENTATION, &curVal);
+ err = err || cbor_value_map_find_value(&arrayVal, OC_RSRVD_REPRESENTATION, &curVal);
CborValue repVal;
// Platform ID
}
}
- err = err || cbor_value_advance(arrayVal);
+ err = err || cbor_value_advance(&arrayVal);
+ err = err || cbor_value_leave_container(rootValue, &arrayVal);
if(err)
{
while (!err && i < dimensions[0] && cbor_value_is_valid(&insideArray))
{
+ bool noAdvance = false;
if (cbor_value_get_type(&insideArray) != CborNullType)
{
switch (type)
err = err || OCParseSingleRepPayload(&tempPl, &insideArray);
((OCRepPayload**)targetArray)[i] = tempPl;
tempPl = NULL;
+ noAdvance = true;
}
else
{
}
}
++i;
- err = err || cbor_value_advance(&insideArray);
+ if (!noAdvance && cbor_value_is_valid(&insideArray))
+ {
+ err = err || cbor_value_advance(&insideArray);
+ }
}
return err;
return err;
}
-static bool OCParseSingleRepPayload(OCRepPayload** outPayload, CborValue* repParent)
+static bool OCParseSingleRepPayload(OCRepPayload** outPayload, CborValue *objMap)
{
if (!outPayload)
{
return false;
}
- *outPayload = OCRepPayloadCreate();
- OCRepPayload* curPayload = *outPayload;
bool err = false;
- if(!*outPayload)
- {
- return CborErrorOutOfMemory;
- }
- size_t len;
- CborValue curVal;
- err = err || cbor_value_map_find_value(repParent, OC_RSRVD_HREF, &curVal);
- if(cbor_value_is_valid(&curVal))
+ if (cbor_value_is_map(objMap))
{
- err = err || cbor_value_dup_text_string(&curVal, &curPayload->uri, &len,
- NULL);
- }
-
- err = err || cbor_value_map_find_value(repParent, OC_RSRVD_PROPERTY, &curVal);
- if(cbor_value_is_valid(&curVal))
- {
- CborValue insidePropValue = {0};
- err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_RESOURCE_TYPE,
- &insidePropValue);
-
- if(cbor_value_is_text_string(&insidePropValue))
+ if (!*outPayload)
{
- char* allRt = NULL;
- err = err || cbor_value_dup_text_string(&insidePropValue, &allRt, &len, NULL);
-
- char* savePtr;
-
- if (allRt)
+ *outPayload = OCRepPayloadCreate();
+ if(!*outPayload)
{
- char* curPtr = strtok_r(allRt, " ", &savePtr);
-
- while (curPtr)
- {
- char* trimmed = InPlaceStringTrim(curPtr);
- if (trimmed[0] != '\0')
- {
- OCRepPayloadAddResourceType(curPayload, curPtr);
- }
- curPtr = strtok_r(NULL, " ", &savePtr);
- }
+ return CborErrorOutOfMemory;
}
- OICFree(allRt);
}
- err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_INTERFACE, &insidePropValue);
-
- if(cbor_value_is_text_string(&insidePropValue))
- {
- char* allIf = NULL;
- err = err || cbor_value_dup_text_string(&insidePropValue, &allIf, &len, NULL);
-
- char* savePtr;
-
- if (allIf)
- {
- char* curPtr = strtok_r(allIf, " ", &savePtr);
+ OCRepPayload* curPayload = *outPayload;
- while (curPtr)
- {
- char* trimmed = InPlaceStringTrim(curPtr);
- if (trimmed[0] != '\0')
- {
- OCRepPayloadAddInterface(curPayload, curPtr);
- }
- curPtr = strtok_r(NULL, " ", &savePtr);
- }
- }
- OICFree(allIf);
- }
- }
-
- err = err || cbor_value_map_find_value(repParent, OC_RSRVD_REPRESENTATION, &curVal);
- if(cbor_value_is_map(&curVal))
- {
+ size_t len;
CborValue repMap;
- err = err || cbor_value_enter_container(&curVal, &repMap);
+ err = err || cbor_value_enter_container(objMap, &repMap);
while(!err && cbor_value_is_valid(&repMap))
{
char* name;
err = err || cbor_value_dup_text_string(&repMap, &name, &len, NULL);
-
err = err || cbor_value_advance(&repMap);
- int64_t intval = 0;
- bool boolval = false;
- char* strval = NULL;
- uint8_t* bytestrval = NULL;
- double doubleval = 0;
- OCRepPayload* pl;
-
- switch(cbor_value_get_type(&repMap))
+ CborType type = cbor_value_get_type(&repMap);
+ switch(type)
{
case CborNullType:
err = !OCRepPayloadSetNull(curPayload, name);
break;
case CborIntegerType:
- err = err || cbor_value_get_int64(&repMap, &intval);
- if (!err)
{
- err = !OCRepPayloadSetPropInt(curPayload, name, intval);
+ int64_t intval = 0;
+ err = err || cbor_value_get_int64(&repMap, &intval);
+ if (!err)
+ {
+ err = !OCRepPayloadSetPropInt(curPayload, name, intval);
+ }
}
break;
case CborDoubleType:
- err = err || cbor_value_get_double(&repMap, &doubleval);
- if (!err)
{
- err = !OCRepPayloadSetPropDouble(curPayload, name, doubleval);
+ double doubleval = 0;
+ err = err || cbor_value_get_double(&repMap, &doubleval);
+ if (!err)
+ {
+ err = !OCRepPayloadSetPropDouble(curPayload, name, doubleval);
+ }
}
break;
case CborBooleanType:
- err = err || cbor_value_get_boolean(&repMap, &boolval);
- if (!err)
{
- err = !OCRepPayloadSetPropBool(curPayload, name, boolval);
+ bool boolval = false;
+ err = err || cbor_value_get_boolean(&repMap, &boolval);
+ if (!err)
+ {
+ err = !OCRepPayloadSetPropBool(curPayload, name, boolval);
+ }
}
break;
case CborTextStringType:
- err = err || cbor_value_dup_text_string(&repMap, &strval, &len, NULL);
- if (!err)
{
- err = !OCRepPayloadSetPropStringAsOwner(curPayload, name, strval);
+ char* strval = NULL;
+ err = err || cbor_value_dup_text_string(&repMap, &strval, &len, NULL);
+ if (!err)
+ {
+ err = !OCRepPayloadSetPropStringAsOwner(curPayload, name, strval);
+ }
}
break;
case CborByteStringType:
- err = err || cbor_value_dup_byte_string(&repMap, &bytestrval, &len, NULL);
- if (!err)
{
- OCByteString tmp = {.bytes = bytestrval, .len = len};
- err = !OCRepPayloadSetPropByteStringAsOwner(curPayload, name, &tmp);
+ uint8_t* bytestrval = NULL;
+ err = err || cbor_value_dup_byte_string(&repMap, &bytestrval, &len, NULL);
+ if (!err)
+ {
+ OCByteString tmp = {.bytes = bytestrval, .len = len};
+ err = !OCRepPayloadSetPropByteStringAsOwner(curPayload, name, &tmp);
+ }
}
break;
case CborMapType:
- err = err || OCParseSingleRepPayload(&pl, &repMap);
- if (!err)
{
- err = !OCRepPayloadSetPropObjectAsOwner(curPayload, name, pl);
+ OCRepPayload *pl = NULL;
+ err = err || OCParseSingleRepPayload(&pl, &repMap);
+ if (!err)
+ {
+ err = !OCRepPayloadSetPropObjectAsOwner(curPayload, name, pl);
+ }
}
break;
case CborArrayType:
err = true;
}
- err = err || cbor_value_advance(&repMap);
+ if (type != CborMapType && cbor_value_is_valid(&repMap))
+ {
+ err = err || cbor_value_advance(&repMap);
+ }
OICFree(name);
}
- err = err || cbor_value_leave_container(&curVal, &repMap);
- }
- if(err)
- {
- OCRepPayloadDestroy(*outPayload);
- *outPayload = NULL;
+ err = err || cbor_value_leave_container(objMap, &repMap);
+
+ if(err)
+ {
+ OCRepPayloadDestroy(*outPayload);
+ *outPayload = NULL;
+ }
}
return err;
}
-static OCStackResult OCParseRepPayload(OCPayload** outPayload, CborValue* arrayVal)
+
+static OCStackResult OCParseRepPayload(OCPayload** outPayload, CborValue* rootMap)
{
if (!outPayload)
{
return OC_STACK_INVALID_PARAM;
}
+ OCRepPayload *rootPayload = NULL;
+ OCRepPayload *curPayload = NULL;
+ OCRepPayload *temp = OCRepPayloadCreate();
+ if (!temp)
+ {
+ return OC_STACK_NO_MEMORY;
+ }
+
+ CborValue curVal;
bool err = false;
+ size_t len;
+ err = err || cbor_value_map_find_value(rootMap, OC_RSRVD_RESOURCE_TYPE, &curVal);
+ if(cbor_value_is_text_string(&curVal))
+ {
+ char* allRt = NULL;
+ err = err || cbor_value_dup_text_string(&curVal, &allRt, &len, NULL);
+ if (allRt)
+ {
+ char* savePtr;
+ char* curPtr = strtok_r(allRt, " ", &savePtr);
+ while (curPtr)
+ {
+ char* trimmed = InPlaceStringTrim(curPtr);
+ if (trimmed[0] != '\0')
+ {
+ OCRepPayloadAddResourceType(temp, curPtr);
+ }
+ curPtr = strtok_r(NULL, " ", &savePtr);
+ }
+ }
+ OICFree(allRt);
+ }
- OCRepPayload* rootPayload = NULL;
- OCRepPayload* curPayload = NULL;
- OCRepPayload* temp = NULL;
- while(!err && cbor_value_is_map(arrayVal))
+ err = err || cbor_value_map_find_value(rootMap, OC_RSRVD_INTERFACE, &curVal);
+ if(cbor_value_is_text_string(&curVal))
{
- err = err || OCParseSingleRepPayload(&temp, arrayVal);
+ char* allIf = NULL;
+ err = err || cbor_value_dup_text_string(&curVal, &allIf, &len, NULL);
+ if (allIf)
+ {
+ char* savePtr;
+ char* curPtr = strtok_r(allIf, " ", &savePtr);
+ while (curPtr)
+ {
+ char* trimmed = InPlaceStringTrim(curPtr);
+ if (trimmed[0] != '\0')
+ {
+ OCRepPayloadAddInterface(temp, curPtr);
+ }
+ curPtr = strtok_r(NULL, " ", &savePtr);
+ }
+ }
+ OICFree(allIf);
+ }
+
+ while (!err && cbor_value_is_map(rootMap))
+ {
+ err = err || OCParseSingleRepPayload(&temp, rootMap);
if(rootPayload == NULL)
{
curPayload = curPayload->next;
}
+ if (cbor_value_is_valid(rootMap))
+ {
+ err = err || cbor_value_advance(rootMap);
+ }
- err = err || cbor_value_advance(arrayVal);
if(err)
{
OCRepPayloadDestroy(rootPayload);
return OC_STACK_OK;
}
-static OCStackResult OCParsePresencePayload(OCPayload** outPayload, CborValue* arrayVal)
+static OCStackResult OCParsePresencePayload(OCPayload** outPayload, CborValue* rootValue)
{
if (!outPayload)
{
}
bool err = false;
- if(cbor_value_is_map(arrayVal))
+ CborValue arrayVal;
+ err = err || cbor_value_enter_container(rootValue, &arrayVal);
+
+ if(cbor_value_is_map(&arrayVal))
{
uint64_t seqNum = 0;
uint64_t maxAge = 0;
CborValue curVal;
// Sequence Number
- err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_NONCE, &curVal);
+ err = err || cbor_value_map_find_value(&arrayVal, OC_RSRVD_NONCE, &curVal);
err = err || cbor_value_get_uint64(&curVal, &seqNum);
// Max Age
- err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_TTL, &curVal);
+ err = err || cbor_value_map_find_value(&arrayVal, OC_RSRVD_TTL, &curVal);
err = err || cbor_value_get_uint64(&curVal, &maxAge);
// Trigger
- err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_TRIGGER, &curVal);
+ err = err || cbor_value_map_find_value(&arrayVal, OC_RSRVD_TRIGGER, &curVal);
err = err || cbor_value_dup_text_string(&curVal, &tempStr, &len, NULL);
trigger = convertTriggerStringToEnum(tempStr);
OICFree(tempStr);
tempStr = NULL;
// Resource type name
- err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_RESOURCE_TYPE, &curVal);
+ err = err || cbor_value_map_find_value(&arrayVal, OC_RSRVD_RESOURCE_TYPE, &curVal);
if(cbor_value_is_valid(&curVal))
{
err = err || cbor_value_dup_text_string(&curVal, &tempStr, &len, NULL);
}
- err = err || cbor_value_advance(arrayVal);
+ err = err || cbor_value_advance(&arrayVal);
+ err = err || cbor_value_leave_container(rootValue, &arrayVal);
if(!err)
{