1 /******************************************************************
\r
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
19 ******************************************************************/
\r
24 #include "NSConsumerService.h"
\r
25 #include "NSMessage.h"
\r
26 #include "NSProvider.h"
\r
27 #include "ocstack.h"
\r
29 #define TAG "NotiConsumerWrapperExample"
\r
30 using namespace std;
\r
31 using namespace OIC::Service;
\r
33 bool isExit = false;
\r
34 std::string REMOTE_SERVER_ADDRESS;
\r
36 void onNotificationPostedCb(OIC::Service::NSMessage *notification)
\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
44 void onNotificationSyncCb(OIC::Service::NSSyncInfo *sync)
\r
46 std::cout << "Sync ID : " << sync->getMessageId() << std::endl;
\r
47 std::cout << "Sync STATE : " << (int) sync->getState() << std::endl;
\r
50 void onDiscoverNotificationCb(OIC::Service::NSProvider *provider)
\r
52 std::cout << "notification resource discovered" << std::endl;
\r
53 provider->subscribe();
\r
54 std::cout << "startSubscribing" << std::endl;
\r
57 void onSubscriptionAcceptedCb(OIC::Service::NSProvider *provider)
\r
59 std::cout << "Subscription accepted" << std::endl;
\r
60 std::cout << "subscribed provider Id : " << provider->getProviderId() << std::endl;
\r
61 provider->setListener(onNotificationPostedCb, onNotificationSyncCb);
\r
64 void *OCProcessThread(void *ptr)
\r
71 if (OCProcess() != OC_STACK_OK)
\r
85 std::cout << "start Iotivity" << std::endl;
\r
86 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
\r
88 std::cout << "OCInit fail" << std::endl;
\r
92 NSConsumerService::ConsumerConfig cfg;
\r
93 cfg.m_discoverCb = onDiscoverNotificationCb;
\r
94 cfg.m_acceptedCb = onSubscriptionAcceptedCb;
\r
96 pthread_create(&OCThread, NULL, OCProcessThread, NULL);
\r
98 std::cout << "Start notification consumer service" << std::endl;
\r
103 std::cout << "1. Start Consumer" << std::endl;
\r
104 std::cout << "2. Stop Consumer" << std::endl;
\r
105 std::cout << "3. Enable NS Consumer RemoteService" << std::endl;
\r
106 std::cout << "5. Exit" << std::endl;
\r
108 std::cout << "Input: " << std::endl;
\r
113 std::cout << "1. Start the Notification Consumer" << std::endl;
\r
114 NSConsumerService::getInstance()->Start(cfg);
\r
117 std::cout << "2. Stop the Notification Consumer" << std::endl;
\r
118 NSConsumerService::getInstance()->Stop();
\r
122 std::cout << "3. Enable NS Consumer RemoteService" << std::endl;
\r
123 std::cout << "Input the Server Address :";
\r
124 std::cin >> REMOTE_SERVER_ADDRESS;
\r
125 NSConsumerService::getInstance()->EnableRemoteService(REMOTE_SERVER_ADDRESS);
\r
129 std::cout << "5. Exit" << std::endl;
\r