// Arbitrarily chosen size that seems to contain the majority of packages
#define INIT_SIZE (255)
-#define CBOR_ROOT_ARRAY_LENGTH 1
+// CBOR Array Length
+#define DISCOVERY_CBOR_ARRAY_LEN 1
+// CBOR Res Map Length
+#define DISCOVERY_CBOR_RES_MAP_LEN 2
+// CBOR Links Map Length
+#define DISCOVERY_CBOR_LINKS_MAP_LEN 4
// Functions all return either a CborError, or a negative version of the OC_STACK return values
static int64_t OCConvertPayloadHelper(OCPayload* payload, uint8_t* outPayload, size_t* size);
if (payload->collectionResources)
{
CborError cborEncoderResult;
- cborEncoderResult = cbor_encoder_create_array(&encoder, &rootArray, CBOR_ROOT_ARRAY_LENGTH);
+ cborEncoderResult = cbor_encoder_create_array(&encoder, &rootArray, DISCOVERY_CBOR_ARRAY_LEN);
if (CborNoError != cborEncoderResult)
{
OC_LOG(ERROR, TAG, "Failed creating root array.");
else if (payload->resources)
{
size_t resourceCount = OCDiscoveryPayloadGetResourceCount(payload);
- err = err || cbor_encoder_create_array(&encoder, &rootArray, resourceCount);
+ err = err | cbor_encoder_create_array(&encoder, &rootArray, resourceCount);
for(size_t i = 0; i < resourceCount; ++i)
{
CborEncoder map;
OCResourcePayload* resource = OCDiscoveryPayloadGetResource(payload, i);
-
if(!resource)
{
OICFree(outPayload);
return OC_STACK_INVALID_PARAM;
}
- err = err | cbor_encoder_create_map(&rootArray, &map, 3);
- // Uri
- err = err | AddTextStringToMap(&map, OC_RSRVD_HREF,
- sizeof(OC_RSRVD_HREF) - 1,
- resource->uri);
+ err = err | cbor_encoder_create_map(&rootArray, &map, DISCOVERY_CBOR_RES_MAP_LEN);
- // Server ID
- err = err | cbor_encode_text_string(&map, OC_RSRVD_SERVER_INSTANCE_ID,
- sizeof(OC_RSRVD_SERVER_INSTANCE_ID) - 1);
+ // Device ID
+ err = err | cbor_encode_text_string(&map, OC_RSRVD_DEVICE_ID,
+ sizeof(OC_RSRVD_DEVICE_ID) - 1);
err = err | cbor_encode_byte_string(&map, resource->sid, UUID_SIZE);
- // Prop Tag
{
- CborEncoder propMap;
- err = err | cbor_encode_text_string(&map, OC_RSRVD_PROPERTY,
- sizeof(OC_RSRVD_PROPERTY) -1 );
- err = err | cbor_encoder_create_map(&map, &propMap, 3);
- // Resource Type
- if (resource->types)
+ CborEncoder linkArray;
+ err = err | cbor_encode_text_string(&map, OC_RSRVD_LINKS, sizeof(OC_RSRVD_LINKS) -1);
+ err = err | cbor_encoder_create_array(&map, &linkArray, CborIndefiniteLength);
+
+ // Link Map
{
- char* joinedTypes = OCStringLLJoin(resource->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
+ CborEncoder linkMap;
+ err = err | cbor_encoder_create_map(&linkArray, &linkMap, DISCOVERY_CBOR_LINKS_MAP_LEN);
+
+ // Uri
+ err = err | AddTextStringToMap(&linkMap, OC_RSRVD_HREF,
+ sizeof(OC_RSRVD_HREF) - 1,
+ resource->uri);
+ // Resource Type
+ if (resource->types)
{
- return OC_STACK_NO_MEMORY;
+ char* joinedTypes = OCStringLLJoin(resource->types);
+ if (joinedTypes)
+ {
+ err = err | cbor_encode_text_string(&linkMap, OC_RSRVD_RESOURCE_TYPE,
+ sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
+ err = err | cbor_encode_text_string(&linkMap, joinedTypes,
+ strlen(joinedTypes));
+ OICFree(joinedTypes);
+ }
+ else
+ {
+ return OC_STACK_NO_MEMORY;
+ }
}
- }
- // Interface Types
- if (resource->interfaces)
- {
- char* joinedInterfaces = OCStringLLJoin(resource->interfaces);
- if (joinedInterfaces)
+ // Interface Types
+ if (resource->interfaces)
{
- 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);
+ char* joinedInterfaces = OCStringLLJoin(resource->interfaces);
+ if (joinedInterfaces)
+ {
+ err = err | cbor_encode_text_string(&linkMap, OC_RSRVD_INTERFACE,
+ sizeof(OC_RSRVD_INTERFACE) - 1);
+ err = err | cbor_encode_text_string(&linkMap, joinedInterfaces,
+ strlen(joinedInterfaces));
+ OICFree(joinedInterfaces);
+ }
+ else
+ {
+ return OC_STACK_NO_MEMORY;
+ }
}
- else
+ // Policy
{
- return OC_STACK_NO_MEMORY;
- }
- }
- // Policy
- {
- CborEncoder policyMap;
- err = err || cbor_encode_text_string(&propMap, OC_RSRVD_POLICY,
- sizeof(OC_RSRVD_POLICY) - 1);
- err = err || cbor_encoder_create_map(&propMap, &policyMap, CborIndefiniteLength);
+ CborEncoder policyMap;
+ err = err | cbor_encode_text_string(&linkMap, OC_RSRVD_POLICY,
+ sizeof(OC_RSRVD_POLICY) - 1);
+ err = err | cbor_encoder_create_map(&linkMap, &policyMap, CborIndefiniteLength);
- // Bitmap
- err = err || cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP,
- sizeof(OC_RSRVD_BITMAP) - 1);
- err = err || cbor_encode_uint(&policyMap, resource->bitmap);
+ // Bitmap
+ err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP,
+ sizeof(OC_RSRVD_BITMAP) - 1);
+ err = err | cbor_encode_uint(&policyMap, resource->bitmap);
- if(resource->secure)
- {
- err = err || cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE,
- sizeof(OC_RSRVD_SECURE) - 1);
- err = err || cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE);
-
- if(resource->port != 0)
+ if(resource->secure)
{
- err = err || cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT,
- sizeof(OC_RSRVD_HOSTING_PORT) - 1);
- err = err || cbor_encode_uint(&policyMap, resource->port);
+ err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE,
+ sizeof(OC_RSRVD_SECURE) - 1);
+ err = err | cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE);
+
+ if(resource->port != 0)
+ {
+ err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT,
+ sizeof(OC_RSRVD_HOSTING_PORT) - 1);
+ err = err | cbor_encode_uint(&policyMap, resource->port);
+ }
}
+
+ err = err | cbor_encoder_close_container(&linkMap, &policyMap);
}
- err = err || cbor_encoder_close_container(&propMap, &policyMap);
+ // Close
+ err = err | cbor_encoder_close_container(&linkArray, &linkMap);
}
- // Close
- err = err || cbor_encoder_close_container(&map, &propMap);
+ err = err | cbor_encoder_close_container(&map, &linkArray);
}
- // Close Item
- err = err || cbor_encoder_close_container(&rootArray, &map);
+ err = err | cbor_encoder_close_container(&rootArray, &map);
}
// Close main array
err = err | cbor_encoder_close_container(&encoder, &rootArray);
CborValue rootValue;
bool err = false;
- OC_LOG_V(INFO, TAG, "CBOR Parsing size: %d %d", payloadSize, payloadType);
+ OC_LOG_V(INFO, TAG, "CBOR Parsing size: %d", payloadSize);
if((err = cbor_parser_init(payload, payloadSize, 0, &parser, &rootValue)) != false)
{
OC_LOG_V(ERROR, TAG, "CBOR Parser init failed: %d", err);
}
bool err = false;
+ OCResourcePayload* resource = NULL;
OCDiscoveryPayload* out = OCDiscoveryPayloadCreate();
-
if(!out)
{
return OC_STACK_NO_MEMORY;
if (cbor_value_is_map(arrayVal))
{
size_t resourceCount = 0;
- while(!err &&
- cbor_value_is_map(arrayVal))
+ while (cbor_value_is_map(arrayVal))
{
- OCResourcePayload* resource = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
+ resource = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
if(!resource)
{
OC_LOG(ERROR, TAG, "Memory allocation failed");
return OC_STACK_NO_MEMORY;
}
CborValue curVal;
-
- // Uri
- err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_HREF, &curVal);
+ // DI
+ err = cbor_value_map_find_value(arrayVal, OC_RSRVD_DEVICE_ID, &curVal);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor find value failed.");
+ goto malformed_cbor;
+ }
size_t len;
- err = err || cbor_value_dup_text_string(&curVal, &(resource->uri), &len, NULL);
-
- // SID
- err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_SERVER_INSTANCE_ID, &curVal);
- err = err || cbor_value_dup_byte_string(&curVal, &(resource->sid), &len, NULL);
-
- // Prop Tag
+ err = cbor_value_dup_byte_string(&curVal, &(resource->sid), &len, NULL);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor di finding failed.");
+ goto malformed_cbor;
+ }
+ // Links TAG
{
- err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_PROPERTY, &curVal);
+ CborValue linkArray;
+ err = cbor_value_map_find_value(arrayVal, OC_RSRVD_LINKS, &linkArray);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor links finding failed.");
+ goto malformed_cbor;
+ }
+ CborValue linkMap;
+ err = cbor_value_enter_container(&linkArray, &linkMap);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor entering map failed.");
+ goto malformed_cbor;
+ }
+ // Uri
+ err = cbor_value_map_find_value(&linkMap, OC_RSRVD_HREF, &curVal);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding href type failed.");
+ goto malformed_cbor;
+ }
+ err = cbor_value_dup_text_string(&curVal, &(resource->uri), &len, NULL);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding href value failed.");
+ goto malformed_cbor;
+ }
// ResourceTypes
CborValue rtVal;
- err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_RESOURCE_TYPE, &rtVal);
-
- if (!err && cbor_value_is_text_string(&rtVal))
+ err = cbor_value_map_find_value(&linkMap, OC_RSRVD_RESOURCE_TYPE, &rtVal);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding rt type failed.");
+ goto malformed_cbor;
+ }
+ if (cbor_value_is_text_string(&rtVal))
{
char* input = NULL;
char* savePtr;
- err = err || cbor_value_dup_text_string(&rtVal, &input, &len, NULL);
-
+ err = cbor_value_dup_text_string(&rtVal, &input, &len, NULL);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding rt value failed.");
+ goto malformed_cbor;
+ }
if (input)
{
char* curPtr = strtok_r(input, " ", &savePtr);
// Interface Types
CborValue ifVal;
- err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_INTERFACE, &ifVal);
-
+ err = cbor_value_map_find_value(&linkMap, OC_RSRVD_INTERFACE, &ifVal);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding if type failed.");
+ goto malformed_cbor;
+ }
if (!err && cbor_value_is_text_string(&ifVal))
{
char* input = NULL;
char* savePtr;
- err = err || cbor_value_dup_text_string(&ifVal, &input, &len, NULL);
-
+ err = cbor_value_dup_text_string(&ifVal, &input, &len, NULL);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding if value failed.");
+ goto malformed_cbor;
+ }
if (input)
{
char* curPtr = strtok_r(input, " ", &savePtr);
OICFree(input);
}
}
-
// Policy
{
CborValue policyMap;
- err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_POLICY, &policyMap);
-
+ err = cbor_value_map_find_value(&linkMap, OC_RSRVD_POLICY, &policyMap);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding policy type failed.");
+ goto malformed_cbor;
+ }
// Bitmap
CborValue val;
- err = err || cbor_value_map_find_value(&policyMap, OC_RSRVD_BITMAP, &val);
+ err = cbor_value_map_find_value(&policyMap, OC_RSRVD_BITMAP, &val);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding bitmap type failed.");
+ goto malformed_cbor;
+ }
uint64_t temp = 0;
- err = err || cbor_value_get_uint64(&val, &temp);
+ err = cbor_value_get_uint64(&val, &temp);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding bitmap value failed.");
+ goto malformed_cbor;
+ }
resource->bitmap = (uint8_t)temp;
// Secure Flag
- err = err || cbor_value_map_find_value(&policyMap, OC_RSRVD_SECURE, &val);
+ err = cbor_value_map_find_value(&policyMap, OC_RSRVD_SECURE, &val);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding secure type failed.");
+ goto malformed_cbor;
+ }
if(cbor_value_is_valid(&val))
{
- err = err || cbor_value_get_boolean(&val, &(resource->secure));
+ err = cbor_value_get_boolean(&val, &(resource->secure));
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding secure value failed.");
+ goto malformed_cbor;
+ }
// Port
CborValue port;
- err = err || cbor_value_map_find_value(&policyMap, OC_RSRVD_HOSTING_PORT,
+ err = cbor_value_map_find_value(&policyMap, OC_RSRVD_HOSTING_PORT,
&port);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding port type failed.");
+ goto malformed_cbor;
+ }
if(cbor_value_is_valid(&port))
{
- err = err || cbor_value_get_uint64(&port, &temp);
+ err = cbor_value_get_uint64(&port, &temp);
+ if (CborNoError != err)
+ {
+ OC_LOG(ERROR, TAG, "Cbor finding port value failed.");
+ goto malformed_cbor;
+ }
resource->port = (uint16_t)temp;
}
}
}
}
-
- err = err || cbor_value_advance(arrayVal);
- if(err)
+ err = cbor_value_advance(arrayVal);
+ if (CborNoError != err)
{
- OICFree(resource->uri);
- OICFree(resource->sid);
- OCFreeOCStringLL(resource->types);
- OCFreeOCStringLL(resource->interfaces);
- OICFree(resource);
- OCDiscoveryPayloadDestroy(out);
- OC_LOG_V(ERROR, TAG, "CBOR in error condition: %d", err);
- return OC_STACK_MALFORMED_RESPONSE;
+ OC_LOG(ERROR, TAG, "Cbor advance value failed.");
+ goto malformed_cbor;
}
++resourceCount;
OCDiscoveryPayloadAddNewResource(out, resource);
}
}
- if(err)
- {
- OCDiscoveryPayloadDestroy(out);
- return OC_STACK_MALFORMED_RESPONSE;
- }
- else
- {
- *outPayload = (OCPayload*)out;
- return OC_STACK_OK;
- }
+ *outPayload = (OCPayload*)out;
+ return OC_STACK_OK;
+
+malformed_cbor:
+ OICFree(resource->uri);
+ OICFree(resource->sid);
+ OCFreeOCStringLL(resource->types);
+ OCFreeOCStringLL(resource->interfaces);
+ OICFree(resource);
+ OCDiscoveryPayloadDestroy(out);
+ return OC_STACK_MALFORMED_RESPONSE;
+
cbor_error:
OCDiscoveryCollectionPayloadDestroy(out);
return OC_STACK_MALFORMED_RESPONSE;