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"
27 #include "oic_malloc.h"
28 #include "oic_string.h"
30 #include "NSConsumerDiscovery.h"
31 #include "NSConsumerNetworkEventListener.h"
33 #define NS_PRESENCE_SUBSCRIBE_QUERY "/oic/ad?rt=x.org.iotivity.notification"
35 void NSConnectionStateListener(const CAEndpoint_t * info, bool isConnected);
37 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled);
39 OCDoHandle * getPresenceHandle()
41 static OCDoHandle g_PresenceHandle = NULL;
43 return & g_PresenceHandle;
47 NSResult NSConsumerListenerInit()
49 // TODO replace with comment lines when enable network monitor of IP Adapter
50 CARegisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener);
51 // if (CARegisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener)
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, NULL, CT_DEFAULT);
61 NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(stackResult), NS_ERROR);
63 NS_LOG(DEBUG, "Request to discover provider");
64 stackResult = NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL,
65 NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener,
66 NULL, NULL, CT_DEFAULT);
67 NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(stackResult), NS_ERROR);
72 void NSConsumerListenerTermiate()
74 CAUnregisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener);
75 OCCancel(*getPresenceHandle(), NS_QOS, NULL, 0);
78 void NSConnectionStateListener(const CAEndpoint_t * info, bool connected)
80 NS_VERIFY_NOT_NULL_V(info);
82 NS_LOG_V(DEBUG, "adapter : %d", info->adapter);
83 NS_LOG_V(INFO_PRIVATE, "remote_address : %s:%d", info->addr, info->port);
84 NS_LOG_V(DEBUG, "isConnect : %d", connected);
86 NSTaskType type = TASK_EVENT_CONNECTED;
87 OCDevAddr * addr = NULL;
90 if (info->adapter == CA_ADAPTER_TCP)
92 type = TASK_EVENT_CONNECTED_TCP;
93 NS_LOG(DEBUG, "try to discover notification provider : TCP.");
95 else if (info->adapter == CA_ADAPTER_IP)
97 NS_LOG(DEBUG, "try to discover notification provider : IP.");
100 addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
101 NS_VERIFY_NOT_NULL_V(addr);
103 addr->adapter = (OCTransportAdapter) info->adapter;
104 OICStrcpy(addr->addr, MAX_ADDR_STR_SIZE, info->addr);
105 addr->flags = info->flags;
106 addr->ifindex = info->ifindex;
107 addr->port = info->port;
109 NSTask * task = NSMakeTask(type, addr);
110 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(task, NSOICFree(addr));
112 NSConsumerPushEvent(task);
116 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled)
118 NS_LOG_V(DEBUG, "adapter : %d", adapter);
119 NS_LOG_V(DEBUG, "isEnabled : %d", enabled);
125 NS_LOG(DEBUG, "try to discover notification provider.");
127 NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
128 NS_VERIFY_NOT_NULL_V(task);
130 NSConsumerPushEvent(task);