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_GET_INFORMATION_QUERY "/notification?if=oic.if.notification"
35 NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse);
37 OCStackApplicationResult NSConsumerPresenceListener(
38 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
43 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
44 NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
46 NS_LOG_V(DEBUG, "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);
53 if (!NSIsStartedConsumer())
55 return OC_STACK_DELETE_TRANSACTION;
58 OCPresencePayload * payload = (OCPresencePayload *)clientResponse->payload;
59 if (payload->trigger == OC_PRESENCE_TRIGGER_DELETE ||
60 clientResponse->result == OC_STACK_PRESENCE_STOPPED)
62 // TODO find request and cancel
63 NS_LOG(DEBUG, "stopped presence or resource is deleted.");
64 //OCCancel(handle, NS_QOS, NULL, 0);
67 else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
69 NS_LOG(DEBUG, "started presence or resource is created.");
70 NSInvokeRequest(NULL, OC_REST_DISCOVER, clientResponse->addr,
71 NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL);
74 return OC_STACK_KEEP_TRANSACTION;
77 OCStackApplicationResult NSProviderDiscoverListener(
78 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
83 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
84 NS_VERIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
85 NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
87 NS_LOG_V(DEBUG, "Discover income : %s:%d",
88 clientResponse->devAddr.addr, clientResponse->devAddr.port);
89 NS_LOG_V(DEBUG, "Discover result : %d",
90 clientResponse->result);
91 NS_LOG_V(DEBUG, "Discover sequenceNum : %d",
92 clientResponse->sequenceNumber);
94 if (!NSIsStartedConsumer())
96 return OC_STACK_DELETE_TRANSACTION;
99 OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
102 if (!strcmp(resource->uri, NS_RESOURCE_URI))
104 NSInvokeRequest(NULL, OC_REST_GET, clientResponse->addr,
105 NS_RESOURCE_URI, NULL, NSIntrospectProvider, NULL);
107 resource = resource->next;
110 return OC_STACK_KEEP_TRANSACTION;
113 void NSRemoveProviderObj(NSProvider_internal * provider)
115 NSOICFree(provider->messageUri);
116 NSOICFree(provider->syncUri);
118 provider->messageHandle = NULL;
119 provider->syncHandle = NULL;
120 NSOICFree(provider->_addr);
125 OCStackApplicationResult NSIntrospectProvider(
126 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
131 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
132 NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
134 NS_LOG_V(DEBUG, "GET response income : %s:%d",
135 clientResponse->devAddr.addr, clientResponse->devAddr.port);
136 NS_LOG_V(DEBUG, "GET response result : %d",
137 clientResponse->result);
138 NS_LOG_V(DEBUG, "GET response sequenceNum : %d",
139 clientResponse->sequenceNumber);
140 NS_LOG_V(DEBUG, "GET response resource uri : %s",
141 clientResponse->resourceUri);
143 if (!NSIsStartedConsumer())
145 return OC_STACK_DELETE_TRANSACTION;
148 NSProvider_internal * newProvider = NSGetProvider(clientResponse);
149 NS_VERIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
151 NS_LOG(DEBUG, "build NSTask");
152 NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DISCOVERED, (void *) newProvider);
153 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveProviderObj(newProvider));
155 NSConsumerPushEvent(task);
157 return OC_STACK_KEEP_TRANSACTION;
160 void NSGetProviderPostClean(char * pId, char * mUri, char * sUri, OCDevAddr * addr)
168 NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse)
170 NS_LOG(DEBUG, "create NSProvider");
171 NS_VERIFY_NOT_NULL(clientResponse->payload, NULL);
173 OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
176 NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
177 payload = payload->next;
180 payload = (OCRepPayload *)clientResponse->payload;
182 char * providerId = NULL;
183 char * messageUri = NULL;
184 char * syncUri = NULL;
185 int64_t accepter = 0;
186 OCDevAddr * addr = NULL;
188 NS_LOG(DEBUG, "get information of accepter");
189 bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_POLICY, & accepter);
190 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
192 NS_LOG(DEBUG, "get provider ID");
193 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, & providerId);
194 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
196 NS_LOG(DEBUG, "get message URI");
197 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_MESSAGE, & messageUri);
198 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
199 NSGetProviderPostClean(providerId, messageUri, syncUri, addr));
201 NS_LOG(DEBUG, "get sync URI");
202 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SYNC, & syncUri);
203 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
204 NSGetProviderPostClean(providerId, messageUri, syncUri, addr));
206 NS_LOG(DEBUG, "get provider address");
207 addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
208 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(addr, NULL,
209 NSGetProviderPostClean(providerId, messageUri, syncUri, addr));
211 memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
213 NSProvider_internal * newProvider
214 = (NSProvider_internal *)OICMalloc(sizeof(NSProvider_internal));
215 NS_VERIFY_NOT_NULL(newProvider, NULL);
217 OICStrcpy(newProvider->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
218 NSOICFree(providerId);
219 newProvider->messageUri = messageUri;
220 newProvider->syncUri = syncUri;
221 newProvider->accessPolicy = (NSAccessPolicy)accepter;
222 newProvider->_addr = addr;
223 newProvider->messageHandle = NULL;
224 newProvider->syncHandle = NULL;
229 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
231 NS_VERIFY_NOT_NULL_V(task);
233 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
234 if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_CONSUMER_REQ_DISCOVER)
236 NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL, NS_DISCOVER_QUERY,
237 NULL, NSProviderDiscoverListener, NULL);
241 NS_LOG(ERROR, "Unknown type message");