Added Consumer API for enable to cloud service.
authorKIM JungYong <jyong2.kim@samsung.com>
Thu, 14 Jul 2016 06:23:27 +0000 (15:23 +0900)
committerUze Choi <uzchoi@samsung.com>
Mon, 18 Jul 2016 08:47:35 +0000 (08:47 +0000)
NSConsumerEnableRemoteService() is added for subscribe on cloud notification service.
Discover logic is updated for seperated TCP and cloud working flow.
Cloud flag is added on NSProviderConnectionInfo sturcture.

Change-Id: I09d5ba588ba081e0b91916554b8a13b8f8ae8467
Signed-off-by: KIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/9387
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
service/notification/include/NSConsumerInterface.h
service/notification/src/consumer/NSConsumerCommon.c
service/notification/src/consumer/NSConsumerCommon.h
service/notification/src/consumer/NSConsumerCommunication.c
service/notification/src/consumer/NSConsumerDiscovery.c
service/notification/src/consumer/NSConsumerInterface.c

index c028820..d7caaef 100644 (file)
@@ -83,6 +83,13 @@ NSResult NSStartConsumer(NSConsumerConfig config);
 NSResult NSStopConsumer();
 
 /**
+ * Request to discover to remote address as parameter.
+ * @param[in]  server address combined with IP address and port number using delimiter :
+ * @return ::NS_OK or result code of NSResult
+ */
+NSResult NSConsumerEnableRemoteService(char *serverAddress);
+
+/**
  * Request discovery manually
  * @return ::NS_OK or result code of NSResult
  */
index 5650f01..d9da4e0 100644 (file)
@@ -273,6 +273,7 @@ NSProviderConnectionInfo * NSCreateProviderConnections(OCDevAddr * inAddr)
     connections->addr = NULL;
     connections->messageHandle = NULL;
     connections->syncHandle = NULL;
+    connections->isCloudConnection = false;
     connections->next = NULL;
 
     if (inAddr)
@@ -301,6 +302,7 @@ NSProviderConnectionInfo * NSCopyProviderConnections(NSProviderConnectionInfo *
 
         copyInfo->messageHandle = tmp->messageHandle;
         copyInfo->syncHandle = tmp->syncHandle;
+        copyInfo->isCloudConnection = tmp->isCloudConnection;
         tmp = tmp->next;
         copyInfo = copyInfo->next;
     }
index e3f1047..d84e01b 100644 (file)
@@ -123,6 +123,14 @@ extern "C" {
         } \
     }
 
+typedef enum
+{
+    NS_DISCOVER_DEFAULT, // will work for adapter_ip.
+    NS_DISCOVER_UDP,
+    NS_DISCOVER_TCP,
+    NS_DISCOVER_CLOUD
+} NSConsumerDiscoverType;
+
 typedef struct NSProviderConnectionInfo
 {
     OCDevAddr * addr;
@@ -130,6 +138,8 @@ typedef struct NSProviderConnectionInfo
     OCDoHandle messageHandle;
     OCDoHandle syncHandle;
 
+    bool isCloudConnection;
+
     struct NSProviderConnectionInfo * next;
 
 } NSProviderConnectionInfo;
index cd968c5..3e0cf04 100644 (file)
@@ -57,8 +57,11 @@ NSResult NSConsumerSubscribeProvider(NSProvider * provider)
         if (connections->addr->adapter == OC_ADAPTER_TCP)
         {
             type = CT_ADAPTER_TCP;
-            msgUri = NSGetCloudUri(provider_internal->providerId, msgUri);
-            syncUri = NSGetCloudUri(provider_internal->providerId, syncUri);
+            if (connections->isCloudConnection == true)
+            {
+                msgUri = NSGetCloudUri(provider_internal->providerId, msgUri);
+                syncUri = NSGetCloudUri(provider_internal->providerId, syncUri);
+            }
         }
 
         NS_LOG(DEBUG, "get subscribe message query");
index 8e66d04..2918b2a 100644 (file)
@@ -35,6 +35,8 @@
 
 NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse);
 
