Merge "Merge branch 'master' into resource-manipulation" into resource-manipulation
[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
26 namespace OC {
27
28 using OC::nil_guard;
29 using OC::result_guard;
30 using OC::checked_guard;
31
32 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
33                         const OCDevAddr& devAddr, const std::string& uri,
34                         const std::string& serverId, bool observable,
35                         const std::vector<std::string>& resourceTypes,
36                         const std::vector<std::string>& interfaces)
37  :  m_clientWrapper(clientWrapper), m_uri(uri),
38     m_resourceId(serverId, m_uri), m_devAddr(devAddr),
39     m_isObservable(observable), m_isCollection(false),
40     m_resourceTypes(resourceTypes), m_interfaces(interfaces),
41     m_observeHandle(nullptr)
42 {
43     m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
44                         != m_interfaces.end();
45
46     if (m_uri.empty() ||
47         resourceTypes.empty() ||
48         interfaces.empty()||
49         m_clientWrapper.expired())
50     {
51         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
52                 interfaces.empty(), m_clientWrapper.expired(), false, false);
53     }
54 }
55
56 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
57                         const std::string& host, const std::string& uri,
58                         const std::string& serverId,
59                         OCConnectivityType connectivityType, bool observable,
60                         const std::vector<std::string>& resourceTypes,
61                         const std::vector<std::string>& interfaces)
62  :  m_clientWrapper(clientWrapper), m_uri(uri),
63     m_resourceId(serverId, m_uri),
64     m_devAddr{ OC_DEFAULT_ADAPTER },
65     m_isObservable(observable), m_isCollection(false),
66     m_resourceTypes(resourceTypes), m_interfaces(interfaces),
67     m_observeHandle(nullptr)
68 {
69     m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
70                         != m_interfaces.end();
71
72     if (m_uri.empty() ||
73         resourceTypes.empty() ||
74         interfaces.empty()||
75         m_clientWrapper.expired())
76     {
77         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
78                 interfaces.empty(), m_clientWrapper.expired(), false, false);
79     }
80
81     // construct the devAddr from the pieces we have
82     m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
83     m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
84     size_t len = host.length();
85     if (len >= MAX_ADDR_STR_SIZE)
86     {
87         throw std::length_error("host address is too long.");
88     }
89     host.copy(m_devAddr.addr, len);
90     m_devAddr.addr[len] = '\0';
91 }
92
93 OCResource::~OCResource()
94 {
95 }
96
97 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
98                               GetCallback attributeHandler, QualityOfService QoS)
99 {
100     return checked_guard(m_clientWrapper.lock(),
101                             &IClientWrapper::GetResourceRepresentation,
102                             m_devAddr, m_uri,
103                             queryParametersMap, m_headerOptions,
104                             attributeHandler, QoS);
105 }
106
107 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
108                               GetCallback attributeHandler)
109 {
110     QualityOfService defaultQos = OC::QualityOfService::NaQos;
111     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
112     return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
113 }
114
115 OCStackResult OCResource::get(const std::string& resourceType,
116                      const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
117                      GetCallback attributeHandler)
118 {
119     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
120     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
121
122     return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
123 }
124
125 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
126         QualityOfService QoS)
127 {
128     QueryParamsMap mapCpy(queryParametersMap);
129
130     if(!resourceType.empty())
131     {
132         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
133     }
134
135     if(!resourceInterface.empty())
136     {
137         mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
138     }
139
140     return result_guard(get(mapCpy, attributeHandler, QoS));
141 }
142
143 OCStackResult OCResource::put(const OCRepresentation& rep,
144                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
145                               QualityOfService QoS)
146 {
147     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
148                          m_devAddr, m_uri, rep, queryParametersMap,
149                          m_headerOptions, attributeHandler, QoS);
150 }
151
152 OCStackResult OCResource::put(const OCRepresentation& rep,
153                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
154 {
155     QualityOfService defaultQos = OC::QualityOfService::NaQos;
156     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
157     return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
158 }
159
160 OCStackResult OCResource::put(const std::string& resourceType,
161                               const std::string& resourceInterface, const OCRepresentation& rep,
162                               const QueryParamsMap& queryParametersMap,
163                               PutCallback attributeHandler)
164 {
165     QualityOfService defaultQos = OC::QualityOfService::NaQos;
166     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
167
168     return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
169             attributeHandler, defaultQos));
170 }
171
172 OCStackResult OCResource::put(const std::string& resourceType,
173                               const std::string& resourceInterface, const OCRepresentation& rep,
174                               const QueryParamsMap& queryParametersMap,
175                               PutCallback attributeHandler,
176                               QualityOfService QoS)
177 {
178     QueryParamsMap mapCpy(queryParametersMap);
179
180     if(!resourceType.empty())
181     {
182         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
183     }
184
185     if(!resourceInterface.empty())
186     {
187         mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
188     }
189
190     return result_guard(put(rep, mapCpy, attributeHandler, QoS));
191 }
192
193 OCStackResult OCResource::post(const OCRepresentation& rep,
194                                const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
195                                QualityOfService QoS)
196 {
197     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
198                          m_devAddr, m_uri, rep, queryParametersMap,
199                          m_headerOptions, attributeHandler, QoS);
200 }
201
202 OCStackResult OCResource::post(const OCRepresentation& rep,
203                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
204 {
205     QualityOfService defaultQos = OC::QualityOfService::NaQos;
206     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
207     return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
208 }
209
210 OCStackResult OCResource::post(const std::string& resourceType,
211                                const std::string& resourceInterface, const OCRepresentation& rep,
212                                const QueryParamsMap& queryParametersMap,
213                                PostCallback attributeHandler)
214 {
215     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
216     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
217
218     return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
219             defaultQoS));
220 }
221
222 OCStackResult OCResource::post(const std::string& resourceType,
223                                const std::string& resourceInterface, const OCRepresentation& rep,
224                                const QueryParamsMap& queryParametersMap,
225                                PostCallback attributeHandler,
226                                QualityOfService QoS)
227 {
228     QueryParamsMap mapCpy(queryParametersMap);
229
230     if(!resourceType.empty())
231     {
232         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
233     }
234
235     if(!resourceInterface.empty())
236     {
237         mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
238     }
239
240     return result_guard(post(rep, mapCpy, attributeHandler, QoS));
241 }
242
243 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
244 {
245     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
246                          m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
247 }
248
249 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
250 {
251     QualityOfService defaultQos = OC::QualityOfService::NaQos;
252     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
253
254     return result_guard(deleteResource(deleteHandler, defaultQos));
255 }
256
257 OCStackResult OCResource::observe(ObserveType observeType,
258         const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
259         QualityOfService QoS)
260 {
261     if(m_observeHandle != nullptr)
262     {
263         return result_guard(OC_STACK_INVALID_PARAM);
264     }
265
266     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
267                          observeType, &m_observeHandle, m_devAddr,
268                          m_uri, queryParametersMap, m_headerOptions,
269                          observeHandler, QoS);
270 }
271
272 OCStackResult OCResource::observe(ObserveType observeType,
273         const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
274 {
275     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
276     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
277
278     return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
279 }
280
281 OCStackResult OCResource::cancelObserve()
282 {
283     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
284     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
285     return result_guard(cancelObserve(defaultQoS));
286 }
287
288 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
289 {
290     if(m_observeHandle == nullptr)
291     {
292         return result_guard(OC_STACK_INVALID_PARAM);
293     }
294
295     OCStackResult result =  checked_guard(m_clientWrapper.lock(),
296             &IClientWrapper::CancelObserveResource,
297             m_observeHandle, "", m_uri, m_headerOptions, QoS);
298
299     if(result == OC_STACK_OK)
300     {
301         m_observeHandle = nullptr;
302     }
303
304     return result;
305 }
306
307 std::string OCResource::host() const
308 {
309     return std::string(m_devAddr.addr);
310 }
311
312 std::string OCResource::uri() const
313 {
314     return m_uri;
315 }
316
317 OCConnectivityType OCResource::connectivityType() const
318 {
319     return static_cast<OCConnectivityType>(
320            (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
321 }
322
323 bool OCResource::isObservable() const
324 {
325     return m_isObservable;
326 }
327
328 OCResourceIdentifier OCResource::uniqueIdentifier() const
329 {
330     return m_resourceId;
331 }
332
333 std::string OCResource::sid() const
334 {
335     return this->uniqueIdentifier().m_representation;
336 }
337
338 bool OCResource::operator==(const OCResource &other) const
339 {
340     return m_resourceId == other.m_resourceId;
341 }
342
343 bool OCResource::operator!=(const OCResource &other) const
344 {
345     return m_resourceId != other.m_resourceId;
346 }
347
348 bool OCResource::operator<(const OCResource &other) const
349 {
350     return m_resourceId < other.m_resourceId;
351 }
352
353 bool OCResource::operator>(const OCResource &other) const
354 {
355     return m_resourceId > other.m_resourceId;
356 }
357
358 bool OCResource::operator<=(const OCResource &other) const
359 {
360     return m_resourceId <= other.m_resourceId;
361 }
362
363 bool OCResource::operator>=(const OCResource &other) const
364 {
365     return m_resourceId >= other.m_resourceId;
366 }
367
368 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
369         const std::string& resourceUri)
370     :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
371 {
372 }
373
374 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
375 {
376     os << ri.m_representation<<ri.m_resourceUri;
377
378     return os;
379 }
380
381 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
382 {
383     return m_representation == other.m_representation
384         && m_resourceUri == other.m_resourceUri;
385 }
386
387 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
388 {
389     return !(*this == other);
390 }
391
392 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
393 {
394     return m_resourceUri < other.m_resourceUri
395         || (m_resourceUri == other.m_resourceUri &&
396                 m_representation < other.m_representation);
397 }
398
399 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
400 {
401     return *this != other && !(*this<other);
402 }
403
404 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
405 {
406     return !(*this > other);
407 }
408
409 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
410 {
411     return !(*this < other);
412 }
413
414 } // namespace OC
415