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 // construct the devAddr from the pieces we have
85 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
86 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
87 size_t len = host.length();
88 if (len >= MAX_ADDR_STR_SIZE)
90 throw std::length_error("host address is too long.");
96 OCResource::~OCResource()
100 void OCResource::setHost(const std::string& host)
104 if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
106 prefix_len = sizeof(COAP) - 1;
108 else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
110 prefix_len = sizeof(COAPS) - 1;
111 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
115 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
116 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
119 // removed coap:// or coaps://
120 std::string host_token = host.substr(prefix_len);
122 if(host_token[0] == '[')
124 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
126 size_t found = host_token.find(']');
128 if(found == std::string::npos)
130 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
131 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
133 // extract the ipaddress
134 std::string ip6Addr = host_token.substr(1, found-1);
135 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
136 m_devAddr.addr[ip6Addr.length()] = '\0';
137 //skip ']' and ':' characters in host string
138 host_token = host_token.substr(found + 2);
142 size_t found = host_token.find(':');
144 if(found == std::string::npos)
146 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
147 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
150 std::string addrPart = host_token.substr(0, found);
151 addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
152 m_devAddr.addr[addrPart.length()] = '\0';
153 //skip ':' character in host string
154 host_token = host_token.substr(found + 1);
157 int port = std::stoi(host_token);
159 if( port < 0 || port > UINT16_MAX )
161 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
162 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
165 m_devAddr.port = static_cast<uint16_t>(port);
169 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
170 GetCallback attributeHandler, QualityOfService QoS)
172 return checked_guard(m_clientWrapper.lock(),
173 &IClientWrapper::GetResourceRepresentation,
175 queryParametersMap, m_headerOptions,
176 attributeHandler, QoS);
179 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
180 GetCallback attributeHandler)
182 QualityOfService defaultQos = OC::QualityOfService::NaQos;
183 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
184 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
187 OCStackResult OCResource::get(const std::string& resourceType,
188 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
189 GetCallback attributeHandler)
191 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
192 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
194 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
197 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
198 QualityOfService QoS)
200 QueryParamsMap mapCpy(queryParametersMap);
202 if(!resourceType.empty())
204 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
207 if(!resourceInterface.empty())
209 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
212 return result_guard(get(mapCpy, attributeHandler, QoS));
215 OCStackResult OCResource::put(const OCRepresentation& rep,
216 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
217 QualityOfService QoS)
219 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
220 m_devAddr, m_uri, rep, queryParametersMap,
221 m_headerOptions, attributeHandler, QoS);
224 OCStackResult OCResource::put(const OCRepresentation& rep,
225 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
227 QualityOfService defaultQos = OC::QualityOfService::NaQos;
228 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
229 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
232 OCStackResult OCResource::put(const std::string& resourceType,
233 const std::string& resourceInterface, const OCRepresentation& rep,
234 const QueryParamsMap& queryParametersMap,
235 PutCallback attributeHandler)
237 QualityOfService defaultQos = OC::QualityOfService::NaQos;
238 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
240 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
241 attributeHandler, defaultQos));
244 OCStackResult OCResource::put(const std::string& resourceType,
245 const std::string& resourceInterface, const OCRepresentation& rep,
246 const QueryParamsMap& queryParametersMap,
247 PutCallback attributeHandler,
248 QualityOfService QoS)
250 QueryParamsMap mapCpy(queryParametersMap);
252 if(!resourceType.empty())
254 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
257 if(!resourceInterface.empty())
259 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
262 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
265 OCStackResult OCResource::post(const OCRepresentation& rep,
266 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
267 QualityOfService QoS)
269 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
270 m_devAddr, m_uri, rep, queryParametersMap,
271 m_headerOptions, attributeHandler, QoS);
274 OCStackResult OCResource::post(const OCRepresentation& rep,
275 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
277 QualityOfService defaultQos = OC::QualityOfService::NaQos;
278 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
279 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
282 OCStackResult OCResource::post(const std::string& resourceType,
283 const std::string& resourceInterface, const OCRepresentation& rep,
284 const QueryParamsMap& queryParametersMap,
285 PostCallback attributeHandler)
287 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
288 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
290 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
294 OCStackResult OCResource::post(const std::string& resourceType,
295 const std::string& resourceInterface, const OCRepresentation& rep,
296 const QueryParamsMap& queryParametersMap,
297 PostCallback attributeHandler,
298 QualityOfService QoS)
300 QueryParamsMap mapCpy(queryParametersMap);
302 if(!resourceType.empty())
304 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
307 if(!resourceInterface.empty())
309 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
312 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
315 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
317 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
318 m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
321 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
323 QualityOfService defaultQos = OC::QualityOfService::NaQos;
324 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
326 return result_guard(deleteResource(deleteHandler, defaultQos));
329 OCStackResult OCResource::observe(ObserveType observeType,
330 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
331 QualityOfService QoS)
333 if(m_observeHandle != nullptr)
335 return result_guard(OC_STACK_INVALID_PARAM);
338 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
339 observeType, &m_observeHandle, m_devAddr,
340 m_uri, queryParametersMap, m_headerOptions,
341 observeHandler, QoS);
344 OCStackResult OCResource::observe(ObserveType observeType,
345 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
347 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
348 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
350 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
353 OCStackResult OCResource::cancelObserve()
355 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
356 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
357 return result_guard(cancelObserve(defaultQoS));
360 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
362 if(m_observeHandle == nullptr)
364 return result_guard(OC_STACK_INVALID_PARAM);
367 OCStackResult result = checked_guard(m_clientWrapper.lock(),
368 &IClientWrapper::CancelObserveResource,
369 m_observeHandle, "", m_uri, m_headerOptions, QoS);
371 if(result == OC_STACK_OK)
373 m_observeHandle = nullptr;
379 std::string OCResource::host() const
381 std::ostringstream ss;
382 if (m_devAddr.flags & OC_SECURE)
390 if (m_devAddr.flags & OC_IP_USE_V6)
392 ss << '[' << m_devAddr.addr << ']';
396 ss << m_devAddr.addr;
400 ss << ':' << m_devAddr.port;
405 std::string OCResource::uri() const
410 OCConnectivityType OCResource::connectivityType() const
412 return static_cast<OCConnectivityType>(
413 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
416 bool OCResource::isObservable() const
418 return m_isObservable;
421 OCResourceIdentifier OCResource::uniqueIdentifier() const
426 std::string OCResource::sid() const
428 return this->uniqueIdentifier().m_representation;
431 bool OCResource::operator==(const OCResource &other) const
433 return m_resourceId == other.m_resourceId;
436 bool OCResource::operator!=(const OCResource &other) const
438 return m_resourceId != other.m_resourceId;
441 bool OCResource::operator<(const OCResource &other) const
443 return m_resourceId < other.m_resourceId;
446 bool OCResource::operator>(const OCResource &other) const
448 return m_resourceId > other.m_resourceId;
451 bool OCResource::operator<=(const OCResource &other) const
453 return m_resourceId <= other.m_resourceId;
456 bool OCResource::operator>=(const OCResource &other) const
458 return m_resourceId >= other.m_resourceId;
461 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
462 const std::string& resourceUri)
463 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
467 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
469 os << ri.m_representation<<ri.m_resourceUri;
474 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
476 return m_representation == other.m_representation
477 && m_resourceUri == other.m_resourceUri;
480 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
482 return !(*this == other);
485 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
487 return m_resourceUri < other.m_resourceUri
488 || (m_resourceUri == other.m_resourceUri &&
489 m_representation < other.m_representation);
492 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
494 return *this != other && !(*this<other);
497 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
499 return !(*this > other);
502 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
504 return !(*this < other);