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"
35 OCStackApplicationResult NSConsumerPresenceListener(
36 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
41 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
42 NS_VERIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
43 NS_VERIFY_STACK_SUCCESS(
44 NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
46 NS_LOG_V(INFO_PRIVATE, "Presence income : %s:%d",
47 clientResponse->devAddr.addr, clientResponse->devAddr.port);
48 NS_LOG_V(DEBUG, "Presence result : %d",
49 clientResponse->result);
50 NS_LOG_V(DEBUG, "Presence sequenceNum : %d",
51 clientResponse->sequenceNumber);
52 NS_LOG_V(DEBUG, "Presence Transport Type : %d",
53 clientResponse->devAddr.adapter);
55 if (!NSIsStartedConsumer())
57 return OC_STACK_DELETE_TRANSACTION;
60 OCPresencePayload * payload = (OCPresencePayload *)clientResponse->payload;
61 if (payload->trigger == OC_PRESENCE_TRIGGER_DELETE ||
62 clientResponse->result == OC_STACK_PRESENCE_STOPPED)
64 NS_LOG(DEBUG, "stopped presence or resource is deleted.");
65 NS_LOG(DEBUG, "build NSTask");
66 OCDevAddr * addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
67 NS_VERIFY_NOT_NULL(addr, OC_STACK_KEEP_TRANSACTION);
68 memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
70 NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DELETED, addr);
71 NS_VERIFY_NOT_NULL(task, OC_STACK_KEEP_TRANSACTION);
73 NSConsumerPushEvent(task);
76 else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
78 NS_LOG(DEBUG, "started presence or resource is created.");
79 NSInvokeRequest(NULL, OC_REST_DISCOVER, clientResponse->addr,
80 NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL, NULL,
81 clientResponse->addr->adapter);
84 return OC_STACK_KEEP_TRANSACTION;
87 OCStackApplicationResult NSProviderDiscoverListener(
88 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
92 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
93 NS_VERIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
94 NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
96 NS_LOG_V(INFO_PRIVATE, "Discover income : %s:%d",
97 clientResponse->devAddr.addr, clientResponse->devAddr.port);
98 NS_LOG_V(DEBUG, "Discover result : %d",
99 clientResponse->result);
100 NS_LOG_V(DEBUG, "Discover sequenceNum : %d",
101 clientResponse->sequenceNumber);
102 NS_LOG_V(DEBUG, "Discover Transport Type : %d",
103 clientResponse->devAddr.adapter);
105 if (!NSIsStartedConsumer())
107 return OC_STACK_DELETE_TRANSACTION;
110 OCDiscoveryPayload * discoveryPayload = (OCDiscoveryPayload *)clientResponse->payload;
112 while (discoveryPayload)
114 OCResourcePayload * resource = discoveryPayload->resources;
117 NS_LOG_V(DEBUG, "Discovered resource uri : %s", resource->uri);
118 NS_VERIFY_NOT_NULL(resource->uri, OC_STACK_KEEP_TRANSACTION);
119 if (strstr(resource->uri, NS_RESOURCE_URI))
121 NS_LOG_V(DEBUG, "Request GET to provider : %s", resource->uri);
122 OCConnectivityType type = CT_DEFAULT;
123 if (clientResponse->addr->adapter == OC_ADAPTER_TCP)
125 type = CT_ADAPTER_TCP;
128 OCDevAddr * addr = clientResponse->addr;
129 if (resource->secure)
131 addr->port = resource->port;
132 addr->flags |= OC_FLAG_SECURE;
135 NSInvokeRequest(NULL, OC_REST_GET, addr,
136 resource->uri, NULL, NSIntrospectProvider, ctx,
139 resource = resource->next;
141 discoveryPayload = discoveryPayload->next;
144 return OC_STACK_KEEP_TRANSACTION;
147 OCStackApplicationResult NSIntrospectProvider(
148 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
152 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
153 NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION);
155 NS_LOG_V(INFO_PRIVATE, "GET response income : %s:%d",
156 clientResponse->devAddr.addr, clientResponse->devAddr.port);
157 NS_LOG_V(DEBUG, "GET response result : %d",
158 clientResponse->result);
159 NS_LOG_V(DEBUG, "GET response sequenceNum : %d",
160 clientResponse->sequenceNumber);
161 NS_LOG_V(DEBUG, "GET response resource uri : %s",
162 clientResponse->resourceUri);
163 NS_LOG_V(DEBUG, "GET response Transport Type : %d",
164 clientResponse->devAddr.adapter);
166 if (!NSIsStartedConsumer())
168 return OC_STACK_DELETE_TRANSACTION;
171 NSProvider_internal * newProvider = NSGetProvider(clientResponse);
172 NS_VERIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
173 if (ctx && ctx == (void *)NS_DISCOVER_CLOUD )
175 newProvider->connection->isCloudConnection = true;
178 NS_LOG(DEBUG, "build NSTask");
179 NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DISCOVERED, (void *) newProvider);
180 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveProvider_internal(newProvider));
182 NSConsumerPushEvent(task);
184 return OC_STACK_KEEP_TRANSACTION;
187 void NSConsumerHandleRequestDiscover(OCDevAddr * address, NSConsumerDiscoverType rType)
189 OCConnectivityType type = CT_ADAPTER_IP;
190 NSConsumerDiscoverType * callbackData = NULL;
194 if (address->adapter == OC_ADAPTER_IP)
196 NS_LOG(DEBUG, "Request discover [UDP]");
198 else if (address->adapter == OC_ADAPTER_TCP)
200 type = CT_ADAPTER_TCP;
201 NS_LOG(DEBUG, "Request discover and subscribe presence [TCP]");
202 NS_LOG(DEBUG, "Subscribe presence [TCP]");
203 NSInvokeRequest(NULL, OC_REST_PRESENCE, address, NS_PRESENCE_SUBSCRIBE_QUERY_TCP,
204 NULL, NSConsumerPresenceListener, NULL, NULL, type);
206 if (rType == NS_DISCOVER_CLOUD)
208 callbackData = (void *) NS_DISCOVER_CLOUD;
213 NS_LOG_V(INFO_PRIVATE, "Request discover But Adapter is not IP : %d", address->adapter);
218 NS_LOG(DEBUG, "Request Multicast discover [UDP]");
221 NSInvokeRequest(NULL, OC_REST_DISCOVER, address, NS_DISCOVER_QUERY,
222 NULL, NSProviderDiscoverListener, (void *)callbackData, NULL, type);
225 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
227 NS_VERIFY_NOT_NULL_V(task);
229 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
230 if (task->taskType == TASK_CONSUMER_REQ_DISCOVER)
232 char * address = (char *) task->taskData;
233 NSConsumerDiscoverType dType = NS_DISCOVER_DEFAULT;
235 OCDevAddr * addr = NULL;
238 addr = NSChangeAddress(address);
239 dType = NS_DISCOVER_CLOUD;
242 NSConsumerHandleRequestDiscover(addr, dType);
243 NSOICFree(task->taskData);
246 else if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_EVENT_CONNECTED_TCP)
248 NSConsumerHandleRequestDiscover((OCDevAddr *) task->taskData, NS_DISCOVER_DEFAULT);
249 NSOICFree(task->taskData);
253 NS_LOG(ERROR, "Unknown type message");