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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include "NSConstants.h"
25 #include "NSConsumerCommon.h"
26 #include "cautilinterface.h"
28 #include "NSConsumerDiscovery.h"
29 #include "NSConsumerNetworkEventListener.h"
31 #define NS_PRESENCE_SUBSCRIBE_QUERY "coap://224.0.1.187:5683/oic/ad?rt=oic.r.notification"
33 void NSConnectionStateListener(CATransportAdapter_t adapter,
34 const char *remote_address, bool connected);
36 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled);
38 OCDoHandle * getPresenceHandle()
40 static OCDoHandle g_PresenceHandle = NULL;
42 return & g_PresenceHandle;
46 NSResult NSConsumerListenerInit()
48 // TODO replace with comment lines when enable network monitor of IP Adapter
49 CARegisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener);
50 // if (CARegisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener)
56 NS_LOG(DEBUG, "Request to subscribe presence");
57 OCStackResult stackResult = NSInvokeRequest(getPresenceHandle(), OC_REST_PRESENCE, NULL,
58 NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener, NULL);
59 NS_VERTIFY_STACK_OK(stackResult, NS_ERROR);
61 NS_LOG(DEBUG, "Request to discover provider");
62 stackResult = NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL,
63 NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL);
64 NS_VERTIFY_STACK_OK(stackResult, NS_ERROR);
69 void NSConsumerListenerTermiate()
71 CARegisterNetworkMonitorHandler(NULL, NULL);
72 OCCancel(*getPresenceHandle(), NS_QOS, NULL, 0);
75 void NSConnectionStateListener(CATransportAdapter_t adapter,
76 const char *remote_address, bool connected)
78 NS_LOG_V(DEBUG, "adapter : %d", adapter);
79 NS_LOG_V(DEBUG, "remote_address : %s", remote_address);
80 NS_LOG_V(DEBUG, "isConnect : %d", connected);
83 (void) remote_address;
87 NS_LOG(DEBUG, "try to discover notification provider.");
89 NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
90 NS_VERTIFY_NOT_NULL_V(task);
92 NSConsumerPushEvent(task);
96 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled)
98 NS_LOG_V(DEBUG, "adapter : %d", adapter);
99 NS_LOG_V(DEBUG, "isEnabled : %d", enabled);
105 NS_LOG(DEBUG, "try to discover notification provider.");
107 NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
108 NS_VERTIFY_NOT_NULL_V(task);
110 NSConsumerPushEvent(task);