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 //******************************************************************
23 // OCPlatform_impl.cpp
25 // Description: Implementation of the OCPlatform functionality. It contains
26 // a singleton interface that is used only by the OCPlatform namespace and is the
27 // central entrance to the stack.
31 //*********************************************************************
33 #include "OCPlatform_impl.h"
41 #include "OCPlatform.h"
43 #include "OCException.h"
44 #include "OCUtilities.h"
45 #include "ocpayload.h"
47 #include "oc_logger.hpp"
52 PlatformConfig& OCPlatform_impl::globalConfig()
54 static PlatformConfig s_config;
58 void OCPlatform_impl::Configure(const PlatformConfig& config)
60 OCRegisterPersistentStorageHandler(config.ps);
61 globalConfig() = config;
64 OCPlatform_impl& OCPlatform_impl::Instance()
66 static OCPlatform_impl platform(globalConfig());
70 void OCPlatform_impl::init(const PlatformConfig& config)
74 case ModeType::Server:
75 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
78 case ModeType::Client:
79 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
83 case ModeType::Gateway:
84 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
85 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
90 OCPlatform_impl::OCPlatform_impl(const PlatformConfig& config)
92 m_WrapperInstance { make_unique<WrapperFactory>() },
93 m_csdkLock { std::make_shared<std::recursive_mutex>() }
98 OCPlatform_impl::~OCPlatform_impl(void)
102 OCStackResult OCPlatform_impl::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
104 return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
108 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle,
109 QualityOfService QoS)
111 return result_guard(OCNotifyAllObservers(resourceHandle,
112 static_cast<OCQualityOfService>(QoS)));
115 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle)
117 return notifyAllObservers(resourceHandle, m_cfg.QoS);
120 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
121 ObservationIds& observationIds,
122 const std::shared_ptr<OCResourceResponse> pResponse)
124 return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
127 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
128 ObservationIds& observationIds,
129 const std::shared_ptr<OCResourceResponse> pResponse,
130 QualityOfService QoS)
134 return result_guard(OC_STACK_ERROR);
137 OCRepPayload* pl = pResponse->getResourceRepresentation().getPayload();
138 OCStackResult result =
139 OCNotifyListOfObservers(resourceHandle,
140 &observationIds[0], observationIds.size(),
142 static_cast<OCQualityOfService>(QoS));
143 OCRepPayloadDestroy(pl);
144 return result_guard(result);
147 OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
148 const std::string& uri,
149 OCConnectivityType connectivityType,
151 const std::vector<std::string>& resourceTypes,
152 const std::vector<std::string>& interfaces)
156 return std::shared_ptr<OCResource>();
159 return std::shared_ptr<OCResource>(new OCResource(m_client,
161 uri, "", connectivityType,
167 OCStackResult OCPlatform_impl::findResource(const std::string& host,
168 const std::string& resourceName,
169 OCConnectivityType connectivityType,
170 FindCallback resourceHandler)
172 return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
175 OCStackResult OCPlatform_impl::findResource(const std::string& host,
176 const std::string& resourceName,
177 OCConnectivityType connectivityType,
178 FindCallback resourceHandler,
179 QualityOfService QoS)
181 return checked_guard(m_client, &IClientWrapper::ListenForResource,
182 host, resourceName, connectivityType, resourceHandler, QoS);
185 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
186 const std::string& deviceURI,
187 OCConnectivityType connectivityType,
188 FindDeviceCallback deviceInfoHandler)
190 return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
191 deviceInfoHandler, m_cfg.QoS));
194 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
195 const std::string& deviceURI,
196 OCConnectivityType connectivityType,
197 FindDeviceCallback deviceInfoHandler,
198 QualityOfService QoS)
200 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
201 host, deviceURI, connectivityType, deviceInfoHandler, QoS);
204 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
205 const std::string& platformURI,
206 OCConnectivityType connectivityType,
207 FindPlatformCallback platformInfoHandler)
209 return result_guard(getPlatformInfo(host, platformURI, connectivityType,
210 platformInfoHandler, m_cfg.QoS));
213 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
214 const std::string& platformURI,
215 OCConnectivityType connectivityType,
216 FindPlatformCallback platformInfoHandler,
217 QualityOfService QoS)
219 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
220 host, platformURI, connectivityType, platformInfoHandler, QoS);
223 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
224 std::string& resourceURI,
225 const std::string& resourceTypeName,
226 const std::string& resourceInterface,
227 EntityHandler entityHandler,
228 uint8_t resourceProperty)
230 return checked_guard(m_server, &IServerWrapper::registerResource,
231 std::ref(resourceHandle), resourceURI, resourceTypeName,
232 resourceInterface, entityHandler, resourceProperty);
235 OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
237 return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
240 OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
242 return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
245 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
246 const std::shared_ptr< OCResource > resource)
248 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
249 std::vector<std::string> resourceTypes = resource->getResourceTypes();
251 return checked_guard(m_server, &IServerWrapper::registerResource,
252 std::ref(resourceHandle), resource->host() + resource->uri(),
253 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
254 (EntityHandler) nullptr, resourceProperty);
257 OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
259 return checked_guard(m_server, &IServerWrapper::unregisterResource,
263 OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
264 OCResourceHandle resourceHandle)
266 return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
269 OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
270 const std::vector<OCResourceHandle>& resourceHandles)
272 for(const auto& h : resourceHandles)
276 if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
285 OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
286 const OCResourceHandle resourceHandle)
288 return result_guard(OCBindResource(collectionHandle, resourceHandle));
291 OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
292 const std::vector<OCResourceHandle>& resourceHandles)
294 for(const auto& h : resourceHandles)
298 if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
307 OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
308 const std::string& resourceTypeName) const
310 return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
311 resourceHandle, resourceTypeName);
314 OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
315 const std::string& resourceInterfaceName) const
317 return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
318 resourceHandle, resourceInterfaceName);
321 OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
323 return checked_guard(m_server, &IServerWrapper::startPresence,
324 announceDurationSeconds);
327 OCStackResult OCPlatform_impl::stopPresence()
329 return checked_guard(m_server, &IServerWrapper::stopPresence);
332 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
333 const std::string& host,
334 OCConnectivityType connectivityType,
335 SubscribeCallback presenceHandler)
337 return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
341 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
342 const std::string& host,
343 const std::string& resourceType,
344 OCConnectivityType connectivityType,
345 SubscribeCallback presenceHandler)
347 return checked_guard(m_client, &IClientWrapper::SubscribePresence,
348 &presenceHandle, host, resourceType, connectivityType,
352 OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
354 return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
355 std::ref(presenceHandle));
358 OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
360 return checked_guard(m_server, &IServerWrapper::sendResponse,
364 std::weak_ptr<std::recursive_mutex> OCPlatform_impl::csdkLock()