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 static const char COAP[] = "coap://";
30 static const char COAPS[] = "coaps://";
33 static const char COAP_TCP[] = "coap+tcp://";
37 using OC::result_guard;
38 using OC::checked_guard;
40 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
41 const OCDevAddr& devAddr, const std::string& uri,
42 const std::string& serverId, bool observable,
43 const std::vector<std::string>& resourceTypes,
44 const std::vector<std::string>& interfaces)
45 : m_clientWrapper(clientWrapper), m_uri(uri),
46 m_resourceId(serverId, m_uri), m_devAddr(devAddr),
47 m_isObservable(observable), m_isCollection(false),
48 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
49 m_observeHandle(nullptr)
51 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
52 != m_interfaces.end();
55 resourceTypes.empty() ||
57 m_clientWrapper.expired())
59 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
60 interfaces.empty(), m_clientWrapper.expired(), false, false);
64 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
65 const std::string& host, const std::string& uri,
66 const std::string& serverId,
67 OCConnectivityType connectivityType, bool observable,
68 const std::vector<std::string>& resourceTypes,
69 const std::vector<std::string>& interfaces)
70 : m_clientWrapper(clientWrapper), m_uri(uri),
71 m_resourceId(serverId, m_uri),
72 m_devAddr{ OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0 },
73 m_isObservable(observable), m_isCollection(false),
74 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
75 m_observeHandle(nullptr)
77 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
78 != m_interfaces.end();
81 resourceTypes.empty() ||
83 m_clientWrapper.expired())
85 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
86 interfaces.empty(), m_clientWrapper.expired(), false, false);
89 if (uri.length() == 1 && uri[0] == '/')
91 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
92 interfaces.empty(), m_clientWrapper.expired(), false, false);
97 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
98 interfaces.empty(), m_clientWrapper.expired(), false, false);
101 // construct the devAddr from the pieces we have
102 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
103 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
104 size_t len = host.length();
105 if (len >= MAX_ADDR_STR_SIZE)
107 throw std::length_error("host address is too long.");
113 OCResource::~OCResource()
117 void OCResource::setHost(const std::string& host)
121 if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
123 prefix_len = sizeof(COAP) - 1;
125 else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
127 prefix_len = sizeof(COAPS) - 1;
128 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
131 else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
133 prefix_len = sizeof(COAP_TCP) - 1;
134 m_devAddr.adapter = static_cast<OCTransportAdapter>(m_devAddr.adapter & OC_ADAPTER_TCP);
139 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
140 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
143 // removed coap:// or coaps:// or coap+tcp://
144 std::string host_token = host.substr(prefix_len);
146 if(host_token[0] == '[')
148 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
150 size_t found = host_token.find(']');
152 if(found == std::string::npos || found == 0)
154 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
155 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
157 // extract the ipaddress
158 std::string ip6Addr = host_token.substr(1, found-1);
159 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
160 m_devAddr.addr[ip6Addr.length()] = '\0';
161 //skip ']' and ':' characters in host string
162 host_token = host_token.substr(found + 2);
166 size_t found = host_token.find(':');
168 if(found == std::string::npos || found == 0)
170 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
171 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
174 std::string addrPart = host_token.substr(0, found);
175 addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
176 m_devAddr.addr[addrPart.length()] = '\0';
177 //skip ':' character in host string
178 host_token = host_token.substr(found + 1);
181 int port = std::stoi(host_token);
183 if( port < 0 || port > UINT16_MAX )
185 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
186 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
189 m_devAddr.port = static_cast<uint16_t>(port);
193 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
194 GetCallback attributeHandler, QualityOfService QoS)
196 return checked_guard(m_clientWrapper.lock(),
197 &IClientWrapper::GetResourceRepresentation,
199 queryParametersMap, m_headerOptions,
200 attributeHandler, QoS);
203 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
204 GetCallback attributeHandler)
206 QualityOfService defaultQos = OC::QualityOfService::NaQos;
207 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
208 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
211 OCStackResult OCResource::get(const std::string& resourceType,
212 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
213 GetCallback attributeHandler)
215 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
216 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
218 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
221 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
222 QualityOfService QoS)
224 QueryParamsMap mapCpy(queryParametersMap);
226 if(!resourceType.empty())
228 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
231 if(!resourceInterface.empty())
233 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
236 return result_guard(get(mapCpy, attributeHandler, QoS));
239 OCStackResult OCResource::put(const OCRepresentation& rep,
240 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
241 QualityOfService QoS)
243 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
244 m_devAddr, m_uri, rep, queryParametersMap,
245 m_headerOptions, attributeHandler, QoS);
248 OCStackResult OCResource::put(const OCRepresentation& rep,
249 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
251 QualityOfService defaultQos = OC::QualityOfService::NaQos;
252 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
253 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
256 OCStackResult OCResource::put(const std::string& resourceType,
257 const std::string& resourceInterface, const OCRepresentation& rep,
258 const QueryParamsMap& queryParametersMap,
259 PutCallback attributeHandler)
261 QualityOfService defaultQos = OC::QualityOfService::NaQos;
262 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
264 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
265 attributeHandler, defaultQos));
268 OCStackResult OCResource::put(const std::string& resourceType,
269 const std::string& resourceInterface, const OCRepresentation& rep,
270 const QueryParamsMap& queryParametersMap,
271 PutCallback attributeHandler,
272 QualityOfService QoS)
274 QueryParamsMap mapCpy(queryParametersMap);
276 if(!resourceType.empty())
278 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
281 if(!resourceInterface.empty())
283 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
286 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
289 OCStackResult OCResource::post(const OCRepresentation& rep,
290 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
291 QualityOfService QoS)
293 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
294 m_devAddr, m_uri, rep, queryParametersMap,
295 m_headerOptions, attributeHandler, QoS);
298 OCStackResult OCResource::post(const OCRepresentation& rep,
299 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
301 QualityOfService defaultQos = OC::QualityOfService::NaQos;
302 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
303 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
306 OCStackResult OCResource::post(const std::string& resourceType,
307 const std::string& resourceInterface, const OCRepresentation& rep,
308 const QueryParamsMap& queryParametersMap,
309 PostCallback attributeHandler)
311 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
312 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
314 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
318 OCStackResult OCResource::post(const std::string& resourceType,
319 const std::string& resourceInterface, const OCRepresentation& rep,
320 const QueryParamsMap& queryParametersMap,
321 PostCallback attributeHandler,
322 QualityOfService QoS)
324 QueryParamsMap mapCpy(queryParametersMap);
326 if(!resourceType.empty())
328 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
331 if(!resourceInterface.empty())
333 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
336 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
339 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
341 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
342 m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
345 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
347 QualityOfService defaultQos = OC::QualityOfService::NaQos;
348 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
350 return result_guard(deleteResource(deleteHandler, defaultQos));
353 OCStackResult OCResource::observe(ObserveType observeType,
354 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
355 QualityOfService QoS)
357 if(m_observeHandle != nullptr)
359 return result_guard(OC_STACK_INVALID_PARAM);
362 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
363 observeType, &m_observeHandle, m_devAddr,
364 m_uri, queryParametersMap, m_headerOptions,
365 observeHandler, QoS);
368 OCStackResult OCResource::observe(ObserveType observeType,
369 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
371 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
372 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
374 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
377 OCStackResult OCResource::cancelObserve()
379 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
380 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
381 return result_guard(cancelObserve(defaultQoS));
384 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
386 if(m_observeHandle == nullptr)
388 return result_guard(OC_STACK_INVALID_PARAM);
391 OCStackResult result = checked_guard(m_clientWrapper.lock(),
392 &IClientWrapper::CancelObserveResource,
393 m_observeHandle, "", m_uri, m_headerOptions, QoS);
395 if(result == OC_STACK_OK)
397 m_observeHandle = nullptr;
403 std::string OCResource::host() const
405 std::ostringstream ss;
406 if (m_devAddr.flags & OC_SECURE)
411 else if (m_devAddr.adapter & OC_ADAPTER_TCP)
420 if (m_devAddr.flags & OC_IP_USE_V6)
422 ss << '[' << m_devAddr.addr << ']';
426 ss << m_devAddr.addr;
430 ss << ':' << m_devAddr.port;
435 std::string OCResource::uri() const
440 OCConnectivityType OCResource::connectivityType() const
442 return static_cast<OCConnectivityType>(
443 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
446 bool OCResource::isObservable() const
448 return m_isObservable;
451 OCResourceIdentifier OCResource::uniqueIdentifier() const
456 std::string OCResource::sid() const
458 return this->uniqueIdentifier().m_representation;
461 bool OCResource::operator==(const OCResource &other) const
463 return m_resourceId == other.m_resourceId;
466 bool OCResource::operator!=(const OCResource &other) const
468 return m_resourceId != other.m_resourceId;
471 bool OCResource::operator<(const OCResource &other) const
473 return m_resourceId < other.m_resourceId;
476 bool OCResource::operator>(const OCResource &other) const
478 return m_resourceId > other.m_resourceId;
481 bool OCResource::operator<=(const OCResource &other) const
483 return m_resourceId <= other.m_resourceId;
486 bool OCResource::operator>=(const OCResource &other) const
488 return m_resourceId >= other.m_resourceId;
491 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
492 const std::string& resourceUri)
493 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
497 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
499 os << ri.m_representation<<ri.m_resourceUri;
504 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
506 return m_representation == other.m_representation
507 && m_resourceUri == other.m_resourceUri;
510 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
512 return !(*this == other);
515 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
517 return m_resourceUri < other.m_resourceUri
518 || (m_resourceUri == other.m_resourceUri &&
519 m_representation < other.m_representation);
522 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
524 return *this != other && !(*this<other);
527 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
529 return !(*this > other);
532 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
534 return !(*this < other);