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 "NSProviderInterface.h"
\r
22 #include "NSProviderScheduler.h"
\r
23 #include "NSProviderListener.h"
\r
24 #include "NSProviderSubscription.h"
\r
25 #include "NSProviderNotification.h"
\r
26 #include "NSStorageAdapter.h"
\r
27 #include "NSProviderMemoryCache.h"
\r
28 #include "oic_malloc.h"
\r
29 #include "oic_string.h"
\r
30 #include "cautilinterface.h"
\r
31 #include "NSProviderSystem.h"
\r
32 #include "oic_time.h"
\r
34 bool initProvider = false;
\r
35 static NSSubscribeRequestCallback g_subscribeRequestCb = NULL;
\r
36 static NSProviderSyncInfoCallback g_syncCb = NULL;
\r
38 pthread_mutex_t nsInitMutex;
\r
40 void initializeMutex()
\r
42 static pthread_mutex_t initMutex = PTHREAD_MUTEX_INITIALIZER;
\r
43 nsInitMutex = initMutex;
\r
46 void NSRegisterSubscribeRequestCb(NSSubscribeRequestCallback subscribeRequestCb)
\r
48 NS_LOG(DEBUG, "NSRegisterSubscribeRequestCb - IN");
\r
49 g_subscribeRequestCb = subscribeRequestCb;
\r
50 NS_LOG(DEBUG, "NSRegisterSubscribeRequestCb - OUT");
\r
53 void NSRegisterSyncCb(NSProviderSyncInfoCallback syncCb)
\r
55 NS_LOG(DEBUG, "NSRegisterSyncCb - IN");
\r
57 NS_LOG(DEBUG, "NSRegisterSyncCb - OUT");
\r
60 void NSSubscribeRequestCb(NSConsumer *consumer)
\r
62 NS_LOG(DEBUG, "NSSubscribeRequestCb - IN");
\r
63 g_subscribeRequestCb(consumer);
\r
64 NS_LOG(DEBUG, "NSSubscribeRequestCb - OUT");
\r
67 void NSSyncCb(NSSyncInfo *sync)
\r
69 NS_LOG(DEBUG, "NSSyncCb - IN");
\r
71 NS_LOG(DEBUG, "NSSyncCb - OUT");
\r
74 NSResult NSStartProvider(NSAccessPolicy policy, NSSubscribeRequestCallback subscribeRequestCb,
\r
75 NSProviderSyncInfoCallback syncCb)
\r
77 NS_LOG(DEBUG, "NSStartProvider - IN");
\r
81 pthread_mutex_lock(&nsInitMutex);
\r
85 NS_LOG(DEBUG, "Init Provider");
\r
86 initProvider = true;
\r
87 NSInitProviderInfo();
\r
88 NSSetSubscriptionAccessPolicy(policy);
\r
89 NSRegisterSubscribeRequestCb(subscribeRequestCb);
\r
90 NSRegisterSyncCb(syncCb);
\r
91 CARegisterNetworkMonitorHandler(NSProviderAdapterStateListener,
\r
92 NSProviderConnectionStateListener);
\r
98 NSPushQueue(DISCOVERY_SCHEDULER, TASK_START_PRESENCE, NULL);
\r
99 NSPushQueue(DISCOVERY_SCHEDULER, TASK_REGISTER_RESOURCE, NULL);
\r
103 NS_LOG(DEBUG, "Already started Notification Provider");
\r
105 pthread_mutex_unlock(&nsInitMutex);
\r
107 NS_LOG(DEBUG, "NSStartProvider - OUT");
\r
113 NS_LOG(DEBUG, "NSSetList - IN");
\r
114 pthread_mutex_init(&NSCacheMutex, NULL);
\r
115 NSInitSubscriptionList();
\r
116 NSInitMessageList();
\r
117 NS_LOG(DEBUG, "NSSetList - OUT");
\r
120 NSResult NSStopProvider()
\r
122 NS_LOG(DEBUG, "NSStopProvider - IN");
\r
123 pthread_mutex_lock(&nsInitMutex);
\r
127 NSDeinitProviderInfo();
\r
128 NSUnRegisterResource();
\r
129 NSRegisterSubscribeRequestCb((NSSubscribeRequestCallback)NULL);
\r
130 NSRegisterSyncCb((NSProviderSyncInfoCallback)NULL);
\r
132 NSStorageDestroy(consumerSubList);
\r
133 NSStorageDestroy(messageList);
\r
135 initProvider = false;
\r
138 pthread_mutex_unlock(&nsInitMutex);
\r
139 NS_LOG(DEBUG, "NSStopProvider - OUT");
\r
143 NSResult NSSendMessage(NSMessage *msg)
\r
145 NS_LOG(DEBUG, "NSSendNotification - IN");
\r
147 pthread_mutex_lock(&nsInitMutex);
\r
151 NS_LOG(ERROR, "Msg is NULL");
\r
152 pthread_mutex_unlock(&nsInitMutex);
\r
156 NSMessage * newMsg = NSDuplicateMessage(msg);
\r
157 NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_NOTIFICATION, newMsg);
\r
159 pthread_mutex_unlock(&nsInitMutex);
\r
161 NS_LOG(DEBUG, "NSSendNotification - OUT");
\r
165 NSResult NSProviderSendSyncInfo(uint64_t messageId, NSSyncType type)
\r
167 NS_LOG(DEBUG, "NSProviderReadCheck - IN");
\r
168 pthread_mutex_lock(&nsInitMutex);
\r
170 NSSyncInfo * syncInfo = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
\r
171 OICStrcpy(syncInfo->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);
\r
172 syncInfo->messageId = messageId;
\r
173 syncInfo->state = type;
\r
174 NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_READ, syncInfo);
\r
176 pthread_mutex_unlock(&nsInitMutex);
\r
177 NS_LOG(DEBUG, "NSProviderReadCheck - OUT");
\r
181 NSResult NSAccept(NSConsumer *consumer, bool accepted)
\r
183 NS_LOG(DEBUG, "NSAccept - IN");
\r
185 pthread_mutex_lock(&nsInitMutex);
\r
187 NSConsumer * newConsumer = NSDuplicateConsumer(consumer);
\r
191 NS_LOG(DEBUG, "accepted is true - ALLOW");
\r
192 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_ALLOW, newConsumer);
\r
196 NS_LOG(DEBUG, "accepted is false - DENY");
\r
197 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_DENY, newConsumer);
\r
200 pthread_mutex_unlock(&nsInitMutex);
\r
201 NS_LOG(DEBUG, "NSAccept - OUT");
\r
205 NSMessage * NSCreateMessage()
\r
207 NS_LOG(DEBUG, "NSCreateMessage - IN");
\r
208 pthread_mutex_lock(&nsInitMutex);
\r
210 NSMessage * msg = NSInitializeMessage();
\r
211 OICStrcpy(msg->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);
\r
213 pthread_mutex_unlock(&nsInitMutex);
\r
214 NS_LOG(DEBUG, "NSCreateMessage - OUT");
\r
218 void * NSInterfaceSchedule(void * ptr)
\r
222 NS_LOG(DEBUG, "Create NSReponseSchedule");
\r
225 while (NSIsRunning[INTERFACE_SCHEDULER])
\r
227 sem_wait(&NSSemaphore[INTERFACE_SCHEDULER]);
\r
228 pthread_mutex_lock(&NSMutex[INTERFACE_SCHEDULER]);
\r
230 if (NSHeadMsg[INTERFACE_SCHEDULER] != NULL)
\r
232 NSTask *node = NSHeadMsg[INTERFACE_SCHEDULER];
\r
233 NSHeadMsg[INTERFACE_SCHEDULER] = node->nextTask;
\r
235 switch (node->taskType)
\r
237 case TASK_CB_SUBSCRIPTION:
\r
239 NS_LOG(DEBUG, "CASE TASK_CB_SUBSCRIPTION : ");
\r
241 OCEntityHandlerRequest * request = (OCEntityHandlerRequest*)node->taskData;
\r
242 NSConsumer * consumer = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
\r
244 char * consumerId = NSGetValueFromQuery(OICStrdup(request->query),
\r
245 NS_QUERY_CONSUMER_ID);
\r
249 OICStrcpy(consumer->consumerId, UUID_STRING_SIZE, consumerId);
\r
250 NSSubscribeRequestCb(consumer);
\r
253 NSFreeConsumer(consumer);
\r
254 NSFreeOCEntityHandlerRequest(request);
\r
260 NS_LOG(DEBUG, "CASE TASK_CB_SYNC : ");
\r
261 NSSyncInfo * sync = (NSSyncInfo*)node->taskData;
\r
262 NSSyncCb(NSDuplicateSync(sync));
\r
267 NS_LOG(DEBUG, "No Task Type");
\r
273 pthread_mutex_unlock(&NSMutex[INTERFACE_SCHEDULER]);
\r
276 NS_LOG(DEBUG, "Destroy NSResponseSchedule");
\r