#include "cbor.h"
#include "rdpayload.h"
-#define TAG "OCPayloadConvert"
+#define TAG "OIC_RI_PAYLOADCONVERT"
+
// Arbitrarily chosen size that seems to contain the majority of packages
#define INIT_SIZE (255)
-// 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
+// Discovery Links Map Length.
+#define 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);
-static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload* payload, uint8_t* outPayload,
- size_t* size);
-static int64_t OCConvertDevicePayload(OCDevicePayload* payload, uint8_t* outPayload,
- size_t* size);
-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,
- size_t* size);
-static int64_t OCConvertSingleRepPayload(CborEncoder* parent, const OCRepPayload* payload);
-static int64_t OCConvertArray(CborEncoder* parent, const OCRepPayloadValueArray* valArray);
-
-static int64_t AddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
- const char* value);
-
-static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
- const char* value);
+static int64_t OCConvertPayloadHelper(OCPayload *payload, uint8_t *outPayload, size_t *size);
+static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload *payload, uint8_t *outPayload,
+ size_t *size);
+static int64_t OCConvertDevicePayload(OCDevicePayload *payload, uint8_t *outPayload,
+ size_t *size);
+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,
+ size_t *size);
+static int64_t OCConvertSingleRepPayload(CborEncoder *parent, const OCRepPayload *payload);
+static int64_t OCConvertArray(CborEncoder *parent, const OCRepPayloadValueArray *valArray);
+
+static int64_t AddTextStringToMap(CborEncoder *map, const char *key, size_t keylen,
+ const char *value);
+static int64_t ConditionalAddTextStringToMap(CborEncoder *map, const char *key, size_t keylen,
+ const char *value);
OCStackResult OCConvertPayload(OCPayload* payload, uint8_t** outPayload, size_t* size)
{
// TinyCbor Version 47a78569c0 or better on master is required for the re-allocation
// strategy to work. If you receive the following assertion error, please do a git-pull
// from the extlibs/tinycbor/tinycbor directory
- #define CborNeedsUpdating (CborErrorOutOfMemory < CborErrorDataTooLarge)
+ #define CborNeedsUpdating (((unsigned int)CborErrorOutOfMemory) < ((unsigned int)CborErrorDataTooLarge))
OC_STATIC_ASSERT(!CborNeedsUpdating, "tinycbor needs to be updated to at least 47a78569c0");
#undef CborNeedsUpdating
- if (!payload)
+
+ OCStackResult ret = OC_STACK_INVALID_PARAM;
+ int64_t err;
+ uint8_t *out = NULL;
+ size_t curSize = INIT_SIZE;
+
+ VERIFY_PARAM_NON_NULL(TAG, payload, "Input param, payload is NULL");
+ VERIFY_PARAM_NON_NULL(TAG, outPayload, "OutPayload parameter is NULL");
+ VERIFY_PARAM_NON_NULL(TAG, size, "size parameter is NULL");
+
+ OIC_LOG_V(INFO, TAG, "Converting payload of type %d", payload->type);
+ if (PAYLOAD_TYPE_SECURITY == payload->type)
{
- OC_LOG(ERROR, TAG, "Payload parameter NULL");
- return OC_STACK_INVALID_PARAM;
+ size_t securityPayloadSize = ((OCSecurityPayload *)payload)->payloadSize;
+ if (securityPayloadSize > 0)
+ {
+ out = (uint8_t *)OICCalloc(1, ((OCSecurityPayload *)payload)->payloadSize);
+ VERIFY_PARAM_NON_NULL(TAG, out, "Failed to allocate security payload");
+ }
}
-
- if (!outPayload || !size)
+ if (out == NULL)
{
- OC_LOG(ERROR, TAG, "Out parameter/s parameter NULL");
- return OC_STACK_INVALID_PARAM;
+ out = (uint8_t *)OICCalloc(1, curSize);
+ VERIFY_PARAM_NON_NULL(TAG, out, "Failed to allocate payload");
}
-
- OC_LOG_V(INFO, TAG, "Converting payload of type %d", payload->type);
-
- size_t curSize = INIT_SIZE;
- uint8_t* out = (uint8_t*)OICCalloc(1, curSize);
- int64_t err = OCConvertPayloadHelper(payload, out, &curSize);
+ err = OCConvertPayloadHelper(payload, out, &curSize);
+ ret = OC_STACK_NO_MEMORY;
if (err == CborErrorOutOfMemory)
{
// reallocate "out" and try again!
- uint8_t* out2 = (uint8_t*)OICRealloc(out, curSize);
-
- if (!out2)
- {
- OICFree(out);
- return OC_STACK_NO_MEMORY;
- }
-
+ uint8_t *out2 = (uint8_t *)OICRealloc(out, curSize);
+ VERIFY_PARAM_NON_NULL(TAG, out2, "Failed to increase payload size");
out = out2;
err = OCConvertPayloadHelper(payload, out, &curSize);
+ while (err == CborErrorOutOfMemory)
+ {
+ uint8_t *out2 = (uint8_t *)OICRealloc(out, curSize);
+ VERIFY_PARAM_NON_NULL(TAG, out2, "Failed to increase payload size");
+ out = out2;
+ err = OCConvertPayloadHelper(payload, out, &curSize);
+ }
}
- if (err == 0)
+ if (err == CborNoError)
{
- if (curSize < INIT_SIZE)
+ if (curSize < INIT_SIZE && PAYLOAD_TYPE_SECURITY != payload->type)
{
- uint8_t* out2 = (uint8_t*)OICRealloc(out, curSize);
-
- if (!out2)
- {
- OICFree(out);
- return OC_STACK_NO_MEMORY;
- }
-
+ uint8_t *out2 = (uint8_t *)OICRealloc(out, curSize);
+ VERIFY_PARAM_NON_NULL(TAG, out2, "Failed to increase payload size");
out = out2;
}
*size = curSize;
*outPayload = out;
+ OIC_LOG_V(DEBUG, TAG, "Payload Size: %zd Payload : ", *size);
+ OIC_LOG_BUFFER(DEBUG, TAG, *outPayload, *size);
return OC_STACK_OK;
}
- else if (err < 0)
- {
- return (OCStackResult)-err;
- }
- else
- {
- return OC_STACK_ERROR;
- }
+
+ //TODO: Proper conversion from CborError to OCStackResult.
+ ret = (OCStackResult)-err;
+
+exit:
+ OICFree(out);
+ return ret;
}
static int64_t OCConvertPayloadHelper(OCPayload* payload, uint8_t* outPayload, size_t* size)
case PAYLOAD_TYPE_RD:
return OCRDPayloadToCbor((OCRDPayload*)payload, outPayload, size);
default:
- OC_LOG_V(INFO,TAG, "ConvertPayload default %d", payload->type);
- return OC_STACK_NOTIMPL;
+ OIC_LOG_V(INFO,TAG, "ConvertPayload default %d", payload->type);
+ return CborErrorUnknownType;
}
}
*size += encoder->ptr - encoder->end;
return err;
}
- else if (err != 0)
+ else if (err != CborNoError)
{
- OC_LOG_V(ERROR, TAG, "Convert Payload failed : %zd", err);
+ OIC_LOG_V(ERROR, TAG, "Convert Payload failed : %s", cbor_error_string(err));
return err;
}
else
{
*size = encoder->ptr - outPayload;
- return 0;
+ return err;
}
}
static int64_t OCConvertSecurityPayload(OCSecurityPayload* payload, uint8_t* outPayload,
size_t* size)
{
- CborEncoder encoder;
- int64_t err = 0;
-
- cbor_encoder_init(&encoder, outPayload, *size, 0);
-
- CborEncoder map;
-
- err = err | cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
-
- if(payload->securityData)
- {
- err = err | cbor_encode_text_string(&map, payload->securityData,
- strlen(payload->securityData));
- }
+ memcpy(outPayload, payload->securityData, payload->payloadSize);
+ *size = payload->payloadSize;
- err = err | cbor_encoder_close_container(&encoder, &map);
- return checkError(err, &encoder, outPayload, size);
+ return CborNoError;
}
-static char* OCStringLLJoin(OCStringLL* val)
+static int64_t OCStringLLJoin(CborEncoder *map, char *type, OCStringLL *val)
{
- OCStringLL* temp = val;
- size_t size = strlen(temp->value);
+ uint16_t count = 0;
+ int64_t err = CborNoError;
- while (temp->next)
+ for (OCStringLL *temp = val; temp; temp = temp->next)
{
- ++size;
- temp = temp->next;
- size += strlen(temp->value);
+ ++count;
}
-
- char* joinedStr = (char*)OICCalloc(sizeof(char), size + 1);
-
- if (!joinedStr)
+ if (count > 0)
{
- return NULL;
+ CborEncoder array;
+ err |= cbor_encode_text_string(map, type, strlen(type));
+ err |= cbor_encoder_create_array(map, &array, count);
+ while (val)
+ {
+ err |= cbor_encode_text_string(&array, val->value, strlen(val->value));
+ val = val->next;
+ }
+ err |= cbor_encoder_close_container(map, &array);
}
- OICStrcat(joinedStr, size + 1, val->value);
- while (val->next)
- {
- val = val->next;
- OICStrcat(joinedStr, size + 1, " ");
- OICStrcat(joinedStr, size + 1, val->value);
- }
- return joinedStr;
+ return err;
}
-static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload* payload, uint8_t* outPayload,
- size_t* size)
+static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload *payload, uint8_t *outPayload,
+ size_t *size)
{
- CborEncoder encoder = {0};
- CborEncoder rootArray = {0};
- int64_t err = 0;
+ CborEncoder encoder;
+ int64_t err = CborNoError;
cbor_encoder_init(&encoder, outPayload, *size, 0);
- if (payload->collectionResources)
- {
- CborError cborEncoderResult;
- cborEncoderResult = cbor_encoder_create_array(&encoder, &rootArray, DISCOVERY_CBOR_ARRAY_LEN);
- if (CborNoError != cborEncoderResult)
- {
- OC_LOG(ERROR, TAG, "Failed creating root array.");
- goto cbor_error;
- }
-
- CborEncoder colArray;
- cborEncoderResult = cbor_encoder_create_array(&rootArray, &colArray, CborIndefiniteLength);
- if (CborNoError != cborEncoderResult)
- {
- OC_LOG(ERROR, TAG, "Failed creating collection array.");
- goto cbor_error;
- }
- OCResourceCollectionPayload *colResources = payload->collectionResources;
- while (colResources)
- {
- if (OC_STACK_OK != OCTagsPayloadToCbor(colResources->tags, &colArray))
- {
- goto cbor_error;
- }
- if (OC_STACK_OK != OCLinksPayloadToCbor(colResources->setLinks, &colArray))
- {
- goto cbor_error;
- }
- colResources = colResources->next;
- }
- cborEncoderResult = cbor_encoder_close_container(&rootArray, &colArray);
- if (CborNoError != cborEncoderResult)
- {
- OC_LOG(ERROR, TAG, "Failed closing collection array.");
- goto cbor_error;
- }
- cborEncoderResult = cbor_encoder_close_container(&encoder, &rootArray);
- if (CborNoError != cborEncoderResult)
- {
- OC_LOG(ERROR, TAG, "Failed closing root array.");
- goto cbor_error;
- }
- }
- else if (payload->resources)
+ if (payload->resources)
{
/*
The format for the payload is "modelled" as JSON.
[ // rootArray
{ // rootMap
"di" : UUID, // device ID
+ "href": "/oic/res"
+ "rt": "oic.wk.res"
+ "n":"MyDevice"
+ "if":"oic.if.ll oic.if.baseline"
+ "di": "0685B960-736F-46F7-BEC0-9E6CBD61ADC1",
links :[ // linksArray contains maps of resources
{
href, rt, if, policy // Resource 1
}
]
*/
- CborEncoder rootMap = {};
- size_t resourceCount = OCDiscoveryPayloadGetResourceCount(payload);
-
// Open the main root array
- err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
+ CborEncoder rootArray;
+ size_t resourceCount = OCDiscoveryPayloadGetResourceCount(payload);
+ err |= cbor_encoder_create_array(&encoder, &rootArray, 1);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating discovery root array");
// Open the root map in the root array
- err = err | cbor_encoder_create_map(&rootArray, &rootMap, DISCOVERY_CBOR_RES_MAP_LEN);
+ CborEncoder rootMap;
+ err |= cbor_encoder_create_map(&rootArray, &rootMap, CborIndefiniteLength);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating discovery map");
+
+ // Insert Name
+ err |= ConditionalAddTextStringToMap(&rootMap, OC_RSRVD_DEVICE_NAME,
+ sizeof(OC_RSRVD_DEVICE_NAME) - 1, payload->name);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting name");
+
+ // Insert URI
+ err |= ConditionalAddTextStringToMap(&rootMap, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1,
+ payload->uri);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting href");
// Insert Device ID into the root map
- err = err | cbor_encode_text_string(&rootMap, OC_RSRVD_DEVICE_ID,
- sizeof(OC_RSRVD_DEVICE_ID) - 1);
- err = err | cbor_encode_byte_string(&rootMap, payload->sid, UUID_SIZE);
+ err |= AddTextStringToMap(&rootMap, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1,
+ payload->sid);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting device id");
+
+ // Insert Resource Type
+ err |= ConditionalAddTextStringToMap(&rootMap, OC_RSRVD_RESOURCE_TYPE,
+ sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->type);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting RT");
+
+ // Insert interfaces
+ if (payload->iface)
+ {
+ err |= OCStringLLJoin(&rootMap, OC_RSRVD_INTERFACE, payload->iface);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interface types tag/value");
+ }
+ // Insert baseURI if present
+ err |= ConditionalAddTextStringToMap(&rootMap, OC_RSRVD_BASE_URI,
+ sizeof(OC_RSRVD_BASE_URI) - 1,
+ payload->baseURI);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting baseURI");
// Insert Links into the root map.
- CborEncoder linkArray = {};
- err = err | cbor_encode_text_string(&rootMap, OC_RSRVD_LINKS,
- sizeof(OC_RSRVD_LINKS) - 1);
- err = err | cbor_encoder_create_array(&rootMap, &linkArray, resourceCount);
+ CborEncoder linkArray;
+ err |= cbor_encode_text_string(&rootMap, OC_RSRVD_LINKS, sizeof(OC_RSRVD_LINKS) - 1);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting links array tag");
+ err |= cbor_encoder_create_array(&rootMap, &linkArray, resourceCount);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting links array");
for(size_t i = 0; i < resourceCount; ++i)
{
- CborEncoder resourceMapElement = {};
- OCResourcePayload* resource = OCDiscoveryPayloadGetResource(payload, i);
- if(!resource)
- {
- OICFree(outPayload);
- return OC_STACK_INVALID_PARAM;
- }
+ CborEncoder linkMap;
+ OCResourcePayload *resource = OCDiscoveryPayloadGetResource(payload, i);
+ VERIFY_PARAM_NON_NULL(TAG, resource, "Failed retrieving resource");
// resource map inside the links array.
- err = err | cbor_encoder_create_map(&linkArray, &resourceMapElement,
- DISCOVERY_CBOR_LINKS_MAP_LEN);
+ err |= cbor_encoder_create_map(&linkArray, &linkMap, LINKS_MAP_LEN);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating links map");
// Below are insertions of the resource properties into the map.
// Uri
- err = err | AddTextStringToMap(&resourceMapElement, OC_RSRVD_HREF,
- sizeof(OC_RSRVD_HREF) - 1,
- resource->uri);
+ err |= AddTextStringToMap(&linkMap, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1,
+ resource->uri);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding uri to links map");
+
// Resource Type
if (resource->types)
{
- char* joinedTypes = OCStringLLJoin(resource->types);
- if (joinedTypes)
- {
- err = err | cbor_encode_text_string(&resourceMapElement, OC_RSRVD_RESOURCE_TYPE,
- sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
- err = err | cbor_encode_text_string(&resourceMapElement, joinedTypes,
- strlen(joinedTypes));
- OICFree(joinedTypes);
- }
- else
- {
- return OC_STACK_NO_MEMORY;
- }
+ err |= OCStringLLJoin(&linkMap, OC_RSRVD_RESOURCE_TYPE, resource->types);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resourceType tag/value to links map");
}
// Interface Types
if (resource->interfaces)
{
- char* joinedInterfaces = OCStringLLJoin(resource->interfaces);
- if (joinedInterfaces)
- {
- err = err | cbor_encode_text_string(&resourceMapElement, OC_RSRVD_INTERFACE,
- sizeof(OC_RSRVD_INTERFACE) - 1);
- err = err | cbor_encode_text_string(&resourceMapElement, joinedInterfaces,
- strlen(joinedInterfaces));
- OICFree(joinedInterfaces);
- }
- else
- {
- return OC_STACK_NO_MEMORY;
- }
+ err |= OCStringLLJoin(&linkMap, OC_RSRVD_INTERFACE, resource->interfaces);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interfaces tag/value to links map");
}
// Policy
CborEncoder policyMap;
- err = err | cbor_encode_text_string(&resourceMapElement, OC_RSRVD_POLICY,
- sizeof(OC_RSRVD_POLICY) - 1);
- err = err | cbor_encoder_create_map(&resourceMapElement, &policyMap, CborIndefiniteLength);
+ err |= cbor_encode_text_string(&linkMap, OC_RSRVD_POLICY, sizeof(OC_RSRVD_POLICY) - 1);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding policy tag to links map");
+ err |= cbor_encoder_create_map(&linkMap, &policyMap, CborIndefiniteLength);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding policy map to links map");
// Bitmap
- err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP,
- sizeof(OC_RSRVD_BITMAP) - 1);
- err = err | cbor_encode_uint(&policyMap, resource->bitmap);
+ err |= cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP, sizeof(OC_RSRVD_BITMAP) - 1);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding bitmap tag to policy map");
+ err |= cbor_encode_uint(&policyMap, resource->bitmap);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding bitmap value to policy map");
- if(resource->secure)
+ if (resource->secure)
{
- err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE,
+ 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);
- }
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure tag to policy map");
+ err |= cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure value to policy map");
}
+ if ((resource->secure && resource->port != 0) || payload->baseURI)
+ {
+ err |= cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT,
+ sizeof(OC_RSRVD_HOSTING_PORT) - 1);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure port tag");
+ err |= cbor_encode_uint(&policyMap, resource->port);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure port value");
+ }
+
+#ifdef TCP_ADAPTER
+ err |= cbor_encode_text_string(&policyMap, OC_RSRVD_TCP_PORT,
+ sizeof(OC_RSRVD_TCP_PORT) - 1);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding tcp port tag");
+ err |= cbor_encode_uint(&policyMap, resource->tcpPort);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding tcp port value");
+#endif
- err = err | cbor_encoder_close_container(&resourceMapElement, &policyMap);
+ err |= cbor_encoder_close_container(&linkMap, &policyMap);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing policy map");
// Finsihed encoding a resource, close the map.
- err = err | cbor_encoder_close_container(&linkArray, &resourceMapElement);
+ err |= cbor_encoder_close_container(&linkArray, &linkMap);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing link map");
}
// Close links array inside the root map.
- err = err | cbor_encoder_close_container(&rootMap, &linkArray);
+ err |= cbor_encoder_close_container(&rootMap, &linkArray);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing link array");
// close root map inside the root array.
- err = err | cbor_encoder_close_container(&rootArray, &rootMap);
+ err |= cbor_encoder_close_container(&rootArray, &rootMap);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root map");
// Close the final root array.
- err = err | cbor_encoder_close_container(&encoder, &rootArray);
+ err |= cbor_encoder_close_container(&encoder, &rootArray);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root array");
}
+exit:
return checkError(err, &encoder, outPayload, size);
-cbor_error:
- OICFree(outPayload);
- return OC_STACK_ERROR;
}
-static int64_t OCConvertDevicePayload(OCDevicePayload* payload, uint8_t* outPayload,
- size_t* size)
+static int64_t OCConvertDevicePayload(OCDevicePayload *payload, uint8_t *outPayload,
+ size_t *size)
{
- CborEncoder encoder = {0};
- int64_t err = 0;
+ if (!payload)
+ {
+ return CborUnknownError;
+ }
+ int64_t err = CborNoError;
+ CborEncoder encoder;
cbor_encoder_init(&encoder, outPayload, *size, 0);
CborEncoder repMap;
- err = err | cbor_encoder_create_map(&encoder, &repMap, CborIndefiniteLength);
+ err |= cbor_encoder_create_map(&encoder, &repMap, CborIndefiniteLength);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating device map");
+
+ // Resource Type
+ if (payload->types)
+ {
+ OIC_LOG(INFO, TAG, "Payload has types");
+ err |= OCStringLLJoin(&repMap, OC_RSRVD_RESOURCE_TYPE, payload->types);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type tag/value.");
+ }
+
+ if (payload->interfaces)
+ {
+ OIC_LOG(INFO, TAG, "Payload has interface");
+ err |= OCStringLLJoin(&repMap, OC_RSRVD_INTERFACE, payload->interfaces);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interface type tag/value.");
+ }
// Device ID
- err = err | cbor_encode_text_string(&repMap, OC_RSRVD_DEVICE_ID,
- sizeof(OC_RSRVD_DEVICE_ID) - 1);
- err = err | cbor_encode_byte_string(&repMap, payload->sid, UUID_SIZE);
+ err |= AddTextStringToMap(&repMap, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1 , payload->sid);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding device id");
// Device Name
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DEVICE_NAME,
- sizeof(OC_RSRVD_DEVICE_NAME) - 1,
- payload->deviceName);
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DEVICE_NAME,
+ sizeof(OC_RSRVD_DEVICE_NAME) - 1, payload->deviceName);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding device name");
// Device Spec Version
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SPEC_VERSION,
- sizeof(OC_RSRVD_SPEC_VERSION) - 1,
- payload->specVersion);
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SPEC_VERSION,
+ sizeof(OC_RSRVD_SPEC_VERSION) - 1, payload->specVersion);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding data spec version");
// Device data Model Version
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DATA_MODEL_VERSION,
- sizeof(OC_RSRVD_DATA_MODEL_VERSION) - 1,
- payload->dataModelVersion);
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DATA_MODEL_VERSION,
+ sizeof(OC_RSRVD_DATA_MODEL_VERSION) - 1, payload->dataModelVersion);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding data model version");
- err = err | cbor_encoder_close_container(&encoder, &repMap);
+ err |= cbor_encoder_close_container(&encoder, &repMap);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing device map");
+exit:
return checkError(err, &encoder, outPayload, size);
}
-static int64_t OCConvertPlatformPayload(OCPlatformPayload* payload, uint8_t* outPayload,
- size_t* size)
+static int64_t OCConvertPlatformPayload(OCPlatformPayload *payload, uint8_t *outPayload,
+ size_t *size)
{
- CborEncoder encoder = {0};
- int64_t err = 0;
+ int64_t err = CborNoError;
+ CborEncoder encoder;
cbor_encoder_init(&encoder, outPayload, *size, 0);
+
+ CborEncoder repMap;
+ err |= cbor_encoder_create_map(&encoder, &repMap, CborIndefiniteLength);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating platform map");
+
+ // Platform ID
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_ID,
+ sizeof(OC_RSRVD_PLATFORM_ID) - 1, payload->info.platformID);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform id");
+
+ // MFG Name
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_NAME,
+ sizeof(OC_RSRVD_MFG_NAME) - 1, payload->info.manufacturerName);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding mfg name");
+
+ // MFG Url
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_URL,
+ sizeof(OC_RSRVD_MFG_URL) - 1, payload->info.manufacturerUrl);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding mfg url");
+
+ // Model Num
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MODEL_NUM,
+ sizeof(OC_RSRVD_MODEL_NUM) -1, payload->info.modelNumber);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding model num");
+
+ // Date of Mfg
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_DATE,
+ sizeof(OC_RSRVD_MFG_DATE) - 1, payload->info.dateOfManufacture);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding mfg date");
+
+ // Platform Version
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_VERSION,
+ sizeof(OC_RSRVD_PLATFORM_VERSION) - 1, payload->info.platformVersion);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform version");
+
+ // OS Version
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_OS_VERSION,
+ sizeof(OC_RSRVD_OS_VERSION) - 1, payload->info.operatingSystemVersion);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding OS version");
+
+ // Hardware Version
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_HARDWARE_VERSION,
+ sizeof(OC_RSRVD_HARDWARE_VERSION) - 1, payload->info.hardwareVersion);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding HW version");
+
+ // Firmware Version
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_FIRMWARE_VERSION,
+ sizeof(OC_RSRVD_FIRMWARE_VERSION) - 1, payload->info.firmwareVersion);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding firmware version");
+
+ // Support URL
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SUPPORT_URL,
+ sizeof(OC_RSRVD_SUPPORT_URL) - 1, payload->info.supportUrl);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding support url");
+
+ // System Time
+ err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SYSTEM_TIME,
+ sizeof(OC_RSRVD_SYSTEM_TIME) - 1, payload->info.systemTime);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding system time");
+
+ // Resource type
+ if (payload->rt)
+ {
+ err |= OCStringLLJoin(&repMap, OC_RSRVD_RESOURCE_TYPE, payload->rt);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type.");
+ }
+
+ // Resource interfaces
+ if (payload->interfaces)
{
- CborEncoder repMap;
- err = err | cbor_encoder_create_map(&encoder, &repMap, CborIndefiniteLength);
-
- // Platform ID
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_ID,
- sizeof(OC_RSRVD_PLATFORM_ID) - 1,
- payload->info.platformID);
-
- // MFG Name
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_NAME,
- sizeof(OC_RSRVD_MFG_NAME) - 1,
- payload->info.manufacturerName);
-
- // MFG Url
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_URL,
- sizeof(OC_RSRVD_MFG_URL) - 1,
- payload->info.manufacturerUrl);
-
- // Model Num
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MODEL_NUM,
- sizeof(OC_RSRVD_MODEL_NUM) - 1,
- payload->info.modelNumber);
-
- // Date of Mfg
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_DATE,
- sizeof(OC_RSRVD_MFG_DATE) - 1,
- payload->info.dateOfManufacture);
-
- // Platform Version
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_VERSION,
- sizeof(OC_RSRVD_PLATFORM_VERSION) - 1,
- payload->info.platformVersion);
-
- // OS Version
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_OS_VERSION,
- sizeof(OC_RSRVD_OS_VERSION) - 1,
- payload->info.operatingSystemVersion);
-
- // Hardware Version
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_HARDWARE_VERSION,
- sizeof(OC_RSRVD_HARDWARE_VERSION) - 1,
- payload->info.hardwareVersion);
-
- // Firmware Version
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_FIRMWARE_VERSION,
- sizeof(OC_RSRVD_FIRMWARE_VERSION) - 1,
- payload->info.firmwareVersion);
-
- // Support URL
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SUPPORT_URL,
- sizeof(OC_RSRVD_SUPPORT_URL) - 1,
- payload->info.supportUrl);
-
- // System Time
- err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SYSTEM_TIME,
- sizeof(OC_RSRVD_SYSTEM_TIME) - 1,
- payload->info.systemTime);
-
- // Close Map
- err = err | cbor_encoder_close_container(&encoder, &repMap);
+ err |= OCStringLLJoin(&repMap, OC_RSRVD_INTERFACE, payload->interfaces);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform interface type.");
}
+ // Close Map
+ err |= cbor_encoder_close_container(&encoder, &repMap);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep map");
+
+exit:
return checkError(err, &encoder, outPayload, size);
}
-static int64_t OCConvertArrayItem(CborEncoder* array, const OCRepPayloadValueArray* valArray,
+static int64_t OCConvertArrayItem(CborEncoder *array, const OCRepPayloadValueArray *valArray,
size_t index)
{
- int64_t err = 0;
+ int64_t err = CborNoError;
switch (valArray->type)
{
case OCREP_PROP_NULL:
- OC_LOG(ERROR, TAG, "ConvertArray Invalid NULL");
+ OIC_LOG(ERROR, TAG, "ConvertArray Invalid NULL");
err = CborUnknownError;
break;
case OCREP_PROP_INT:
- err = err | cbor_encode_int(array, valArray->iArray[index]);
+ err |= cbor_encode_int(array, valArray->iArray[index]);
break;
case OCREP_PROP_DOUBLE:
- err = err | cbor_encode_double(array, valArray->dArray[index]);
+ err |= cbor_encode_double(array, valArray->dArray[index]);
break;
case OCREP_PROP_BOOL:
- err = err | cbor_encode_boolean(array, valArray->bArray[index]);
+ err |= cbor_encode_boolean(array, valArray->bArray[index]);
break;
case OCREP_PROP_STRING:
if (!valArray->strArray[index])
{
- err = err | cbor_encode_null(array);
+ err |= cbor_encode_null(array);
}
else
{
- err = err | cbor_encode_text_string(array, valArray->strArray[index],
+ err |= cbor_encode_text_string(array, valArray->strArray[index],
strlen(valArray->strArray[index]));
}
break;
case OCREP_PROP_BYTE_STRING:
if (!valArray->strArray[index])
{
- err = err | cbor_encode_null(array);
+ err |= cbor_encode_null(array);
}
else
{
- err = err | cbor_encode_byte_string(array, valArray->ocByteStrArray[index].bytes,
+ err |= cbor_encode_byte_string(array, valArray->ocByteStrArray[index].bytes,
valArray->ocByteStrArray[index].len);
}
break;
case OCREP_PROP_OBJECT:
if (!valArray->objArray[index])
{
- err = err | cbor_encode_null(array);
+ err |= cbor_encode_null(array);
}
else
{
- err = OCConvertRepMap(array, valArray->objArray[index]);
+ err |= OCConvertRepMap(array, valArray->objArray[index]);
}
break;
case OCREP_PROP_ARRAY:
- OC_LOG(ERROR, TAG, "ConvertArray Invalid child array");
+ OIC_LOG(ERROR, TAG, "ConvertArray Invalid child array");
err = CborUnknownError;
break;
}
return err;
}
-static int64_t OCConvertArray(CborEncoder* parent, const OCRepPayloadValueArray* valArray)
+
+static int64_t OCConvertArray(CborEncoder *parent, const OCRepPayloadValueArray *valArray)
{
+ int64_t err = CborNoError;
CborEncoder array;
- int64_t err = 0;
+ err |= cbor_encoder_create_array(parent, &array, valArray->dimensions[0]);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep array");
- err = err | cbor_encoder_create_array(parent, &array, valArray->dimensions[0]);
-
- for (size_t i = 0; i < valArray->dimensions[0];++i)
+ for (size_t i = 0; i < valArray->dimensions[0]; ++i)
{
- if (valArray->dimensions[1] != 0)
+ if (0 != valArray->dimensions[1])
{
CborEncoder array2;
- err = err | cbor_encoder_create_array(&array, &array2, valArray->dimensions[1]);
+ err |= cbor_encoder_create_array(&array, &array2, valArray->dimensions[1]);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep array2");
for (size_t j = 0; j < valArray->dimensions[1]; ++j)
{
- if (valArray->dimensions[2] != 0)
+ if (0 != valArray->dimensions[2])
{
CborEncoder array3;
- err = err | cbor_encoder_create_array(&array2, &array3,
- valArray->dimensions[2]);
+ err |= cbor_encoder_create_array(&array2, &array3, valArray->dimensions[2]);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep array3");
for(size_t k = 0; k < valArray->dimensions[2]; ++k)
{
- OCConvertArrayItem(&array3, valArray,
+ err |= OCConvertArrayItem(&array3, valArray,
j * valArray->dimensions[2] +
i * valArray->dimensions[2] * valArray->dimensions[1] +
k);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep array3 value");
}
- err = err | cbor_encoder_close_container(&array2, &array3);
+ err |= cbor_encoder_close_container(&array2, &array3);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep array3");
}
else
{
- OCConvertArrayItem(&array2, valArray,
- i * valArray->dimensions[1] + j);
+ err |= OCConvertArrayItem(&array2, valArray, i * valArray->dimensions[1] + j);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep array2 value");
}
}
- err = err | cbor_encoder_close_container(&array, &array2);
+ err |= cbor_encoder_close_container(&array, &array2);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep array2");
}
else
{
- OCConvertArrayItem(&array, valArray, i);
+ err |= OCConvertArrayItem(&array, valArray, i);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep array value");
}
}
- err = err | cbor_encoder_close_container(parent, &array);
+ err |= cbor_encoder_close_container(parent, &array);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep array");
+
+exit:
return err;
}
-static int64_t OCConvertRepMap(CborEncoder *map, const OCRepPayload* payload)
+static int64_t OCConvertRepMap(CborEncoder *map, const OCRepPayload *payload)
{
- int64_t err = 0;
+ int64_t err = CborNoError;
CborEncoder repMap;
- err = err | cbor_encoder_create_map(map, &repMap, CborIndefiniteLength);
- err = err | OCConvertSingleRepPayload(&repMap, payload);
- err = err | cbor_encoder_close_container(map, &repMap);
+ err |= cbor_encoder_create_map(map, &repMap, CborIndefiniteLength);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep map");
+ err |= OCConvertSingleRepPayload(&repMap, payload);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed converting single rep payload");
+ err |= cbor_encoder_close_container(map, &repMap);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep map");
+exit:
return err;
}
-static int64_t OCConvertSingleRepPayload(CborEncoder* repMap, const OCRepPayload* payload)
+static int64_t OCConvertSingleRepPayload(CborEncoder *repMap, const OCRepPayload *payload)
{
- int64_t err = 0;
- OCRepPayloadValue* value = payload->values;
- while(value)
+ int64_t err = CborNoError;
+ OCRepPayloadValue *value = payload->values;
+ while (value)
{
- err = err | cbor_encode_text_string(repMap,
- value->name,
- strlen(value->name));
- switch(value->type)
+ err |= cbor_encode_text_string(repMap, value->name, strlen(value->name));
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding tag name");
+
+ switch (value->type)
{
case OCREP_PROP_NULL:
- err = err | cbor_encode_null(repMap);
+ err |= cbor_encode_null(repMap);
break;
case OCREP_PROP_INT:
- err = err | cbor_encode_int(repMap,
- value->i);
+ err |= cbor_encode_int(repMap, value->i);
break;
case OCREP_PROP_DOUBLE:
- err = err | cbor_encode_double(repMap,
- value->d);
+ err |= cbor_encode_double(repMap, value->d);
break;
case OCREP_PROP_BOOL:
- err = err | cbor_encode_boolean(repMap,
- value->b);
+ err |= cbor_encode_boolean(repMap, value->b);
break;
case OCREP_PROP_STRING:
- err = err | cbor_encode_text_string(repMap,
- value->str, strlen(value->str));
+ 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);
+ err |= cbor_encode_byte_string(repMap, value->ocByteStr.bytes, value->ocByteStr.len);
break;
case OCREP_PROP_OBJECT:
- err = err | OCConvertRepMap(repMap, value->obj);
+ err |= OCConvertRepMap(repMap, value->obj);
break;
case OCREP_PROP_ARRAY:
- err = err | OCConvertArray(repMap, &value->arr);
+ err |= OCConvertArray(repMap, &value->arr);
break;
default:
- OC_LOG_V(ERROR, TAG, "Invalid Prop type: %d",
- value->type);
+ OIC_LOG_V(ERROR, TAG, "Invalid Prop type: %d", value->type);
break;
}
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding single rep value");
value = value->next;
}
+exit:
return err;
}
-static int64_t OCConvertRepPayload(OCRepPayload* payload, uint8_t* outPayload, size_t* size)
+static int64_t OCConvertRepPayload(OCRepPayload *payload, uint8_t *outPayload, size_t *size)
{
- CborEncoder encoder = {0};
- int64_t err = 0;
+ CborEncoder encoder;
+ int64_t err = CborNoError;
cbor_encoder_init(&encoder, outPayload, *size, 0);
- CborEncoder rootMap;
- err = err | cbor_encoder_create_map(&encoder, &rootMap, CborIndefiniteLength);
- if (payload->types)
+ size_t arrayCount = 0;
+ for (OCRepPayload *temp = payload; temp; temp = temp->next)
{
- 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;
- }
+ arrayCount++;
}
- if (payload->interfaces)
+ CborEncoder rootArray;
+ if (arrayCount > 1)
+ {
+ err |= cbor_encoder_create_array(&encoder, &rootArray, arrayCount);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep root map");
+ }
+
+ while (payload != NULL && (err == CborNoError))
{
- char* joinedInterfaces = OCStringLLJoin(payload->interfaces);
- if (joinedInterfaces)
+ CborEncoder rootMap;
+ err |= cbor_encoder_create_map(((arrayCount == 1)? &encoder: &rootArray),
+ &rootMap, CborIndefiniteLength);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating root map");
+
+ // Only in case of collection href is included.
+ if (arrayCount > 1 && payload->uri && strlen(payload->uri) > 0)
{
- 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);
+ OIC_LOG(INFO, TAG, "Payload has uri");
+ err |= cbor_encode_text_string(&rootMap, OC_RSRVD_HREF, strlen(OC_RSRVD_HREF));
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep href tag");
+ err |= cbor_encode_text_string(&rootMap, payload->uri, strlen(payload->uri));
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep href value");
}
- else
+ if (payload->types)
{
- return OC_STACK_NO_MEMORY;
+ OIC_LOG(INFO, TAG, "Payload has types");
+ err |= OCStringLLJoin(&rootMap, OC_RSRVD_RESOURCE_TYPE, payload->types);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type.");
+ }
+ if (payload->interfaces)
+ {
+ OIC_LOG(INFO, TAG, "Payload has interfaces");
+ err |= OCStringLLJoin(&rootMap, OC_RSRVD_INTERFACE, payload->interfaces);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform interface type.");
}
- }
- while(payload != NULL && (err == 0 || err == CborErrorOutOfMemory))
- {
- err = err | OCConvertSingleRepPayload(&rootMap, payload);
+ err |= OCConvertSingleRepPayload(&rootMap, payload);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting rep payload");
+
+ // Close main array
+ err |= cbor_encoder_close_container(((arrayCount == 1) ? &encoder: &rootArray),
+ &rootMap);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root map");
payload = payload->next;
}
+ if (arrayCount > 1)
+ {
+ err |= cbor_encoder_close_container(&encoder, &rootArray);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root array");
+ }
- // Close main array
- err = err | cbor_encoder_close_container(&encoder, &rootMap);
-
+exit:
return checkError(err, &encoder, outPayload, size);
}
-static int64_t OCConvertPresencePayload(OCPresencePayload* payload,
- uint8_t* outPayload, size_t* size)
+static int64_t OCConvertPresencePayload(OCPresencePayload *payload, uint8_t *outPayload,
+ size_t *size)
{
- CborEncoder encoder = {0};
- int64_t err = 0;
+ int64_t err = CborNoError;
+ CborEncoder encoder;
cbor_encoder_init(&encoder, outPayload, *size, 0);
CborEncoder map;
- err = err | cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
+ err |= cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating presence map");
// Sequence Number
- err = err | cbor_encode_text_string(&map,
- OC_RSRVD_NONCE,
- sizeof(OC_RSRVD_NONCE) - 1);
- err = err | cbor_encode_uint(&map, payload->sequenceNumber);
+ err |= cbor_encode_text_string(&map, OC_RSRVD_NONCE, sizeof(OC_RSRVD_NONCE) - 1);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding nonce tag to presence map");
+ err |= cbor_encode_uint(&map, payload->sequenceNumber);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding nonce value to presence map");
// Max Age
- err = err | cbor_encode_text_string(&map,
- OC_RSRVD_TTL,
- sizeof(OC_RSRVD_TTL) - 1);
- err = err | cbor_encode_uint(&map, payload->maxAge);
+ err |= cbor_encode_text_string(&map, OC_RSRVD_TTL, sizeof(OC_RSRVD_TTL) - 1);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding ttl tag to presence map");
+ err |= cbor_encode_uint(&map, payload->maxAge);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding ttl value to presence map");
// Trigger
- const char* triggerStr = convertTriggerEnumToString(payload->trigger);
- err = err | AddTextStringToMap(&map, OC_RSRVD_TRIGGER, sizeof(OC_RSRVD_TRIGGER) - 1,
- triggerStr);
+ err |= cbor_encode_text_string(&map, OC_RSRVD_TRIGGER, sizeof(OC_RSRVD_TRIGGER) - 1);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding trigger tag to presence map");
+ err |= cbor_encode_simple_value(&map, payload->trigger);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding trigger value to presence map");
// Resource type name
- if(payload->trigger != OC_PRESENCE_TRIGGER_DELETE)
+ if (payload->trigger != OC_PRESENCE_TRIGGER_DELETE)
{
- err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_RESOURCE_TYPE,
+ err |= ConditionalAddTextStringToMap(&map, OC_RSRVD_RESOURCE_TYPE,
sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->resourceType);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type to presence map");
}
// Close Map
- err = err | cbor_encoder_close_container(&encoder, &map);
+ err |= cbor_encoder_close_container(&encoder, &map);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing presence map");
+exit:
return checkError(err, &encoder, outPayload, size);
}
static int64_t AddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
const char* value)
{
- return cbor_encode_text_string(map, key, keylen) |
- cbor_encode_text_string(map, value, strlen(value));
+ int64_t err = cbor_encode_text_string(map, key, keylen);
+ if (CborNoError != err)
+ {
+ return err;
+ }
+ return cbor_encode_text_string(map, value, strlen(value));
}
static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,