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