1 /******************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
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 ******************************************************************/
25 #include "NSConsumerService.h"
26 #include "NSMessage.h"
27 #include "NSProvider.h"
28 #include "NSTopicsList.h"
31 #define TAG "NotiConsumerWrapperExample"
33 using namespace OIC::Service;
36 std::string REMOTE_SERVER_ADDRESS;
37 std::string mainProvider;
38 uint64_t mainMessageId = 0;
40 FILE *server_fopen(const char *path, const char *mode)
42 if (0 == strcmp(path, OC_SECURITY_DB_DAT_FILE_NAME))
44 return fopen("oic_ns_provider_db.dat", mode);
48 return fopen(path, mode);
52 void onNotificationPostedCb(OIC::Service::NSMessage notification)
54 std::cout << "------------------------------------" << std::endl;
55 std::cout << "Message Received " << std::endl;
56 std::cout << "------------------------------------" << std::endl;
57 std::cout << "id : " << notification.getMessageId() << std::endl;
58 std::cout << "title : " << notification.getTitle() << std::endl;
59 std::cout << "content : " << notification.getContentText() << std::endl;
60 std::cout << "source : " << notification.getSourceName() << std::endl;
61 std::cout << "topic : " << notification.getTopic() << std::endl;
62 std::cout << "type : " << (int) notification.getType() << std::endl;
63 std::cout << "TTL : " << notification.getTTL() << std::endl;
64 std::cout << "time : " << notification.getTime() << std::endl;
65 if (notification.getMediaContents() != nullptr)
67 std::cout << "MediaContents IconImage : " << notification.getMediaContents()->getIconImage()
70 std::cout << "ExtraInfo " << std::endl;
71 OC::OCRepresentation rep = notification.getExtraInfo();
72 for (auto it : rep.getResourceTypes())
74 std::cout << "resourceType : " << it << std::endl;
76 for (auto it : rep.getResourceInterfaces())
78 std::cout << "Interface : " << it << std::endl;
80 for (auto it : rep.getValues())
82 std::cout << "Key : " << it.first << std::endl;
84 mainMessageId = notification.getMessageId();
87 void onNotificationSyncCb(OIC::Service::NSSyncInfo sync)
89 std::cout << "------------------------------------" << std::endl;
90 std::cout << "SyncInfo Received " << std::endl;
91 std::cout << "------------------------------------" << std::endl;
92 std::cout << "Sync ID : " << sync.getMessageId() << std::endl;
93 std::cout << "Provider ID : " << sync.getProviderId() << std::endl;
94 std::cout << "Sync STATE : " << (int) sync.getState() << std::endl;
97 void onProviderStateChangedCb(OIC::Service::NSProviderState state)
99 std::cout << "onProviderStateChangedCb" << std::endl;
100 if (state == OIC::Service::NSProviderState::ALLOW)
102 std::cout << "Provider Subscription Accepted" << std::endl;
104 else if (state == OIC::Service::NSProviderState::DENY)
106 std::cout << "Provider Subscription Denied" << std::endl;
107 std::cout << "------------------------------------" << std::endl;
109 else if (state == OIC::Service::NSProviderState::TOPIC)
111 std::shared_ptr<OIC::Service::NSProvider> provider =
112 NSConsumerService::getInstance()->getProvider(mainProvider);
113 if (provider != nullptr)
115 auto topicList = provider->getTopicList();
116 if (topicList != nullptr)
118 for (auto it : topicList->getTopicsList())
120 std::cout << "Topic Name: " << it.getTopicName() << std::endl;
121 std::cout << "Topic state: " << (int) it.getState() << std::endl;
126 else if (state == OIC::Service::NSProviderState::STOPPED)
128 std::cout << "Provider Stopped" << std::endl;
129 std::cout << "------------------------------------" << std::endl;
133 void onDiscoverNotificationCb(std::shared_ptr<OIC::Service::NSProvider> provider)
135 std::cout << "Notification Resource Discovered" << std::endl;
136 std::cout << "SetListeners for callbacks" << std::endl;
137 std::cout << "ProviderID : " << provider->getProviderId() << std::endl;
138 provider->setListener(onProviderStateChangedCb, onNotificationPostedCb, onNotificationSyncCb);
139 if (!provider->isSubscribed())
141 std::cout << "startSubscribing" << std::endl;
142 provider->subscribe();
144 if (mainProvider.empty())
146 mainProvider = provider->getProviderId();
150 void *OCProcessThread(void *ptr)
157 if (OCProcess() != OC_STACK_OK)
169 pthread_t OCThread = 0;
171 std::cout << "start Iotivity" << std::endl;
174 static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
175 OCRegisterPersistentStorageHandler(&ps);
177 if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
179 std::cout << "OCInit fail" << std::endl;
183 pthread_create(&OCThread, NULL, OCProcessThread, NULL);
185 std::cout << "Start notification consumer service" << std::endl;
190 std::cout << "1. Start Consumer" << std::endl;
191 std::cout << "2. Stop Consumer" << std::endl;
192 std::cout << "3. SendSyncInfo" << std::endl;
193 std::cout << "4. GetTopicList" << std::endl;
194 std::cout << "5. UpdateTopicList" << std::endl;
195 std::cout << "6. Subscribe provider" << std::endl;
196 std::cout << "7. UnSubscribe provider" << std::endl;
197 std::cout << "8. Rescan provider" << std::endl;
199 std::cout << "9. Enable NS Consumer RemoteService" << std::endl;
201 std::cout << "10. Exit" << std::endl;
203 std::cout << "Input: " << std::endl;
209 std::cout << "Start the Notification Consumer" << std::endl;
210 NSConsumerService::getInstance()->start(onDiscoverNotificationCb);
215 std::cout << "Stop the Notification Consumer" << std::endl;
216 NSConsumerService::getInstance()->stop();
221 std::cout << "SendSyncInfo" << std::endl;
224 std::cout << "Message ID is empty" << std::endl;
227 std::cout << "1. Send Read Sync" << std::endl;
228 std::cout << "2. Send Delete Sync" << std::endl;
230 while (!(std::cin >> syn))
232 std::cout << "Bad value!" << std::endl;;
234 std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
240 std::cout << "Sending Read Sync" << std::endl;
241 auto provider = NSConsumerService::getInstance()->getProvider(
243 if (provider != nullptr)
245 provider->sendSyncInfo(mainMessageId,
246 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
252 std::cout << "Sending Delete Sync" << std::endl;
253 auto provider = NSConsumerService::getInstance()->getProvider(
255 if (provider != nullptr)
257 provider->sendSyncInfo(mainMessageId,
258 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED);
264 cout << "Invalid Input!. sending default Read Sync";
265 auto provider = NSConsumerService::getInstance()->getProvider(
267 if (provider != nullptr)
269 provider->sendSyncInfo(mainMessageId,
270 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
273 std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
281 std::cout << "GetTopicList" << std::endl;
282 std::shared_ptr<OIC::Service::NSProvider> provider =
283 NSConsumerService::getInstance()->getProvider(mainProvider);
284 if (provider != nullptr)
286 auto topicList = provider->getTopicList();
287 if (topicList != nullptr)
289 for (auto it : topicList->getTopicsList())
291 std::cout << "Topic Name: " << it.getTopicName() << std::endl;
292 std::cout << "Topic state: " << (int) it.getState() << std::endl;
300 std::cout << "UpdateTopicList" << std::endl;
301 std::shared_ptr<OIC::Service::NSProvider> provider =
302 NSConsumerService::getInstance()->getProvider(mainProvider);
303 if (provider != nullptr)
305 std::shared_ptr<NSTopicsList> topicList = std::make_shared<NSTopicsList>();
306 topicList->addTopic("OCF_TOPIC1", NSTopic::NSTopicState::SUBSCRIBED);
307 topicList->addTopic("OCF_TOPIC2", NSTopic::NSTopicState::SUBSCRIBED);
308 topicList->addTopic("OCF_TOPIC3", NSTopic::NSTopicState::UNSUBSCRIBED);
310 provider->updateTopicList(topicList);
316 std::cout << "Subscribe provider" << std::endl;
317 if (!mainProvider.empty())
319 std::shared_ptr<OIC::Service::NSProvider> provider =
320 NSConsumerService::getInstance()->getProvider(mainProvider);
321 if (provider != nullptr )
323 std::cout << "calling Subscribe on discovered mainProvider" << std::endl;
324 if (!provider->isSubscribed())
326 std::cout << "start Subscribing" << std::endl;
327 provider->subscribe();
335 std::cout << "UnSubscribe provider" << std::endl;
336 if (!mainProvider.empty())
338 std::shared_ptr<OIC::Service::NSProvider> provider =
339 NSConsumerService::getInstance()->getProvider(mainProvider);
340 if (provider != nullptr )
342 std::cout << "calling UnSubscribe on discovered mainProvider" << std::endl;
343 if (provider->isSubscribed())
345 std::cout << "start UnSubscribing" << std::endl;
346 provider->unsubscribe();
354 std::cout << "Rescan Provider" << std::endl;
355 NSConsumerService::getInstance()->rescanProvider();
362 std::cout << "Enable NS Consumer RemoteService" << std::endl;
363 std::cout << "Input the Server Address :";
364 std::cin >> REMOTE_SERVER_ADDRESS;
365 NSConsumerService::getInstance()->enableRemoteService(REMOTE_SERVER_ADDRESS);
371 std::cout << "Exit" << std::endl;
372 NSConsumerService::getInstance()->stop();
378 std::cout << "Under Construction" << std::endl;
380 std::cin.ignore(numeric_limits<streamsize>::max(), '\n');