Revert "[CONPRO-1337] Disabled Presence Feature"
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerDiscovery.c
index fe84ff0..28c137e 100644 (file)
 
 #include <string.h>
 #include "NSCommon.h"
+#include "NSUtil.h"
 #include "NSConsumerCommon.h"
 #include "NSConstants.h"
 #include "ocpayload.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
 
-#define NS_DISCOVER_QUERY "/oic/res?rt=oic.r.notification"
-#define NS_PRESENCE_SUBSCRIBE_QUERY_TCP "/oic/ad?rt=oic.r.notification"
-#define NS_GET_INFORMATION_QUERY "/notification?if=oic.if.notification"
-
-NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse);
-
-OCDevAddr * NSChangeAddress(const char * address);
+#define NS_DISCOVER_QUERY "/oic/res?rt=x.org.iotivity.notification"
+#define NS_PRESENCE_SUBSCRIBE_QUERY_TCP "/oic/ad?rt=x.org.iotivity.notification"
 
 OCStackApplicationResult NSConsumerPresenceListener(
         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
@@ -43,10 +39,11 @@ OCStackApplicationResult NSConsumerPresenceListener(
     (void) handle;
 
     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
+    NS_VERIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
     NS_VERIFY_STACK_SUCCESS(
             NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
 
-    NS_LOG_V(DEBUG, "Presence income : %s:%d",
+    NS_LOG_V(INFO_PRIVATE, "Presence income : %s:%d",
             clientResponse->devAddr.addr, clientResponse->devAddr.port);
     NS_LOG_V(DEBUG, "Presence result : %d",
             clientResponse->result);
@@ -64,16 +61,23 @@ OCStackApplicationResult NSConsumerPresenceListener(
     if (payload->trigger == OC_PRESENCE_TRIGGER_DELETE ||
             clientResponse->result == OC_STACK_PRESENCE_STOPPED)
     {
-        // TODO find request and cancel
         NS_LOG(DEBUG, "stopped presence or resource is deleted.");
-        //OCCancel(handle, NS_QOS, NULL, 0);
+        NS_LOG(DEBUG, "build NSTask");
+        OCDevAddr * addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
+        NS_VERIFY_NOT_NULL(addr, OC_STACK_KEEP_TRANSACTION);
+        memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
+
+        NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DELETED, addr);
+        NS_VERIFY_NOT_NULL(task, OC_STACK_KEEP_TRANSACTION);
+
+        NSConsumerPushEvent(task);
     }
 
     else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
     {
         NS_LOG(DEBUG, "started presence or resource is created.");
         NSInvokeRequest(NULL, OC_REST_DISCOVER, clientResponse->addr,
-            NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL,
+            NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL, NULL,
             clientResponse->addr->adapter);
     }
 
@@ -89,7 +93,7 @@ OCStackApplicationResult NSProviderDiscoverListener(
     NS_VERIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
     NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
 
-    NS_LOG_V(DEBUG, "Discover income : %s:%d",
+    NS_LOG_V(INFO_PRIVATE, "Discover income : %s:%d",
             clientResponse->devAddr.addr, clientResponse->devAddr.port);
     NS_LOG_V(DEBUG, "Discover result : %d",
             clientResponse->result);
@@ -103,36 +107,43 @@ OCStackApplicationResult NSProviderDiscoverListener(
         return OC_STACK_DELETE_TRANSACTION;
     }
 
-    OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
-    while (resource)
+    OCDiscoveryPayload * discoveryPayload = (OCDiscoveryPayload *)clientResponse->payload;
+
+    while (discoveryPayload)
     {
-        if (strstr(resource->uri, NS_RESOURCE_URI))
+        OCResourcePayload * resource = discoveryPayload->resources;
+        while (resource)
         {
-            OCConnectivityType type = CT_DEFAULT;
-            if (clientResponse->addr->adapter == OC_ADAPTER_TCP)
+            NS_LOG_V(DEBUG, "Discovered resource uri : %s", resource->uri);
+            NS_VERIFY_NOT_NULL(resource->uri, OC_STACK_KEEP_TRANSACTION);
+            if (strstr(resource->uri, NS_RESOURCE_URI))
             {
-                type = CT_ADAPTER_TCP;
+                NS_LOG_V(DEBUG, "Request GET to provider : %s", resource->uri);
+                OCConnectivityType type = CT_DEFAULT;
+                if (clientResponse->addr->adapter == OC_ADAPTER_TCP)
+                {
+                    type = CT_ADAPTER_TCP;
+                }
+
+                OCDevAddr * addr = clientResponse->addr;
+                if (resource->secure)
+                {
+                    addr->port = resource->port;
+                    addr->flags |= OC_FLAG_SECURE;
+                }
+
+                NSInvokeRequest(NULL, OC_REST_GET, addr,
+                        resource->uri, NULL, NSIntrospectProvider, ctx,
+                        NULL, type);
             }
-
-            NSInvokeRequest(NULL, OC_REST_GET, clientResponse->addr,
-                    resource->uri, NULL, NSIntrospectProvider, ctx,
-                    type);
+            resource = resource->next;
         }
-        resource = resource->next;
+        discoveryPayload = discoveryPayload->next;
     }
 
     return OC_STACK_KEEP_TRANSACTION;
 }
 
-void NSRemoveProviderObj(NSProvider_internal * provider)
-{
-    NSOICFree(provider->messageUri);
-    NSOICFree(provider->syncUri);
-
-    NSRemoveConnections(provider->connection);
-    NSOICFree(provider);
-}
-
 OCStackApplicationResult NSIntrospectProvider(
         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
 {
@@ -141,7 +152,7 @@ OCStackApplicationResult NSIntrospectProvider(
     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
     NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
 
-    NS_LOG_V(DEBUG, "GET response income : %s:%d",
+    NS_LOG_V(INFO_PRIVATE, "GET response income : %s:%d",
             clientResponse->devAddr.addr, clientResponse->devAddr.port);
     NS_LOG_V(DEBUG, "GET response result : %d",
             clientResponse->result);
@@ -159,138 +170,29 @@ OCStackApplicationResult NSIntrospectProvider(
 
     NSProvider_internal * newProvider = NSGetProvider(clientResponse);
     NS_VERIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
-    if (ctx && *((NSConsumerDiscoverType *)ctx) == NS_DISCOVER_CLOUD )
+    if (ctx && ctx == (void *)NS_DISCOVER_CLOUD )
     {
         newProvider->connection->isCloudConnection = true;
-        NSOICFree(ctx);
     }
 
     NS_LOG(DEBUG, "build NSTask");
     NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DISCOVERED, (void *) newProvider);
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveProviderObj(newProvider));
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveProvider_internal(newProvider));
 
     NSConsumerPushEvent(task);
 
     return OC_STACK_KEEP_TRANSACTION;
 }
 
-void NSGetProviderPostClean(
-        char * pId, char * mUri, char * sUri, NSProviderConnectionInfo * connection)
-{
-    NSOICFree(pId);
-    NSOICFree(mUri);
-    NSOICFree(sUri);
-    NSRemoveConnections(connection);
-}
-
-NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse)
-{
-    NS_LOG(DEBUG, "create NSProvider");
-    NS_VERIFY_NOT_NULL(clientResponse->payload, NULL);
-
-    OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
-    while (payload)
-    {
-        NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
-        payload = payload->next;
-    }
-
-    payload = (OCRepPayload *)clientResponse->payload;
-
-    char * providerId = NULL;
-    char * messageUri = NULL;
-    char * syncUri = NULL;
-    int64_t accepter = 0;
-    NSProviderConnectionInfo * connection = NULL;
-
-    NS_LOG(DEBUG, "get information of accepter");
-    bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_POLICY, & accepter);
-    NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
-
-    NS_LOG(DEBUG, "get provider ID");
-    getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, & providerId);
-    NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
-
-    NS_LOG(DEBUG, "get message URI");
-    getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_MESSAGE, & messageUri);
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
-            NSGetProviderPostClean(providerId, messageUri, syncUri, connection));
-
-    NS_LOG(DEBUG, "get sync URI");
-    getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SYNC, & syncUri);
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
-            NSGetProviderPostClean(providerId, messageUri, syncUri, connection));
-
-    NS_LOG(DEBUG, "get provider connection information");
-    NS_VERIFY_NOT_NULL(clientResponse->addr, NULL);
-    connection = NSCreateProviderConnections(clientResponse->addr);
-    NS_VERIFY_NOT_NULL(connection, NULL);
-
-    NSProvider_internal * newProvider
-        = (NSProvider_internal *)OICMalloc(sizeof(NSProvider_internal));
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newProvider, NULL,
-          NSGetProviderPostClean(providerId, messageUri, syncUri, connection));
-
-    OICStrcpy(newProvider->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
-    NSOICFree(providerId);
-    newProvider->messageUri = messageUri;
-    newProvider->syncUri = syncUri;
-    newProvider->accessPolicy = (NSAccessPolicy)accepter;
-    newProvider->connection = connection;
-
-    return newProvider;
-}
-
-OCDevAddr * NSChangeAddress(const char * address)
-{
-    NS_VERIFY_NOT_NULL(address, NULL);
-    OCDevAddr * retAddr = NULL;
-
-    int index = 0;
-    while(address[index] != '\0')
-    {
-        if (address[index] == ':')
-        {
-            break;
-        }
-        index++;
-    }
-
-    if (address[index] == '\0')
-    {
-        return NULL;
-    }
-
-    int tmp = index + 1;
-    uint16_t port = address[tmp++];
-
-    while(address[tmp] != '\0')
-    {
-        port *= 10;
-        port += address[tmp++] - '0';
-    }
-
-    retAddr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
-    NS_VERIFY_NOT_NULL(retAddr, NULL);
-
-    retAddr->adapter = OC_ADAPTER_TCP;
-    OICStrcpy(retAddr->addr, index - 1, address);
-    retAddr->addr[index] = '\0';
-    retAddr->port = port;
-
-    return retAddr;
-}
-
 void NSConsumerHandleRequestDiscover(OCDevAddr * address, NSConsumerDiscoverType rType)
 {
-    OCConnectivityType type = CT_DEFAULT;
+    OCConnectivityType type = CT_ADAPTER_IP;
     NSConsumerDiscoverType * callbackData = NULL;
 
     if (address)
     {
         if (address->adapter == OC_ADAPTER_IP)
         {
-            type = CT_ADAPTER_IP;
             NS_LOG(DEBUG, "Request discover [UDP]");
         }
         else if (address->adapter == OC_ADAPTER_TCP)
@@ -299,17 +201,16 @@ void NSConsumerHandleRequestDiscover(OCDevAddr * address, NSConsumerDiscoverType
             NS_LOG(DEBUG, "Request discover and subscribe presence [TCP]");
             NS_LOG(DEBUG, "Subscribe presence [TCP]");
             NSInvokeRequest(NULL, OC_REST_PRESENCE, address, NS_PRESENCE_SUBSCRIBE_QUERY_TCP,
-                    NULL, NSConsumerPresenceListener, NULL, type);
+                    NULL, NSConsumerPresenceListener, NULL, NULL, type);
 
             if (rType == NS_DISCOVER_CLOUD)
             {
-                callbackData = (NSConsumerDiscoverType *)OICMalloc(sizeof(NSConsumerDiscoverType));
-                *callbackData = NS_DISCOVER_CLOUD;
+                callbackData = (void *) NS_DISCOVER_CLOUD;
             }
         }
         else
         {
-            NS_LOG_V(DEBUG, "Request discover But Adapter is not IP : %d", address->adapter);
+            NS_LOG_V(INFO_PRIVATE, "Request discover But Adapter is not IP : %d", address->adapter);
         }
     }
     else
@@ -318,7 +219,7 @@ void NSConsumerHandleRequestDiscover(OCDevAddr * address, NSConsumerDiscoverType
     }
 
     NSInvokeRequest(NULL, OC_REST_DISCOVER, address, NS_DISCOVER_QUERY,
-            NULL, NSProviderDiscoverListener, (void *)callbackData, type);
+            NULL, NSProviderDiscoverListener, (void *)callbackData, NULL, type);
 }
 
 void NSConsumerDiscoveryTaskProcessing(NSTask * task)