replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / src / ocpayloadparse.c
old mode 100644 (file)
new mode 100755 (executable)
index e27f7ba..59c4c62
@@ -26,6 +26,7 @@
 // Refer http://pubs.opengroup.org/onlinepubs/009695399/
 // Required for strok_r
 #define _POSIX_C_SOURCE 200112L
+
 #include <string.h>
 #include <stdlib.h>
 #include "oic_string.h"
 #include "payload_logging.h"
 #include "platform_features.h"
 
-#if defined(RD_CLIENT) || defined(RD_SERVER)
-#include "rdpayload.h"
-#endif
-
 #define TAG "OIC_RI_PAYLOADPARSE"
 
 static OCStackResult OCParseDiscoveryPayload(OCPayload **outPayload, CborValue *arrayVal);
-static OCStackResult OCParseDevicePayload(OCPayload **outPayload, CborValue *arrayVal);
-static OCStackResult OCParsePlatformPayload(OCPayload **outPayload, CborValue *arrayVal);
 static CborError OCParseSingleRepPayload(OCRepPayload **outPayload, CborValue *repParent, bool isRoot);
 static OCStackResult OCParseRepPayload(OCPayload **outPayload, CborValue *arrayVal);
+#ifdef WITH_PRESENCE
 static OCStackResult OCParsePresencePayload(OCPayload **outPayload, CborValue *arrayVal);
