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 "NSConstants.h"
22 #include "NSConsumerCommon.h"
23 #include "NSConsumerInternalTaskController.h"
24 #include "NSStructs.h"
26 #include "oic_malloc.h"
27 #include "oic_string.h"
29 #define NS_RESERVED_MESSAGEID 10
31 // MessageState storage structure
32 typedef struct _NSMessageStateLL
36 struct _NSMessageStateLL * next;
42 NSMessageStateLL * head;
43 NSMessageStateLL * tail;
47 // Mutex of MessageState storage
48 pthread_mutex_t ** NSGetMessageListMutex();
49 void NSLockMessageListMutex();
50 void NSUnlockMessageListMutex();
52 // Function for MessageState
53 NSMessageStateList * NSGetMessageStateList();
54 NSMessageStateLL * NSFindMessageState(uint64_t msgId);
55 bool NSUpdateMessageState(uint64_t msgId, NSSyncType state);
56 bool NSDeleteMessageState(uint64_t msgId);
57 bool NSInsertMessageState(uint64_t msgId, NSSyncType state);
58 void NSDestroyMessageStateList();
60 NSCacheList ** NSGetProviderCacheList()
62 static NSCacheList * providerCache = NULL;
63 return & providerCache;
66 void NSSetProviderCacheList(NSCacheList * cache)
68 *(NSGetProviderCacheList()) = cache;
71 void NSDestroyInternalCachedList()
73 NSCacheList * cache = *(NSGetProviderCacheList());
76 NSStorageDestroy(cache);
79 NSSetProviderCacheList(NULL);
81 NSDestroyMessageStateList();
82 pthread_mutex_destroy(*NSGetMessageListMutex());
83 *NSGetMessageListMutex() = NULL;
86 NSProvider_internal * NSProviderCacheFind(const char * providerId)
88 NS_VERIFY_NOT_NULL(providerId, NULL);
90 NSCacheList * ProviderCache = *(NSGetProviderCacheList());
93 NS_LOG(DEBUG, "Provider Cache Init");
94 ProviderCache = NSStorageCreate();
95 NS_VERIFY_NOT_NULL(ProviderCache, NULL);
97 ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER;
98 NSSetProviderCacheList(ProviderCache);
101 NSCacheElement * cacheElement = NSStorageRead(ProviderCache, providerId);
102 NS_VERIFY_NOT_NULL(cacheElement, NULL);
104 return NSCopyProvider_internal((NSProvider_internal *) cacheElement->data);
107 NSProvider_internal * NSFindProviderFromAddr(OCDevAddr * addr)
109 NS_VERIFY_NOT_NULL(addr, NULL);
111 NSCacheList * ProviderCache = *(NSGetProviderCacheList());
114 NS_LOG(DEBUG, "Provider Cache does not intialized.");
118 NSCacheElement * cacheElement =
119 NSGetProviderFromAddr(ProviderCache, addr->addr, addr->port);
121 NS_VERIFY_NOT_NULL(cacheElement, NULL);
123 return NSCopyProvider_internal((NSProvider_internal *) cacheElement->data);
126 NSResult NSProviderCacheUpdate(NSProvider_internal * provider)
128 NSCacheList * ProviderCache = *(NSGetProviderCacheList());
131 NS_LOG(DEBUG, "Provider Cache Init");
132 ProviderCache = NSStorageCreate();
133 NS_VERIFY_NOT_NULL(ProviderCache, NS_ERROR);
135 ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER;
136 NSSetProviderCacheList(ProviderCache);
139 NS_VERIFY_NOT_NULL(provider, NS_ERROR);
141 NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
142 NS_VERIFY_NOT_NULL(obj, NS_ERROR);
144 obj->data = (NSCacheData *) provider;
147 NS_LOG(DEBUG, "try to write to storage");
148 NSResult ret = NSStorageWrite(ProviderCache, obj);
149 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
150 NS_ERROR, NSOICFree(obj));
155 void NSCancelAllSubscription()
157 NSCacheList * ProviderCache = *(NSGetProviderCacheList());
160 NS_LOG(DEBUG, "Provider Cache Init");
161 ProviderCache = NSStorageCreate();
162 NS_VERIFY_NOT_NULL_V(ProviderCache);
164 ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER;
165 NSSetProviderCacheList(ProviderCache);
168 NSCacheElement * obj = NULL;
169 while ((obj = NSPopProviderCacheList(ProviderCache)))
171 NS_LOG(DEBUG, "build NSTask");
172 NSProvider * prov = NSCopyProvider((NSProvider_internal *) obj->data);
173 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(prov,
174 NSRemoveProvider_internal((NSProvider_internal *) obj->data));
176 NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, prov);
177 NS_VERIFY_NOT_NULL_V(task);
179 NSConsumerPushEvent(task);
183 void NSConsumerHandleProviderDiscovered(NSProvider_internal * provider)
185 NS_VERIFY_NOT_NULL_V(provider);
188 bool isSubscribing = false;
190 NSProvider_internal * providerCacheDataFromAddr
191 = NSFindProviderFromAddr(provider->connection->addr);
193 if (providerCacheDataFromAddr)
195 if (!strcmp(providerCacheDataFromAddr->providerId, provider->providerId))
197 NSProviderConnectionInfo * infos = providerCacheDataFromAddr->connection;
200 isSubscribing |= infos->isSubscribing;
204 if (isSubscribing == false)
206 NSProvider * providerForCb = NSCopyProvider(providerCacheDataFromAddr);
207 NSProviderChanged(providerForCb, NS_DISCOVERED);
208 NSRemoveProvider(providerForCb);
210 NSRemoveProvider_internal(providerCacheDataFromAddr);
213 NSRemoveProvider_internal(providerCacheDataFromAddr);
216 NSProvider_internal * providerCacheData = NSProviderCacheFind(provider->providerId);
218 if (providerCacheData == NULL)
224 providerCacheData->accessPolicy = provider->accessPolicy;
225 NSProviderConnectionInfo * infos = providerCacheData->connection;
226 OCTransportAdapter newAdapter = provider->connection->addr->adapter;
229 isSubscribing |= infos->isSubscribing;
230 if (infos->addr->adapter == newAdapter && infos->isSubscribing == true)
232 NS_LOG_V(DEBUG, "This provider already discovered : %s:%d",
233 infos->addr->addr, infos->addr->port);
234 NS_LOG_V(DEBUG, "Subscription : %d", infos->isSubscribing);
241 NSResult ret = NSProviderCacheUpdate(provider);
242 NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
244 if (isAdded == false)
246 NS_LOG(DEBUG, "New provider is discovered");
250 provider = providerCacheData;
251 NS_LOG(DEBUG, "provider's connection is updated.");
254 if (provider->accessPolicy == NS_SELECTION_CONSUMER && isSubscribing == false)
256 NS_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user");
257 NSProvider * providerForCb = NSCopyProvider(provider);
258 NSProviderChanged(providerForCb, NS_DISCOVERED);
259 NSRemoveProvider(providerForCb);
263 NS_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe");
264 NSProvider_internal * subProvider = NSCopyProvider_internal(provider);
265 NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) subProvider);
266 NS_VERIFY_NOT_NULL_V(task);
268 NSConsumerPushEvent(task);
271 NSRemoveProvider_internal(providerCacheData);
274 void NSConsumerHandleProviderDeleted(NSProvider_internal * provider)
276 NS_VERIFY_NOT_NULL_V(provider);
278 NSCacheList * providerCache = *(NSGetProviderCacheList());
279 NS_VERIFY_NOT_NULL_V(providerCache);
281 NSResult ret = NSStorageDelete(providerCache, provider->providerId);
282 NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *)1 : NULL);
284 NS_LOG_V(DEBUG, "Stopped Provider : %s", provider->providerId);
285 NSProvider * providerForCb = NSCopyProvider(provider);
286 NSProviderChanged(providerForCb, NS_STOPPED);
289 void NSConsumerHandleSubscribeSucceed(NSProvider_internal * provider)
291 NS_VERIFY_NOT_NULL_V(provider);
293 NSCacheList * ProviderCache = *(NSGetProviderCacheList());
295 NSCacheElement * cacheElement = NSStorageRead(ProviderCache, provider->providerId);
296 NS_VERIFY_NOT_NULL_V(cacheElement);
298 pthread_mutex_t * mutex = NSGetCacheMutex();
299 pthread_mutex_lock(mutex);
301 NS_VERIFY_NOT_NULL_V(cacheElement);
302 NSProvider_internal * prov = (NSProvider_internal *)cacheElement->data;
303 NSProviderConnectionInfo *infos = prov->connection;
306 infos->isSubscribing = true;
310 pthread_mutex_unlock(mutex);
313 void NSConsumerHandleRecvProviderChanged(NSMessage * msg)
315 NS_VERIFY_NOT_NULL_V(msg);
317 NS_LOG_V(DEBUG, "confirmed by : %s", msg->providerId);
319 NSCacheList * ProviderCache = *(NSGetProviderCacheList());
321 NSCacheElement * cacheElement = NSStorageRead(ProviderCache, msg->providerId);
322 NS_VERIFY_NOT_NULL_V(cacheElement);
324 pthread_mutex_t * mutex = NSGetCacheMutex();
325 pthread_mutex_lock(mutex);
326 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(cacheElement, pthread_mutex_unlock(mutex));
327 NSProvider_internal * provider = (NSProvider_internal *) cacheElement->data;
328 if (provider->state == (NSProviderState) msg->messageId)
330 NS_LOG_V(DEBUG, "Already receive message(ALLOW/DENY) : %d", (int) msg->messageId);
331 pthread_mutex_unlock(mutex);
335 NS_LOG_V(DEBUG, "Provider State Changed %d -> %d",
336 (int) provider->state, (int) msg->messageId);
337 NS_LOG(DEBUG, "call back to user");
338 provider->state = (NSProviderState) msg->messageId;
340 NSProvider * prov = NSCopyProvider(provider);
342 pthread_mutex_unlock(mutex);
343 NSProviderChanged(prov, (NSProviderState) msg->messageId);
346 void NSConsumerHandleRecvMessage(NSMessage * msg)
348 NS_VERIFY_NOT_NULL_V(msg);
350 if (NSInsertMessageState(msg->messageId, NS_SYNC_UNREAD))
356 void NSConsumerHandleRecvSyncInfo(NSSyncInfo * sync)
358 NS_VERIFY_NOT_NULL_V(sync);
360 if (NSUpdateMessageState(sync->messageId, sync->state))
362 NSNotificationSync(sync);
365 if (sync->state == NS_SYNC_DELETED)
367 NSDeleteMessageState(sync->messageId);
371 void NSConsumerHandleMakeSyncInfo(NSSyncInfo * sync)
373 NS_VERIFY_NOT_NULL_V(sync);
375 NSProvider_internal * provider = NSProviderCacheFind(sync->providerId);
376 NS_VERIFY_NOT_NULL_V (provider);
378 NSProviderConnectionInfo * connections = NSCopyProviderConnections(provider->connection);
379 NSRemoveProvider_internal(provider);
380 NS_VERIFY_NOT_NULL_V (connections);
382 NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)OICMalloc(sizeof(NSSyncInfo_internal));
383 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(syncInfo, NSRemoveConnections(connections));
385 OICStrcpy(syncInfo->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, sync->providerId);
386 syncInfo->messageId = sync->messageId;
387 syncInfo->state = sync->state;
388 syncInfo->connection = connections;
390 NSTask * syncTask = NSMakeTask(TASK_SEND_SYNCINFO, (void *) syncInfo);
391 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(syncTask, NSOICFree(syncInfo));
393 NSConsumerPushEvent(syncTask);
396 void NSConsumerHandleGetTopicUri(NSMessage * msg)
398 NS_VERIFY_NOT_NULL_V(msg);
400 NSProvider_internal * provider = NSProviderCacheFind(msg->providerId);
401 NS_VERIFY_NOT_NULL_V(provider);
403 NSTask * topicTask = NSMakeTask(TASK_CONSUMER_REQ_TOPIC_LIST, (void *) provider);
404 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(topicTask, NSRemoveProvider_internal(provider));
406 NSConsumerPushEvent(topicTask);
409 void NSConsumerHandleRecvTopicLL(NSProvider_internal * provider)
411 NS_VERIFY_NOT_NULL_V(provider);
413 NSRemoveConnections(provider->connection);
414 provider->connection = NULL;
416 NSProvider_internal * cachedProvider = NSProviderCacheFind(provider->providerId);
417 NS_VERIFY_NOT_NULL_V(cachedProvider);
419 if (!cachedProvider->topicLL && !provider->topicLL)
421 NS_LOG(DEBUG, "topic is null and previous status is same.");
422 NSRemoveProvider_internal(cachedProvider);
425 NSRemoveProvider_internal(cachedProvider);
427 NSResult ret = NSProviderCacheUpdate(provider);
428 NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
430 NS_LOG(DEBUG, "call back to user");
431 NSProvider * prov = NSCopyProvider(provider);
432 NSProviderChanged((NSProvider *) prov, (NSProviderState) NS_TOPIC);
435 void NSConsumerInternalTaskProcessing(NSTask * task)
437 NS_VERIFY_NOT_NULL_V(task);
439 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
440 switch (task->taskType)
442 case TASK_CONSUMER_SENT_REQ_OBSERVE:
444 NS_LOG(DEBUG, "Receive Subscribe succeed from provider.");
445 NSConsumerHandleSubscribeSucceed((NSProvider_internal *)task->taskData);
446 NSRemoveProvider_internal((NSProvider_internal *)task->taskData);
449 case TASK_CONSUMER_RECV_PROVIDER_CHANGED:
451 NS_LOG(DEBUG, "Receive Provider Changed");
452 NSConsumerHandleRecvProviderChanged((NSMessage *)task->taskData);
453 NSRemoveMessage((NSMessage *)task->taskData);
456 case TASK_CONSUMER_RECV_MESSAGE:
458 NS_LOG(DEBUG, "Receive New Notification");
459 NSConsumerHandleRecvMessage((NSMessage *)task->taskData);
460 NSRemoveMessage((NSMessage *)task->taskData);
463 case TASK_CONSUMER_PROVIDER_DISCOVERED:
465 NS_LOG(DEBUG, "Receive New Provider is discovered.");
466 NSConsumerHandleProviderDiscovered((NSProvider_internal *)task->taskData);
467 NSRemoveProvider_internal((NSProvider_internal *)task->taskData);
470 case TASK_RECV_SYNCINFO:
472 NS_LOG(DEBUG, "Receive SyncInfo.");
473 NSConsumerHandleRecvSyncInfo((NSSyncInfo *)task->taskData);
474 NSOICFree(task->taskData);
477 case TASK_MAKE_SYNCINFO:
479 NS_LOG(DEBUG, "Make SyncInfo, get Provider's Addr");
480 NSConsumerHandleMakeSyncInfo((NSSyncInfo *)task->taskData);
481 NSOICFree(task->taskData);
484 case TASK_CONSUMER_REQ_TOPIC_URI:
486 NS_LOG(DEBUG, "Request Topic Uri");
487 NSConsumerHandleGetTopicUri((NSMessage *)task->taskData);
488 NSRemoveMessage((NSMessage *)task->taskData);
491 case TASK_CONSUMER_RECV_TOPIC_LIST:
493 NS_LOG(DEBUG, "Receive Topic List");
494 NSConsumerHandleRecvTopicLL((NSProvider_internal *)task->taskData);
495 NSRemoveProvider_internal((NSProvider_internal *)task->taskData);
498 case TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL:
500 NS_LOG(DEBUG, "Make Subscribe cancel from provider.");
501 NSConsumerHandleProviderDeleted((NSProvider_internal *)task->taskData);
502 NSRemoveProvider_internal((NSProvider_internal *)task->taskData);
507 NS_LOG(ERROR, "Unknown TASK Type");
514 // implements of MessageState function
515 pthread_mutex_t ** NSGetMessageListMutex()
517 static pthread_mutex_t * g_mutex = NULL;
520 g_mutex = (pthread_mutex_t *) OICMalloc(sizeof(pthread_mutex_t));
521 NS_VERIFY_NOT_NULL(g_mutex, NULL);
523 pthread_mutex_init(g_mutex, NULL);
528 void NSLockMessageListMutex()
530 NS_LOG_V(DEBUG, "%s", __func__);
531 pthread_mutex_lock(*NSGetMessageListMutex());
534 void NSUnlockMessageListMutex()
536 NS_LOG_V(DEBUG, "%s", __func__);
537 pthread_mutex_unlock(*NSGetMessageListMutex());
540 NSMessageStateList * NSGetMessageStateList()
542 static NSMessageStateList * g_messageStateList = NULL;
543 if (g_messageStateList == NULL)
545 g_messageStateList = (NSMessageStateList *)OICMalloc(sizeof(NSMessageStateList));
546 NS_VERIFY_NOT_NULL(g_messageStateList, NULL);
548 g_messageStateList->head = NULL;
549 g_messageStateList->tail = NULL;
552 return g_messageStateList;
555 NSMessageStateLL * NSFindMessageState(uint64_t msgId)
557 NS_LOG_V(DEBUG, "%s", __func__);
558 if (msgId <= NS_RESERVED_MESSAGEID) return NULL;
559 NSMessageStateLL * iter = NULL;
561 NSLockMessageListMutex();
562 if (NSGetMessageStateList()->head == NULL)
564 NSUnlockMessageListMutex();
568 for (iter = NSGetMessageStateList()->head; iter; iter = iter->next)
570 if (iter->messageId == msgId)
572 NSUnlockMessageListMutex();
577 NSUnlockMessageListMutex();
581 bool NSUpdateMessageState(uint64_t msgId, NSSyncType state)
583 NS_LOG_V(DEBUG, "%s", __func__);
584 if (msgId <= NS_RESERVED_MESSAGEID) return NULL;
585 NSMessageStateLL * iter = NULL;
587 NSLockMessageListMutex();
588 for (iter = NSGetMessageStateList()->head; iter; iter = iter->next)
590 if (iter->messageId == msgId && state != iter->state)
593 NSUnlockMessageListMutex();
598 NSUnlockMessageListMutex();
602 bool NSDeleteMessageState(uint64_t msgId)
604 NS_LOG_V(DEBUG, "%s", __func__);
605 if (msgId <= NS_RESERVED_MESSAGEID) return NULL;
606 NSMessageStateLL * iter = NULL;
607 NSMessageStateLL * prev = NULL;
609 NSLockMessageListMutex();
610 for (iter = NSGetMessageStateList()->head; iter; iter = iter->next)
612 if (iter->messageId == msgId)
614 if (iter == NSGetMessageStateList()->head)
616 NSGetMessageStateList()->head = NULL;
617 NSGetMessageStateList()->tail = NULL;
619 else if (iter == NSGetMessageStateList()->tail)
622 NSGetMessageStateList()->tail = prev;
626 prev->next = iter->next;
628 NSUnlockMessageListMutex();
636 NSUnlockMessageListMutex();
640 bool NSInsertMessageState(uint64_t msgId, NSSyncType state)
642 NS_LOG_V(DEBUG, "%s", __func__);
643 if (NSFindMessageState(msgId))
648 NSMessageStateLL * insertMsg = (NSMessageStateLL * )OICMalloc(sizeof(NSMessageStateLL));
649 NS_VERIFY_NOT_NULL(insertMsg, false);
651 insertMsg->messageId = msgId;
652 insertMsg->state = state;
653 insertMsg->next = NULL;
655 NSLockMessageListMutex();
656 if (NSGetMessageStateList()->head == NULL)
658 NSGetMessageStateList()->head = insertMsg;
662 NSGetMessageStateList()->tail->next = insertMsg;
664 NSGetMessageStateList()->tail = insertMsg;
665 NSUnlockMessageListMutex();
670 void NSDestroyMessageStateList()
672 NS_LOG_V(DEBUG, "%s", __func__);
673 NSLockMessageListMutex();
675 NSMessageStateLL * iter = NSGetMessageStateList()->head;
678 NSMessageStateLL * del = iter;
683 NSGetMessageStateList()->head = NULL;
684 NSGetMessageStateList()->tail = NULL;
686 NSUnlockMessageListMutex();