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>
29 static const char COAP[] = "coap://";
30 static const char COAPS[] = "coaps://";
31 static const char COAP_TCP[] = "coap+tcp://";
34 using OC::result_guard;
35 using OC::checked_guard;
37 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
38 const OCDevAddr& devAddr, const std::string& uri,
39 const std::string& serverId, bool observable,
40 const std::vector<std::string>& resourceTypes,
41 const std::vector<std::string>& interfaces)
42 : m_clientWrapper(clientWrapper), m_uri(uri),
43 m_resourceId(serverId, m_uri), m_devAddr(devAddr),
44 m_isObservable(observable), m_isCollection(false),
45 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
46 m_observeHandle(nullptr)
48 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
49 != m_interfaces.end();
52 resourceTypes.empty() ||
54 m_clientWrapper.expired())
56 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
57 interfaces.empty(), m_clientWrapper.expired(), false, false);
61 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
62 const std::string& host, const std::string& uri,
63 const std::string& serverId,
64 OCConnectivityType connectivityType, bool observable,
65 const std::vector<std::string>& resourceTypes,
66 const std::vector<std::string>& interfaces)
67 : m_clientWrapper(clientWrapper), m_uri(uri),
68 m_resourceId(serverId, m_uri),
69 m_devAddr{ OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0
70 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
74 m_isObservable(observable), m_isCollection(false),
75 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
76 m_observeHandle(nullptr)
78 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
79 != m_interfaces.end();
82 resourceTypes.empty() ||
84 m_clientWrapper.expired())
86 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
87 interfaces.empty(), m_clientWrapper.expired(), false, false);
90 if (uri.length() == 1 && uri[0] == '/')
92 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
93 interfaces.empty(), m_clientWrapper.expired(), false, false);
98 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
99 interfaces.empty(), m_clientWrapper.expired(), false, false);
102 // construct the devAddr from the pieces we have
103 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
104 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
109 OCResource::~OCResource()
113 void OCResource::setHost(const std::string& host)
117 if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
119 prefix_len = sizeof(COAP) - 1;
121 else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
123 prefix_len = sizeof(COAPS) - 1;
124 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
126 else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
128 prefix_len = sizeof(COAP_TCP) - 1;
132 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
133 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
136 // removed coap:// or coaps:// or coap+tcp://
137 std::string host_token = host.substr(prefix_len);
139 if(host_token[0] == '[')
141 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
143 size_t found = host_token.find(']');
145 if(found == std::string::npos || found == 0)
147 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
148 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
150 // extract the ipaddress
151 std::string ip6Addr = host_token.substr(1, found-1);
153 if (ip6Addr.length() >= MAX_ADDR_STR_SIZE)
155 throw std::length_error("host address is too long.");
158 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
159 m_devAddr.addr[ip6Addr.length()] = '\0';
160 //skip ']' and ':' characters in host string
161 host_token = host_token.substr(found + 2);
165 size_t found = host_token.find(':');
167 if(found == std::string::npos || found == 0)
169 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
170 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
173 std::string addrPart = host_token.substr(0, found);
175 if (addrPart.length() >= MAX_ADDR_STR_SIZE)
177 throw std::length_error("host address is too long.");
180 addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
181 m_devAddr.addr[addrPart.length()] = '\0';
182 //skip ':' character in host string
183 host_token = host_token.substr(found + 1);
186 int port = std::stoi(host_token);
188 if( port < 0 || port > UINT16_MAX )
190 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
191 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
194 m_devAddr.port = static_cast<uint16_t>(port);
198 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
199 GetCallback attributeHandler, QualityOfService QoS)
201 return checked_guard(m_clientWrapper.lock(),
202 &IClientWrapper::GetResourceRepresentation,
204 queryParametersMap, m_headerOptions,
205 attributeHandler, QoS);
208 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
209 GetCallback attributeHandler)
211 QualityOfService defaultQos = OC::QualityOfService::NaQos;
212 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
213 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
216 OCStackResult OCResource::get(const std::string& resourceType,
217 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
218 GetCallback attributeHandler)
220 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
221 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
223 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
226 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
227 QualityOfService QoS)
229 QueryParamsMap mapCpy(queryParametersMap);
231 if(!resourceType.empty())
233 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
236 if(!resourceInterface.empty())
238 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
241 return result_guard(get(mapCpy, attributeHandler, QoS));
244 OCStackResult OCResource::put(const OCRepresentation& rep,
245 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
246 QualityOfService QoS)
248 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
249 m_devAddr, m_uri, rep, queryParametersMap,
250 m_headerOptions, attributeHandler, QoS);
253 OCStackResult OCResource::put(const OCRepresentation& rep,
254 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
256 QualityOfService defaultQos = OC::QualityOfService::NaQos;
257 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
258 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
261 OCStackResult OCResource::put(const std::string& resourceType,
262 const std::string& resourceInterface, const OCRepresentation& rep,
263 const QueryParamsMap& queryParametersMap,
264 PutCallback attributeHandler)
266 QualityOfService defaultQos = OC::QualityOfService::NaQos;
267 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
269 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
270 attributeHandler, defaultQos));
273 OCStackResult OCResource::put(const std::string& resourceType,
274 const std::string& resourceInterface, const OCRepresentation& rep,
275 const QueryParamsMap& queryParametersMap,
276 PutCallback attributeHandler,
277 QualityOfService QoS)
279 QueryParamsMap mapCpy(queryParametersMap);
281 if(!resourceType.empty())
283 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
286 if(!resourceInterface.empty())
288 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
291 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
294 OCStackResult OCResource::post(const OCRepresentation& rep,
295 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
296 QualityOfService QoS)
298 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
299 m_devAddr, m_uri, rep, queryParametersMap,
300 m_headerOptions, attributeHandler, QoS);
303 OCStackResult OCResource::post(const OCRepresentation& rep,
304 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
306 QualityOfService defaultQos = OC::QualityOfService::NaQos;
307 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
308 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
311 OCStackResult OCResource::post(const std::string& resourceType,
312 const std::string& resourceInterface, const OCRepresentation& rep,
313 const QueryParamsMap& queryParametersMap,
314 PostCallback attributeHandler)
316 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
317 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
319 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
323 OCStackResult OCResource::post(const std::string& resourceType,
324 const std::string& resourceInterface, const OCRepresentation& rep,
325 const QueryParamsMap& queryParametersMap,
326 PostCallback 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(post(rep, mapCpy, attributeHandler, QoS));
344 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
346 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
347 m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
350 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
352 QualityOfService defaultQos = OC::QualityOfService::NaQos;
353 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
355 return result_guard(deleteResource(deleteHandler, defaultQos));
358 OCStackResult OCResource::observe(ObserveType observeType,
359 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
360 QualityOfService QoS)
362 if(m_observeHandle != nullptr)
364 return result_guard(OC_STACK_INVALID_PARAM);
367 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
368 observeType, &m_observeHandle, m_devAddr,
369 m_uri, queryParametersMap, m_headerOptions,
370 observeHandler, QoS);
373 OCStackResult OCResource::observe(ObserveType observeType,
374 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
376 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
377 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
379 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
382 OCStackResult OCResource::cancelObserve()
384 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
385 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
386 return result_guard(cancelObserve(defaultQoS));
389 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
391 if(m_observeHandle == nullptr)
393 return result_guard(OC_STACK_INVALID_PARAM);
396 OCStackResult result = checked_guard(m_clientWrapper.lock(),
397 &IClientWrapper::CancelObserveResource,
398 m_observeHandle, "", m_uri, m_headerOptions, QoS);
400 if(result == OC_STACK_OK)
402 m_observeHandle = nullptr;
408 void OCResource::setHeaderOptions(const HeaderOptions& headerOptions)
410 m_headerOptions = headerOptions;
413 void OCResource::unsetHeaderOptions()
415 m_headerOptions.clear();
418 std::string OCResource::host() const
420 std::ostringstream ss;
421 if (m_devAddr.flags & OC_SECURE)
425 else if ((m_devAddr.adapter & OC_ADAPTER_TCP)
426 || (m_devAddr.adapter & OC_ADAPTER_GATT_BTLE)
427 || (m_devAddr.adapter & OC_ADAPTER_RFCOMM_BTEDR))
435 if (m_devAddr.flags & OC_IP_USE_V6)
437 ss << '[' << m_devAddr.addr << ']';
441 ss << m_devAddr.addr;
445 ss << ':' << m_devAddr.port;
450 std::string OCResource::uri() const
455 OCConnectivityType OCResource::connectivityType() const
457 return static_cast<OCConnectivityType>(
458 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
461 bool OCResource::isObservable() const
463 return m_isObservable;
466 std::vector<std::string> OCResource::getResourceTypes() const
468 return m_resourceTypes;
471 std::vector<std::string> OCResource::getResourceInterfaces(void) const
476 OCResourceIdentifier OCResource::uniqueIdentifier() const
481 std::string OCResource::sid() const
483 return this->uniqueIdentifier().m_representation;
486 bool OCResource::operator==(const OCResource &other) const
488 return m_resourceId == other.m_resourceId;
491 bool OCResource::operator!=(const OCResource &other) const
493 return m_resourceId != other.m_resourceId;
496 bool OCResource::operator<(const OCResource &other) const
498 return m_resourceId < other.m_resourceId;
501 bool OCResource::operator>(const OCResource &other) const
503 return m_resourceId > other.m_resourceId;
506 bool OCResource::operator<=(const OCResource &other) const
508 return m_resourceId <= other.m_resourceId;
511 bool OCResource::operator>=(const OCResource &other) const
513 return m_resourceId >= other.m_resourceId;
516 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
517 const std::string& resourceUri)
518 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
522 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
524 os << ri.m_representation<<ri.m_resourceUri;
529 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
531 return m_representation == other.m_representation
532 && m_resourceUri == other.m_resourceUri;
535 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
537 return !(*this == other);
540 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
542 return m_resourceUri < other.m_resourceUri
543 || (m_resourceUri == other.m_resourceUri &&
544 m_representation < other.m_representation);
547 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
549 return *this != other && !(*this<other);
552 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
554 return !(*this > other);
557 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
559 return !(*this < other);