Merge "Removed Protocol Plugin developer guide document."
[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 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper, const std::string& host,
31                        const std::string& uri, bool observable, const std::vector<std::string>& resourceTypes,
32                        const std::vector<std::string>& interfaces)
33  :  m_clientWrapper(clientWrapper), m_uri(uri), m_host(host), m_isObservable(observable),
34     m_isCollection(false), m_resourceTypes(resourceTypes), m_interfaces(interfaces),
35     m_observeHandle(nullptr)
36 {
37     m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
38                         != m_interfaces.end();
39
40     if (m_uri.empty() ||
41         resourceTypes.empty() ||
42         interfaces.empty()||
43         m_clientWrapper.expired())
44     {
45         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
46                 interfaces.empty(), m_clientWrapper.expired());
47     }
48 }
49
50 OCResource::~OCResource()
51 {
52 }
53
54 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
55                               GetCallback attributeHandler, QualityOfService QoS)
56 {
57     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
58                          m_host, m_uri, queryParametersMap, m_headerOptions, attributeHandler, QoS);
59 }
60
61 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
62                               GetCallback attributeHandler)
63 {
64     QualityOfService defaultQos = OC::QualityOfService::NaQos;
65     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
66     return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
67 }
68
69 OCStackResult OCResource::get(const std::string& resourceType,
70                      const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
71                      GetCallback attributeHandler)
72 {
73     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
74     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
75
76     return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
77 }
78
79 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
80         QualityOfService QoS)
81 {
82     QueryParamsMap mapCpy(queryParametersMap);
83
84     if(!resourceType.empty())
85     {
86         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
87     }
88
89     if(!resourceInterface.empty())
90     {
91         mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
92     }
93
94     return result_guard(get(mapCpy, attributeHandler, QoS));
95 }
96
97 OCStackResult OCResource::put(const OCRepresentation& rep,
98                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
99                               QualityOfService QoS)
100 {
101     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
102                          m_host, m_uri, rep, queryParametersMap, m_headerOptions, attributeHandler, QoS);
103 }
104
105 OCStackResult OCResource::put(const OCRepresentation& rep,
106                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
107 {
108     QualityOfService defaultQos = OC::QualityOfService::NaQos;
109     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
110     return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
111 }
112
113 OCStackResult OCResource::put(const std::string& resourceType,
114                               const std::string& resourceInterface, const OCRepresentation& rep,
115                               const QueryParamsMap& queryParametersMap,
116                               PutCallback attributeHandler)
117 {
118     QualityOfService defaultQos = OC::QualityOfService::NaQos;
119     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
120
121     return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
122             attributeHandler, defaultQos));
123 }
124
125 OCStackResult OCResource::put(const std::string& resourceType,
126                               const std::string& resourceInterface, const OCRepresentation& rep,
127                               const QueryParamsMap& queryParametersMap,
128                               PutCallback attributeHandler,
129                               QualityOfService QoS)
130 {
131     QueryParamsMap mapCpy(queryParametersMap);
132
133     if(!resourceType.empty())
134     {
135         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
136     }
137
138     if(!resourceInterface.empty())
139     {
140         mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
141     }
142
143     return result_guard(put(rep, mapCpy, attributeHandler, QoS));
144 }
145
146 OCStackResult OCResource::post(const OCRepresentation& rep,
147                                const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
148                                QualityOfService QoS)
149 {
150     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
151                          m_host, m_uri, rep, queryParametersMap, m_headerOptions, attributeHandler, QoS);
152 }
153
154 OCStackResult OCResource::post(const OCRepresentation& rep,
155                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
156 {
157     QualityOfService defaultQos = OC::QualityOfService::NaQos;
158     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
159     return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
160 }
161
162 OCStackResult OCResource::post(const std::string& resourceType,
163                                const std::string& resourceInterface, const OCRepresentation& rep,
164                                const QueryParamsMap& queryParametersMap,
165                                PostCallback attributeHandler)
166 {
167     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
168     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
169
170     return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
171             defaultQoS));
172 }
173
174 OCStackResult OCResource::post(const std::string& resourceType,
175                                const std::string& resourceInterface, const OCRepresentation& rep,
176                                const QueryParamsMap& queryParametersMap,
177                                PostCallback attributeHandler,
178                                QualityOfService QoS)
179 {
180     QueryParamsMap mapCpy(queryParametersMap);
181
182     if(!resourceType.empty())
183     {
184         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
185     }
186
187     if(!resourceInterface.empty())
188     {
189         mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
190     }
191
192     return result_guard(post(rep, mapCpy, attributeHandler, QoS));
193 }
194
195 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
196 {
197     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
198                          m_host, m_uri, m_headerOptions, deleteHandler, QoS);
199 }
200
201 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
202 {
203     QualityOfService defaultQos = OC::QualityOfService::NaQos;
204     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
205
206     return result_guard(deleteResource(deleteHandler, defaultQos));
207 }
208
209 OCStackResult OCResource::observe(ObserveType observeType,
210         const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
211         QualityOfService QoS)
212 {
213     if(m_observeHandle != nullptr)
214     {
215         return result_guard(OC_STACK_INVALID_PARAM);
216     }
217
218     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
219                          observeType, &m_observeHandle, m_host,
220                          m_uri, queryParametersMap, m_headerOptions, observeHandler, QoS);
221 }
222
223 OCStackResult OCResource::observe(ObserveType observeType,
224         const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
225 {
226     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
227     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
228
229     return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
230 }
231
232 OCStackResult OCResource::cancelObserve()
233 {
234     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
235     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
236     return result_guard(cancelObserve(defaultQoS));
237 }
238
239 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
240 {
241     if(m_observeHandle == nullptr)
242     {
243         return result_guard(OC_STACK_INVALID_PARAM);
244     }
245
246     OCStackResult result =  checked_guard(m_clientWrapper.lock(),
247             &IClientWrapper::CancelObserveResource,
248             m_observeHandle, m_host, m_uri, m_headerOptions, QoS);
249
250     if(result == OC_STACK_OK)
251     {
252         m_observeHandle = nullptr;
253     }
254
255     return result;
256 }
257
258 std::string OCResource::host() const
259 {
260     return m_host;
261 }
262
263 std::string OCResource::uri() const
264 {
265     return m_uri;
266 }
267
268 bool OCResource::isObservable() const
269 {
270     return m_isObservable;
271 }
272
273 } // namespace OC