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 NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
\r
113 NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));
\r
115 subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);
\r
116 subData->isWhite = false;
\r
117 subData->messageObId = entityHandlerRequest->obsInfo.obsId;
\r
118 subData->syncObId = 0;
\r
120 element->data = (void*) subData;
\r
121 element->next = NULL;
\r
123 printf("NS_ IP = %s in message ob\n", entityHandlerRequest->devAddr.addr);
\r
124 printf("NS_ IP2 = %s in message ob\n", subData->id);
\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, true);
\r
145 else if (resourceType == NS_RESOURCE_SYNC)
\r
147 NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
\r
149 NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));
\r
150 printf("NS_ entityHandlerRequest->devAddr.addr = %s\n", entityHandlerRequest->devAddr.addr);
\r
151 subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);
\r
152 subData->isWhite = false;
\r
153 subData->syncObId = entityHandlerRequest->obsInfo.obsId;
\r
154 subData->messageObId = 0;
\r
156 element->data = (void*) subData;
\r
157 element->next = NULL;
\r
159 printf("NS_ sync ob Id = %d\n", subData->syncObId);
\r
161 if (NSCacheWrite(consumerSubList, element) != NS_OK)
\r
163 printf("Cache Write Error\n");
\r
168 void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest)
\r
170 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to unsubscription process");
\r
172 NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
\r
173 NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));
\r
174 subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);
\r
175 subData->isWhite = false;
\r
176 subData->messageObId = entityHandlerRequest->obsInfo.obsId;
\r
178 element->data = (void*) subData;
\r
179 element->next = NULL;
\r
181 if (NSCacheWrite(consumerSubList, element) != NS_OK)
\r
183 OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");
\r
187 void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest)
\r
189 OIC_LOG(DEBUG, SUBSCRIPTION_TAG, "Ask for user to Allow or Deny");
\r
191 NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SUBSCRIPTION, entityHandlerRequest);
\r
192 // one queue is required for response callback to ask subscription allowance
\r
195 NSResult NSSendResponse(const char * id, bool accepted)
\r
198 OCRepPayload* payload = OCRepPayloadCreate();
\r
201 OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to allocate Payload"));
\r
205 OCResourceHandle rHandle;
\r
206 if (NSPutMessageResource(NULL, &rHandle) != NS_OK)
\r
208 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource"));
\r
212 OCRepPayloadSetUri(payload, NSGetNotificationMessageUri());
\r
213 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_ID, "0000-0000-0000-0000");
\r
214 OCRepPayloadSetPropBool(payload, NS_ATTRIBUTE_ACCPETANCE, accepted);
\r
216 NSCacheElement * element = NSCacheRead(consumerSubList, id);
\r
218 if(element == NULL)
\r
220 printf("NS_ NSSendResponse - element is NULL\n");
\r
223 NSCacheSubData * subData = (NSCacheSubData*) element->data;
\r
225 if (OCNotifyListOfObservers(rHandle, (OCObservationId*)&subData->messageObId, 1, payload, OC_HIGH_QOS)
\r
228 OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to send Acceptance");
\r
229 OCRepPayloadDestroy(payload);
\r
233 OCRepPayloadDestroy(payload);
\r
238 NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest, bool accepted)
\r
240 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Send ACCEPT message to consumer");
\r
242 if (!entityHandlerRequest)
\r
244 OIC_LOG (ERROR, LISTENER_TAG, "Invalid request pointer");
\r
245 return OC_EH_ERROR;
\r
248 // write consumer info to cache
\r
250 printf("accepted 1 \n");
\r
253 NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
\r
255 NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));
\r
256 subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);
\r
257 subData->isWhite = true;
\r
258 subData->messageObId = entityHandlerRequest->obsInfo.obsId;
\r
260 element->data = (void*) subData;
\r
261 element->next = NULL;
\r
263 if (NSCacheWrite(consumerSubList, element) != NS_OK)
\r
265 OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");
\r
269 NSSendResponse(entityHandlerRequest->devAddr.addr, accepted);
\r
275 void * NSSubScriptionSchedule(void *ptr)
\r
280 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Create NSSubscriptionSchedule");
\r
283 while (NSIsRunning[SUBSCRIPTION_SCHEDULER])
\r
285 sem_wait(&NSSemaphore[SUBSCRIPTION_SCHEDULER]);
\r
286 pthread_mutex_lock(&NSMutex[SUBSCRIPTION_SCHEDULER]);
\r
288 if (NSHeadMsg[SUBSCRIPTION_SCHEDULER] != NULL)
\r
290 NSTask *node = NSHeadMsg[SUBSCRIPTION_SCHEDULER];
\r
291 NSHeadMsg[SUBSCRIPTION_SCHEDULER] = node->nextTask;
\r
293 switch (node->taskType)
\r
296 case TASK_SEND_POLICY:
\r
297 NSSendAccessPolicyResponse((OCEntityHandlerRequest*) node->taskData);
\r
300 case TASK_RECV_SUBSCRIPTION:
\r
301 printf("before TASK_RECV_SUBSCRIPTION:\n");
\r
302 NSHandleSubscription((OCEntityHandlerRequest*) node->taskData,
\r
303 NS_RESOURCE_MESSAGE);
\r
304 printf("after TASK_RECV_SUBSCRIPTION:\n");
\r
307 case TASK_RECV_UNSUBSCRIPTION:
\r
309 NSHandleUnsubscription((OCEntityHandlerRequest*) node->taskData);
\r
312 case TASK_SEND_ALLOW:
\r
314 NSConsumer * consumer = (NSConsumer *) node->taskData;
\r
315 int * pObId = (int *) consumer->mUserData;
\r
317 NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));
\r
318 subData->id = OICStrdup(consumer->mId);
\r
319 subData->isWhite = true;
\r
320 subData->messageObId = 0;
\r
321 subData->syncObId = 0;
\r
323 NSCacheUpdateSubScriptionState(consumerSubList, subData);
\r
325 printf("observer ID = %d\n", *pObId);
\r
327 NSSendResponse(consumer->mId, true);
\r
331 case TASK_SEND_DENY:
\r
333 NSConsumer * consumer = (NSConsumer *) node->taskData;
\r
334 int * pObId = (int *) consumer->mUserData;
\r
336 NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));
\r
337 subData->id = OICStrdup(consumer->mId);
\r
338 subData->isWhite = false;
\r
339 subData->messageObId = 0;
\r
340 subData->syncObId = 0;
\r
342 NSCacheUpdateSubScriptionState(consumerSubList, subData);
\r
343 printf("observer ID = %d\n", *pObId);
\r
344 NSSendResponse(consumer->mId, false);
\r
347 case TASK_SYNC_SUBSCRIPTION:
\r
348 NSHandleSubscription((OCEntityHandlerRequest*) node->taskData,
\r
358 pthread_mutex_unlock(&NSMutex[SUBSCRIPTION_SCHEDULER]);
\r