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 ******************************************************************/
29 #include "NSProviderService.h"
31 #include "NSTopicsList.h"
36 #include "oic_string.h"
37 #include "oic_malloc.h"
43 #define TAG "NotiProviderWrapperExample"
45 using namespace OIC::Service;
46 std::vector<std::string> discoveredConsumers;
47 uint64_t mainMessageId = 0;
49 extern char *strdup(const char *s);
54 std::string REMOTE_SERVER_ADDRESS;
56 void *OCProcessThread(void *ptr)
62 if (OCProcess() != OC_STACK_OK)
64 std::cout << "OCStack process error" << std::endl;
72 void subscribeRequestCallback(std::shared_ptr<OIC::Service::NSConsumer> consumer)
74 std::cout << "consumer requested to subscribe" << std::endl;
76 std::cout << "Consumer Device ID: " << consumer->getConsumerId() << std::endl;
77 discoveredConsumers.push_back(consumer->getConsumerId());
78 consumer->acceptSubscription(true);
81 void syncCallback(OIC::Service::NSSyncInfo sync)
83 std::cout << "SyncInfo Received " << std::endl;
84 std::cout << "Sync ID : " << sync.getMessageId() << std::endl;
85 std::cout << "Provider ID : " << sync.getProviderId() << std::endl;
86 std::cout << "Sync State: " << (int) sync.getState() << std::endl;
89 std::shared_ptr<OIC::Service::NSConsumer> printAvailableConsumers()
91 std::cout << "Choose the Consumer ID for operation" << std::endl;
93 unsigned int option = 0;
94 for (auto it : discoveredConsumers)
96 std::cout << pos << ". " << it << std::endl;
99 while (!(std::cin >> option))
101 std::cout << "Bad value!" << std::endl;;
103 std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
106 if (option > discoveredConsumers.size())
110 std::string consumerId = discoveredConsumers[option];
111 std::shared_ptr<OIC::Service::NSConsumer> consumer =
112 NSProviderService::getInstance()->getConsumer(consumerId);
119 pthread_t processThread = 0;
121 std::cout << "start Iotivity" << std::endl;
124 if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)
126 std::cout << "OCStack init error" << std::endl;
130 pthread_create(&processThread, NULL, OCProcessThread, NULL);
134 std::cout << "1. Start the Notification Provider(Accepter: Provider)" << std::endl;
135 std::cout << "2. Start the Notification Provider(Accepter: Consumer)" << std::endl;
136 std::cout << "3. Allow Subscription" << std::endl;
137 std::cout << "4. Deny Subscription" << std::endl;
138 std::cout << "5. SendMessage " << std::endl;
139 std::cout << "6. SendSyncInfo" << std::endl;
141 std::cout << "7. RegisterTopic" << std::endl;
142 std::cout << "8. UnregisterTopic" << std::endl;
143 std::cout << "9. SetTopic" << std::endl;
144 std::cout << "10. UnsetTopic" << std::endl;
145 std::cout << "11. GetConsumerTopicList" << std::endl;
146 std::cout << "12. GetRegisteredTopicList" << std::endl;
148 std::cout << "13. Enable NS Provider RemoteService" << std::endl;
149 std::cout << "14. Disable NS Provider RemoteService" << std::endl;
151 std::cout << "15. Stop the Notification Provider" << std::endl;
152 std::cout << "16. Exit()" << std::endl;
154 std::cout << "input : ";
162 std::cout << "Start (Accepter: Provider)" << std::endl;
163 NSProviderService::ProviderConfig cfg;
164 cfg.m_subscribeRequestCb = subscribeRequestCallback;
165 cfg.m_syncInfoCb = syncCallback;
166 cfg.subControllability = true;
168 NSProviderService::getInstance()->start(cfg);
173 std::cout << "Start (Accepter: Consumer)" << std::endl;
174 NSProviderService::ProviderConfig cfg;
175 cfg.m_subscribeRequestCb = subscribeRequestCallback;
176 cfg.m_syncInfoCb = syncCallback;
177 cfg.subControllability = false;
179 NSProviderService::getInstance()->start(cfg);
184 std::cout << "Allow Subscription" << std::endl;
185 std::shared_ptr<OIC::Service::NSConsumer> consumer = printAvailableConsumers();
186 if (consumer != nullptr)
188 std::cout << "ALLOW" << std::endl;
189 consumer->acceptSubscription(true);
195 std::cout << "Deny Subscription" << std::endl;
196 std::shared_ptr<OIC::Service::NSConsumer>consumer = printAvailableConsumers();
197 if (consumer != nullptr)
199 std::cout << "DENY" << std::endl;
200 consumer->acceptSubscription(false);
206 std::cout << "------------------------------------" << std::endl;
207 std::cout << "SendMessage" << std::endl;
208 std::cout << "------------------------------------" << std::endl;
215 std::cout << "id : " << ++id << std::endl;
216 std::cout << "title : ";
218 std::getline(std::cin, dummy);
219 std::getline(std::cin, title);
221 std::cout << "body : ";
222 std::getline(std::cin, body);
224 std::cout << "topic : ";
225 std::getline(std::cin, topic);
227 std::cout << "app - mTitle : " << title << std::endl;
228 std::cout << "app - mContentText : " << body << std::endl;
229 std::cout << "app - mTopic : " << topic << std::endl;
231 OIC::Service::NSMessage msg = NSProviderService::getInstance()->createMessage();
233 msg.setType(OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_INFO);
234 msg.setTitle(title.c_str());
235 msg.setContentText(body.c_str());
236 msg.setSourceName("OCF");
239 msg.setTime("12:30");
240 OIC::Service::NSMediaContents *mediaContents =
241 new OIC::Service::NSMediaContents("iconImage");
242 msg.setMediaContents(mediaContents);
244 OC::OCRepresentation rep;
245 rep.setValue("Key1", "Value1");
246 rep.setValue("Key2", "Value2");
247 msg.setExtraInfo(rep);
249 mainMessageId = msg.getMessageId();
250 std::cout << "ProviderID for Message : " << msg.getProviderId() << std::endl;
251 std::cout << "MessageID for Message : " << msg.getMessageId() << std::endl;
253 NSProviderService::getInstance()->sendMessage(msg);
258 std::cout << "------------------------------------" << std::endl;
259 std::cout << "SendSyncInfo" << std::endl;
260 std::cout << "------------------------------------" << std::endl;
263 std::cout << "Message ID is empty" << std::endl;
266 std::cout << "1. Send Read Sync" << std::endl;
267 std::cout << "2. Send Delete Sync" << std::endl;
269 while (!(std::cin >> syn))
271 std::cout << "Bad value!" << std::endl;;
273 std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
279 std::cout << "Sending Read Sync" << std::endl;
280 NSProviderService::getInstance()->sendSyncInfo(mainMessageId,
281 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
286 std::cout << "Sending Delete Sync" << std::endl;
287 NSProviderService::getInstance()->sendSyncInfo(mainMessageId,
288 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED);
293 cout << "Invalid Input!. sending default Read Sync";
294 NSProviderService::getInstance()->sendSyncInfo(mainMessageId,
295 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
297 std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
306 std::cout << "RegisterTopic" << std::endl;
307 NSProviderService::getInstance()->registerTopic("OCF_TOPIC1");
308 NSProviderService::getInstance()->registerTopic("OCF_TOPIC2");
309 NSProviderService::getInstance()->registerTopic("OCF_TOPIC3");
310 NSProviderService::getInstance()->registerTopic("OCF_TOPIC4");
315 std::cout << "UnregisterTopic" << std::endl;
316 NSProviderService::getInstance()->unregisterTopic("OCF_TOPIC2");
321 std::cout << "SetTopic" << std::endl;
322 std::shared_ptr<OIC::Service::NSConsumer> consumer = printAvailableConsumers();
323 if (consumer != nullptr)
325 consumer->setTopic("OCF_TOPIC1");
326 consumer->setTopic("OCF_TOPIC2");
327 consumer->setTopic("OCF_TOPIC3");
328 std::cout << "SelectTopic completed" << std::endl;
334 std::cout << "UnsetTopic" << std::endl;
335 std::shared_ptr<OIC::Service::NSConsumer> consumer = printAvailableConsumers();
336 if (consumer != nullptr)
338 consumer->unsetTopic("OCF_TOPIC1");
339 std::cout << "UnSelectTopic completed" << std::endl;
346 std::cout << "GetConsumerTopicList" << std::endl;
347 std::shared_ptr<OIC::Service::NSConsumer> consumer = printAvailableConsumers();
348 if (consumer != nullptr)
350 auto nsTopics = consumer->getConsumerTopicList();
351 if (nsTopics != nullptr)
353 for (auto it : nsTopics->getTopicsList())
356 std::cout << it.getTopicName() << std::endl;
357 std::cout << (int) it.getState() << std::endl;
360 std::cout << "GetConsumerTopicList completed" << std::endl;
367 std::cout << "GetRegisteredTopicList" << std::endl;
368 auto nsTopics = NSProviderService::getInstance()->getRegisteredTopicList();
369 for (auto it : nsTopics->getTopicsList())
372 std::cout << it.getTopicName() << std::endl;
373 std::cout << (int) it.getState() << std::endl;
380 std::cout << "Enable NS Provider RemoteService" << std::endl;
381 std::cout << "Input the Server Address :";
382 std::cin >> REMOTE_SERVER_ADDRESS;
383 NSProviderService::getInstance()->enableRemoteService(REMOTE_SERVER_ADDRESS);
388 std::cout << "Disable NS Provider RemoteService" << std::endl;
389 std::cout << "Input the Server Address :";
390 NSProviderService::getInstance()->disableRemoteService(REMOTE_SERVER_ADDRESS);
396 std::cout << "Stop the Notification Provider" << std::endl;
397 NSProviderService::getInstance()->stop();
402 std::cout << "Exit()" << std::endl;
403 NSProviderService::getInstance()->stop();
409 std::cout << "Under Construction" << std::endl;
411 std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
416 std::cout << std::endl;