Added logic for discovering Provider in TCP transport
[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
29 #include "NSConsumerDiscovery.h"
30 #include "NSConsumerNetworkEventListener.h"
31
32 #define NS_PRESENCE_SUBSCRIBE_QUERY "coap://224.0.1.187:5683/oic/ad?rt=oic.r.notification"
33
34 void NSConnectionStateListener(CATransportAdapter_t adapter,
35         const char *remote_address, bool connected);
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
57     NS_LOG(DEBUG, "Request to subscribe presence");
58     OCStackResult stackResult = NSInvokeRequest(getPresenceHandle(), OC_REST_PRESENCE, NULL,
59                         NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener,
60                         NULL, CT_DEFAULT);
61     NS_VERIFY_STACK_OK(stackResult, NS_ERROR);
62
63     NS_LOG(DEBUG, "Request to discover provider");
64     stackResult = NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL,
65                       NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener,
66                       NULL, CT_DEFAULT);
67     NS_VERIFY_STACK_OK(stackResult, NS_ERROR);
68
69     return NS_OK;
70 }
71
72 void NSConsumerListenerTermiate()
73 {
74     CARegisterNetworkMonitorHandler(NULL, NULL);
75     OCCancel(*getPresenceHandle(), NS_QOS, NULL, 0);
76 }
77
78 void NSConnectionStateListener(CATransportAdapter_t adapter,
79         const char *remote_address, bool connected)
80 {
81     NS_LOG_V(DEBUG, "adapter : %d", adapter);
82     NS_LOG_V(DEBUG, "remote_address : %s", remote_address);
83     NS_LOG_V(DEBUG, "isConnect : %d", connected);
84
85     (void) adapter;
86     (void) remote_address;
87
88     NSTaskType type = TASK_EVENT_CONNECTED;
89     OCDevAddr * addr = NULL;
90     if (connected)
91     {
92         if (adapter == CA_ADAPTER_TCP)
93         {
94             type = TASK_EVENT_CONNECTED_TCP;
95             NS_LOG(DEBUG, "try to discover notification provider : TCP.");
96             // TODO convet to OCDevAddr;
97             // addr = .....
98         }
99         else if (adapter == CA_ADAPTER_IP)
100         {
101             NS_LOG(DEBUG, "try to discover notification provider.");
102             // TODO convet to OCDevAddr;
103             // addr = .....
104         }
105
106         NSTask * task = NSMakeTask(type, addr);
107         NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(task, NSOICFree(addr));
108
109         NSConsumerPushEvent(task);
110     }
111 }
112
113 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled)
114 {
115     NS_LOG_V(DEBUG, "adapter : %d", adapter);
116     NS_LOG_V(DEBUG, "isEnabled : %d", enabled);
117
118     (void) adapter;
119
120     if (enabled)
121     {
122         NS_LOG(DEBUG, "try to discover notification provider.");
123
124         NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
125         NS_VERIFY_NOT_NULL_V(task);
126
127         NSConsumerPushEvent(task);
128     }
129 }