1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "NSProviderInterface.h"
22 #include "NSProviderScheduler.h"
23 #include "NSProviderListener.h"
24 #include "NSProviderSubscription.h"
25 #include "NSProviderNotification.h"
26 #include "NSProviderCallbackResponse.h"
27 #include "NSStorageAdapter.h"
28 #include "NSProviderMemoryCache.h"
29 #include "NSProviderTopic.h"
30 #include "oic_malloc.h"
31 #include "oic_string.h"
32 #include "cautilinterface.h"
33 #include "NSProviderSystem.h"
37 bool initProvider = false;
39 pthread_mutex_t nsInitMutex;
40 pthread_cond_t nstopicCond;
42 void initializeMutex()
44 static pthread_mutex_t initMutex = PTHREAD_MUTEX_INITIALIZER;
45 nsInitMutex = initMutex;
50 NS_LOG(DEBUG, "NSSetList - IN");
52 pthread_mutexattr_init(&NSCacheMutexAttr);
53 pthread_mutexattr_settype(&NSCacheMutexAttr, PTHREAD_MUTEX_RECURSIVE);
54 pthread_mutex_init(&NSCacheMutex, &NSCacheMutexAttr);
55 pthread_cond_init(&nstopicCond, NULL);
57 NSInitSubscriptionList();
59 NS_LOG(DEBUG, "NSSetList - OUT");
64 NSStorageDestroy(consumerSubList);
65 NSStorageDestroy(consumerTopicList);
66 NSStorageDestroy(registeredTopicList);
68 pthread_mutex_destroy(&NSCacheMutex);
69 pthread_mutexattr_destroy(&NSCacheMutexAttr);
70 pthread_cond_destroy(&nstopicCond);
73 NSResult NSStartProvider(NSProviderConfig config)
75 NS_LOG(DEBUG, "NSStartProvider - IN");
79 pthread_mutex_lock(&nsInitMutex);
83 NS_LOG(DEBUG, "Init Provider");
85 NSInitProviderInfo(config.userInfo);
86 NSSetSubscriptionAccessPolicy(config.subControllability);
87 NSRegisterSubscribeRequestCb(config.subRequestCallback);
88 NSRegisterSyncCb(config.syncInfoCallback);
89 CARegisterNetworkMonitorHandler((CAAdapterStateChangedCB)NSProviderAdapterStateListener,
90 (CAConnectionStateChangedCB)NSProviderConnectionStateListener);
92 NS_LOG(DEBUG, "Check secured build option..");
94 NS_LOG(DEBUG, "Built with SECURED");
96 if(!config.resourceSecurity)
98 NS_LOG(DEBUG, "Resource Security Off");
100 NSSetResourceSecurity(config.resourceSecurity);
107 NSPushQueue(DISCOVERY_SCHEDULER, TASK_START_PRESENCE, NULL);
108 NSPushQueue(DISCOVERY_SCHEDULER, TASK_REGISTER_RESOURCE, NULL);
112 NS_LOG(DEBUG, "Already started Notification Provider");
114 pthread_mutex_unlock(&nsInitMutex);
116 NS_LOG(DEBUG, "NSStartProvider - OUT");
120 NSResult NSStopProvider()
122 NS_LOG(DEBUG, "NSStopProvider - IN");
123 pthread_mutex_lock(&nsInitMutex);
127 NSPushQueue(DISCOVERY_SCHEDULER, TASK_STOP_PRESENCE, NULL);
128 NSDeinitProviderInfo();
129 NSUnRegisterResource();
130 NSRegisterSubscribeRequestCb((NSSubscribeRequestCallback)NULL);
131 NSRegisterSyncCb((NSProviderSyncInfoCallback)NULL);
135 initProvider = false;
138 pthread_mutex_unlock(&nsInitMutex);
139 NS_LOG(DEBUG, "NSStopProvider - OUT");
143 NSResult NSProviderEnableRemoteService(char *serverAddress)
145 #if(defined WITH_CLOUD && defined RD_CLIENT)
146 NS_LOG(DEBUG, "NSProviderEnableRemoteService - IN");
147 pthread_mutex_lock(&nsInitMutex);
149 if(!initProvider || !serverAddress)
151 NS_LOG(DEBUG, "Provider service has not been started yet");
152 pthread_mutex_unlock(&nsInitMutex);
155 NS_LOG_V(DEBUG, "Remote server address: %s", serverAddress);
156 NS_LOG(DEBUG, "Request to publish resource");
157 NSPushQueue(DISCOVERY_SCHEDULER, TASK_PUBLISH_RESOURCE, serverAddress);
159 pthread_mutex_unlock(&nsInitMutex);
160 NS_LOG(DEBUG, "NSProviderEnableRemoteService - OUT");
163 (void) serverAddress;
165 NS_LOG_V(DEBUG, "Not logged in remote server: %s", serverAddress);
169 NSResult NSProviderDisableRemoteService(char *serverAddress)
171 #if(defined WITH_CLOUD && defined RD_CLIENT)
172 NS_LOG(DEBUG, "NSProviderDisableRemoteService - IN");
173 pthread_mutex_lock(&nsInitMutex);
175 if(!initProvider || !serverAddress)
177 NS_LOG(DEBUG, "Provider service has not been started yet");
180 NS_LOG_V(DEBUG, "Remote server address: %s", serverAddress);
182 NS_LOG(DEBUG, "Delete remote server info");
183 NSDeleteRemoteServerAddress(serverAddress);
185 pthread_mutex_unlock(&nsInitMutex);
186 NS_LOG(DEBUG, "NSProviderDisableRemoteService - OUT");
189 (void) serverAddress;
191 NS_LOG_V(DEBUG, "Not logged in remote server : %s", serverAddress);
195 NSResult NSSendMessage(NSMessage * msg)
197 NS_LOG(DEBUG, "NSSendNotification - IN");
199 pthread_mutex_lock(&nsInitMutex);
203 NS_LOG(ERROR, "Msg is NULL");
204 pthread_mutex_unlock(&nsInitMutex);
208 NSMessage * newMsg = NSDuplicateMessage(msg);
209 NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_NOTIFICATION, newMsg);
211 pthread_mutex_unlock(&nsInitMutex);
213 NS_LOG(DEBUG, "NSSendNotification - OUT");
217 NSResult NSProviderSendSyncInfo(uint64_t messageId, NSSyncType type)
219 NS_LOG(DEBUG, "NSProviderReadCheck - IN");
220 pthread_mutex_lock(&nsInitMutex);
222 NSSyncInfo * syncInfo = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
223 OICStrcpy(syncInfo->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);
224 syncInfo->messageId = messageId;
225 syncInfo->state = type;
226 NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_READ, syncInfo);
228 pthread_mutex_unlock(&nsInitMutex);
229 NS_LOG(DEBUG, "NSProviderReadCheck - OUT");
233 NSResult NSAcceptSubscription(const char * consumerId, bool accepted)
235 NS_LOG(DEBUG, "NSAccept - IN");
236 pthread_mutex_lock(&nsInitMutex);
240 NS_LOG(ERROR, "consumerId is NULL");
241 pthread_mutex_unlock(&nsInitMutex);
245 char * newConsumerId = OICStrdup(consumerId);
248 NS_LOG(DEBUG, "accepted is true - ALLOW");
249 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_ALLOW, newConsumerId);
253 NS_LOG(DEBUG, "accepted is false - DENY");
254 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_DENY, newConsumerId);
257 pthread_mutex_unlock(&nsInitMutex);
258 NS_LOG(DEBUG, "NSAccept - OUT");
262 NSMessage * NSCreateMessage()
264 NS_LOG(DEBUG, "NSCreateMessage - IN");
265 pthread_mutex_lock(&nsInitMutex);
267 NSMessage * msg = NSInitializeMessage();
269 OICStrcpy(msg->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);
271 pthread_mutex_unlock(&nsInitMutex);
272 NS_LOG(DEBUG, "NSCreateMessage - OUT");
276 NSTopicLL * NSProviderGetConsumerTopics(const char * consumerId)
278 NS_LOG(DEBUG, "NSProviderGetConsumerTopics - IN");
279 pthread_mutex_lock(&nsInitMutex);
281 if(!consumerId || consumerId[0] == '\0')
283 NS_LOG(DEBUG, "consumer id should be set");
284 pthread_mutex_unlock(&nsInitMutex);
288 NSTopicSynchronization topics;
289 topics.consumerId = OICStrdup(consumerId);
290 topics.topics = NULL;
291 topics.condition = nstopicCond;
293 NSPushQueue(TOPIC_SCHEDULER, TAST_GET_CONSUMER_TOPICS, &topics);
294 pthread_cond_wait(&topics.condition, &nsInitMutex);
295 OICFree(topics.consumerId);
297 pthread_mutex_unlock(&nsInitMutex);
298 NS_LOG(DEBUG, "NSProviderGetConsumerTopics - OUT");
299 return topics.topics;
302 NSTopicLL * NSProviderGetTopics()
304 NS_LOG(DEBUG, "NSProviderGetTopics - IN");
305 pthread_mutex_lock(&nsInitMutex);
307 NSTopicSynchronization topics;
308 topics.consumerId = NULL;
309 topics.topics = NULL;
310 topics.condition = nstopicCond;
312 NSPushQueue(TOPIC_SCHEDULER, TASK_GET_TOPICS, &topics);
313 pthread_cond_wait(&topics.condition, &nsInitMutex);
315 pthread_mutex_unlock(&nsInitMutex);
316 NS_LOG(DEBUG, "NSProviderGetTopics - OUT");
317 return topics.topics;
320 NSResult NSProviderRegisterTopic(const char * topicName)
322 NS_LOG(DEBUG, "NSProviderAddTopics - IN");
323 pthread_mutex_lock(&nsInitMutex);
325 if(!topicName || topicName == '\0')
327 pthread_mutex_unlock(&nsInitMutex);
328 NS_LOG(DEBUG, "topic Name should be set");
332 NSPushQueue(TOPIC_SCHEDULER, TASK_ADD_TOPIC, OICStrdup(topicName));
334 pthread_mutex_unlock(&nsInitMutex);
335 NS_LOG(DEBUG, "NSProviderAddTopics - OUT");
339 NSResult NSProviderUnregisterTopic(const char * topicName)
341 NS_LOG(DEBUG, "NSProviderDeleteTopics - IN");
342 pthread_mutex_lock(&nsInitMutex);
344 if(!topicName || topicName[0] == '\0')
346 pthread_mutex_unlock(&nsInitMutex);
347 NS_LOG(DEBUG, "topic Name should be set");
351 NSPushQueue(TOPIC_SCHEDULER, TASK_DELETE_TOPIC, (void *) topicName);
353 pthread_mutex_unlock(&nsInitMutex);
354 NS_LOG(DEBUG, "NSProviderDeleteTopics - OUT");
358 NSResult NSProviderSetConsumerTopic(const char * consumerId, const char * topicName)
360 NS_LOG(DEBUG, "NSProviderSelectTopics - IN");
361 pthread_mutex_lock(&nsInitMutex);
363 if(!consumerId || consumerId[0] == '\0' || !topicName || topicName[0] == '\0' || !NSGetPolicy())
365 NS_LOG(DEBUG, "consumer id should be set for topic subscription or "
366 "Configuration must set to true.");
367 pthread_mutex_unlock(&nsInitMutex);
371 NSCacheTopicSubData * topicSubData =
372 (NSCacheTopicSubData *) OICMalloc(sizeof(NSCacheTopicSubData));
374 OICStrcpy(topicSubData->id, NS_UUID_STRING_SIZE, consumerId);
375 topicSubData->topicName = OICStrdup(topicName);
377 NSPushQueue(TOPIC_SCHEDULER, TASK_SUBSCRIBE_TOPIC, (void *)topicSubData);
379 pthread_mutex_unlock(&nsInitMutex);
380 NS_LOG(DEBUG, "NSProviderSelectTopics - OUT");
384 NSResult NSProviderUnsetConsumerTopic(const char * consumerId, const char * topicName)
386 NS_LOG(DEBUG, "NSProviderUnselectTopics - IN");
387 pthread_mutex_lock(&nsInitMutex);
389 if(!consumerId || consumerId[0] == '\0' || !topicName || topicName[0] == '\0' || !NSGetPolicy())
391 NS_LOG(DEBUG, "consumer id should be set for topic subscription or "
392 "Configuration must set to true.");
393 pthread_mutex_unlock(&nsInitMutex);
397 NSCacheTopicSubData * topicSubData =
398 (NSCacheTopicSubData *) OICMalloc(sizeof(NSCacheTopicSubData));
400 OICStrcpy(topicSubData->id, NS_UUID_STRING_SIZE, consumerId);
401 topicSubData->topicName = OICStrdup(topicName);
403 NSPushQueue(TOPIC_SCHEDULER, TASK_UNSUBSCRIBE_TOPIC, (void *)topicSubData);
405 pthread_mutex_unlock(&nsInitMutex);
406 NS_LOG(DEBUG, "NSProviderUnselectTopics - OUT");