Modify subscribe presence query.
[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 "/oic/ad?rt=oic.r.notification"
33
34 void NSConnectionStateListener(const CAEndpoint_t * info, bool isConnected);
35
36 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled);
37
38 OCDoHandle * getPresenceHandle()
39 {
40     static OCDoHandle g_PresenceHandle = NULL;
41
42     return & g_PresenceHandle;
43 }
44
45
46 NSResult NSConsumerListenerInit()
47 {
48     // TODO replace with comment lines when enable network monitor of IP Adapter
49     CARegisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener);
50 //    if (CARegisterNetworkMonitorHandler(NSAdapterStateListener, NSConnectionStateListener)
51 //            != CA_STATUS_OK)
52 //    {
53 //        return NS_ERROR;
54 //    }
55
56     NS_LOG(DEBUG, "Request to subscribe presence");
57     OCStackResult stackResult = NSInvokeRequest(getPresenceHandle(), OC_REST_PRESENCE, NULL,
58                         NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener,
59                         NULL, CT_DEFAULT);
60     NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(stackResult), NS_ERROR);
61
62     NS_LOG(DEBUG, "Request to discover provider");
63     stackResult = NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL,
64                       NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener,
65                       NULL, CT_DEFAULT);
66     NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(stackResult), NS_ERROR);
67
68     return NS_OK;
69 }
70
71 void NSConsumerListenerTermiate()
72 {
73     CARegisterNetworkMonitorHandler(NULL, NULL);
74     OCCancel(*getPresenceHandle(), NS_QOS, NULL, 0);
75 }
76
77 void NSConnectionStateListener(const CAEndpoint_t * info, bool connected)
78 {
79     NS_VERIFY_NOT_NULL_V(info);
80
81     NS_LOG_V(DEBUG, "adapter : %d", info->adapter);
82     NS_LOG_V(DEBUG, "remote_address : %s:%d", info->addr, info->port);
83     NS_LOG_V(DEBUG, "isConnect : %d", connected);
84
85     NSTaskType type = TASK_EVENT_CONNECTED;
86     OCDevAddr * addr = NULL;
87     if (connected)
88     {
89         if (info->adapter == CA_ADAPTER_TCP)
90         {
91             type = TASK_EVENT_CONNECTED_TCP;
92             NS_LOG(DEBUG, "try to discover notification provider : TCP.");
93             // TODO convet to OCDevAddr;
94             // addr = .....
95         }
96         else if (info->adapter == CA_ADAPTER_IP)
97         {
98             NS_LOG(DEBUG, "try to discover notification provider.");
99             // TODO convet to OCDevAddr;
100             // addr = .....
101         }
102
103         NSTask * task = NSMakeTask(type, addr);
104         NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(task, NSOICFree(addr));
105
106         NSConsumerPushEvent(task);
107     }
108 }
109
110 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled)
111 {
112     NS_LOG_V(DEBUG, "adapter : %d", adapter);
113     NS_LOG_V(DEBUG, "isEnabled : %d", enabled);
114
115     (void) adapter;
116
117     if (enabled)
118     {
119         NS_LOG(DEBUG, "try to discover notification provider.");
120
121         NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
122         NS_VERIFY_NOT_NULL_V(task);
123
124         NSConsumerPushEvent(task);
125     }
126 }