NSResult NSSetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload) { NS_LOG(DEBUG, "NSSetMessagePayload - IN"); *msgPayload = msg->extraInfo != NULL ? msg->extraInfo : OCRepPayloadCreate(); if (!*msgPayload) { NS_LOG(ERROR, "Failed to allocate payload"); return NS_ERROR; } OCRepPayloadSetUri(*msgPayload, NS_COLLECTION_MESSAGE_URI); OCRepPayloadSetPropInt(*msgPayload, NS_ATTRIBUTE_MESSAGE_ID, msg->messageId); OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_PROVIDER_ID, msg->providerId); NSDuplicateSetPropertyInt(msgPayload, NS_ATTRIBUTE_TYPE, msg->type); NSDuplicateSetPropertyInt(msgPayload, NS_ATTRIBUTE_TTL, msg->ttl); NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_DATETIME, msg->dateTime); NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TITLE, msg->title); NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TEXT, msg->contentText); NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_SOURCE, msg->sourceName); NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TOPIC_NAME, msg->topic); if (msg->mediaContents) { NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_ICON_IMAGE, msg->mediaContents->iconImage); } NS_LOG(DEBUG, "NSSetMessagePayload - OUT"); return NS_OK; }
NSResult NSProviderService::EnableRemoteService(const std::string &serverAddress) { NS_LOG(DEBUG, "EnableRemoteService - IN"); NSResult result = NSProviderEnableRemoteService(OICStrdup(serverAddress.c_str())); NS_LOG(DEBUG, "EnableRemoteService - OUT"); return result; }
NSResult NSProviderCacheUpdate(NSProvider_internal * provider) { NSCacheList * ProviderCache = *(NSGetProviderCacheList()); if (!ProviderCache) { NS_LOG(DEBUG, "Provider Cache Init"); ProviderCache = NSStorageCreate(); NS_VERIFY_NOT_NULL(ProviderCache, NS_ERROR); ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER; NSSetProviderCacheList(ProviderCache); } NS_VERIFY_NOT_NULL(provider, NS_ERROR); NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement)); NS_VERIFY_NOT_NULL(obj, NS_ERROR); obj->data = (NSCacheData *) provider; obj->next = NULL; NS_LOG(DEBUG, "try to write to storage"); NSResult ret = NSStorageWrite(ProviderCache, obj); NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR, NSOICFree(obj)); return NS_OK; }
NSResult NSRegisterResource() { NS_LOG(DEBUG, "NSRegisterResource - IN"); if (NSCreateResource(NS_COLLECTION_SYNC_URI) != NS_OK) { NS_LOG(ERROR, "Fail to register Sync Resource"); return NS_ERROR; } if (NSCreateResource(NS_COLLECTION_MESSAGE_URI) != NS_OK) { NS_LOG(ERROR, "Fail to register Message Resource"); return NS_ERROR; } if (NSCreateResource(NS_ROOT_URI) != NS_OK) { NS_LOG(ERROR, "Fail to register Notification Resource"); return NS_ERROR; } NS_LOG(DEBUG, "NSRegisterResource - OUT"); return NS_OK; }
NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle) { NS_LOG(DEBUG, "NSPutMessageResource - IN"); if(msg != NULL) { NS_LOG(DEBUG, "NSMessage is valid"); NotificationMessageResource.messageId = msg->messageId; OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId); NotificationMessageResource.type = msg->type; NotificationMessageResource.dateTime = msg->dateTime; NotificationMessageResource.ttl = msg->ttl; NotificationMessageResource.title = msg->title; NotificationMessageResource.contentText = msg->contentText; NotificationMessageResource.sourceName = msg->sourceName; NotificationMessageResource.mediaContents = msg->mediaContents; } else { NS_LOG(ERROR, "NSMessage is NULL"); } *handle = NotificationMessageResource.handle; NS_LOG(DEBUG, "NSPutMessageResource - OUT"); return NS_OK; }
NSCacheElement * NSProviderStorageRead(NSCacheList * list, const char * findId) { pthread_mutex_lock(&NSCacheMutex); NS_LOG(DEBUG, "NSCacheRead - IN"); NSCacheElement * iter = list->head; NSCacheElement * next = NULL; NSCacheType type = list->cacheType; NS_LOG_V(INFO_PRIVATE, "Find ID - %s", findId); while (iter) { next = iter->next; if (NSProviderCompareIdCacheData(type, iter->data, findId)) { NS_LOG(DEBUG, "Found in Cache"); pthread_mutex_unlock(&NSCacheMutex); return iter; } iter = next; } NS_LOG(DEBUG, "Not found in Cache"); NS_LOG(DEBUG, "NSCacheRead - OUT"); pthread_mutex_unlock(&NSCacheMutex); return NULL; }
NSResult NSProviderService::Stop() { NS_LOG(DEBUG, "Stop - IN"); NSResult result = NSStopProvider(); NS_LOG(DEBUG, "Stop - OUT"); return result; }
NSResult NSUnRegisterResource() { NS_LOG(DEBUG, "NSUnRegisterResource - IN"); if (OCDeleteResource(NotificationResource.handle) != OC_STACK_OK) { NS_LOG(ERROR, "Fail to Delete Notification Resource"); return NS_ERROR; } if (OCDeleteResource(NotificationMessageResource.handle) != OC_STACK_OK) { NS_LOG(ERROR, "Fail to Delete Notification Message Resource"); return NS_ERROR; } if (OCDeleteResource(NotificationSyncResource.handle) != OC_STACK_OK) { NS_LOG(ERROR, "Fail to Delete Notification Sync Resource"); return NS_ERROR; } NotificationResource.handle = NULL; NotificationMessageResource.handle = NULL; NotificationSyncResource.handle = NULL; NS_LOG(DEBUG, "NSUnRegisterResource - OUT"); return NS_OK; }
void NSProviderService::SendSyncInfo(uint64_t messageId, NSSyncInfo::NSSyncType type) { NS_LOG(DEBUG, "SendSyncInfo - IN"); NSProviderSendSyncInfo(messageId, (NSSyncType)type); NS_LOG(DEBUG, "SendSyncInfo - OUT"); return; }
void onConsumerSubscribedCallback(::NSConsumer *consumer) { NS_LOG(DEBUG, "onConsumerSubscribedCallback - IN"); NSConsumer *nsConsumer = new NSConsumer(consumer); if (NSProviderService::getInstance()->getProviderConfig().m_subscribeRequestCb != NULL) NSProviderService::getInstance()->getProviderConfig().m_subscribeRequestCb(nsConsumer); delete nsConsumer; NS_LOG(DEBUG, "onConsumerSubscribedCallback - OUT"); }
void onMessageSynchronizedCallback(::NSSyncInfo *syncInfo) { NS_LOG(DEBUG, "onMessageSynchronizedCallback - IN"); NSSyncInfo *nsSyncInfo = new NSSyncInfo(syncInfo); if (NSProviderService::getInstance()->getProviderConfig().m_syncInfoCb != NULL) NSProviderService::getInstance()->getProviderConfig().m_syncInfoCb(nsSyncInfo); delete nsSyncInfo; NS_LOG(DEBUG, "onMessageSynchronizedCallback - OUT"); }
NSResult NSProviderService::SendMessage(NSMessage *msg) { NS_LOG(DEBUG, "SendMessage - IN"); NSResult result = NS_ERROR; if (msg != nullptr) result = NSSendMessage(getNSMessage(msg)); else NS_LOG(DEBUG, "Empty Message"); NS_LOG(DEBUG, "SendMessage - OUT"); return result; }
NSResult NSProviderService::Start(NSProviderService::NSAccessPolicy policy, NSProviderService::ProviderConfig config) { NS_LOG(DEBUG, "Start - IN"); m_config = config; NSResult result = NSStartProvider((::NSAccessPolicy)policy, onConsumerSubscribedCallback, onMessageSynchronizedCallback); NS_LOG(DEBUG, "Start - OUT"); return result; }
NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle) { NS_LOG(DEBUG, "NSPutSyncResource - IN"); (void) sync; *handle = NotificationSyncResource.handle; NS_LOG(DEBUG, "NSPutSyncResource - OUT"); return NS_OK; }
NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle) { NS_LOG(DEBUG, "NSPutNotificationResource - IN"); NotificationResource.accepter = accepter; NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI; NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI; *handle = NotificationResource.handle; NS_LOG(DEBUG, "NSPutNotificationResource - OUT"); return NS_OK; }
bool NSStartScheduler() { int i = 0; for (i = 0; i < THREAD_COUNT; i++) { pthread_mutex_lock(&NSMutex[i]); switch (i) { case CALLBACK_RESPONSE_SCHEDULER: { NS_LOG(DEBUG, "CASE RESPONSE_SCHEDULER :"); pthread_create(&NSThread[i], NULL, NSCallbackResponseSchedule, NULL); } break; case DISCOVERY_SCHEDULER: { NS_LOG(DEBUG, "CASE DISCOVERY_SCHEDULER :"); pthread_create(&NSThread[i], NULL, NSDiscoverySchedule, NULL); } break; case SUBSCRIPTION_SCHEDULER: { NS_LOG(DEBUG, "CASE SUBSCRIPTION_SCHEDULER :"); pthread_create(&NSThread[i], NULL, NSSubScriptionSchedule, NULL); } break; case NOTIFICATION_SCHEDULER: { NS_LOG(DEBUG, "CASE NOTIFICATION_SCHEDULER :"); pthread_create(&NSThread[i], NULL, NSNotificationSchedule, NULL); } break; default: break; } NSHeadMsg[i] = NSTailMsg[i] = NULL; pthread_mutex_unlock(&NSMutex[i]); } return true; }
NSTopicLL * NSProviderGetTopicsCacheData(NSCacheList * regTopicList) { NS_LOG(DEBUG, "NSProviderGetTopicsCache - IN"); pthread_mutex_lock(&NSCacheMutex); NSCacheElement * iter = regTopicList->head; if (!iter) { pthread_mutex_unlock(&NSCacheMutex); return NULL; } NSTopicLL * iterTopic = NULL; NSTopicLL * newTopic = NULL; NSTopicLL * topics = NULL; while (iter) { NSCacheTopicData * curr = (NSCacheTopicData *) iter->data; newTopic = (NSTopicLL *) OICMalloc(sizeof(NSTopicLL)); if (!newTopic) { pthread_mutex_unlock(&NSCacheMutex); return NULL; } newTopic->state = curr->state; newTopic->next = NULL; newTopic->topicName = OICStrdup(curr->topicName); if (!topics) { iterTopic = topics = newTopic; } else { iterTopic->next = newTopic; iterTopic = newTopic; } iter = iter->next; } pthread_mutex_unlock(&NSCacheMutex); NS_LOG(DEBUG, "NSProviderGetTopicsCache - OUT"); return topics; }
NSTopicLL * NSProviderGetConsumerTopicsCacheData(NSCacheList * regTopicList, NSCacheList * conTopicList, const char * consumerId) { NS_LOG(DEBUG, "NSProviderGetConsumerTopicsCacheData - IN"); pthread_mutex_lock(&NSCacheMutex); NSTopicLL * topics = NSProviderGetTopicsCacheData(regTopicList); if (!topics) { pthread_mutex_unlock(&NSCacheMutex); return NULL; } NSCacheElement * iter = conTopicList->head; conTopicList->cacheType = NS_PROVIDER_CACHE_CONSUMER_TOPIC_CID; while (iter) { NSCacheTopicSubData * curr = (NSCacheTopicSubData *)iter->data; if (curr && strcmp(curr->id, consumerId) == 0) { NS_LOG_V(INFO_PRIVATE, "curr->id = %s", curr->id); NS_LOG_V(DEBUG, "curr->topicName = %s", curr->topicName); NSTopicLL * topicIter = topics; while (topicIter) { if (strcmp(topicIter->topicName, curr->topicName) == 0) { topicIter->state = NS_TOPIC_SUBSCRIBED; break; } topicIter = topicIter->next; } } iter = iter->next; } conTopicList->cacheType = NS_PROVIDER_CACHE_CONSUMER_TOPIC_NAME; pthread_mutex_unlock(&NSCacheMutex); NS_LOG(DEBUG, "NSProviderGetConsumerTopics - OUT"); return topics; }
NSCacheElement * NSConsumerStorageRead(NSCacheList * list, const char * findId) { NS_VERIFY_NOT_NULL(list, NULL); NS_VERIFY_NOT_NULL(findId, NULL); pthread_mutex_t * mutex = NSGetCacheMutex(); pthread_mutex_lock(mutex); NSCacheElement * iter = list->head; NSCacheType type = list->cacheType; while (iter) { if (NSConsumerCompareIdCacheData(type, iter->data, findId)) { pthread_mutex_unlock(mutex); return iter; } iter = iter->next; } NS_LOG (DEBUG, "No Cache Element"); pthread_mutex_unlock(mutex); return NULL; }
NSResult NSPublishResourceToCloud(char *serverAddress) { NS_LOG(DEBUG, "NSPublishResourceToCloud - IN"); NS_LOG_V(DEBUG, "Cloud address: %s", serverAddress); const char * publishQuery = "/oic/rd?rt=oic.wk.rdpub"; if (NSCloudPublish(serverAddress, publishQuery, &NSHandlePublishCb, 1, NotificationResource.handle) != OC_STACK_OK) { NS_LOG(DEBUG, "Unable to publish resources to cloud"); } NS_LOG(DEBUG, "NSPublishResourceToCloud - OUT"); return NS_OK; }
bool NSInitScheduler() { NS_LOG(DEBUG, "NSInitScheduler - IN"); int i = 0; for (i = 0; i < THREAD_COUNT; i++) { pthread_mutex_init(&NSMutex[i], NULL); NSIsRunning[i] = true; sem_init(&(NSSemaphore[i]), 0, 0); } NS_LOG(DEBUG, "NSInitScheduler - OUT"); return true; }
NSResult NSProviderStorageDelete(NSCacheList * list, const char * delId) { pthread_mutex_lock(&NSCacheMutex); NSCacheElement * prev = list->head; NSCacheElement * del = list->head; NSCacheType type = list->cacheType; if (!del) { NS_LOG(DEBUG, "list head is NULL"); pthread_mutex_unlock(&NSCacheMutex); return NS_FAIL; } if (NSProviderCompareIdCacheData(type, del->data, delId)) { if (del == list->head) // first object { if (del == list->tail) // first object (one object) { list->tail = del->next; } list->head = del->next; NSProviderDeleteCacheData(type, del->data); NSOICFree(del); pthread_mutex_unlock(&NSCacheMutex); return NS_OK; } } del = del->next; while (del) { if (NSProviderCompareIdCacheData(type, del->data, delId)) { if (del == list->tail) // delete object same to last object { list->tail = prev; } prev->next = del->next; NSProviderDeleteCacheData(type, del->data); NSOICFree(del); pthread_mutex_unlock(&NSCacheMutex); return NS_OK; } prev = del; del = del->next; } pthread_mutex_unlock(&NSCacheMutex); return NS_FAIL; }
void NSConsumerInternalTaskProcessing(NSTask * task) { NS_VERIFY_NOT_NULL_V(task); NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType); switch (task->taskType) { case TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED: { NS_LOG(DEBUG, "Receive Subscribe confirm from provider."); NSConsumerHandleRecvSubscriptionConfirmed((NSMessage *)task->taskData); NSRemoveMessage((NSMessage *)task->taskData); break; } case TASK_CONSUMER_RECV_MESSAGE: { NS_LOG(DEBUG, "Receive New Notification"); NSConsumerHandleRecvMessage((NSMessage *)task->taskData); NSRemoveMessage((NSMessage *)task->taskData); break; } case TASK_CONSUMER_PROVIDER_DISCOVERED: { NS_LOG(DEBUG, "Receive New Provider is discovered."); NSConsumerHandleProviderDiscovered((NSProvider_internal *)task->taskData); NSRemoveProvider((NSProvider_internal *)task->taskData); break; } case TASK_RECV_SYNCINFO: { NS_LOG(DEBUG, "Receive SyncInfo."); NSConsumerHandleRecvSyncInfo((NSSyncInfo *)task->taskData); NSOICFree(task->taskData); break; } case TASK_MAKE_SYNCINFO: { NS_LOG(DEBUG, "Make SyncInfo, get Provider's Addr"); NSConsumerHandleMakeSyncInfo((NSSyncInfo *)task->taskData); NSOICFree(task->taskData); break; } case TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL: { NS_LOG(DEBUG, "Make Subscribe cancel from provider."); NSConsumerHandleProviderDeleted((NSProvider_internal *)task->taskData); NSRemoveProvider((NSProvider_internal *)task->taskData); break; } default : { NS_LOG(ERROR, "Unknown TASK Type"); return ; } } NSOICFree(task); }
NSResult NSSetSyncPayload(NSSyncInfo *sync, OCRepPayload** syncPayload) { NS_LOG(DEBUG, "NSSetSyncPayload - IN"); *syncPayload = OCRepPayloadCreate(); if (!*syncPayload) { NS_LOG(ERROR, "Failed to allocate payload"); return NS_ERROR; } OCRepPayloadSetUri(*syncPayload, NS_COLLECTION_SYNC_URI); OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_PROVIDER_ID, sync->providerId); OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_MESSAGE_ID, sync->messageId); OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->state); NS_LOG(DEBUG, "NSSetSyncPayload - OUT"); return NS_OK; }
OCStackResult NSProviderPublishTopic(OCRepPayload * payload, OCClientResponseHandler response) { NS_LOG(DEBUG, "NSProviderPublishTopic - IN"); OCCallbackData cbData; memset(&cbData, 0, sizeof(OCCallbackData)); cbData.cb = response; cbData.cd = NULL; cbData.context = NULL; NSMQServerInfo * serverInfo = NSGetMQServerInfo(); if (!serverInfo) { NS_LOG(DEBUG, "serverInfo is not NULL"); NS_LOG_V(DEBUG, "serverInfo->serverUri = %s", serverInfo->serverUri); } NS_LOG(DEBUG, "NSProviderPublishTopic - OUT"); return OCDoResource(NULL, OC_REST_POST, serverInfo->serverUri, serverInfo->devAddr, (OCPayload *)payload, CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0); }
void NSConsumerHandleRecvSubscriptionConfirmed(NSMessage * msg) { NS_VERIFY_NOT_NULL_V(msg); NS_LOG_V(DEBUG, "confirmed by : %s", msg->providerId); NSProvider_internal * provider = NSProviderCacheFind(msg->providerId); NS_VERIFY_NOT_NULL_V(provider); if (provider->connection->next == NULL) { NS_LOG(DEBUG, "call back to user"); NSSubscriptionAccepted((NSProvider *) provider); } }
OCStackApplicationResult NSHandlePublishCb(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse) { (void) handle; if (ctx != (void *)DEFAULT_CONTEXT_VALUE) { NS_LOG(DEBUG, "Invalid publish callback received"); } NS_LOG_V(DEBUG, "Publish resource response received code: %d", clientResponse->result); return OC_STACK_KEEP_TRANSACTION; }
void NSCancelAllSubscription() { NSCacheList * ProviderCache = *(NSGetProviderCacheList()); if (!ProviderCache) { NS_LOG(DEBUG, "Provider Cache Init"); ProviderCache = NSStorageCreate(); NS_VERIFY_NOT_NULL_V(ProviderCache); ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER; NSSetProviderCacheList(ProviderCache); } NSCacheElement * obj = NULL; while ((obj = NSPopProviderCacheList(ProviderCache))) { NS_LOG(DEBUG, "build NSTask"); NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, (void *) obj->data); NS_VERIFY_NOT_NULL_V(task); NSConsumerPushEvent(task); } }
NSResult NSMessageCacheUpdate(NSMessage * msg, NSSyncType type) { NS_VERIFY_NOT_NULL(msg, NS_ERROR); NSCacheList * MessageCache = *(NSGetMessageCacheList()); if (!MessageCache) { NS_LOG(DEBUG, "Message Cache Init"); MessageCache = NSStorageCreate(); NS_VERIFY_NOT_NULL(MessageCache, NS_ERROR); MessageCache->cacheType = NS_CONSUMER_CACHE_MESSAGE; NSSetMessageCacheList(MessageCache); } NSStoreMessage * sMsg = (NSStoreMessage *)OICMalloc(sizeof(NSStoreMessage)); NS_VERIFY_NOT_NULL(sMsg, NS_ERROR); NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement)); NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, NSOICFree(sMsg)); sMsg->status = type; sMsg->msg = NSCopyMessage(msg); obj->data = (NSCacheData *) sMsg; obj->next = NULL; NS_LOG(DEBUG, "try to write to storage"); NSResult ret = NSStorageWrite(MessageCache, obj); NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR, NSRemoveCacheElementMessage(obj)); NSRemoveCacheElementMessage(obj); NS_LOG(DEBUG, "Update message done"); return NS_OK; }
void NSPushQueue(NSSchedulerType schedulerType, NSTaskType taskType, void* data) { if(!NSIsRunning[schedulerType]) { return; } pthread_mutex_lock(&NSMutex[schedulerType]); NS_LOG(DEBUG, "NSPushQueue - IN"); NS_LOG_V(DEBUG, "NSSchedulerType = %d", schedulerType); NS_LOG_V(DEBUG, "NSTaskType = %d", taskType); if (NSHeadMsg[schedulerType] == NULL) { NSHeadMsg[schedulerType] = (NSTask*) OICMalloc(sizeof(NSTask)); NSHeadMsg[schedulerType]->taskType = taskType; NSHeadMsg[schedulerType]->taskData = data; NSHeadMsg[schedulerType]->nextTask = NULL; NSTailMsg[schedulerType] = NSHeadMsg[schedulerType]; } else { NSTask* newNode = (NSTask*) OICMalloc(sizeof(NSTask)); newNode->taskType = taskType; newNode->taskData = data; newNode->nextTask = NULL; NSTailMsg[schedulerType]->nextTask = newNode; NSTailMsg[schedulerType] = newNode; } sem_post(&(NSSemaphore[schedulerType])); NS_LOG(DEBUG, "NSPushQueue - OUT"); pthread_mutex_unlock(&NSMutex[schedulerType]); }