#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. */
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);
while (svc)
{
- CborEncoder svcMap = {{.ptr = NULL }, .end = 0 };
- CborEncoder owners = {{.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.");
if (CborNoError == cborEncoderResult)
{
*cborPayload = outPayload;
- *cborSize = encoder.ptr - outPayload;
+ *cborSize = cbor_encoder_get_buffer_size(&encoder, outPayload);
ret = OC_STACK_OK;
}
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;
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 };
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 };
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);
OCEntityHandlerResult ehRet = (res == OC_STACK_OK) ? OC_EH_OK : OC_EH_ERROR;
// Send response payload to request originator
- SendSRMCBORResponse(ehRequest, ehRet, cborSvc, size);
+ 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)
{
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, 0);
+ 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, 0);
+ 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,