1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "NSConsumerDiscovery.h"
25 #include "NSConsumerCommon.h"
26 #include "NSConstants.h"
27 #include "ocpayload.h"
28 #include "oic_malloc.h"
29 #include "oic_string.h"
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"
35 NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse);
37 OCDevAddr * NSChangeAddress(const char * address);
39 OCStackApplicationResult NSConsumerPresenceListener(
40 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
45 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
46 NS_VERIFY_STACK_SUCCESS(
47 NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
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);
58 if (!NSIsStartedConsumer())
60 return OC_STACK_DELETE_TRANSACTION;
63 OCPresencePayload * payload = (OCPresencePayload *)clientResponse->payload;
64 if (payload->trigger == OC_PRESENCE_TRIGGER_DELETE ||
65 clientResponse->result == OC_STACK_PRESENCE_STOPPED)
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));
73 NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DELETED, addr);
74 NS_VERIFY_NOT_NULL(task, OC_STACK_KEEP_TRANSACTION);
76 NSConsumerPushEvent(task);
79 else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
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);
87 return OC_STACK_KEEP_TRANSACTION;
90 OCStackApplicationResult NSProviderDiscoverListener(
91 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
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);
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);
108 if (!NSIsStartedConsumer())
110 return OC_STACK_DELETE_TRANSACTION;
113 OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
116 NS_VERIFY_NOT_NULL(resource->uri, OC_STACK_KEEP_TRANSACTION);
117 if (strstr(resource->uri, NS_RESOURCE_URI))
119 OCConnectivityType type = CT_DEFAULT;
120 if (clientResponse->addr->adapter == OC_ADAPTER_TCP)
122 type = CT_ADAPTER_TCP;
125 NSInvokeRequest(NULL, OC_REST_GET, clientResponse->addr,
126 resource->uri, NULL, NSIntrospectProvider, ctx,
129 resource = resource->next;
132 return OC_STACK_KEEP_TRANSACTION;
135 OCStackApplicationResult NSIntrospectProvider(
136 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
140 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
141 NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
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);
154 if (!NSIsStartedConsumer())
156 return OC_STACK_DELETE_TRANSACTION;
159 NSProvider_internal * newProvider = NSGetProvider(clientResponse);
160 NS_VERIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
161 if (ctx && *((NSConsumerDiscoverType *)ctx) == NS_DISCOVER_CLOUD )
163 newProvider->connection->isCloudConnection = true;
167 NS_LOG(DEBUG, "build NSTask");
168 NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DISCOVERED, (void *) newProvider);
169 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveProvider_internal(newProvider));
171 NSConsumerPushEvent(task);
173 return OC_STACK_KEEP_TRANSACTION;
176 void NSGetProviderPostClean(
177 char * pId, char * mUri, char * sUri, char * tUri, NSProviderConnectionInfo * connection)
183 NSRemoveConnections(connection);
186 NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse)
188 NS_LOG(DEBUG, "create NSProvider");
189 NS_VERIFY_NOT_NULL(clientResponse->payload, NULL);
191 OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
194 NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
195 payload = payload->next;
198 payload = (OCRepPayload *)clientResponse->payload;
200 char * providerId = NULL;
201 char * messageUri = NULL;
202 char * syncUri = NULL;
203 char * topicUri = NULL;
204 int64_t accepter = 0;
205 NSProviderConnectionInfo * connection = NULL;
207 NS_LOG(DEBUG, "get information of accepter");
208 bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_POLICY, & accepter);
209 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
211 NS_LOG(DEBUG, "get provider ID");
212 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, & providerId);
213 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
215 NS_LOG(DEBUG, "get message URI");
216 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_MESSAGE, & messageUri);
217 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
218 NSGetProviderPostClean(providerId, messageUri, syncUri, topicUri, connection));
220 NS_LOG(DEBUG, "get sync URI");
221 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SYNC, & syncUri);
222 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
223 NSGetProviderPostClean(providerId, messageUri, syncUri, topicUri, connection));
225 NS_LOG(DEBUG, "get topic URI");
226 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TOPIC, & topicUri);
228 NS_LOG(DEBUG, "get provider connection information");
229 NS_VERIFY_NOT_NULL(clientResponse->addr, NULL);
230 connection = NSCreateProviderConnections(clientResponse->addr);
231 NS_VERIFY_NOT_NULL(connection, NULL);
233 NSProvider_internal * newProvider
234 = (NSProvider_internal *)OICMalloc(sizeof(NSProvider_internal));
235 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newProvider, NULL,
236 NSGetProviderPostClean(providerId, messageUri, syncUri, topicUri, connection));
238 OICStrcpy(newProvider->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
239 NSOICFree(providerId);
240 newProvider->messageUri = messageUri;
241 newProvider->syncUri = syncUri;
242 newProvider->topicUri = NULL;
243 if (topicUri && strlen(topicUri) > 0)
245 newProvider->topicUri = topicUri;
247 newProvider->accessPolicy = (NSSelector)accepter;
248 newProvider->connection = connection;
249 newProvider->topicLL = NULL;
254 OCDevAddr * NSChangeAddress(const char * address)
256 NS_VERIFY_NOT_NULL(address, NULL);
257 OCDevAddr * retAddr = NULL;
260 while(address[index] != '\0')
262 if (address[index] == ':')
269 if (address[index] == '\0')
275 uint16_t port = address[tmp++];
277 while(address[tmp] != '\0')
280 port += address[tmp++] - '0';
283 retAddr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
284 NS_VERIFY_NOT_NULL(retAddr, NULL);
286 retAddr->adapter = OC_ADAPTER_TCP;
287 OICStrcpy(retAddr->addr, index - 1, address);
288 retAddr->addr[index] = '\0';
289 retAddr->port = port;
294 void NSConsumerHandleRequestDiscover(OCDevAddr * address, NSConsumerDiscoverType rType)
296 OCConnectivityType type = CT_DEFAULT;
297 NSConsumerDiscoverType * callbackData = NULL;
301 if (address->adapter == OC_ADAPTER_IP)
303 type = CT_ADAPTER_IP;
304 NS_LOG(DEBUG, "Request discover [UDP]");
306 else if (address->adapter == OC_ADAPTER_TCP)
308 type = CT_ADAPTER_TCP;
309 NS_LOG(DEBUG, "Request discover and subscribe presence [TCP]");
310 NS_LOG(DEBUG, "Subscribe presence [TCP]");
311 NSInvokeRequest(NULL, OC_REST_PRESENCE, address, NS_PRESENCE_SUBSCRIBE_QUERY_TCP,
312 NULL, NSConsumerPresenceListener, NULL, type);
314 if (rType == NS_DISCOVER_CLOUD)
316 callbackData = (NSConsumerDiscoverType *)OICMalloc(sizeof(NSConsumerDiscoverType));
317 *callbackData = NS_DISCOVER_CLOUD;
322 NS_LOG_V(DEBUG, "Request discover But Adapter is not IP : %d", address->adapter);
327 NS_LOG(DEBUG, "Request Multicast discover [UDP]");
330 NSInvokeRequest(NULL, OC_REST_DISCOVER, address, NS_DISCOVER_QUERY,
331 NULL, NSProviderDiscoverListener, (void *)callbackData, type);
334 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
336 NS_VERIFY_NOT_NULL_V(task);
338 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
339 if (task->taskType == TASK_CONSUMER_REQ_DISCOVER)
341 char * address = (char *) task->taskData;
342 NSConsumerDiscoverType dType = NS_DISCOVER_DEFAULT;
344 OCDevAddr * addr = NULL;
347 addr = NSChangeAddress(address);
348 dType = NS_DISCOVER_CLOUD;
351 NSConsumerHandleRequestDiscover(addr, dType);
352 NSOICFree(task->taskData);
355 else if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_EVENT_CONNECTED_TCP)
357 NSConsumerHandleRequestDiscover((OCDevAddr *) task->taskData, NS_DISCOVER_DEFAULT);
358 NSOICFree(task->taskData);
362 NS_LOG(ERROR, "Unknown type message");