+OCDevAddr * NSChangeAddress(const char * address);
+
 OCStackApplicationResult NSConsumerPresenceListener(
         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
 {
@@ -81,7 +83,6 @@ OCStackApplicationResult NSConsumerPresenceListener(
 OCStackApplicationResult NSProviderDiscoverListener(
         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
 {
-    (void) ctx;
     (void) handle;
 
     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
@@ -114,7 +115,7 @@ OCStackApplicationResult NSProviderDiscoverListener(
             }
 
             NSInvokeRequest(NULL, OC_REST_GET, clientResponse->addr,
-                    resource->uri, NULL, NSIntrospectProvider, NULL,
+                    resource->uri, NULL, NSIntrospectProvider, ctx,
                     type);
         }
         resource = resource->next;
@@ -135,7 +136,6 @@ void NSRemoveProviderObj(NSProvider_internal * provider)
 OCStackApplicationResult NSIntrospectProvider(
         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
 {
-    (void) ctx;
     (void) handle;
 
     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
@@ -159,6 +159,11 @@ OCStackApplicationResult NSIntrospectProvider(
 
     NSProvider_internal * newProvider = NSGetProvider(clientResponse);
     NS_VERIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
+    if (ctx && *((NSConsumerDiscoverType *)ctx) == NS_DISCOVER_CLOUD )
+    {
+        newProvider->connection->isCloudConnection = true;
+        NSOICFree(ctx);
+    }
 
     NS_LOG(DEBUG, "build NSTask");
     NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DISCOVERED, (void *) newProvider);
@@ -236,42 +241,110 @@ NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse)
     return newProvider;
 }
 
-void NSConsumerDiscoveryTaskProcessing(NSTask * task)
+OCDevAddr * NSChangeAddress(const char * address)
 {
-    NS_VERIFY_NOT_NULL_V(task);
+    NS_VERIFY_NOT_NULL(address, NULL);
+    OCDevAddr * retAddr = NULL;
 
-    NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
-    if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_CONSUMER_REQ_DISCOVER)
+    int index = 0;
+    while(address[index] != '\0')
     {
-        OCDevAddr * addr = (OCDevAddr *) task->taskData;
-        OCConnectivityType type = CT_DEFAULT;
-        if (addr)
+        if (address[index] == ':')
         {
-            type = addr->adapter;
+            break;
         }
+        index++;
+    }
 
-        NS_LOG(DEBUG, "Request discover [UDP]");
-        NSInvokeRequest(NULL, OC_REST_DISCOVER, addr, NS_DISCOVER_QUERY,
-                NULL, NSProviderDiscoverListener, NULL, type);
+    if (address[index] == '\0')
+    {
+        return NULL;
     }
-    else if (task->taskType == TASK_EVENT_CONNECTED_TCP)
+
+    int tmp = index + 1;
+    uint16_t port = address[tmp++];
+
+    while(address[tmp] != '\0')
     {
-        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(task->taskData, NSOICFree(task));
-        OCDevAddr * addr = (OCDevAddr *) task->taskData;
-        OCConnectivityType type = CT_ADAPTER_TCP;
-        if (addr)
+        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;
+    NSConsumerDiscoverType * callbackData = NULL;
+
+    if (address)
+    {
+        if (address->adapter == OC_ADAPTER_IP)
         {
-            type = addr->adapter;
+            type = CT_ADAPTER_IP;
+            NS_LOG(DEBUG, "Request discover [UDP]");
         }
+        else if (address->adapter == OC_ADAPTER_TCP)
+        {
+            type = CT_ADAPTER_TCP;
+            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);
 
-        NS_LOG(DEBUG, "Request discover [TCP]");
-        NSInvokeRequest(NULL, OC_REST_DISCOVER, addr, NS_DISCOVER_QUERY,
-                NULL, NSProviderDiscoverListener, NULL, type);
+            if (rType == NS_DISCOVER_CLOUD)
+            {
+                callbackData = (NSConsumerDiscoverType *)OICMalloc(sizeof(NSConsumerDiscoverType));
+                *callbackData = NS_DISCOVER_CLOUD;
+            }
+        }
+        else
+        {
+            NS_LOG_V(DEBUG, "Request discover But Adapter is not IP : %d", address->adapter);
+        }
+    }
+    else
+    {
+        NS_LOG(DEBUG, "Request Multicast discover [UDP]");
+    }
+
+    NSInvokeRequest(NULL, OC_REST_DISCOVER, address, NS_DISCOVER_QUERY,
+            NULL, NSProviderDiscoverListener, (void *)callbackData, type);
+}
+
+void NSConsumerDiscoveryTaskProcessing(NSTask * task)
+{
+    NS_VERIFY_NOT_NULL_V(task);
 
-        NS_LOG(DEBUG, "Subscribe presence [TCP]");
-        NSInvokeRequest(NULL, OC_REST_PRESENCE, addr, NS_PRESENCE_SUBSCRIBE_QUERY_TCP,
-                NULL, NSConsumerPresenceListener, NULL, type);
+    NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
+    if (task->taskType == TASK_CONSUMER_REQ_DISCOVER)
+    {
+        char * address = (char *) task->taskData;
+        NSConsumerDiscoverType dType = NS_DISCOVER_DEFAULT;
 
+        OCDevAddr * addr = NULL;
+        if (address)
+        {
+            addr = NSChangeAddress(address);
+            dType = NS_DISCOVER_CLOUD;
+        }
+
+        NSConsumerHandleRequestDiscover(addr, dType);
+        NSOICFree(task->taskData);
+        NSOICFree(addr);
+    }
+    else if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_EVENT_CONNECTED_TCP)
+    {
+        NSConsumerHandleRequestDiscover((OCDevAddr *) task->taskData, NS_DISCOVER_DEFAULT);
         NSOICFree(task->taskData);
     }
     else
index 617454d..70f1dc3 100644 (file)
@@ -67,6 +67,14 @@ NSResult NSStopConsumer()
     return NS_OK;
 }
 
+NSResult NSConsumerEnableRemoteService(char *serverAddress)
+{
+    NSTask * discoverTask = NSMakeTask(TASK_CONSUMER_REQ_DISCOVER, (void *)serverAddress);
+    NS_VERIFY_NOT_NULL(discoverTask, NS_ERROR);
+
+    return NSConsumerPushEvent(discoverTask);
+}
+
 NSResult NSSubscribe(NSProvider * provider)
 {
     NSTask * subscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) provider);