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;
32 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
33 const OCDevAddr& devAddr, const std::string& uri,
34 const std::string& serverId, bool observable,
35 const std::vector<std::string>& resourceTypes,
36 const std::vector<std::string>& interfaces)
37 : m_clientWrapper(clientWrapper), m_uri(uri),
38 m_resourceId(serverId, m_uri), m_devAddr(devAddr),
39 m_isObservable(observable), m_isCollection(false),
40 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
41 m_observeHandle(nullptr)
43 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
44 != m_interfaces.end();
47 resourceTypes.empty() ||
49 m_clientWrapper.expired())
51 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
52 interfaces.empty(), m_clientWrapper.expired(), false, false);
56 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
57 const std::string& host, const std::string& uri,
58 const std::string& serverId,
59 OCConnectivityType connectivityType, bool observable,
60 const std::vector<std::string>& resourceTypes,
61 const std::vector<std::string>& interfaces)
62 : m_clientWrapper(clientWrapper), m_uri(uri),
63 m_resourceId(serverId, m_uri),
64 m_devAddr{ OC_DEFAULT_ADAPTER },
65 m_isObservable(observable), m_isCollection(false),
66 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
67 m_observeHandle(nullptr)
69 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
70 != m_interfaces.end();
73 resourceTypes.empty() ||
75 m_clientWrapper.expired())
77 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
78 interfaces.empty(), m_clientWrapper.expired(), false, false);
81 // construct the devAddr from the pieces we have
82 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
83 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
84 size_t len = host.length();
85 if (len >= MAX_ADDR_STR_SIZE)
87 throw std::length_error("host address is too long.");
89 host.copy(m_devAddr.addr, len);
90 m_devAddr.addr[len] = '\0';
93 OCResource::~OCResource()
97 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
98 GetCallback attributeHandler, QualityOfService QoS)
100 return checked_guard(m_clientWrapper.lock(),
101 &IClientWrapper::GetResourceRepresentation,
103 queryParametersMap, m_headerOptions,
104 attributeHandler, QoS);
107 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
108 GetCallback attributeHandler)
110 QualityOfService defaultQos = OC::QualityOfService::NaQos;
111 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
112 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
115 OCStackResult OCResource::get(const std::string& resourceType,
116 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
117 GetCallback attributeHandler)
119 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
120 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
122 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
125 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
126 QualityOfService QoS)
128 QueryParamsMap mapCpy(queryParametersMap);
130 if(!resourceType.empty())
132 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
135 if(!resourceInterface.empty())
137 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
140 return result_guard(get(mapCpy, attributeHandler, QoS));
143 OCStackResult OCResource::put(const OCRepresentation& rep,
144 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
145 QualityOfService QoS)
147 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
148 m_devAddr, m_uri, rep, queryParametersMap,
149 m_headerOptions, attributeHandler, QoS);
152 OCStackResult OCResource::put(const OCRepresentation& rep,
153 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
155 QualityOfService defaultQos = OC::QualityOfService::NaQos;
156 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
157 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
160 OCStackResult OCResource::put(const std::string& resourceType,
161 const std::string& resourceInterface, const OCRepresentation& rep,
162 const QueryParamsMap& queryParametersMap,
163 PutCallback attributeHandler)
165 QualityOfService defaultQos = OC::QualityOfService::NaQos;
166 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
168 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
169 attributeHandler, defaultQos));
172 OCStackResult OCResource::put(const std::string& resourceType,
173 const std::string& resourceInterface, const OCRepresentation& rep,
174 const QueryParamsMap& queryParametersMap,
175 PutCallback attributeHandler,
176 QualityOfService QoS)
178 QueryParamsMap mapCpy(queryParametersMap);
180 if(!resourceType.empty())
182 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
185 if(!resourceInterface.empty())
187 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
190 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
193 OCStackResult OCResource::post(const OCRepresentation& rep,
194 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
195 QualityOfService QoS)
197 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
198 m_devAddr, m_uri, rep, queryParametersMap,
199 m_headerOptions, attributeHandler, QoS);
202 OCStackResult OCResource::post(const OCRepresentation& rep,
203 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
205 QualityOfService defaultQos = OC::QualityOfService::NaQos;
206 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
207 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
210 OCStackResult OCResource::post(const std::string& resourceType,
211 const std::string& resourceInterface, const OCRepresentation& rep,
212 const QueryParamsMap& queryParametersMap,
213 PostCallback attributeHandler)
215 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
216 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
218 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
222 OCStackResult OCResource::post(const std::string& resourceType,
223 const std::string& resourceInterface, const OCRepresentation& rep,
224 const QueryParamsMap& queryParametersMap,
225 PostCallback attributeHandler,
226 QualityOfService QoS)
228 QueryParamsMap mapCpy(queryParametersMap);
230 if(!resourceType.empty())
232 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
235 if(!resourceInterface.empty())
237 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
240 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
243 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
245 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
246 m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
249 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
251 QualityOfService defaultQos = OC::QualityOfService::NaQos;
252 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
254 return result_guard(deleteResource(deleteHandler, defaultQos));
257 OCStackResult OCResource::observe(ObserveType observeType,
258 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
259 QualityOfService QoS)
261 if(m_observeHandle != nullptr)
263 return result_guard(OC_STACK_INVALID_PARAM);
266 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
267 observeType, &m_observeHandle, m_devAddr,
268 m_uri, queryParametersMap, m_headerOptions,
269 observeHandler, QoS);
272 OCStackResult OCResource::observe(ObserveType observeType,
273 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
275 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
276 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
278 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
281 OCStackResult OCResource::cancelObserve()
283 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
284 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
285 return result_guard(cancelObserve(defaultQoS));
288 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
290 if(m_observeHandle == nullptr)
292 return result_guard(OC_STACK_INVALID_PARAM);
295 OCStackResult result = checked_guard(m_clientWrapper.lock(),
296 &IClientWrapper::CancelObserveResource,
297 m_observeHandle, "", m_uri, m_headerOptions, QoS);
299 if(result == OC_STACK_OK)
301 m_observeHandle = nullptr;
307 std::string OCResource::host() const
309 return std::string(m_devAddr.addr);
312 std::string OCResource::uri() const
317 OCConnectivityType OCResource::connectivityType() const
319 return static_cast<OCConnectivityType>(
320 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
323 bool OCResource::isObservable() const
325 return m_isObservable;
328 OCResourceIdentifier OCResource::uniqueIdentifier() const
333 std::string OCResource::sid() const
335 return this->uniqueIdentifier().m_representation;
338 bool OCResource::operator==(const OCResource &other) const
340 return m_resourceId == other.m_resourceId;
343 bool OCResource::operator!=(const OCResource &other) const
345 return m_resourceId != other.m_resourceId;
348 bool OCResource::operator<(const OCResource &other) const
350 return m_resourceId < other.m_resourceId;
353 bool OCResource::operator>(const OCResource &other) const
355 return m_resourceId > other.m_resourceId;
358 bool OCResource::operator<=(const OCResource &other) const
360 return m_resourceId <= other.m_resourceId;
363 bool OCResource::operator>=(const OCResource &other) const
365 return m_resourceId >= other.m_resourceId;
368 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
369 const std::string& resourceUri)
370 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
374 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
376 os << ri.m_representation<<ri.m_resourceUri;
381 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
383 return m_representation == other.m_representation
384 && m_resourceUri == other.m_resourceUri;
387 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
389 return !(*this == other);
392 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
394 return m_resourceUri < other.m_resourceUri
395 || (m_resourceUri == other.m_resourceUri &&
396 m_representation < other.m_representation);
399 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
401 return *this != other && !(*this<other);
404 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
406 return !(*this > other);
409 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
411 return !(*this < other);