1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #ifndef _NS_CONSUMER_SERVICE_SIMULATOR_H_
22 #define _NS_CONSUMER_SERVICE_SIMULATOR_H_
26 #include "OCPlatform.h"
29 class NSConsumerSimulator
32 std::function<void(const int &, const std::string &, const std::string &,
33 const std::string &)> m_messageFunc;
34 std::function<void(const int &, const int &)> m_syncFunc;
36 std::shared_ptr<OC::OCResource> m_syncResource;
37 std::shared_ptr<OC::OCResource> m_msgResource;
38 std::shared_ptr<OC::OCResource> m_topicResource;
42 : m_messageFunc(), m_syncFunc(),
46 ~NSConsumerSimulator() = default;
48 NSConsumerSimulator(const NSConsumerSimulator &) = delete;
49 NSConsumerSimulator &operator = (const NSConsumerSimulator &) = delete;
51 NSConsumerSimulator(NSConsumerSimulator &&) = delete;
52 NSConsumerSimulator &operator = (NSConsumerSimulator &&) = delete;
56 OC::OCPlatform::findResource("", std::string("/oic/res?rt=x.org.iotivity.notification"),
57 OCConnectivityType::CT_DEFAULT,
58 std::bind(&NSConsumerSimulator::findResultCallback, this, std::placeholders::_1),
59 OC::QualityOfService::LowQos);
62 void syncToProvider(int &type, const int &id, const std::string &providerID)
64 if (m_syncResource == nullptr)
69 OC::OCRepresentation rep;
70 rep.setValue("x.org.iotivity.ns.providerid", providerID);
71 rep.setValue("x.org.iotivity.ns.messageid", id);
72 rep.setValue("x.org.iotivity.ns.state", type);
74 m_syncResource->post(rep, OC::QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
79 if ((msgResourceCancelObserve(OC::QualityOfService::HighQos) == OC_STACK_OK) &&
80 (syncResourceCancelObserve(OC::QualityOfService::HighQos) == OC_STACK_OK))
87 void setCallback(std::function<void(const int &, const std::string &,
88 const std::string &, const std::string &)> messageFunc,
89 const std::function<void(const int &, const int &)> &syncFunc)
91 m_messageFunc = messageFunc;
92 m_syncFunc = syncFunc;
96 static void onPost(const OC::HeaderOptions &/*headerOption*/,
97 const OC::OCRepresentation & /*rep*/ , const int /*eCode*/)
100 void findResultCallback(std::shared_ptr<OC::OCResource> resource)
104 if (resource->uri() == "/notification")
106 resource->get(OC::QueryParamsMap(),
107 std::bind(&NSConsumerSimulator::onGet, this,
108 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
109 resource), OC::QualityOfService::LowQos);
112 void onGet(const OC::HeaderOptions &/*headerOption*/,
113 const OC::OCRepresentation & /*rep*/ , const int /*eCode*/,
114 std::shared_ptr<OC::OCResource> resource)
117 OC::QueryParamsMap map;
118 map.insert(std::pair<std::string, std::string>(std::string("x.org.iotivity.ns.consumerid"),
119 std::string("123456789012345678901234567890123456")));
124 std::vector<std::string> rts{"x.org.iotivity.notification"};
127 = OC::OCPlatform::constructResourceObject(
128 std::string(resource->host()), std::string(resource->uri() + "/message"),
129 OCConnectivityType(resource->connectivityType()), true, rts,
130 std::vector<std::string>(resource->getResourceInterfaces()));
132 m_msgResource->observe(OC::ObserveType::Observe, map,
133 std::bind(&NSConsumerSimulator::onObserve, this,
134 std::placeholders::_1, std::placeholders::_2,
135 std::placeholders::_3, std::placeholders::_4, resource),
136 OC::QualityOfService::LowQos);
138 catch (std::exception &e)
140 std::cout << "OC::ResoureInitException : " << e.what() << std::endl;
143 = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/sync",
144 resource->connectivityType(), true, resource->getResourceTypes(),
145 resource->getResourceInterfaces());
147 m_syncResource->observe(OC::ObserveType::Observe, map,
148 std::bind(&NSConsumerSimulator::onObserve, this,
149 std::placeholders::_1, std::placeholders::_2,
150 std::placeholders::_3, std::placeholders::_4, resource),
151 OC::QualityOfService::LowQos);
154 = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/topic",
155 resource->connectivityType(), true, resource->getResourceTypes(),
156 resource->getResourceInterfaces());
158 void onObserve(const OC::HeaderOptions &/*headerOption*/,
159 const OC::OCRepresentation &rep , const int & /*eCode*/, const int &,
160 std::shared_ptr<OC::OCResource> )
163 if (rep.getUri() == "/notification/message" && rep.hasAttribute("x.org.iotivity.ns.messageid")
164 && rep.getValue<int>("x.org.iotivity.ns.messageid") != 1)
166 m_messageFunc(int(rep.getValue<int>("x.org.iotivity.ns.messageid")),
167 std::string(rep.getValueToString("x.org.iotivity.ns.title")),
168 std::string(rep.getValueToString("x.org.iotivity.ns.contenttext")),
169 std::string(rep.getValueToString(".x.org.iotivity.ns.source")));
170 if (rep.getValue<int>("x.org.iotivity.ns.messageid") == 3)
172 m_topicResource->get(OC::QueryParamsMap(),
173 std::bind(&NSConsumerSimulator::onTopicGet, this, std::placeholders::_1,
174 std::placeholders::_2, std::placeholders::_3, m_topicResource),
175 OC::QualityOfService::LowQos);
178 else if (rep.getUri() == "/notification/sync")
180 m_syncFunc(int(rep.getValue<int>("x.org.iotivity.ns.state")), int(rep.getValue<int>("x.org.iotivity.ns.messageid")));
183 void onTopicGet(const OC::HeaderOptions &/*headerOption*/,
184 const OC::OCRepresentation & /*rep*/ , const int /*eCode*/,
185 std::shared_ptr<OC::OCResource> /*resource*/)
189 OCStackResult msgResourceCancelObserve(OC::QualityOfService qos)
191 return m_msgResource->cancelObserve(qos);
194 OCStackResult syncResourceCancelObserve(OC::QualityOfService qos)
196 return m_syncResource->cancelObserve(qos);
201 #endif //_NS_CONSUMER_SERVICE_SIMULATOR_H_