[IOT-980] oic.if.ll support for /oic/res
authorHabib Virji <habib.virji@samsung.com>
Thu, 17 Mar 2016 13:31:37 +0000 (13:31 +0000)
committerHabib Virji <habib.virji@samsung.com>
Tue, 19 Apr 2016 08:53:40 +0000 (08:53 +0000)
/oic/res currently was supporting only oic.if.baseline.

Support for oic.if.ll has been added. It includes unit test as a callback.

Change-Id: I8b90c7c686b753c5d41191508866b0c23bcc6578
Signed-off-by: Habib Virji <habib.virji@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/6025
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
(cherry picked from commit 8b297671a821177b41e62d39aa4d2d1bb6114235)
Reviewed-on: https://gerrit.iotivity.org/gerrit/7813

resource/csdk/stack/src/ocpayloadparse.c
resource/csdk/stack/src/ocresource.c
resource/csdk/stack/test/stacktests.cpp

index daa94fe..f06aec4 100755 (executable)
@@ -261,7 +261,13 @@ static OCStackResult OCParseDiscoveryPayload(OCPayload **outPayload, CborValue *
 
         // Interface Types
         err =  OCParseStringLL(&resourceMap, OC_RSRVD_INTERFACE, &resource->interfaces);
-        VERIFY_CBOR_SUCCESS(TAG, err, "to find interface tag/value");
+        if (CborNoError != err)
+        {
+            if (!OCResourcePayloadAddStringLL(&resource->interfaces, OC_RSRVD_INTERFACE_LL))
+            {
+                err = CborErrorOutOfMemory;
+            }
+        }
 
         // Policy
         CborValue policyMap;
index f4d0698..d338499 100755 (executable)
@@ -673,58 +673,68 @@ static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource
     // Step 1: Generate the response to discovery request
     if (virtualUriInRequest == OC_WELL_KNOWN_URI)
     {
-        char *filterOne = NULL;
-        char *filterTwo = NULL;
+        char *interfaceQuery = NULL;
+        char *resourceTypeQuery = NULL;
 
         discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
-                &filterOne, &filterTwo);
+                &interfaceQuery, &resourceTypeQuery);
 
         if (discoveryResult == OC_STACK_OK)
         {
             payload = (OCPayload *)OCDiscoveryPayloadCreate();
 
-            if(payload)
+            if (payload)
             {
                 ((OCDiscoveryPayload*)payload)->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
                 VERIFY_NON_NULL(((OCDiscoveryPayload*)payload)->sid, ERROR, OC_STACK_NO_MEMORY);
                 memcpy(((OCDiscoveryPayload*)payload)->sid, OCGetServerInstanceIDString(), UUID_STRING_SIZE);
 
-                bool foundResourceAtRD = false;
-                for(;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
+                if (interfaceQuery && 0 == strcmp(OC_RSRVD_INTERFACE_LL, interfaceQuery))
                 {
-#ifdef WITH_RD
-                    if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
+                    for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
                     {
-                        OCResource *resource = NULL;
-                        OCDevAddr devAddr;
-                        discoveryResult = checkResourceExistsAtRD(filterOne, filterTwo,
-                                                                  &resource, &devAddr);
-                        if (discoveryResult != OC_STACK_OK)
+                        discoveryResult = BuildVirtualResourceResponse(resource,
+                                (OCDiscoveryPayload *)payload, &request->devAddr, false);
+                    }
+                }
+                else
+                {
+                    bool foundResourceAtRD = false;
+                    for (;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
+                    {
+#ifdef WITH_RD
+                        if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
                         {
-                             break;
+                            OCResource *resource1 = NULL;
+                            OCDevAddr devAddr;
+                            discoveryResult = checkResourceExistsAtRD(interfaceQuery,
+                                resourceTypeQuery, &resource1, &devAddr);
+                            if (discoveryResult != OC_STACK_OK)
+                            {
+                                 break;
+                            }
+                            discoveryResult = BuildVirtualResourceResponse(resource1,
+                                (OCDiscoveryPayload*)payload, &devAddr, true);
+                            if (payload)
+                            {
+                                ((OCDiscoveryPayload*)payload)->baseURI = OICStrdup(devAddr.addr);
+                            }
+                            OCDiscoveryResourceDestroy(resource1);
+                            foundResourceAtRD = true;
                         }
-                        discoveryResult = BuildVirtualResourceResponse(resource,
-                                    (OCDiscoveryPayload*)payload,
-                                    &devAddr, true);
-                        if (payload)
+#endif
+                        if (!foundResourceAtRD && includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
                         {
-                            ((OCDiscoveryPayload*)payload)->baseURI = OICStrdup(devAddr.addr);
+                            discoveryResult = BuildVirtualResourceResponse(resource,
+                                (OCDiscoveryPayload*)payload, &request->devAddr, false);
                         }
-                        foundResourceAtRD = true;
                     }
-#endif
-                    if(!foundResourceAtRD && includeThisResourceInResponse(resource, filterOne, filterTwo))
+                    // Set discoveryResult appropriately if no 'valid' resources are available
+                    if (((OCDiscoveryPayload*)payload)->resources == NULL && !foundResourceAtRD)
                     {
-                        discoveryResult = BuildVirtualResourceResponse(resource,
-                                (OCDiscoveryPayload*)payload,
-                                &request->devAddr, false);
+                        discoveryResult = OC_STACK_NO_RESOURCE;
                     }
                 }
-                // Set discoveryResult appropriately if no 'valid' resources are available
-                if (((OCDiscoveryPayload*)payload)->resources == NULL && !foundResourceAtRD)
-                {
-                    discoveryResult = OC_STACK_NO_RESOURCE;
-                }
             }
             else
             {
index e259d5b..ae60c5b 100755 (executable)
@@ -91,6 +91,40 @@ static void resultCallback(OCDPDev_t *UNUSED1, OCStackResult UNUSED2)
     (void) (UNUSED2);
 }
 
+extern "C" OCStackApplicationResult discoveryCallback(void* ctx,
+        OCDoHandle /*handle*/, OCClientResponse * clientResponse)
+{
+    OIC_LOG(INFO, TAG, "Entering asyncDoResourcesCallback");
+
+    EXPECT_EQ(OC_STACK_OK, clientResponse->result);
+
+    if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
+    {
+        OIC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
+    }
+
+    OIC_LOG_V(INFO, TAG, "result = %d", clientResponse->result);
+
+    OCDiscoveryPayload *discoveryPayload = ((OCDiscoveryPayload *) clientResponse->payload);
+    EXPECT_TRUE(discoveryPayload != NULL);
+    OCResourcePayload *res = discoveryPayload->resources;
+    size_t count = 0;
+    for (OCResourcePayload *res1 = discoveryPayload->resources; res1; res1 = res1->next)
+    {
+        count++;
+    }
+    EXPECT_EQ(3, count);
+    EXPECT_EQ("/a/led1", res->uri);
+    res = res->next;
+    EXPECT_EQ("/a/led2", res->uri);
+    res = res->next;
+    EXPECT_EQ("/a/led3", res->uri);
+    res = res->next;
+    EXPECT_TRUE(res == NULL);
+
+    return OC_STACK_KEEP_TRANSACTION;
+}
+
 //-----------------------------------------------------------------------------
 // Entity handler
 //-----------------------------------------------------------------------------
@@ -1656,3 +1690,56 @@ TEST(OCDoDirectPairingTests, NullpinNumber)
 {
     EXPECT_EQ(OC_STACK_INVALID_PARAM,OCDoDirectPairing(&peer, pmSel, NULL, &resultCallback));
 }
+
+TEST(StackResource, MultipleResourcesDiscovery)
+{
+    itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
+    OIC_LOG(INFO, TAG, "Starting MultipleResourcesDiscovery test");
+    InitStack(OC_SERVER);
+
+    OCResourceHandle handle1;
+    EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle1,
+                                            "core.led",
+                                            "core.rw",
+                                            "/a/led1",
+                                            0,
+                                            NULL,
+                                            OC_DISCOVERABLE|OC_OBSERVABLE));
+
+    OCResourceHandle handle2;
+    EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle2,
+                                            "core.led",
+                                            "core.rw",
+                                            "/a/led2",
+                                            0,
+                                            NULL,
+                                            OC_DISCOVERABLE|OC_OBSERVABLE));
+    OCResourceHandle handle3;
+    EXPECT_EQ(OC_STACK_OK, OCCreateResource(&handle3,
+                                            "core.led",
+                                            "core.rw",
+                                            "/a/led3",
+                                            0,
+                                            NULL,
+                                            OC_DISCOVERABLE|OC_OBSERVABLE));
+    /* Start a discovery query*/
+    char szQueryUri[256] = "/oic/res?if=oic.if.ll";
+    OCCallbackData cbData;
+    cbData.cb = discoveryCallback;
+    cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
+    cbData.cd = NULL;
+
+    OCDoHandle handle;
+    EXPECT_EQ(OC_STACK_OK, OCDoResource(&handle,
+                                        OC_REST_DISCOVER,
+                                        szQueryUri,
+                                        0,
+                                        0,
+                                        CT_ADAPTER_IP,
+                                        OC_LOW_QOS,
+                                        &cbData,
+                                        NULL,
+                                        0));
+
+    EXPECT_EQ(OC_STACK_OK, OCStop());
+}