IOT-1080 Update rt and if to send array of string over the air
authorHabib Virji <habib.virji@samsung.com>
Mon, 11 Apr 2016 22:30:09 +0000 (23:30 +0100)
committerHabib Virji <habib.virji@samsung.com>
Fri, 22 Apr 2016 14:14:09 +0000 (14:14 +0000)
It was being return in form of "oic.if.ll oic.if.baseline". It is now changed to
["oic.if.ll", "oic.if.baseline"]

Change-Id: Ie32a0c2135a6f9994193d6be85fedb7041a68a70
Signed-off-by: Habib Virji <habib.virji@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/7749
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/7839

resource/csdk/stack/src/ocpayloadconvert.c
resource/csdk/stack/src/ocpayloadparse.c

index 865cc23..c187f71 100644 (file)
@@ -190,33 +190,29 @@ exit:
     return checkError(err, &encoder, outPayload, size);
 }
 
-static char* OCStringLLJoin(OCStringLL* val)
+static int64_t OCStringLLJoin(CborEncoder *map, char *type, OCStringLL *val)
 {
-    OCStringLL* temp = val;
-    size_t size = strlen(temp->value);
+    uint16_t count = 0;
+    int64_t err = CborNoError;
 
-    while (temp->next)
+    for (OCStringLL *temp = val; temp; temp = temp->next)
     {
-        ++size;
-        temp = temp->next;
-        size += strlen(temp->value);
+        ++count;
     }
-
-    char* joinedStr = (char*)OICCalloc(sizeof(char), size + 1);
-
-    if (!joinedStr)
+    if (count > 0)
     {
-        return NULL;
+        CborEncoder array;
+        err |= cbor_encode_text_string(map, type, strlen(type));
+        err |= cbor_encoder_create_array(map, &array, count);
+        while (val)
+        {
+            err |= cbor_encode_text_string(&array, val->value, strlen(val->value));
+            val = val->next;
+        }
+        err |= cbor_encoder_close_container(map, &array);
     }
 
-    OICStrcat(joinedStr, size + 1, val->value);
-    while (val->next)
-    {
-        val = val->next;
-        OICStrcat(joinedStr, size + 1, " ");
-        OICStrcat(joinedStr, size + 1, val->value);
-    }
-    return joinedStr;
+    return err;
 }
 
 static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload *payload, uint8_t *outPayload,
@@ -288,14 +284,8 @@ static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload *payload, uint8_t *o
         // Insert interfaces
         if (payload->interface)
         {
-            err |= cbor_encode_text_string(&rootMap, OC_RSRVD_INTERFACE,
-                    sizeof(OC_RSRVD_INTERFACE) - 1);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interface types tag");
-            char *joinedTypes = OCStringLLJoin(payload->interface);
-            VERIFY_PARAM_NON_NULL(TAG, joinedTypes, "Failed creating joined string");
-            err |= cbor_encode_text_string(&rootMap, joinedTypes, strlen(joinedTypes));
-            OICFree(joinedTypes);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interface types value");
+            err |= OCStringLLJoin(&rootMap, OC_RSRVD_INTERFACE, payload->interface);
+            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interface types tag/value");
         }
 
         // Insert baseURI if present
