1 //******************************************************************
\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
7 // Licensed under the Apache License, Version 2.0 (the "License");
\r
8 // you may not use this file except in compliance with the License.
\r
9 // You may obtain a copy of the License at
\r
11 // http://www.apache.org/licenses/LICENSE-2.0
\r
13 // Unless required by applicable law or agreed to in writing, software
\r
14 // distributed under the License is distributed on an "AS IS" BASIS,
\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 // See the License for the specific language governing permissions and
\r
17 // limitations under the License.
\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
22 #include "NSProviderService.h"
\r
24 #include "NSCommon.h"
\r
25 #include "NSProviderInterface.h"
\r
26 #include "oic_string.h"
\r
27 #include "NSConsumer.h"
\r
28 #include "NSSyncInfo.h"
\r
29 #include "NSConstants.h"
\r
35 void onConsumerSubscribedCallback(::NSConsumer *consumer)
\r
37 NS_LOG(DEBUG, "onConsumerSubscribedCallback - IN");
\r
38 NSConsumer *nsConsumer = new NSConsumer(consumer);
\r
39 if (NSProviderService::getInstance()->getProviderConfig().m_subscribeRequestCb != NULL)
\r
40 NSProviderService::getInstance()->getProviderConfig().m_subscribeRequestCb(nsConsumer);
\r
42 NS_LOG(DEBUG, "onConsumerSubscribedCallback - OUT");
\r
45 void onMessageSynchronizedCallback(::NSSyncInfo *syncInfo)
\r
47 NS_LOG(DEBUG, "onMessageSynchronizedCallback - IN");
\r
48 NSSyncInfo *nsSyncInfo = new NSSyncInfo(syncInfo);
\r
49 if (NSProviderService::getInstance()->getProviderConfig().m_syncInfoCb != NULL)
\r
50 NSProviderService::getInstance()->getProviderConfig().m_syncInfoCb(nsSyncInfo);
\r
52 NS_LOG(DEBUG, "onMessageSynchronizedCallback - OUT");
\r
55 ::NSMessage *NSProviderService::getNSMessage(NSMessage *msg)
\r
57 ::NSMessage *nsMsg = new ::NSMessage;
\r
58 nsMsg->messageId = msg->getMessageId();
\r
59 OICStrcpy(nsMsg->providerId, msg->getProviderId().length(), msg->getProviderId().c_str());
\r
60 nsMsg->sourceName = OICStrdup(msg->getSourceName().c_str());
\r
61 nsMsg->type = (::NSMessageType) msg->getType();
\r
62 nsMsg->dateTime = OICStrdup(msg->getTime().c_str());
\r
63 nsMsg->ttl = msg->getTTL();
\r
64 nsMsg->title = OICStrdup(msg->getTitle().c_str());
\r
65 nsMsg->contentText = OICStrdup(msg->getContentText().c_str());
\r
67 nsMsg->mediaContents = new ::NSMediaContents;
\r
68 if (msg->getMediaContents() != nullptr)
\r
69 nsMsg->mediaContents->iconImage = OICStrdup(msg->getMediaContents()->getIconImage().c_str());
\r
71 nsMsg->mediaContents->iconImage = nullptr;
\r
75 NSProviderService *NSProviderService::getInstance()
\r
77 static NSProviderService s_instance;
\r
81 NSResult NSProviderService::Start(NSProviderService::ProviderConfig config)
\r
83 NS_LOG(DEBUG, "Start - IN");
\r
86 NSProviderConfig nsConfig;
\r
87 nsConfig.subRequestCallback = onConsumerSubscribedCallback;
\r
88 nsConfig.syncInfoCallback = onMessageSynchronizedCallback;
\r
89 nsConfig.policy = config.policy;
\r
90 nsConfig.userInfo = OICStrdup(config.userInfo.c_str());
\r
92 NSResult result = (NSResult) NSStartProvider(nsConfig);
\r
93 NS_LOG(DEBUG, "Start - OUT");
\r
97 NSResult NSProviderService::Stop()
\r
99 NS_LOG(DEBUG, "Stop - IN");
\r
100 NSResult result = (NSResult) NSStopProvider();
\r
101 NS_LOG(DEBUG, "Stop - OUT");
\r
105 NSResult NSProviderService::EnableRemoteService(const std::string &serverAddress)
\r
107 NS_LOG(DEBUG, "EnableRemoteService - IN");
\r
108 NSResult result = NSResult::ERROR;
\r
110 result = (NSResult) NSProviderEnableRemoteService(OICStrdup(serverAddress.c_str()));
\r
112 NS_LOG(ERROR, "Remote Services feature is not enabled in the Build");
\r
114 NS_LOG(DEBUG, "EnableRemoteService - OUT");
\r
118 NSResult NSProviderService::DisableRemoteService(const std::string &serverAddress)
\r
120 NS_LOG(DEBUG, "DisableRemoteService - IN");
\r
121 NSResult result = NSResult::ERROR;
\r
123 result = (NSResult) NSProviderDisableRemoteService(OICStrdup(serverAddress.c_str()));
\r
125 NS_LOG(ERROR, "Remote Services feature is not enabled in the Build");
\r
127 NS_LOG(DEBUG, "DisableRemoteService - OUT");
\r
131 NSResult NSProviderService::SendMessage(NSMessage *msg)
\r
133 NS_LOG(DEBUG, "SendMessage - IN");
\r
134 NSResult result = NSResult::ERROR;
\r
135 if (msg != nullptr)
\r
136 result = (NSResult) NSSendMessage(getNSMessage(msg));
\r
138 NS_LOG(DEBUG, "Empty Message");
\r
139 NS_LOG(DEBUG, "SendMessage - OUT");
\r
143 void NSProviderService::SendSyncInfo(uint64_t messageId,
\r
144 NSSyncInfo::NSSyncType type)
\r
146 NS_LOG(DEBUG, "SendSyncInfo - IN");
\r
147 NSProviderSendSyncInfo(messageId, (NSSyncType)type);
\r
148 NS_LOG(DEBUG, "SendSyncInfo - OUT");
\r
152 NSMessage *NSProviderService::CreateMessage()
\r
154 NS_LOG(DEBUG, "CreateMessage - IN");
\r
156 ::NSMessage *message = NSCreateMessage();
\r
157 NSMessage *nsMessage = new NSMessage(message);
\r
159 NS_LOG_V(DEBUG, "Message ID : %lld", nsMessage->getMessageId());
\r
160 NS_LOG_V(DEBUG, "Provider ID : %s", nsMessage->getProviderId().c_str());
\r
161 NS_LOG(DEBUG, "CreateMessage - OUT");
\r
166 NSProviderService::ProviderConfig NSProviderService::getProviderConfig()
\r