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://";
32 using OC::result_guard;
33 using OC::checked_guard;
35 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
36 const OCDevAddr& devAddr, const std::string& uri,
37 const std::string& serverId, bool observable,
38 const std::vector<std::string>& resourceTypes,
39 const std::vector<std::string>& interfaces)
40 : m_clientWrapper(clientWrapper), m_uri(uri),
41 m_resourceId(serverId, m_uri), m_devAddr(devAddr),
42 m_isObservable(observable), m_isCollection(false),
43 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
44 m_observeHandle(nullptr)
46 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
47 != m_interfaces.end();
50 resourceTypes.empty() ||
52 m_clientWrapper.expired())
54 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
55 interfaces.empty(), m_clientWrapper.expired(), false, false);
59 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
60 const std::string& host, const std::string& uri,
61 const std::string& serverId,
62 OCConnectivityType connectivityType, bool observable,
63 const std::vector<std::string>& resourceTypes,
64 const std::vector<std::string>& interfaces)
65 : m_clientWrapper(clientWrapper), m_uri(uri),
66 m_resourceId(serverId, m_uri),
67 m_devAddr{ OC_DEFAULT_ADAPTER },
68 m_isObservable(observable), m_isCollection(false),
69 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
70 m_observeHandle(nullptr)
72 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
73 != m_interfaces.end();
76 resourceTypes.empty() ||
78 m_clientWrapper.expired())
80 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
81 interfaces.empty(), m_clientWrapper.expired(), false, false);
84 if (uri.length() == 1 && uri[0] == '/')
86 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
87 interfaces.empty(), m_clientWrapper.expired(), false, false);
90 // construct the devAddr from the pieces we have
91 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
92 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
93 size_t len = host.length();
94 if (len >= MAX_ADDR_STR_SIZE)
96 throw std::length_error("host address is too long.");
102 OCResource::~OCResource()
106 void OCResource::setHost(const std::string& host)
110 if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
112 prefix_len = sizeof(COAP) - 1;
114 else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
116 prefix_len = sizeof(COAPS) - 1;
117 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
121 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
122 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
125 // removed coap:// or coaps://
126 std::string host_token = host.substr(prefix_len);
128 if(host_token[0] == '[')
130 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
132 size_t found = host_token.find(']');
134 if(found == std::string::npos || found == 0)
136 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
137 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
139 // extract the ipaddress
140 std::string ip6Addr = host_token.substr(1, found-1);
141 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
142 m_devAddr.addr[ip6Addr.length()] = '\0';
143 //skip ']' and ':' characters in host string
144 host_token = host_token.substr(found + 2);
148 size_t found = host_token.find(':');
150 if(found == std::string::npos || found == 0)
152 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
153 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
156 std::string addrPart = host_token.substr(0, found);
157 addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
158 m_devAddr.addr[addrPart.length()] = '\0';
159 //skip ':' character in host string
160 host_token = host_token.substr(found + 1);
163 int port = std::stoi(host_token);
165 if( port < 0 || port > UINT16_MAX )
167 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
168 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
171 m_devAddr.port = static_cast<uint16_t>(port);
175 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
176 GetCallback attributeHandler, QualityOfService QoS)
178 return checked_guard(m_clientWrapper.lock(),
179 &IClientWrapper::GetResourceRepresentation,
181 queryParametersMap, m_headerOptions,
182 attributeHandler, QoS);
185 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
186 GetCallback attributeHandler)
188 QualityOfService defaultQos = OC::QualityOfService::NaQos;
189 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
190 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
193 OCStackResult OCResource::get(const std::string& resourceType,
194 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
195 GetCallback attributeHandler)
197 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
198 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
200 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
203 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
204 QualityOfService QoS)
206 QueryParamsMap mapCpy(queryParametersMap);
208 if(!resourceType.empty())
210 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
213 if(!resourceInterface.empty())
215 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
218 return result_guard(get(mapCpy, attributeHandler, QoS));
221 OCStackResult OCResource::put(const OCRepresentation& rep,
222 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
223 QualityOfService QoS)
225 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
226 m_devAddr, m_uri, rep, queryParametersMap,
227 m_headerOptions, attributeHandler, QoS);
230 OCStackResult OCResource::put(const OCRepresentation& rep,
231 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
233 QualityOfService defaultQos = OC::QualityOfService::NaQos;
234 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
235 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
238 OCStackResult OCResource::put(const std::string& resourceType,
239 const std::string& resourceInterface, const OCRepresentation& rep,
240 const QueryParamsMap& queryParametersMap,
241 PutCallback attributeHandler)
243 QualityOfService defaultQos = OC::QualityOfService::NaQos;
244 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
246 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
247 attributeHandler, defaultQos));
250 OCStackResult OCResource::put(const std::string& resourceType,
251 const std::string& resourceInterface, const OCRepresentation& rep,
252 const QueryParamsMap& queryParametersMap,
253 PutCallback attributeHandler,
254 QualityOfService QoS)
256 QueryParamsMap mapCpy(queryParametersMap);
258 if(!resourceType.empty())
260 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
263 if(!resourceInterface.empty())
265 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
268 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
271 OCStackResult OCResource::post(const OCRepresentation& rep,
272 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
273 QualityOfService QoS)
275 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
276 m_devAddr, m_uri, rep, queryParametersMap,
277 m_headerOptions, attributeHandler, QoS);
280 OCStackResult OCResource::post(const OCRepresentation& rep,
281 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
283 QualityOfService defaultQos = OC::QualityOfService::NaQos;
284 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
285 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
288 OCStackResult OCResource::post(const std::string& resourceType,
289 const std::string& resourceInterface, const OCRepresentation& rep,
290 const QueryParamsMap& queryParametersMap,
291 PostCallback attributeHandler)
293 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
294 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
296 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
300 OCStackResult OCResource::post(const std::string& resourceType,
301 const std::string& resourceInterface, const OCRepresentation& rep,
302 const QueryParamsMap& queryParametersMap,
303 PostCallback attributeHandler,
304 QualityOfService QoS)
306 QueryParamsMap mapCpy(queryParametersMap);
308 if(!resourceType.empty())
310 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
313 if(!resourceInterface.empty())
315 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
318 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
321 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
323 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
324 m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
327 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
329 QualityOfService defaultQos = OC::QualityOfService::NaQos;
330 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
332 return result_guard(deleteResource(deleteHandler, defaultQos));
335 OCStackResult OCResource::observe(ObserveType observeType,
336 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
337 QualityOfService QoS)
339 if(m_observeHandle != nullptr)
341 return result_guard(OC_STACK_INVALID_PARAM);
344 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
345 observeType, &m_observeHandle, m_devAddr,
346 m_uri, queryParametersMap, m_headerOptions,
347 observeHandler, QoS);
350 OCStackResult OCResource::observe(ObserveType observeType,
351 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
353 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
354 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
356 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
359 OCStackResult OCResource::cancelObserve()
361 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
362 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
363 return result_guard(cancelObserve(defaultQoS));
366 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
368 if(m_observeHandle == nullptr)
370 return result_guard(OC_STACK_INVALID_PARAM);
373 OCStackResult result = checked_guard(m_clientWrapper.lock(),
374 &IClientWrapper::CancelObserveResource,
375 m_observeHandle, "", m_uri, m_headerOptions, QoS);
377 if(result == OC_STACK_OK)
379 m_observeHandle = nullptr;
385 std::string OCResource::host() const
387 std::ostringstream ss;
388 if (m_devAddr.flags & OC_SECURE)
396 if (m_devAddr.flags & OC_IP_USE_V6)
398 ss << '[' << m_devAddr.addr << ']';
402 ss << m_devAddr.addr;
406 ss << ':' << m_devAddr.port;
411 std::string OCResource::uri() const
416 OCConnectivityType OCResource::connectivityType() const
418 return static_cast<OCConnectivityType>(
419 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
422 bool OCResource::isObservable() const
424 return m_isObservable;
427 OCResourceIdentifier OCResource::uniqueIdentifier() const
432 std::string OCResource::sid() const
434 return this->uniqueIdentifier().m_representation;
437 bool OCResource::operator==(const OCResource &other) const
439 return m_resourceId == other.m_resourceId;
442 bool OCResource::operator!=(const OCResource &other) const
444 return m_resourceId != other.m_resourceId;
447 bool OCResource::operator<(const OCResource &other) const
449 return m_resourceId < other.m_resourceId;
452 bool OCResource::operator>(const OCResource &other) const
454 return m_resourceId > other.m_resourceId;
457 bool OCResource::operator<=(const OCResource &other) const
459 return m_resourceId <= other.m_resourceId;
462 bool OCResource::operator>=(const OCResource &other) const
464 return m_resourceId >= other.m_resourceId;
467 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
468 const std::string& resourceUri)
469 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
473 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
475 os << ri.m_representation<<ri.m_resourceUri;
480 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
482 return m_representation == other.m_representation
483 && m_resourceUri == other.m_resourceUri;
486 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
488 return !(*this == other);
491 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
493 return m_resourceUri < other.m_resourceUri
494 || (m_resourceUri == other.m_resourceUri &&
495 m_representation < other.m_representation);
498 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
500 return *this != other && !(*this<other);
503 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
505 return !(*this > other);
508 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
510 return !(*this < other);