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;
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)
37 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
38 != m_interfaces.end();
41 resourceTypes.empty() ||
43 m_clientWrapper.expired())
45 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
46 interfaces.empty(), m_clientWrapper.expired(), false, false);
50 OCResource::~OCResource()
54 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
55 GetCallback attributeHandler, QualityOfService QoS)
57 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
58 m_host, m_uri, queryParametersMap, m_headerOptions, attributeHandler, QoS);
61 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
62 GetCallback attributeHandler)
64 QualityOfService defaultQos = OC::QualityOfService::NaQos;
65 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
66 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
69 OCStackResult OCResource::get(const std::string& resourceType,
70 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
71 GetCallback attributeHandler)
73 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
74 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
76 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
79 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
82 QueryParamsMap mapCpy(queryParametersMap);
84 if(!resourceType.empty())
86 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
89 if(!resourceInterface.empty())
91 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
94 return result_guard(get(mapCpy, attributeHandler, QoS));
97 OCStackResult OCResource::put(const OCRepresentation& rep,
98 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
101 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
102 m_host, m_uri, rep, queryParametersMap, m_headerOptions, attributeHandler, QoS);
105 OCStackResult OCResource::put(const OCRepresentation& rep,
106 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
108 QualityOfService defaultQos = OC::QualityOfService::NaQos;
109 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
110 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
113 OCStackResult OCResource::put(const std::string& resourceType,
114 const std::string& resourceInterface, const OCRepresentation& rep,
115 const QueryParamsMap& queryParametersMap,
116 PutCallback attributeHandler)
118 QualityOfService defaultQos = OC::QualityOfService::NaQos;
119 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
121 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
122 attributeHandler, defaultQos));
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)
131 QueryParamsMap mapCpy(queryParametersMap);
133 if(!resourceType.empty())
135 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
138 if(!resourceInterface.empty())
140 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
143 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
146 OCStackResult OCResource::post(const OCRepresentation& rep,
147 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
148 QualityOfService QoS)
150 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
151 m_host, m_uri, rep, queryParametersMap, m_headerOptions, attributeHandler, QoS);
154 OCStackResult OCResource::post(const OCRepresentation& rep,
155 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
157 QualityOfService defaultQos = OC::QualityOfService::NaQos;
158 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
159 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
162 OCStackResult OCResource::post(const std::string& resourceType,
163 const std::string& resourceInterface, const OCRepresentation& rep,
164 const QueryParamsMap& queryParametersMap,
165 PostCallback attributeHandler)
167 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
168 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
170 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
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)
180 QueryParamsMap mapCpy(queryParametersMap);
182 if(!resourceType.empty())
184 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
187 if(!resourceInterface.empty())
189 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
192 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
195 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
197 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
198 m_host, m_uri, m_headerOptions, deleteHandler, QoS);
201 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
203 QualityOfService defaultQos = OC::QualityOfService::NaQos;
204 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
206 return result_guard(deleteResource(deleteHandler, defaultQos));
209 OCStackResult OCResource::observe(ObserveType observeType,
210 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
211 QualityOfService QoS)
213 if(m_observeHandle != nullptr)
215 return result_guard(OC_STACK_INVALID_PARAM);
218 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
219 observeType, &m_observeHandle, m_host,
220 m_uri, queryParametersMap, m_headerOptions, observeHandler, QoS);
223 OCStackResult OCResource::observe(ObserveType observeType,
224 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
226 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
227 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
229 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
232 OCStackResult OCResource::cancelObserve()
234 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
235 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
236 return result_guard(cancelObserve(defaultQoS));
239 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
241 if(m_observeHandle == nullptr)
243 return result_guard(OC_STACK_INVALID_PARAM);
246 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::CancelObserveResource,
247 m_observeHandle, m_host, m_uri, m_headerOptions, QoS);
250 std::string OCResource::host() const
255 std::string OCResource::uri() const
260 bool OCResource::isObservable() const
262 return m_isObservable;