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>
26 #ifdef HAVE_ARPA_INET_H
27 #include <arpa/inet.h>
29 #ifdef HAVE_WS2TCPIP_H
38 static const char COAP[] = "coap://";
39 static const char COAPS[] = "coaps://";
40 static const char COAP_TCP[] = "coap+tcp://";
41 static const char COAP_GATT[] = "coap+gatt://";
42 static const char COAP_RFCOMM[] = "coap+rfcomm://";
45 using OC::result_guard;
46 using OC::checked_guard;
48 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
49 const OCDevAddr& devAddr, const std::string& uri,
50 const std::string& serverId, uint8_t property,
51 const std::vector<std::string>& resourceTypes,
52 const std::vector<std::string>& interfaces)
53 : m_clientWrapper(clientWrapper), m_uri(uri),
54 m_resourceId(serverId, m_uri), m_devAddr(devAddr),
55 m_isCollection(false), m_property(property),
56 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
57 m_observeHandle(nullptr)
59 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
60 != m_interfaces.end();
63 resourceTypes.empty() ||
65 m_clientWrapper.expired())
67 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
68 interfaces.empty(), m_clientWrapper.expired(), false, false);
72 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
73 const std::string& host, const std::string& uri,
74 const std::string& serverId,
75 OCConnectivityType connectivityType, uint8_t property,
76 const std::vector<std::string>& resourceTypes,
77 const std::vector<std::string>& interfaces)
78 : m_clientWrapper(clientWrapper), m_uri(uri),
79 m_resourceId(serverId, m_uri),
80 m_isCollection(false), m_property(property),
81 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
82 m_observeHandle(nullptr)
84 m_devAddr = OCDevAddr{OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0,
85 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
89 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
90 != m_interfaces.end();
93 resourceTypes.empty() ||
95 m_clientWrapper.expired())
97 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
98 interfaces.empty(), m_clientWrapper.expired(), false, false);
101 if (uri.length() == 1 && uri[0] == '/')
103 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
104 interfaces.empty(), m_clientWrapper.expired(), false, false);
109 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
110 interfaces.empty(), m_clientWrapper.expired(), false, false);
113 // construct the devAddr from the pieces we have
114 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
115 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
120 OCResource::~OCResource()
124 void OCResource::setHost(const std::string& host)
128 if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
130 prefix_len = sizeof(COAP) - 1;
132 else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
134 prefix_len = sizeof(COAPS) - 1;
135 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags | OC_SECURE);
137 else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
139 prefix_len = sizeof(COAP_TCP) - 1;
141 else if (host.compare(0, sizeof(COAP_GATT) - 1, COAP_GATT) == 0)
143 prefix_len = sizeof(COAP_GATT) - 1;
145 else if (host.compare(0, sizeof(COAP_RFCOMM) - 1, COAP_RFCOMM) == 0)
147 prefix_len = sizeof(COAP_RFCOMM) - 1;
151 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
152 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
155 // remove 'coap://' or 'coaps://' or 'coap+tcp://' or 'coap+gatt://' or 'coap+rfcomm://'
156 std::string host_token = host.substr(prefix_len);
158 if(host_token[0] == '[') // IPv6
160 size_t bracket = host_token.find(']');
162 if(bracket == std::string::npos || bracket == 0)
164 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
165 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
167 // extract the ipv6 address
168 std::string ip6Addr = host_token.substr(1, bracket - 1);
170 // address validity check
172 const char *cAddr = ip6Addr.c_str();
173 if(0 == inet_pton(AF_INET6, cAddr, &buf))
175 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
176 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
179 //skip ']' and ':' characters in host string
180 host_token = host_token.substr(bracket + 2);
181 int port = std::stoi(host_token);
183 if (0 > port || UINT16_MAX < port)
185 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
186 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
189 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
190 m_devAddr.addr[ip6Addr.length()] = '\0';
191 m_devAddr.port = static_cast<uint16_t>(port);
192 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
194 else if (host_token[0] == ':')
196 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
197 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
201 size_t dot = host_token.find('.');
202 if (std::string::npos == dot) // MAC
204 std::string macAddr = host_token;
206 // address validity check
207 if (MAC_ADDR_STR_SIZE != macAddr.length())
209 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
210 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
213 for (size_t blockCnt = 0; blockCnt < MAC_ADDR_BLOCKS; blockCnt++)
215 std::string block = macAddr.substr(blockCnt * 3, 2);
217 if (std::string::npos != block.find_first_not_of("0123456789ABCDEFabcdef"))
219 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
220 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
223 if (MAC_ADDR_BLOCKS - 1 > blockCnt)
225 char delimiter = macAddr[blockCnt * 3 + 2];
227 if (':' != delimiter)
229 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
230 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
235 macAddr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
236 m_devAddr.addr[MAC_ADDR_STR_SIZE] = '\0';
240 size_t colon = host_token.find(':');
242 if (colon == std::string::npos || colon == 0)
244 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
245 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
248 // extract the ipv4 address
249 std::string ip4Addr = host_token.substr(0, colon);
251 // address validity check
253 const char *cAddr = ip4Addr.c_str();
254 if(0 == inet_pton(AF_INET, cAddr, &buf))
256 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
257 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
260 //skip ':' characters in host string
261 host_token = host_token.substr(colon + 1);
262 int port = std::stoi(host_token);
264 if (0 > port || UINT16_MAX < port)
266 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
267 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
270 ip4Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
271 m_devAddr.addr[ip4Addr.length()] = '\0';
272 m_devAddr.port = static_cast<uint16_t>(port);
277 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
278 GetCallback attributeHandler, QualityOfService QoS)
280 return checked_guard(m_clientWrapper.lock(),
281 &IClientWrapper::GetResourceRepresentation,
283 queryParametersMap, m_headerOptions, CT_DEFAULT,
284 attributeHandler, QoS);
287 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
288 GetCallback attributeHandler)
290 QualityOfService defaultQos = OC::QualityOfService::NaQos;
291 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
292 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
295 OCStackResult OCResource::get(const std::string& resourceType,
296 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
297 GetCallback attributeHandler)
299 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
300 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
302 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
305 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
306 QualityOfService QoS)
308 QueryParamsMap mapCpy(queryParametersMap);
310 if(!resourceType.empty())
312 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
315 if(!resourceInterface.empty())
317 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
320 return result_guard(get(mapCpy, attributeHandler, QoS));
323 OCStackResult OCResource::put(const OCRepresentation& rep,
324 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
325 QualityOfService QoS)
327 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
328 m_devAddr, m_uri, rep, queryParametersMap,
329 m_headerOptions, attributeHandler, QoS);
332 OCStackResult OCResource::put(const OCRepresentation& rep,
333 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
335 QualityOfService defaultQos = OC::QualityOfService::NaQos;
336 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
337 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
340 OCStackResult OCResource::put(const std::string& resourceType,
341 const std::string& resourceInterface, const OCRepresentation& rep,
342 const QueryParamsMap& queryParametersMap,
343 PutCallback attributeHandler)
345 QualityOfService defaultQos = OC::QualityOfService::NaQos;
346 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
348 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
349 attributeHandler, defaultQos));
352 OCStackResult OCResource::put(const std::string& resourceType,
353 const std::string& resourceInterface, const OCRepresentation& rep,
354 const QueryParamsMap& queryParametersMap,
355 PutCallback attributeHandler,
356 QualityOfService QoS)
358 QueryParamsMap mapCpy(queryParametersMap);
360 if(!resourceType.empty())
362 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
365 if(!resourceInterface.empty())
367 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
370 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
373 OCStackResult OCResource::post(const OCRepresentation& rep,
374 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
375 QualityOfService QoS)
377 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
378 m_devAddr, m_uri, rep, queryParametersMap,
379 m_headerOptions, CT_DEFAULT, attributeHandler, QoS);
382 OCStackResult OCResource::post(const OCRepresentation& rep,
383 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
385 QualityOfService defaultQos = OC::QualityOfService::NaQos;
386 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
387 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
390 OCStackResult OCResource::post(const std::string& resourceType,
391 const std::string& resourceInterface, const OCRepresentation& rep,
392 const QueryParamsMap& queryParametersMap,
393 PostCallback attributeHandler)
395 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
396 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
398 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
402 OCStackResult OCResource::post(const std::string& resourceType,
403 const std::string& resourceInterface, const OCRepresentation& rep,
404 const QueryParamsMap& queryParametersMap,
405 PostCallback attributeHandler,
406 QualityOfService QoS)
408 QueryParamsMap mapCpy(queryParametersMap);
410 if(!resourceType.empty())
412 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
415 if(!resourceInterface.empty())
417 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
420 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
423 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
425 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
426 m_devAddr, m_uri, m_headerOptions, CT_DEFAULT, deleteHandler, QoS);
429 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
431 QualityOfService defaultQos = OC::QualityOfService::NaQos;
432 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
434 return result_guard(deleteResource(deleteHandler, defaultQos));
437 OCStackResult OCResource::observe(ObserveType observeType,
438 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
439 QualityOfService QoS)
441 if(m_observeHandle != nullptr)
443 return result_guard(OC_STACK_INVALID_PARAM);
446 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
447 observeType, &m_observeHandle, m_devAddr,
448 m_uri, queryParametersMap, m_headerOptions,
449 observeHandler, QoS);
452 OCStackResult OCResource::observe(ObserveType observeType,
453 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
455 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
456 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
458 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
461 OCStackResult OCResource::cancelObserve()
463 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
464 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
465 return result_guard(cancelObserve(defaultQoS));
468 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
470 if(m_observeHandle == nullptr)
472 return result_guard(OC_STACK_INVALID_PARAM);
475 OCStackResult result = checked_guard(m_clientWrapper.lock(),
476 &IClientWrapper::CancelObserveResource,
477 m_observeHandle, (const char*)"", m_uri, m_headerOptions, QoS);
479 if(result == OC_STACK_OK)
481 m_observeHandle = nullptr;
487 void OCResource::setHeaderOptions(const HeaderOptions& headerOptions)
489 m_headerOptions = headerOptions;
492 void OCResource::unsetHeaderOptions()
494 m_headerOptions.clear();
497 std::string OCResource::host() const
499 std::ostringstream ss;
501 if (m_devAddr.adapter & OC_ADAPTER_TCP)
505 else if (m_devAddr.adapter & OC_ADAPTER_GATT_BTLE)
509 else if (m_devAddr.adapter & OC_ADAPTER_RFCOMM_BTEDR)
515 if (m_devAddr.flags & OC_SECURE)
525 if (m_devAddr.flags & OC_IP_USE_V6)
527 ss << '[' << m_devAddr.addr << ']';
531 ss << m_devAddr.addr;
535 ss << ':' << m_devAddr.port;
540 std::string OCResource::uri() const
545 OCConnectivityType OCResource::connectivityType() const
547 return static_cast<OCConnectivityType>(
548 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
551 bool OCResource::isObservable() const
553 return (m_property & OC_OBSERVABLE) == OC_OBSERVABLE;
557 bool OCResource::isPublish() const
559 return (m_property & OC_MQ_PUBLISHER) == OC_MQ_PUBLISHER;
563 std::vector<std::string> OCResource::getResourceTypes() const
565 return m_resourceTypes;
568 std::vector<std::string> OCResource::getResourceInterfaces(void) const
573 OCResourceIdentifier OCResource::uniqueIdentifier() const
578 std::string OCResource::sid() const
580 return this->uniqueIdentifier().m_representation;
584 OCStackResult OCResource::discoveryMQTopics(const QueryParamsMap& queryParametersMap,
585 FindCallback attributeHandler)
587 QualityOfService defaultQos = OC::QualityOfService::NaQos;
588 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
589 return checked_guard(m_clientWrapper.lock(),
590 &IClientWrapper::ListenForMQTopic,
592 queryParametersMap, m_headerOptions,
593 attributeHandler, defaultQos);
596 OCStackResult OCResource::createMQTopic(const OCRepresentation& rep,
597 const std::string& topicUri,
598 const QueryParamsMap& queryParametersMap,
599 MQCreateTopicCallback attributeHandler)
601 QualityOfService defaultQos = OC::QualityOfService::NaQos;
602 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
603 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutMQTopicRepresentation,
604 m_devAddr, topicUri, rep, queryParametersMap,
605 m_headerOptions, attributeHandler, defaultQos);
609 OCStackResult OCResource::subscribeMQTopic(ObserveType observeType,
610 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
612 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
613 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
615 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
618 OCStackResult OCResource::unsubscribeMQTopic()
620 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
621 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
622 return result_guard(cancelObserve(defaultQoS));
625 OCStackResult OCResource::requestMQPublish(const QueryParamsMap& queryParametersMap,
626 PostCallback attributeHandler)
628 OCRepresentation rep;
629 rep.setValue(std::string("req_pub"), std::string("true"));
630 QualityOfService defaultQos = OC::QualityOfService::NaQos;
631 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
632 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
636 OCStackResult OCResource::publishMQTopic(const OCRepresentation& rep,
637 const QueryParamsMap& queryParametersMap,
638 PostCallback attributeHandler)
640 QualityOfService defaultQos = OC::QualityOfService::NaQos;
641 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
642 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
646 bool OCResource::operator==(const OCResource &other) const
648 return m_resourceId == other.m_resourceId;
651 bool OCResource::operator!=(const OCResource &other) const
653 return m_resourceId != other.m_resourceId;
656 bool OCResource::operator<(const OCResource &other) const
658 return m_resourceId < other.m_resourceId;
661 bool OCResource::operator>(const OCResource &other) const
663 return m_resourceId > other.m_resourceId;
666 bool OCResource::operator<=(const OCResource &other) const
668 return m_resourceId <= other.m_resourceId;
671 bool OCResource::operator>=(const OCResource &other) const
673 return m_resourceId >= other.m_resourceId;
676 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
677 const std::string& resourceUri)
678 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
682 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
684 os << ri.m_representation<<ri.m_resourceUri;
689 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
691 return m_representation == other.m_representation
692 && m_resourceUri == other.m_resourceUri;
695 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
697 return !(*this == other);
700 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
702 return m_resourceUri < other.m_resourceUri
703 || (m_resourceUri == other.m_resourceUri &&
704 m_representation < other.m_representation);
707 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
709 return *this != other && !(*this<other);
712 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
714 return !(*this > other);
717 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
719 return !(*this < other);