@@ -330,26 +320,14 @@ static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload *payload, uint8_t *o
             // Resource Type
             if (resource->types)
             {
-                err |= cbor_encode_text_string(&linkMap, OC_RSRVD_RESOURCE_TYPE,
-                        sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
-                VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource types tag to links map");
-                char *joinedTypes = OCStringLLJoin(resource->types);
-                VERIFY_PARAM_NON_NULL(TAG, joinedTypes, "Failed creating joined string");
-                err |= cbor_encode_text_string(&linkMap, joinedTypes, strlen(joinedTypes));
-                OICFree(joinedTypes);
-                VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource types value to links map");
+                err |= OCStringLLJoin(&linkMap, OC_RSRVD_RESOURCE_TYPE, resource->types);
+                VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resourceType tag/value to links map");
             }
             // Interface Types
             if (resource->interfaces)
             {
-                err |= cbor_encode_text_string(&linkMap, OC_RSRVD_INTERFACE,
-                        sizeof(OC_RSRVD_INTERFACE) - 1);
-                VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interfaces tag to links map");
-                char* joinedInterfaces = OCStringLLJoin(resource->interfaces);
-                VERIFY_PARAM_NON_NULL(TAG, joinedInterfaces, "Failed creating joined string");
-                err |= cbor_encode_text_string(&linkMap, joinedInterfaces, strlen(joinedInterfaces));
-                OICFree(joinedInterfaces);
-                VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interfaces value to links map");
+                err |= OCStringLLJoin(&linkMap, OC_RSRVD_INTERFACE, resource->interfaces);
+                VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interfaces tag/value to links map");
             }
 
             // Policy
@@ -423,27 +401,15 @@ static int64_t OCConvertDevicePayload(OCDevicePayload *payload, uint8_t *outPayl
     if (payload->types)
     {
         OIC_LOG(INFO, TAG, "Payload has types");
-        err |= cbor_encode_text_string(&repMap, OC_RSRVD_RESOURCE_TYPE,
-                sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
-        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep resource type tag");
-        char *joinedTypes = OCStringLLJoin(payload->types);
-        VERIFY_PARAM_NON_NULL(TAG, joinedTypes, "Failed creating joined string");
-        err |= cbor_encode_text_string(&repMap, joinedTypes, strlen(joinedTypes));
-        OICFree(joinedTypes);
-        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep resource type value");
+        err |= OCStringLLJoin(&repMap, OC_RSRVD_RESOURCE_TYPE, payload->types);
+        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type tag/value.");
     }
 
     if (payload->interfaces)
     {
         OIC_LOG(INFO, TAG, "Payload has interface");
-        err |= cbor_encode_text_string(&repMap, OC_RSRVD_INTERFACE,
-                sizeof(OC_RSRVD_INTERFACE) - 1);
-        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interface type tag");
-        char *joinedTypes = OCStringLLJoin(payload->interfaces);
-        VERIFY_PARAM_NON_NULL(TAG, joinedTypes, "Failed creating joined string");
-        err |= cbor_encode_text_string(&repMap, joinedTypes, strlen(joinedTypes));
-        OICFree(joinedTypes);
-        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep resource type value");
+        err |= OCStringLLJoin(&repMap, OC_RSRVD_INTERFACE, payload->interfaces);
+        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interface type tag/value.");
     }
 
     // Device ID
@@ -544,20 +510,14 @@ static int64_t OCConvertPlatformPayload(OCPlatformPayload *payload, uint8_t *out
     {
         err |= ConditionalAddTextStringToMap(&repMap, OC_RSRVD_RESOURCE_TYPE,
                 sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->rt);
-        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type");
+        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type.");
     }
 
     // Resource interfaces
     if (payload->interfaces)
     {
-        err |= cbor_encode_text_string(&repMap, OC_RSRVD_INTERFACE,
-                sizeof(OC_RSRVD_INTERFACE) - 1);
-        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform interface tag");
-        char* joinedInterfaces = OCStringLLJoin(payload->interfaces);
-        VERIFY_PARAM_NON_NULL(TAG, joinedInterfaces, "Failed creating joined string");
-        err |= cbor_encode_text_string(&repMap, joinedInterfaces, strlen(joinedInterfaces));
-        OICFree(joinedInterfaces);
-        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform interface value");
+        err |= OCStringLLJoin(&repMap, OC_RSRVD_INTERFACE, payload->interfaces);
+        VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform interface type.");
     }
 
     // Close Map
@@ -783,26 +743,14 @@ static int64_t OCConvertRepPayload(OCRepPayload *payload, uint8_t *outPayload, s
         if (payload->types)
         {
             OIC_LOG(INFO, TAG, "Payload has types");
-            err |= cbor_encode_text_string(&rootMap, OC_RSRVD_RESOURCE_TYPE,
-                    sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep resource type tag");
-            char* joinedTypes = OCStringLLJoin(payload->types);
-            VERIFY_PARAM_NON_NULL(TAG, joinedTypes, "Failed creating joined string");
-            err |= cbor_encode_text_string(&rootMap, joinedTypes, strlen(joinedTypes));
-            OICFree(joinedTypes);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep resource type value");
+            err |= OCStringLLJoin(&rootMap, OC_RSRVD_RESOURCE_TYPE, payload->types);
+            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type.");
         }
         if (payload->interfaces)
         {
             OIC_LOG(INFO, TAG, "Payload has interfaces");
-            err |= cbor_encode_text_string(&rootMap, OC_RSRVD_INTERFACE,
-                    sizeof(OC_RSRVD_INTERFACE) - 1);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep interface tag");
-            char* joinedInterfaces = OCStringLLJoin(payload->interfaces);
-            VERIFY_PARAM_NON_NULL(TAG, joinedInterfaces, "Failed creating joined string");
-            err |= cbor_encode_text_string(&rootMap, joinedInterfaces, strlen(joinedInterfaces));
-            OICFree(joinedInterfaces);
-            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep interface value");
+            err |= OCStringLLJoin(&rootMap, OC_RSRVD_INTERFACE, payload->interfaces);
+            VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform interface type.");
         }
 
         err |= OCConvertSingleRepPayload(&rootMap, payload);
index 37e33e0..65a3c06 100755 (executable)
@@ -153,31 +153,40 @@ static CborError OCParseStringLL(CborValue *map, char *type, OCStringLL **resour
     CborError err = cbor_value_map_find_value(map, type, &val);
     VERIFY_CBOR_SUCCESS(TAG, err, "to find StringLL TAG");
 
-    if (cbor_value_is_text_string(&val))
+    if (cbor_value_is_array(&val))
     {
-        char *input = NULL;
-        char *savePtr = NULL;
-        size_t len = 0;
-
-        err = cbor_value_dup_text_string(&val, &input, &len, NULL);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find StringLL value");
-
-        if (input)
+        CborValue txtStr;
+        err = cbor_value_enter_container(&val, &txtStr);
+        VERIFY_CBOR_SUCCESS(TAG, err, "to enter container");
+        while (cbor_value_is_text_string(&txtStr))
         {
-            char *curPtr = strtok_r(input, " ", &savePtr);
-            while (curPtr)
+            size_t len = 0;
+            char *input = NULL;
+            err = cbor_value_dup_text_string(&txtStr, &input, &len, NULL);
+            VERIFY_CBOR_SUCCESS(TAG, err, "to find StringLL value.");
+            if (input)
             {
-                char *trimmed = InPlaceStringTrim(curPtr);
-                if (trimmed[0] !='\0')
+                char *savePtr = NULL;
+                char *curPtr = strtok_r(input, " ", &savePtr);
+                while (curPtr)
                 {
-                    if (!OCResourcePayloadAddStringLL(resource, trimmed))
+                    char *trimmed = InPlaceStringTrim(curPtr);
+                    if (trimmed[0] !='\0')
                     {
-                        return CborErrorOutOfMemory;
+                        if (!OCResourcePayloadAddStringLL(resource, trimmed))
+                        {
+                            return CborErrorOutOfMemory;
+                        }
                     }
+                    curPtr = strtok_r(NULL, " ", &savePtr);
                 }
-                curPtr = strtok_r(NULL, " ", &savePtr);
+                OICFree(input);
+            }
+            if (cbor_value_is_text_string(&txtStr))
+            {
+                err = cbor_value_advance(&txtStr);
+                VERIFY_CBOR_SUCCESS(TAG, err, "to advance string value");
             }
-            OICFree(input);
         }
     }
 exit: