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 COAPS_TCP[] = "coaps+tcp://";
43 static const char COAP_GATT[] = "coap+gatt://";
44 static const char COAP_RFCOMM[] = "coap+rfcomm://";
47 using OC::result_guard;
48 using OC::checked_guard;
50 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
51 const OCDevAddr& devAddr, const std::string& uri,
52 const std::string& serverId, uint8_t property,
53 const std::vector<std::string>& resourceTypes,
54 const std::vector<std::string>& interfaces)
55 : m_clientWrapper(clientWrapper), m_uri(uri),
56 m_resourceId(serverId, m_uri), m_devAddr(devAddr),
57 m_isCollection(false), m_property(property),
58 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
59 m_observeHandle(nullptr)
61 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
62 != m_interfaces.end();
65 resourceTypes.empty() ||
67 m_clientWrapper.expired())
69 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
70 interfaces.empty(), m_clientWrapper.expired(), false, false);
74 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
75 const std::string& host, const std::string& uri,
76 const std::string& serverId,
77 OCConnectivityType connectivityType, uint8_t property,
78 const std::vector<std::string>& resourceTypes,
79 const std::vector<std::string>& interfaces)
80 : m_clientWrapper(clientWrapper), m_uri(uri),
81 m_resourceId(serverId, m_uri),
82 m_isCollection(false), m_property(property),
83 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
84 m_observeHandle(nullptr)
86 m_devAddr = OCDevAddr{OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0,
87 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
91 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
92 != m_interfaces.end();
95 resourceTypes.empty() ||
97 m_clientWrapper.expired())
99 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
100 interfaces.empty(), m_clientWrapper.expired(), false, false);
103 if (uri.length() == 1 && uri[0] == '/')
105 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
106 interfaces.empty(), m_clientWrapper.expired(), false, false);
111 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
112 interfaces.empty(), m_clientWrapper.expired(), false, false);
115 // construct the devAddr from the pieces we have
116 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
117 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
122 OCResource::~OCResource()
126 void OCResource::setHost(const std::string& host)
130 if (host.compare(0, sizeof(COAP) - 1, COAP) == 0)
132 prefix_len = sizeof(COAP) - 1;
134 else if (host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
136 prefix_len = sizeof(COAPS) - 1;
137 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags | OC_SECURE);
139 else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
141 prefix_len = sizeof(COAP_TCP) - 1;
143 else if (host.compare(0, sizeof(COAPS_TCP) - 1, COAPS_TCP) == 0)
145 prefix_len = sizeof(COAPS_TCP) - 1;
146 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags | OC_SECURE);
148 else if (host.compare(0, sizeof(COAP_GATT) - 1, COAP_GATT) == 0)
150 prefix_len = sizeof(COAP_GATT) - 1;
152 else if (host.compare(0, sizeof(COAP_RFCOMM) - 1, COAP_RFCOMM) == 0)
154 prefix_len = sizeof(COAP_RFCOMM) - 1;
158 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
159 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
162 // remove 'coap://' or 'coaps://' or 'coap+tcp://' or 'coap+gatt://' or 'coap+rfcomm://'
163 std::string host_token = host.substr(prefix_len);
165 if (host_token[0] == '[') // IPv6
167 size_t bracket = host_token.find(']');
169 if (std::string::npos == bracket || 0 == bracket)
171 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
172 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
174 // extract the ipv6 address
175 std::string ip6Addr = host_token.substr(1, bracket - 1);
177 // address validity check
178 std::string ip6AddrToValidityCheck(ip6Addr);
179 size_t percent = ip6AddrToValidityCheck.find('%');
180 if (std::string::npos != percent)
182 ip6AddrToValidityCheck.resize(percent);
185 const char *cAddr = ip6AddrToValidityCheck.c_str();
186 if (0 == inet_pton(AF_INET6, cAddr, &buf))
188 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
189 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
192 //skip ']' and ':' characters in host string
193 host_token = host_token.substr(bracket + 2);
194 int port = std::stoi(host_token);
196 if (0 > port || UINT16_MAX < port)
198 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
199 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
202 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
203 m_devAddr.addr[ip6Addr.length()] = '\0';
204 m_devAddr.port = static_cast<uint16_t>(port);
205 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
207 else if (host_token[0] == ':')
209 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
210 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
214 size_t dot = host_token.find('.');
215 if (std::string::npos == dot) // MAC
217 std::string macAddr = host_token;
219 // address validity check
220 if (MAC_ADDR_STR_SIZE != macAddr.length())
222 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
223 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
226 for (size_t blockCnt = 0; blockCnt < MAC_ADDR_BLOCKS; blockCnt++)
228 std::string block = macAddr.substr(blockCnt * 3, 2);
230 if (std::string::npos != block.find_first_not_of("0123456789ABCDEFabcdef"))
232 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
233 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
236 if (MAC_ADDR_BLOCKS - 1 > blockCnt)
238 char delimiter = macAddr[blockCnt * 3 + 2];
240 if (':' != delimiter)
242 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
243 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
248 macAddr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
249 m_devAddr.addr[MAC_ADDR_STR_SIZE] = '\0';
253 size_t colon = host_token.find(':');
255 if (colon == std::string::npos || colon == 0)
257 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
258 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
261 // extract the ipv4 address
262 std::string ip4Addr = host_token.substr(0, colon);
264 // address validity check
266 const char *cAddr = ip4Addr.c_str();
267 if (0 == inet_pton(AF_INET, cAddr, &buf))
269 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
270 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
273 //skip ':' characters in host string
274 host_token = host_token.substr(colon + 1);
275 int port = std::stoi(host_token);
277 if (0 > port || UINT16_MAX < port)
279 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
280 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
283 ip4Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
284 m_devAddr.addr[ip4Addr.length()] = '\0';
285 m_devAddr.port = static_cast<uint16_t>(port);
290 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
291 GetCallback attributeHandler, QualityOfService QoS)
293 return checked_guard(m_clientWrapper.lock(),
294 &IClientWrapper::GetResourceRepresentation,
296 queryParametersMap, m_headerOptions, CT_DEFAULT,
297 attributeHandler, QoS);
300 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
301 GetCallback attributeHandler)
303 QualityOfService defaultQos = OC::QualityOfService::NaQos;
304 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
305 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
308 OCStackResult OCResource::get(const std::string& resourceType,
309 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
310 GetCallback attributeHandler)
312 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
313 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
315 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
318 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
319 QualityOfService QoS)
321 QueryParamsMap mapCpy(queryParametersMap);
323 if (!resourceType.empty())
325 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
328 if (!resourceInterface.empty())
330 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
333 return result_guard(get(mapCpy, attributeHandler, QoS));
336 OCStackResult OCResource::put(const OCRepresentation& rep,
337 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
338 QualityOfService QoS)
340 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
341 m_devAddr, m_uri, rep, queryParametersMap,
342 m_headerOptions, attributeHandler, QoS);
345 OCStackResult OCResource::put(const OCRepresentation& rep,
346 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
348 QualityOfService defaultQos = OC::QualityOfService::NaQos;
349 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
350 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
353 OCStackResult OCResource::put(const std::string& resourceType,
354 const std::string& resourceInterface, const OCRepresentation& rep,
355 const QueryParamsMap& queryParametersMap,
356 PutCallback attributeHandler)
358 QualityOfService defaultQos = OC::QualityOfService::NaQos;
359 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
361 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
362 attributeHandler, defaultQos));
365 OCStackResult OCResource::put(const std::string& resourceType,
366 const std::string& resourceInterface, const OCRepresentation& rep,
367 const QueryParamsMap& queryParametersMap,
368 PutCallback attributeHandler,
369 QualityOfService QoS)
371 QueryParamsMap mapCpy(queryParametersMap);
373 if (!resourceType.empty())
375 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
378 if (!resourceInterface.empty())
380 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
383 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
386 OCStackResult OCResource::post(const OCRepresentation& rep,
387 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
388 QualityOfService QoS)
390 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
391 m_devAddr, m_uri, rep, queryParametersMap,
392 m_headerOptions, CT_DEFAULT, attributeHandler, QoS);
395 OCStackResult OCResource::post(const OCRepresentation& rep,
396 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
398 QualityOfService defaultQos = OC::QualityOfService::NaQos;
399 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
400 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
403 OCStackResult OCResource::post(const std::string& resourceType,
404 const std::string& resourceInterface, const OCRepresentation& rep,
405 const QueryParamsMap& queryParametersMap,
406 PostCallback attributeHandler)
408 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
409 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
411 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
415 OCStackResult OCResource::post(const std::string& resourceType,
416 const std::string& resourceInterface, const OCRepresentation& rep,
417 const QueryParamsMap& queryParametersMap,
418 PostCallback attributeHandler,
419 QualityOfService QoS)
421 QueryParamsMap mapCpy(queryParametersMap);
423 if (!resourceType.empty())
425 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
428 if (!resourceInterface.empty())
430 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
433 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
436 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
438 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
439 m_devAddr, m_uri, m_headerOptions, CT_DEFAULT, deleteHandler, QoS);
442 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
444 QualityOfService defaultQos = OC::QualityOfService::NaQos;
445 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
447 return result_guard(deleteResource(deleteHandler, defaultQos));
450 OCStackResult OCResource::observe(ObserveType observeType,
451 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
452 QualityOfService QoS)
454 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
455 observeType, &m_observeHandle, m_devAddr,
456 m_uri, queryParametersMap, m_headerOptions,
457 observeHandler, QoS);
460 OCStackResult OCResource::observe(ObserveType observeType,
461 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
463 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
464 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
466 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
469 OCStackResult OCResource::cancelObserve()
471 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
472 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
473 return result_guard(cancelObserve(defaultQoS));
476 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
478 if (m_observeHandle == nullptr)
480 return result_guard(OC_STACK_INVALID_PARAM);
483 OCStackResult result = checked_guard(m_clientWrapper.lock(),
484 &IClientWrapper::CancelObserveResource,
485 m_observeHandle, (const char*)"", m_uri, m_headerOptions, QoS);
487 if (result == OC_STACK_OK)
489 m_observeHandle = nullptr;
495 void OCResource::setHeaderOptions(const HeaderOptions& headerOptions)
497 m_headerOptions = headerOptions;
500 void OCResource::unsetHeaderOptions()
502 m_headerOptions.clear();
505 std::string OCResource::host() const
507 std::ostringstream ss;
509 if (m_devAddr.adapter & OC_ADAPTER_TCP)
511 if (m_devAddr.flags & OC_SECURE)
520 else if (m_devAddr.adapter & OC_ADAPTER_GATT_BTLE)
524 else if (m_devAddr.adapter & OC_ADAPTER_RFCOMM_BTEDR)
530 if (m_devAddr.flags & OC_SECURE)
540 if (m_devAddr.flags & OC_IP_USE_V6)
542 ss << '[' << m_devAddr.addr << ']';
546 ss << m_devAddr.addr;
550 ss << ':' << m_devAddr.port;
555 std::string OCResource::uri() const
560 OCConnectivityType OCResource::connectivityType() const
562 return static_cast<OCConnectivityType>(
563 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
566 bool OCResource::isObservable() const
568 return (m_property & OC_OBSERVABLE) == OC_OBSERVABLE;
572 bool OCResource::isPublish() const
574 return (m_property & OC_MQ_PUBLISHER) == OC_MQ_PUBLISHER;
578 std::vector<std::string> OCResource::getResourceTypes() const
580 return m_resourceTypes;
583 std::vector<std::string> OCResource::getResourceInterfaces(void) const
588 OCResourceIdentifier OCResource::uniqueIdentifier() const
593 std::string OCResource::sid() const
595 return this->uniqueIdentifier().m_representation;
599 OCStackResult OCResource::discoveryMQTopics(const QueryParamsMap& queryParametersMap,
600 MQTopicCallback attributeHandler,
601 QualityOfService qos)
603 return checked_guard(m_clientWrapper.lock(),
604 &IClientWrapper::ListenForMQTopic,
606 queryParametersMap, m_headerOptions,
607 attributeHandler, qos);
610 OCStackResult OCResource::createMQTopic(const OCRepresentation& rep,
611 const std::string& topicUri,
612 const QueryParamsMap& queryParametersMap,
613 MQTopicCallback attributeHandler,
614 QualityOfService qos)
616 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutMQTopicRepresentation,
617 m_devAddr, topicUri, rep, queryParametersMap,
618 m_headerOptions, attributeHandler, qos);
622 OCStackResult OCResource::subscribeMQTopic(ObserveType observeType,
623 const QueryParamsMap& queryParametersMap,
624 ObserveCallback observeHandler,
625 QualityOfService qos)
627 return result_guard(observe(observeType, queryParametersMap, observeHandler, qos));
630 OCStackResult OCResource::unsubscribeMQTopic(QualityOfService qos)
632 return result_guard(cancelObserve(qos));
635 OCStackResult OCResource::requestMQPublish(const QueryParamsMap& queryParametersMap,
636 PostCallback attributeHandler,
637 QualityOfService qos)
639 OCRepresentation rep;
640 rep.setValue(std::string("req_pub"), std::string("true"));
641 return result_guard(post(rep, queryParametersMap, attributeHandler, qos));
645 OCStackResult OCResource::publishMQTopic(const OCRepresentation& rep,
646 const QueryParamsMap& queryParametersMap,
647 PostCallback attributeHandler,
648 QualityOfService qos)
650 return result_guard(post(rep, queryParametersMap, attributeHandler, qos));
654 bool OCResource::operator==(const OCResource &other) const
656 return m_resourceId == other.m_resourceId;
659 bool OCResource::operator!=(const OCResource &other) const
661 return m_resourceId != other.m_resourceId;
664 bool OCResource::operator<(const OCResource &other) const
666 return m_resourceId < other.m_resourceId;
669 bool OCResource::operator>(const OCResource &other) const
671 return m_resourceId > other.m_resourceId;
674 bool OCResource::operator<=(const OCResource &other) const
676 return m_resourceId <= other.m_resourceId;
679 bool OCResource::operator>=(const OCResource &other) const
681 return m_resourceId >= other.m_resourceId;
684 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
685 const std::string& resourceUri)
686 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
690 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
692 os << ri.m_representation<<ri.m_resourceUri;
697 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
699 return m_representation == other.m_representation
700 && m_resourceUri == other.m_resourceUri;
703 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
705 return !(*this == other);
708 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
710 return m_resourceUri < other.m_resourceUri
711 || (m_resourceUri == other.m_resourceUri &&
712 m_representation < other.m_representation);
715 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
717 return *this != other && !(*this<other);
720 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
722 return !(*this > other);
725 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
727 return !(*this < other);