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