Remove unused pkg dependancy
[platform/upstream/iotivity.git] / resource / csdk / security / src / svcresource.c
index 3cfe8ee..d77a75e 100644 (file)
@@ -22,6 +22,7 @@
 
 #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;
@@ -92,10 +93,10 @@ OCStackResult SVCToCBORPayload(const OicSecSvc_t *svc, uint8_t **cborPayload,
     *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);
@@ -103,13 +104,14 @@ OCStackResult SVCToCBORPayload(const OicSecSvc_t *svc, uint8_t **cborPayload,
     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.");
 
@@ -129,10 +131,10 @@ OCStackResult SVCToCBORPayload(const OicSecSvc_t *svc, uint8_t **cborPayload,
         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++)
@@ -153,9 +155,12 @@ OCStackResult SVCToCBORPayload(const OicSecSvc_t *svc, uint8_t **cborPayload,
     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))
@@ -164,9 +169,10 @@ exit:
         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)
@@ -184,7 +190,7 @@ exit:
 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;
     }
@@ -196,13 +202,8 @@ OCStackResult CBORPayloadToSVC(const uint8_t *cborPayload, size_t size,
     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 };
@@ -212,25 +213,25 @@ OCStackResult CBORPayloadToSVC(const uint8_t *cborPayload, size_t size,
     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);
@@ -239,31 +240,36 @@ OCStackResult CBORPayloadToSVC(const uint8_t *cborPayload, size_t size,
                 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);
                 }
             }
@@ -303,6 +309,7 @@ exit:
     {
         DeleteSVCList(headSvc);
         headSvc = NULL;
+        *secSvc = NULL;
         ret = OC_STACK_ERROR;
     }
     return ret;
@@ -317,10 +324,10 @@ static OCEntityHandlerResult HandleSVCGetRequest(const OCEntityHandlerRequest *
     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;
 }
@@ -328,12 +335,13 @@ static OCEntityHandlerResult HandleSVCGetRequest(const OCEntityHandlerRequest *
 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
@@ -346,14 +354,18 @@ static OCEntityHandlerResult HandleSVCPostRequest(const OCEntityHandlerRequest *
             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;
@@ -389,7 +401,7 @@ static OCEntityHandlerResult SVCEntityHandler(OCEntityHandlerFlag flag,
 
             default:
                 ehRet = OC_EH_ERROR;
-                SendSRMCBORResponse(ehRequest, ehRet, NULL);
+                SendSRMResponse(ehRequest, ehRet, NULL, 0);
         }
     }
 
@@ -403,7 +415,7 @@ static OCStackResult CreateSVCResource()
 {
     OCStackResult ret = OCCreateResource(&gSvcHandle,
                                          OIC_RSRC_TYPE_SEC_SVC,
-                                         OIC_MI_DEF,
+                                         OC_RSRVD_INTERFACE_DEFAULT,
                                          OIC_RSRC_SVC_URI,
                                          SVCEntityHandler,
                                          NULL,
@@ -434,8 +446,12 @@ OCStackResult InitSVCResource()
 
     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);
     }