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 m_isObservable(observable), m_isCollection(false),
71 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
72 m_observeHandle(nullptr)
74 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
75 != m_interfaces.end();
78 resourceTypes.empty() ||
80 m_clientWrapper.expired())
82 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
83 interfaces.empty(), m_clientWrapper.expired(), false, false);
86 if (uri.length() == 1 && uri[0] == '/')
88 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
89 interfaces.empty(), m_clientWrapper.expired(), false, false);
94 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
95 interfaces.empty(), m_clientWrapper.expired(), false, false);
98 // construct the devAddr from the pieces we have
99 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
100 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
101 size_t len = host.length();
102 if (len >= MAX_ADDR_STR_SIZE)
104 throw std::length_error("host address is too long.");
110 OCResource::~OCResource()
114 void OCResource::setHost(const std::string& host)
118 if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
120 prefix_len = sizeof(COAP) - 1;
122 else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
124 prefix_len = sizeof(COAPS) - 1;
125 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
127 else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
129 prefix_len = sizeof(COAP_TCP) - 1;
130 m_devAddr.adapter = static_cast<OCTransportAdapter>(m_devAddr.adapter & OC_ADAPTER_TCP);
134 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
135 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
138 // removed coap:// or coaps:// or coap+tcp://
139 std::string host_token = host.substr(prefix_len);
141 if(host_token[0] == '[')
143 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
145 size_t found = host_token.find(']');
147 if(found == std::string::npos || found == 0)
149 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
150 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
152 // extract the ipaddress
153 std::string ip6Addr = host_token.substr(1, found-1);
154 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
155 m_devAddr.addr[ip6Addr.length()] = '\0';
156 //skip ']' and ':' characters in host string
157 host_token = host_token.substr(found + 2);
161 size_t found = host_token.find(':');
163 if(found == std::string::npos || found == 0)
165 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
166 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
169 std::string addrPart = host_token.substr(0, found);
170 addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
171 m_devAddr.addr[addrPart.length()] = '\0';
172 //skip ':' character in host string
173 host_token = host_token.substr(found + 1);
176 int port = std::stoi(host_token);
178 if( port < 0 || port > UINT16_MAX )
180 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
181 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
184 m_devAddr.port = static_cast<uint16_t>(port);
188 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
189 GetCallback attributeHandler, QualityOfService QoS)
191 return checked_guard(m_clientWrapper.lock(),
192 &IClientWrapper::GetResourceRepresentation,
194 queryParametersMap, m_headerOptions,
195 attributeHandler, QoS);
198 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
199 GetCallback attributeHandler)
201 QualityOfService defaultQos = OC::QualityOfService::NaQos;
202 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
203 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
206 OCStackResult OCResource::get(const std::string& resourceType,
207 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
208 GetCallback attributeHandler)
210 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
211 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
213 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
216 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
217 QualityOfService QoS)
219 QueryParamsMap mapCpy(queryParametersMap);
221 if(!resourceType.empty())
223 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
226 if(!resourceInterface.empty())
228 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
231 return result_guard(get(mapCpy, attributeHandler, QoS));
234 OCStackResult OCResource::put(const OCRepresentation& rep,
235 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
236 QualityOfService QoS)
238 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
239 m_devAddr, m_uri, rep, queryParametersMap,
240 m_headerOptions, attributeHandler, QoS);
243 OCStackResult OCResource::put(const OCRepresentation& rep,
244 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
246 QualityOfService defaultQos = OC::QualityOfService::NaQos;
247 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
248 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
251 OCStackResult OCResource::put(const std::string& resourceType,
252 const std::string& resourceInterface, const OCRepresentation& rep,
253 const QueryParamsMap& queryParametersMap,
254 PutCallback attributeHandler)
256 QualityOfService defaultQos = OC::QualityOfService::NaQos;
257 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
259 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
260 attributeHandler, defaultQos));
263 OCStackResult OCResource::put(const std::string& resourceType,
264 const std::string& resourceInterface, const OCRepresentation& rep,
265 const QueryParamsMap& queryParametersMap,
266 PutCallback attributeHandler,
267 QualityOfService QoS)
269 QueryParamsMap mapCpy(queryParametersMap);
271 if(!resourceType.empty())
273 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
276 if(!resourceInterface.empty())
278 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
281 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
284 OCStackResult OCResource::post(const OCRepresentation& rep,
285 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
286 QualityOfService QoS)
288 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
289 m_devAddr, m_uri, rep, queryParametersMap,
290 m_headerOptions, attributeHandler, QoS);
293 OCStackResult OCResource::post(const OCRepresentation& rep,
294 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
296 QualityOfService defaultQos = OC::QualityOfService::NaQos;
297 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
298 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
301 OCStackResult OCResource::post(const std::string& resourceType,
302 const std::string& resourceInterface, const OCRepresentation& rep,
303 const QueryParamsMap& queryParametersMap,
304 PostCallback attributeHandler)
306 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
307 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
309 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
313 OCStackResult OCResource::post(const std::string& resourceType,
314 const std::string& resourceInterface, const OCRepresentation& rep,
315 const QueryParamsMap& queryParametersMap,
316 PostCallback attributeHandler,
317 QualityOfService QoS)
319 QueryParamsMap mapCpy(queryParametersMap);
321 if(!resourceType.empty())
323 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
326 if(!resourceInterface.empty())
328 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
331 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
334 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
336 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
337 m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
340 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
342 QualityOfService defaultQos = OC::QualityOfService::NaQos;
343 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
345 return result_guard(deleteResource(deleteHandler, defaultQos));
348 OCStackResult OCResource::observe(ObserveType observeType,
349 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
350 QualityOfService QoS)
352 if(m_observeHandle != nullptr)
354 return result_guard(OC_STACK_INVALID_PARAM);
357 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
358 observeType, &m_observeHandle, m_devAddr,
359 m_uri, queryParametersMap, m_headerOptions,
360 observeHandler, QoS);
363 OCStackResult OCResource::observe(ObserveType observeType,
364 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
366 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
367 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
369 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
372 OCStackResult OCResource::cancelObserve()
374 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
375 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
376 return result_guard(cancelObserve(defaultQoS));
379 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
381 if(m_observeHandle == nullptr)
383 return result_guard(OC_STACK_INVALID_PARAM);
386 OCStackResult result = checked_guard(m_clientWrapper.lock(),
387 &IClientWrapper::CancelObserveResource,
388 m_observeHandle, "", m_uri, m_headerOptions, QoS);
390 if(result == OC_STACK_OK)
392 m_observeHandle = nullptr;
398 void OCResource::setHeaderOptions(const HeaderOptions& headerOptions)
400 m_headerOptions = headerOptions;
403 void OCResource::unsetHeaderOptions()
405 m_headerOptions.clear();
408 std::string OCResource::host() const
410 std::ostringstream ss;
411 if (m_devAddr.flags & OC_SECURE)
415 else if (m_devAddr.adapter & OC_ADAPTER_TCP)
423 if (m_devAddr.flags & OC_IP_USE_V6)
425 ss << '[' << m_devAddr.addr << ']';
429 ss << m_devAddr.addr;
433 ss << ':' << m_devAddr.port;
438 std::string OCResource::uri() const
443 OCConnectivityType OCResource::connectivityType() const
445 return static_cast<OCConnectivityType>(
446 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
449 bool OCResource::isObservable() const
451 return m_isObservable;
454 std::vector<std::string> OCResource::getResourceTypes() const
456 return m_resourceTypes;
459 std::vector<std::string> OCResource::getResourceInterfaces(void) const
464 OCResourceIdentifier OCResource::uniqueIdentifier() const
469 std::string OCResource::sid() const
471 return this->uniqueIdentifier().m_representation;
474 bool OCResource::operator==(const OCResource &other) const
476 return m_resourceId == other.m_resourceId;
479 bool OCResource::operator!=(const OCResource &other) const
481 return m_resourceId != other.m_resourceId;
484 bool OCResource::operator<(const OCResource &other) const
486 return m_resourceId < other.m_resourceId;
489 bool OCResource::operator>(const OCResource &other) const
491 return m_resourceId > other.m_resourceId;
494 bool OCResource::operator<=(const OCResource &other) const
496 return m_resourceId <= other.m_resourceId;
499 bool OCResource::operator>=(const OCResource &other) const
501 return m_resourceId >= other.m_resourceId;
504 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
505 const std::string& resourceUri)
506 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
510 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
512 os << ri.m_representation<<ri.m_resourceUri;
517 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
519 return m_representation == other.m_representation
520 && m_resourceUri == other.m_resourceUri;
523 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
525 return !(*this == other);
528 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
530 return m_resourceUri < other.m_resourceUri
531 || (m_resourceUri == other.m_resourceUri &&
532 m_representation < other.m_representation);
535 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
537 return *this != other && !(*this<other);
540 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
542 return !(*this > other);
545 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
547 return !(*this < other);