[CONPRO-1337] Disabled Presence Feature
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerDiscovery.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 "NSConsumerDiscovery.h"
22
23 #include <string.h>
24 #include "NSCommon.h"
25 #include "NSUtil.h"
26 #include "NSConsumerCommon.h"
27 #include "NSConstants.h"
28 #include "ocpayload.h"
29 #include "oic_malloc.h"
30 #include "oic_string.h"
31
32 #define NS_DISCOVER_QUERY "/oic/res?rt=x.org.iotivity.notification"
33 #define NS_PRESENCE_SUBSCRIBE_QUERY_TCP "/oic/ad?rt=x.org.iotivity.notification"
34
35 #ifdef WITH_PRESENCE
36 OCStackApplicationResult NSConsumerPresenceListener(
37         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
38 {
39     (void) ctx;
40     (void) handle;
41
42     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
43     NS_VERIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
44     NS_VERIFY_STACK_SUCCESS(
45             NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
46
47     NS_LOG_V(INFO_PRIVATE, "Presence income : %s:%d",
48             clientResponse->devAddr.addr, clientResponse->devAddr.port);
49     NS_LOG_V(DEBUG, "Presence result : %d",
50             clientResponse->result);
51     NS_LOG_V(DEBUG, "Presence sequenceNum : %d",
52             clientResponse->sequenceNumber);
53     NS_LOG_V(DEBUG, "Presence Transport Type : %d",
54                 clientResponse->devAddr.adapter);
55
56     if (!NSIsStartedConsumer())
57     {
58         return OC_STACK_DELETE_TRANSACTION;
59     }
60
61     OCPresencePayload * payload = (OCPresencePayload *)clientResponse->payload;
62     if (payload->trigger == OC_PRESENCE_TRIGGER_DELETE ||
63             clientResponse->result == OC_STACK_PRESENCE_STOPPED)
64     {
65         NS_LOG(DEBUG, "stopped presence or resource is deleted.");
66         NS_LOG(DEBUG, "build NSTask");
67         OCDevAddr * addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
68         NS_VERIFY_NOT_NULL(addr, OC_STACK_KEEP_TRANSACTION);
69         memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
70
71         NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DELETED, addr);
72         NS_VERIFY_NOT_NULL(task, OC_STACK_KEEP_TRANSACTION);
73
74         NSConsumerPushEvent(task);
75     }
76
77     else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
78     {
79         NS_LOG(DEBUG, "started presence or resource is created.");
80         NSInvokeRequest(NULL, OC_REST_DISCOVER, clientResponse->addr,
81             NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL, NULL,
82             clientResponse->addr->adapter);
83     }
84
85     return OC_STACK_KEEP_TRANSACTION;
86 }
87 #endif
88
89 OCStackApplicationResult NSProviderDiscoverListener(
90         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
91 {
92     (void) handle;
93
94     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
95     NS_VERIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
96     NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
97
98     NS_LOG_V(INFO_PRIVATE, "Discover income : %s:%d",
99             clientResponse->devAddr.addr, clientResponse->devAddr.port);
100     NS_LOG_V(DEBUG, "Discover result : %d",
101             clientResponse->result);
102     NS_LOG_V(DEBUG, "Discover sequenceNum : %d",
103             clientResponse->sequenceNumber);
104     NS_LOG_V(DEBUG, "Discover Transport Type : %d",
105                     clientResponse->devAddr.adapter);
106
107     if (!NSIsStartedConsumer())
108     {
109         return OC_STACK_DELETE_TRANSACTION;
110     }
111
112     OCDiscoveryPayload * discoveryPayload = (OCDiscoveryPayload *)clientResponse->payload;
113
114     while (discoveryPayload)
115     {
116         OCResourcePayload * resource = discoveryPayload->resources;
117         while (resource)
118         {
119             NS_LOG_V(DEBUG, "Discovered resource uri : %s", resource->uri);
120             NS_VERIFY_NOT_NULL(resource->uri, OC_STACK_KEEP_TRANSACTION);
121             if (strstr(resource->uri, NS_RESOURCE_URI))
122             {
123                 NS_LOG_V(DEBUG, "Request GET to provider : %s", resource->uri);
124                 OCConnectivityType type = CT_DEFAULT;
125                 if (clientResponse->addr->adapter == OC_ADAPTER_TCP)
126                 {
127                     type = CT_ADAPTER_TCP;
128                 }
129
130                 OCDevAddr * addr = clientResponse->addr;
131                 if (resource->secure)
132                 {
133                     addr->port = resource->port;
134                     addr->flags |= OC_FLAG_SECURE;
135                 }
136
137                 NSInvokeRequest(NULL, OC_REST_GET, addr,
138                         resource->uri, NULL, NSIntrospectProvider, ctx,
139                         NULL, type);
140             }
141             resource = resource->next;
142         }
143         discoveryPayload = discoveryPayload->next;
144     }
145
146     return OC_STACK_KEEP_TRANSACTION;
147 }
148
149 OCStackApplicationResult NSIntrospectProvider(
150         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
151 {
152     (void) handle;
153
154     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
155     NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
156
157     NS_LOG_V(INFO_PRIVATE, "GET response income : %s:%d",
158             clientResponse->devAddr.addr, clientResponse->devAddr.port);
159     NS_LOG_V(DEBUG, "GET response result : %d",
160             clientResponse->result);
161     NS_LOG_V(DEBUG, "GET response sequenceNum : %d",
162             clientResponse->sequenceNumber);
163     NS_LOG_V(DEBUG, "GET response resource uri : %s",
164             clientResponse->resourceUri);
165     NS_LOG_V(DEBUG, "GET response Transport Type : %d",
166                     clientResponse->devAddr.adapter);
167
168     if (!NSIsStartedConsumer())
169     {
170         return OC_STACK_DELETE_TRANSACTION;
171     }
172
173     NSProvider_internal * newProvider = NSGetProvider(clientResponse);
174     NS_VERIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
175     if (ctx && ctx == (void *)NS_DISCOVER_CLOUD )
176     {
177         newProvider->connection->isCloudConnection = true;
178     }
179
180     NS_LOG(DEBUG, "build NSTask");
181     NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DISCOVERED, (void *) newProvider);
182     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveProvider_internal(newProvider));
183
184     NSConsumerPushEvent(task);
185
186     return OC_STACK_KEEP_TRANSACTION;
187 }
188
189 void NSConsumerHandleRequestDiscover(OCDevAddr * address, NSConsumerDiscoverType rType)
190 {
191     OCConnectivityType type = CT_ADAPTER_IP;
192     NSConsumerDiscoverType * callbackData = NULL;
193
194     if (address)
195     {
196         if (address->adapter == OC_ADAPTER_IP)
197         {
198             NS_LOG(DEBUG, "Request discover [UDP]");
199         }
200         else if (address->adapter == OC_ADAPTER_TCP)
201         {
202             type = CT_ADAPTER_TCP;
203             NS_LOG(DEBUG, "Request discover [TCP]");
204 #ifdef WITH_PRESENCE
205             NS_LOG(DEBUG, "Subscribe presence [TCP]");
206             NSInvokeRequest(NULL, OC_REST_PRESENCE, address, NS_PRESENCE_SUBSCRIBE_QUERY_TCP,
207                     NULL, NSConsumerPresenceListener, NULL, NULL, type);
208 #endif
209             if (rType == NS_DISCOVER_CLOUD)
210             {
211                 callbackData = (void *) NS_DISCOVER_CLOUD;
212             }
213         }
214         else
215         {
216             NS_LOG_V(INFO_PRIVATE, "Request discover But Adapter is not IP : %d", address->adapter);
217         }
218     }
219     else
220     {
221         NS_LOG(DEBUG, "Request Multicast discover [UDP]");
222     }
223
224     NSInvokeRequest(NULL, OC_REST_DISCOVER, address, NS_DISCOVER_QUERY,
225             NULL, NSProviderDiscoverListener, (void *)callbackData, NULL, type);
226 }
227
228 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
229 {
230     NS_VERIFY_NOT_NULL_V(task);
231
232     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
233     if (task->taskType == TASK_CONSUMER_REQ_DISCOVER)
234     {
235         char * address = (char *) task->taskData;
236         NSConsumerDiscoverType dType = NS_DISCOVER_DEFAULT;
237
238         OCDevAddr * addr = NULL;
239         if (address)
240         {
241             addr = NSChangeAddress(address);
242             dType = NS_DISCOVER_CLOUD;
243         }
244
245         NSConsumerHandleRequestDiscover(addr, dType);
246         NSOICFree(task->taskData);
247         NSOICFree(addr);
248     }
249     else if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_EVENT_CONNECTED_TCP)
250     {
251         NSConsumerHandleRequestDiscover((OCDevAddr *) task->taskData, NS_DISCOVER_DEFAULT);
252         NSOICFree(task->taskData);
253     }
254     else
255     {
256         NS_LOG(ERROR, "Unknown type message");
257     }
258
259     NSOICFree(task);
260 }