1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 #include "OCResource.h"
22 #include "OCUtilities.h"
24 #include <boost/lexical_cast.hpp>
29 using OC::result_guard;
30 using OC::checked_guard;
33 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper, const std::string& host,
34 const std::string& uri, const std::string& serverId,
35 uint8_t connectivityType, bool observable,
36 const std::vector<std::string>& resourceTypes,
37 const std::vector<std::string>& interfaces)
38 : m_clientWrapper(clientWrapper), m_uri(uri), m_resourceId(serverId, m_uri),
39 m_connectivityType(connectivityType),
40 m_host(host), m_isObservable(observable),
41 m_isCollection(false), m_resourceTypes(resourceTypes), m_interfaces(interfaces),
42 m_observeHandle(nullptr)
44 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
45 != m_interfaces.end();
48 resourceTypes.empty() ||
50 m_clientWrapper.expired())
52 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
53 interfaces.empty(), m_clientWrapper.expired(), false, false);
57 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper, const std::string& host,
58 const std::string& uri, const std::string& serverId, bool observable,
59 const std::vector<std::string>& resourceTypes,
60 const std::vector<std::string>& interfaces)
61 : m_clientWrapper(clientWrapper), m_uri(uri), m_resourceId(serverId, m_uri), m_host(host),
62 m_isObservable(observable), m_isCollection(false), m_resourceTypes(resourceTypes),
63 m_interfaces(interfaces), m_observeHandle(nullptr)
65 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
66 != m_interfaces.end();
69 resourceTypes.empty() ||
71 m_clientWrapper.expired())
73 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
74 interfaces.empty(), m_clientWrapper.expired(), false, false);
79 OCResource::~OCResource()
83 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
84 GetCallback attributeHandler, QualityOfService QoS)
87 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
88 m_host, m_uri, m_connectivityType, queryParametersMap, m_headerOptions,
89 attributeHandler, QoS);
91 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
92 m_host, m_uri, queryParametersMap, m_headerOptions, attributeHandler, QoS);
96 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
97 GetCallback attributeHandler)
99 QualityOfService defaultQos = OC::QualityOfService::NaQos;
100 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
101 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
104 OCStackResult OCResource::get(const std::string& resourceType,
105 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
106 GetCallback attributeHandler)
108 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
109 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
111 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
114 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
115 QualityOfService QoS)
117 QueryParamsMap mapCpy(queryParametersMap);
119 if(!resourceType.empty())
121 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
124 if(!resourceInterface.empty())
126 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
129 return result_guard(get(mapCpy, attributeHandler, QoS));
132 OCStackResult OCResource::put(const OCRepresentation& rep,
133 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
134 QualityOfService QoS)
137 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
138 m_host, m_uri, m_connectivityType, rep, queryParametersMap,
139 m_headerOptions, attributeHandler, QoS);
141 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
142 m_host, m_uri, rep, queryParametersMap, m_headerOptions, attributeHandler, QoS);
146 OCStackResult OCResource::put(const OCRepresentation& rep,
147 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
149 QualityOfService defaultQos = OC::QualityOfService::NaQos;
150 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
151 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
154 OCStackResult OCResource::put(const std::string& resourceType,
155 const std::string& resourceInterface, const OCRepresentation& rep,
156 const QueryParamsMap& queryParametersMap,
157 PutCallback attributeHandler)
159 QualityOfService defaultQos = OC::QualityOfService::NaQos;
160 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
162 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
163 attributeHandler, defaultQos));
166 OCStackResult OCResource::put(const std::string& resourceType,
167 const std::string& resourceInterface, const OCRepresentation& rep,
168 const QueryParamsMap& queryParametersMap,
169 PutCallback attributeHandler,
170 QualityOfService QoS)
172 QueryParamsMap mapCpy(queryParametersMap);
174 if(!resourceType.empty())
176 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
179 if(!resourceInterface.empty())
181 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
184 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
187 OCStackResult OCResource::post(const OCRepresentation& rep,
188 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
189 QualityOfService QoS)
192 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
193 m_host, m_uri, m_connectivityType, rep, queryParametersMap,
194 m_headerOptions, attributeHandler, QoS);
196 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
197 m_host, m_uri, rep, queryParametersMap, m_headerOptions, attributeHandler, QoS);
201 OCStackResult OCResource::post(const OCRepresentation& rep,
202 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
204 QualityOfService defaultQos = OC::QualityOfService::NaQos;
205 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
206 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
209 OCStackResult OCResource::post(const std::string& resourceType,
210 const std::string& resourceInterface, const OCRepresentation& rep,
211 const QueryParamsMap& queryParametersMap,
212 PostCallback attributeHandler)
214 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
215 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
217 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
221 OCStackResult OCResource::post(const std::string& resourceType,
222 const std::string& resourceInterface, const OCRepresentation& rep,
223 const QueryParamsMap& queryParametersMap,
224 PostCallback attributeHandler,
225 QualityOfService QoS)
227 QueryParamsMap mapCpy(queryParametersMap);
229 if(!resourceType.empty())
231 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
234 if(!resourceInterface.empty())
236 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
239 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
242 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
245 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
246 m_host, m_uri, m_connectivityType, m_headerOptions, deleteHandler, QoS);
248 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
249 m_host, m_uri, m_headerOptions, deleteHandler, QoS);
253 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
255 QualityOfService defaultQos = OC::QualityOfService::NaQos;
256 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
258 return result_guard(deleteResource(deleteHandler, defaultQos));
261 OCStackResult OCResource::observe(ObserveType observeType,
262 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
263 QualityOfService QoS)
265 if(m_observeHandle != nullptr)
267 return result_guard(OC_STACK_INVALID_PARAM);
271 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
272 observeType, &m_observeHandle, m_host,
273 m_uri, m_connectivityType, queryParametersMap, m_headerOptions,
274 observeHandler, QoS);
276 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
277 observeType, &m_observeHandle, m_host,
278 m_uri, queryParametersMap, m_headerOptions, observeHandler, QoS);
282 OCStackResult OCResource::observe(ObserveType observeType,
283 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
285 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
286 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
288 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
291 OCStackResult OCResource::cancelObserve()
293 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
294 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
295 return result_guard(cancelObserve(defaultQoS));
298 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
300 if(m_observeHandle == nullptr)
302 return result_guard(OC_STACK_INVALID_PARAM);
305 OCStackResult result = checked_guard(m_clientWrapper.lock(),
306 &IClientWrapper::CancelObserveResource,
307 m_observeHandle, m_host, m_uri, m_headerOptions, QoS);
309 if(result == OC_STACK_OK)
311 m_observeHandle = nullptr;
317 std::string OCResource::host() const
322 std::string OCResource::uri() const
328 uint8_t OCResource::connectivityType() const
330 return m_connectivityType;
334 bool OCResource::isObservable() const
336 return m_isObservable;
340 OCResourceIdentifier OCResource::uniqueIdentifier() const
345 std::string OCResource::sid() const
347 std::ostringstream os;
348 os << this->uniqueIdentifier().m_representation;
352 bool OCResource::operator==(const OCResource &other) const
354 return m_resourceId == other.m_resourceId;
357 bool OCResource::operator!=(const OCResource &other) const
359 return m_resourceId != other.m_resourceId;
362 bool OCResource::operator<(const OCResource &other) const
364 return m_resourceId < other.m_resourceId;
367 bool OCResource::operator>(const OCResource &other) const
369 return m_resourceId > other.m_resourceId;
372 bool OCResource::operator<=(const OCResource &other) const
374 return m_resourceId <= other.m_resourceId;
377 bool OCResource::operator>=(const OCResource &other) const
379 return m_resourceId >= other.m_resourceId;
382 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
383 const std::string& resourceUri)
384 :m_representation(0), m_resourceUri(resourceUri)
386 // test required so we can create Resources without a server. Will leave as default.
387 if(!wireServerIdentifier.empty())
389 m_representation = boost::lexical_cast<unsigned int>(wireServerIdentifier);
393 ostream& operator <<(ostream& os, const OCResourceIdentifier& ri)
396 os << ri.m_representation<<ri.m_resourceUri;
401 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
403 return m_representation == other.m_representation
404 && m_resourceUri == other.m_resourceUri;
407 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
409 return !(*this == other);
412 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
414 return m_resourceUri < other.m_resourceUri
415 || (m_resourceUri == other.m_resourceUri &&
416 m_representation < other.m_representation);
419 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
421 return *this != other && !(*this<other);
424 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
426 return !(*this > other);
429 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
431 return !(*this < other);