replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / examples / linux / notificationserviceconsumer.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 #include <iostream>
22 #include <limits>
23
24 #include <unistd.h>
25 #include "NSConsumerService.h"
26 #include "NSMessage.h"
27 #include "NSProvider.h"
28 #include "NSTopicsList.h"
29 #include "ocstack.h"
30
31 #define TAG "NotiConsumerWrapperExample"
32 using namespace std;
33 using namespace OIC::Service;
34
35 bool isExit = false;
36 std::string REMOTE_SERVER_ADDRESS;
37 std::string mainProvider;
38 uint64_t mainMessageId = 0;
39
40 #ifndef OC_SECURITY_DB_DAT_FILE_NAME
41 #define OC_SECURITY_DB_DAT_FILE_NAME "oic_svr_db.dat"
42 #endif
43
44 FILE *server_fopen(const char *path, const char *mode)
45 {
46     if (0 == strcmp(path, OC_SECURITY_DB_DAT_FILE_NAME))
47     {
48         return fopen("oic_ns_provider_db.dat", mode);
49     }
50     else
51     {
52         return fopen(path, mode);
53     }
54 }
55
56 void onNotificationPostedCb(OIC::Service::NSMessage notification)
57 {
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)
70     {
71         std::cout << "MediaContents IconImage : " <<  notification.getMediaContents()->getIconImage()
72                   << std::endl;
73     }
74     std::cout << "ExtraInfo " << std::endl;
75     OC::OCRepresentation rep = notification.getExtraInfo();
76     for (auto it : rep.getResourceTypes())
77     {
78         std::cout << "resourceType : " << it << std::endl;
79     }
80     for (auto it : rep.getResourceInterfaces())
81     {
82         std::cout << "Interface : " << it << std::endl;
83     }
84     for (auto it : rep.getValues())
85     {
86         std::cout << "Key : " << it.first << std::endl;
87     }
88     mainMessageId = notification.getMessageId();
89 }
90
91 void onNotificationSyncCb(OIC::Service::NSSyncInfo sync)
92 {
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;
99 }
100
101 void onProviderStateChangedCb(OIC::Service::NSProviderState state)
102 {
103     std::cout << "onProviderStateChangedCb" << std::endl;
104     if (state == OIC::Service::NSProviderState::ALLOW)
105     {
106         std::cout << "Provider Subscription Accepted" << std::endl;
107     }
108     else if (state == OIC::Service::NSProviderState::DENY)
109     {
110         std::cout << "Provider Subscription Denied" << std::endl;
111         std::cout << "------------------------------------" << std::endl;
112     }
113     else if (state == OIC::Service::NSProviderState::TOPIC)
114     {
115         std::shared_ptr<OIC::Service::NSProvider> provider =
116             NSConsumerService::getInstance()->getProvider(mainProvider);
117         if (provider != nullptr)
118         {
119             auto topicList = provider->getTopicList();
120             if (topicList != nullptr)
121             {
122                 for (auto it : topicList->getTopicsList())
123                 {
124                     std::cout << "Topic Name: " << it.getTopicName() << std::endl;
125                     std::cout << "Topic state: " << (int) it.getState() << std::endl;
126                 }
127             }
128         }
129     }
130     else if (state == OIC::Service::NSProviderState::STOPPED)
131     {
132         std::cout << "Provider Stopped" << std::endl;
133         std::cout << "------------------------------------" << std::endl;
134     }
135 }
136
137 void onDiscoverNotificationCb(std::shared_ptr<OIC::Service::NSProvider> provider)
138 {
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())
144     {
145         std::cout << "startSubscribing" << std::endl;
146         provider->subscribe();
147     }
148     if (mainProvider.empty())
149     {
150         mainProvider = provider->getProviderId();
151     }
152 }
153
154 void *OCProcessThread(void *ptr)
155 {
156     (void) ptr;
157
158     while (!isExit)
159     {
160         usleep(2000);
161         if (OCProcess() != OC_STACK_OK)
162         {
163             OCStop();
164             break;
165         }
166     }
167
168     return NULL;
169 }
170
171 int main(void)
172 {
173     pthread_t OCThread = 0;
174
175     std::cout << "start Iotivity" << std::endl;
176
177     // open oic_db
178     static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
179     OCRegisterPersistentStorageHandler(&ps);
180
181     if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
182     {
183         std::cout << "OCInit fail" << std::endl;
184         return 0;
185     }
186
187     pthread_create(&OCThread, NULL, OCProcessThread, NULL);
188
189     std::cout << "Start notification consumer service" << std::endl;
190     while (!isExit)
191     {
192         int num = 0;
193
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;
202 #ifdef WITH_CLOUD
203         std::cout << "9. Enable  NS Consumer RemoteService" << std::endl;
204 #endif
205         std::cout << "10. Exit" << std::endl;
206
207         std::cout << "Input: " << std::endl;
208         std::cin >> num;
209         switch (num)
210         {
211             case 1:
212                 {
213                     std::cout << "Start the Notification Consumer" << std::endl;
214                     NSConsumerService::getInstance()->start(onDiscoverNotificationCb);
215                     break;
216                 }
217             case 2:
218                 {
219                     std::cout << "Stop the Notification Consumer" << std::endl;
220                     NSConsumerService::getInstance()->stop();
221                     break;
222                 }
223             case 3:
224                 {
225                     std::cout <<  "SendSyncInfo" << std::endl;
226                     if (!mainMessageId)
227                     {
228                         std::cout <<  "Message ID is empty" << std::endl;
229                         break;
230                     }
231                     std::cout << "1. Send Read Sync" << std::endl;
232                     std::cout << "2. Send Delete Sync" << std::endl;
233                     int syn = 0;
234                     while (!(std::cin >> syn))
235                     {
236                         std::cout << "Bad value!" << std::endl;;
237                         std::cin.clear();
238                         std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
239                     }
240                     switch (syn)
241                     {
242                         case 1:
243                             {
244                                 std::cout << "Sending Read Sync" << std::endl;
245                                 auto provider = NSConsumerService::getInstance()->getProvider(
246                                                     mainProvider);
247                                 if (provider != nullptr)
248                                 {
249                                     provider->sendSyncInfo(mainMessageId,
250                                                            OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
251                                 }
252                             }
253                             break;
254                         case 2:
255                             {
256                                 std::cout << "Sending Delete Sync" << std::endl;
257                                 auto provider = NSConsumerService::getInstance()->getProvider(
258                                                     mainProvider);
259                                 if (provider != nullptr)
260                                 {
261                                     provider->sendSyncInfo(mainMessageId,
262                                                            OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED);
263                                 }
264                             }
265                             break;
266                         default:
267                             {
268                                 cout << "Invalid Input!. sending default Read Sync";
269                                 auto provider = NSConsumerService::getInstance()->getProvider(
270                                                     mainProvider);
271                                 if (provider != nullptr)
272                                 {
273                                     provider->sendSyncInfo(mainMessageId,
274                                                            OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
275                                 }
276                                 std::cin.clear();
277                                 std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
278                                 break;
279                             }
280                     }
281                     break;
282                 }
283             case 4:
284                 {
285                     std::cout <<  "GetTopicList" << std::endl;
286                     std::shared_ptr<OIC::Service::NSProvider> provider =
287                         NSConsumerService::getInstance()->getProvider(mainProvider);
288                     if (provider != nullptr)
289                     {
290                         auto topicList = provider->getTopicList();
291                         if (topicList != nullptr)
292                         {
293                             for (auto it : topicList->getTopicsList())
294                             {
295                                 std::cout << "Topic Name: " << it.getTopicName() << std::endl;
296                                 std::cout << "Topic state: " << (int) it.getState() << std::endl;
297                             }
298                         }
299                     }
300                     break;
301                 }
302             case 5:
303                 {
304                     std::cout <<  "UpdateTopicList" << std::endl;
305                     std::shared_ptr<OIC::Service::NSProvider> provider =
306                         NSConsumerService::getInstance()->getProvider(mainProvider);
307                     if (provider != nullptr)
308                     {
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);
313
314                         provider->updateTopicList(topicList);
315                     }
316                     break;
317                 }
318             case 6:
319                 {
320                     std::cout << "Subscribe provider" << std::endl;
321                     if (!mainProvider.empty())
322                     {
323                         std::shared_ptr<OIC::Service::NSProvider> provider =
324                             NSConsumerService::getInstance()->getProvider(mainProvider);
325                         if (provider != nullptr )
326                         {
327                             std::cout << "calling Subscribe on discovered mainProvider" << std::endl;
328                             if (!provider->isSubscribed())
329                             {
330                                 std::cout << "start Subscribing" << std::endl;
331                                 provider->subscribe();
332                             }
333                         }
334                     }
335                     break;
336                 }
337             case 7:
338                 {
339                     std::cout << "UnSubscribe provider" << std::endl;
340                     if (!mainProvider.empty())
341                     {
342                         std::shared_ptr<OIC::Service::NSProvider> provider =
343                             NSConsumerService::getInstance()->getProvider(mainProvider);
344                         if (provider != nullptr )
345                         {
346                             std::cout << "calling UnSubscribe on discovered mainProvider" << std::endl;
347                             if (provider->isSubscribed())
348                             {
349                                 std::cout << "start UnSubscribing" << std::endl;
350                                 provider->unsubscribe();
351                             }
352                         }
353                     }
354                     break;
355                 }
356             case 8:
357                 {
358                     std::cout << "Rescan Provider" << std::endl;
359                     NSConsumerService::getInstance()->rescanProvider();
360                     break;
361                 }
362
363 #ifdef WITH_CLOUD
364             case 9:
365                 {
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);
370                     break;
371                 }
372 #endif
373             case 10:
374                 {
375                     std::cout << "Exit" << std::endl;
376                     NSConsumerService::getInstance()->stop();
377                     isExit = true;
378                     break;
379                 }
380             default:
381                 {
382                     std::cout << "Under Construction" << std::endl;
383                     std::cin.clear();
384                     std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
385                     break;
386                 }
387         }
388     }
389
390     return 0;
391 }