Disable auto-discovery of NSProvider over TCP.
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerNetworkEventListener.c
index 1dd6764..078b6a5 100644 (file)
 #include "NSConstants.h"
 #include "NSConsumerCommon.h"
 #include "cautilinterface.h"
+#include "oic_malloc.h"
+#include "oic_string.h"
 
 #include "NSConsumerDiscovery.h"
 #include "NSConsumerNetworkEventListener.h"
 
-#define NS_PRESENCE_SUBSCRIBE_QUERY "coap://224.0.1.187:5683/oic/ad?rt=oic.r.notification"
+#define NS_PRESENCE_SUBSCRIBE_QUERY "/oic/ad?rt=x.org.iotivity.notification"
 
-void NSConnectionStateListener(CATransportAdapter_t adapter,
-        const char *remote_address, bool connected);
+void NSConnectionStateListener(const CAEndpoint_t * info, bool isConnected);
 
 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled);
 
@@ -52,42 +53,70 @@ NSResult NSConsumerListenerInit()
 //    {
 //        return NS_ERROR;
 //    }
-
+    OCStackResult stackResult;
+#ifdef WITH_PRESENCE
     NS_LOG(DEBUG, "Request to subscribe presence");
-    OCStackResult stackResult = NSInvokeRequest(getPresenceHandle(), OC_REST_PRESENCE, NULL,
-                        NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener, NULL);
-    NS_VERTIFY_STACK_OK(stackResult, NS_ERROR);
-
+    stackResult = NSInvokeRequest(getPresenceHandle(), OC_REST_PRESENCE, NULL,
+                        NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener,
+                        NULL, NULL, CT_DEFAULT);
+    NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(stackResult), NS_ERROR);
+#endif
     NS_LOG(DEBUG, "Request to discover provider");
     stackResult = NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL,
-                      NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL);
-    NS_VERTIFY_STACK_OK(stackResult, NS_ERROR);
+                      NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener,
+                      NULL, NULL, CT_DEFAULT);
+    NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(stackResult), NS_ERROR);
 
     return NS_OK;
 }
 
 void NSConsumerListenerTermiate()
 {
-    CARegisterNetworkMonitorHandler(NULL, NULL);
+    CAUnregisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener);
     OCCancel(*getPresenceHandle(), NS_QOS, NULL, 0);
 }
 
-void NSConnectionStateListener(CATransportAdapter_t adapter,
-        const char *remote_address, bool connected)
+void NSConnectionStateListener(const CAEndpoint_t * info, bool connected)
 {
-    NS_LOG_V(DEBUG, "adapter : %d", adapter);
-    NS_LOG_V(DEBUG, "remote_address : %s", remote_address);
+    NS_VERIFY_NOT_NULL_V(info);
+
+    NS_LOG_V(DEBUG, "adapter : %d", info->adapter);
+    NS_LOG_V(INFO_PRIVATE, "remote_address : %s:%d", info->addr, info->port);
     NS_LOG_V(DEBUG, "isConnect : %d", connected);
 
-    (void) adapter;
-    (void) remote_address;
+#ifdef DISABLE_AUTO_TCP_NSPROVIDER_DISCOVERY
+    if (info->adapter == CA_ADAPTER_TCP)
+    {
+        NS_LOG(DEBUG, "Ignoring the TCP event.");
+        return;
+    }
+#endif
 
+    NSTaskType type = TASK_EVENT_CONNECTED;
+    OCDevAddr * addr = NULL;
     if (connected)
     {
-        NS_LOG(DEBUG, "try to discover notification provider.");
-
-        NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
-        NS_VERTIFY_NOT_NULL_V(task);
+        if (info->adapter == CA_ADAPTER_TCP)
+        {
+            type = TASK_EVENT_CONNECTED_TCP;
+            NS_LOG(DEBUG, "try to discover notification provider : TCP.");
+        }
+        else if (info->adapter == CA_ADAPTER_IP)
+        {
+            NS_LOG(DEBUG, "try to discover notification provider : IP.");
+        }
+
+        addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
+        NS_VERIFY_NOT_NULL_V(addr);
+
+        addr->adapter = (OCTransportAdapter) info->adapter;
+        OICStrcpy(addr->addr, MAX_ADDR_STR_SIZE, info->addr);
+        addr->flags = info->flags;
+        addr->ifindex = info->ifindex;
+        addr->port = info->port;
+
+        NSTask * task = NSMakeTask(type, addr);
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(task, NSOICFree(addr));
 
         NSConsumerPushEvent(task);
     }
@@ -105,7 +134,7 @@ void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled)
         NS_LOG(DEBUG, "try to discover notification provider.");
 
         NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
-        NS_VERTIFY_NOT_NULL_V(task);
+        NS_VERIFY_NOT_NULL_V(task);
 
         NSConsumerPushEvent(task);
     }