#include "ocstack.h"
#include "ocpayload.h"
+#include "ocpayloadcbor.h"
#include "oic_malloc.h"
#include "utlist.h"
#include "payload_logging.h"
#include "security_internals.h"
-#define TAG "SRM-SVC"
+#define TAG "OIC_SRM_SVC"
/** Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
* The value of payload size is increased until reaching belox max cbor size. */
-static const uint8_t CBOR_SIZE = 255;
+static const uint16_t CBOR_SIZE = 512;
/** Max cbor size payload. */
static const uint16_t CBOR_MAX_SIZE = 4400;
*cborPayload = NULL;
*cborSize = 0;
- CborError cborEncoderResult = CborNoError;
+ int64_t cborEncoderResult = CborNoError;
OCStackResult ret = OC_STACK_ERROR;
- CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
- CborEncoder svcArray = { {.ptr = NULL }, .end = 0 };
+ CborEncoder encoder;
+ CborEncoder svcArray;
uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
VERIFY_NON_NULL(TAG, outPayload, ERROR);
cbor_encoder_init(&encoder, outPayload, cborLen, 0);
// Create SVC Array
- cborEncoderResult = cbor_encoder_create_array(&encoder, &svcArray,
- svcElementsCount(svc));
+ cborEncoderResult = cbor_encoder_create_array(&encoder, &svcArray, svcElementsCount(svc));
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to Create SVC Array.");
while (svc)
{
- CborEncoder svcMap = { {.ptr = NULL }, .end = 0};
+ CborEncoder svcMap;
+ CborEncoder owners;
+
cborEncoderResult = cbor_encoder_create_map(&svcArray, &svcMap, SVC_MAP_SIZE);
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to Create SVC Map.");
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to Add SVC Serv Type Value.");
// Owners
+ // TODO: Need to modification to single ROwner, (Currently SINGLE_SERVICE_CLIENT_DRIVEN only)
cborEncoderResult = cbor_encode_text_string(&svcMap, OIC_JSON_OWNERS_NAME,
strlen(OIC_JSON_OWNERS_NAME));
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to Add SVC Owners Tag.");
- CborEncoder owners = { {.ptr = NULL }, .end = 0 };
cborEncoderResult = cbor_encoder_create_array(&svcMap, &owners, svc->ownersLen);
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to Add SVC Array.");
for (size_t i = 0; i < svc->ownersLen; i++)
cborEncoderResult = cbor_encoder_close_container(&encoder, &svcArray);
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to Close SVC Array.");
- *cborPayload = outPayload;
- *cborSize = encoder.ptr - outPayload;
- ret = OC_STACK_OK;
+ if (CborNoError == cborEncoderResult)
+ {
+ *cborPayload = outPayload;
+ *cborSize = cbor_encoder_get_buffer_size(&encoder, outPayload);
+ ret = OC_STACK_OK;
+ }
exit:
if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
OICFree(outPayload);
outPayload = NULL;
// Since the allocated initial memory failed, double the memory.
- cborLen += encoder.ptr - encoder.end;
+ cborLen += cbor_encoder_get_buffer_size(&encoder, encoder.end);
cborEncoderResult = CborNoError;
ret = SVCToCBORPayload(svc, cborPayload, &cborLen);
+ *cborSize = cborLen;
}
if (CborNoError != cborEncoderResult)
OCStackResult CBORPayloadToSVC(const uint8_t *cborPayload, size_t size,
OicSecSvc_t **secSvc)
{
- if (NULL == cborPayload || NULL == secSvc || NULL != *secSvc)
+ if (NULL == cborPayload || NULL == secSvc || NULL != *secSvc || 0 == size)
{
return OC_STACK_INVALID_PARAM;
}
CborValue svcCbor = { .parser = NULL };
CborParser parser = { .end = NULL };
CborError cborFindResult = CborNoError;
- int cborLen = size;
- if (0 == size)
- {
- cborLen = CBOR_SIZE;
- }
- cbor_parser_init(cborPayload, cborLen, 0, &parser, &svcCbor);
+ cbor_parser_init(cborPayload, size, 0, &parser, &svcCbor);
OicSecSvc_t *headSvc = NULL;
CborValue svcArray = { .parser = NULL };
while (cbor_value_is_valid(&svcArray))
{
CborValue svcMap = { .parser = NULL };
- cborFindResult = cbor_value_enter_container(&svcArray, &svcMap);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to Enter SVC Map.");
-
OicSecSvc_t *svc = (OicSecSvc_t *) OICCalloc(1, sizeof(OicSecSvc_t));
VERIFY_NON_NULL(TAG, svc, ERROR);
+ cborFindResult = cbor_value_enter_container(&svcArray, &svcMap);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to Enter SVC Map.");
while (cbor_value_is_valid(&svcMap))
{
char* name = NULL;
size_t len = 0;
+ CborType type = CborInvalidType;
+
cborFindResult = cbor_value_dup_text_string(&svcMap, &name, &len, NULL);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to Find Name.");
cborFindResult = cbor_value_advance(&svcMap);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to Advance.");
- CborType type = cbor_value_get_type(&svcMap);
-
+ type = cbor_value_get_type(&svcMap);
// Service Device Identity
- if (0 == strcmp(OIC_JSON_SERVICE_DEVICE_ID, name))
+ if (0 == strcmp(OIC_JSON_SERVICE_DEVICE_ID, name) && cbor_value_is_byte_string(&svcMap))
{
uint8_t *subjectId = NULL;
cborFindResult = cbor_value_dup_byte_string(&svcMap, &subjectId, &len, NULL);
OICFree(subjectId);
}
// Service Type
- if (0 == strcmp(OIC_JSON_SERVICE_TYPE, name))
+ if (0 == strcmp(OIC_JSON_SERVICE_TYPE, name) && cbor_value_is_integer(&svcMap))
{
- cborFindResult = cbor_value_get_int(&svcMap, (int *) &svc->svct);
+ int svct;
+
+ cborFindResult = cbor_value_get_int(&svcMap, &svct);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to Find SVCT.");
+ svc->svct = (OicSecSvcType_t)svct;
}
// Owners -- Mandatory
- if (0 == strcmp(OIC_JSON_OWNERS_NAME, name))
+ if (0 == strcmp(OIC_JSON_OWNERS_NAME, name) && cbor_value_is_array(&svcMap))
{
+ int i = 0;
CborValue owners = { .parser = NULL };
+
cborFindResult = cbor_value_get_array_length(&svcMap, &svc->ownersLen);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to Find Owner Len.");
cborFindResult = cbor_value_enter_container(&svcMap, &owners);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to Enter Owner Array.");
- int i = 0;
svc->owners = (OicUuid_t *)OICCalloc(svc->ownersLen, sizeof(*svc->owners));
VERIFY_NON_NULL(TAG, svc->owners, ERROR);
- while (cbor_value_is_valid(&owners))
+
+ while (cbor_value_is_valid(&owners) && cbor_value_is_byte_string(&owners))
{
uint8_t *owner = NULL;
cborFindResult = cbor_value_dup_byte_string(&owners, &owner, &len, NULL);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to Find Owner Array Value.");
cborFindResult = cbor_value_advance(&owners);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to Advance Owner Array.");
- memcpy(svc->owners[i].id, owner, len);
+ memcpy(svc->owners[i++].id, owner, len);
OICFree(owner);
}
}
{
DeleteSVCList(headSvc);
headSvc = NULL;
+ *secSvc = NULL;
ret = OC_STACK_ERROR;
}
return ret;
OCEntityHandlerResult ehRet = (res == OC_STACK_OK) ? OC_EH_OK : OC_EH_ERROR;
// Send response payload to request originator
- SendSRMCBORResponse(ehRequest, ehRet, cborSvc);
+ ehRet = ((SendSRMResponse(ehRequest, ehRet, cborSvc, size)) == OC_STACK_OK) ?
+ OC_EH_OK : OC_EH_ERROR;
OICFree(cborSvc);
-
OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
return ehRet;
}
static OCEntityHandlerResult HandleSVCPostRequest(const OCEntityHandlerRequest * ehRequest)
{
OCEntityHandlerResult ehRet = OC_EH_ERROR;
- uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData1;;
+ uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData;
+ size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
if (payload)
{
// Convert CBOR SVC data into SVC. This will also validate the SVC data received.
OicSecSvc_t *newSvc = NULL;
- OCStackResult res = CBORPayloadToSVC(payload, CBOR_SIZE, &newSvc);
+ OCStackResult res = CBORPayloadToSVC(payload, size, &newSvc);
if (newSvc && res == OC_STACK_OK)
{
// Append the new SVC to existing SVC
if (cborPayload && OC_STACK_OK == res &&
UpdateSecureResourceInPS(OIC_JSON_SVC_NAME, cborPayload, size) == OC_STACK_OK)
{
- ehRet = OC_EH_RESOURCE_CREATED;
+ ehRet = OC_EH_CHANGED;
}
OICFree(cborPayload);
}
}
// Send payload to request originator
- SendSRMCBORResponse(ehRequest, ehRet, NULL);
+ if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
+ {
+ ehRet = OC_EH_ERROR;
+ OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandleSVCPostRequest");
+ }
OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
return ehRet;
default:
ehRet = OC_EH_ERROR;
- SendSRMCBORResponse(ehRequest, ehRet, NULL);
+ SendSRMResponse(ehRequest, ehRet, NULL, 0);
}
}
{
OCStackResult ret = OCCreateResource(&gSvcHandle,
OIC_RSRC_TYPE_SEC_SVC,
- OIC_MI_DEF,
+ OC_RSRVD_INTERFACE_DEFAULT,
OIC_RSRC_SVC_URI,
SVCEntityHandler,
NULL,
if (data)
{
- // Convert JSON SVC into binary format
+ // Convert CBOR SVC into binary format
ret = CBORPayloadToSVC(data, size, &gSvc);
+ if (ret != OC_STACK_OK)
+ {
+ OIC_LOG (DEBUG, TAG, " ConvertCBOR SVC into binary format failed");
+ }
OICFree(data);
}