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://";
31 static const char COAP_TCP[] = "coap+tcp://";
34 using OC::result_guard;
35 using OC::checked_guard;
37 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
38 const OCDevAddr& devAddr, const std::string& uri,
39 const std::string& serverId, bool observable,
40 const std::vector<std::string>& resourceTypes,
41 const std::vector<std::string>& interfaces)
42 : m_clientWrapper(clientWrapper), m_uri(uri),
43 m_resourceId(serverId, m_uri), m_devAddr(devAddr),
44 m_isObservable(observable), m_isCollection(false),
45 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
46 m_observeHandle(nullptr)
48 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
49 != m_interfaces.end();
52 resourceTypes.empty() ||
54 m_clientWrapper.expired())
56 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
57 interfaces.empty(), m_clientWrapper.expired(), false, false);
61 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
62 const std::string& host, const std::string& uri,
63 const std::string& serverId,
64 OCConnectivityType connectivityType, bool observable,
65 const std::vector<std::string>& resourceTypes,
66 const std::vector<std::string>& interfaces)
67 : m_clientWrapper(clientWrapper), m_uri(uri),
68 m_resourceId(serverId, m_uri),
69 m_devAddr{ OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0
70 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
74 m_isObservable(observable), m_isCollection(false),
75 m_resourceTypes(resourceTypes), m_interfaces(interfaces),
76 m_observeHandle(nullptr)
78 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
79 != m_interfaces.end();
82 resourceTypes.empty() ||
84 m_clientWrapper.expired())
86 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
87 interfaces.empty(), m_clientWrapper.expired(), false, false);
90 if (uri.length() == 1 && uri[0] == '/')
92 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
93 interfaces.empty(), m_clientWrapper.expired(), false, false);
98 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
99 interfaces.empty(), m_clientWrapper.expired(), false, false);
102 // construct the devAddr from the pieces we have
103 m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
104 m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
109 OCResource::~OCResource()
113 void OCResource::setHost(const std::string& host)
117 if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
119 prefix_len = sizeof(COAP) - 1;
121 else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
123 prefix_len = sizeof(COAPS) - 1;
124 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
126 else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
128 prefix_len = sizeof(COAP_TCP) - 1;
129 m_devAddr.adapter = static_cast<OCTransportAdapter>(m_devAddr.adapter & OC_ADAPTER_TCP);
133 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
134 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
137 // removed coap:// or coaps:// or coap+tcp://
138 std::string host_token = host.substr(prefix_len);
140 if(host_token[0] == '[')
142 m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
144 size_t found = host_token.find(']');
146 if(found == std::string::npos || found == 0)
148 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
149 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
151 // extract the ipaddress
152 std::string ip6Addr = host_token.substr(1, found-1);
154 if (ip6Addr.length() >= MAX_ADDR_STR_SIZE)
156 throw std::length_error("host address is too long.");
159 ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
160 m_devAddr.addr[ip6Addr.length()] = '\0';
161 //skip ']' and ':' characters in host string
162 host_token = host_token.substr(found + 2);
166 size_t found = host_token.find(':');
168 if(found == std::string::npos || found == 0)
170 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
171 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
174 std::string addrPart = host_token.substr(0, found);
176 if (addrPart.length() >= MAX_ADDR_STR_SIZE)
178 throw std::length_error("host address is too long.");
181 addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
182 m_devAddr.addr[addrPart.length()] = '\0';
183 //skip ':' character in host string
184 host_token = host_token.substr(found + 1);
187 int port = std::stoi(host_token);
189 if( port < 0 || port > UINT16_MAX )
191 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
192 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
195 m_devAddr.port = static_cast<uint16_t>(port);
199 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
200 GetCallback attributeHandler, QualityOfService QoS)
202 return checked_guard(m_clientWrapper.lock(),
203 &IClientWrapper::GetResourceRepresentation,
205 queryParametersMap, m_headerOptions,
206 attributeHandler, QoS);
209 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
210 GetCallback attributeHandler)
212 QualityOfService defaultQos = OC::QualityOfService::NaQos;
213 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
214 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
217 OCStackResult OCResource::get(const std::string& resourceType,
218 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
219 GetCallback attributeHandler)
221 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
222 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
224 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
227 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
228 QualityOfService QoS)
230 QueryParamsMap mapCpy(queryParametersMap);
232 if(!resourceType.empty())
234 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
237 if(!resourceInterface.empty())
239 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
242 return result_guard(get(mapCpy, attributeHandler, QoS));
245 OCStackResult OCResource::put(const OCRepresentation& rep,
246 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
247 QualityOfService QoS)
249 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
250 m_devAddr, m_uri, rep, queryParametersMap,
251 m_headerOptions, attributeHandler, QoS);
254 OCStackResult OCResource::put(const OCRepresentation& rep,
255 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
257 QualityOfService defaultQos = OC::QualityOfService::NaQos;
258 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
259 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
262 OCStackResult OCResource::put(const std::string& resourceType,
263 const std::string& resourceInterface, const OCRepresentation& rep,
264 const QueryParamsMap& queryParametersMap,
265 PutCallback attributeHandler)
267 QualityOfService defaultQos = OC::QualityOfService::NaQos;
268 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
270 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
271 attributeHandler, defaultQos));
274 OCStackResult OCResource::put(const std::string& resourceType,
275 const std::string& resourceInterface, const OCRepresentation& rep,
276 const QueryParamsMap& queryParametersMap,
277 PutCallback attributeHandler,
278 QualityOfService QoS)
280 QueryParamsMap mapCpy(queryParametersMap);
282 if(!resourceType.empty())
284 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
287 if(!resourceInterface.empty())
289 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
292 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
295 OCStackResult OCResource::post(const OCRepresentation& rep,
296 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
297 QualityOfService QoS)
299 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
300 m_devAddr, m_uri, rep, queryParametersMap,
301 m_headerOptions, attributeHandler, QoS);
304 OCStackResult OCResource::post(const OCRepresentation& rep,
305 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
307 QualityOfService defaultQos = OC::QualityOfService::NaQos;
308 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
309 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
312 OCStackResult OCResource::post(const std::string& resourceType,
313 const std::string& resourceInterface, const OCRepresentation& rep,
314 const QueryParamsMap& queryParametersMap,
315 PostCallback attributeHandler)
317 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
318 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
320 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
324 OCStackResult OCResource::post(const std::string& resourceType,
325 const std::string& resourceInterface, const OCRepresentation& rep,
326 const QueryParamsMap& queryParametersMap,
327 PostCallback attributeHandler,
328 QualityOfService QoS)
330 QueryParamsMap mapCpy(queryParametersMap);
332 if(!resourceType.empty())
334 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
337 if(!resourceInterface.empty())
339 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
342 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
345 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
347 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
348 m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
351 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
353 QualityOfService defaultQos = OC::QualityOfService::NaQos;
354 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
356 return result_guard(deleteResource(deleteHandler, defaultQos));
359 OCStackResult OCResource::observe(ObserveType observeType,
360 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
361 QualityOfService QoS)
363 if(m_observeHandle != nullptr)
365 return result_guard(OC_STACK_INVALID_PARAM);
368 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
369 observeType, &m_observeHandle, m_devAddr,
370 m_uri, queryParametersMap, m_headerOptions,
371 observeHandler, QoS);
374 OCStackResult OCResource::observe(ObserveType observeType,
375 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
377 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
378 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
380 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
383 OCStackResult OCResource::cancelObserve()
385 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
386 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
387 return result_guard(cancelObserve(defaultQoS));
390 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
392 if(m_observeHandle == nullptr)
394 return result_guard(OC_STACK_INVALID_PARAM);
397 OCStackResult result = checked_guard(m_clientWrapper.lock(),
398 &IClientWrapper::CancelObserveResource,
399 m_observeHandle, "", m_uri, m_headerOptions, QoS);
401 if(result == OC_STACK_OK)
403 m_observeHandle = nullptr;
409 void OCResource::setHeaderOptions(const HeaderOptions& headerOptions)
411 m_headerOptions = headerOptions;
414 void OCResource::unsetHeaderOptions()
416 m_headerOptions.clear();
419 std::string OCResource::host() const
421 std::ostringstream ss;
422 if (m_devAddr.flags & OC_SECURE)
426 else if (m_devAddr.adapter & OC_ADAPTER_TCP)
434 if (m_devAddr.flags & OC_IP_USE_V6)
436 ss << '[' << m_devAddr.addr << ']';
440 ss << m_devAddr.addr;
444 ss << ':' << m_devAddr.port;
449 std::string OCResource::uri() const
454 OCConnectivityType OCResource::connectivityType() const
456 return static_cast<OCConnectivityType>(
457 (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
460 bool OCResource::isObservable() const
462 return m_isObservable;
465 std::vector<std::string> OCResource::getResourceTypes() const
467 return m_resourceTypes;
470 std::vector<std::string> OCResource::getResourceInterfaces(void) const
475 OCResourceIdentifier OCResource::uniqueIdentifier() const
480 std::string OCResource::sid() const
482 return this->uniqueIdentifier().m_representation;
485 bool OCResource::operator==(const OCResource &other) const
487 return m_resourceId == other.m_resourceId;
490 bool OCResource::operator!=(const OCResource &other) const
492 return m_resourceId != other.m_resourceId;
495 bool OCResource::operator<(const OCResource &other) const
497 return m_resourceId < other.m_resourceId;
500 bool OCResource::operator>(const OCResource &other) const
502 return m_resourceId > other.m_resourceId;
505 bool OCResource::operator<=(const OCResource &other) const
507 return m_resourceId <= other.m_resourceId;
510 bool OCResource::operator>=(const OCResource &other) const
512 return m_resourceId >= other.m_resourceId;
515 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
516 const std::string& resourceUri)
517 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
521 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
523 os << ri.m_representation<<ri.m_resourceUri;
528 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
530 return m_representation == other.m_representation
531 && m_resourceUri == other.m_resourceUri;
534 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
536 return !(*this == other);
539 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
541 return m_resourceUri < other.m_resourceUri
542 || (m_resourceUri == other.m_resourceUri &&
543 m_representation < other.m_representation);
546 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
548 return *this != other && !(*this<other);
551 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
553 return !(*this > other);
556 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
558 return !(*this < other);