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 "iotivity_config.h"
22 #include "OCResource.h"
23 #include "OCUtilities.h"
25 #include <boost/lexical_cast.hpp>
27 #ifdef HAVE_ARPA_INET_H
28 #include <arpa/inet.h>
30 #ifdef HAVE_WS2TCPIP_H
40 static const char COAP[] = "coap://";
41 static const char COAPS[] = "coaps://";
42 static const char COAP_TCP[] = "coap+tcp://";
43 static const char COAPS_TCP[] = "coaps+tcp://";
44 static const char COAP_GATT[] = "coap+gatt://";
45 static const char COAP_RFCOMM[] = "coap+rfcomm://";
48 using OC::result_guard;
49 using OC::checked_guard;
51 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
52 const OCDevAddr& devAddr, const std::string& uri,
53 const std::string& serverId, uint8_t property,
54 const std::vector<std::string>& resourceTypes,
55 const std::vector<std::string>& interfaces)
56 : m_clientWrapper(clientWrapper), m_uri(uri),
57 m_resourceId(serverId, m_uri), m_devAddr(devAddr),
58 m_isCollection(false), m_property(property),
59 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
60 m_observeHandle(nullptr)
62 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
63 != m_interfaces.end();
66 resourceTypes.empty() ||
68 m_clientWrapper.expired())
70 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
71 interfaces.empty(), m_clientWrapper.expired(), false, false);
75 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
76 const std::string& host, const std::string& uri,
77 const std::string& serverId,
78 OCConnectivityType connectivityType, uint8_t property,
79 const std::vector<std::string>& resourceTypes,
80 const std::vector<std::string>& interfaces)
81 : m_clientWrapper(clientWrapper), m_uri(uri),
82 m_resourceId(serverId, m_uri),
83 m_isCollection(false), m_property(property),
84 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
85 m_observeHandle(nullptr)
87 m_devAddr = OCDevAddr{OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0,
88 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
92 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
93 != m_interfaces.end();
96 resourceTypes.empty() ||
98 m_clientWrapper.expired())
100 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
101 interfaces.empty(), m_clientWrapper.expired(), false, false);
104 if (uri.length() == 1 && uri[0] == '/')
106 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
107 interfaces.empty(), m_clientWrapper.expired(), false, false);
112 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
113 interfaces.empty(), m_clientWrapper.expired(), false, false);
116 // construct the devAddr from the pieces we have
117 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
118 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
123 OCResource::~OCResource()
127 void OCResource::setHost(const std::string& host)
131 if (host.compare(0, sizeof(COAP) - 1, COAP) == 0)
133 prefix_len = sizeof(COAP) - 1;
135 else if (host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
137 prefix_len = sizeof(COAPS) - 1;
138 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags | OC_SECURE);
140 else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
142 prefix_len = sizeof(COAP_TCP) - 1;
144 else if (host.compare(0, sizeof(COAPS_TCP) - 1, COAPS_TCP) == 0)
146 prefix_len = sizeof(COAPS_TCP) - 1;
147 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags | OC_SECURE);
149 else if (host.compare(0, sizeof(COAP_GATT) - 1, COAP_GATT) == 0)
151 prefix_len = sizeof(COAP_GATT) - 1;
153 else if (host.compare(0, sizeof(COAP_RFCOMM) - 1, COAP_RFCOMM) == 0)
155 prefix_len = sizeof(COAP_RFCOMM) - 1;
159 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
160 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
163 // remove 'coap://' or 'coaps://' or 'coap+tcp://' or 'coap+gatt://' or 'coap+rfcomm://'
164 std::string host_token = host.substr(prefix_len);
166 if (host_token[0] == '[') // IPv6
168 size_t bracket = host_token.find(']');
170 if (std::string::npos == bracket || 0 == bracket)
172 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
173 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
175 // extract the ipv6 address
176 std::string ip6Addr = host_token.substr(1, bracket - 1);
178 // address validity check
179 std::string ip6AddrToValidityCheck(ip6Addr);
180 size_t percent = ip6AddrToValidityCheck.find('%');
181 if (std::string::npos != percent)
183 ip6AddrToValidityCheck.resize(percent);
186 const char *cAddr = ip6AddrToValidityCheck.c_str();
187 if (0 == inet_pton(AF_INET6, cAddr, &buf))
189 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
190 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
193 //skip ']' and ':' characters in host string
194 host_token = host_token.substr(bracket + 2);
195 int port = std::stoi(host_token);
197 if (0 > port || UINT16_MAX < port)
199 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
200 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
203 OCStackResult result = OCDecodeAddressForRFC6874(m_devAddr.addr,
204 sizeof(m_devAddr.addr), ip6Addr.c_str(), nullptr);
206 if (OC_STACK_OK != result)
208 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
209 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
212 m_devAddr.port = static_cast<uint16_t>(port);
213 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags | OC_IP_USE_V6);
215 else if (host_token[0] == ':')
217 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
218 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
222 size_t dot = host_token.find('.');
223 if (std::string::npos == dot) // MAC
225 std::string macAddr = host_token;
227 // address validity check
228 if (MAC_ADDR_STR_SIZE != macAddr.length())
230 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
231 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
234 for (size_t blockCnt = 0; blockCnt < MAC_ADDR_BLOCKS; blockCnt++)
236 std::string block = macAddr.substr(blockCnt * 3, 2);
238 if (std::string::npos != block.find_first_not_of("0123456789ABCDEFabcdef"))
240 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
241 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
244 if (MAC_ADDR_BLOCKS - 1 > blockCnt)
246 char delimiter = macAddr[blockCnt * 3 + 2];
248 if (':' != delimiter)
250 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
251 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
256 macAddr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
257 m_devAddr.addr[MAC_ADDR_STR_SIZE] = '\0';
261 size_t colon = host_token.find(':');
263 if (colon == std::string::npos || colon == 0)
265 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
266 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
269 // extract the ipv4 address
270 std::string ip4Addr = host_token.substr(0, colon);
272 // address validity check
274 const char *cAddr = ip4Addr.c_str();
275 if (0 == inet_pton(AF_INET, cAddr, &buf))
277 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
278 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
281 //skip ':' characters in host string
282 host_token = host_token.substr(colon + 1);
283 int port = std::stoi(host_token);
285 if (0 > port || UINT16_MAX < port)
287 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
288 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
291 ip4Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
292 m_devAddr.addr[ip4Addr.length()] = '\0';
293 m_devAddr.port = static_cast<uint16_t>(port);
298 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
299 GetCallback attributeHandler, QualityOfService QoS)
301 return checked_guard(m_clientWrapper.lock(),
302 &IClientWrapper::GetResourceRepresentation,
304 queryParametersMap, m_headerOptions, CT_DEFAULT,
305 attributeHandler, QoS);
308 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
309 GetCallback attributeHandler)
311 QualityOfService defaultQos = OC::QualityOfService::NaQos;
312 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
313 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
316 OCStackResult OCResource::get(const std::string& resourceType,
317 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
318 GetCallback attributeHandler)
320 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
321 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
323 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
326 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
327 QualityOfService QoS)
329 QueryParamsMap mapCpy(queryParametersMap);
331 if (!resourceType.empty())
333 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
336 if (!resourceInterface.empty())
338 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
341 return result_guard(get(mapCpy, attributeHandler, QoS));
344 OCStackResult OCResource::put(const OCRepresentation& rep,
345 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
346 QualityOfService QoS)
348 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
349 m_devAddr, m_uri, rep, queryParametersMap,
350 m_headerOptions, attributeHandler, QoS);
353 OCStackResult OCResource::put(const OCRepresentation& rep,
354 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
356 QualityOfService defaultQos = OC::QualityOfService::NaQos;
357 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
358 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
361 OCStackResult OCResource::put(const std::string& resourceType,
362 const std::string& resourceInterface, const OCRepresentation& rep,
363 const QueryParamsMap& queryParametersMap,
364 PutCallback attributeHandler)
366 QualityOfService defaultQos = OC::QualityOfService::NaQos;
367 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
369 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
370 attributeHandler, defaultQos));
373 OCStackResult OCResource::put(const std::string& resourceType,
374 const std::string& resourceInterface, const OCRepresentation& rep,
375 const QueryParamsMap& queryParametersMap,
376 PutCallback attributeHandler,
377 QualityOfService QoS)
379 QueryParamsMap mapCpy(queryParametersMap);
381 if (!resourceType.empty())
383 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
386 if (!resourceInterface.empty())
388 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
391 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
394 OCStackResult OCResource::post(const OCRepresentation& rep,
395 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
396 QualityOfService QoS)
398 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
399 m_devAddr, m_uri, rep, queryParametersMap,
400 m_headerOptions, CT_DEFAULT, attributeHandler, QoS);
403 OCStackResult OCResource::post(const OCRepresentation& rep,
404 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
406 QualityOfService defaultQos = OC::QualityOfService::NaQos;
407 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
408 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
411 OCStackResult OCResource::post(const std::string& resourceType,
412 const std::string& resourceInterface, const OCRepresentation& rep,
413 const QueryParamsMap& queryParametersMap,
414 PostCallback attributeHandler)
416 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
417 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
419 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
423 OCStackResult OCResource::post(const std::string& resourceType,
424 const std::string& resourceInterface, const OCRepresentation& rep,
425 const QueryParamsMap& queryParametersMap,
426 PostCallback attributeHandler,
427 QualityOfService QoS)
429 QueryParamsMap mapCpy(queryParametersMap);
431 if (!resourceType.empty())
433 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
436 if (!resourceInterface.empty())
438 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
441 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
444 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
446 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
447 m_devAddr, m_uri, m_headerOptions, CT_DEFAULT, deleteHandler, QoS);
450 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
452 QualityOfService defaultQos = OC::QualityOfService::NaQos;
453 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
455 return result_guard(deleteResource(deleteHandler, defaultQos));
458 OCStackResult OCResource::observe(ObserveType observeType,
459 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
460 QualityOfService QoS)
462 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
463 observeType, &m_observeHandle, m_devAddr,
464 m_uri, queryParametersMap, m_headerOptions,
465 observeHandler, QoS);
468 OCStackResult OCResource::observe(ObserveType observeType,
469 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
471 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
472 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
474 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
477 OCStackResult OCResource::cancelObserve()
479 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
480 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
481 return result_guard(cancelObserve(defaultQoS));
484 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
486 if (m_observeHandle == nullptr)
488 return result_guard(OC_STACK_INVALID_PARAM);
491 OCStackResult result = checked_guard(m_clientWrapper.lock(),
492 &IClientWrapper::CancelObserveResource,
493 m_observeHandle, (const char*)"", m_uri, m_headerOptions, QoS);
495 if (result == OC_STACK_OK)
497 m_observeHandle = nullptr;
503 void OCResource::setHeaderOptions(const HeaderOptions& headerOptions)
505 m_headerOptions = headerOptions;
508 void OCResource::unsetHeaderOptions()
510 m_headerOptions.clear();
513 std::string OCResource::host() const
515 std::ostringstream ss;
517 if (m_devAddr.adapter & OC_ADAPTER_TCP)
519 if (m_devAddr.flags & OC_SECURE)
528 else if (m_devAddr.adapter & OC_ADAPTER_GATT_BTLE)
532 else if (m_devAddr.adapter & OC_ADAPTER_RFCOMM_BTEDR)
538 if (m_devAddr.flags & OC_SECURE)
548 if (m_devAddr.flags & OC_IP_USE_V6)
550 char addressEncoded[128] = {0};
552 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
553 sizeof(addressEncoded),
555 if (OC_STACK_OK != result)
557 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
558 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
560 ss << '[' << addressEncoded << ']';
564 ss << m_devAddr.addr;
568 ss << ':' << m_devAddr.port;
573 std::string OCResource::uri() const
578 OCConnectivityType OCResource::connectivityType() const
580 return static_cast<OCConnectivityType>(
581 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
584 bool OCResource::isObservable() const
586 return (m_property & OC_OBSERVABLE) == OC_OBSERVABLE;
590 bool OCResource::isPublish() const
592 return (m_property & OC_MQ_PUBLISHER) == OC_MQ_PUBLISHER;
596 std::vector<std::string> OCResource::getResourceTypes() const
598 return m_resourceTypes;
601 std::vector<std::string> OCResource::getResourceInterfaces(void) const
606 OCResourceIdentifier OCResource::uniqueIdentifier() const
611 std::string OCResource::sid() const
613 return this->uniqueIdentifier().m_representation;
617 OCStackResult OCResource::discoveryMQTopics(const QueryParamsMap& queryParametersMap,
618 MQTopicCallback attributeHandler,
619 QualityOfService qos)
621 return checked_guard(m_clientWrapper.lock(),
622 &IClientWrapper::ListenForMQTopic,
624 queryParametersMap, m_headerOptions,
625 attributeHandler, qos);
628 OCStackResult OCResource::createMQTopic(const OCRepresentation& rep,
629 const std::string& topicUri,
630 const QueryParamsMap& queryParametersMap,
631 MQTopicCallback attributeHandler,
632 QualityOfService qos)
634 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutMQTopicRepresentation,
635 m_devAddr, topicUri, rep, queryParametersMap,
636 m_headerOptions, attributeHandler, qos);
640 OCStackResult OCResource::subscribeMQTopic(ObserveType observeType,
641 const QueryParamsMap& queryParametersMap,
642 ObserveCallback observeHandler,
643 QualityOfService qos)
645 return result_guard(observe(observeType, queryParametersMap, observeHandler, qos));
648 OCStackResult OCResource::unsubscribeMQTopic(QualityOfService qos)
650 return result_guard(cancelObserve(qos));
653 OCStackResult OCResource::requestMQPublish(const QueryParamsMap& queryParametersMap,
654 PostCallback attributeHandler,
655 QualityOfService qos)
657 OCRepresentation rep;
658 rep.setValue(std::string("req_pub"), std::string("true"));
659 return result_guard(post(rep, queryParametersMap, attributeHandler, qos));
663 OCStackResult OCResource::publishMQTopic(const OCRepresentation& rep,
664 const QueryParamsMap& queryParametersMap,
665 PostCallback attributeHandler,
666 QualityOfService qos)
668 return result_guard(post(rep, queryParametersMap, attributeHandler, qos));
672 bool OCResource::operator==(const OCResource &other) const
674 return m_resourceId == other.m_resourceId;
677 bool OCResource::operator!=(const OCResource &other) const
679 return m_resourceId != other.m_resourceId;
682 bool OCResource::operator<(const OCResource &other) const
684 return m_resourceId < other.m_resourceId;
687 bool OCResource::operator>(const OCResource &other) const
689 return m_resourceId > other.m_resourceId;
692 bool OCResource::operator<=(const OCResource &other) const
694 return m_resourceId <= other.m_resourceId;
697 bool OCResource::operator>=(const OCResource &other) const
699 return m_resourceId >= other.m_resourceId;
702 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
703 const std::string& resourceUri)
704 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
708 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
710 os << ri.m_representation<<ri.m_resourceUri;
715 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
717 return m_representation == other.m_representation
718 && m_resourceUri == other.m_resourceUri;
721 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
723 return !(*this == other);
726 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
728 return m_resourceUri < other.m_resourceUri
729 || (m_resourceUri == other.m_resourceUri &&
730 m_representation < other.m_representation);
733 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
735 return *this != other && !(*this<other);
738 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
740 return !(*this > other);
743 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
745 return !(*this < other);