added cacheadapter interface and memorycache of notification.
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerListener.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 "NSConsumerListener.h"
22
23 #include <memory.h>
24 #include <string.h>
25
26 #include "NSConstants.h"
27 #include "NSConsumerCommon.h"
28 #include "payload_logging.h"
29 #include "cautilinterface.h"
30
31 #include "NSConsumerDiscovery.h"
32
33 #define NS_PRESENCE_SUBSCRIBE_QUERY "coap://224.0.1.187:5683/oic/ad?rt=oic.r.notification"
34
35 typedef OCStackApplicationResult(*onRIResponse)(OCDoHandle, OCClientResponse *);
36
37 OCStackApplicationResult NSRIResponseListener(
38         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse);
39
40 void NSConnectionStateListener(CATransportAdapter_t adapter,
41         const char *remote_address, bool connected);
42
43 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled);
44
45 NSResult NSConsumerListenerInit()
46 {
47     NSSetResponseListener(NSRIResponseListener);
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     if (OC_STACK_OK != NSRequestToResourceIntrospection(NULL, OC_REST_PRESENCE, NULL,
58             NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener))
59     {
60         NS_LOG(ERROR, "Presence request fail");
61         return NS_ERROR;
62     }
63
64     if (OC_STACK_OK != NSRequestToResourceIntrospection(NULL, OC_REST_DISCOVER, NULL,
65             NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener))
66     {
67         NS_LOG(ERROR, "Discover request fail");
68         return NS_ERROR;
69     }
70
71     return NS_OK;
72 }
73
74 OCStackApplicationResult NSRIResponseListener(
75         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
76 {
77     if (ctx == NULL)
78     {
79         NS_LOG(ERROR, "Callback is null");
80         return OC_STACK_DELETE_TRANSACTION;
81     }
82
83     if (clientResponse->result == OC_STACK_OK && clientResponse != NULL)
84     {
85         OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
86         ((onRIResponse)ctx)(handle, clientResponse);
87     }
88     else
89     {
90         NS_LOG_V(ERROR, "result is not ok : %d", clientResponse->result);
91     }
92
93     return OC_STACK_KEEP_TRANSACTION;
94 }
95
96 void NSConnectionStateListener(CATransportAdapter_t adapter,
97         const char *remote_address, bool connected)
98 {
99     NS_LOG_V(DEBUG, "adapter : %d", adapter);
100     NS_LOG_V(DEBUG, "remote_address : %s", remote_address);
101     NS_LOG_V(DEBUG, "isConnect : %d", connected);
102
103     (void) adapter;
104     (void) remote_address;
105
106     if (connected)
107     {
108         NS_LOG(DEBUG, "try to discover notification provider.");
109
110         NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
111         if (!task)
112         {
113             NS_LOG(ERROR, "NSTask allocation fail.");
114             return;
115         }
116         NSConsumerPushEvent(task);
117     }
118 }
119
120 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled)
121 {
122     NS_LOG_V(DEBUG, "adapter : %d", adapter);
123     NS_LOG_V(DEBUG, "isEnabled : %d", enabled);
124
125     (void) adapter;
126
127     if (enabled)
128     {
129         NS_LOG(DEBUG, "try to discover notification provider.");
130
131         NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
132         if (!task)
133         {
134             NS_LOG(ERROR, "NSTask allocation fail.");
135             return;
136         }
137         NSConsumerPushEvent(task);
138     }
139 }
140
141 ///////////////////////////////////////////////////////////////////////////////////////////////////
142 // TODO move to other modules
143 ///////////////////////////////////////////////////////////////////////////////////////////////////
144 //NSDiscoveredProvider * NSCreateDiscoveredProviderInfo(OCClientResponse *);
145 //
146 //OCStackResult NSSubscribeNotification(NSDiscoveredProvider * handle);
147 //
148 //NSObject * NSBuildOICNotification(OCClientResponse * clientResponse);
149 //
150 //
151 //OCStackApplicationResult NSNotificationListener(
152 //        void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
153 //{
154 //    if (!g_isStartedConsumer)
155 //    {
156 //        return OC_STACK_DELETE_TRANSACTION;
157 //    }
158 //
159 //    (void) ctx;
160 //    (void) handle;
161 //    if (!clientResponse)
162 //    {
163 //        return OC_STACK_KEEP_TRANSACTION;
164 //    }
165 //
166 //    if (clientResponse->result != OC_STACK_OK)
167 //    {
168 //        // TODO error handle.
169 //        return OC_STACK_KEEP_TRANSACTION;
170 //    }
171 //
172 //    NSObject * notification = NSBuildOICNotification(clientResponse);
173 //    if (!notification)
174 //    {
175 //        // TODO error handle.
176 //        return OC_STACK_KEEP_TRANSACTION;
177 //    }
178 //
179 //    g_NotificationPostedCB(notification);
180 //
181 //    return OC_STACK_KEEP_TRANSACTION;
182 //}
183 //
184 //OCStackResult NSSubscribeNotification(NSDiscoveredProvider * handle)
185 //{
186 //    // TODO add provider list.
187 //
188 //    OCDevAddr * addr = ((NSDiscoveredProvider *)handle)->addr;
189 //
190 //    return NSRequestToResourceIntrospection(
191 //            ((NSDiscoveredProvider *)handle)->observeHandle,
192 //            OC_REST_OBSERVE, addr, NS_OBSERVE_QUERY,
193 //            NSNotificationListener, (void *)DEFAULT_CONTEXT_VALUE, NS_QOS);
194 //}
195 //
196 //NSDiscoveredProvider * NSCreateDiscoveredProviderInfo(OCClientResponse * client)
197 //{
198 //    NSDiscoveredProvider * discoveredProvider
199 //        = (NSDiscoveredProvider * )OICMalloc(sizeof(NSDiscoveredProvider));
200 //
201 //    if (!discoveredProvider)
202 //    {
203 //        return NULL;
204 //    }
205 //
206 //    discoveredProvider->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
207 //    memcpy(discoveredProvider->addr, client->addr,sizeof(OCDevAddr));
208 //
209 //    return discoveredProvider;
210 //}