Fix build warnings
[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 #ifdef CA_INT
33 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper, const std::string& host,
34                        const std::string& uri, const std::string& serverId,
35                        OCConnectivityType connectivityType, bool observable,
36                        const std::vector<std::string>& resourceTypes,
37                        const std::vector<std::string>& interfaces)
38  :  m_clientWrapper(clientWrapper), m_uri(uri), m_resourceId(serverId, m_uri),
39     m_host(host),
40     m_connectivityType(connectivityType),
41     m_isObservable(observable),
42     m_isCollection(false), m_resourceTypes(resourceTypes), m_interfaces(interfaces),
43     m_observeHandle(nullptr)
44 {
45     m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
46                         != m_interfaces.end();
47
48     if (m_uri.empty() ||
49         resourceTypes.empty() ||
50         interfaces.empty()||
51         m_clientWrapper.expired())
52     {
53         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
54                 interfaces.empty(), m_clientWrapper.expired(), false, false);
55     }
56 }
57 #else
58 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper, const std::string& host,
59                        const std::string& uri, const std::string& serverId, bool observable,
60                        const std::vector<std::string>& resourceTypes,
61                        const std::vector<std::string>& interfaces)
62  :  m_clientWrapper(clientWrapper), m_uri(uri), m_resourceId(serverId, m_uri), m_host(host),
63     m_isObservable(observable), m_isCollection(false), m_resourceTypes(resourceTypes),
64     m_interfaces(interfaces), m_observeHandle(nullptr)
65 {
66     m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
67                         != m_interfaces.end();
68
69     if (m_uri.empty() ||
70         resourceTypes.empty() ||
71         interfaces.empty()||
72         m_clientWrapper.expired())
73     {
74         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
75                 interfaces.empty(), m_clientWrapper.expired(), false, false);
76     }
77 }
78 #endif
79
80 OCResource::~OCResource()
81 {
82 }
83
84 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
85                               GetCallback attributeHandler, QualityOfService QoS)
86 {
87 #ifdef CA_INT
88     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
89                          m_host, m_uri, m_connectivityType, queryParametersMap, m_headerOptions,
90                          attributeHandler, QoS);
91 #else
92     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
93                          m_host, m_uri, queryParametersMap, m_headerOptions, attributeHandler, QoS);
94 #endif
95 }
96
97 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
98                               GetCallback attributeHandler)
99 {
100     QualityOfService defaultQos = OC::QualityOfService::NaQos;
101     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
102     return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
103 }
104
105 OCStackResult OCResource::get(const std::string& resourceType,
106                      const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
107                      GetCallback attributeHandler)
108 {
109     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
110     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
111
112     return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
113 }
114
115 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
116         QualityOfService QoS)
117 {
118     QueryParamsMap mapCpy(queryParametersMap);
119
120     if(!resourceType.empty())
121     {
122         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
123     }
124
125     if(!resourceInterface.empty())
126     {
127         mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
128     }
129
130     return result_guard(get(mapCpy, attributeHandler, QoS));
131 }
132
133 OCStackResult OCResource::put(const OCRepresentation& rep,
134                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
135                               QualityOfService QoS)
136 {
137 #ifdef CA_INT
138     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
139                          m_host, m_uri, m_connectivityType, rep, queryParametersMap,
140                          m_headerOptions, attributeHandler, QoS);
141 #else
142     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
143                          m_host, m_uri, rep, queryParametersMap, m_headerOptions, attributeHandler, QoS);
144 #endif
145 }
146
147 OCStackResult OCResource::put(const OCRepresentation& rep,
148                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
149 {
150     QualityOfService defaultQos = OC::QualityOfService::NaQos;
151     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
152     return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
153 }
154
155 OCStackResult OCResource::put(const std::string& resourceType,
156                               const std::string& resourceInterface, const OCRepresentation& rep,
157                               const QueryParamsMap& queryParametersMap,
158                               PutCallback attributeHandler)
159 {
160     QualityOfService defaultQos = OC::QualityOfService::NaQos;
161     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
162
163     return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
164             attributeHandler, defaultQos));
165 }
166
167 OCStackResult OCResource::put(const std::string& resourceType,
168                               const std::string& resourceInterface, const OCRepresentation& rep,
169                               const QueryParamsMap& queryParametersMap,
170                               PutCallback attributeHandler,
171                               QualityOfService QoS)
172 {
173     QueryParamsMap mapCpy(queryParametersMap);
174
175     if(!resourceType.empty())
176     {
177         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
178     }
179
180     if(!resourceInterface.empty())
181     {
182         mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
183     }
184
185     return result_guard(put(rep, mapCpy, attributeHandler, QoS));
186 }
187
188 OCStackResult OCResource::post(const OCRepresentation& rep,
189                                const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
190                                QualityOfService QoS)
191 {
192 #ifdef CA_INT
193     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
194                          m_host, m_uri, m_connectivityType, rep, queryParametersMap,
195                          m_headerOptions, attributeHandler, QoS);
196 #else
197     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
198                          m_host, m_uri, rep, queryParametersMap, m_headerOptions, attributeHandler, QoS);
199 #endif
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 #ifdef CA_INT
246     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
247                          m_host, m_uri, m_connectivityType, m_headerOptions, deleteHandler, QoS);
248 #else
249     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
250                          m_host, m_uri, m_headerOptions, deleteHandler, QoS);
251 #endif
252 }
253
254 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
255 {
256     QualityOfService defaultQos = OC::QualityOfService::NaQos;
257     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
258
259     return result_guard(deleteResource(deleteHandler, defaultQos));
260 }
261
262 OCStackResult OCResource::observe(ObserveType observeType,
263         const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
264         QualityOfService QoS)
265 {
266     if(m_observeHandle != nullptr)
267     {
268         return result_guard(OC_STACK_INVALID_PARAM);
269     }
270
271 #ifdef CA_INT
272     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
273                          observeType, &m_observeHandle, m_host,
274                          m_uri, m_connectivityType, queryParametersMap, m_headerOptions,
275                          observeHandler, QoS);
276 #else
277     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
278                          observeType, &m_observeHandle, m_host,
279                          m_uri, queryParametersMap, m_headerOptions, observeHandler, QoS);
280 #endif
281 }
282
283 OCStackResult OCResource::observe(ObserveType observeType,
284         const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
285 {
286     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
287     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
288
289     return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
290 }
291
292 OCStackResult OCResource::cancelObserve()
293 {
294     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
295     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
296     return result_guard(cancelObserve(defaultQoS));
297 }
298
299 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
300 {
301     if(m_observeHandle == nullptr)
302     {
303         return result_guard(OC_STACK_INVALID_PARAM);
304     }
305
306     OCStackResult result =  checked_guard(m_clientWrapper.lock(),
307             &IClientWrapper::CancelObserveResource,
308             m_observeHandle, m_host, m_uri, m_headerOptions, QoS);
309
310     if(result == OC_STACK_OK)
311     {
312         m_observeHandle = nullptr;
313     }
314
315     return result;
316 }
317
318 std::string OCResource::host() const
319 {
320     return m_host;
321 }
322
323 std::string OCResource::uri() const
324 {
325     return m_uri;
326 }
327
328 #ifdef CA_INT
329 OCConnectivityType OCResource::connectivityType() const
330 {
331     return m_connectivityType;
332 }
333 #endif
334
335 bool OCResource::isObservable() const
336 {
337     return m_isObservable;
338 }
339
340
341 OCResourceIdentifier OCResource::uniqueIdentifier() const
342 {
343     return m_resourceId;
344 }
345
346 std::string OCResource::sid() const
347 {
348     std::ostringstream os;
349     os << this->uniqueIdentifier().m_representation;
350     return os.str();
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 bool OCResource::operator>(const OCResource &other) const
369 {
370     return m_resourceId > other.m_resourceId;
371 }
372
373 bool OCResource::operator<=(const OCResource &other) const
374 {
375     return m_resourceId <= other.m_resourceId;
376 }
377
378 bool OCResource::operator>=(const OCResource &other) const
379 {
380     return m_resourceId >= other.m_resourceId;
381 }
382
383 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
384         const std::string& resourceUri)
385     :m_representation(0), m_resourceUri(resourceUri)
386 {
387     // test required so we can create Resources without a server. Will leave as default.
388     if(!wireServerIdentifier.empty())
389     {
390         m_representation = boost::lexical_cast<unsigned int>(wireServerIdentifier);
391     }
392 }
393
394 ostream& operator <<(ostream& os, const OCResourceIdentifier& ri)
395 {
396
397     os << ri.m_representation<<ri.m_resourceUri;
398
399     return os;
400 }
401
402 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
403 {
404     return m_representation == other.m_representation
405         && m_resourceUri == other.m_resourceUri;
406 }
407
408 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
409 {
410     return !(*this == other);
411 }
412
413 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
414 {
415     return m_resourceUri < other.m_resourceUri
416         || (m_resourceUri == other.m_resourceUri &&
417                 m_representation < other.m_representation);
418 }
419
420 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
421 {
422     return *this != other && !(*this<other);
423 }
424
425 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
426 {
427     return !(*this > other);
428 }
429
430 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
431 {
432     return !(*this < other);
433 }
434
435 } // namespace OC