replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / examples / linux / notificationserviceprovider.cpp
1 /******************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 // std
22 #include <iostream>
23 #include <stdlib.h>
24 #include <cstdint>
25 #include <limits>
26
27 // ns
28 #include "NSCommon.h"
29 #include "NSProviderService.h"
30 #include "NSUtils.h"
31 #include "NSTopicsList.h"
32
33 // base
34 #include "logger.h"
35 #include "octypes.h"
36 #include "oic_string.h"
37 #include "oic_malloc.h"
38 #include "ocstack.h"
39
40 // external
41 #include "pthread.h"
42
43 #define TAG "NotiProviderWrapperExample"
44 using namespace std;
45 using namespace OIC::Service;
46 std::vector<std::string> discoveredConsumers;
47 uint64_t mainMessageId = 0;
48
49 extern char *strdup(const char *s);
50
51 bool isExit = false;
52
53 int id = 0;
54 std::string REMOTE_SERVER_ADDRESS;
55
56 void *OCProcessThread(void *ptr)
57 {
58     (void) ptr;
59     while (!isExit)
60     {
61         usleep(2000);
62         if (OCProcess() != OC_STACK_OK)
63         {
64             std::cout << "OCStack process error" << std::endl;
65             return NULL;
66         }
67     }
68
69     return NULL;
70 }
71
72 void subscribeRequestCallback(std::shared_ptr<OIC::Service::NSConsumer> consumer)
73 {
74     std::cout << "consumer requested to subscribe" << std::endl;
75
76     std::cout << "Consumer Device ID: " << consumer->getConsumerId() << std::endl;
77     discoveredConsumers.push_back(consumer->getConsumerId());
78     consumer->acceptSubscription(true);
79 }
80
81 void syncCallback(OIC::Service::NSSyncInfo sync)
82 {
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;
87 }
88
89 std::shared_ptr<OIC::Service::NSConsumer> printAvailableConsumers()
90 {
91     std::cout << "Choose the Consumer ID for operation" << std::endl;
92     int pos = 1;
93     unsigned int option = 0;
94     for (auto it : discoveredConsumers)
95     {
96         std::cout << pos << ". " << it << std::endl;
97         pos++;
98     }
99     while (!(std::cin >> option))
100     {
101         std::cout << "Bad value!" << std::endl;;
102         std::cin.clear();
103         std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
104     }
105     option--;
106     if (option > discoveredConsumers.size())
107     {
108         return NULL;
109     }
110     std::string consumerId = discoveredConsumers[option];
111     std::shared_ptr<OIC::Service::NSConsumer> consumer =
112         NSProviderService::getInstance()->getConsumer(consumerId);
113     return consumer;
114 }
115
116 int main()
117 {
118     int num = 0;
119     pthread_t processThread = 0;
120
121     std::cout << "start Iotivity" << std::endl;
122
123
124     if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)
125     {
126         std::cout << "OCStack init error" << std::endl;
127         return 0;
128     }
129
130     pthread_create(&processThread, NULL, OCProcessThread, NULL);
131
132     while (!isExit)
133     {
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;
140
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;
147 #ifdef WITH_CLOUD
148         std::cout << "13. Enable NS Provider RemoteService" << std::endl;
149         std::cout << "14. Disable NS Provider RemoteService" << std::endl;
150 #endif
151         std::cout << "15. Stop the Notification Provider" << std::endl;
152         std::cout << "16. Exit()" << std::endl;
153
154         std::cout << "input : ";
155
156         std::cin >> num;
157
158         switch (num)
159         {
160             case 1:
161                 {
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;
167
168                     NSProviderService::getInstance()->start(cfg);
169                     break;
170                 }
171             case 2:
172                 {
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;
178
179                     NSProviderService::getInstance()->start(cfg);
180                     break;
181                 }
182             case 3:
183                 {
184                     std::cout << "Allow Subscription" << std::endl;
185                     std::shared_ptr<OIC::Service::NSConsumer> consumer = printAvailableConsumers();
186                     if (consumer != nullptr)
187                     {
188                         std::cout << "ALLOW" << std::endl;
189                         consumer->acceptSubscription(true);
190                     }
191                     break;
192                 }
193             case 4:
194                 {
195                     std::cout << "Deny Subscription" << std::endl;
196                     std::shared_ptr<OIC::Service::NSConsumer>consumer = printAvailableConsumers();
197                     if (consumer != nullptr)
198                     {
199                         std::cout << "DENY" << std::endl;
200                         consumer->acceptSubscription(false);
201                     }
202                     break;
203                 }
204             case 5:
205                 {
206                     std::cout << "------------------------------------" << std::endl;
207                     std::cout << "SendMessage" << std::endl;
208                     std::cout << "------------------------------------" << std::endl;
209
210                     std::string dummy;
211                     std::string title;
212                     std::string body;
213                     std::string topic;
214
215                     std::cout << "id : " << ++id << std::endl;
216                     std::cout << "title : ";
217
218                     std::getline(std::cin, dummy);
219                     std::getline(std::cin, title);
220
221                     std::cout << "body : ";
222                     std::getline(std::cin, body);
223
224                     std::cout << "topic : ";
225                     std::getline(std::cin, topic);
226
227                     std::cout << "app - mTitle : " << title << std::endl;
228                     std::cout << "app - mContentText : " << body << std::endl;
229                     std::cout << "app - mTopic : " << topic << std::endl;
230
231                     OIC::Service::NSMessage msg = NSProviderService::getInstance()->createMessage();
232
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");
237                     msg.setTopic(topic);
238                     msg.setTTL(40);
239                     msg.setTime("12:30");
240                     OIC::Service::NSMediaContents *mediaContents =
241                         new OIC::Service::NSMediaContents("iconImage");
242                     msg.setMediaContents(mediaContents);
243
244                     OC::OCRepresentation rep;
245                     rep.setValue("Key1", "Value1");
246                     rep.setValue("Key2", "Value2");
247                     msg.setExtraInfo(rep);
248
249                     mainMessageId = msg.getMessageId();
250                     std::cout << "ProviderID for Message : " << msg.getProviderId() << std::endl;
251                     std::cout << "MessageID for Message : " << msg.getMessageId() << std::endl;
252
253                     NSProviderService::getInstance()->sendMessage(msg);
254                     break;
255                 }
256             case 6:
257                 {
258                     std::cout << "------------------------------------" << std::endl;
259                     std::cout <<  "SendSyncInfo" << std::endl;
260                     std::cout << "------------------------------------" << std::endl;
261                     if (!mainMessageId)
262                     {
263                         std::cout <<  "Message ID is empty" << std::endl;
264                         break;
265                     }
266                     std::cout << "1. Send Read Sync" << std::endl;
267                     std::cout << "2. Send Delete Sync" << std::endl;
268                     int syn = 0;
269                     while (!(std::cin >> syn))
270                     {
271                         std::cout << "Bad value!" << std::endl;;
272                         std::cin.clear();
273                         std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
274                     }
275                     switch (syn)
276                     {
277                         case 1:
278                             {
279                                 std::cout << "Sending Read Sync" << std::endl;
280                                 NSProviderService::getInstance()->sendSyncInfo(mainMessageId,
281                                         OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
282                             }
283                             break;
284                         case 2:
285                             {
286                                 std::cout << "Sending Delete Sync" << std::endl;
287                                 NSProviderService::getInstance()->sendSyncInfo(mainMessageId,
288                                         OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED);
289                             }
290                             break;
291                         default:
292                             {
293                                 cout << "Invalid Input!. sending default Read Sync";
294                                 NSProviderService::getInstance()->sendSyncInfo(mainMessageId,
295                                         OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
296                                 std::cin.clear();
297                                 std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
298                                 break;
299                             }
300                     }
301                     break;
302                 }
303
304             case 7:
305                 {
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");
311                     break;
312                 }
313             case 8:
314                 {
315                     std::cout <<  "UnregisterTopic" << std::endl;
316                     NSProviderService::getInstance()->unregisterTopic("OCF_TOPIC2");
317                     break;
318                 }
319             case 9:
320                 {
321                     std::cout <<  "SetTopic" << std::endl;
322                     std::shared_ptr<OIC::Service::NSConsumer> consumer = printAvailableConsumers();
323                     if (consumer != nullptr)
324                     {
325                         consumer->setTopic("OCF_TOPIC1");
326                         consumer->setTopic("OCF_TOPIC2");
327                         consumer->setTopic("OCF_TOPIC3");
328                         std::cout <<  "SelectTopic completed" << std::endl;
329                     }
330                     break;
331                 }
332             case 10:
333                 {
334                     std::cout <<  "UnsetTopic" << std::endl;
335                     std::shared_ptr<OIC::Service::NSConsumer> consumer = printAvailableConsumers();
336                     if (consumer != nullptr)
337                     {
338                         consumer->unsetTopic("OCF_TOPIC1");
339                         std::cout <<  "UnSelectTopic completed" << std::endl;
340                     }
341                     break;
342                 }
343
344             case 11:
345                 {
346                     std::cout <<  "GetConsumerTopicList" << std::endl;
347                     std::shared_ptr<OIC::Service::NSConsumer> consumer = printAvailableConsumers();
348                     if (consumer != nullptr)
349                     {
350                         auto nsTopics = consumer->getConsumerTopicList();
351                         if (nsTopics != nullptr)
352                         {
353                             for (auto it : nsTopics->getTopicsList())
354                             {
355
356                                 std::cout << it.getTopicName() << std::endl;
357                                 std::cout << (int) it.getState() << std::endl;
358                             }
359                         }
360                         std::cout <<  "GetConsumerTopicList completed" << std::endl;
361                     }
362                     break;
363                 }
364
365             case 12:
366                 {
367                     std::cout <<  "GetRegisteredTopicList" << std::endl;
368                     auto nsTopics = NSProviderService::getInstance()->getRegisteredTopicList();
369                     for (auto it : nsTopics->getTopicsList())
370                     {
371
372                         std::cout << it.getTopicName() << std::endl;
373                         std::cout << (int) it.getState() << std::endl;
374                     }
375                     break;
376                 }
377 #ifdef WITH_CLOUD
378             case 13:
379                 {
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);
384                     break;
385                 }
386             case 14:
387                 {
388                     std::cout << "Disable NS Provider RemoteService" << std::endl;
389                     std::cout << "Input the Server Address :";
390                     NSProviderService::getInstance()->disableRemoteService(REMOTE_SERVER_ADDRESS);
391                     break;
392                 }
393 #endif
394             case 15:
395                 {
396                     std::cout << "Stop the Notification Provider" << std::endl;
397                     NSProviderService::getInstance()->stop();
398                     break;
399                 }
400             case 16:
401                 {
402                     std::cout << "Exit()" << std::endl;
403                     NSProviderService::getInstance()->stop();
404                     isExit = true;
405                     break;
406                 }
407             default:
408                 {
409                     std::cout << "Under Construction" << std::endl;
410                     std::cin.clear();
411                     std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
412                     break;
413                 }
414         }
415
416         std::cout << std::endl;
417     }
418
419     return 0;
420 }