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 "NSProviderMemoryCache.h"
28 #include "NSProviderTopic.h"
29 #include "oic_malloc.h"
30 #include "oic_string.h"
31 #include "cautilinterface.h"
32 #include "NSProviderSystem.h"
36 bool initProvider = false;
38 pthread_mutex_t nsInitMutex;
39 pthread_cond_t nstopicCond;
41 void initializeMutex()
43 static pthread_mutex_t initMutex = PTHREAD_MUTEX_INITIALIZER;
44 nsInitMutex = initMutex;
49 NS_LOG(DEBUG, "NSSetList - IN");
51 pthread_mutexattr_init(&NSCacheMutexAttr);
52 pthread_mutexattr_settype(&NSCacheMutexAttr, PTHREAD_MUTEX_RECURSIVE);
53 pthread_mutex_init(&NSCacheMutex, &NSCacheMutexAttr);
54 pthread_cond_init(&nstopicCond, NULL);
56 NSInitSubscriptionList();
58 NS_LOG(DEBUG, "NSSetList - OUT");
63 NSProviderStorageDestroy(consumerSubList);
64 NSProviderStorageDestroy(consumerTopicList);
65 NSProviderStorageDestroy(registeredTopicList);
67 pthread_mutex_destroy(&NSCacheMutex);
68 pthread_mutexattr_destroy(&NSCacheMutexAttr);
69 pthread_cond_destroy(&nstopicCond);
72 NSResult NSStartProvider(NSProviderConfig config)
74 NS_LOG(DEBUG, "NSStartProvider - IN");
78 pthread_mutex_lock(&nsInitMutex);
82 NS_LOG(DEBUG, "Init Provider");
84 NSInitProviderInfo(config.userInfo);
85 NSSetSubscriptionAccessPolicy(config.subControllability);
86 NSRegisterSubscribeRequestCb(config.subRequestCallback);
87 NSRegisterSyncCb(config.syncInfoCallback);
88 CARegisterNetworkMonitorHandler((CAAdapterStateChangedCB)NSProviderAdapterStateListener,
89 (CAConnectionStateChangedCB)NSProviderConnectionStateListener);
91 NS_LOG(DEBUG, "Check secured build option..");
93 NS_LOG(DEBUG, "Built with SECURED");
95 if(!config.resourceSecurity)
97 NS_LOG(DEBUG, "Resource Security Off");
99 NSSetResourceSecurity(config.resourceSecurity);
106 NSPushQueue(DISCOVERY_SCHEDULER, TASK_START_PRESENCE, NULL);
107 NSPushQueue(DISCOVERY_SCHEDULER, TASK_REGISTER_RESOURCE, NULL);
111 NS_LOG(DEBUG, "Already started Notification Provider");
113 pthread_mutex_unlock(&nsInitMutex);
115 NS_LOG(DEBUG, "NSStartProvider - OUT");
119 NSResult NSStopProvider()
121 NS_LOG(DEBUG, "NSStopProvider - IN");
122 pthread_mutex_lock(&nsInitMutex);
126 NSPushQueue(DISCOVERY_SCHEDULER, TASK_STOP_PRESENCE, NULL);
127 NSDeinitProviderInfo();
128 NSUnRegisterResource();
129 NSRegisterSubscribeRequestCb((NSSubscribeRequestCallback)NULL);
130 NSRegisterSyncCb((NSProviderSyncInfoCallback)NULL);
134 initProvider = false;
137 pthread_mutex_unlock(&nsInitMutex);
138 NS_LOG(DEBUG, "NSStopProvider - OUT");
142 NSResult NSProviderEnableRemoteService(char *serverAddress)
144 #if(defined WITH_CLOUD && defined RD_CLIENT)
145 NS_LOG(DEBUG, "NSProviderEnableRemoteService - IN");
146 pthread_mutex_lock(&nsInitMutex);
148 if(!initProvider || !serverAddress)
150 NS_LOG(DEBUG, "Provider service has not been started yet");
151 pthread_mutex_unlock(&nsInitMutex);
154 NS_LOG_V(DEBUG, "Remote server address: %s", serverAddress);
155 NS_LOG(DEBUG, "Request to publish resource");
156 NSPushQueue(DISCOVERY_SCHEDULER, TASK_PUBLISH_RESOURCE, serverAddress);
158 pthread_mutex_unlock(&nsInitMutex);
159 NS_LOG(DEBUG, "NSProviderEnableRemoteService - OUT");
162 (void) serverAddress;
164 NS_LOG_V(DEBUG, "Not logged in remote server: %s", serverAddress);
168 NSResult NSProviderDisableRemoteService(char *serverAddress)
170 #if(defined WITH_CLOUD && defined RD_CLIENT)
171 NS_LOG(DEBUG, "NSProviderDisableRemoteService - IN");
172 pthread_mutex_lock(&nsInitMutex);
174 if(!initProvider || !serverAddress)
176 NS_LOG(DEBUG, "Provider service has not been started yet");
179 NS_LOG_V(DEBUG, "Remote server address: %s", serverAddress);
181 NS_LOG(DEBUG, "Delete remote server info");
182 NSDeleteRemoteServerAddress(serverAddress);
184 pthread_mutex_unlock(&nsInitMutex);
185 NS_LOG(DEBUG, "NSProviderDisableRemoteService - OUT");
188 (void) serverAddress;
190 NS_LOG_V(DEBUG, "Not logged in remote server : %s", serverAddress);
194 NSResult NSSendMessage(NSMessage * msg)
196 NS_LOG(DEBUG, "NSSendNotification - IN");
198 pthread_mutex_lock(&nsInitMutex);
200 if (!initProvider || msg == NULL)
202 NS_LOG(ERROR, "Msg is NULL");
203 pthread_mutex_unlock(&nsInitMutex);
207 NSMessage * newMsg = NSDuplicateMessage(msg);
208 NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_NOTIFICATION, newMsg);
210 pthread_mutex_unlock(&nsInitMutex);
212 NS_LOG(DEBUG, "NSSendNotification - OUT");
216 NSResult NSProviderSendSyncInfo(uint64_t messageId, NSSyncType type)
218 NS_LOG(DEBUG, "NSProviderReadCheck - IN");
219 pthread_mutex_lock(&nsInitMutex);
221 NSSyncInfo * syncInfo = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
223 if (!initProvider || !syncInfo)
225 NS_LOG(ERROR, "Provider is not started");
226 pthread_mutex_unlock(&nsInitMutex);
230 OICStrcpy(syncInfo->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);
231 syncInfo->messageId = messageId;
232 syncInfo->state = type;
233 NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_READ, syncInfo);
235 pthread_mutex_unlock(&nsInitMutex);
236 NS_LOG(DEBUG, "NSProviderReadCheck - OUT");
240 NSResult NSAcceptSubscription(const char * consumerId, bool accepted)
242 NS_LOG(DEBUG, "NSAccept - IN");
243 pthread_mutex_lock(&nsInitMutex);
245 if(!initProvider || !consumerId || consumerId[0] == '\0' || NSGetPolicy() == NS_POLICY_CONSUMER)
247 NS_LOG(ERROR, "Provider is not started or consumerId is NULL or NS Policy is Consumer");
248 pthread_mutex_unlock(&nsInitMutex);
252 char * newConsumerId = OICStrdup(consumerId);
255 NS_LOG(DEBUG, "accepted is true - ALLOW");
256 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_ALLOW, newConsumerId);
260 NS_LOG(DEBUG, "accepted is false - DENY");
261 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_DENY, newConsumerId);
264 pthread_mutex_unlock(&nsInitMutex);
265 NS_LOG(DEBUG, "NSAccept - OUT");
269 NSMessage * NSCreateMessage()
271 NS_LOG(DEBUG, "NSCreateMessage - IN");
272 pthread_mutex_lock(&nsInitMutex);
274 NSMessage * msg = NSInitializeMessage();
276 OICStrcpy(msg->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);
278 pthread_mutex_unlock(&nsInitMutex);
279 NS_LOG(DEBUG, "NSCreateMessage - OUT");
283 NSTopicLL * NSProviderGetConsumerTopics(const char * consumerId)
285 NS_LOG(DEBUG, "NSProviderGetConsumerTopics - IN");
286 pthread_mutex_lock(&nsInitMutex);
288 if(!initProvider || !consumerId || consumerId[0] == '\0')
290 NS_LOG(DEBUG, "Provider is not started or consumer id should be set");
291 pthread_mutex_unlock(&nsInitMutex);
295 NSTopicSync topicSync;
296 topicSync.consumerId = OICStrdup(consumerId);
297 topicSync.topics = NULL;
298 topicSync.condition = &nstopicCond;
299 topicSync.mutex = &nsInitMutex;
301 NSPushQueue(TOPIC_SCHEDULER, TAST_GET_CONSUMER_TOPICS, &topicSync);
302 pthread_cond_wait(topicSync.condition, &nsInitMutex);
303 OICFree(topicSync.consumerId);
305 pthread_mutex_unlock(&nsInitMutex);
306 NS_LOG(DEBUG, "NSProviderGetConsumerTopics - OUT");
307 return topicSync.topics;
310 NSTopicLL * NSProviderGetTopics()
312 NS_LOG(DEBUG, "NSProviderGetTopics - IN");
313 pthread_mutex_lock(&nsInitMutex);
317 NS_LOG(ERROR, "Provider is not started");
318 pthread_mutex_unlock(&nsInitMutex);
322 NSTopicSync topicSync;
323 topicSync.consumerId = NULL;
324 topicSync.topics = NULL;
325 topicSync.condition = &nstopicCond;
326 topicSync.mutex = &nsInitMutex;
328 NSPushQueue(TOPIC_SCHEDULER, TASK_GET_TOPICS, &topicSync);
329 pthread_cond_wait(topicSync.condition, &nsInitMutex);
331 pthread_mutex_unlock(&nsInitMutex);
332 NS_LOG(DEBUG, "NSProviderGetTopics - OUT");
333 return topicSync.topics;
336 NSResult NSProviderRegisterTopic(const char * topicName)
338 NS_LOG(DEBUG, "NSProviderAddTopics - IN");
339 pthread_mutex_lock(&nsInitMutex);
341 if(!initProvider || !topicName || topicName[0] == '\0')
343 pthread_mutex_unlock(&nsInitMutex);
344 NS_LOG(DEBUG, "Provider is not started or topic Name should be set");
348 NSTopicSyncResult topicSyncResult;
349 topicSyncResult.topicData = (void *) OICStrdup(topicName);
350 topicSyncResult.condition = &nstopicCond;
351 topicSyncResult.result = NS_OK;
352 topicSyncResult.mutex = &nsInitMutex;
354 NSPushQueue(TOPIC_SCHEDULER, TASK_REGISTER_TOPIC, &topicSyncResult);
355 pthread_cond_wait(topicSyncResult.condition, &nsInitMutex);
356 if(topicSyncResult.result != NS_OK)
358 pthread_mutex_unlock(&nsInitMutex);
362 pthread_mutex_unlock(&nsInitMutex);
363 NS_LOG(DEBUG, "NSProviderAddTopics - OUT");
367 NSResult NSProviderUnregisterTopic(const char * topicName)
369 NS_LOG(DEBUG, "NSProviderDeleteTopics - IN");
370 pthread_mutex_lock(&nsInitMutex);
372 if(!initProvider || !topicName || topicName[0] == '\0')
374 pthread_mutex_unlock(&nsInitMutex);
375 NS_LOG(DEBUG, "Provider is not started or topic Name should be set");
379 NSTopicSyncResult topicSyncResult;
380 topicSyncResult.topicData = (void *) OICStrdup(topicName);
381 topicSyncResult.condition = &nstopicCond;
382 topicSyncResult.result = NS_OK;
383 topicSyncResult.mutex = &nsInitMutex;
385 NSPushQueue(TOPIC_SCHEDULER, TASK_UNREGISTER_TOPIC, &topicSyncResult);
386 pthread_cond_wait(topicSyncResult.condition, &nsInitMutex);
387 if(topicSyncResult.result != NS_OK)
389 pthread_mutex_unlock(&nsInitMutex);
392 OICFree(topicSyncResult.topicData);
394 pthread_mutex_unlock(&nsInitMutex);
395 NS_LOG(DEBUG, "NSProviderDeleteTopics - OUT");
399 NSResult NSProviderSetConsumerTopic(const char * consumerId, const char * topicName)
401 NS_LOG(DEBUG, "NSProviderSelectTopics - IN");
402 pthread_mutex_lock(&nsInitMutex);
404 NSCacheTopicSubData * topicSubData =
405 (NSCacheTopicSubData *) OICMalloc(sizeof(NSCacheTopicSubData));
407 if(!initProvider || !consumerId || consumerId[0] == '\0' || !topicName || topicName[0] == '\0'
408 || !NSGetPolicy() || !topicSubData)
410 NS_LOG(DEBUG, "provider is not started or "
411 "consumer id should be set for topic subscription or "
412 "Configuration must set to true.");
413 pthread_mutex_unlock(&nsInitMutex);
417 OICStrcpy(topicSubData->id, NS_UUID_STRING_SIZE, consumerId);
418 topicSubData->topicName = OICStrdup(topicName);
420 NSTopicSyncResult topicSyncResult;
421 topicSyncResult.topicData = (void *) topicSubData;
422 topicSyncResult.condition = &nstopicCond;
423 topicSyncResult.result = NS_FAIL;
424 topicSyncResult.mutex = &nsInitMutex;
426 NSPushQueue(TOPIC_SCHEDULER, TASK_SUBSCRIBE_TOPIC, (void *)&topicSyncResult);
427 pthread_cond_wait(topicSyncResult.condition, &nsInitMutex);
429 pthread_mutex_unlock(&nsInitMutex);
430 NS_LOG(DEBUG, "NSProviderSelectTopics - OUT");
431 return topicSyncResult.result;
434 NSResult NSProviderUnsetConsumerTopic(const char * consumerId, const char * topicName)
436 NS_LOG(DEBUG, "NSProviderUnselectTopics - IN");
437 pthread_mutex_lock(&nsInitMutex);
439 NSCacheTopicSubData * topicSubData =
440 (NSCacheTopicSubData *) OICMalloc(sizeof(NSCacheTopicSubData));
442 if(!initProvider || !consumerId || consumerId[0] == '\0' || !topicName || topicName[0] == '\0'
443 || !NSGetPolicy() || !topicSubData)
445 NS_LOG(DEBUG, "provider is not started or "
446 "consumer id should be set for topic subscription or "
447 "Configuration must set to true.");
448 pthread_mutex_unlock(&nsInitMutex);
452 OICStrcpy(topicSubData->id, NS_UUID_STRING_SIZE, consumerId);
453 topicSubData->topicName = OICStrdup(topicName);
455 NSTopicSyncResult topicSyncResult;
456 topicSyncResult.topicData = (void *) topicSubData;
457 topicSyncResult.condition = &nstopicCond;
458 topicSyncResult.result = NS_FAIL;
459 topicSyncResult.mutex = &nsInitMutex;
461 NSPushQueue(TOPIC_SCHEDULER, TASK_UNSUBSCRIBE_TOPIC, (void *)&topicSyncResult);
462 pthread_cond_wait(topicSyncResult.condition, &nsInitMutex);
464 pthread_mutex_unlock(&nsInitMutex);
465 NS_LOG(DEBUG, "NSProviderUnselectTopics - OUT");
466 return topicSyncResult.result;