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