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
39 static const char COAP[] = "coap://";
40 static const char COAPS[] = "coaps://";
41 static const char COAP_TCP[] = "coap+tcp://";
42 static const char COAP_GATT[] = "coap+gatt://";
43 static const char COAP_RFCOMM[] = "coap+rfcomm://";
46 using OC::result_guard;
47 using OC::checked_guard;
49 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
50 const OCDevAddr& devAddr, const std::string& uri,
51 const std::string& serverId, uint8_t property,
52 const std::vector<std::string>& resourceTypes,
53 const std::vector<std::string>& interfaces)
54 : m_clientWrapper(clientWrapper), m_uri(uri),
55 m_resourceId(serverId, m_uri), m_devAddr(devAddr),
56 m_isCollection(false), m_property(property),
57 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
58 m_observeHandle(nullptr)
60 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
61 != m_interfaces.end();
64 resourceTypes.empty() ||
66 m_clientWrapper.expired())
68 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
69 interfaces.empty(), m_clientWrapper.expired(), false, false);
73 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
74 const std::string& host, const std::string& uri,
75 const std::string& serverId,
76 OCConnectivityType connectivityType, uint8_t property,
77 const std::vector<std::string>& resourceTypes,
78 const std::vector<std::string>& interfaces)
79 : m_clientWrapper(clientWrapper), m_uri(uri),
80 m_resourceId(serverId, m_uri),
81 m_isCollection(false), m_property(property),
82 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
83 m_observeHandle(nullptr)
85 m_devAddr = OCDevAddr{OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0,
86 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
90 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
91 != m_interfaces.end();
94 resourceTypes.empty() ||
96 m_clientWrapper.expired())
98 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
99 interfaces.empty(), m_clientWrapper.expired(), false, false);
102 if (uri.length() == 1 && uri[0] == '/')
104 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
105 interfaces.empty(), m_clientWrapper.expired(), false, false);
110 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
111 interfaces.empty(), m_clientWrapper.expired(), false, false);
114 // construct the devAddr from the pieces we have
115 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
116 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
121 OCResource::~OCResource()
125 void OCResource::setHost(const std::string& host)
129 if (host.compare(0, sizeof(COAP) - 1, COAP) == 0)
131 prefix_len = sizeof(COAP) - 1;
133 else if (host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
135 prefix_len = sizeof(COAPS) - 1;
136 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags | OC_SECURE);
138 else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
140 prefix_len = sizeof(COAP_TCP) - 1;
142 else if (host.compare(0, sizeof(COAP_GATT) - 1, COAP_GATT) == 0)
144 prefix_len = sizeof(COAP_GATT) - 1;
146 else if (host.compare(0, sizeof(COAP_RFCOMM) - 1, COAP_RFCOMM) == 0)
148 prefix_len = sizeof(COAP_RFCOMM) - 1;
152 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
153 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
156 // remove 'coap://' or 'coaps://' or 'coap+tcp://' or 'coap+gatt://' or 'coap+rfcomm://'
157 std::string host_token = host.substr(prefix_len);
159 if (host_token[0] == '[') // IPv6
161 size_t bracket = host_token.find(']');
163 if (std::string::npos == bracket || 0 == bracket)
165 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
166 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
168 // extract the ipv6 address
169 std::string ip6Addr = host_token.substr(1, bracket - 1);
171 // address validity check
172 std::string ip6AddrToValidityCheck(ip6Addr);
173 size_t percent = ip6AddrToValidityCheck.find('%');
174 if (std::string::npos != percent)
176 ip6AddrToValidityCheck.resize(percent);
179 const char *cAddr = ip6AddrToValidityCheck.c_str();
180 if (0 == inet_pton(AF_INET6, cAddr, &buf))
182 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
183 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
186 //skip ']' and ':' characters in host string
187 host_token = host_token.substr(bracket + 2);
188 int port = std::stoi(host_token);
190 if (0 > port || UINT16_MAX < port)
192 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
193 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
196 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
197 m_devAddr.addr[ip6Addr.length()] = '\0';
198 m_devAddr.port = static_cast<uint16_t>(port);
199 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
201 else if (host_token[0] == ':')
203 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
204 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
208 size_t dot = host_token.find('.');
209 if (std::string::npos == dot) // MAC
211 std::string macAddr = host_token;
213 // address validity check
214 if (MAC_ADDR_STR_SIZE != macAddr.length())
216 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
217 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
220 for (size_t blockCnt = 0; blockCnt < MAC_ADDR_BLOCKS; blockCnt++)
222 std::string block = macAddr.substr(blockCnt * 3, 2);
224 if (std::string::npos != block.find_first_not_of("0123456789ABCDEFabcdef"))
226 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
227 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
230 if (MAC_ADDR_BLOCKS - 1 > blockCnt)
232 char delimiter = macAddr[blockCnt * 3 + 2];
234 if (':' != delimiter)
236 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
237 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
242 macAddr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
243 m_devAddr.addr[MAC_ADDR_STR_SIZE] = '\0';
247 size_t colon = host_token.find(':');
249 if (colon == std::string::npos || colon == 0)
251 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
252 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
255 // extract the ipv4 address
256 std::string ip4Addr = host_token.substr(0, colon);
258 // address validity check
260 const char *cAddr = ip4Addr.c_str();
261 if (0 == inet_pton(AF_INET, cAddr, &buf))
263 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
264 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
267 //skip ':' characters in host string
268 host_token = host_token.substr(colon + 1);
269 int port = std::stoi(host_token);
271 if (0 > port || UINT16_MAX < port)
273 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
274 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
277 ip4Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
278 m_devAddr.addr[ip4Addr.length()] = '\0';
279 m_devAddr.port = static_cast<uint16_t>(port);
284 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
285 GetCallback attributeHandler, QualityOfService QoS)
287 return checked_guard(m_clientWrapper.lock(),
288 &IClientWrapper::GetResourceRepresentation,
290 queryParametersMap, m_headerOptions, CT_DEFAULT,
291 attributeHandler, QoS);
294 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
295 GetCallback attributeHandler)
297 QualityOfService defaultQos = OC::QualityOfService::NaQos;
298 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
299 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
302 OCStackResult OCResource::get(const std::string& resourceType,
303 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
304 GetCallback attributeHandler)
306 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
307 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
309 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
312 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
313 QualityOfService QoS)
315 QueryParamsMap mapCpy(queryParametersMap);
317 if (!resourceType.empty())
319 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
322 if (!resourceInterface.empty())
324 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
327 return result_guard(get(mapCpy, attributeHandler, QoS));
330 OCStackResult OCResource::put(const OCRepresentation& rep,
331 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
332 QualityOfService QoS)
334 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
335 m_devAddr, m_uri, rep, queryParametersMap,
336 m_headerOptions, attributeHandler, QoS);
339 OCStackResult OCResource::put(const OCRepresentation& rep,
340 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
342 QualityOfService defaultQos = OC::QualityOfService::NaQos;
343 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
344 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
347 OCStackResult OCResource::put(const std::string& resourceType,
348 const std::string& resourceInterface, const OCRepresentation& rep,
349 const QueryParamsMap& queryParametersMap,
350 PutCallback attributeHandler)
352 QualityOfService defaultQos = OC::QualityOfService::NaQos;
353 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
355 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
356 attributeHandler, defaultQos));
359 OCStackResult OCResource::put(const std::string& resourceType,
360 const std::string& resourceInterface, const OCRepresentation& rep,
361 const QueryParamsMap& queryParametersMap,
362 PutCallback attributeHandler,
363 QualityOfService QoS)
365 QueryParamsMap mapCpy(queryParametersMap);
367 if (!resourceType.empty())
369 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
372 if (!resourceInterface.empty())
374 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
377 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
380 OCStackResult OCResource::post(const OCRepresentation& rep,
381 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
382 QualityOfService QoS)
384 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
385 m_devAddr, m_uri, rep, queryParametersMap,
386 m_headerOptions, CT_DEFAULT, attributeHandler, QoS);
389 OCStackResult OCResource::post(const OCRepresentation& rep,
390 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
392 QualityOfService defaultQos = OC::QualityOfService::NaQos;
393 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
394 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
397 OCStackResult OCResource::post(const std::string& resourceType,
398 const std::string& resourceInterface, const OCRepresentation& rep,
399 const QueryParamsMap& queryParametersMap,
400 PostCallback attributeHandler)
402 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
403 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
405 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
409 OCStackResult OCResource::post(const std::string& resourceType,
410 const std::string& resourceInterface, const OCRepresentation& rep,
411 const QueryParamsMap& queryParametersMap,
412 PostCallback attributeHandler,
413 QualityOfService QoS)
415 QueryParamsMap mapCpy(queryParametersMap);
417 if (!resourceType.empty())
419 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
422 if (!resourceInterface.empty())
424 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
427 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
430 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
432 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
433 m_devAddr, m_uri, m_headerOptions, CT_DEFAULT, deleteHandler, QoS);
436 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
438 QualityOfService defaultQos = OC::QualityOfService::NaQos;
439 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
441 return result_guard(deleteResource(deleteHandler, defaultQos));
444 OCStackResult OCResource::observe(ObserveType observeType,
445 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
446 QualityOfService QoS)
448 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
449 observeType, &m_observeHandle, m_devAddr,
450 m_uri, queryParametersMap, m_headerOptions,
451 observeHandler, QoS);
454 OCStackResult OCResource::observe(ObserveType observeType,
455 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
457 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
458 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
460 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
463 OCStackResult OCResource::cancelObserve()
465 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
466 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
467 return result_guard(cancelObserve(defaultQoS));
470 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
472 if (m_observeHandle == nullptr)
474 return result_guard(OC_STACK_INVALID_PARAM);
477 OCStackResult result = checked_guard(m_clientWrapper.lock(),
478 &IClientWrapper::CancelObserveResource,
479 m_observeHandle, (const char*)"", m_uri, m_headerOptions, QoS);
481 if (result == OC_STACK_OK)
483 m_observeHandle = nullptr;
489 void OCResource::setHeaderOptions(const HeaderOptions& headerOptions)
491 m_headerOptions = headerOptions;
494 void OCResource::unsetHeaderOptions()
496 m_headerOptions.clear();
499 std::string OCResource::host() const
501 std::ostringstream ss;
503 if (m_devAddr.adapter & OC_ADAPTER_TCP)
507 else if (m_devAddr.adapter & OC_ADAPTER_GATT_BTLE)
511 else if (m_devAddr.adapter & OC_ADAPTER_RFCOMM_BTEDR)
517 if (m_devAddr.flags & OC_SECURE)
527 if (m_devAddr.flags & OC_IP_USE_V6)
529 ss << '[' << m_devAddr.addr << ']';
533 ss << m_devAddr.addr;
537 ss << ':' << m_devAddr.port;
542 std::string OCResource::uri() const
547 OCConnectivityType OCResource::connectivityType() const
549 return static_cast<OCConnectivityType>(
550 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
553 bool OCResource::isObservable() const
555 return (m_property & OC_OBSERVABLE) == OC_OBSERVABLE;
559 bool OCResource::isPublish() const
561 return (m_property & OC_MQ_PUBLISHER) == OC_MQ_PUBLISHER;
565 std::vector<std::string> OCResource::getResourceTypes() const
567 return m_resourceTypes;
570 std::vector<std::string> OCResource::getResourceInterfaces(void) const
575 OCResourceIdentifier OCResource::uniqueIdentifier() const
580 std::string OCResource::sid() const
582 return this->uniqueIdentifier().m_representation;
586 OCStackResult OCResource::discoveryMQTopics(const QueryParamsMap& queryParametersMap,
587 MQTopicCallback attributeHandler,
588 QualityOfService qos)
590 return checked_guard(m_clientWrapper.lock(),
591 &IClientWrapper::ListenForMQTopic,
593 queryParametersMap, m_headerOptions,
594 attributeHandler, qos);
597 OCStackResult OCResource::createMQTopic(const OCRepresentation& rep,
598 const std::string& topicUri,
599 const QueryParamsMap& queryParametersMap,
600 MQTopicCallback attributeHandler,
601 QualityOfService qos)
603 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutMQTopicRepresentation,
604 m_devAddr, topicUri, rep, queryParametersMap,
605 m_headerOptions, attributeHandler, qos);
609 OCStackResult OCResource::subscribeMQTopic(ObserveType observeType,
610 const QueryParamsMap& queryParametersMap,
611 ObserveCallback observeHandler,
612 QualityOfService qos)
614 return result_guard(observe(observeType, queryParametersMap, observeHandler, qos));
617 OCStackResult OCResource::unsubscribeMQTopic(QualityOfService qos)
619 return result_guard(cancelObserve(qos));
622 OCStackResult OCResource::requestMQPublish(const QueryParamsMap& queryParametersMap,
623 PostCallback attributeHandler,
624 QualityOfService qos)
626 OCRepresentation rep;
627 rep.setValue(std::string("req_pub"), std::string("true"));
628 return result_guard(post(rep, queryParametersMap, attributeHandler, qos));
632 OCStackResult OCResource::publishMQTopic(const OCRepresentation& rep,
633 const QueryParamsMap& queryParametersMap,
634 PostCallback attributeHandler,
635 QualityOfService qos)
637 return result_guard(post(rep, queryParametersMap, attributeHandler, qos));
641 bool OCResource::operator==(const OCResource &other) const
643 return m_resourceId == other.m_resourceId;
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 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
672 const std::string& resourceUri)
673 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
677 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
679 os << ri.m_representation<<ri.m_resourceUri;
684 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
686 return m_representation == other.m_representation
687 && m_resourceUri == other.m_resourceUri;
690 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
692 return !(*this == other);
695 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
697 return m_resourceUri < other.m_resourceUri
698 || (m_resourceUri == other.m_resourceUri &&
699 m_representation < other.m_representation);
702 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
704 return *this != other && !(*this<other);
707 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
709 return !(*this > other);
712 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
714 return !(*this < other);