Merge branch 'simulator'.
[platform/upstream/iotivity.git] / resource / src / OCResource.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "OCResource.h"
22 #include "OCUtilities.h"
23
24 #include <boost/lexical_cast.hpp>
25 #include <sstream>
26
27 namespace OC {
28
29 static const char COAP[] = "coap://";
30 static const char COAPS[] = "coaps://";
31 static const char COAP_TCP[] = "coap+tcp://";
32
33 using OC::nil_guard;
34 using OC::result_guard;
35 using OC::checked_guard;
36
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)
47 {
48     m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
49                         != m_interfaces.end();
50
51     if (m_uri.empty() ||
52         resourceTypes.empty() ||
53         interfaces.empty()||
54         m_clientWrapper.expired())
55     {
56         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
57                 interfaces.empty(), m_clientWrapper.expired(), false, false);
58     }
59 }
60
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)
71     , {0}
72 #endif
73     },
74     m_isObservable(observable), m_isCollection(false),
75     m_resourceTypes(resourceTypes), m_interfaces(interfaces),
76     m_observeHandle(nullptr)
77 {
78     m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
79                         != m_interfaces.end();
80
81     if (m_uri.empty() ||
82         resourceTypes.empty() ||
83         interfaces.empty()||
84         m_clientWrapper.expired())
85     {
86         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
87                 interfaces.empty(), m_clientWrapper.expired(), false, false);
88     }
89
90     if (uri.length() == 1 && uri[0] == '/')
91     {
92         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
93                 interfaces.empty(), m_clientWrapper.expired(), false, false);
94     }
95
96     if (uri[0] != '/')
97     {
98         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
99                 interfaces.empty(), m_clientWrapper.expired(), false, false);
100     }
101
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);
105
106     this->setHost(host);
107 }
108
109 OCResource::~OCResource()
110 {
111 }
112
113 void OCResource::setHost(const std::string& host)
114 {
115     size_t prefix_len;
116
117     if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
118     {
119         prefix_len = sizeof(COAP) - 1;
120     }
121     else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
122     {
123         prefix_len = sizeof(COAPS) - 1;
124         m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
125     }
126     else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
127     {
128         prefix_len = sizeof(COAP_TCP) - 1;
129     }
130     else
131     {
132         throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
133             m_interfaces.empty(), m_clientWrapper.expired(), false, false);
134     }
135
136     // removed coap:// or coaps:// or coap+tcp://
137     std::string host_token = host.substr(prefix_len);
138
139     if(host_token[0] == '[')
140     {
141         m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
142
143         size_t found = host_token.find(']');
144
145         if(found == std::string::npos || found == 0)
146         {
147             throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
148                 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
149         }
150         // extract the ipaddress
151         std::string ip6Addr = host_token.substr(1, found-1);
152
153         if (ip6Addr.length() >= MAX_ADDR_STR_SIZE)
154         {
155             throw std::length_error("host address is too long.");
156         }
157
158         ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
159         m_devAddr.addr[ip6Addr.length()] = '\0';
160         //skip ']' and ':' characters in host string
161         host_token = host_token.substr(found + 2);
162     }
163     else
164     {
165         size_t found = host_token.find(':');
166
167         if(found == std::string::npos || found == 0)
168         {
169             throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
170                 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
171         }
172
173         std::string addrPart = host_token.substr(0, found);
174
175         if (addrPart.length() >= MAX_ADDR_STR_SIZE)
176         {
177             throw std::length_error("host address is too long.");
178         }
179
180         addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
181         m_devAddr.addr[addrPart.length()] = '\0';
182         //skip ':' character in host string
183         host_token = host_token.substr(found + 1);
184     }
185
186     int port = std::stoi(host_token);
187
188     if( port < 0 || port > UINT16_MAX )
189     {
190         throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
191             m_interfaces.empty(), m_clientWrapper.expired(), false, false);
192     }
193
194     m_devAddr.port = static_cast<uint16_t>(port);
195
196 }
197
198 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
199                               GetCallback attributeHandler, QualityOfService QoS)
200 {
201     return checked_guard(m_clientWrapper.lock(),
202                             &IClientWrapper::GetResourceRepresentation,
203                             m_devAddr, m_uri,
204                             queryParametersMap, m_headerOptions,
205                             attributeHandler, QoS);
206 }
207
208 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
209                               GetCallback attributeHandler)
210 {
211     QualityOfService defaultQos = OC::QualityOfService::NaQos;
212     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
213     return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
214 }
215
216 OCStackResult OCResource::get(const std::string& resourceType,
217                      const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
218                      GetCallback attributeHandler)
219 {
220     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
221     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
222
223     return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
224 }
225
226 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
227         QualityOfService QoS)
228 {
229     QueryParamsMap mapCpy(queryParametersMap);
230
231     if(!resourceType.empty())
232     {
233         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
234     }
235
236     if(!resourceInterface.empty())
237     {
238         mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
239     }
240
241     return result_guard(get(mapCpy, attributeHandler, QoS));
242 }
243
244 OCStackResult OCResource::put(const OCRepresentation& rep,
245                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
246                               QualityOfService QoS)
247 {
248     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
249                          m_devAddr, m_uri, rep, queryParametersMap,
250                          m_headerOptions, attributeHandler, QoS);
251 }
252
253 OCStackResult OCResource::put(const OCRepresentation& rep,
254                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
255 {
256     QualityOfService defaultQos = OC::QualityOfService::NaQos;
257     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
258     return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
259 }
260
261 OCStackResult OCResource::put(const std::string& resourceType,
262                               const std::string& resourceInterface, const OCRepresentation& rep,
263                               const QueryParamsMap& queryParametersMap,
264                               PutCallback attributeHandler)
265 {
266     QualityOfService defaultQos = OC::QualityOfService::NaQos;
267     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
268
269     return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
270             attributeHandler, defaultQos));
271 }
272
273 OCStackResult OCResource::put(const std::string& resourceType,
274                               const std::string& resourceInterface, const OCRepresentation& rep,
275                               const QueryParamsMap& queryParametersMap,
276                               PutCallback attributeHandler,
277                               QualityOfService QoS)
278 {
279     QueryParamsMap mapCpy(queryParametersMap);
280
281     if(!resourceType.empty())
282     {
283         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
284     }
285
286     if(!resourceInterface.empty())
287     {
288         mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
289     }
290
291     return result_guard(put(rep, mapCpy, attributeHandler, QoS));
292 }
293
294 OCStackResult OCResource::post(const OCRepresentation& rep,
295                                const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
296                                QualityOfService QoS)
297 {
298     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
299                          m_devAddr, m_uri, rep, queryParametersMap,
300                          m_headerOptions, attributeHandler, QoS);
301 }
302
303 OCStackResult OCResource::post(const OCRepresentation& rep,
304                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
305 {
306     QualityOfService defaultQos = OC::QualityOfService::NaQos;
307     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
308     return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
309 }
310
311 OCStackResult OCResource::post(const std::string& resourceType,
312                                const std::string& resourceInterface, const OCRepresentation& rep,
313                                const QueryParamsMap& queryParametersMap,
314                                PostCallback attributeHandler)
315 {
316     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
317     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
318
319     return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
320             defaultQoS));
321 }
322
323 OCStackResult OCResource::post(const std::string& resourceType,
324                                const std::string& resourceInterface, const OCRepresentation& rep,
325                                const QueryParamsMap& queryParametersMap,
326                                PostCallback attributeHandler,
327                                QualityOfService QoS)
328 {
329     QueryParamsMap mapCpy(queryParametersMap);
330
331     if(!resourceType.empty())
332     {
333         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
334     }
335
336     if(!resourceInterface.empty())
337     {
338         mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
339     }
340
341     return result_guard(post(rep, mapCpy, attributeHandler, QoS));
342 }
343
344 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
345 {
346     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
347                          m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
348 }
349
350 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
351 {
352     QualityOfService defaultQos = OC::QualityOfService::NaQos;
353     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
354
355     return result_guard(deleteResource(deleteHandler, defaultQos));
356 }
357
358 OCStackResult OCResource::observe(ObserveType observeType,
359         const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
360         QualityOfService QoS)
361 {
362     if(m_observeHandle != nullptr)
363     {
364         return result_guard(OC_STACK_INVALID_PARAM);
365     }
366
367     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
368                          observeType, &m_observeHandle, m_devAddr,
369                          m_uri, queryParametersMap, m_headerOptions,
370                          observeHandler, QoS);
371 }
372
373 OCStackResult OCResource::observe(ObserveType observeType,
374         const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
375 {
376     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
377     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
378
379     return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
380 }
381
382 OCStackResult OCResource::cancelObserve()
383 {
384     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
385     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
386     return result_guard(cancelObserve(defaultQoS));
387 }
388
389 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
390 {
391     if(m_observeHandle == nullptr)
392     {
393         return result_guard(OC_STACK_INVALID_PARAM);
394     }
395
396     OCStackResult result =  checked_guard(m_clientWrapper.lock(),
397             &IClientWrapper::CancelObserveResource,
398             m_observeHandle, "", m_uri, m_headerOptions, QoS);
399
400     if(result == OC_STACK_OK)
401     {
402         m_observeHandle = nullptr;
403     }
404
405     return result;
406 }
407
408 void OCResource::setHeaderOptions(const HeaderOptions& headerOptions)
409 {
410     m_headerOptions = headerOptions;
411 }
412
413 void OCResource::unsetHeaderOptions()
414 {
415     m_headerOptions.clear();
416 }
417
418 std::string OCResource::host() const
419 {
420     std::ostringstream ss;
421     if (m_devAddr.flags & OC_SECURE)
422     {
423         ss << COAPS;
424     }
425     else if ((m_devAddr.adapter & OC_ADAPTER_TCP)
426             || (m_devAddr.adapter & OC_ADAPTER_GATT_BTLE)
427             || (m_devAddr.adapter & OC_ADAPTER_RFCOMM_BTEDR))
428     {
429         ss << COAP_TCP;
430     }
431     else
432     {
433         ss << COAP;
434     }
435     if (m_devAddr.flags & OC_IP_USE_V6)
436     {
437         ss << '[' << m_devAddr.addr << ']';
438     }
439     else
440     {
441         ss << m_devAddr.addr;
442     }
443     if (m_devAddr.port)
444     {
445         ss << ':' << m_devAddr.port;
446     }
447     return ss.str();
448 }
449
450 std::string OCResource::uri() const
451 {
452     return m_uri;
453 }
454
455 OCConnectivityType OCResource::connectivityType() const
456 {
457     return static_cast<OCConnectivityType>(
458            (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
459 }
460
461 bool OCResource::isObservable() const
462 {
463     return m_isObservable;
464 }
465
466 std::vector<std::string> OCResource::getResourceTypes() const
467 {
468     return m_resourceTypes;
469 }
470
471 std::vector<std::string> OCResource::getResourceInterfaces(void) const
472 {
473     return m_interfaces;
474 }
475
476 OCResourceIdentifier OCResource::uniqueIdentifier() const
477 {
478     return m_resourceId;
479 }
480
481 std::string OCResource::sid() const
482 {
483     return this->uniqueIdentifier().m_representation;
484 }
485
486 bool OCResource::operator==(const OCResource &other) const
487 {
488     return m_resourceId == other.m_resourceId;
489 }
490
491 bool OCResource::operator!=(const OCResource &other) const
492 {
493     return m_resourceId != other.m_resourceId;
494 }
495
496 bool OCResource::operator<(const OCResource &other) const
497 {
498     return m_resourceId < other.m_resourceId;
499 }
500
501 bool OCResource::operator>(const OCResource &other) const
502 {
503     return m_resourceId > other.m_resourceId;
504 }
505
506 bool OCResource::operator<=(const OCResource &other) const
507 {
508     return m_resourceId <= other.m_resourceId;
509 }
510
511 bool OCResource::operator>=(const OCResource &other) const
512 {
513     return m_resourceId >= other.m_resourceId;
514 }
515
516 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
517         const std::string& resourceUri)
518     :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
519 {
520 }
521
522 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
523 {
524     os << ri.m_representation<<ri.m_resourceUri;
525
526     return os;
527 }
528
529 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
530 {
531     return m_representation == other.m_representation
532         && m_resourceUri == other.m_resourceUri;
533 }
534
535 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
536 {
537     return !(*this == other);
538 }
539
540 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
541 {
542     return m_resourceUri < other.m_resourceUri
543         || (m_resourceUri == other.m_resourceUri &&
544                 m_representation < other.m_representation);
545 }
546
547 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
548 {
549     return *this != other && !(*this<other);
550 }
551
552 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
553 {
554     return !(*this > other);
555 }
556
557 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
558 {
559     return !(*this < other);
560 }
561
562 } // namespace OC