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 "coap://224.0.1.187:5683/oic/ad?rt=oic.r.notification"
33 #define NS_PRESENCE_SUBSCRIBE_QUERY_TCP "/oic/ad?rt=oic.r.notification"
34 #define NS_GET_INFORMATION_QUERY "/notification?if=oic.if.notification"
36 NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse);
38 OCStackApplicationResult NSConsumerPresenceListener(
39 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
44 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
45 NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
47 NS_LOG_V(DEBUG, "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 // TODO find request and cancel
66 NS_LOG(DEBUG, "stopped presence or resource is deleted.");
67 //OCCancel(handle, NS_QOS, NULL, 0);
70 else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
72 NS_LOG(DEBUG, "started presence or resource is created.");
73 NSInvokeRequest(NULL, OC_REST_DISCOVER, clientResponse->addr,
74 NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL,
75 clientResponse->addr->adapter);
78 return OC_STACK_KEEP_TRANSACTION;
81 OCStackApplicationResult NSProviderDiscoverListener(
82 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
87 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
88 NS_VERIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
89 NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
91 NS_LOG_V(DEBUG, "Discover income : %s:%d",
92 clientResponse->devAddr.addr, clientResponse->devAddr.port);
93 NS_LOG_V(DEBUG, "Discover result : %d",
94 clientResponse->result);
95 NS_LOG_V(DEBUG, "Discover sequenceNum : %d",
96 clientResponse->sequenceNumber);
97 NS_LOG_V(DEBUG, "Discover Transport Type : %d",
98 clientResponse->devAddr.adapter);
100 if (!NSIsStartedConsumer())
102 return OC_STACK_DELETE_TRANSACTION;
105 OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
108 if (strstr(resource->uri, NS_RESOURCE_URI))
110 OCConnectivityType type = CT_DEFAULT;
111 if (clientResponse->addr->adapter == OC_ADAPTER_TCP)
113 type = CT_ADAPTER_TCP;
116 NSInvokeRequest(NULL, OC_REST_GET, clientResponse->addr,
117 resource->uri, NULL, NSIntrospectProvider, NULL,
120 resource = resource->next;
123 return OC_STACK_KEEP_TRANSACTION;
126 void NSRemoveProviderObj(NSProvider_internal * provider)
128 NSOICFree(provider->messageUri);
129 NSOICFree(provider->syncUri);
131 NSRemoveConnections(provider->connection);
135 OCStackApplicationResult NSIntrospectProvider(
136 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
141 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
142 NS_VERIFY_STACK_OK(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);
163 NS_LOG(DEBUG, "build NSTask");
164 NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DISCOVERED, (void *) newProvider);
165 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveProviderObj(newProvider));
167 NSConsumerPushEvent(task);
169 return OC_STACK_KEEP_TRANSACTION;
172 void NSGetProviderPostClean(
173 char * pId, char * mUri, char * sUri, NSProviderConnectionInfo * connection)
178 NSRemoveConnections(connection);
181 NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse)
183 NS_LOG(DEBUG, "create NSProvider");
184 NS_VERIFY_NOT_NULL(clientResponse->payload, NULL);
186 OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
189 NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
190 payload = payload->next;
193 payload = (OCRepPayload *)clientResponse->payload;
195 char * providerId = NULL;
196 char * messageUri = NULL;
197 char * syncUri = NULL;
198 int64_t accepter = 0;
199 NSProviderConnectionInfo * connection = NULL;
201 NS_LOG(DEBUG, "get information of accepter");
202 bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_POLICY, & accepter);
203 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
205 NS_LOG(DEBUG, "get provider ID");
206 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, & providerId);
207 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
209 NS_LOG(DEBUG, "get message URI");
210 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_MESSAGE, & messageUri);
211 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
212 NSGetProviderPostClean(providerId, messageUri, syncUri, connection));
214 NS_LOG(DEBUG, "get sync URI");
215 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SYNC, & syncUri);
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 provider connection information");
220 NS_VERIFY_NOT_NULL(clientResponse->addr, NULL);
221 connection = NSCreateProviderConnections(clientResponse->addr);
222 NS_VERIFY_NOT_NULL(connection, NULL);
224 NSProvider_internal * newProvider
225 = (NSProvider_internal *)OICMalloc(sizeof(NSProvider_internal));
226 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newProvider, NULL,
227 NSGetProviderPostClean(providerId, messageUri, syncUri, connection));
229 OICStrcpy(newProvider->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
230 NSOICFree(providerId);
231 newProvider->messageUri = messageUri;
232 newProvider->syncUri = syncUri;
233 newProvider->accessPolicy = (NSAccessPolicy)accepter;
234 newProvider->connection = connection;
239 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
241 NS_VERIFY_NOT_NULL_V(task);
243 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
244 if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_CONSUMER_REQ_DISCOVER)
246 OCDevAddr * addr = (OCDevAddr *) task->taskData;
247 OCConnectivityType type = CT_DEFAULT;
250 type = addr->adapter;
253 NS_LOG(DEBUG, "Request discover [UDP]");
254 NSInvokeRequest(NULL, OC_REST_DISCOVER, addr, NS_DISCOVER_QUERY,
255 NULL, NSProviderDiscoverListener, NULL, type);
257 else if (task->taskType == TASK_EVENT_CONNECTED_TCP)
259 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(task->taskData, NSOICFree(task));
260 OCDevAddr * addr = (OCDevAddr *) task->taskData;
261 OCConnectivityType type = CT_ADAPTER_TCP;
264 type = addr->adapter;
267 NS_LOG(DEBUG, "Request discover [TCP]");
268 NSInvokeRequest(NULL, OC_REST_DISCOVER, addr, NS_DISCOVER_QUERY,
269 NULL, NSProviderDiscoverListener, NULL, type);
271 NS_LOG(DEBUG, "Subscribe presence [TCP]");
272 NSInvokeRequest(NULL, OC_REST_PRESENCE, addr, NS_PRESENCE_SUBSCRIBE_QUERY_TCP,
273 NULL, NSConsumerPresenceListener, NULL, type);
275 NSOICFree(task->taskData);
279 NS_LOG(ERROR, "Unknown type message");