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, OC_DEFAULT_FLAGS, 0, {0}, 0 },
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);
92 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
93 interfaces.empty(), m_clientWrapper.expired(), false, false);
96 // construct the devAddr from the pieces we have
97 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
98 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
99 size_t len = host.length();
100 if (len >= MAX_ADDR_STR_SIZE)
102 throw std::length_error("host address is too long.");
108 OCResource::~OCResource()
112 void OCResource::setHost(const std::string& host)
116 if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
118 prefix_len = sizeof(COAP) - 1;
120 else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
122 prefix_len = sizeof(COAPS) - 1;
123 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
127 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
128 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
131 // removed coap:// or coaps://
132 std::string host_token = host.substr(prefix_len);
134 if(host_token[0] == '[')
136 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
138 size_t found = host_token.find(']');
140 if(found == std::string::npos || found == 0)
142 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
143 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
145 // extract the ipaddress
146 std::string ip6Addr = host_token.substr(1, found-1);
147 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
148 m_devAddr.addr[ip6Addr.length()] = '\0';
149 //skip ']' and ':' characters in host string
150 host_token = host_token.substr(found + 2);
154 size_t found = host_token.find(':');
156 if(found == std::string::npos || found == 0)
158 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
159 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
162 std::string addrPart = host_token.substr(0, found);
163 addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
164 m_devAddr.addr[addrPart.length()] = '\0';
165 //skip ':' character in host string
166 host_token = host_token.substr(found + 1);
169 int port = std::stoi(host_token);
171 if( port < 0 || port > UINT16_MAX )
173 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
174 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
177 m_devAddr.port = static_cast<uint16_t>(port);
181 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
182 GetCallback attributeHandler, QualityOfService QoS)
184 return checked_guard(m_clientWrapper.lock(),
185 &IClientWrapper::GetResourceRepresentation,
187 queryParametersMap, m_headerOptions,
188 attributeHandler, QoS);
191 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
192 GetCallback attributeHandler)
194 QualityOfService defaultQos = OC::QualityOfService::NaQos;
195 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
196 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
199 OCStackResult OCResource::get(const std::string& resourceType,
200 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
201 GetCallback attributeHandler)
203 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
204 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
206 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
209 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
210 QualityOfService QoS)
212 QueryParamsMap mapCpy(queryParametersMap);
214 if(!resourceType.empty())
216 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
219 if(!resourceInterface.empty())
221 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
224 return result_guard(get(mapCpy, attributeHandler, QoS));
227 OCStackResult OCResource::put(const OCRepresentation& rep,
228 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
229 QualityOfService QoS)
231 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
232 m_devAddr, m_uri, rep, queryParametersMap,
233 m_headerOptions, attributeHandler, QoS);
236 OCStackResult OCResource::put(const OCRepresentation& rep,
237 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
239 QualityOfService defaultQos = OC::QualityOfService::NaQos;
240 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
241 return result_guard(put(rep, queryParametersMap, 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)
249 QualityOfService defaultQos = OC::QualityOfService::NaQos;
250 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
252 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
253 attributeHandler, defaultQos));
256 OCStackResult OCResource::put(const std::string& resourceType,
257 const std::string& resourceInterface, const OCRepresentation& rep,
258 const QueryParamsMap& queryParametersMap,
259 PutCallback attributeHandler,
260 QualityOfService QoS)
262 QueryParamsMap mapCpy(queryParametersMap);
264 if(!resourceType.empty())
266 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
269 if(!resourceInterface.empty())
271 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
274 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
277 OCStackResult OCResource::post(const OCRepresentation& rep,
278 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
279 QualityOfService QoS)
281 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
282 m_devAddr, m_uri, rep, queryParametersMap,
283 m_headerOptions, attributeHandler, QoS);
286 OCStackResult OCResource::post(const OCRepresentation& rep,
287 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
289 QualityOfService defaultQos = OC::QualityOfService::NaQos;
290 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
291 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
294 OCStackResult OCResource::post(const std::string& resourceType,
295 const std::string& resourceInterface, const OCRepresentation& rep,
296 const QueryParamsMap& queryParametersMap,
297 PostCallback attributeHandler)
299 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
300 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
302 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
306 OCStackResult OCResource::post(const std::string& resourceType,
307 const std::string& resourceInterface, const OCRepresentation& rep,
308 const QueryParamsMap& queryParametersMap,
309 PostCallback attributeHandler,
310 QualityOfService QoS)
312 QueryParamsMap mapCpy(queryParametersMap);
314 if(!resourceType.empty())
316 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
319 if(!resourceInterface.empty())
321 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
324 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
327 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
329 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
330 m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
333 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
335 QualityOfService defaultQos = OC::QualityOfService::NaQos;
336 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
338 return result_guard(deleteResource(deleteHandler, defaultQos));
341 OCStackResult OCResource::observe(ObserveType observeType,
342 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
343 QualityOfService QoS)
345 if(m_observeHandle != nullptr)
347 return result_guard(OC_STACK_INVALID_PARAM);
350 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
351 observeType, &m_observeHandle, m_devAddr,
352 m_uri, queryParametersMap, m_headerOptions,
353 observeHandler, QoS);
356 OCStackResult OCResource::observe(ObserveType observeType,
357 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
359 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
360 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
362 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
365 OCStackResult OCResource::cancelObserve()
367 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
368 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
369 return result_guard(cancelObserve(defaultQoS));
372 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
374 if(m_observeHandle == nullptr)
376 return result_guard(OC_STACK_INVALID_PARAM);
379 OCStackResult result = checked_guard(m_clientWrapper.lock(),
380 &IClientWrapper::CancelObserveResource,
381 m_observeHandle, "", m_uri, m_headerOptions, QoS);
383 if(result == OC_STACK_OK)
385 m_observeHandle = nullptr;
391 std::string OCResource::host() const
393 std::ostringstream ss;
394 if (m_devAddr.flags & OC_SECURE)
402 if (m_devAddr.flags & OC_IP_USE_V6)
404 ss << '[' << m_devAddr.addr << ']';
408 ss << m_devAddr.addr;
412 ss << ':' << m_devAddr.port;
417 std::string OCResource::uri() const
422 OCConnectivityType OCResource::connectivityType() const
424 return static_cast<OCConnectivityType>(
425 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
428 bool OCResource::isObservable() const
430 return m_isObservable;
433 OCResourceIdentifier OCResource::uniqueIdentifier() const
438 std::string OCResource::sid() const
440 return this->uniqueIdentifier().m_representation;
443 bool OCResource::operator==(const OCResource &other) const
445 return m_resourceId == other.m_resourceId;
448 bool OCResource::operator!=(const OCResource &other) const
450 return m_resourceId != other.m_resourceId;
453 bool OCResource::operator<(const OCResource &other) const
455 return m_resourceId < other.m_resourceId;
458 bool OCResource::operator>(const OCResource &other) const
460 return m_resourceId > other.m_resourceId;
463 bool OCResource::operator<=(const OCResource &other) const
465 return m_resourceId <= other.m_resourceId;
468 bool OCResource::operator>=(const OCResource &other) const
470 return m_resourceId >= other.m_resourceId;
473 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
474 const std::string& resourceUri)
475 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
479 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
481 os << ri.m_representation<<ri.m_resourceUri;
486 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
488 return m_representation == other.m_representation
489 && m_resourceUri == other.m_resourceUri;
492 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
494 return !(*this == other);
497 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
499 return m_resourceUri < other.m_resourceUri
500 || (m_resourceUri == other.m_resourceUri &&
501 m_representation < other.m_representation);
504 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
506 return *this != other && !(*this<other);
509 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
511 return !(*this > other);
514 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
516 return !(*this < other);