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