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