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 (std::string::npos == bracket || 0 == bracket)
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
171 std::string ip6AddrToValidityCheck(ip6Addr);
172 size_t percent = ip6AddrToValidityCheck.find('%');
173 if (std::string::npos != percent)
175 ip6AddrToValidityCheck.resize(percent);
178 const char *cAddr = ip6AddrToValidityCheck.c_str();
179 if (0 == inet_pton(AF_INET6, cAddr, &buf))
181 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
182 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
185 //skip ']' and ':' characters in host string
186 host_token = host_token.substr(bracket + 2);
187 int port = std::stoi(host_token);
189 if (0 > port || UINT16_MAX < port)
191 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
192 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
195 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
196 m_devAddr.addr[ip6Addr.length()] = '\0';
197 m_devAddr.port = static_cast<uint16_t>(port);
198 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
200 else if (host_token[0] == ':')
202 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
203 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
207 size_t dot = host_token.find('.');
208 if (std::string::npos == dot) // MAC
210 std::string macAddr = host_token;
212 // address validity check
213 if (MAC_ADDR_STR_SIZE != macAddr.length())
215 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
216 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
219 for (size_t blockCnt = 0; blockCnt < MAC_ADDR_BLOCKS; blockCnt++)
221 std::string block = macAddr.substr(blockCnt * 3, 2);
223 if (std::string::npos != block.find_first_not_of("0123456789ABCDEFabcdef"))
225 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
226 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
229 if (MAC_ADDR_BLOCKS - 1 > blockCnt)
231 char delimiter = macAddr[blockCnt * 3 + 2];
233 if (':' != delimiter)
235 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
236 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
241 macAddr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
242 m_devAddr.addr[MAC_ADDR_STR_SIZE] = '\0';
246 size_t colon = host_token.find(':');
248 if (colon == std::string::npos || colon == 0)
250 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
251 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
254 // extract the ipv4 address
255 std::string ip4Addr = host_token.substr(0, colon);
257 // address validity check
259 const char *cAddr = ip4Addr.c_str();
260 if (0 == inet_pton(AF_INET, cAddr, &buf))
262 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
263 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
266 //skip ':' characters in host string
267 host_token = host_token.substr(colon + 1);
268 int port = std::stoi(host_token);
270 if (0 > port || UINT16_MAX < port)
272 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
273 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
276 ip4Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
277 m_devAddr.addr[ip4Addr.length()] = '\0';
278 m_devAddr.port = static_cast<uint16_t>(port);
283 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
284 GetCallback attributeHandler, QualityOfService QoS)
286 return checked_guard(m_clientWrapper.lock(),
287 &IClientWrapper::GetResourceRepresentation,
289 queryParametersMap, m_headerOptions, CT_DEFAULT,
290 attributeHandler, QoS);
293 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
294 GetCallback attributeHandler)
296 QualityOfService defaultQos = OC::QualityOfService::NaQos;
297 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
298 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
301 OCStackResult OCResource::get(const std::string& resourceType,
302 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
303 GetCallback attributeHandler)
305 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
306 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
308 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
311 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
312 QualityOfService QoS)
314 QueryParamsMap mapCpy(queryParametersMap);
316 if (!resourceType.empty())
318 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
321 if (!resourceInterface.empty())
323 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
326 return result_guard(get(mapCpy, attributeHandler, QoS));
329 OCStackResult OCResource::put(const OCRepresentation& rep,
330 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
331 QualityOfService QoS)
333 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
334 m_devAddr, m_uri, rep, queryParametersMap,
335 m_headerOptions, attributeHandler, QoS);
338 OCStackResult OCResource::put(const OCRepresentation& rep,
339 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
341 QualityOfService defaultQos = OC::QualityOfService::NaQos;
342 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
343 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
346 OCStackResult OCResource::put(const std::string& resourceType,
347 const std::string& resourceInterface, const OCRepresentation& rep,
348 const QueryParamsMap& queryParametersMap,
349 PutCallback attributeHandler)
351 QualityOfService defaultQos = OC::QualityOfService::NaQos;
352 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
354 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
355 attributeHandler, defaultQos));
358 OCStackResult OCResource::put(const std::string& resourceType,
359 const std::string& resourceInterface, const OCRepresentation& rep,
360 const QueryParamsMap& queryParametersMap,
361 PutCallback attributeHandler,
362 QualityOfService QoS)
364 QueryParamsMap mapCpy(queryParametersMap);
366 if (!resourceType.empty())
368 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
371 if (!resourceInterface.empty())
373 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
376 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
379 OCStackResult OCResource::post(const OCRepresentation& rep,
380 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
381 QualityOfService QoS)
383 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
384 m_devAddr, m_uri, rep, queryParametersMap,
385 m_headerOptions, CT_DEFAULT, attributeHandler, QoS);
388 OCStackResult OCResource::post(const OCRepresentation& rep,
389 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
391 QualityOfService defaultQos = OC::QualityOfService::NaQos;
392 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
393 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
396 OCStackResult OCResource::post(const std::string& resourceType,
397 const std::string& resourceInterface, const OCRepresentation& rep,
398 const QueryParamsMap& queryParametersMap,
399 PostCallback attributeHandler)
401 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
402 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
404 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
408 OCStackResult OCResource::post(const std::string& resourceType,
409 const std::string& resourceInterface, const OCRepresentation& rep,
410 const QueryParamsMap& queryParametersMap,
411 PostCallback attributeHandler,
412 QualityOfService QoS)
414 QueryParamsMap mapCpy(queryParametersMap);
416 if (!resourceType.empty())
418 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
421 if (!resourceInterface.empty())
423 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
426 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
429 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
431 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
432 m_devAddr, m_uri, m_headerOptions, CT_DEFAULT, deleteHandler, QoS);
435 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
437 QualityOfService defaultQos = OC::QualityOfService::NaQos;
438 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
440 return result_guard(deleteResource(deleteHandler, defaultQos));
443 OCStackResult OCResource::observe(ObserveType observeType,
444 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
445 QualityOfService QoS)
447 if (m_observeHandle != nullptr)
449 return result_guard(OC_STACK_INVALID_PARAM);
452 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
453 observeType, &m_observeHandle, m_devAddr,
454 m_uri, queryParametersMap, m_headerOptions,
455 observeHandler, QoS);
458 OCStackResult OCResource::observe(ObserveType observeType,
459 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
461 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
462 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
464 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
467 OCStackResult OCResource::cancelObserve()
469 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
470 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
471 return result_guard(cancelObserve(defaultQoS));
474 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
476 if (m_observeHandle == nullptr)
478 return result_guard(OC_STACK_INVALID_PARAM);
481 OCStackResult result = checked_guard(m_clientWrapper.lock(),
482 &IClientWrapper::CancelObserveResource,
483 m_observeHandle, (const char*)"", m_uri, m_headerOptions, QoS);
485 if (result == OC_STACK_OK)
487 m_observeHandle = nullptr;
493 void OCResource::setHeaderOptions(const HeaderOptions& headerOptions)
495 m_headerOptions = headerOptions;
498 void OCResource::unsetHeaderOptions()
500 m_headerOptions.clear();
503 std::string OCResource::host() const
505 std::ostringstream ss;
507 if (m_devAddr.adapter & OC_ADAPTER_TCP)
511 else if (m_devAddr.adapter & OC_ADAPTER_GATT_BTLE)
515 else if (m_devAddr.adapter & OC_ADAPTER_RFCOMM_BTEDR)
521 if (m_devAddr.flags & OC_SECURE)
531 if (m_devAddr.flags & OC_IP_USE_V6)
533 ss << '[' << m_devAddr.addr << ']';
537 ss << m_devAddr.addr;
541 ss << ':' << m_devAddr.port;
546 std::string OCResource::uri() const
551 OCConnectivityType OCResource::connectivityType() const
553 return static_cast<OCConnectivityType>(
554 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
557 bool OCResource::isObservable() const
559 return (m_property & OC_OBSERVABLE) == OC_OBSERVABLE;
563 bool OCResource::isPublish() const
565 return (m_property & OC_MQ_PUBLISHER) == OC_MQ_PUBLISHER;
569 std::vector<std::string> OCResource::getResourceTypes() const
571 return m_resourceTypes;
574 std::vector<std::string> OCResource::getResourceInterfaces(void) const
579 OCResourceIdentifier OCResource::uniqueIdentifier() const
584 std::string OCResource::sid() const
586 return this->uniqueIdentifier().m_representation;
590 OCStackResult OCResource::discoveryMQTopics(const QueryParamsMap& queryParametersMap,
591 MQTopicCallback attributeHandler,
592 QualityOfService qos)
594 return checked_guard(m_clientWrapper.lock(),
595 &IClientWrapper::ListenForMQTopic,
597 queryParametersMap, m_headerOptions,
598 attributeHandler, qos);
601 OCStackResult OCResource::createMQTopic(const OCRepresentation& rep,
602 const std::string& topicUri,
603 const QueryParamsMap& queryParametersMap,
604 MQTopicCallback attributeHandler,
605 QualityOfService qos)
607 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutMQTopicRepresentation,
608 m_devAddr, topicUri, rep, queryParametersMap,
609 m_headerOptions, attributeHandler, qos);
613 OCStackResult OCResource::subscribeMQTopic(ObserveType observeType,
614 const QueryParamsMap& queryParametersMap,
615 ObserveCallback observeHandler,
616 QualityOfService qos)
618 return result_guard(observe(observeType, queryParametersMap, observeHandler, qos));
621 OCStackResult OCResource::unsubscribeMQTopic(QualityOfService qos)
623 return result_guard(cancelObserve(qos));
626 OCStackResult OCResource::requestMQPublish(const QueryParamsMap& queryParametersMap,
627 PostCallback attributeHandler,
628 QualityOfService qos)
630 OCRepresentation rep;
631 rep.setValue(std::string("req_pub"), std::string("true"));
632 return result_guard(post(rep, queryParametersMap, attributeHandler, qos));
636 OCStackResult OCResource::publishMQTopic(const OCRepresentation& rep,
637 const QueryParamsMap& queryParametersMap,
638 PostCallback attributeHandler,
639 QualityOfService qos)
641 return result_guard(post(rep, queryParametersMap, attributeHandler, qos));
645 bool OCResource::operator==(const OCResource &other) const
647 return m_resourceId == other.m_resourceId;
650 bool OCResource::operator!=(const OCResource &other) const
652 return m_resourceId != other.m_resourceId;
655 bool OCResource::operator<(const OCResource &other) const
657 return m_resourceId < other.m_resourceId;
660 bool OCResource::operator>(const OCResource &other) const
662 return m_resourceId > other.m_resourceId;
665 bool OCResource::operator<=(const OCResource &other) const
667 return m_resourceId <= other.m_resourceId;
670 bool OCResource::operator>=(const OCResource &other) const
672 return m_resourceId >= other.m_resourceId;
675 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
676 const std::string& resourceUri)
677 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
681 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
683 os << ri.m_representation<<ri.m_resourceUri;
688 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
690 return m_representation == other.m_representation
691 && m_resourceUri == other.m_resourceUri;
694 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
696 return !(*this == other);
699 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
701 return m_resourceUri < other.m_resourceUri
702 || (m_resourceUri == other.m_resourceUri &&
703 m_representation < other.m_representation);
706 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
708 return *this != other && !(*this<other);
711 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
713 return !(*this > other);
716 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
718 return !(*this < other);