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://";
33 static const char COAP_TCP[] = "coap+tcp://";
37 using OC::result_guard;
38 using OC::checked_guard;
40 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
41 const OCDevAddr& devAddr, const std::string& uri,
42 const std::string& serverId, bool observable,
43 const std::vector<std::string>& resourceTypes,
44 const std::vector<std::string>& interfaces)
45 : m_clientWrapper(clientWrapper), m_uri(uri),
46 m_resourceId(serverId, m_uri), m_devAddr(devAddr),
47 m_isObservable(observable), m_isCollection(false),
48 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
49 m_observeHandle(nullptr)
51 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
52 != m_interfaces.end();
55 resourceTypes.empty() ||
57 m_clientWrapper.expired())
59 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
60 interfaces.empty(), m_clientWrapper.expired(), false, false);
64 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
65 const std::string& host, const std::string& uri,
66 const std::string& serverId,
67 OCConnectivityType connectivityType, bool observable,
68 const std::vector<std::string>& resourceTypes,
69 const std::vector<std::string>& interfaces)
70 : m_clientWrapper(clientWrapper), m_uri(uri),
71 m_resourceId(serverId, m_uri),
72 m_devAddr{ OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0
73 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
77 m_isObservable(observable), m_isCollection(false),
78 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
79 m_observeHandle(nullptr)
81 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
82 != m_interfaces.end();
85 resourceTypes.empty() ||
87 m_clientWrapper.expired())
89 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
90 interfaces.empty(), m_clientWrapper.expired(), false, false);
93 if (uri.length() == 1 && uri[0] == '/')
95 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
96 interfaces.empty(), m_clientWrapper.expired(), false, false);
101 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
102 interfaces.empty(), m_clientWrapper.expired(), false, false);
105 // construct the devAddr from the pieces we have
106 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
107 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
112 OCResource::~OCResource()
116 void OCResource::setHost(const std::string& host)
120 if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
122 prefix_len = sizeof(COAP) - 1;
124 else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
126 prefix_len = sizeof(COAPS) - 1;
127 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
130 else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
132 prefix_len = sizeof(COAP_TCP) - 1;
133 m_devAddr.adapter = static_cast<OCTransportAdapter>(m_devAddr.adapter & OC_ADAPTER_TCP);
138 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
139 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
142 // removed coap:// or coaps:// or coap+tcp://
143 std::string host_token = host.substr(prefix_len);
145 if(host_token[0] == '[')
147 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
149 size_t found = host_token.find(']');
151 if(found == std::string::npos || found == 0)
153 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
154 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
156 // extract the ipaddress
157 std::string ip6Addr = host_token.substr(1, found-1);
159 if (ip6Addr.length() >= MAX_ADDR_STR_SIZE)
161 throw std::length_error("host address is too long.");
164 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
165 m_devAddr.addr[ip6Addr.length()] = '\0';
166 //skip ']' and ':' characters in host string
167 host_token = host_token.substr(found + 2);
171 size_t found = host_token.find(':');
173 if(found == std::string::npos || found == 0)
175 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
176 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
179 std::string addrPart = host_token.substr(0, found);
181 if (addrPart.length() >= MAX_ADDR_STR_SIZE)
183 throw std::length_error("host address is too long.");
186 addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
187 m_devAddr.addr[addrPart.length()] = '\0';
188 //skip ':' character in host string
189 host_token = host_token.substr(found + 1);
192 int port = std::stoi(host_token);
194 if( port < 0 || port > UINT16_MAX )
196 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
197 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
200 m_devAddr.port = static_cast<uint16_t>(port);
204 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
205 GetCallback attributeHandler, QualityOfService QoS)
207 return checked_guard(m_clientWrapper.lock(),
208 &IClientWrapper::GetResourceRepresentation,
210 queryParametersMap, m_headerOptions,
211 attributeHandler, QoS);
214 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
215 GetCallback attributeHandler)
217 QualityOfService defaultQos = OC::QualityOfService::NaQos;
218 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
219 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
222 OCStackResult OCResource::get(const std::string& resourceType,
223 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
224 GetCallback attributeHandler)
226 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
227 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
229 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
232 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
233 QualityOfService QoS)
235 QueryParamsMap mapCpy(queryParametersMap);
237 if(!resourceType.empty())
239 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
242 if(!resourceInterface.empty())
244 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
247 return result_guard(get(mapCpy, attributeHandler, QoS));
250 OCStackResult OCResource::put(const OCRepresentation& rep,
251 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
252 QualityOfService QoS)
254 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
255 m_devAddr, m_uri, rep, queryParametersMap,
256 m_headerOptions, attributeHandler, QoS);
259 OCStackResult OCResource::put(const OCRepresentation& rep,
260 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
262 QualityOfService defaultQos = OC::QualityOfService::NaQos;
263 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
264 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
267 OCStackResult OCResource::put(const std::string& resourceType,
268 const std::string& resourceInterface, const OCRepresentation& rep,
269 const QueryParamsMap& queryParametersMap,
270 PutCallback attributeHandler)
272 QualityOfService defaultQos = OC::QualityOfService::NaQos;
273 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
275 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
276 attributeHandler, defaultQos));
279 OCStackResult OCResource::put(const std::string& resourceType,
280 const std::string& resourceInterface, const OCRepresentation& rep,
281 const QueryParamsMap& queryParametersMap,
282 PutCallback attributeHandler,
283 QualityOfService QoS)
285 QueryParamsMap mapCpy(queryParametersMap);
287 if(!resourceType.empty())
289 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
292 if(!resourceInterface.empty())
294 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
297 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
300 OCStackResult OCResource::post(const OCRepresentation& rep,
301 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
302 QualityOfService QoS)
304 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
305 m_devAddr, m_uri, rep, queryParametersMap,
306 m_headerOptions, attributeHandler, QoS);
309 OCStackResult OCResource::post(const OCRepresentation& rep,
310 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
312 QualityOfService defaultQos = OC::QualityOfService::NaQos;
313 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
314 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
317 OCStackResult OCResource::post(const std::string& resourceType,
318 const std::string& resourceInterface, const OCRepresentation& rep,
319 const QueryParamsMap& queryParametersMap,
320 PostCallback attributeHandler)
322 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
323 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
325 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
329 OCStackResult OCResource::post(const std::string& resourceType,
330 const std::string& resourceInterface, const OCRepresentation& rep,
331 const QueryParamsMap& queryParametersMap,
332 PostCallback attributeHandler,
333 QualityOfService QoS)
335 QueryParamsMap mapCpy(queryParametersMap);
337 if(!resourceType.empty())
339 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
342 if(!resourceInterface.empty())
344 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
347 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
350 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
352 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
353 m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
356 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
358 QualityOfService defaultQos = OC::QualityOfService::NaQos;
359 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
361 return result_guard(deleteResource(deleteHandler, defaultQos));
364 OCStackResult OCResource::observe(ObserveType observeType,
365 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
366 QualityOfService QoS)
368 if(m_observeHandle != nullptr)
370 return result_guard(OC_STACK_INVALID_PARAM);
373 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
374 observeType, &m_observeHandle, m_devAddr,
375 m_uri, queryParametersMap, m_headerOptions,
376 observeHandler, QoS);
379 OCStackResult OCResource::observe(ObserveType observeType,
380 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
382 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
383 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
385 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
388 OCStackResult OCResource::cancelObserve()
390 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
391 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
392 return result_guard(cancelObserve(defaultQoS));
395 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
397 if(m_observeHandle == nullptr)
399 return result_guard(OC_STACK_INVALID_PARAM);
402 OCStackResult result = checked_guard(m_clientWrapper.lock(),
403 &IClientWrapper::CancelObserveResource,
404 m_observeHandle, "", m_uri, m_headerOptions, QoS);
406 if(result == OC_STACK_OK)
408 m_observeHandle = nullptr;
414 void OCResource::setHeaderOptions(const HeaderOptions& headerOptions)
416 m_headerOptions = headerOptions;
419 void OCResource::unsetHeaderOptions()
421 m_headerOptions.clear();
424 std::string OCResource::host() const
426 std::ostringstream ss;
427 if (m_devAddr.flags & OC_SECURE)
432 else if (m_devAddr.adapter & OC_ADAPTER_TCP)
441 if (m_devAddr.flags & OC_IP_USE_V6)
443 ss << '[' << m_devAddr.addr << ']';
447 ss << m_devAddr.addr;
451 ss << ':' << m_devAddr.port;
456 std::string OCResource::uri() const
461 OCConnectivityType OCResource::connectivityType() const
463 return static_cast<OCConnectivityType>(
464 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
467 bool OCResource::isObservable() const
469 return m_isObservable;
472 std::vector<std::string> OCResource::getResourceTypes() const
474 return m_resourceTypes;
477 std::vector<std::string> OCResource::getResourceInterfaces(void) const
482 OCResourceIdentifier OCResource::uniqueIdentifier() const
487 std::string OCResource::sid() const
489 return this->uniqueIdentifier().m_representation;
492 bool OCResource::operator==(const OCResource &other) const
494 return m_resourceId == other.m_resourceId;
497 bool OCResource::operator!=(const OCResource &other) const
499 return m_resourceId != other.m_resourceId;
502 bool OCResource::operator<(const OCResource &other) const
504 return m_resourceId < other.m_resourceId;
507 bool OCResource::operator>(const OCResource &other) const
509 return m_resourceId > other.m_resourceId;
512 bool OCResource::operator<=(const OCResource &other) const
514 return m_resourceId <= other.m_resourceId;
517 bool OCResource::operator>=(const OCResource &other) const
519 return m_resourceId >= other.m_resourceId;
522 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
523 const std::string& resourceUri)
524 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
528 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
530 os << ri.m_representation<<ri.m_resourceUri;
535 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
537 return m_representation == other.m_representation
538 && m_resourceUri == other.m_resourceUri;
541 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
543 return !(*this == other);
546 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
548 return m_resourceUri < other.m_resourceUri
549 || (m_resourceUri == other.m_resourceUri &&
550 m_representation < other.m_representation);
553 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
555 return *this != other && !(*this<other);
558 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
560 return !(*this > other);
563 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
565 return !(*this < other);