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");
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 CAUnregisterNetworkMonitorHandler((CAAdapterStateChangedCB)NSProviderAdapterStateListener,
128 (CAConnectionStateChangedCB)NSProviderConnectionStateListener);
129 NSPushQueue(DISCOVERY_SCHEDULER, TASK_STOP_PRESENCE, NULL);
130 NSRegisterSubscribeRequestCb((NSSubscribeRequestCallback)NULL);
131 NSRegisterSyncCb((NSProviderSyncInfoCallback)NULL);
132 NSUnRegisterResource();
133 NSDeinitProviderInfo();
137 initProvider = false;
140 pthread_mutex_unlock(&nsInitMutex);
141 NS_LOG(DEBUG, "NSStopProvider - OUT");
145 NSResult NSProviderEnableRemoteService(char *serverAddress)
147 #if (defined WITH_CLOUD)
148 NS_LOG(DEBUG, "NSProviderEnableRemoteService - IN");
149 pthread_mutex_lock(&nsInitMutex);
151 if (!initProvider || !serverAddress)
153 NS_LOG(DEBUG, "Provider service has not been started yet");
154 pthread_mutex_unlock(&nsInitMutex);
158 NS_LOG_V(INFO_PRIVATE, "Remote server address: %s", serverAddress);
159 NS_LOG(DEBUG, "Request to publish resource");
160 NSPushQueue(DISCOVERY_SCHEDULER, TASK_PUBLISH_RESOURCE, serverAddress);
162 pthread_mutex_unlock(&nsInitMutex);
163 NS_LOG(DEBUG, "NSProviderEnableRemoteService - OUT");
166 (void) serverAddress;
168 NS_LOG_V(INFO_PRIVATE, "Not logged in remote server: %s", serverAddress);
172 NSResult NSProviderDisableRemoteService(char *serverAddress)
174 #if (defined WITH_CLOUD)
175 NS_LOG(DEBUG, "NSProviderDisableRemoteService - IN");
176 pthread_mutex_lock(&nsInitMutex);
178 if (!initProvider || !serverAddress)
180 NS_LOG(DEBUG, "Provider service has not been started yet");
181 pthread_mutex_unlock(&nsInitMutex);
185 NS_LOG_V(INFO_PRIVATE, "Remote server address: %s", serverAddress);
187 NS_LOG(DEBUG, "Delete remote server info");
188 NSDeleteRemoteServerAddress(serverAddress);
190 pthread_mutex_unlock(&nsInitMutex);
191 NS_LOG(DEBUG, "NSProviderDisableRemoteService - OUT");
194 (void) serverAddress;
196 NS_LOG_V(INFO_PRIVATE, "Not logged in remote server : %s", serverAddress);
201 NSResult NSProviderSubscribeMQService(const char * serverAddress, const char * topicName)
203 NS_LOG(DEBUG, "NSProviderSubscribeMQService - IN");
204 pthread_mutex_lock(&nsInitMutex);
206 if (!initProvider || !serverAddress || !topicName)
208 NS_LOG(DEBUG, "Provider service has not been started yet or set the server "
209 "address and topicName");
210 pthread_mutex_unlock(&nsInitMutex);
214 NSMQTopicAddress * topicAddr = (NSMQTopicAddress *)OICMalloc(sizeof(NSMQTopicAddress));
218 NS_LOG(DEBUG, "fail to memory allocate");
219 pthread_mutex_unlock(&nsInitMutex);
223 topicAddr->serverAddr = NSGetQueryAddress(serverAddress);
224 topicAddr->topicName = OICStrdup(topicName);
226 NS_LOG_V(DEBUG, "input Topic Name : %s", topicAddr->topicName);
228 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_MQ_REQ_SUBSCRIBE, (void *) topicAddr);
230 pthread_mutex_unlock(&nsInitMutex);
231 NS_LOG(DEBUG, "NSProviderSubscribeMQService - OUT");
236 NSResult NSSendMessage(NSMessage * msg)
238 NS_LOG(DEBUG, "NSSendNotification - IN");
240 pthread_mutex_lock(&nsInitMutex);
242 if (!initProvider || msg == NULL)
244 NS_LOG(ERROR, "Msg is NULL");
245 pthread_mutex_unlock(&nsInitMutex);
249 NSMessage * newMsg = NSDuplicateMessage(msg);
250 NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_NOTIFICATION, newMsg);
252 pthread_mutex_unlock(&nsInitMutex);
254 NS_LOG(DEBUG, "NSSendNotification - OUT");
258 NSResult NSProviderSendSyncInfo(uint64_t messageId, NSSyncType type)
260 NS_LOG(DEBUG, "NSProviderReadCheck - IN");
261 pthread_mutex_lock(&nsInitMutex);
263 NSSyncInfo * syncInfo = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
265 if (!initProvider || !syncInfo)
267 NS_LOG(ERROR, "Provider is not started");
269 pthread_mutex_unlock(&nsInitMutex);
273 OICStrcpy(syncInfo->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);
274 syncInfo->messageId = messageId;
275 syncInfo->state = type;
276 NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_READ, syncInfo);
278 pthread_mutex_unlock(&nsInitMutex);
279 NS_LOG(DEBUG, "NSProviderReadCheck - OUT");
283 NSResult NSAcceptSubscription(const char * consumerId, bool accepted)
285 NS_LOG(DEBUG, "NSAccept - IN");
286 pthread_mutex_lock(&nsInitMutex);
288 if(!initProvider || !consumerId || consumerId[0] == '\0' || NSGetPolicy() == NS_POLICY_CONSUMER)
290 NS_LOG(ERROR, "Provider is not started or consumerId is NULL or NS Policy is Consumer");
291 pthread_mutex_unlock(&nsInitMutex);
295 char * newConsumerId = OICStrdup(consumerId);
298 NS_LOG(DEBUG, "accepted is true - ALLOW");
299 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_ALLOW, newConsumerId);
303 NS_LOG(DEBUG, "accepted is false - DENY");
304 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_DENY, newConsumerId);
307 pthread_mutex_unlock(&nsInitMutex);
308 NS_LOG(DEBUG, "NSAccept - OUT");
312 NSMessage * NSCreateMessage()
314 NS_LOG(DEBUG, "NSCreateMessage - IN");
315 pthread_mutex_lock(&nsInitMutex);
317 NSMessage * msg = NSInitializeMessage();
319 OICStrcpy(msg->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);
321 pthread_mutex_unlock(&nsInitMutex);
322 NS_LOG(DEBUG, "NSCreateMessage - OUT");
326 NSTopicLL * NSProviderGetConsumerTopics(const char * consumerId)
328 NS_LOG(DEBUG, "NSProviderGetConsumerTopics - IN");
329 pthread_mutex_lock(&nsInitMutex);
331 if(!initProvider || !consumerId || consumerId[0] == '\0')
333 NS_LOG(DEBUG, "Provider is not started or consumer id should be set");
334 pthread_mutex_unlock(&nsInitMutex);
338 NSTopicSync topicSync;
339 topicSync.consumerId = OICStrdup(consumerId);
340 topicSync.topics = NULL;
341 topicSync.condition = &nstopicCond;
342 topicSync.mutex = &nsInitMutex;
344 NSPushQueue(TOPIC_SCHEDULER, TAST_GET_CONSUMER_TOPICS, &topicSync);
345 pthread_cond_wait(topicSync.condition, &nsInitMutex);
346 NSOICFree(topicSync.consumerId);
348 pthread_mutex_unlock(&nsInitMutex);
349 NS_LOG(DEBUG, "NSProviderGetConsumerTopics - OUT");
350 return topicSync.topics;
353 NSTopicLL * NSProviderGetTopics()
355 NS_LOG(DEBUG, "NSProviderGetTopics - IN");
356 pthread_mutex_lock(&nsInitMutex);
360 NS_LOG(ERROR, "Provider is not started");
361 pthread_mutex_unlock(&nsInitMutex);
365 NSTopicSync topicSync;
366 topicSync.consumerId = NULL;
367 topicSync.topics = NULL;
368 topicSync.condition = &nstopicCond;
369 topicSync.mutex = &nsInitMutex;
371 NSPushQueue(TOPIC_SCHEDULER, TASK_GET_TOPICS, &topicSync);
372 pthread_cond_wait(topicSync.condition, &nsInitMutex);
374 pthread_mutex_unlock(&nsInitMutex);
375 NS_LOG(DEBUG, "NSProviderGetTopics - OUT");
376 return topicSync.topics;
379 NSResult NSProviderRegisterTopic(const char * topicName)
381 NS_LOG(DEBUG, "NSProviderAddTopics - IN");
382 pthread_mutex_lock(&nsInitMutex);
384 if(!initProvider || !topicName || topicName[0] == '\0')
386 pthread_mutex_unlock(&nsInitMutex);
387 NS_LOG(DEBUG, "Provider is not started or topic Name should be set");
391 NSTopicSyncResult topicSyncResult;
392 topicSyncResult.topicData = (void *) OICStrdup(topicName);
393 topicSyncResult.condition = &nstopicCond;
394 topicSyncResult.result = NS_OK;
395 topicSyncResult.mutex = &nsInitMutex;
397 NSPushQueue(TOPIC_SCHEDULER, TASK_REGISTER_TOPIC, &topicSyncResult);
398 pthread_cond_wait(topicSyncResult.condition, &nsInitMutex);
399 if(topicSyncResult.result != NS_OK)
401 pthread_mutex_unlock(&nsInitMutex);
405 pthread_mutex_unlock(&nsInitMutex);
406 NS_LOG(DEBUG, "NSProviderAddTopics - OUT");
410 NSResult NSProviderUnregisterTopic(const char * topicName)
412 NS_LOG(DEBUG, "NSProviderDeleteTopics - IN");
413 pthread_mutex_lock(&nsInitMutex);
415 if(!initProvider || !topicName || topicName[0] == '\0')
417 pthread_mutex_unlock(&nsInitMutex);
418 NS_LOG(DEBUG, "Provider is not started or topic Name should be set");
422 NSTopicSyncResult topicSyncResult;
423 topicSyncResult.topicData = (void *) OICStrdup(topicName);
424 topicSyncResult.condition = &nstopicCond;
425 topicSyncResult.result = NS_OK;
426 topicSyncResult.mutex = &nsInitMutex;
428 NSPushQueue(TOPIC_SCHEDULER, TASK_UNREGISTER_TOPIC, &topicSyncResult);
429 pthread_cond_wait(topicSyncResult.condition, &nsInitMutex);
430 if (topicSyncResult.result != NS_OK)
432 pthread_mutex_unlock(&nsInitMutex);
435 NSOICFree(topicSyncResult.topicData);
437 pthread_mutex_unlock(&nsInitMutex);
438 NS_LOG(DEBUG, "NSProviderDeleteTopics - OUT");
442 NSResult NSProviderSetConsumerTopic(const char * consumerId, const char * topicName)
444 NS_LOG(DEBUG, "NSProviderSelectTopics - IN");
445 pthread_mutex_lock(&nsInitMutex);
447 NSCacheTopicSubData * topicSubData =
448 (NSCacheTopicSubData *) OICMalloc(sizeof(NSCacheTopicSubData));
450 if (!initProvider || !consumerId || consumerId[0] == '\0' || !topicName || topicName[0] == '\0'
451 || !NSGetPolicy() || !topicSubData)
453 NS_LOG(DEBUG, "provider is not started or "
454 "consumer id should be set for topic subscription or "
455 "Configuration must set to true.");
456 NSOICFree(topicSubData);
458 pthread_mutex_unlock(&nsInitMutex);
462 OICStrcpy(topicSubData->id, NS_UUID_STRING_SIZE, consumerId);
463 topicSubData->topicName = OICStrdup(topicName);
465 NSTopicSyncResult topicSyncResult;
466 topicSyncResult.topicData = (void *) topicSubData;
467 topicSyncResult.condition = &nstopicCond;
468 topicSyncResult.result = NS_FAIL;
469 topicSyncResult.mutex = &nsInitMutex;
471 NSPushQueue(TOPIC_SCHEDULER, TASK_SUBSCRIBE_TOPIC, (void *)&topicSyncResult);
472 pthread_cond_wait(topicSyncResult.condition, &nsInitMutex);
474 pthread_mutex_unlock(&nsInitMutex);
475 NS_LOG(DEBUG, "NSProviderSelectTopics - OUT");
476 return topicSyncResult.result;
479 NSResult NSProviderUnsetConsumerTopic(const char * consumerId, const char * topicName)
481 NS_LOG(DEBUG, "NSProviderUnselectTopics - IN");
482 pthread_mutex_lock(&nsInitMutex);
484 NSCacheTopicSubData * topicSubData =
485 (NSCacheTopicSubData *) OICMalloc(sizeof(NSCacheTopicSubData));
487 if (!initProvider || !consumerId || consumerId[0] == '\0' || !topicName || topicName[0] == '\0'
488 || !NSGetPolicy() || !topicSubData)
490 NS_LOG(DEBUG, "provider is not started or "
491 "consumer id should be set for topic subscription or "
492 "Configuration must set to true.");
493 NSOICFree(topicSubData);
495 pthread_mutex_unlock(&nsInitMutex);
499 OICStrcpy(topicSubData->id, NS_UUID_STRING_SIZE, consumerId);
500 topicSubData->topicName = OICStrdup(topicName);
502 NSTopicSyncResult topicSyncResult;
503 topicSyncResult.topicData = (void *) topicSubData;
504 topicSyncResult.condition = &nstopicCond;
505 topicSyncResult.result = NS_FAIL;
506 topicSyncResult.mutex = &nsInitMutex;
508 NSPushQueue(TOPIC_SCHEDULER, TASK_UNSUBSCRIBE_TOPIC, (void *)&topicSyncResult);
509 pthread_cond_wait(topicSyncResult.condition, &nsInitMutex);
511 pthread_mutex_unlock(&nsInitMutex);
512 NS_LOG(DEBUG, "NSProviderUnselectTopics - OUT");
513 return topicSyncResult.result;