1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "NSConsumerListener.h"
26 #include "NSConstants.h"
27 #include "NSConsumerCommon.h"
28 #include "payload_logging.h"
29 #include "cautilinterface.h"
31 #include "NSConsumerDiscovery.h"
33 #define NS_PRESENCE_SUBSCRIBE_QUERY "coap://224.0.1.187:5683/oic/ad?rt=oic.r.notification"
35 typedef OCStackApplicationResult(*onRIResponse)(OCDoHandle, OCClientResponse *);
37 OCStackApplicationResult NSRIResponseListener(
38 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse);
40 void NSConnectionStateListener(CATransportAdapter_t adapter,
41 const char *remote_address, bool connected);
43 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled);
45 NSResult NSConsumerListenerInit()
47 NSSetResponseListener(NSRIResponseListener);
49 // TODO replace with comment lines when enable network monitor of IP Adapter
50 CARegisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener);
51 // if (CARegisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener)
57 if (OC_STACK_OK != NSRequestToResourceIntrospection(NULL, OC_REST_PRESENCE, NULL,
58 NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener))
60 NS_LOG(ERROR, "Presence request fail");
64 if (OC_STACK_OK != NSRequestToResourceIntrospection(NULL, OC_REST_DISCOVER, NULL,
65 NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener))
67 NS_LOG(ERROR, "Discover request fail");
74 OCStackApplicationResult NSRIResponseListener(
75 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
79 NS_LOG(ERROR, "Callback is null");
80 return OC_STACK_DELETE_TRANSACTION;
83 if (clientResponse->result == OC_STACK_OK && clientResponse != NULL)
85 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
86 ((onRIResponse)ctx)(handle, clientResponse);
90 NS_LOG_V(ERROR, "result is not ok : %d", clientResponse->result);
93 return OC_STACK_KEEP_TRANSACTION;
96 void NSConnectionStateListener(CATransportAdapter_t adapter,
97 const char *remote_address, bool connected)
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);
104 (void) remote_address;
108 NS_LOG(DEBUG, "try to discover notification provider.");
110 NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
113 NS_LOG(ERROR, "NSTask allocation fail.");
116 NSConsumerPushEvent(task);
120 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled)
122 NS_LOG_V(DEBUG, "adapter : %d", adapter);
123 NS_LOG_V(DEBUG, "isEnabled : %d", enabled);
129 NS_LOG(DEBUG, "try to discover notification provider.");
131 NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
134 NS_LOG(ERROR, "NSTask allocation fail.");
137 NSConsumerPushEvent(task);
141 ///////////////////////////////////////////////////////////////////////////////////////////////////
142 // TODO move to other modules
143 ///////////////////////////////////////////////////////////////////////////////////////////////////
144 //NSDiscoveredProvider * NSCreateDiscoveredProviderInfo(OCClientResponse *);
146 //OCStackResult NSSubscribeNotification(NSDiscoveredProvider * handle);
148 //NSObject * NSBuildOICNotification(OCClientResponse * clientResponse);
151 //OCStackApplicationResult NSNotificationListener(
152 // void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
154 // if (!g_isStartedConsumer)
156 // return OC_STACK_DELETE_TRANSACTION;
161 // if (!clientResponse)
163 // return OC_STACK_KEEP_TRANSACTION;
166 // if (clientResponse->result != OC_STACK_OK)
168 // // TODO error handle.
169 // return OC_STACK_KEEP_TRANSACTION;
172 // NSObject * notification = NSBuildOICNotification(clientResponse);
173 // if (!notification)
175 // // TODO error handle.
176 // return OC_STACK_KEEP_TRANSACTION;
179 // g_NotificationPostedCB(notification);
181 // return OC_STACK_KEEP_TRANSACTION;
184 //OCStackResult NSSubscribeNotification(NSDiscoveredProvider * handle)
186 // // TODO add provider list.
188 // OCDevAddr * addr = ((NSDiscoveredProvider *)handle)->addr;
190 // return NSRequestToResourceIntrospection(
191 // ((NSDiscoveredProvider *)handle)->observeHandle,
192 // OC_REST_OBSERVE, addr, NS_OBSERVE_QUERY,
193 // NSNotificationListener, (void *)DEFAULT_CONTEXT_VALUE, NS_QOS);
196 //NSDiscoveredProvider * NSCreateDiscoveredProviderInfo(OCClientResponse * client)
198 // NSDiscoveredProvider * discoveredProvider
199 // = (NSDiscoveredProvider * )OICMalloc(sizeof(NSDiscoveredProvider));
201 // if (!discoveredProvider)
206 // discoveredProvider->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
207 // memcpy(discoveredProvider->addr, client->addr,sizeof(OCDevAddr));
209 // return discoveredProvider;