1 //******************************************************************
\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
7 // Licensed under the Apache License, Version 2.0 (the "License");
\r
8 // you may not use this file except in compliance with the License.
\r
9 // You may obtain a copy of the License at
\r
11 // http://www.apache.org/licenses/LICENSE-2.0
\r
13 // Unless required by applicable law or agreed to in writing, software
\r
14 // distributed under the License is distributed on an "AS IS" BASIS,
\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 // See the License for the specific language governing permissions and
\r
17 // limitations under the License.
\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
21 #include "NSProviderSubscription.h"
\r
23 NSResult NSInitSubscriptionList()
\r
25 NS_LOG(DEBUG, "NSInitSubscriptionList - IN");
\r
27 consumerSubList = NSStorageCreate();
\r
28 consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER;
\r
30 NS_LOG(DEBUG, "NSInitSubscriptionList - OUT");
\r
34 NSResult NSSetSubscriptionAccessPolicy(NSAccessPolicy access)
\r
36 NS_LOG(DEBUG, "NSSetSubscriptionAcceptPolicy - IN");
\r
38 if (access == NS_ACCESS_ALLOW)
\r
40 NS_LOG(DEBUG, "Place Provider as a subscription accepter");
\r
42 else if (access == NS_ACCESS_DENY)
\r
44 NS_LOG(DEBUG, "Place Consumer as a subscription accepter");
\r
47 NSSubscriptionAccess = access;
\r
49 NS_LOG(DEBUG, "NSSetSubscriptionAcceptPolicy - OUT");
\r
53 int NSGetSubscriptionAccepter()
\r
55 return NSSubscriptionAccess;
\r
58 NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest)
\r
60 NS_LOG(DEBUG, "NSSendAccessPolicyResponse - IN");
\r
62 // put notification resource
\r
63 OCResourceHandle notificationResourceHandle;
\r
64 if (NSPutNotificationResource(NSGetSubscriptionAccepter(), ¬ificationResourceHandle)
\r
67 NS_LOG(ERROR, "Fail to put notification resource");
\r
71 // make response for the Get Request
\r
72 OCEntityHandlerResponse response;
\r
73 response.numSendVendorSpecificHeaderOptions = 0;
\r
74 memset(response.sendVendorSpecificHeaderOptions, 0,
\r
75 sizeof response.sendVendorSpecificHeaderOptions);
\r
76 memset(response.resourceUri, 0, sizeof response.resourceUri);
\r
78 OCRepPayload* payload = OCRepPayloadCreate();
\r
81 NS_LOG(ERROR, "payload is NULL");
\r
85 NS_LOG_V(DEBUG, "NS Provider ID: %s", NSGetProviderInfo()->providerId);
\r
87 OCRepPayloadSetUri(payload, NS_ROOT_URI);
\r
88 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, NSGetProviderInfo()->providerId);
\r
89 OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_POLICY, NSGetSubscriptionAccepter());
\r
90 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_MESSAGE, NS_COLLECTION_MESSAGE_URI);
\r
91 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_SYNC, NS_COLLECTION_SYNC_URI);
\r
93 response.requestHandle = entityHandlerRequest->requestHandle;
\r
94 response.resourceHandle = entityHandlerRequest->resource;
\r
95 response.persistentBufferFlag = 0;
\r
96 response.ehResult = OC_EH_OK;
\r
97 response.payload = (OCPayload *) payload;
\r
100 if (OCDoResponse(&response) != OC_STACK_OK)
\r
102 NS_LOG(ERROR, "Fail to AccessPolicy send response");
\r
105 OCRepPayloadDestroy(payload);
\r
106 NSFreeOCEntityHandlerRequest(entityHandlerRequest);
\r
108 NS_LOG(DEBUG, "NSSendAccessPolicyResponse - OUT");
\r
112 void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResourceType resourceType)
\r
114 NS_LOG(DEBUG, "NSHandleSubscription - IN");
\r
116 char * id = NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_CONSUMER_ID);
\r
120 NSFreeOCEntityHandlerRequest(entityHandlerRequest);
\r
121 NS_LOG(ERROR, "Invalid ConsumerID");
\r
125 NS_LOG_V(DEBUG, "consumerId = %s", id);
\r
126 if (resourceType == NS_RESOURCE_MESSAGE)
\r
128 NS_LOG(DEBUG, "resourceType == NS_RESOURCE_MESSAGE");
\r
129 NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
\r
130 NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));
\r
132 OICStrcpy(subData->id, UUID_STRING_SIZE, id);
\r
133 NS_LOG_V(DEBUG, "SubList ID = [%s]", subData->id);
\r
135 NS_LOG_V(DEBUG, "Consumer Address: %s", entityHandlerRequest->devAddr.addr);
\r
136 if(NSIsRemoteServerAddress(entityHandlerRequest->devAddr.addr))
\r
138 NS_LOG(DEBUG, "Requested by remote server");
\r
139 subData->remote_messageObId = entityHandlerRequest->obsInfo.obsId;
\r
140 subData->messageObId = 0;
\r
141 NS_LOG_V(DEBUG, "SubList message observation ID = [%d]", subData->remote_messageObId);
\r
145 NS_LOG(DEBUG, "Requested by local consumer");
\r
146 subData->messageObId = entityHandlerRequest->obsInfo.obsId;
\r
147 subData->remote_messageObId = 0;
\r
148 NS_LOG_V(DEBUG, "SubList message observation ID = [%d]", subData->messageObId);
\r
150 subData->isWhite = false;
\r
151 subData->remote_syncObId = 0;
\r
152 subData->syncObId = 0;
\r
154 element->data = (void*) subData;
\r
155 element->next = NULL;
\r
157 if (NSStorageWrite(consumerSubList, element) != NS_OK)
\r
159 NS_LOG(DEBUG, "fail to write cache");
\r
162 if (NSGetSubscriptionAccepter() == NS_ACCESS_ALLOW)
\r
164 NS_LOG(DEBUG, "NSGetSubscriptionAccepter == NS_ACCEPTER_PROVIDER");
\r
165 NSAskAcceptanceToUser(entityHandlerRequest);
\r
167 else if (NSGetSubscriptionAccepter() == NS_ACCESS_DENY)
\r
169 NS_LOG(DEBUG, "NSGetSubscriptionAccepter == NS_ACCEPTER_CONSUMER");
\r
170 NSSendSubscriptionResponse(entityHandlerRequest, true);
\r
173 else if (resourceType == NS_RESOURCE_SYNC)
\r
175 NS_LOG(DEBUG, "resourceType == NS_RESOURCE_SYNC");
\r
176 NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
\r
177 NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));
\r
179 OICStrcpy(subData->id, UUID_STRING_SIZE, id);
\r
180 NS_LOG_V(DEBUG, "SubList ID = [%s]", subData->id);
\r
182 NS_LOG_V(DEBUG, "Consumer Address: %s", entityHandlerRequest->devAddr.addr);
\r
183 if(NSIsRemoteServerAddress(entityHandlerRequest->devAddr.addr))
\r
185 NS_LOG(DEBUG, "Requested by remote server");
\r
186 subData->remote_syncObId = entityHandlerRequest->obsInfo.obsId;
\r
187 subData->syncObId = 0;
\r
188 NS_LOG_V(DEBUG, "SubList sync observation ID = [%d]", subData->remote_syncObId);
\r
192 NS_LOG(DEBUG, "Requested by local consumer");
\r
193 subData->syncObId = entityHandlerRequest->obsInfo.obsId;
\r
194 subData->remote_syncObId = 0;
\r
195 NS_LOG_V(DEBUG, "SubList sync observation ID = [%d]", subData->syncObId);
\r
198 subData->isWhite = false;
\r
199 subData->messageObId = 0;
\r
200 subData->remote_messageObId = 0;
\r
202 element->data = (void*) subData;
\r
203 element->next = NULL;
\r
205 if (NSStorageWrite(consumerSubList, element) != NS_OK)
\r
207 NS_LOG(ERROR, "Fail to write cache");
\r
210 NSFreeOCEntityHandlerRequest(entityHandlerRequest);
\r
213 NS_LOG(DEBUG, "NSHandleSubscription - OUT");
\r
216 void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest)
\r
218 NS_LOG(DEBUG, "NSHandleUnsubscription - IN");
\r
220 NSProviderDeleteSubDataFromObId(consumerSubList, entityHandlerRequest->obsInfo.obsId);
\r
222 NSFreeOCEntityHandlerRequest(entityHandlerRequest);
\r
224 NS_LOG(DEBUG, "NSHandleUnsubscription - OUT");
\r
227 void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest)
\r
229 NS_LOG(DEBUG, "NSAskAcceptanceToUser - IN");
\r
231 NSPushQueue(CALLBACK_RESPONSE_SCHEDULER, TASK_CB_SUBSCRIPTION, entityHandlerRequest);
\r
233 NS_LOG(DEBUG, "NSAskAcceptanceToUser - OUT");
\r
236 NSResult NSSendResponse(const char * id, bool accepted)
\r
238 NS_LOG(DEBUG, "NSSendResponse - IN");
\r
240 OCRepPayload* payload = OCRepPayloadCreate();
\r
243 NS_LOG(ERROR, "fail to create playload");
\r
247 OCResourceHandle rHandle;
\r
248 if (NSPutMessageResource(NULL, &rHandle) != NS_OK)
\r
250 NS_LOG(ERROR, "Fail to put notification resource");
\r
254 OCRepPayloadSetUri(payload, NS_COLLECTION_MESSAGE_URI);
\r
255 OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, 1);
\r
256 OCRepPayloadSetPropBool(payload, NS_ATTRIBUTE_ACCPETANCE, accepted);
\r
257 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, NSGetProviderInfo()->providerId);
\r
259 NSCacheElement * element = NSStorageRead(consumerSubList, id);
\r
261 if(element == NULL)
\r
263 NS_LOG(ERROR, "element is NULL");
\r
266 NSCacheSubData * subData = (NSCacheSubData*) element->data;
\r
268 if (OCNotifyListOfObservers(rHandle, (OCObservationId*)&subData->messageObId, 1, payload, OC_HIGH_QOS)
\r
271 NS_LOG(ERROR, "fail to send Acceptance");
\r
272 OCRepPayloadDestroy(payload);
\r
276 OCRepPayloadDestroy(payload);
\r
278 NS_LOG(DEBUG, "NSSendResponse - OUT");
\r
282 NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest, bool accepted)
\r
284 NS_LOG(DEBUG, "NSSendSubscriptionResponse - IN");
\r
286 if (!entityHandlerRequest)
\r
288 NS_LOG(ERROR, "Invalid request pointer");
\r
289 return OC_EH_ERROR;
\r
292 char * id = NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_CONSUMER_ID);
\r
296 NSFreeOCEntityHandlerRequest(entityHandlerRequest);
\r
297 NS_LOG(ERROR, "Invalid ConsumerID");
\r
303 NS_LOG(DEBUG, "accepted is true");
\r
304 NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
\r
305 NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));
\r
307 OICStrcpy(subData->id, UUID_STRING_SIZE, id);
\r
309 subData->isWhite = true;
\r
310 subData->messageObId = entityHandlerRequest->obsInfo.obsId;
\r
312 element->data = (void*) subData;
\r
313 element->next = NULL;
\r
315 if (NSStorageWrite(consumerSubList, element) != NS_OK)
\r
317 NS_LOG(ERROR, "fail to write consumer white list");
\r
321 NSSendResponse(id, accepted);
\r
323 NSFreeOCEntityHandlerRequest(entityHandlerRequest);
\r
325 NS_LOG(DEBUG, "NSSendSubscriptionResponse - OUT");
\r
329 void * NSSubScriptionSchedule(void *ptr)
\r
333 NS_LOG(DEBUG, "Create NSSubScriptionSchedule");
\r
336 while (NSIsRunning[SUBSCRIPTION_SCHEDULER])
\r
338 sem_wait(&NSSemaphore[SUBSCRIPTION_SCHEDULER]);
\r
339 pthread_mutex_lock(&NSMutex[SUBSCRIPTION_SCHEDULER]);
\r
341 if (NSHeadMsg[SUBSCRIPTION_SCHEDULER] != NULL)
\r
343 NSTask *node = NSHeadMsg[SUBSCRIPTION_SCHEDULER];
\r
344 NSHeadMsg[SUBSCRIPTION_SCHEDULER] = node->nextTask;
\r
346 switch (node->taskType)
\r
348 case TASK_SEND_POLICY:
\r
349 NS_LOG(DEBUG, "CASE TASK_SEND_POLICY : ");
\r
350 NSSendAccessPolicyResponse((OCEntityHandlerRequest*) node->taskData);
\r
353 case TASK_RECV_SUBSCRIPTION:
\r
354 NS_LOG(DEBUG, "CASE TASK_RECV_SUBSCRIPTION : ");
\r
355 NSHandleSubscription((OCEntityHandlerRequest*) node->taskData,
\r
356 NS_RESOURCE_MESSAGE);
\r
359 case TASK_RECV_UNSUBSCRIPTION:
\r
360 NS_LOG(DEBUG, "CASE TASK_RECV_UNSUBSCRIPTION : ");
\r
361 NSHandleUnsubscription((OCEntityHandlerRequest*) node->taskData);
\r
364 case TASK_SEND_ALLOW:
\r
366 NS_LOG(DEBUG, "CASE TASK_SEND_ALLOW : ");
\r
367 NSConsumer * consumer = (NSConsumer *) node->taskData;
\r
369 NSCacheUpdateSubScriptionState(consumerSubList, consumer->consumerId, true);
\r
370 NSSendResponse(consumer->consumerId, true);
\r
371 NSFreeConsumer(consumer);
\r
374 case TASK_SEND_DENY:
\r
376 NS_LOG(DEBUG, "CASE TASK_SEND_DENY : ");
\r
377 NSConsumer * consumer = (NSConsumer *) node->taskData;
\r
379 NSCacheUpdateSubScriptionState(consumerSubList, consumer->consumerId, false);
\r
380 NSSendResponse(consumer->consumerId, false);
\r
381 NSFreeConsumer(consumer);
\r
385 case TASK_SYNC_SUBSCRIPTION:
\r
386 NS_LOG(DEBUG, "CASE TASK_SYNC_SUBSCRIPTION : ");
\r
387 NSHandleSubscription((OCEntityHandlerRequest*) node->taskData,
\r
397 pthread_mutex_unlock(&NSMutex[SUBSCRIPTION_SCHEDULER]);
\r
400 NS_LOG(INFO, "Destroy NSSubScriptionSchedule");
\r