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 // TODO find request and cancel
68 NS_LOG(DEBUG, "stopped presence or resource is deleted.");
69 //OCCancel(handle, NS_QOS, NULL, 0);
72 else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
74 NS_LOG(DEBUG, "started presence or resource is created.");
75 NSInvokeRequest(NULL, OC_REST_DISCOVER, clientResponse->addr,
76 NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL,
77 clientResponse->addr->adapter);
80 return OC_STACK_KEEP_TRANSACTION;
83 OCStackApplicationResult NSProviderDiscoverListener(
84 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
88 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
89 NS_VERIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
90 NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
92 NS_LOG_V(DEBUG, "Discover income : %s:%d",
93 clientResponse->devAddr.addr, clientResponse->devAddr.port);
94 NS_LOG_V(DEBUG, "Discover result : %d",
95 clientResponse->result);
96 NS_LOG_V(DEBUG, "Discover sequenceNum : %d",
97 clientResponse->sequenceNumber);
98 NS_LOG_V(DEBUG, "Discover Transport Type : %d",
99 clientResponse->devAddr.adapter);
101 if (!NSIsStartedConsumer())
103 return OC_STACK_DELETE_TRANSACTION;
106 OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
109 if (strstr(resource->uri, NS_RESOURCE_URI))
111 OCConnectivityType type = CT_DEFAULT;
112 if (clientResponse->addr->adapter == OC_ADAPTER_TCP)
114 type = CT_ADAPTER_TCP;
117 NSInvokeRequest(NULL, OC_REST_GET, clientResponse->addr,
118 resource->uri, NULL, NSIntrospectProvider, ctx,
121 resource = resource->next;
124 return OC_STACK_KEEP_TRANSACTION;
127 void NSRemoveProviderObj(NSProvider_internal * provider)
129 NSOICFree(provider->messageUri);
130 NSOICFree(provider->syncUri);
132 NSRemoveConnections(provider->connection);
136 OCStackApplicationResult NSIntrospectProvider(
137 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
141 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
142 NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
144 NS_LOG_V(DEBUG, "GET response income : %s:%d",
145 clientResponse->devAddr.addr, clientResponse->devAddr.port);
146 NS_LOG_V(DEBUG, "GET response result : %d",
147 clientResponse->result);
148 NS_LOG_V(DEBUG, "GET response sequenceNum : %d",
149 clientResponse->sequenceNumber);
150 NS_LOG_V(DEBUG, "GET response resource uri : %s",
151 clientResponse->resourceUri);
152 NS_LOG_V(DEBUG, "GET response Transport Type : %d",
153 clientResponse->devAddr.adapter);
155 if (!NSIsStartedConsumer())
157 return OC_STACK_DELETE_TRANSACTION;
160 NSProvider_internal * newProvider = NSGetProvider(clientResponse);
161 NS_VERIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
162 if (ctx && *((NSConsumerDiscoverType *)ctx) == NS_DISCOVER_CLOUD )
164 newProvider->connection->isCloudConnection = true;
168 NS_LOG(DEBUG, "build NSTask");
169 NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DISCOVERED, (void *) newProvider);
170 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveProviderObj(newProvider));
172 NSConsumerPushEvent(task);
174 return OC_STACK_KEEP_TRANSACTION;
177 void NSGetProviderPostClean(
178 char * pId, char * mUri, char * sUri, 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 int64_t accepter = 0;
204 NSProviderConnectionInfo * connection = NULL;
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);
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);
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, connection));
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, connection));
224 NS_LOG(DEBUG, "get provider connection information");
225 NS_VERIFY_NOT_NULL(clientResponse->addr, NULL);
226 connection = NSCreateProviderConnections(clientResponse->addr);
227 NS_VERIFY_NOT_NULL(connection, NULL);
229 NSProvider_internal * newProvider
230 = (NSProvider_internal *)OICMalloc(sizeof(NSProvider_internal));
231 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newProvider, NULL,
232 NSGetProviderPostClean(providerId, messageUri, syncUri, connection));
234 OICStrcpy(newProvider->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
235 NSOICFree(providerId);
236 newProvider->messageUri = messageUri;
237 newProvider->syncUri = syncUri;
238 newProvider->accessPolicy = (NSAccessPolicy)accepter;
239 newProvider->connection = connection;
244 OCDevAddr * NSChangeAddress(const char * address)
246 NS_VERIFY_NOT_NULL(address, NULL);
247 OCDevAddr * retAddr = NULL;
250 while(address[index] != '\0')
252 if (address[index] == ':')
259 if (address[index] == '\0')
265 uint16_t port = address[tmp++];
267 while(address[tmp] != '\0')
270 port += address[tmp++] - '0';
273 retAddr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
274 NS_VERIFY_NOT_NULL(retAddr, NULL);
276 retAddr->adapter = OC_ADAPTER_TCP;
277 OICStrcpy(retAddr->addr, index - 1, address);
278 retAddr->addr[index] = '\0';
279 retAddr->port = port;
284 void NSConsumerHandleRequestDiscover(OCDevAddr * address, NSConsumerDiscoverType rType)
286 OCConnectivityType type = CT_DEFAULT;
287 NSConsumerDiscoverType * callbackData = NULL;
291 if (address->adapter == OC_ADAPTER_IP)
293 type = CT_ADAPTER_IP;
294 NS_LOG(DEBUG, "Request discover [UDP]");
296 else if (address->adapter == OC_ADAPTER_TCP)
298 type = CT_ADAPTER_TCP;
299 NS_LOG(DEBUG, "Request discover and subscribe presence [TCP]");
300 NS_LOG(DEBUG, "Subscribe presence [TCP]");
301 NSInvokeRequest(NULL, OC_REST_PRESENCE, address, NS_PRESENCE_SUBSCRIBE_QUERY_TCP,
302 NULL, NSConsumerPresenceListener, NULL, type);
304 if (rType == NS_DISCOVER_CLOUD)
306 callbackData = (NSConsumerDiscoverType *)OICMalloc(sizeof(NSConsumerDiscoverType));
307 *callbackData = NS_DISCOVER_CLOUD;
312 NS_LOG_V(DEBUG, "Request discover But Adapter is not IP : %d", address->adapter);
317 NS_LOG(DEBUG, "Request Multicast discover [UDP]");
320 NSInvokeRequest(NULL, OC_REST_DISCOVER, address, NS_DISCOVER_QUERY,
321 NULL, NSProviderDiscoverListener, (void *)callbackData, type);
324 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
326 NS_VERIFY_NOT_NULL_V(task);
328 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
329 if (task->taskType == TASK_CONSUMER_REQ_DISCOVER)
331 char * address = (char *) task->taskData;
332 NSConsumerDiscoverType dType = NS_DISCOVER_DEFAULT;
334 OCDevAddr * addr = NULL;
337 addr = NSChangeAddress(address);
338 dType = NS_DISCOVER_CLOUD;
341 NSConsumerHandleRequestDiscover(addr, dType);
342 NSOICFree(task->taskData);
345 else if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_EVENT_CONNECTED_TCP)
347 NSConsumerHandleRequestDiscover((OCDevAddr *) task->taskData, NS_DISCOVER_DEFAULT);
348 NSOICFree(task->taskData);
352 NS_LOG(ERROR, "Unknown type message");