+#endif
 static OCStackResult OCParseSecurityPayload(OCPayload **outPayload, const uint8_t *payload, size_t size);
 
 OCStackResult OCParsePayload(OCPayload **outPayload, OCPayloadType payloadType,
@@ -61,7 +58,6 @@ OCStackResult OCParsePayload(OCPayload **outPayload, OCPayloadType payloadType,
 
     OIC_LOG_V(INFO, TAG, "CBOR Parsing size: %zu of Payload Type: %d, Payload:",
             payloadSize, payloadType);
-    OIC_LOG_BUFFER(DEBUG, TAG, payload, payloadSize);
 
     CborParser parser;
     CborValue rootValue;
@@ -74,26 +70,17 @@ OCStackResult OCParsePayload(OCPayload **outPayload, OCPayloadType payloadType,
         case PAYLOAD_TYPE_DISCOVERY:
             result = OCParseDiscoveryPayload(outPayload, &rootValue);
             break;
-        case PAYLOAD_TYPE_DEVICE:
-            result = OCParseDevicePayload(outPayload, &rootValue);
-            break;
-        case PAYLOAD_TYPE_PLATFORM:
-            result = OCParsePlatformPayload(outPayload, &rootValue);
-            break;
         case PAYLOAD_TYPE_REPRESENTATION:
             result = OCParseRepPayload(outPayload, &rootValue);
             break;
+#ifdef WITH_PRESENCE
         case PAYLOAD_TYPE_PRESENCE:
             result = OCParsePresencePayload(outPayload, &rootValue);
             break;
+#endif
         case PAYLOAD_TYPE_SECURITY:
             result = OCParseSecurityPayload(outPayload, payload, payloadSize);
             break;
-#if defined(RD_CLIENT) || defined(RD_SERVER)
-        case PAYLOAD_TYPE_RD:
-            result = OCRDCborToPayload(&rootValue, outPayload);
-            break;
-#endif
         default:
             OIC_LOG_V(ERROR, TAG, "ParsePayload Type default: %d", payloadType);
             result = OC_STACK_INVALID_PARAM;
@@ -162,7 +149,7 @@ static CborError OCParseStringLL(CborValue *map, char *type, OCStringLL **resour
                 while (curPtr)
                 {
                     char *trimmed = InPlaceStringTrim(curPtr);
-                    if (trimmed[0] !='\0')
+                    if (trimmed && strlen(trimmed) > 0)
                     {
                         if (!OCResourcePayloadAddStringLL(resource, trimmed))
                         {
@@ -233,26 +220,18 @@ static OCStackResult OCParseDiscoveryPayload(OCPayload **outPayload, CborValue *
             // BaseURI - Not a mandatory field
             err = cbor_value_map_find_value(&rootMap, OC_RSRVD_BASE_URI, &curVal);
             VERIFY_CBOR_SUCCESS(TAG, err, "to find uri tag");
-            if (cbor_value_is_valid(&curVal))
+            if (cbor_value_is_text_string(&curVal))
             {
                 err = cbor_value_dup_text_string(&curVal, &(temp->baseURI), &len, NULL);
                 VERIFY_CBOR_SUCCESS(TAG, err, "to find base uri value");
             }
 
-            // HREF - Not a mandatory field
-            err = cbor_value_map_find_value(&rootMap, OC_RSRVD_HREF, &curVal);
-            if (cbor_value_is_valid(&curVal))
-            {
-                err = cbor_value_dup_text_string(&curVal, &(temp->uri), &len, NULL);
-                VERIFY_CBOR_SUCCESS(TAG, err, "to find uri value");
-            }
-
             // RT - Not a mandatory field
             err = cbor_value_map_find_value(&rootMap, OC_RSRVD_RESOURCE_TYPE, &curVal);
             if (cbor_value_is_valid(&curVal))
             {
                 err = OCParseStringLL(&rootMap, OC_RSRVD_RESOURCE_TYPE, &temp->type);
-                VERIFY_CBOR_SUCCESS(TAG, err, "to find base uri value");
+                VERIFY_CBOR_SUCCESS(TAG, err, "to find resource type");
             }
 
             // IF - Not a mandatory field
@@ -260,18 +239,12 @@ static OCStackResult OCParseDiscoveryPayload(OCPayload **outPayload, CborValue *
             if (cbor_value_is_valid(&curVal))
             {
                 err =  OCParseStringLL(&rootMap, OC_RSRVD_INTERFACE, &temp->iface);
-            }
-            if (!temp->iface)
-            {
-                if (!OCResourcePayloadAddStringLL(&temp->iface, OC_RSRVD_INTERFACE_LL))
-                {
-                    err = CborErrorOutOfMemory;
-                }
+                VERIFY_CBOR_SUCCESS(TAG, err, "to find interface");
             }
 
             // Name - Not a mandatory field
             err = cbor_value_map_find_value(&rootMap, OC_RSRVD_DEVICE_NAME, &curVal);
-            if (cbor_value_is_valid(&curVal))
+            if (cbor_value_is_text_string(&curVal))
             {
                 err = cbor_value_dup_text_string(&curVal, &temp->name, &len, NULL);
                 VERIFY_CBOR_SUCCESS(TAG, err, "to find device name");
@@ -331,7 +304,7 @@ static OCStackResult OCParseDiscoveryPayload(OCPayload **outPayload, CborValue *
                 // Secure Flag
                 err = cbor_value_map_find_value(&policyMap, OC_RSRVD_SECURE, &curVal);
                 VERIFY_CBOR_SUCCESS(TAG, err, "to find secure tag");
-                if (cbor_value_is_valid(&curVal))
+                if (cbor_value_is_boolean(&curVal))
                 {
                     err = cbor_value_get_boolean(&curVal, &(resource->secure));
                     VERIFY_CBOR_SUCCESS(TAG, err, "to find secure value");
@@ -340,7 +313,7 @@ static OCStackResult OCParseDiscoveryPayload(OCPayload **outPayload, CborValue *
                 // Port
                 err = cbor_value_map_find_value(&policyMap, OC_RSRVD_HOSTING_PORT, &curVal);
                 VERIFY_CBOR_SUCCESS(TAG, err, "to find port tag");
-                if (cbor_value_is_valid(&curVal))
+                if (cbor_value_is_integer(&curVal))
                 {
                     int port;
 
@@ -352,7 +325,7 @@ static OCStackResult OCParseDiscoveryPayload(OCPayload **outPayload, CborValue *
 #ifdef TCP_ADAPTER
                 // TCP Port
                 err = cbor_value_map_find_value(&policyMap, OC_RSRVD_TCP_PORT, &curVal);
-                if (cbor_value_is_valid(&curVal))
+                if (cbor_value_is_integer(&curVal))
                 {
                     int tcpPort;
 
@@ -360,6 +333,19 @@ static OCStackResult OCParseDiscoveryPayload(OCPayload **outPayload, CborValue *
                     VERIFY_CBOR_SUCCESS(TAG, err, "to find tcp port value");
                     resource->tcpPort = (uint16_t)tcpPort;
                 }
+
+#ifdef __WITH_TLS__
+                // TLS Port
+                err = cbor_value_map_find_value(&policyMap, OC_RSRVD_TLS_PORT, &curVal);
+                if (cbor_value_is_integer(&curVal))
+                {
+                    int tlsPort;
+
+                    err = cbor_value_get_int(&curVal, &tlsPort);
+                    VERIFY_CBOR_SUCCESS(TAG, err, "to find tcp tls port value");
+                    resource->tcpPort = (uint16_t)tlsPort;
+                }
+#endif
 #endif
 
                 err = cbor_value_advance(&resourceMap);
@@ -407,241 +393,6 @@ exit:
     return ret;
 }
 
-static OCStackResult OCParseDevicePayload(OCPayload **outPayload, CborValue *rootValue)
-{
-    OCStackResult ret = OC_STACK_INVALID_PARAM;
-    CborError err = CborNoError;
-    OCDevicePayload *out = NULL;
-    VERIFY_PARAM_NON_NULL(TAG, outPayload, "Invalid param outPayload");
-    VERIFY_PARAM_NON_NULL(TAG, rootValue, "Invalid param rootValue");
-
-    *outPayload = NULL;
-
-    out = (OCDevicePayload *)OICCalloc(1, sizeof(OCDevicePayload));
-    VERIFY_PARAM_NON_NULL(TAG, out, "Failed allocating device payload")
-    out->base.type = PAYLOAD_TYPE_DEVICE;
-    ret = OC_STACK_MALFORMED_RESPONSE;
-
-    if (cbor_value_is_map(rootValue))
-    {
-        CborValue curVal;
-        // Resource Type
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_RESOURCE_TYPE, &curVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find resource type tag");
-
-        if (cbor_value_is_valid(&curVal))
-        {
-            err =  OCParseStringLL(rootValue, OC_RSRVD_RESOURCE_TYPE, &out->types);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find rt type tag/value");
-        }
-
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_INTERFACE, &curVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find interface tag");
-        if (cbor_value_is_valid(&curVal))
-        {
-            err =  OCParseStringLL(rootValue, OC_RSRVD_INTERFACE, &out->interfaces);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find interfaces tag/value");
-        }
-        // Device ID
-        size_t len = 0;
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_DEVICE_ID, &curVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find device id tag");
-        if (cbor_value_is_valid(&curVal))
-        {
-            if (cbor_value_is_byte_string(&curVal))
-            {
-                err = cbor_value_dup_byte_string(&curVal, (uint8_t **)&out->sid, &len, NULL);
-                VERIFY_CBOR_SUCCESS(TAG, err, "to find device id in device payload");
-            }
-            else if (cbor_value_is_text_string(&curVal))
-            {
-                err = cbor_value_dup_text_string(&curVal, &out->sid, &len, NULL);
-                VERIFY_CBOR_SUCCESS(TAG, err, "to find device id in device payload");
-            }
-        }
-        // Device Name
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_DEVICE_NAME, &curVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find device name tag");
-        if (cbor_value_is_valid(&curVal))
-        {
-            err = cbor_value_dup_text_string(&curVal, &out->deviceName, &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "to find device name in device payload");
-        }
-        // Device Spec Version
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_SPEC_VERSION, &curVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find spec ver tag");
-        if (cbor_value_is_valid(&curVal))
-        {
-            err = cbor_value_dup_text_string(&curVal, &out->specVersion, &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "to find spec version in device payload");
-        }
-        // Data Model Versions
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_DATA_MODEL_VERSION, &curVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find data model versions tag");
-        if (cbor_value_is_valid(&curVal))
-        {
-            size_t len = 0;
-            char * str = NULL;
-            err = cbor_value_dup_text_string(&curVal, &str, &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "to find data model versions in device payload");
-            out->dataModelVersions = OCCreateOCStringLL(str);
-            OICFree(str);
-        }
-        err = cbor_value_advance(rootValue);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to advance device payload");
-
-        *outPayload = (OCPayload *)out;
-        return OC_STACK_OK;
-    }
-
-exit:
-    OCDevicePayloadDestroy(out);
-    return ret;
-}
-
-static OCStackResult OCParsePlatformPayload(OCPayload **outPayload, CborValue *rootValue)
-{
-    OCStackResult ret = OC_STACK_INVALID_PARAM;
-    CborError err = CborNoError;
-    OCPlatformInfo info = {0};
-    OCStringLL* rt = NULL;
-    OCStringLL* interfaces = NULL;
-    OCPlatformPayload* out = NULL;
-
-    VERIFY_PARAM_NON_NULL(TAG, outPayload, "Invalid Parameter outPayload");
-
-    if (cbor_value_is_map(rootValue))
-    {
-        CborValue repVal;
-        size_t len = 0;
-        ret = OC_STACK_MALFORMED_RESPONSE;
-
-        // Platform ID
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_PLATFORM_ID, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find platform id tag");
-        if (cbor_value_is_valid(&repVal))
-        {
-            err = cbor_value_dup_text_string(&repVal, &(info.platformID), &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find platformID in the platform payload");
-        }
-         // MFG Name
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_MFG_NAME, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find mfg name tag");
-        if (cbor_value_is_valid(&repVal))
-        {
-            err = cbor_value_dup_text_string(&repVal, &(info.manufacturerName), &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find manufactureName in the platform payload");
-        }
-        // MFG URL
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_MFG_URL, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find mfg url tag");
-        if (cbor_value_is_valid(&repVal))
-        {
-            err = cbor_value_dup_text_string(&repVal, &(info.manufacturerUrl), &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find manufactureUrl in the platform payload");
-        }
-        // Model Num
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_MODEL_NUM, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find model num tag");
-        if (cbor_value_is_valid(&repVal))
-        {
-            err = cbor_value_dup_text_string(&repVal, &(info.modelNumber), &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find modelNumber in the platform payload");
-        }
-        // Date of Mfg
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_MFG_DATE, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find mfg date tag");
-        if (cbor_value_is_valid(&repVal))
-        {
-            err = cbor_value_dup_text_string(&repVal, &(info.dateOfManufacture), &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find dateOfManufacture in the platform payload");
-        }
-        // Platform Version
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_PLATFORM_VERSION, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find platform ver tag");
-        if (cbor_value_is_valid(&repVal))
-        {
-            err = cbor_value_dup_text_string(&repVal, &(info.platformVersion), &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find platformVersion in the platform payload");
-        }
-        // OS Version
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_OS_VERSION, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find os ver tag");
-        if (cbor_value_is_valid(&repVal))
-        {
-            err = cbor_value_dup_text_string(&repVal, &(info.operatingSystemVersion), &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find OSVersion in the platform payload");
-        }
-        // Hardware Version
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_HARDWARE_VERSION, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find hw ver tag");
-        if(cbor_value_is_valid(&repVal))
-        {
-            err = cbor_value_dup_text_string(&repVal, &(info.hardwareVersion), &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find HWVersion in the platform payload");
-        }
-        // Firmware Version
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_FIRMWARE_VERSION, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find fw ver tag");
-        if(cbor_value_is_valid(&repVal))
-        {
-            err = cbor_value_dup_text_string(&repVal, &(info.firmwareVersion), &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find firmwareVersion in the platform payload");
-        }
-        // Support URL
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_SUPPORT_URL, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find support url tag");
-        if(cbor_value_is_valid(&repVal))
-        {
-            err = cbor_value_dup_text_string(&repVal, &(info.supportUrl), &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find supportUrl in the platform payload");
-        }
-        // System Time
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_SYSTEM_TIME, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find sys time tag");
-        if(cbor_value_is_valid(&repVal))
-        {
-            err = cbor_value_dup_text_string(&repVal, &(info.systemTime), &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find systemTume in the platform payload");
-        }
-
-        // Resource type
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_RESOURCE_TYPE, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find resource type tag");
-
-        if(cbor_value_is_valid(&repVal))
-        {
-            err = OCParseStringLL(rootValue, OC_RSRVD_RESOURCE_TYPE, &rt);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find resource type in the platform payload");
-        }
-
-        // Interface Types
-        err = cbor_value_map_find_value(rootValue, OC_RSRVD_INTERFACE, &repVal);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find interface tag");
-
-        if(cbor_value_is_valid(&repVal))
-        {
-            err =  OCParseStringLL(rootValue, OC_RSRVD_INTERFACE, &interfaces);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find interfaces tag/value");
-        }
-
-        err = cbor_value_advance(rootValue);
-        VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find supportUrl in the platform payload");
-
-       out = (OCPlatformPayload *)OCPlatformPayloadCreateAsOwner(&info);
-       out->rt = rt;
-       out->interfaces = interfaces;
-       *outPayload = (OCPayload *)out;
-       OIC_LOG_PAYLOAD(DEBUG, *outPayload);
-       return OC_STACK_OK;
-    }
-
-exit:
-    OCPlatformInfoDestroy(&info);
-    OIC_LOG(ERROR, TAG, "CBOR error In ParsePlatformPayload");
-    return ret;
-}
-
 static OCRepPayloadPropType DecodeCborType(CborType type)
 {
     switch (type)
@@ -906,8 +657,7 @@ static CborError OCParseArray(OCRepPayload *out, const char *name, CborValue *co
         res = OCRepPayloadSetNull(out, name);
         err = (CborError) !res;
         VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting value");
-        err = cbor_value_advance(container);
-        VERIFY_CBOR_SUCCESS(TAG, err, "Failed advancing container");
+        container = container + 1;
         return err;
     }
 
@@ -1014,6 +764,7 @@ static CborError OCParseSingleRepPayload(OCRepPayload **outPayload, CborValue *o
                 {
                     err = cbor_value_advance(&repMap);
                     OICFree(name);
+                    name = NULL;
                     continue;
                 }
             }
@@ -1143,7 +894,7 @@ static OCStackResult OCParseRepPayload(OCPayload **outPayload, CborValue *root)
         {
             err = cbor_value_map_find_value(&rootMap, OC_RSRVD_HREF, &curVal);
             VERIFY_CBOR_SUCCESS(TAG, err, "to find href tag");
-            if (cbor_value_is_valid(&curVal))
+            if (cbor_value_is_text_string(&curVal))
             {
                 size_t len = 0;
                 err = cbor_value_dup_text_string(&curVal, &temp->uri, &len, NULL);
@@ -1204,6 +955,7 @@ exit:
     return ret;
 }
 
+#ifdef WITH_PRESENCE
 static OCStackResult OCParsePresencePayload(OCPayload **outPayload, CborValue *rootValue)
 {
     OCStackResult ret = OC_STACK_INVALID_PARAM;
@@ -1249,7 +1001,7 @@ static OCStackResult OCParsePresencePayload(OCPayload **outPayload, CborValue *r
         // Resource type name
         err = cbor_value_map_find_value(rootValue, OC_RSRVD_RESOURCE_TYPE, &curVal);
         VERIFY_CBOR_SUCCESS(TAG, err, "to find res type tag");
-        if (cbor_value_is_valid(&curVal))
+        if (cbor_value_is_text_string(&curVal))
         {
             size_t len = 0;
             err = cbor_value_dup_text_string(&curVal, &payload->resourceType, &len, NULL);
@@ -1267,3 +1019,4 @@ exit:
     OCPresencePayloadDestroy(payload);
     return ret;
 }
+#endif // WITH_PRESENCE