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);
108 OCResource::~OCResource()
112 void OCResource::setHost(const std::string& host)
116 if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
118 prefix_len = sizeof(COAP) - 1;
120 else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
122 prefix_len = sizeof(COAPS) - 1;
123 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
126 else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
128 prefix_len = sizeof(COAP_TCP) - 1;
129 m_devAddr.adapter = static_cast<OCTransportAdapter>(m_devAddr.adapter & OC_ADAPTER_TCP);
134 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
135 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
138 // removed coap:// or coaps:// or coap+tcp://
139 std::string host_token = host.substr(prefix_len);
141 if(host_token[0] == '[')
143 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
145 size_t found = host_token.find(']');
147 if(found == std::string::npos || found == 0)
149 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
150 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
152 // extract the ipaddress
153 std::string ip6Addr = host_token.substr(1, found-1);
155 if (ip6Addr.length() >= MAX_ADDR_STR_SIZE)
157 throw std::length_error("host address is too long.");
160 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
161 m_devAddr.addr[ip6Addr.length()] = '\0';
162 //skip ']' and ':' characters in host string
163 host_token = host_token.substr(found + 2);
167 size_t found = host_token.find(':');
169 if(found == std::string::npos || found == 0)
171 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
172 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
175 std::string addrPart = host_token.substr(0, found);
177 if (addrPart.length() >= MAX_ADDR_STR_SIZE)
179 throw std::length_error("host address is too long.");
182 addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
183 m_devAddr.addr[addrPart.length()] = '\0';
184 //skip ':' character in host string
185 host_token = host_token.substr(found + 1);
188 int port = std::stoi(host_token);
190 if( port < 0 || port > UINT16_MAX )
192 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
193 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
196 m_devAddr.port = static_cast<uint16_t>(port);
200 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
201 GetCallback attributeHandler, QualityOfService QoS)
203 return checked_guard(m_clientWrapper.lock(),
204 &IClientWrapper::GetResourceRepresentation,
206 queryParametersMap, m_headerOptions,
207 attributeHandler, QoS);
210 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
211 GetCallback attributeHandler)
213 QualityOfService defaultQos = OC::QualityOfService::NaQos;
214 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
215 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
218 OCStackResult OCResource::get(const std::string& resourceType,
219 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
220 GetCallback attributeHandler)
222 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
223 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
225 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
228 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
229 QualityOfService QoS)
231 QueryParamsMap mapCpy(queryParametersMap);
233 if(!resourceType.empty())
235 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
238 if(!resourceInterface.empty())
240 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
243 return result_guard(get(mapCpy, attributeHandler, QoS));
246 OCStackResult OCResource::put(const OCRepresentation& rep,
247 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
248 QualityOfService QoS)
250 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
251 m_devAddr, m_uri, rep, queryParametersMap,
252 m_headerOptions, attributeHandler, QoS);
255 OCStackResult OCResource::put(const OCRepresentation& rep,
256 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
258 QualityOfService defaultQos = OC::QualityOfService::NaQos;
259 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
260 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
263 OCStackResult OCResource::put(const std::string& resourceType,
264 const std::string& resourceInterface, const OCRepresentation& rep,
265 const QueryParamsMap& queryParametersMap,
266 PutCallback attributeHandler)
268 QualityOfService defaultQos = OC::QualityOfService::NaQos;
269 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
271 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
272 attributeHandler, defaultQos));
275 OCStackResult OCResource::put(const std::string& resourceType,
276 const std::string& resourceInterface, const OCRepresentation& rep,
277 const QueryParamsMap& queryParametersMap,
278 PutCallback attributeHandler,
279 QualityOfService QoS)
281 QueryParamsMap mapCpy(queryParametersMap);
283 if(!resourceType.empty())
285 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
288 if(!resourceInterface.empty())
290 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
293 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
296 OCStackResult OCResource::post(const OCRepresentation& rep,
297 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
298 QualityOfService QoS)
300 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
301 m_devAddr, m_uri, rep, queryParametersMap,
302 m_headerOptions, attributeHandler, QoS);
305 OCStackResult OCResource::post(const OCRepresentation& rep,
306 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
308 QualityOfService defaultQos = OC::QualityOfService::NaQos;
309 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
310 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
313 OCStackResult OCResource::post(const std::string& resourceType,
314 const std::string& resourceInterface, const OCRepresentation& rep,
315 const QueryParamsMap& queryParametersMap,
316 PostCallback attributeHandler)
318 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
319 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
321 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
325 OCStackResult OCResource::post(const std::string& resourceType,
326 const std::string& resourceInterface, const OCRepresentation& rep,
327 const QueryParamsMap& queryParametersMap,
328 PostCallback attributeHandler,
329 QualityOfService QoS)
331 QueryParamsMap mapCpy(queryParametersMap);
333 if(!resourceType.empty())
335 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
338 if(!resourceInterface.empty())
340 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
343 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
346 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
348 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
349 m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
352 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
354 QualityOfService defaultQos = OC::QualityOfService::NaQos;
355 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
357 return result_guard(deleteResource(deleteHandler, defaultQos));
360 OCStackResult OCResource::observe(ObserveType observeType,
361 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
362 QualityOfService QoS)
364 if(m_observeHandle != nullptr)
366 return result_guard(OC_STACK_INVALID_PARAM);
369 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
370 observeType, &m_observeHandle, m_devAddr,
371 m_uri, queryParametersMap, m_headerOptions,
372 observeHandler, QoS);
375 OCStackResult OCResource::observe(ObserveType observeType,
376 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
378 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
379 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
381 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
384 OCStackResult OCResource::cancelObserve()
386 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
387 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
388 return result_guard(cancelObserve(defaultQoS));
391 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
393 if(m_observeHandle == nullptr)
395 return result_guard(OC_STACK_INVALID_PARAM);
398 OCStackResult result = checked_guard(m_clientWrapper.lock(),
399 &IClientWrapper::CancelObserveResource,
400 m_observeHandle, "", m_uri, m_headerOptions, QoS);
402 if(result == OC_STACK_OK)
404 m_observeHandle = nullptr;
410 std::string OCResource::host() const
412 std::ostringstream ss;
413 if (m_devAddr.flags & OC_SECURE)
418 else if (m_devAddr.adapter & OC_ADAPTER_TCP)
427 if (m_devAddr.flags & OC_IP_USE_V6)
429 ss << '[' << m_devAddr.addr << ']';
433 ss << m_devAddr.addr;
437 ss << ':' << m_devAddr.port;
442 std::string OCResource::uri() const
447 OCConnectivityType OCResource::connectivityType() const
449 return static_cast<OCConnectivityType>(
450 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
453 bool OCResource::isObservable() const
455 return m_isObservable;
458 OCResourceIdentifier OCResource::uniqueIdentifier() const
463 std::string OCResource::sid() const
465 return this->uniqueIdentifier().m_representation;
468 bool OCResource::operator==(const OCResource &other) const
470 return m_resourceId == other.m_resourceId;
473 bool OCResource::operator!=(const OCResource &other) const
475 return m_resourceId != other.m_resourceId;
478 bool OCResource::operator<(const OCResource &other) const
480 return m_resourceId < other.m_resourceId;
483 bool OCResource::operator>(const OCResource &other) const
485 return m_resourceId > other.m_resourceId;
488 bool OCResource::operator<=(const OCResource &other) const
490 return m_resourceId <= other.m_resourceId;
493 bool OCResource::operator>=(const OCResource &other) const
495 return m_resourceId >= other.m_resourceId;
498 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
499 const std::string& resourceUri)
500 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
504 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
506 os << ri.m_representation<<ri.m_resourceUri;
511 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
513 return m_representation == other.m_representation
514 && m_resourceUri == other.m_resourceUri;
517 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
519 return !(*this == other);
522 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
524 return m_resourceUri < other.m_resourceUri
525 || (m_resourceUri == other.m_resourceUri &&
526 m_representation < other.m_representation);
529 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
531 return *this != other && !(*this<other);
534 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
536 return !(*this > other);
539 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
541 return !(*this < other);