1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "OCResource.h"
22 #include "OCUtilities.h"
27 using OC::result_guard;
28 using OC::checked_guard;
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)
40 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
41 != m_interfaces.end();
44 resourceTypes.empty() ||
46 m_clientWrapper.expired())
48 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
49 interfaces.empty(), m_clientWrapper.expired(), false, false);
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)
60 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
61 != m_interfaces.end();
64 resourceTypes.empty() ||
66 m_clientWrapper.expired())
68 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
69 interfaces.empty(), m_clientWrapper.expired(), false, false);
74 OCResource::~OCResource()
78 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
79 GetCallback attributeHandler, QualityOfService QoS)
82 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
83 m_host, m_uri, m_connectivityType, queryParametersMap, m_headerOptions,
84 attributeHandler, QoS);
86 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
87 m_host, m_uri, queryParametersMap, m_headerOptions, attributeHandler, QoS);
91 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
92 GetCallback attributeHandler)
94 QualityOfService defaultQos = OC::QualityOfService::NaQos;
95 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
96 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
99 OCStackResult OCResource::get(const std::string& resourceType,
100 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
101 GetCallback attributeHandler)
103 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
104 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
106 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
109 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
110 QualityOfService QoS)
112 QueryParamsMap mapCpy(queryParametersMap);
114 if(!resourceType.empty())
116 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
119 if(!resourceInterface.empty())
121 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
124 return result_guard(get(mapCpy, attributeHandler, QoS));
127 OCStackResult OCResource::put(const OCRepresentation& rep,
128 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
129 QualityOfService QoS)
132 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
133 m_host, m_uri, m_connectivityType, rep, queryParametersMap,
134 m_headerOptions, attributeHandler, QoS);
136 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
137 m_host, m_uri, rep, queryParametersMap, m_headerOptions, attributeHandler, QoS);
141 OCStackResult OCResource::put(const OCRepresentation& rep,
142 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
144 QualityOfService defaultQos = OC::QualityOfService::NaQos;
145 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
146 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
149 OCStackResult OCResource::put(const std::string& resourceType,
150 const std::string& resourceInterface, const OCRepresentation& rep,
151 const QueryParamsMap& queryParametersMap,
152 PutCallback attributeHandler)
154 QualityOfService defaultQos = OC::QualityOfService::NaQos;
155 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
157 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
158 attributeHandler, defaultQos));
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)
167 QueryParamsMap mapCpy(queryParametersMap);
169 if(!resourceType.empty())
171 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
174 if(!resourceInterface.empty())
176 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
179 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
182 OCStackResult OCResource::post(const OCRepresentation& rep,
183 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
184 QualityOfService QoS)
187 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
188 m_host, m_uri, m_connectivityType, rep, queryParametersMap,
189 m_headerOptions, attributeHandler, QoS);
191 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
192 m_host, m_uri, rep, queryParametersMap, m_headerOptions, attributeHandler, QoS);
196 OCStackResult OCResource::post(const OCRepresentation& rep,
197 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
199 QualityOfService defaultQos = OC::QualityOfService::NaQos;
200 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
201 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
204 OCStackResult OCResource::post(const std::string& resourceType,
205 const std::string& resourceInterface, const OCRepresentation& rep,
206 const QueryParamsMap& queryParametersMap,
207 PostCallback attributeHandler)
209 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
210 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
212 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
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)
222 QueryParamsMap mapCpy(queryParametersMap);
224 if(!resourceType.empty())
226 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
229 if(!resourceInterface.empty())
231 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
234 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
237 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
240 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
241 m_host, m_uri, m_connectivityType, m_headerOptions, deleteHandler, QoS);
243 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
244 m_host, m_uri, m_headerOptions, deleteHandler, QoS);
248 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
250 QualityOfService defaultQos = OC::QualityOfService::NaQos;
251 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
253 return result_guard(deleteResource(deleteHandler, defaultQos));
256 OCStackResult OCResource::observe(ObserveType observeType,
257 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
258 QualityOfService QoS)
260 if(m_observeHandle != nullptr)
262 return result_guard(OC_STACK_INVALID_PARAM);
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);
271 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
272 observeType, &m_observeHandle, m_host,
273 m_uri, queryParametersMap, m_headerOptions, observeHandler, QoS);
277 OCStackResult OCResource::observe(ObserveType observeType,
278 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
280 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
281 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
283 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
286 OCStackResult OCResource::cancelObserve()
288 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
289 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
290 return result_guard(cancelObserve(defaultQoS));
293 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
295 if(m_observeHandle == nullptr)
297 return result_guard(OC_STACK_INVALID_PARAM);
300 OCStackResult result = checked_guard(m_clientWrapper.lock(),
301 &IClientWrapper::CancelObserveResource,
302 m_observeHandle, m_host, m_uri, m_headerOptions, QoS);
304 if(result == OC_STACK_OK)
306 m_observeHandle = nullptr;
312 std::string OCResource::host() const
317 std::string OCResource::uri() const
323 uint8_t OCResource::connectivityType() const
325 return m_connectivityType;
329 bool OCResource::isObservable() const
331 return m_isObservable;