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