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 NSSetSubscriptionAcceptPolicy(NSAccessPolicy policy)
\r
25 if (policy == NS_ACCEPTER_PROVIDER)
\r
27 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Place Provider as a subscription accepter");
\r
29 else if (policy == NS_ACCEPTER_CONSUMER)
\r
31 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Place Consumer as a subscription accepter");
\r
34 NSSubscriptionAccepter = policy;
\r
39 int NSGetSubscriptionAccepter()
\r
41 return NSSubscriptionAccepter;
\r
44 NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest)
\r
46 OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Policy to consumer");
\r
48 // put notification resource
\r
49 OCResourceHandle notificationResourceHandle;
\r
50 if(NSPutNotificationResource(NSGetSubscriptionAccepter(), ¬ificationResourceHandle) != NS_OK)
\r
52 OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to put notification resource"));
\r
56 // make response for the Get Request
\r
57 OCEntityHandlerResponse response;
\r
58 response.numSendVendorSpecificHeaderOptions = 0;
\r
59 memset(response.sendVendorSpecificHeaderOptions, 0,
\r
60 sizeof response.sendVendorSpecificHeaderOptions);
\r
61 memset(response.resourceUri, 0, sizeof response.resourceUri);
\r
63 OCRepPayload* payload = OCRepPayloadCreate();
\r
64 //response.payload = OCRepPayloadCreate();
\r
67 OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to allocate Payload"));
\r
71 OCRepPayloadSetUri(payload, NSGetNotificationUri());
\r
72 OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_POLICY, NSGetSubscriptionAccepter());
\r
73 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_MESSAGE, NSGetNotificationMessageUri());
\r
74 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_SYNC, NSGetNotificationSyncUri());
\r
76 response.requestHandle = entityHandlerRequest->requestHandle;
\r
77 response.resourceHandle = entityHandlerRequest->resource;
\r
78 response.persistentBufferFlag = 0;
\r
79 response.ehResult = OC_EH_OK;
\r
80 response.payload = (OCPayload *)payload;
\r
84 if (OCDoResponse(&response) != OC_STACK_OK)
\r
86 OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Fail to send response"));
\r
89 OCRepPayloadDestroy(payload);
\r
94 void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, int state)
\r
97 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to subscription process");
\r
99 printf("state = 0 \n");
\r
101 // write consumer info to cache
\r
105 printf("state = 0 - 1\n");
\r
107 if (NSProviderWriteToCache(NS_CONSUMER_BLACKLIST, entityHandlerRequest->obsInfo.obsId, state) != NS_OK)
\r
109 OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("fail to write consumer black list"));
\r
111 printf("state = 0 - 2\n");
\r
112 // check access policy
\r
113 if (NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)
\r
115 // OCDevAddr --> NSConsumer
\r
116 printf("before NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)\n");
\r
117 NSAskAcceptanceToUser(entityHandlerRequest);
\r
118 printf("After NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)\n");
\r
120 else if (NSGetSubscriptionAccepter() == NS_ACCEPTER_CONSUMER)
\r
122 printf("state = 0 - 3\n");
\r
123 NSSendSubscriptionResponse(entityHandlerRequest, NS_ACCEPTER_CONSUMER, true);
\r
126 else if(state == 1)
\r
128 printf("state = 1 - 0\n");
\r
129 if (NSProviderWriteToCache(NS_CONSUMER_WHITELIST, entityHandlerRequest->obsInfo.obsId, state) != NS_OK)
\r
131 OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("fail to write consumer white list"));
\r
133 printf("state = 1 - 1\n");
\r
137 void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest)
\r
139 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to unsubscription process");
\r
141 // ProcessObserveUnregister (entityHandlerRequest);
\r
143 // write consumer info to cache
\r
144 if (NSProviderWriteToCache(NS_CONSUMER_BLACKLIST, entityHandlerRequest->devAddr) != NS_OK)
\r
146 OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer black list");
\r
150 void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest)
\r
152 OIC_LOG(DEBUG, SUBSCRIPTION_TAG, "Ask for user to Allow or Deny");
\r
154 NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SUBSCRIPTION, entityHandlerRequest);
\r
155 // one queue is required for response callback to ask subscription allowance
\r
158 NSResult NSSendResponse(int observeId, bool accepted)
\r
161 OCRepPayload* payload = OCRepPayloadCreate();
\r
164 OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to allocate Payload"));
\r
168 OCResourceHandle rHandle;
\r
169 if (NSPutMessageResource(NULL, &rHandle) != NS_OK)
\r
171 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource"));
\r
175 OCRepPayloadSetUri(payload, NSGetNotificationMessageUri());
\r
176 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_ID, "0000-0000-0000-0000");
\r
177 OCRepPayloadSetPropBool(payload, NS_ATTRIBUTE_ACCPETANCE, accepted);
\r
179 if (OCNotifyListOfObservers(rHandle, &observeId, 1, payload, OC_HIGH_QOS) != OC_STACK_OK)
\r
181 OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to send Acceptance");
\r
182 OCRepPayloadDestroy(payload);
\r
186 OCRepPayloadDestroy(payload);
\r
191 NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest,
\r
192 NSAccessPolicy accepter, bool accepted)
\r
194 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Send ACCEPT message to consumer");
\r
196 if (!entityHandlerRequest)
\r
198 OIC_LOG (ERROR, LISTENER_TAG, "Invalid request pointer");
\r
199 return OC_EH_ERROR;
\r
202 // write consumer info to cache
\r
204 printf("accepted 1 \n");
\r
207 if (NSProviderWriteToCache(NS_CONSUMER_WHITELIST, entityHandlerRequest->obsInfo.obsId) != NS_OK)
\r
209 OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");
\r
213 NSSendResponse(&entityHandlerRequest->obsInfo.obsId, accepted);
\r
219 void * NSSubScriptionSchedule(void *ptr)
\r
224 OIC_LOG(INFO, SUBSCRIPTION_TAG, "Create NSSubscriptionSchedule");
\r
227 while (NSIsRunning[SUBSCRIPTION_SCHEDULER])
\r
229 sem_wait(&NSSemaphore[SUBSCRIPTION_SCHEDULER]);
\r
230 pthread_mutex_lock(&NSMutex[SUBSCRIPTION_SCHEDULER]);
\r
232 if (NSHeadMsg[SUBSCRIPTION_SCHEDULER] != NULL)
\r
234 NSTask *node = NSHeadMsg[SUBSCRIPTION_SCHEDULER];
\r
235 NSHeadMsg[SUBSCRIPTION_SCHEDULER] = node->nextTask;
\r
237 switch (node->taskType)
\r
240 case TASK_SEND_POLICY:
\r
242 NSSendAccessPolicyResponse((OCEntityHandlerRequest*) node->taskData);
\r
245 case TASK_RECV_SUBSCRIPTION:
\r
246 printf("before TASK_RECV_SUBSCRIPTION:\n");
\r
247 NSHandleSubscription((OCEntityHandlerRequest*) node->taskData, 0);
\r
248 printf("after TASK_RECV_SUBSCRIPTION:\n");
\r
251 case TASK_RECV_UNSUBSCRIPTION:
\r
253 NSHandleUnsubscription((OCEntityHandlerRequest*) node->taskData);
\r
256 case TASK_SEND_ALLOW:
\r
258 NSConsumer * consumer = (NSConsumer *)node->taskData;
\r
259 int * pObId = (int *)consumer->mUserData;
\r
261 printf("observer ID = %d\n", *pObId);
\r
263 if (NSProviderWriteToCache(NS_CONSUMER_WHITELIST, *pObId, 0) != NS_OK)
\r
265 OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");
\r
267 NSSendResponse(*pObId, true);
\r
271 case TASK_SEND_DENY:
\r
273 // NSSendSubscriptionResponse((OCEntityHandlerRequest*) node->taskData,
\r
274 // NS_ACCEPTER_PROVIDER, false);
\r
277 case TASK_SYNC_SUBSCRIPTION:
\r
278 NSHandleSubscription((OCEntityHandlerRequest*) node->taskData, 1);
\r
285 pthread_mutex_unlock(&NSMutex[SUBSCRIPTION_SCHEDULER]);
\r