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"
24 #include <boost/lexical_cast.hpp>
29 using OC::result_guard;
30 using OC::checked_guard;
32 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper, const std::string& host,
33 const std::string& uri, const std::string& serverId,
34 OCConnectivityType connectivityType, bool observable,
35 const std::vector<std::string>& resourceTypes,
36 const std::vector<std::string>& interfaces)
37 : m_clientWrapper(clientWrapper), m_uri(uri), m_resourceId(serverId, m_uri),
39 m_connectivityType(connectivityType),
40 m_isObservable(observable),
41 m_isCollection(false), m_resourceTypes(resourceTypes), m_interfaces(interfaces),
42 m_observeHandle(nullptr)
44 m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
45 != m_interfaces.end();
48 resourceTypes.empty() ||
50 m_clientWrapper.expired())
52 throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
53 interfaces.empty(), m_clientWrapper.expired(), false, false);
57 OCResource::~OCResource()
61 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
62 GetCallback attributeHandler, QualityOfService QoS)
64 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
65 m_host, m_uri, m_connectivityType, queryParametersMap, m_headerOptions,
66 attributeHandler, QoS);
69 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
70 GetCallback attributeHandler)
72 QualityOfService defaultQos = OC::QualityOfService::NaQos;
73 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
74 return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
77 OCStackResult OCResource::get(const std::string& resourceType,
78 const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
79 GetCallback attributeHandler)
81 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
82 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
84 return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
87 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
90 QueryParamsMap mapCpy(queryParametersMap);
92 if(!resourceType.empty())
94 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
97 if(!resourceInterface.empty())
99 mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
102 return result_guard(get(mapCpy, attributeHandler, QoS));
105 OCStackResult OCResource::put(const OCRepresentation& rep,
106 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
107 QualityOfService QoS)
109 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
110 m_host, m_uri, m_connectivityType, rep, queryParametersMap,
111 m_headerOptions, attributeHandler, QoS);
114 OCStackResult OCResource::put(const OCRepresentation& rep,
115 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
117 QualityOfService defaultQos = OC::QualityOfService::NaQos;
118 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
119 return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
122 OCStackResult OCResource::put(const std::string& resourceType,
123 const std::string& resourceInterface, const OCRepresentation& rep,
124 const QueryParamsMap& queryParametersMap,
125 PutCallback attributeHandler)
127 QualityOfService defaultQos = OC::QualityOfService::NaQos;
128 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
130 return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
131 attributeHandler, defaultQos));
134 OCStackResult OCResource::put(const std::string& resourceType,
135 const std::string& resourceInterface, const OCRepresentation& rep,
136 const QueryParamsMap& queryParametersMap,
137 PutCallback attributeHandler,
138 QualityOfService QoS)
140 QueryParamsMap mapCpy(queryParametersMap);
142 if(!resourceType.empty())
144 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
147 if(!resourceInterface.empty())
149 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
152 return result_guard(put(rep, mapCpy, attributeHandler, QoS));
155 OCStackResult OCResource::post(const OCRepresentation& rep,
156 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
157 QualityOfService QoS)
159 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
160 m_host, m_uri, m_connectivityType, rep, queryParametersMap,
161 m_headerOptions, attributeHandler, QoS);
164 OCStackResult OCResource::post(const OCRepresentation& rep,
165 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
167 QualityOfService defaultQos = OC::QualityOfService::NaQos;
168 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
169 return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
172 OCStackResult OCResource::post(const std::string& resourceType,
173 const std::string& resourceInterface, const OCRepresentation& rep,
174 const QueryParamsMap& queryParametersMap,
175 PostCallback attributeHandler)
177 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
178 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
180 return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
184 OCStackResult OCResource::post(const std::string& resourceType,
185 const std::string& resourceInterface, const OCRepresentation& rep,
186 const QueryParamsMap& queryParametersMap,
187 PostCallback attributeHandler,
188 QualityOfService QoS)
190 QueryParamsMap mapCpy(queryParametersMap);
192 if(!resourceType.empty())
194 mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
197 if(!resourceInterface.empty())
199 mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
202 return result_guard(post(rep, mapCpy, attributeHandler, QoS));
205 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
207 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
208 m_host, m_uri, m_connectivityType, m_headerOptions, deleteHandler, QoS);
211 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
213 QualityOfService defaultQos = OC::QualityOfService::NaQos;
214 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
216 return result_guard(deleteResource(deleteHandler, defaultQos));
219 OCStackResult OCResource::observe(ObserveType observeType,
220 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
221 QualityOfService QoS)
223 if(m_observeHandle != nullptr)
225 return result_guard(OC_STACK_INVALID_PARAM);
228 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
229 observeType, &m_observeHandle, m_host,
230 m_uri, m_connectivityType, queryParametersMap, m_headerOptions,
231 observeHandler, QoS);
234 OCStackResult OCResource::observe(ObserveType observeType,
235 const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
237 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
238 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
240 return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
243 OCStackResult OCResource::cancelObserve()
245 QualityOfService defaultQoS = OC::QualityOfService::NaQos;
246 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
247 return result_guard(cancelObserve(defaultQoS));
250 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
252 if(m_observeHandle == nullptr)
254 return result_guard(OC_STACK_INVALID_PARAM);
257 OCStackResult result = checked_guard(m_clientWrapper.lock(),
258 &IClientWrapper::CancelObserveResource,
259 m_observeHandle, m_host, m_uri, m_headerOptions, QoS);
261 if(result == OC_STACK_OK)
263 m_observeHandle = nullptr;
269 std::string OCResource::host() const
274 std::string OCResource::uri() const
279 OCConnectivityType OCResource::connectivityType() const
281 return m_connectivityType;
284 bool OCResource::isObservable() const
286 return m_isObservable;
290 OCResourceIdentifier OCResource::uniqueIdentifier() const
295 std::string OCResource::sid() const
297 return this->uniqueIdentifier().m_representation;
300 bool OCResource::operator==(const OCResource &other) const
302 return m_resourceId == other.m_resourceId;
305 bool OCResource::operator!=(const OCResource &other) const
307 return m_resourceId != other.m_resourceId;
310 bool OCResource::operator<(const OCResource &other) const
312 return m_resourceId < other.m_resourceId;
315 bool OCResource::operator>(const OCResource &other) const
317 return m_resourceId > other.m_resourceId;
320 bool OCResource::operator<=(const OCResource &other) const
322 return m_resourceId <= other.m_resourceId;
325 bool OCResource::operator>=(const OCResource &other) const
327 return m_resourceId >= other.m_resourceId;
330 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
331 const std::string& resourceUri)
332 :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
336 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
339 os << ri.m_representation<<ri.m_resourceUri;
344 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
346 return m_representation == other.m_representation
347 && m_resourceUri == other.m_resourceUri;
350 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
352 return !(*this == other);
355 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
357 return m_resourceUri < other.m_resourceUri
358 || (m_resourceUri == other.m_resourceUri &&
359 m_representation < other.m_representation);
362 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
364 return *this != other && !(*this<other);
367 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
369 return !(*this > other);
372 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
374 return !(*this < other);