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"
26 #include "NSConsumerCommon.h"
27 #include "NSConstants.h"
28 #include "ocpayload.h"
29 #include "oic_malloc.h"
30 #include "oic_string.h"
32 #define NS_DISCOVER_QUERY "/oic/res?rt=x.org.iotivity.notification"
33 #define NS_PRESENCE_SUBSCRIBE_QUERY_TCP "/oic/ad?rt=x.org.iotivity.notification"
36 OCStackApplicationResult NSConsumerPresenceListener(
37 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
42 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
43 NS_VERIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
44 NS_VERIFY_STACK_SUCCESS(
45 NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
47 NS_LOG_V(INFO_PRIVATE, "Presence income : %s:%d",
48 clientResponse->devAddr.addr, clientResponse->devAddr.port);
49 NS_LOG_V(DEBUG, "Presence result : %d",
50 clientResponse->result);
51 NS_LOG_V(DEBUG, "Presence sequenceNum : %d",
52 clientResponse->sequenceNumber);
53 NS_LOG_V(DEBUG, "Presence Transport Type : %d",
54 clientResponse->devAddr.adapter);
56 if (!NSIsStartedConsumer())
58 return OC_STACK_DELETE_TRANSACTION;
61 OCPresencePayload * payload = (OCPresencePayload *)clientResponse->payload;
62 if (payload->trigger == OC_PRESENCE_TRIGGER_DELETE ||
63 clientResponse->result == OC_STACK_PRESENCE_STOPPED)
65 NS_LOG(DEBUG, "stopped presence or resource is deleted.");
66 NS_LOG(DEBUG, "build NSTask");
67 OCDevAddr * addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
68 NS_VERIFY_NOT_NULL(addr, OC_STACK_KEEP_TRANSACTION);
69 memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
71 NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DELETED, addr);
72 NS_VERIFY_NOT_NULL(task, OC_STACK_KEEP_TRANSACTION);
74 NSConsumerPushEvent(task);
77 else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
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, NULL,
82 clientResponse->addr->adapter);
85 return OC_STACK_KEEP_TRANSACTION;
89 OCStackApplicationResult NSProviderDiscoverListener(
90 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
94 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
95 NS_VERIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
96 NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
98 NS_LOG_V(INFO_PRIVATE, "Discover income : %s:%d",
99 clientResponse->devAddr.addr, clientResponse->devAddr.port);
100 NS_LOG_V(DEBUG, "Discover result : %d",
101 clientResponse->result);
102 NS_LOG_V(DEBUG, "Discover sequenceNum : %d",
103 clientResponse->sequenceNumber);
104 NS_LOG_V(DEBUG, "Discover Transport Type : %d",
105 clientResponse->devAddr.adapter);
107 if (!NSIsStartedConsumer())
109 return OC_STACK_DELETE_TRANSACTION;
112 OCDiscoveryPayload * discoveryPayload = (OCDiscoveryPayload *)clientResponse->payload;
114 while (discoveryPayload)
116 OCResourcePayload * resource = discoveryPayload->resources;
119 NS_LOG_V(DEBUG, "Discovered resource uri : %s", resource->uri);
120 NS_VERIFY_NOT_NULL(resource->uri, OC_STACK_KEEP_TRANSACTION);
121 if (strstr(resource->uri, NS_RESOURCE_URI))
123 NS_LOG_V(DEBUG, "Request GET to provider : %s", resource->uri);
124 OCConnectivityType type = CT_DEFAULT;
125 if (clientResponse->addr->adapter == OC_ADAPTER_TCP)
127 type = CT_ADAPTER_TCP;
130 OCDevAddr * addr = clientResponse->addr;
131 if (resource->secure)
133 addr->port = resource->port;
134 addr->flags |= OC_FLAG_SECURE;
137 NSInvokeRequest(NULL, OC_REST_GET, addr,
138 resource->uri, NULL, NSIntrospectProvider, ctx,
141 resource = resource->next;
143 discoveryPayload = discoveryPayload->next;
146 return OC_STACK_KEEP_TRANSACTION;
149 OCStackApplicationResult NSIntrospectProvider(
150 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
154 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
155 NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
157 NS_LOG_V(INFO_PRIVATE, "GET response income : %s:%d",
158 clientResponse->devAddr.addr, clientResponse->devAddr.port);
159 NS_LOG_V(DEBUG, "GET response result : %d",
160 clientResponse->result);
161 NS_LOG_V(DEBUG, "GET response sequenceNum : %d",
162 clientResponse->sequenceNumber);
163 NS_LOG_V(DEBUG, "GET response resource uri : %s",
164 clientResponse->resourceUri);
165 NS_LOG_V(DEBUG, "GET response Transport Type : %d",
166 clientResponse->devAddr.adapter);
168 if (!NSIsStartedConsumer())
170 return OC_STACK_DELETE_TRANSACTION;
173 NSProvider_internal * newProvider = NSGetProvider(clientResponse);
174 NS_VERIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
175 if (ctx && ctx == (void *)NS_DISCOVER_CLOUD )
177 newProvider->connection->isCloudConnection = true;
180 NS_LOG(DEBUG, "build NSTask");
181 NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DISCOVERED, (void *) newProvider);
182 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveProvider_internal(newProvider));
184 NSConsumerPushEvent(task);
186 return OC_STACK_KEEP_TRANSACTION;
189 void NSConsumerHandleRequestDiscover(OCDevAddr * address, NSConsumerDiscoverType rType)
191 OCConnectivityType type = CT_ADAPTER_IP;
192 NSConsumerDiscoverType * callbackData = NULL;
196 if (address->adapter == OC_ADAPTER_IP)
198 NS_LOG(DEBUG, "Request discover [UDP]");
200 else if (address->adapter == OC_ADAPTER_TCP)
202 type = CT_ADAPTER_TCP;
203 NS_LOG(DEBUG, "Request discover [TCP]");
205 NS_LOG(DEBUG, "Subscribe presence [TCP]");
206 NSInvokeRequest(NULL, OC_REST_PRESENCE, address, NS_PRESENCE_SUBSCRIBE_QUERY_TCP,
207 NULL, NSConsumerPresenceListener, NULL, NULL, type);
209 if (rType == NS_DISCOVER_CLOUD)
211 callbackData = (void *) NS_DISCOVER_CLOUD;
216 NS_LOG_V(INFO_PRIVATE, "Request discover But Adapter is not IP : %d", address->adapter);
221 NS_LOG(DEBUG, "Request Multicast discover [UDP]");
224 NSInvokeRequest(NULL, OC_REST_DISCOVER, address, NS_DISCOVER_QUERY,
225 NULL, NSProviderDiscoverListener, (void *)callbackData, NULL, type);
228 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
230 NS_VERIFY_NOT_NULL_V(task);
232 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
233 if (task->taskType == TASK_CONSUMER_REQ_DISCOVER)
235 char * address = (char *) task->taskData;
236 NSConsumerDiscoverType dType = NS_DISCOVER_DEFAULT;
238 OCDevAddr * addr = NULL;
241 addr = NSChangeAddress(address);
242 dType = NS_DISCOVER_CLOUD;
245 NSConsumerHandleRequestDiscover(addr, dType);
246 NSOICFree(task->taskData);
249 else if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_EVENT_CONNECTED_TCP)
251 NSConsumerHandleRequestDiscover((OCDevAddr *) task->taskData, NS_DISCOVER_DEFAULT);
252 NSOICFree(task->taskData);
256 NS_LOG(ERROR, "Unknown type message");