Fix bug for discover logic on consumer service.
[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 "NSConsumerCommon.h"
26 #include "NSConstants.h"
27 #include "ocpayload.h"
28 #include "oic_malloc.h"
29 #include "oic_string.h"
30
31 #define NS_DISCOVER_QUERY "/oic/res?rt=oic.r.notification"
32 #define NS_PRESENCE_SUBSCRIBE_QUERY_TCP "/oic/ad?rt=oic.r.notification"
33 #define NS_GET_INFORMATION_QUERY "/notification?if=oic.if.notification"
34
35 NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse);
36
37 OCDevAddr * NSChangeAddress(const char * address);
38
39 OCStackApplicationResult NSConsumerPresenceListener(
40         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
41 {
42     (void) ctx;
43     (void) handle;
44
45     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
46     NS_VERIFY_STACK_SUCCESS(
47             NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
48
49     NS_LOG_V(DEBUG, "Presence income : %s:%d",
50             clientResponse->devAddr.addr, clientResponse->devAddr.port);
51     NS_LOG_V(DEBUG, "Presence result : %d",
52             clientResponse->result);
53     NS_LOG_V(DEBUG, "Presence sequenceNum : %d",
54             clientResponse->sequenceNumber);
55     NS_LOG_V(DEBUG, "Presence Transport Type : %d",
56                 clientResponse->devAddr.adapter);
57
58     if (!NSIsStartedConsumer())
59     {
60         return OC_STACK_DELETE_TRANSACTION;
61     }
62
63     OCPresencePayload * payload = (OCPresencePayload *)clientResponse->payload;
64     if (payload->trigger == OC_PRESENCE_TRIGGER_DELETE ||
65             clientResponse->result == OC_STACK_PRESENCE_STOPPED)
66     {
67         NS_LOG(DEBUG, "stopped presence or resource is deleted.");
68         NS_LOG(DEBUG, "build NSTask");
69         OCDevAddr * addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
70         NS_VERIFY_NOT_NULL(addr, OC_STACK_KEEP_TRANSACTION);
71         memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
72
73         NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DELETED, addr);
74         NS_VERIFY_NOT_NULL(task, OC_STACK_KEEP_TRANSACTION);
75
76         NSConsumerPushEvent(task);
77     }
78
79     else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
80     {
81         NS_LOG(DEBUG, "started presence or resource is created.");
82         NSInvokeRequest(NULL, OC_REST_DISCOVER, clientResponse->addr,
83             NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL,
84             clientResponse->addr->adapter);
85     }
86
87     return OC_STACK_KEEP_TRANSACTION;
88 }
89
90 OCStackApplicationResult NSProviderDiscoverListener(
91         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
92 {
93     (void) handle;
94
95     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
96     NS_VERIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
97     NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
98
99     NS_LOG_V(DEBUG, "Discover income : %s:%d",
100             clientResponse->devAddr.addr, clientResponse->devAddr.port);
101     NS_LOG_V(DEBUG, "Discover result : %d",
102             clientResponse->result);
103     NS_LOG_V(DEBUG, "Discover sequenceNum : %d",
104             clientResponse->sequenceNumber);
105     NS_LOG_V(DEBUG, "Discover Transport Type : %d",
106                     clientResponse->devAddr.adapter);
107
108     if (!NSIsStartedConsumer())
109     {
110         return OC_STACK_DELETE_TRANSACTION;
111     }
112
113     OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
114     while (resource)
115     {
116         NS_VERIFY_NOT_NULL(resource->uri, OC_STACK_KEEP_TRANSACTION);
117         if (strstr(resource->uri, NS_RESOURCE_URI))
118         {
119             OCConnectivityType type = CT_DEFAULT;
120             if (clientResponse->addr->adapter == OC_ADAPTER_TCP)
121             {
122                 type = CT_ADAPTER_TCP;
123             }
124
125             NSInvokeRequest(NULL, OC_REST_GET, clientResponse->addr,
126                     resource->uri, NULL, NSIntrospectProvider, ctx,
127                     type);
128         }
129         resource = resource->next;
130     }
131
132     return OC_STACK_KEEP_TRANSACTION;
133 }
134
135 OCStackApplicationResult NSIntrospectProvider(
136         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
137 {
138     (void) handle;
139
140     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
141     NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
142
143     NS_LOG_V(DEBUG, "GET response income : %s:%d",
144             clientResponse->devAddr.addr, clientResponse->devAddr.port);
145     NS_LOG_V(DEBUG, "GET response result : %d",
146             clientResponse->result);
147     NS_LOG_V(DEBUG, "GET response sequenceNum : %d",
148             clientResponse->sequenceNumber);
149     NS_LOG_V(DEBUG, "GET response resource uri : %s",
150             clientResponse->resourceUri);
151     NS_LOG_V(DEBUG, "GET response Transport Type : %d",
152                     clientResponse->devAddr.adapter);
153
154     if (!NSIsStartedConsumer())
155     {
156         return OC_STACK_DELETE_TRANSACTION;
157     }
158
159     NSProvider_internal * newProvider = NSGetProvider(clientResponse);
160     NS_VERIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
161     if (ctx && *((NSConsumerDiscoverType *)ctx) == NS_DISCOVER_CLOUD )
162     {
163         newProvider->connection->isCloudConnection = true;
164     }
165
166     NS_LOG(DEBUG, "build NSTask");
167     NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DISCOVERED, (void *) newProvider);
168     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveProvider_internal(newProvider));
169
170     NSConsumerPushEvent(task);
171
172     return OC_STACK_KEEP_TRANSACTION;
173 }
174
175 void NSGetProviderPostClean(
176         char * pId, char * mUri, char * sUri, char * tUri, NSProviderConnectionInfo * connection)
177 {
178     NSOICFree(pId);
179     NSOICFree(mUri);
180     NSOICFree(sUri);
181     NSOICFree(tUri);
182     NSRemoveConnections(connection);
183 }
184
185 NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse)
186 {
187     NS_LOG(DEBUG, "create NSProvider");
188     NS_VERIFY_NOT_NULL(clientResponse->payload, NULL);
189
190     OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
191     while (payload)
192     {
193         NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
194         payload = payload->next;
195     }
196
197     payload = (OCRepPayload *)clientResponse->payload;
198
199     char * providerId = NULL;
200     char * messageUri = NULL;
201     char * syncUri = NULL;
202     char * topicUri = NULL;
203     int64_t accepter = 0;
204     NSProviderConnectionInfo * connection = NULL;
205
206     NS_LOG(DEBUG, "get information of accepter");
207     bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_POLICY, & accepter);
208     NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
209
210     NS_LOG(DEBUG, "get provider ID");
211     getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, & providerId);
212     NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
213
214     NS_LOG(DEBUG, "get message URI");
215     getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_MESSAGE, & messageUri);
216     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
217             NSGetProviderPostClean(providerId, messageUri, syncUri, topicUri, connection));
218
219     NS_LOG(DEBUG, "get sync URI");
220     getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SYNC, & syncUri);
221     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
222             NSGetProviderPostClean(providerId, messageUri, syncUri, topicUri, connection));
223
224     NS_LOG(DEBUG, "get topic URI");
225     getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TOPIC, & topicUri);
226
227     NS_LOG(DEBUG, "get provider connection information");
228     NS_VERIFY_NOT_NULL(clientResponse->addr, NULL);
229     connection = NSCreateProviderConnections(clientResponse->addr);
230     NS_VERIFY_NOT_NULL(connection, NULL);
231
232     NSProvider_internal * newProvider
233         = (NSProvider_internal *)OICMalloc(sizeof(NSProvider_internal));
234     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newProvider, NULL,
235           NSGetProviderPostClean(providerId, messageUri, syncUri, topicUri, connection));
236
237     OICStrcpy(newProvider->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
238     NSOICFree(providerId);
239     newProvider->messageUri = messageUri;
240     newProvider->syncUri = syncUri;
241     newProvider->topicUri = NULL;
242     if (topicUri && strlen(topicUri) > 0)
243     {
244         newProvider->topicUri = topicUri;
245     }
246     newProvider->accessPolicy = (NSSelector)accepter;
247     newProvider->connection = connection;
248     newProvider->topicLL = NULL;
249
250     return newProvider;
251 }
252
253 OCDevAddr * NSChangeAddress(const char * address)
254 {
255     NS_VERIFY_NOT_NULL(address, NULL);
256     OCDevAddr * retAddr = NULL;
257
258     int index = 0;
259     while(address[index] != '\0')
260     {
261         if (address[index] == ':')
262         {
263             break;
264         }
265         index++;
266     }
267
268     if (address[index] == '\0')
269     {
270         return NULL;
271     }
272
273     int tmp = index + 1;
274     uint16_t port = address[tmp++];
275
276     while(address[tmp] != '\0')
277     {
278         port *= 10;
279         port += address[tmp++] - '0';
280     }
281
282     retAddr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
283     NS_VERIFY_NOT_NULL(retAddr, NULL);
284
285     retAddr->adapter = OC_ADAPTER_TCP;
286     OICStrcpy(retAddr->addr, index - 1, address);
287     retAddr->addr[index] = '\0';
288     retAddr->port = port;
289
290     return retAddr;
291 }
292
293 void NSConsumerHandleRequestDiscover(OCDevAddr * address, NSConsumerDiscoverType rType)
294 {
295     OCConnectivityType type = CT_ADAPTER_IP;
296     NSConsumerDiscoverType * callbackData = NULL;
297
298     if (address)
299     {
300         if (address->adapter == OC_ADAPTER_IP)
301         {
302             NS_LOG(DEBUG, "Request discover [UDP]");
303         }
304         else if (address->adapter == OC_ADAPTER_TCP)
305         {
306             type = CT_ADAPTER_TCP;
307             NS_LOG(DEBUG, "Request discover and subscribe presence [TCP]");
308             NS_LOG(DEBUG, "Subscribe presence [TCP]");
309             NSInvokeRequest(NULL, OC_REST_PRESENCE, address, NS_PRESENCE_SUBSCRIBE_QUERY_TCP,
310                     NULL, NSConsumerPresenceListener, NULL, type);
311
312             if (rType == NS_DISCOVER_CLOUD)
313             {
314                 *callbackData = NS_DISCOVER_CLOUD;
315             }
316         }
317         else
318         {
319             NS_LOG_V(DEBUG, "Request discover But Adapter is not IP : %d", address->adapter);
320         }
321     }
322     else
323     {
324         NS_LOG(DEBUG, "Request Multicast discover [UDP]");
325     }
326
327     NSInvokeRequest(NULL, OC_REST_DISCOVER, address, NS_DISCOVER_QUERY,
328             NULL, NSProviderDiscoverListener, (void *)callbackData, type);
329 }
330
331 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
332 {
333     NS_VERIFY_NOT_NULL_V(task);
334
335     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
336     if (task->taskType == TASK_CONSUMER_REQ_DISCOVER)
337     {
338         char * address = (char *) task->taskData;
339         NSConsumerDiscoverType dType = NS_DISCOVER_DEFAULT;
340
341         OCDevAddr * addr = NULL;
342         if (address)
343         {
344             addr = NSChangeAddress(address);
345             dType = NS_DISCOVER_CLOUD;
346         }
347
348         NSConsumerHandleRequestDiscover(addr, dType);
349         NSOICFree(task->taskData);
350         NSOICFree(addr);
351     }
352     else if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_EVENT_CONNECTED_TCP)
353     {
354         NSConsumerHandleRequestDiscover((OCDevAddr *) task->taskData, NS_DISCOVER_DEFAULT);
355         NSOICFree(task->taskData);
356     }
357     else
358     {
359         NS_LOG(ERROR, "Unknown type message");
360     }
361
362     NSOICFree(task);
363 }