#include "octypes.h"
-OCStackResult OCParsePayload(OCPayload** outPayload, const uint8_t* payload, size_t payloadSize);
+OCStackResult OCParsePayload(OCPayload** outPayload, OCPayloadType type,
+ const uint8_t* payload, size_t payloadSize);
OCStackResult OCConvertPayload(OCPayload* payload, uint8_t** outPayload, size_t* size);
#endif
cbor_encoder_init(&encoder, outPayload, *size, 0);
CborEncoder rootArray;
- err = err | cbor_encoder_create_array(&encoder, &rootArray, 2);
- err = err | cbor_encode_uint(&rootArray, PAYLOAD_TYPE_SECURITY);
-
+ err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
CborEncoder map;
err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);
cbor_encoder_init(&encoder, outPayload, *size, 0);
CborEncoder rootArray;
- err = err | cbor_encoder_create_array(&encoder, &rootArray, 1 + resourceCount);
- err = err | cbor_encode_uint(&rootArray, PAYLOAD_TYPE_DISCOVERY);
+ err = err | cbor_encoder_create_array(&encoder, &rootArray, resourceCount);
for(size_t i = 0; i < resourceCount; ++i)
{
cbor_encoder_init(&encoder, outPayload, *size, 0);
CborEncoder rootArray;
- err = err | cbor_encoder_create_array(&encoder, &rootArray, 2);
- err = err | cbor_encode_uint(&rootArray, PAYLOAD_TYPE_DEVICE);
+ err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
{
CborEncoder map;
cbor_encoder_init(&encoder, outPayload, *size, 0);
CborEncoder rootArray;
- err = err | cbor_encoder_create_array(&encoder, &rootArray, 2);
- err = err | cbor_encode_uint(&rootArray, PAYLOAD_TYPE_PLATFORM);
+ err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
{
CborEncoder map;
err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);
cbor_encoder_init(&encoder, outPayload, *size, 0);
CborEncoder rootArray;
err = err | cbor_encoder_create_array(&encoder, &rootArray, CborIndefiniteLength);
- err = err | cbor_encode_uint(&rootArray, PAYLOAD_TYPE_REPRESENTATION);
while(payload != NULL && (err == 0 || err == CborErrorOutOfMemory))
{
cbor_encoder_init(&encoder, outPayload, *size, 0);
CborEncoder rootArray;
- err = err | cbor_encoder_create_array(&encoder, &rootArray, 2);
- err = err | cbor_encode_uint(&rootArray, PAYLOAD_TYPE_PRESENCE);
-
+ err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
CborEncoder map;
err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);
static OCStackResult OCParsePresencePayload(OCPayload** outPayload, CborValue* arrayVal);
static OCStackResult OCParseSecurityPayload(OCPayload** outPayload, CborValue* arrayVal);
-OCStackResult OCParsePayload(OCPayload** outPayload, const uint8_t* payload, size_t payloadSize)
+OCStackResult OCParsePayload(OCPayload** outPayload, OCPayloadType payloadType,
+ const uint8_t* payload, size_t payloadSize)
{
CborParser parser;
CborValue rootValue;
// enter the array
err = err || cbor_value_enter_container(&rootValue, &arrayValue);
- int payloadType = 0;
- err = err || cbor_value_get_int(&arrayValue, &payloadType);
- err = err || cbor_value_advance_fixed(&arrayValue);
-
- if(err)
+ if(err || arrayValue.type != CborMapType)
{
OC_LOG_V(ERROR, TAG, "CBOR payload parse failed :%d", err);
return OC_STACK_MALFORMED_RESPONSE;
if(payload && payloadSize)
{
- if(OCParsePayload(&entityHandlerRequest->payload, payload, payloadSize) != OC_STACK_OK)
+ if(OCParsePayload(&entityHandlerRequest->payload, PAYLOAD_TYPE_REPRESENTATION,
+ payload, payloadSize) != OC_STACK_OK)
{
return OC_STACK_ERROR;
}
if (responseInfo->info.payload)
{
- result = OCParsePayload(&response.payload, responseInfo->info.payload,
+ result = OCParsePayload(&response.payload,
+ PAYLOAD_TYPE_PRESENCE,
+ responseInfo->info.payload,
responseInfo->info.payloadSize);
if(result != OC_STACK_OK)
response.identity.id_length = responseInfo->info.identity.id_length;
response.result = CAToOCStackResult(responseInfo->result);
+
if(responseInfo->info.payload &&
- responseInfo->info.payloadSize &&
- OC_STACK_OK != OCParsePayload(&response.payload, responseInfo->info.payload,
- responseInfo->info.payloadSize))
+ responseInfo->info.payloadSize)
{
- OC_LOG(ERROR, TAG, "Error converting payload");
- OCPayloadDestroy(response.payload);
- return;
+ OCPayloadType type = PAYLOAD_TYPE_INVALID;
+ if (cbNode->method == OC_REST_DISCOVER)
+ {
+ if (strncmp(OC_RSRVD_WELL_KNOWN_URI,cbNode->requestUri,
+ sizeof(OC_RSRVD_WELL_KNOWN_URI) - 1) == 0)
+ {
+ type = PAYLOAD_TYPE_DISCOVERY;
+ }
+ else if (strcmp(cbNode->requestUri, OC_RSRVD_DEVICE_URI) == 0)
+ {
+ type = PAYLOAD_TYPE_DEVICE;
+ }
+ else if (strcmp(cbNode->requestUri, OC_RSRVD_PLATFORM_URI) == 0)
+ {
+ type = PAYLOAD_TYPE_PLATFORM;
+ }
+ else
+ {
+ OC_LOG_V(ERROR, TAG, "Unknown Payload type in Discovery: %d %s",
+ cbNode->method, cbNode->requestUri);
+ return;
+ }
+ }
+ else if (cbNode->method == OC_REST_GET ||
+ cbNode->method == OC_REST_PUT ||
+ cbNode->method == OC_REST_POST ||
+ cbNode->method == OC_REST_OBSERVE ||
+ cbNode->method == OC_REST_OBSERVE_ALL ||
+ cbNode->method == OC_REST_DELETE)
+ {
+ OC_LOG_V(INFO, TAG, "Assuming PAYLOAD_TYPE_REPRESENTATION: %d %s",
+ cbNode->method, cbNode->requestUri);
+ type = PAYLOAD_TYPE_REPRESENTATION;
+ }
+ else
+ {
+ OC_LOG_V(ERROR, TAG, "Unknown Payload type: %d %s",
+ cbNode->method, cbNode->requestUri);
+ return;
+ }
+
+ if(OC_STACK_OK != OCParsePayload(&response.payload,
+ type,
+ responseInfo->info.payload,
+ responseInfo->info.payloadSize))
+ {
+ OC_LOG(ERROR, TAG, "Error converting payload");
+ OCPayloadDestroy(response.payload);
+ return;
+ }
}
response.numRcvdVendorSpecificHeaderOptions = 0;