// Discovery Links Map Length.
#define LINKS_MAP_LEN 4
+// Default data model versions in CVS form
+#define DEFAULT_DATA_MODEL_VERSIONS "res.1.1.0,sh.1.1.0"
+
// 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,
}
static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload *payload, uint8_t *outPayload,
- size_t *size)
+ size_t *size)
{
CborEncoder encoder;
int64_t err = CborNoError;
cbor_encoder_init(&encoder, outPayload, *size, 0);
- 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
- },
- {
- href, rt, if, policy // Resource 2
- },
- .
- .
- .
- ]
- }
- ]
- */
- // Open the main root array
- 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");
+ /*
+ The format for the payload is "modelled" as JSON.
+
+ [ // rootArray
+ { // rootMap
+ "di" : UUID, // device ID
+ "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
+ },
+ {
+ href, rt, if, policy // Resource 2
+ },
+ .
+ .
+ .
+ ]
+ }
+ { // rootMap
+ ...
+ }
+ ]
+ */
+ // Open the main root array
+ CborEncoder rootArray;
+ err |= cbor_encoder_create_array(&encoder, &rootArray, 1);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating discovery root array");
+
+ while (payload && payload->resources)
+ {
// Open the root map in the root array
CborEncoder rootMap;
err |= cbor_encoder_create_map(&rootArray, &rootMap, CborIndefiniteLength);
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 |= 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);
+ err |= OCStringLLJoin(&rootMap, OC_RSRVD_RESOURCE_TYPE, payload->type);
VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting RT");
// Insert interfaces
// Insert baseURI if present
err |= ConditionalAddTextStringToMap(&rootMap, OC_RSRVD_BASE_URI,
- sizeof(OC_RSRVD_BASE_URI) - 1,
- payload->baseURI);
+ 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 |= cbor_encode_text_string(&rootMap, OC_RSRVD_LINKS, sizeof(OC_RSRVD_LINKS) - 1);
VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting links array tag");
+ size_t resourceCount = OCDiscoveryPayloadGetResourceCount(payload);
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)
+ for (size_t i = 0; i < resourceCount; ++i)
{
CborEncoder linkMap;
OCResourcePayload *resource = OCDiscoveryPayloadGetResource(payload, i);
if (resource->types)
{
err |= OCStringLLJoin(&linkMap, OC_RSRVD_RESOURCE_TYPE, resource->types);
- VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resourceType tag/value to links map");
+ VERIFY_CBOR_SUCCESS(TAG, err,
+ "Failed adding resourceType tag/value to links map");
}
// Interface Types
if (resource->interfaces)
{
err |= OCStringLLJoin(&linkMap, OC_RSRVD_INTERFACE, resource->interfaces);
- VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interfaces tag/value to links map");
+ VERIFY_CBOR_SUCCESS(TAG, err,
+ "Failed adding interfaces tag/value to links map");
}
// Policy
CborEncoder policyMap;
- err |= cbor_encode_text_string(&linkMap, OC_RSRVD_POLICY, sizeof(OC_RSRVD_POLICY) - 1);
+ 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 |= cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP, sizeof(OC_RSRVD_BITMAP) - 1);
+ 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)
- {
- err |= cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE,
- sizeof(OC_RSRVD_SECURE) - 1);
- 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)
+ // Secure
+ err |= cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE,
+ sizeof(OC_RSRVD_SECURE) - 1);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure tag to policy map");
+ err |= cbor_encode_boolean(&policyMap, resource->secure);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure value to policy map");
+
+ if (resource->secure || payload->baseURI)
{
err |= cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT,
- sizeof(OC_RSRVD_HOSTING_PORT) - 1);
+ 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");
// close root map inside the root array.
err |= cbor_encoder_close_container(&rootArray, &rootMap);
VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root map");
- // Close the final root array.
- err |= cbor_encoder_close_container(&encoder, &rootArray);
- VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root array");
+
+ payload = payload->next;
}
+ // Close the final root array.
+ err |= cbor_encoder_close_container(&encoder, &rootArray);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root array");
+
exit:
return checkError(err, &encoder, outPayload, size);
}
}
int64_t err = CborNoError;
CborEncoder encoder;
+ char *dataModelVersions = 0;
cbor_encoder_init(&encoder, outPayload, *size, 0);
CborEncoder repMap;
sizeof(OC_RSRVD_SPEC_VERSION) - 1, payload->specVersion);
VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding data spec version");
- // Device data Model Version
+ // Device data Model Versions
+ if (payload->dataModelVersions)
+ {
+ OIC_LOG(INFO, TAG, "Payload has data model versions");
+ dataModelVersions = OCCreateString(payload->dataModelVersions);
+ }
+ else
+ {
+ dataModelVersions = OICStrdup(DEFAULT_DATA_MODEL_VERSIONS);
+ }
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");
+ sizeof(OC_RSRVD_DATA_MODEL_VERSION) - 1, dataModelVersions);
+ OICFree(dataModelVersions);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding data model versions");
err |= cbor_encoder_close_container(&encoder, &repMap);
VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing device map");
// Resource type
if (payload->rt)
{
- err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_RESOURCE_TYPE,
- sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->rt);
+ err |= OCStringLLJoin(&repMap, OC_RSRVD_RESOURCE_TYPE, payload->rt);
VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type.");
}