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