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