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