Modify Callback function in cpp wrapper for Notification Service.
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / examples / linux / notificationserviceconsumer.cpp
1 /******************************************************************\r
2  *\r
3  * Copyright 2016 Samsung Electronics All Rights Reserved.\r
4  *\r
5  *\r
6  *\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  ******************************************************************/\r
20 \r
21 #include <iostream>\r
22 \r
23 #include <unistd.h>\r
24 #include "NSConsumerService.h"\r
25 #include "NSMessage.h"\r
26 #include "NSProvider.h"\r
27 #include "ocstack.h"\r
28 \r
29 #define TAG "NotiConsumerWrapperExample"\r
30 using namespace std;\r
31 using namespace OIC::Service;\r
32 \r
33 bool isExit = false;\r
34 std::string REMOTE_SERVER_ADDRESS;\r
35 \r
36 void onNotificationPostedCb(OIC::Service::NSMessage *notification)\r
37 {\r
38     std::cout << "id : " << notification->getMessageId() << std::endl;\r
39     std::cout << "title : " << notification->getTitle() << std::endl;\r
40     std::cout << "content : " <<  notification->getContentText() << std::endl;\r
41     std::cout << "source : " <<  notification->getSourceName() << std::endl;\r
42 }\r
43 \r
44 void onNotificationSyncCb(OIC::Service::NSSyncInfo *sync)\r
45 {\r
46     std::cout << "Sync ID : " <<  sync->getMessageId() << std::endl;\r
47     std::cout << "Sync STATE : " << (int) sync->getState() << std::endl;\r
48 }\r
49 \r
50 void onDiscoverNotificationCb(OIC::Service::NSProvider *provider)\r
51 {\r
52     std::cout << "notification resource discovered" << std::endl;\r
53     provider->subscribe();\r
54     std::cout << "startSubscribing" << std::endl;\r
55 }\r
56 \r
57 void onProviderChangedCb(OIC::Service::NSProvider *provider,OIC::Service::NSResponse response)\r
58 {\r
59     std::cout << "Subscription accepted" << std::endl;\r
60     std::cout << "subscribed provider Id : " << provider->getProviderId() << std::endl;\r
61     if(response == OIC::Service::NSResponse::ALLOW)\r
62         provider->setListener(onNotificationPostedCb, onNotificationSyncCb);\r
63 }\r
64 \r
65 void *OCProcessThread(void *ptr)\r
66 {\r
67     (void) ptr;\r
68 \r
69     while (!isExit)\r
70     {\r
71         usleep(2000);\r
72         if (OCProcess() != OC_STACK_OK)\r
73         {\r
74             OCStop();\r
75             break;\r
76         }\r
77     }\r
78 \r
79     return NULL;\r
80 }\r
81 \r
82 int main(void)\r
83 {\r
84     pthread_t OCThread;\r
85 \r
86     std::cout << "start Iotivity" << std::endl;\r
87     if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)\r
88     {\r
89         std::cout << "OCInit fail" << std::endl;\r
90         return 0;\r
91     }\r
92 \r
93     NSConsumerService::ConsumerConfig cfg;\r
94     cfg.m_discoverCb = onDiscoverNotificationCb;\r
95     cfg.m_changedCb = onProviderChangedCb;\r
96 \r
97     pthread_create(&OCThread, NULL, OCProcessThread, NULL);\r
98 \r
99     std::cout << "Start notification consumer service" << std::endl;\r
100     while (!isExit)\r
101     {\r
102         int num;\r
103 \r
104         std::cout << "1. Start Consumer" << std::endl;\r
105         std::cout << "2. Stop Consumer" << std::endl;\r
106 #ifdef WITH_CLOUD\r
107         std::cout << "3. Enable  NS Consumer RemoteService" << std::endl;\r
108 #endif\r
109         std::cout << "5. Exit" << std::endl;\r
110 \r
111         std::cout << "Input: " << std::endl;\r
112         std::cin >> num;\r
113         switch (num)\r
114         {\r
115             case 1:\r
116                 std::cout << "1. Start the Notification Consumer" << std::endl;\r
117                 NSConsumerService::getInstance()->Start(cfg);\r
118                 break;\r
119             case 2:\r
120                 std::cout << "2. Stop the Notification Consumer" << std::endl;\r
121                 NSConsumerService::getInstance()->Stop();\r
122                 break;\r
123 #ifdef WITH_CLOUD\r
124             case 3:\r
125                 {\r
126                     std::cout << "3. Enable NS Consumer RemoteService" << std::endl;\r
127                     std::cout << "Input the Server Address :";\r
128                     std::cin >> REMOTE_SERVER_ADDRESS;\r
129                     NSConsumerService::getInstance()->EnableRemoteService(REMOTE_SERVER_ADDRESS);\r
130                     break;\r
131                 }\r
132 #endif\r
133             case 5:\r
134                 std::cout << "5. Exit" << std::endl;\r
135                 isExit = true;\r
136                 break;\r
137             default:\r
138                 break;\r
139         }\r
140     }\r
141 \r
142     return 0;\r
143 }\r