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 consumerSubList = NSCacheCreate();
\r
26 consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER;
\r
30 NSResult NSSetSubscriptionAcceptPolicy(NSAccessPolicy policy)
\r
32 if (policy == NS_ACCEPTER_PROVIDER)
\r
34 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Place Provider as a subscription accepter");
\r
36 else if (policy == NS_ACCEPTER_CONSUMER)
\r
38 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Place Consumer as a subscription accepter");
\r
41 NSSubscriptionAccepter = policy;
\r
46 int NSGetSubscriptionAccepter()
\r
48 return NSSubscriptionAccepter;
\r
51 NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest)
\r
53 OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Policy to consumer");
\r
55 // put notification resource
\r
56 OCResourceHandle notificationResourceHandle;
\r
57 if (NSPutNotificationResource(NSGetSubscriptionAccepter(), ¬ificationResourceHandle)
\r
60 OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to put notification resource"));
\r
64 // make response for the Get Request
\r
65 OCEntityHandlerResponse response;
\r
66 response.numSendVendorSpecificHeaderOptions = 0;
\r
67 memset(response.sendVendorSpecificHeaderOptions, 0,
\r
68 sizeof response.sendVendorSpecificHeaderOptions);
\r
69 memset(response.resourceUri, 0, sizeof response.resourceUri);
\r
71 OCRepPayload* payload = OCRepPayloadCreate();
\r
72 //response.payload = OCRepPayloadCreate();
\r
75 OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to allocate Payload"));
\r
79 OCRepPayloadSetUri(payload, NSGetNotificationUri());
\r
80 OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_POLICY, NSGetSubscriptionAccepter());
\r
81 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_MESSAGE, NSGetNotificationMessageUri());
\r
82 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_SYNC, NSGetNotificationSyncUri());
\r
84 response.requestHandle = entityHandlerRequest->requestHandle;
\r
85 response.resourceHandle = entityHandlerRequest->resource;
\r
86 response.persistentBufferFlag = 0;
\r
87 response.ehResult = OC_EH_OK;
\r
88 response.payload = (OCPayload *) payload;
\r
91 if (OCDoResponse(&response) != OC_STACK_OK)
\r
93 OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Fail to send response"));
\r
96 OCRepPayloadDestroy(payload);
\r
101 void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResourceType resourceType)
\r
104 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to subscription process");
\r
106 printf("state = 0 \n");
\r
108 // write consumer info to cache
\r
110 if (resourceType == NS_RESOURCE_MESSAGE)
\r
112 int obId = entityHandlerRequest->obsInfo.obsId;
\r
114 NSCacheElement * element = (NSCacheElement *) malloc(sizeof(NSCacheElement));
\r
116 NSCacheSubData * subData = (NSCacheSubData *) malloc(sizeof(NSCacheSubData));
\r
117 subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);
\r
118 subData->isWhite = false;
\r
119 subData->messageObId = entityHandlerRequest->obsInfo.obsId;
\r
120 subData->syncObId = 0;
\r
122 element->data = (void*) subData;
\r
123 element->next = NULL;
\r
125 printf("NS_ message ob Id = %d\n", subData->messageObId);
\r
127 if (NSCacheWrite(consumerSubList, element) != NS_OK)
\r
129 printf("Cache Write Error\n");
\r
132 if (NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)
\r
134 // OCDevAddr --> NSConsumer
\r
135 printf("before NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)\n");
\r
136 NSAskAcceptanceToUser(entityHandlerRequest);
\r
137 printf("After NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)\n");
\r
139 else if (NSGetSubscriptionAccepter() == NS_ACCEPTER_CONSUMER)
\r
141 printf("state = 0 - 3\n");
\r
142 NSSendSubscriptionResponse(entityHandlerRequest, NS_ACCEPTER_CONSUMER, true);
\r
145 else if (resourceType == NS_RESOURCE_SYNC)
\r
147 int obId = entityHandlerRequest->obsInfo.obsId;
\r
149 NSCacheElement * element = (NSCacheElement *) malloc(sizeof(NSCacheElement));
\r
151 NSCacheSubData * subData = (NSCacheSubData *) malloc(sizeof(NSCacheSubData));
\r
152 printf("NS_ entityHandlerRequest->devAddr.addr = %s\n", entityHandlerRequest->devAddr.addr);
\r
153 subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);
\r
154 subData->isWhite = false;
\r
155 subData->syncObId = entityHandlerRequest->obsInfo.obsId;
\r
156 subData->messageObId = 0;
\r
158 element->data = (void*) subData;
\r
159 element->next = NULL;
\r
161 printf("NS_ sync ob Id = %d\n", subData->syncObId);
\r
163 if (NSCacheWrite(consumerSubList, element) != NS_OK)
\r
165 printf("Cache Write Error\n");
\r
170 void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest)
\r
172 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to unsubscription process");
\r
174 // ProcessObserveUnregister (entityHandlerRequest);
\r
176 // write consumer info to cache
\r
177 int obId = entityHandlerRequest->obsInfo.obsId;
\r
179 NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
\r
180 NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));
\r
181 subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);
\r
182 subData->isWhite = false;
\r
183 subData->messageObId = entityHandlerRequest->obsInfo.obsId;
\r
185 element->data = (void*) subData;
\r
186 element->next = NULL;
\r
188 if (NSCacheWrite(consumerSubList, element) != NS_OK)
\r
190 OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");
\r
194 void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest)
\r
196 OIC_LOG(DEBUG, SUBSCRIPTION_TAG, "Ask for user to Allow or Deny");
\r
198 NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SUBSCRIPTION, entityHandlerRequest);
\r
199 // one queue is required for response callback to ask subscription allowance
\r
202 NSResult NSSendResponse(const char * id, bool accepted)
\r
205 OCRepPayload* payload = OCRepPayloadCreate();
\r
208 OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to allocate Payload"));
\r
212 OCResourceHandle rHandle;
\r
213 if (NSPutMessageResource(NULL, &rHandle) != NS_OK)
\r
215 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource"));
\r
219 OCRepPayloadSetUri(payload, NSGetNotificationMessageUri());
\r
220 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_ID, "0000-0000-0000-0000");
\r
221 OCRepPayloadSetPropBool(payload, NS_ATTRIBUTE_ACCPETANCE, accepted);
\r
223 NSCacheElement * element = NSCacheRead(consumerSubList, id);
\r
224 NSCacheSubData * subData = element->data;
\r
226 if (OCNotifyListOfObservers(rHandle, &subData->messageObId, 1, payload, OC_HIGH_QOS)
\r
229 OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to send Acceptance");
\r
230 OCRepPayloadDestroy(payload);
\r
234 OCRepPayloadDestroy(payload);
\r
239 NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest,
\r
240 NSAccessPolicy accepter, bool accepted)
\r
242 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Send ACCEPT message to consumer");
\r
244 if (!entityHandlerRequest)
\r
246 OIC_LOG (ERROR, LISTENER_TAG, "Invalid request pointer");
\r
247 return OC_EH_ERROR;
\r
250 // write consumer info to cache
\r
252 printf("accepted 1 \n");
\r
255 int obId = entityHandlerRequest->obsInfo.obsId;
\r
257 NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
\r
259 NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));
\r
260 subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);
\r
261 subData->isWhite = true;
\r
262 subData->messageObId = entityHandlerRequest->obsInfo.obsId;
\r
264 element->data = (void*) subData;
\r
265 element->next = NULL;
\r
267 if (NSCacheWrite(consumerSubList, element) != NS_OK)
\r
269 OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");
\r
273 NSSendResponse(&entityHandlerRequest->obsInfo.obsId, accepted);
\r
279 void * NSSubScriptionSchedule(void *ptr)
\r
284 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Create NSSubscriptionSchedule");
\r
287 while (NSIsRunning[SUBSCRIPTION_SCHEDULER])
\r
289 sem_wait(&NSSemaphore[SUBSCRIPTION_SCHEDULER]);
\r
290 pthread_mutex_lock(&NSMutex[SUBSCRIPTION_SCHEDULER]);
\r
292 if (NSHeadMsg[SUBSCRIPTION_SCHEDULER] != NULL)
\r
294 NSTask *node = NSHeadMsg[SUBSCRIPTION_SCHEDULER];
\r
295 NSHeadMsg[SUBSCRIPTION_SCHEDULER] = node->nextTask;
\r
297 switch (node->taskType)
\r
300 case TASK_SEND_POLICY:
\r
302 NSSendAccessPolicyResponse((OCEntityHandlerRequest*) node->taskData);
\r
305 case TASK_RECV_SUBSCRIPTION:
\r
306 printf("before TASK_RECV_SUBSCRIPTION:\n");
\r
307 NSHandleSubscription((OCEntityHandlerRequest*) node->taskData,
\r
308 NS_RESOURCE_MESSAGE);
\r
309 printf("after TASK_RECV_SUBSCRIPTION:\n");
\r
312 case TASK_RECV_UNSUBSCRIPTION:
\r
314 NSHandleUnsubscription((OCEntityHandlerRequest*) node->taskData);
\r
317 case TASK_SEND_ALLOW:
\r
319 NSConsumer * consumer = (NSConsumer *) node->taskData;
\r
320 int * pObId = (int *) consumer->mUserData;
\r
322 NSCacheSubData * subData = (NSCacheMsgData *) OICMalloc(sizeof(NSCacheSubData));
\r
323 subData->id = OICStrdup(consumer->mId);
\r
324 subData->isWhite = true;
\r
325 subData->messageObId = 0;
\r
326 subData->syncObId = 0;
\r
328 NSCacheUpdateSubScriptionState(consumerSubList, subData);
\r
330 printf("observer ID = %d\n", *pObId);
\r
332 NSSendResponse(consumer->mId, true);
\r
336 case TASK_SEND_DENY:
\r
338 NSConsumer * consumer = (NSConsumer *) node->taskData;
\r
339 int * pObId = (int *) consumer->mUserData;
\r
341 NSCacheSubData * subData = (NSCacheMsgData *) OICMalloc(sizeof(NSCacheSubData));
\r
342 subData->id = OICStrdup(consumer->mId);
\r
343 subData->isWhite = false;
\r
344 subData->messageObId = 0;
\r
345 subData->syncObId = 0;
\r
347 NSCacheUpdateSubScriptionState(consumerSubList, subData);
\r
348 printf("observer ID = %d\n", *pObId);
\r
349 NSSendResponse(consumer->mId, false);
\r
352 case TASK_SYNC_SUBSCRIPTION:
\r
353 NSHandleSubscription((OCEntityHandlerRequest*) node->taskData,
\r
361 pthread_mutex_unlock(&NSMutex[SUBSCRIPTION_SCHEDULER]);
\r