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