Disable auto-discovery of NSProvider over TCP.
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerNetworkEventListener.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <memory.h>
22 #include <string.h>
23
24 #include "NSConstants.h"
25 #include "NSConsumerCommon.h"
26 #include "cautilinterface.h"
27 #include "oic_malloc.h"
28 #include "oic_string.h"
29
30 #include "NSConsumerDiscovery.h"
31 #include "NSConsumerNetworkEventListener.h"
32
33 #define NS_PRESENCE_SUBSCRIBE_QUERY "/oic/ad?rt=x.org.iotivity.notification"
34
35 void NSConnectionStateListener(const CAEndpoint_t * info, bool isConnected);
36
37 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled);
38
39 OCDoHandle * getPresenceHandle()
40 {
41     static OCDoHandle g_PresenceHandle = NULL;
42
43     return & g_PresenceHandle;
44 }
45
46
47 NSResult NSConsumerListenerInit()
48 {
49     // TODO replace with comment lines when enable network monitor of IP Adapter
50     CARegisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener);
51 //    if (CARegisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener)
52 //            != CA_STATUS_OK)
53 //    {
54 //        return NS_ERROR;
55 //    }
56     OCStackResult stackResult;
57 #ifdef WITH_PRESENCE
58     NS_LOG(DEBUG, "Request to subscribe presence");
59     stackResult = NSInvokeRequest(getPresenceHandle(), OC_REST_PRESENCE, NULL,
60                         NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener,
61                         NULL, NULL, CT_DEFAULT);
62     NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(stackResult), NS_ERROR);
63 #endif
64     NS_LOG(DEBUG, "Request to discover provider");
65     stackResult = NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL,
66                       NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener,
67                       NULL, NULL, CT_DEFAULT);
68     NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(stackResult), NS_ERROR);
69
70     return NS_OK;
71 }
72
73 void NSConsumerListenerTermiate()
74 {
75     CAUnregisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener);
76     OCCancel(*getPresenceHandle(), NS_QOS, NULL, 0);
77 }
78
79 void NSConnectionStateListener(const CAEndpoint_t * info, bool connected)
80 {
81     NS_VERIFY_NOT_NULL_V(info);
82
83     NS_LOG_V(DEBUG, "adapter : %d", info->adapter);
84     NS_LOG_V(INFO_PRIVATE, "remote_address : %s:%d", info->addr, info->port);
85     NS_LOG_V(DEBUG, "isConnect : %d", connected);
86
87 #ifdef DISABLE_AUTO_TCP_NSPROVIDER_DISCOVERY
88     if (info->adapter == CA_ADAPTER_TCP)
89     {
90         NS_LOG(DEBUG, "Ignoring the TCP event.");
91         return;
92     }
93 #endif
94
95     NSTaskType type = TASK_EVENT_CONNECTED;
96     OCDevAddr * addr = NULL;
97     if (connected)
98     {
99         if (info->adapter == CA_ADAPTER_TCP)
100         {
101             type = TASK_EVENT_CONNECTED_TCP;
102             NS_LOG(DEBUG, "try to discover notification provider : TCP.");
103         }
104         else if (info->adapter == CA_ADAPTER_IP)
105         {
106             NS_LOG(DEBUG, "try to discover notification provider : IP.");
107         }
108
109         addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
110         NS_VERIFY_NOT_NULL_V(addr);
111
112         addr->adapter = (OCTransportAdapter) info->adapter;
113         OICStrcpy(addr->addr, MAX_ADDR_STR_SIZE, info->addr);
114         addr->flags = info->flags;
115         addr->ifindex = info->ifindex;
116         addr->port = info->port;
117
118         NSTask * task = NSMakeTask(type, addr);
119         NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(task, NSOICFree(addr));
120
121         NSConsumerPushEvent(task);
122     }
123 }
124
125 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled)
126 {
127     NS_LOG_V(DEBUG, "adapter : %d", adapter);
128     NS_LOG_V(DEBUG, "isEnabled : %d", enabled);
129
130     (void) adapter;
131
132     if (enabled)
133     {
134         NS_LOG(DEBUG, "try to discover notification provider.");
135
136         NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
137         NS_VERIFY_NOT_NULL_V(task);
138
139         NSConsumerPushEvent(task);
140     }
141 }