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 #ifndef OC_SECURITY_DB_DAT_FILE_NAME
41 #define OC_SECURITY_DB_DAT_FILE_NAME "oic_svr_db.dat"
44 FILE *server_fopen(const char *path, const char *mode)
46 if (0 == strcmp(path, OC_SECURITY_DB_DAT_FILE_NAME))
48 return fopen("oic_ns_provider_db.dat", mode);
52 return fopen(path, mode);
56 void onNotificationPostedCb(OIC::Service::NSMessage notification)
58 std::cout << "------------------------------------" << std::endl;
59 std::cout << "Message Received " << std::endl;
60 std::cout << "------------------------------------" << std::endl;
61 std::cout << "id : " << notification.getMessageId() << std::endl;
62 std::cout << "title : " << notification.getTitle() << std::endl;
63 std::cout << "content : " << notification.getContentText() << std::endl;
64 std::cout << "source : " << notification.getSourceName() << std::endl;
65 std::cout << "topic : " << notification.getTopic() << std::endl;
66 std::cout << "type : " << (int) notification.getType() << std::endl;
67 std::cout << "TTL : " << notification.getTTL() << std::endl;
68 std::cout << "time : " << notification.getTime() << std::endl;
69 if (notification.getMediaContents() != nullptr)
71 std::cout << "MediaContents IconImage : " << notification.getMediaContents()->getIconImage()
74 std::cout << "ExtraInfo " << std::endl;
75 OC::OCRepresentation rep = notification.getExtraInfo();
76 for (auto it : rep.getResourceTypes())
78 std::cout << "resourceType : " << it << std::endl;
80 for (auto it : rep.getResourceInterfaces())
82 std::cout << "Interface : " << it << std::endl;
84 for (auto it : rep.getValues())
86 std::cout << "Key : " << it.first << std::endl;
88 mainMessageId = notification.getMessageId();
91 void onNotificationSyncCb(OIC::Service::NSSyncInfo sync)
93 std::cout << "------------------------------------" << std::endl;
94 std::cout << "SyncInfo Received " << std::endl;
95 std::cout << "------------------------------------" << std::endl;
96 std::cout << "Sync ID : " << sync.getMessageId() << std::endl;
97 std::cout << "Provider ID : " << sync.getProviderId() << std::endl;
98 std::cout << "Sync STATE : " << (int) sync.getState() << std::endl;
101 void onProviderStateChangedCb(OIC::Service::NSProviderState state)
103 std::cout << "onProviderStateChangedCb" << std::endl;
104 if (state == OIC::Service::NSProviderState::ALLOW)
106 std::cout << "Provider Subscription Accepted" << std::endl;
108 else if (state == OIC::Service::NSProviderState::DENY)
110 std::cout << "Provider Subscription Denied" << std::endl;
111 std::cout << "------------------------------------" << std::endl;
113 else if (state == OIC::Service::NSProviderState::TOPIC)
115 std::shared_ptr<OIC::Service::NSProvider> provider =
116 NSConsumerService::getInstance()->getProvider(mainProvider);
117 if (provider != nullptr)
119 auto topicList = provider->getTopicList();
120 if (topicList != nullptr)
122 for (auto it : topicList->getTopicsList())
124 std::cout << "Topic Name: " << it.getTopicName() << std::endl;
125 std::cout << "Topic state: " << (int) it.getState() << std::endl;
130 else if (state == OIC::Service::NSProviderState::STOPPED)
132 std::cout << "Provider Stopped" << std::endl;
133 std::cout << "------------------------------------" << std::endl;
137 void onDiscoverNotificationCb(std::shared_ptr<OIC::Service::NSProvider> provider)
139 std::cout << "Notification Resource Discovered" << std::endl;
140 std::cout << "SetListeners for callbacks" << std::endl;
141 std::cout << "ProviderID : " << provider->getProviderId() << std::endl;
142 provider->setListener(onProviderStateChangedCb, onNotificationPostedCb, onNotificationSyncCb);
143 if (!provider->isSubscribed())
145 std::cout << "startSubscribing" << std::endl;
146 provider->subscribe();
148 if (mainProvider.empty())
150 mainProvider = provider->getProviderId();
154 void *OCProcessThread(void *ptr)
161 if (OCProcess() != OC_STACK_OK)
173 pthread_t OCThread = 0;
175 std::cout << "start Iotivity" << std::endl;
178 static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
179 OCRegisterPersistentStorageHandler(&ps);
181 if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
183 std::cout << "OCInit fail" << std::endl;
187 pthread_create(&OCThread, NULL, OCProcessThread, NULL);
189 std::cout << "Start notification consumer service" << std::endl;
194 std::cout << "1. Start Consumer" << std::endl;
195 std::cout << "2. Stop Consumer" << std::endl;
196 std::cout << "3. SendSyncInfo" << std::endl;
197 std::cout << "4. GetTopicList" << std::endl;
198 std::cout << "5. UpdateTopicList" << std::endl;
199 std::cout << "6. Subscribe provider" << std::endl;
200 std::cout << "7. UnSubscribe provider" << std::endl;
201 std::cout << "8. Rescan provider" << std::endl;
203 std::cout << "9. Enable NS Consumer RemoteService" << std::endl;
205 std::cout << "10. Exit" << std::endl;
207 std::cout << "Input: " << std::endl;
213 std::cout << "Start the Notification Consumer" << std::endl;
214 NSConsumerService::getInstance()->start(onDiscoverNotificationCb);
219 std::cout << "Stop the Notification Consumer" << std::endl;
220 NSConsumerService::getInstance()->stop();
225 std::cout << "SendSyncInfo" << std::endl;
228 std::cout << "Message ID is empty" << std::endl;
231 std::cout << "1. Send Read Sync" << std::endl;
232 std::cout << "2. Send Delete Sync" << std::endl;
234 while (!(std::cin >> syn))
236 std::cout << "Bad value!" << std::endl;;
238 std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
244 std::cout << "Sending Read Sync" << std::endl;
245 auto provider = NSConsumerService::getInstance()->getProvider(
247 if (provider != nullptr)
249 provider->sendSyncInfo(mainMessageId,
250 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
256 std::cout << "Sending Delete Sync" << std::endl;
257 auto provider = NSConsumerService::getInstance()->getProvider(
259 if (provider != nullptr)
261 provider->sendSyncInfo(mainMessageId,
262 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED);
268 cout << "Invalid Input!. sending default Read Sync";
269 auto provider = NSConsumerService::getInstance()->getProvider(
271 if (provider != nullptr)
273 provider->sendSyncInfo(mainMessageId,
274 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
277 std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
285 std::cout << "GetTopicList" << std::endl;
286 std::shared_ptr<OIC::Service::NSProvider> provider =
287 NSConsumerService::getInstance()->getProvider(mainProvider);
288 if (provider != nullptr)
290 auto topicList = provider->getTopicList();
291 if (topicList != nullptr)
293 for (auto it : topicList->getTopicsList())
295 std::cout << "Topic Name: " << it.getTopicName() << std::endl;
296 std::cout << "Topic state: " << (int) it.getState() << std::endl;
304 std::cout << "UpdateTopicList" << std::endl;
305 std::shared_ptr<OIC::Service::NSProvider> provider =
306 NSConsumerService::getInstance()->getProvider(mainProvider);
307 if (provider != nullptr)
309 std::shared_ptr<NSTopicsList> topicList = std::make_shared<NSTopicsList>();
310 topicList->addTopic("OCF_TOPIC1", NSTopic::NSTopicState::SUBSCRIBED);
311 topicList->addTopic("OCF_TOPIC2", NSTopic::NSTopicState::SUBSCRIBED);
312 topicList->addTopic("OCF_TOPIC3", NSTopic::NSTopicState::UNSUBSCRIBED);
314 provider->updateTopicList(topicList);
320 std::cout << "Subscribe provider" << std::endl;
321 if (!mainProvider.empty())
323 std::shared_ptr<OIC::Service::NSProvider> provider =
324 NSConsumerService::getInstance()->getProvider(mainProvider);
325 if (provider != nullptr )
327 std::cout << "calling Subscribe on discovered mainProvider" << std::endl;
328 if (!provider->isSubscribed())
330 std::cout << "start Subscribing" << std::endl;
331 provider->subscribe();
339 std::cout << "UnSubscribe provider" << std::endl;
340 if (!mainProvider.empty())
342 std::shared_ptr<OIC::Service::NSProvider> provider =
343 NSConsumerService::getInstance()->getProvider(mainProvider);
344 if (provider != nullptr )
346 std::cout << "calling UnSubscribe on discovered mainProvider" << std::endl;
347 if (provider->isSubscribed())
349 std::cout << "start UnSubscribing" << std::endl;
350 provider->unsubscribe();
358 std::cout << "Rescan Provider" << std::endl;
359 NSConsumerService::getInstance()->rescanProvider();
366 std::cout << "Enable NS Consumer RemoteService" << std::endl;
367 std::cout << "Input the Server Address :";
368 std::cin >> REMOTE_SERVER_ADDRESS;
369 NSConsumerService::getInstance()->enableRemoteService(REMOTE_SERVER_ADDRESS);
375 std::cout << "Exit" << std::endl;
376 NSConsumerService::getInstance()->stop();
382 std::cout << "Under Construction" << std::endl;
384 std::cin.ignore(numeric_limits<streamsize>::max(), '\n');