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