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>
27 #include <arpa/inet.h>
35 static const char COAP[] = "coap://";
36 static const char COAPS[] = "coaps://";
37 static const char COAP_TCP[] = "coap+tcp://";
40 using OC::result_guard;
41 using OC::checked_guard;
43 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
44 const OCDevAddr& devAddr, const std::string& uri,
45 const std::string& serverId, bool observable,
46 const std::vector<std::string>& resourceTypes,
47 const std::vector<std::string>& interfaces)
48 : m_clientWrapper(clientWrapper), m_uri(uri),
49 m_resourceId(serverId, m_uri), m_devAddr(devAddr),
50 m_isObservable(observable), m_isCollection(false),
51 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
52 m_observeHandle(nullptr)
54 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
55 != m_interfaces.end();
58 resourceTypes.empty() ||
60 m_clientWrapper.expired())
62 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
63 interfaces.empty(), m_clientWrapper.expired(), false, false);
67 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
68 const std::string& host, const std::string& uri,
69 const std::string& serverId,
70 OCConnectivityType connectivityType, bool observable,
71 const std::vector<std::string>& resourceTypes,
72 const std::vector<std::string>& interfaces)
73 : m_clientWrapper(clientWrapper), m_uri(uri),
74 m_resourceId(serverId, m_uri),
75 m_devAddr{ OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0
76 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
80 m_isObservable(observable), m_isCollection(false),
81 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
82 m_observeHandle(nullptr)
84 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
85 != m_interfaces.end();
88 resourceTypes.empty() ||
90 m_clientWrapper.expired())
92 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
93 interfaces.empty(), m_clientWrapper.expired(), false, false);
96 if (uri.length() == 1 && uri[0] == '/')
98 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
99 interfaces.empty(), m_clientWrapper.expired(), false, false);
104 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
105 interfaces.empty(), m_clientWrapper.expired(), false, false);
108 // construct the devAddr from the pieces we have
109 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
110 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
115 OCResource::~OCResource()
119 void OCResource::setHost(const std::string& host)
123 if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
125 prefix_len = sizeof(COAP) - 1;
127 else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
129 prefix_len = sizeof(COAPS) - 1;
130 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
132 else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
134 prefix_len = sizeof(COAP_TCP) - 1;
138 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
139 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
142 // remove 'coap://' or 'coaps://' or 'coap+tcp://'
143 std::string host_token = host.substr(prefix_len);
145 if(host_token[0] == '[') // IPv6
147 size_t bracket = host_token.find(']');
149 if(bracket == std::string::npos || bracket == 0)
151 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
152 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
154 // extract the ipv6 address
155 std::string ip6Addr = host_token.substr(1, bracket - 1);
157 // address validity check
159 const char *cAddr = ip6Addr.c_str();
160 if(0 == inet_pton(AF_INET6, cAddr, &buf))
162 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
163 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
166 //skip ']' and ':' characters in host string
167 host_token = host_token.substr(bracket + 2);
168 int port = std::stoi(host_token);
170 if (0 > port || UINT16_MAX < port)
172 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
173 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
176 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
177 m_devAddr.addr[ip6Addr.length()] = '\0';
178 m_devAddr.port = static_cast<uint16_t>(port);
179 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
181 else if (host_token[0] == ':')
183 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
184 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
188 size_t dot = host_token.find('.');
189 if (std::string::npos == dot) // MAC
191 std::string macAddr = host_token;
193 // address validity check
194 if (MAC_ADDR_STR_SIZE != macAddr.length())
196 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
197 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
200 for (size_t blockCnt = 0; blockCnt < MAC_ADDR_BLOCKS; blockCnt++)
202 std::string block = macAddr.substr(blockCnt * 3, 2);
204 if (std::string::npos != block.find_first_not_of("0123456789ABCDEFabcdef"))
206 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
207 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
210 if (MAC_ADDR_BLOCKS - 1 > blockCnt)
212 char delimiter = macAddr[blockCnt * 3 + 2];
214 if (':' != delimiter)
216 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
217 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
222 macAddr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
223 m_devAddr.addr[MAC_ADDR_STR_SIZE] = '\0';
227 size_t colon = host_token.find(':');
229 if (colon == std::string::npos || colon == 0)
231 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
232 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
235 // extract the ipv4 address
236 std::string ip4Addr = host_token.substr(0, colon);
238 // address validity check
240 const char *cAddr = ip4Addr.c_str();
241 if(0 == inet_pton(AF_INET, cAddr, &buf))
243 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
244 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
247 //skip ':' characters in host string
248 host_token = host_token.substr(colon + 1);
249 int port = std::stoi(host_token);
251 if (0 > port || UINT16_MAX < port)
253 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
254 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
257 ip4Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
258 m_devAddr.addr[ip4Addr.length()] = '\0';
259 m_devAddr.port = static_cast<uint16_t>(port);
264 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
265 GetCallback attributeHandler, QualityOfService QoS)
267 return checked_guard(m_clientWrapper.lock(),
268 &IClientWrapper::GetResourceRepresentation,
270 queryParametersMap, m_headerOptions,
271 attributeHandler, QoS);
274 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
275 GetCallback attributeHandler)
277 QualityOfService defaultQos = OC::QualityOfService::NaQos;
278 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
279 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
282 OCStackResult OCResource::get(const std::string& resourceType,
283 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
284 GetCallback attributeHandler)
286 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
287 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
289 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
292 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
293 QualityOfService QoS)
295 QueryParamsMap mapCpy(queryParametersMap);
297 if(!resourceType.empty())
299 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
302 if(!resourceInterface.empty())
304 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
307 return result_guard(get(mapCpy, attributeHandler, QoS));
310 OCStackResult OCResource::put(const OCRepresentation& rep,
311 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
312 QualityOfService QoS)
314 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
315 m_devAddr, m_uri, rep, queryParametersMap,
316 m_headerOptions, attributeHandler, QoS);
319 OCStackResult OCResource::put(const OCRepresentation& rep,
320 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
322 QualityOfService defaultQos = OC::QualityOfService::NaQos;
323 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
324 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
327 OCStackResult OCResource::put(const std::string& resourceType,
328 const std::string& resourceInterface, const OCRepresentation& rep,
329 const QueryParamsMap& queryParametersMap,
330 PutCallback attributeHandler)
332 QualityOfService defaultQos = OC::QualityOfService::NaQos;
333 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
335 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
336 attributeHandler, defaultQos));
339 OCStackResult OCResource::put(const std::string& resourceType,
340 const std::string& resourceInterface, const OCRepresentation& rep,
341 const QueryParamsMap& queryParametersMap,
342 PutCallback attributeHandler,
343 QualityOfService QoS)
345 QueryParamsMap mapCpy(queryParametersMap);
347 if(!resourceType.empty())
349 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
352 if(!resourceInterface.empty())
354 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
357 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
360 OCStackResult OCResource::post(const OCRepresentation& rep,
361 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
362 QualityOfService QoS)
364 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
365 m_devAddr, m_uri, rep, queryParametersMap,
366 m_headerOptions, attributeHandler, QoS);
369 OCStackResult OCResource::post(const OCRepresentation& rep,
370 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
372 QualityOfService defaultQos = OC::QualityOfService::NaQos;
373 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
374 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
377 OCStackResult OCResource::post(const std::string& resourceType,
378 const std::string& resourceInterface, const OCRepresentation& rep,
379 const QueryParamsMap& queryParametersMap,
380 PostCallback attributeHandler)
382 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
383 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
385 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
389 OCStackResult OCResource::post(const std::string& resourceType,
390 const std::string& resourceInterface, const OCRepresentation& rep,
391 const QueryParamsMap& queryParametersMap,
392 PostCallback attributeHandler,
393 QualityOfService QoS)
395 QueryParamsMap mapCpy(queryParametersMap);
397 if(!resourceType.empty())
399 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
402 if(!resourceInterface.empty())
404 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
407 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
410 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
412 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
413 m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
416 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
418 QualityOfService defaultQos = OC::QualityOfService::NaQos;
419 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
421 return result_guard(deleteResource(deleteHandler, defaultQos));
424 OCStackResult OCResource::observe(ObserveType observeType,
425 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
426 QualityOfService QoS)
428 if(m_observeHandle != nullptr)
430 return result_guard(OC_STACK_INVALID_PARAM);
433 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
434 observeType, &m_observeHandle, m_devAddr,
435 m_uri, queryParametersMap, m_headerOptions,
436 observeHandler, QoS);
439 OCStackResult OCResource::observe(ObserveType observeType,
440 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
442 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
443 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
445 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
448 OCStackResult OCResource::cancelObserve()
450 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
451 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
452 return result_guard(cancelObserve(defaultQoS));
455 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
457 if(m_observeHandle == nullptr)
459 return result_guard(OC_STACK_INVALID_PARAM);
462 OCStackResult result = checked_guard(m_clientWrapper.lock(),
463 &IClientWrapper::CancelObserveResource,
464 m_observeHandle, "", m_uri, m_headerOptions, QoS);
466 if(result == OC_STACK_OK)
468 m_observeHandle = nullptr;
474 void OCResource::setHeaderOptions(const HeaderOptions& headerOptions)
476 m_headerOptions = headerOptions;
479 void OCResource::unsetHeaderOptions()
481 m_headerOptions.clear();
484 std::string OCResource::host() const
486 std::ostringstream ss;
487 if (m_devAddr.flags & OC_SECURE)
491 else if ((m_devAddr.adapter & OC_ADAPTER_TCP)
492 || (m_devAddr.adapter & OC_ADAPTER_GATT_BTLE)
493 || (m_devAddr.adapter & OC_ADAPTER_RFCOMM_BTEDR))
501 if (m_devAddr.flags & OC_IP_USE_V6)
503 ss << '[' << m_devAddr.addr << ']';
507 ss << m_devAddr.addr;
511 ss << ':' << m_devAddr.port;
516 std::string OCResource::uri() const
521 OCConnectivityType OCResource::connectivityType() const
523 return static_cast<OCConnectivityType>(
524 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
527 bool OCResource::isObservable() const
529 return m_isObservable;
532 std::vector<std::string> OCResource::getResourceTypes() const
534 return m_resourceTypes;
537 std::vector<std::string> OCResource::getResourceInterfaces(void) const
542 OCResourceIdentifier OCResource::uniqueIdentifier() const
547 std::string OCResource::sid() const
549 return this->uniqueIdentifier().m_representation;
552 bool OCResource::operator==(const OCResource &other) const
554 return m_resourceId == other.m_resourceId;
557 bool OCResource::operator!=(const OCResource &other) const
559 return m_resourceId != other.m_resourceId;
562 bool OCResource::operator<(const OCResource &other) const
564 return m_resourceId < other.m_resourceId;
567 bool OCResource::operator>(const OCResource &other) const
569 return m_resourceId > other.m_resourceId;
572 bool OCResource::operator<=(const OCResource &other) const
574 return m_resourceId <= other.m_resourceId;
577 bool OCResource::operator>=(const OCResource &other) const
579 return m_resourceId >= other.m_resourceId;
582 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
583 const std::string& resourceUri)
584 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
588 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
590 os << ri.m_representation<<ri.m_resourceUri;
595 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
597 return m_representation == other.m_representation
598 && m_resourceUri == other.m_resourceUri;
601 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
603 return !(*this == other);
606 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
608 return m_resourceUri < other.m_resourceUri
609 || (m_resourceUri == other.m_resourceUri &&
610 m_representation < other.m_representation);
613 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
615 return *this != other && !(*this<other);
618 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
620 return !(*this > other);
623 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
625 return !(*this < other);