// 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
{
(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
//-----------------------------------------------------------------------------
{
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());
+}