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);
134 std::string ip6Addr = host_token.substr(0, found);
135 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
136 m_devAddr.addr[ip6Addr.length()] = '\0';
137 host_token = host_token.substr(found + 1);
141 size_t found = host_token.find(':');
143 if(found == std::string::npos)
145 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
146 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
149 std::string addrPart = host_token.substr(0, found);
150 addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
151 m_devAddr.addr[addrPart.length()] = '\0';
153 host_token = host_token.substr(found + 1);
156 int port = stoi(host_token);
158 if( port < 0 || port > UINT16_MAX )
160 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
161 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
164 m_devAddr.port = static_cast<uint16_t>(port);
168 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
169 GetCallback attributeHandler, QualityOfService QoS)
171 return checked_guard(m_clientWrapper.lock(),
172 &IClientWrapper::GetResourceRepresentation,
174 queryParametersMap, m_headerOptions,
175 attributeHandler, QoS);
178 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
179 GetCallback attributeHandler)
181 QualityOfService defaultQos = OC::QualityOfService::NaQos;
182 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
183 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
186 OCStackResult OCResource::get(const std::string& resourceType,
187 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
188 GetCallback attributeHandler)
190 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
191 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
193 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
196 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
197 QualityOfService QoS)
199 QueryParamsMap mapCpy(queryParametersMap);
201 if(!resourceType.empty())
203 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
206 if(!resourceInterface.empty())
208 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
211 return result_guard(get(mapCpy, attributeHandler, QoS));
214 OCStackResult OCResource::put(const OCRepresentation& rep,
215 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
216 QualityOfService QoS)
218 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
219 m_devAddr, m_uri, rep, queryParametersMap,
220 m_headerOptions, attributeHandler, QoS);
223 OCStackResult OCResource::put(const OCRepresentation& rep,
224 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
226 QualityOfService defaultQos = OC::QualityOfService::NaQos;
227 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
228 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
231 OCStackResult OCResource::put(const std::string& resourceType,
232 const std::string& resourceInterface, const OCRepresentation& rep,
233 const QueryParamsMap& queryParametersMap,
234 PutCallback attributeHandler)
236 QualityOfService defaultQos = OC::QualityOfService::NaQos;
237 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
239 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
240 attributeHandler, defaultQos));
243 OCStackResult OCResource::put(const std::string& resourceType,
244 const std::string& resourceInterface, const OCRepresentation& rep,
245 const QueryParamsMap& queryParametersMap,
246 PutCallback attributeHandler,
247 QualityOfService QoS)
249 QueryParamsMap mapCpy(queryParametersMap);
251 if(!resourceType.empty())
253 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
256 if(!resourceInterface.empty())
258 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
261 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
264 OCStackResult OCResource::post(const OCRepresentation& rep,
265 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
266 QualityOfService QoS)
268 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
269 m_devAddr, m_uri, rep, queryParametersMap,
270 m_headerOptions, attributeHandler, QoS);
273 OCStackResult OCResource::post(const OCRepresentation& rep,
274 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
276 QualityOfService defaultQos = OC::QualityOfService::NaQos;
277 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
278 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
281 OCStackResult OCResource::post(const std::string& resourceType,
282 const std::string& resourceInterface, const OCRepresentation& rep,
283 const QueryParamsMap& queryParametersMap,
284 PostCallback attributeHandler)
286 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
287 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
289 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
293 OCStackResult OCResource::post(const std::string& resourceType,
294 const std::string& resourceInterface, const OCRepresentation& rep,
295 const QueryParamsMap& queryParametersMap,
296 PostCallback attributeHandler,
297 QualityOfService QoS)
299 QueryParamsMap mapCpy(queryParametersMap);
301 if(!resourceType.empty())
303 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
306 if(!resourceInterface.empty())
308 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
311 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
314 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
316 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
317 m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
320 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
322 QualityOfService defaultQos = OC::QualityOfService::NaQos;
323 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
325 return result_guard(deleteResource(deleteHandler, defaultQos));
328 OCStackResult OCResource::observe(ObserveType observeType,
329 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
330 QualityOfService QoS)
332 if(m_observeHandle != nullptr)
334 return result_guard(OC_STACK_INVALID_PARAM);
337 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
338 observeType, &m_observeHandle, m_devAddr,
339 m_uri, queryParametersMap, m_headerOptions,
340 observeHandler, QoS);
343 OCStackResult OCResource::observe(ObserveType observeType,
344 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
346 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
347 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
349 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
352 OCStackResult OCResource::cancelObserve()
354 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
355 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
356 return result_guard(cancelObserve(defaultQoS));
359 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
361 if(m_observeHandle == nullptr)
363 return result_guard(OC_STACK_INVALID_PARAM);
366 OCStackResult result = checked_guard(m_clientWrapper.lock(),
367 &IClientWrapper::CancelObserveResource,
368 m_observeHandle, "", m_uri, m_headerOptions, QoS);
370 if(result == OC_STACK_OK)
372 m_observeHandle = nullptr;
378 std::string OCResource::host() const
380 std::ostringstream ss;
381 if (m_devAddr.flags & OC_SECURE)
389 if (m_devAddr.flags & OC_IP_USE_V6)
391 ss << '[' << m_devAddr.addr << ']';
395 ss << m_devAddr.addr;
399 ss << ':' << m_devAddr.port;
404 std::string OCResource::uri() const
409 OCConnectivityType OCResource::connectivityType() const
411 return static_cast<OCConnectivityType>(
412 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
415 bool OCResource::isObservable() const
417 return m_isObservable;
420 OCResourceIdentifier OCResource::uniqueIdentifier() const
425 std::string OCResource::sid() const
427 return this->uniqueIdentifier().m_representation;
430 bool OCResource::operator==(const OCResource &other) const
432 return m_resourceId == other.m_resourceId;
435 bool OCResource::operator!=(const OCResource &other) const
437 return m_resourceId != other.m_resourceId;
440 bool OCResource::operator<(const OCResource &other) const
442 return m_resourceId < other.m_resourceId;
445 bool OCResource::operator>(const OCResource &other) const
447 return m_resourceId > other.m_resourceId;
450 bool OCResource::operator<=(const OCResource &other) const
452 return m_resourceId <= other.m_resourceId;
455 bool OCResource::operator>=(const OCResource &other) const
457 return m_resourceId >= other.m_resourceId;
460 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
461 const std::string& resourceUri)
462 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
466 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
468 os << ri.m_representation<<ri.m_resourceUri;
473 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
475 return m_representation == other.m_representation
476 && m_resourceUri == other.m_resourceUri;
479 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
481 return !(*this == other);
484 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
486 return m_resourceUri < other.m_resourceUri
487 || (m_resourceUri == other.m_resourceUri &&
488 m_representation < other.m_representation);
491 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
493 return *this != other && !(*this<other);
496 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
498 return !(*this > other);
501 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
503 return !(*this < other);