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 uint8_t resourceProperty = 0;
162 resourceProperty = (resourceProperty | OC_OBSERVABLE);
164 return std::shared_ptr<OCResource>(new OCResource(m_client,
166 uri, "", connectivityType,
172 OCStackResult OCPlatform_impl::findResource(const std::string& host,
173 const std::string& resourceName,
174 OCConnectivityType connectivityType,
175 FindCallback resourceHandler)
177 return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
180 OCStackResult OCPlatform_impl::findResource(const std::string& host,
181 const std::string& resourceName,
182 OCConnectivityType connectivityType,
183 FindCallback resourceHandler,
184 QualityOfService QoS)
186 return checked_guard(m_client, &IClientWrapper::ListenForResource,
187 host, resourceName, connectivityType, resourceHandler, QoS);
190 OCStackResult OCPlatform_impl::findResource(const std::string& host,
191 const std::string& resourceName,
192 OCConnectivityType connectivityType,
193 FindCallback resourceHandler,
194 FindErrorCallback errorHandler)
196 return findResource(host, resourceName, connectivityType, resourceHandler,
197 errorHandler, m_cfg.QoS);
200 OCStackResult OCPlatform_impl::findResource(const std::string& host,
201 const std::string& resourceName,
202 OCConnectivityType connectivityType,
203 FindCallback resourceHandler,
204 FindErrorCallback errorHandler,
205 QualityOfService QoS)
207 return checked_guard(m_client, &IClientWrapper::ListenErrorForResource,
208 host, resourceName, connectivityType, resourceHandler,
212 OCStackResult OCPlatform_impl::findResourceList(const std::string& host,
213 const std::string& resourceName,
214 OCConnectivityType connectivityType,
215 FindResListCallback resourceHandler,
216 QualityOfService QoS)
218 return checked_guard(m_client, &IClientWrapper::ListenForResource2,
219 host, resourceName, connectivityType, resourceHandler, QoS);
222 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
223 const std::string& deviceURI,
224 OCConnectivityType connectivityType,
225 FindDeviceCallback deviceInfoHandler)
227 return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
228 deviceInfoHandler, m_cfg.QoS));
231 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
232 const std::string& deviceURI,
233 OCConnectivityType connectivityType,
234 FindDeviceCallback deviceInfoHandler,
235 QualityOfService QoS)
237 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
238 host, deviceURI, connectivityType, deviceInfoHandler, QoS);
241 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
242 const std::string& platformURI,
243 OCConnectivityType connectivityType,
244 FindPlatformCallback platformInfoHandler)
246 return result_guard(getPlatformInfo(host, platformURI, connectivityType,
247 platformInfoHandler, m_cfg.QoS));
250 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
251 const std::string& platformURI,
252 OCConnectivityType connectivityType,
253 FindPlatformCallback platformInfoHandler,
254 QualityOfService QoS)
256 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
257 host, platformURI, connectivityType, platformInfoHandler, QoS);
260 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
261 std::string& resourceURI,
262 const std::string& resourceTypeName,
263 const std::string& resourceInterface,
264 EntityHandler entityHandler,
265 uint8_t resourceProperty)
267 return checked_guard(m_server, &IServerWrapper::registerResource,
268 std::ref(resourceHandle), resourceURI, resourceTypeName,
269 resourceInterface, entityHandler, resourceProperty);
272 OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
274 return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
277 OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
279 return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
282 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
283 const std::shared_ptr< OCResource > resource)
285 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
286 std::vector<std::string> resourceTypes = resource->getResourceTypes();
288 return checked_guard(m_server, &IServerWrapper::registerResource,
289 std::ref(resourceHandle), resource->host() + resource->uri(),
290 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
291 (EntityHandler) nullptr, resourceProperty);
294 OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
296 return checked_guard(m_server, &IServerWrapper::unregisterResource,
300 OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
301 OCResourceHandle resourceHandle)
303 return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
306 OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
307 const std::vector<OCResourceHandle>& resourceHandles)
309 for(const auto& h : resourceHandles)
313 if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
322 OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
323 const OCResourceHandle resourceHandle)
325 return result_guard(OCBindResource(collectionHandle, resourceHandle));
328 OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
329 const std::vector<OCResourceHandle>& resourceHandles)
331 for(const auto& h : resourceHandles)
335 if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
344 OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
345 const std::string& resourceTypeName) const
347 return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
348 resourceHandle, resourceTypeName);
351 OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
352 const std::string& resourceInterfaceName) const
354 return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
355 resourceHandle, resourceInterfaceName);
358 OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
360 return checked_guard(m_server, &IServerWrapper::startPresence,
361 announceDurationSeconds);
364 OCStackResult OCPlatform_impl::stopPresence()
366 return checked_guard(m_server, &IServerWrapper::stopPresence);
369 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
370 const std::string& host,
371 OCConnectivityType connectivityType,
372 SubscribeCallback presenceHandler)
374 return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
378 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
379 const std::string& host,
380 const std::string& resourceType,
381 OCConnectivityType connectivityType,
382 SubscribeCallback presenceHandler)
384 return checked_guard(m_client, &IClientWrapper::SubscribePresence,
385 &presenceHandle, host, resourceType, connectivityType,
389 OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
391 return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
392 std::ref(presenceHandle));
396 OCStackResult OCPlatform_impl::subscribeDevicePresence(OCPresenceHandle& presenceHandle,
397 const std::string& host,
398 const std::vector<std::string>& di,
399 OCConnectivityType connectivityType,
400 ObserveCallback callback)
402 return checked_guard(m_client, &IClientWrapper::SubscribeDevicePresence,
403 &presenceHandle, host, di, connectivityType, callback);
407 OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
409 return checked_guard(m_server, &IServerWrapper::sendResponse,
413 OCStackResult OCPlatform_impl::publishResourceToRD(const std::string& host,
414 OCConnectivityType connectivityType,
415 ResourceHandles& resourceHandles,
416 PublishResourceCallback callback)
418 return publishResourceToRD(host, connectivityType, resourceHandles,
419 callback, m_cfg.QoS);
422 OCStackResult OCPlatform_impl::publishResourceToRD(const std::string& host,
423 OCConnectivityType connectivityType,
424 ResourceHandles& resourceHandles,
425 PublishResourceCallback callback,
426 QualityOfService qos)
428 return checked_guard(m_server, &IServerWrapper::publishResourceToRD,
429 host, connectivityType, resourceHandles, callback,
430 static_cast<OCQualityOfService>(qos));
433 OCStackResult OCPlatform_impl::deleteResourceFromRD(const std::string& host,
434 OCConnectivityType connectivityType,
435 ResourceHandles& resourceHandles,
436 DeleteResourceCallback callback)
438 return deleteResourceFromRD(host, connectivityType, resourceHandles, callback,
442 OCStackResult OCPlatform_impl::deleteResourceFromRD(const std::string& host,
443 OCConnectivityType connectivityType,
444 ResourceHandles& resourceHandles,
445 DeleteResourceCallback callback,
446 QualityOfService qos)
448 return checked_guard(m_server, &IServerWrapper::deleteResourceFromRD,
449 host, connectivityType, resourceHandles, callback,
450 static_cast<OCQualityOfService>(qos));
453 std::weak_ptr<std::recursive_mutex> OCPlatform_impl::csdkLock()
458 OCStackResult OCPlatform_impl::findDirectPairingDevices(unsigned short waittime,
459 GetDirectPairedCallback directPairingHandler)
461 return checked_guard(m_client, &IClientWrapper::FindDirectPairingDevices,
462 waittime, directPairingHandler);
466 OCStackResult OCPlatform_impl::getDirectPairedDevices(
467 GetDirectPairedCallback directPairingHandler)
470 return checked_guard(m_client, &IClientWrapper::GetDirectPairedDevices,
471 directPairingHandler);
474 OCStackResult OCPlatform_impl::doDirectPairing(std::shared_ptr<OCDirectPairing> peer,
476 const std::string& pinNumber,
477 DirectPairingCallback resultCallback)
479 return checked_guard(m_client, &IClientWrapper::DoDirectPairing,
480 peer, pmSel, pinNumber, resultCallback);
483 OCAccountManager::Ptr OCPlatform_impl::constructAccountManagerObject(const std::string& host,
484 OCConnectivityType connectivityType)
488 return std::shared_ptr<OCAccountManager>();
491 return std::shared_ptr<OCAccountManager>(new OCAccountManager(m_client,
497 OCStackResult OCPlatform_impl::getDeviceId(OCUUIdentity *myUuid)
499 return OCGetDeviceId(myUuid);
502 OCStackResult OCPlatform_impl::setDeviceId(const OCUUIdentity *myUuid)
504 return OCSetDeviceId(myUuid);