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"
46 #include "oc_logger.hpp"
51 PlatformConfig& OCPlatform_impl::globalConfig()
53 static PlatformConfig s_config;
57 void OCPlatform_impl::Configure(const PlatformConfig& config)
59 OCRegisterPersistentStorageHandler(config.ps);
60 globalConfig() = config;
63 OCPlatform_impl& OCPlatform_impl::Instance()
65 static OCPlatform_impl platform(globalConfig());
69 void OCPlatform_impl::init(const PlatformConfig& config)
73 case ModeType::Server:
74 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
77 case ModeType::Client:
78 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
82 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
83 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
88 OCPlatform_impl::OCPlatform_impl(const PlatformConfig& config)
90 m_WrapperInstance { make_unique<WrapperFactory>() },
91 m_csdkLock { std::make_shared<std::recursive_mutex>() }
96 OCPlatform_impl::~OCPlatform_impl(void)
100 OCStackResult OCPlatform_impl::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
102 return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
106 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle,
107 QualityOfService QoS)
109 return result_guard(OCNotifyAllObservers(resourceHandle,
110 static_cast<OCQualityOfService>(QoS)));
113 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle)
115 return notifyAllObservers(resourceHandle, m_cfg.QoS);
118 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
119 ObservationIds& observationIds,
120 const std::shared_ptr<OCResourceResponse> pResponse)
122 return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
125 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
126 ObservationIds& observationIds,
127 const std::shared_ptr<OCResourceResponse> pResponse,
128 QualityOfService QoS)
132 return result_guard(OC_STACK_ERROR);
135 std::string payload(pResponse->getResourceRepresentation().getJSONRepresentation());
138 OCNotifyListOfObservers(resourceHandle,
139 &observationIds[0], observationIds.size(),
141 static_cast<OCQualityOfService>(QoS)));
144 OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
145 const std::string& uri,
146 OCConnectivityType connectivityType,
148 const std::vector<std::string>& resourceTypes,
149 const std::vector<std::string>& interfaces)
153 return std::shared_ptr<OCResource>();
156 return std::shared_ptr<OCResource>(new OCResource(m_client,
158 uri, "", connectivityType,
164 OCStackResult OCPlatform_impl::findResource(const std::string& host,
165 const std::string& resourceName,
166 OCConnectivityType connectivityType,
167 FindCallback resourceHandler)
169 return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
172 OCStackResult OCPlatform_impl::findResource(const std::string& host,
173 const std::string& resourceName,
174 OCConnectivityType connectivityType,
175 FindCallback resourceHandler,
176 QualityOfService QoS)
178 return checked_guard(m_client, &IClientWrapper::ListenForResource,
179 host, resourceName, connectivityType, resourceHandler, QoS);
182 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
183 const std::string& deviceURI,
184 OCConnectivityType connectivityType,
185 FindDeviceCallback deviceInfoHandler)
187 return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
188 deviceInfoHandler, m_cfg.QoS));
191 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
192 const std::string& deviceURI,
193 OCConnectivityType connectivityType,
194 FindDeviceCallback deviceInfoHandler,
195 QualityOfService QoS)
197 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
198 host, deviceURI, connectivityType, deviceInfoHandler, QoS);
201 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
202 const std::string& platformURI,
203 OCConnectivityType connectivityType,
204 FindPlatformCallback platformInfoHandler)
206 return result_guard(getPlatformInfo(host, platformURI, connectivityType,
207 platformInfoHandler, m_cfg.QoS));
210 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
211 const std::string& platformURI,
212 OCConnectivityType connectivityType,
213 FindPlatformCallback platformInfoHandler,
214 QualityOfService QoS)
216 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
217 host, platformURI, connectivityType, platformInfoHandler, QoS);
220 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
221 std::string& resourceURI,
222 const std::string& resourceTypeName,
223 const std::string& resourceInterface,
224 EntityHandler entityHandler,
225 uint8_t resourceProperty)
227 return checked_guard(m_server, &IServerWrapper::registerResource,
228 std::ref(resourceHandle), resourceURI, resourceTypeName,
229 resourceInterface, entityHandler, resourceProperty);
232 OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
234 return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
237 OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
239 return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
242 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
243 const std::shared_ptr< OCResource > resource)
245 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
246 std::vector<std::string> resourceTypes = resource->getResourceTypes();
248 return checked_guard(m_server, &IServerWrapper::registerResource,
249 std::ref(resourceHandle), resource->uri(),
250 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
251 (EntityHandler) nullptr, resourceProperty);
254 OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
256 return checked_guard(m_server, &IServerWrapper::unregisterResource,
260 OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
261 OCResourceHandle resourceHandle)
263 return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
266 OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
267 const std::vector<OCResourceHandle>& resourceHandles)
269 for(const auto& h : resourceHandles)
273 if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
282 OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
283 const OCResourceHandle resourceHandle)
285 return result_guard(OCBindResource(collectionHandle, resourceHandle));
288 OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
289 const std::vector<OCResourceHandle>& resourceHandles)
291 for(const auto& h : resourceHandles)
295 if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
304 OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
305 const std::string& resourceTypeName) const
307 return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
308 resourceHandle, resourceTypeName);
311 OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
312 const std::string& resourceInterfaceName) const
314 return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
315 resourceHandle, resourceInterfaceName);
318 OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
320 return checked_guard(m_server, &IServerWrapper::startPresence,
321 announceDurationSeconds);
324 OCStackResult OCPlatform_impl::stopPresence()
326 return checked_guard(m_server, &IServerWrapper::stopPresence);
329 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
330 const std::string& host,
331 OCConnectivityType connectivityType,
332 SubscribeCallback presenceHandler)
334 return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
338 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
339 const std::string& host,
340 const std::string& resourceType,
341 OCConnectivityType connectivityType,
342 SubscribeCallback presenceHandler)
344 return checked_guard(m_client, &IClientWrapper::SubscribePresence,
345 &presenceHandle, host, resourceType, connectivityType,
349 OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
351 return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
352 std::ref(presenceHandle));
355 OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
357 return checked_guard(m_server, &IServerWrapper::sendResponse,