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"
48 #include "oc_logger.hpp"
50 #define TAG "OIC_PLATFORM"
55 PlatformConfig& OCPlatform_impl::globalConfig()
57 static PlatformConfig s_config;
61 OCStackResult OCPlatform_impl::Configure(const PlatformConfig& config)
63 OIC_LOG_V(INFO, TAG, "Configure : %d %p", config.transportType, config.ps);
64 OCStackResult res = OC_STACK_OK;
66 res = OCSetSecurePSI(config.key, config.ps, config.psEnc, config.psRescue);
67 if (OC_STACK_OK == res && config.psEnc)
69 OIC_LOG(INFO, TAG, "It uses psEnc for svr db.");
70 res = OCRegisterPersistentStorageHandler(config.psEnc);
73 OIC_LOG(INFO, TAG, "It uses ps for svr db.");
74 res = OCRegisterPersistentStorageHandler(config.ps);
77 globalConfig() = config;
81 OCPlatform_impl& OCPlatform_impl::Instance()
83 static OCPlatform_impl platform(globalConfig());
87 OCStackResult OCPlatform_impl::start()
89 OIC_LOG(INFO, TAG, "start");
91 OCStackResult res = OC_STACK_OK;
92 if (OC_CLIENT == m_modeType)
94 if (OC_STACK_OK != checked_guard(m_client, &IClientWrapper::start))
99 else if (OC_SERVER == m_modeType)
101 if (OC_STACK_OK != checked_guard(m_server, &IServerWrapper::start))
103 res = OC_STACK_ERROR;
106 else if (OC_CLIENT_SERVER == m_modeType || OC_GATEWAY == m_modeType)
108 if (OC_STACK_OK != checked_guard(m_client, &IClientWrapper::start))
110 res = OC_STACK_ERROR;
113 if (OC_STACK_OK != checked_guard(m_server, &IServerWrapper::start))
115 res = OC_STACK_ERROR;
120 res = OC_STACK_ERROR;
126 OCStackResult OCPlatform_impl::stop()
128 OIC_LOG(INFO, TAG, "stop");
130 OCStackResult res = OC_STACK_OK;
131 if (OC_CLIENT == m_modeType)
133 if (OC_STACK_OK != checked_guard(m_client, &IClientWrapper::stop))
135 res = OC_STACK_ERROR;
138 else if (OC_SERVER == m_modeType)
140 if (OC_STACK_OK != checked_guard(m_server, &IServerWrapper::stop))
142 res = OC_STACK_ERROR;
145 else if (OC_CLIENT_SERVER == m_modeType)
147 if (OC_STACK_OK != checked_guard(m_client, &IClientWrapper::stop))
149 res = OC_STACK_ERROR;
152 if (OC_STACK_OK != checked_guard(m_server, &IServerWrapper::stop))
154 res = OC_STACK_ERROR;
159 res = OC_STACK_ERROR;
165 void OCPlatform_impl::init(const PlatformConfig& config)
167 OIC_LOG(INFO, TAG, "init");
171 case ModeType::Server:
172 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
173 m_modeType = OC_SERVER;
176 case ModeType::Client:
177 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
178 m_modeType = OC_CLIENT;
182 case ModeType::Gateway:
183 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
184 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
185 m_modeType = OC_CLIENT_SERVER;
190 OCPlatform_impl::OCPlatform_impl(const PlatformConfig& config)
192 m_WrapperInstance { make_unique<WrapperFactory>() },
193 m_csdkLock { std::make_shared<std::recursive_mutex>() }
198 OCPlatform_impl::~OCPlatform_impl(void)
202 OCStackResult OCPlatform_impl::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
204 return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
208 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle,
209 QualityOfService QoS)
211 return checked_guard(m_server, &IServerWrapper::notifyAllObservers, resourceHandle, QoS);
214 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle)
216 return notifyAllObservers(resourceHandle, m_cfg.QoS);
219 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
220 ObservationIds& observationIds,
221 const std::shared_ptr<OCResourceResponse> pResponse)
223 return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
226 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
227 ObservationIds& observationIds,
228 const std::shared_ptr<OCResourceResponse> pResponse,
229 QualityOfService QoS)
231 return checked_guard(m_server, &IServerWrapper::notifyListOfObservers, resourceHandle,
232 observationIds, pResponse, QoS);
235 OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
236 const std::string& uri,
237 OCConnectivityType connectivityType,
239 const std::vector<std::string>& resourceTypes,
240 const std::vector<std::string>& interfaces)
244 return std::shared_ptr<OCResource>();
247 uint8_t resourceProperty = 0;
250 resourceProperty = (resourceProperty | OC_OBSERVABLE);
252 return std::shared_ptr<OCResource>(new OCResource(m_client,
254 uri, "", connectivityType,
260 OCStackResult OCPlatform_impl::findResource(const std::string& host,
261 const std::string& resourceName,
262 OCConnectivityType connectivityType,
263 FindCallback resourceHandler)
265 return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
268 OCStackResult OCPlatform_impl::findResource(const std::string& host,
269 const std::string& resourceName,
270 OCConnectivityType connectivityType,
271 FindCallback resourceHandler,
272 QualityOfService QoS)
274 return checked_guard(m_client, &IClientWrapper::ListenForResource,
275 host, resourceName, connectivityType, resourceHandler, QoS);
278 OCStackResult OCPlatform_impl::findResource(const std::string& host,
279 const std::string& resourceName,
280 OCConnectivityType connectivityType,
281 FindCallback resourceHandler,
282 FindErrorCallback errorHandler)
284 return findResource(host, resourceName, connectivityType, resourceHandler,
285 errorHandler, m_cfg.QoS);
288 OCStackResult OCPlatform_impl::findResource(const std::string& host,
289 const std::string& resourceName,
290 OCConnectivityType connectivityType,
291 FindCallback resourceHandler,
292 FindErrorCallback errorHandler,
293 QualityOfService QoS)
295 return checked_guard(m_client, &IClientWrapper::ListenErrorForResource,
296 host, resourceName, connectivityType, resourceHandler,
300 OCStackResult OCPlatform_impl::findResourceList(const std::string& host,
301 const std::string& resourceName,
302 OCConnectivityType connectivityType,
303 FindResListCallback resourceHandler,
304 QualityOfService QoS)
306 return checked_guard(m_client, &IClientWrapper::ListenForResourceList,
307 host, resourceName, connectivityType, resourceHandler, QoS);
310 OCStackResult OCPlatform_impl::findResourceList(const std::string& host,
311 const std::string& resourceName,
312 OCConnectivityType connectivityType,
313 FindResListCallback resourceHandler,
314 FindErrorCallback errorHandler,
315 QualityOfService Qos)
317 return checked_guard(m_client, &IClientWrapper::ListenForResourceListWithError,
318 host, resourceName, connectivityType, resourceHandler,
322 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
323 const std::string& deviceURI,
324 OCConnectivityType connectivityType,
325 FindDeviceCallback deviceInfoHandler)
327 return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
328 deviceInfoHandler, m_cfg.QoS));
331 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
332 const std::string& deviceURI,
333 OCConnectivityType connectivityType,
334 FindDeviceCallback deviceInfoHandler,
335 QualityOfService QoS)
337 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
338 host, deviceURI, connectivityType, deviceInfoHandler, QoS);
341 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
342 const std::string& platformURI,
343 OCConnectivityType connectivityType,
344 FindPlatformCallback platformInfoHandler)
346 return result_guard(getPlatformInfo(host, platformURI, connectivityType,
347 platformInfoHandler, m_cfg.QoS));
350 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
351 const std::string& platformURI,
352 OCConnectivityType connectivityType,
353 FindPlatformCallback platformInfoHandler,
354 QualityOfService QoS)
356 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
357 host, platformURI, connectivityType, platformInfoHandler, QoS);
360 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
361 std::string& resourceURI,
362 const std::string& resourceTypeName,
363 const std::string& resourceInterface,
364 EntityHandler entityHandler,
365 uint8_t resourceProperty)
367 return checked_guard(m_server, &IServerWrapper::registerResource,
368 std::ref(resourceHandle), resourceURI, resourceTypeName,
369 resourceInterface, entityHandler, resourceProperty);
372 OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
374 return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
377 OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
379 return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
382 OCStackResult OCPlatform_impl::setPropertyValue(OCPayloadType type, const std::string& tag, const std::string& value)
385 return checked_guard(m_server, &IServerWrapper::setPropertyValue, type, tag, value);
388 OCStackResult OCPlatform_impl::setPropertyValue(OCPayloadType type, const std::string& tag, const std::vector<std::string>& value)
390 for (const auto& h : value)
394 if (OC_STACK_OK != (r = result_guard(setPropertyValue(type, tag, h))))
403 OCStackResult OCPlatform_impl::getPropertyValue(OCPayloadType type, const std::string& tag, std::string& value)
405 return checked_guard(m_server, &IServerWrapper::getPropertyValue, type, tag, value);
408 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
409 const std::shared_ptr< OCResource > resource)
411 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
412 std::vector<std::string> resourceTypes = resource->getResourceTypes();
414 return checked_guard(m_server, &IServerWrapper::registerResource,
415 std::ref(resourceHandle), resource->host() + resource->uri(),
416 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
417 (EntityHandler) nullptr, resourceProperty);
420 OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
422 return checked_guard(m_server, &IServerWrapper::unregisterResource,
426 OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
427 OCResourceHandle resourceHandle)
429 return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
432 OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
433 const std::vector<OCResourceHandle>& resourceHandles)
435 for(const auto& h : resourceHandles)
439 if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
448 OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
449 const OCResourceHandle resourceHandle)
451 return result_guard(OCBindResource(collectionHandle, resourceHandle));
454 OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
455 const std::vector<OCResourceHandle>& resourceHandles)
457 for(const auto& h : resourceHandles)
461 if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
470 OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
471 const std::string& resourceTypeName) const
473 return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
474 resourceHandle, resourceTypeName);
477 OCStackResult OCPlatform_impl::resetResourceTypes(const OCResourceHandle& resourceHandle,
478 const std::string& newResourceType) const
480 return checked_guard(m_server, &IServerWrapper::resetResourceTypes,
481 resourceHandle, newResourceType);
484 OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
485 const std::string& resourceInterfaceName) const
487 return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
488 resourceHandle, resourceInterfaceName);
491 OCStackResult OCPlatform_impl::resetResourceInterfaces(const OCResourceHandle& resourceHandle,
492 const std::string& newResourceInterface) const
494 return checked_guard(m_server, &IServerWrapper::resetResourceInterfaces,
495 resourceHandle, newResourceInterface);
498 OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
500 return checked_guard(m_server, &IServerWrapper::startPresence,
501 announceDurationSeconds);
504 OCStackResult OCPlatform_impl::stopPresence()
506 return checked_guard(m_server, &IServerWrapper::stopPresence);
509 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
510 const std::string& host,
511 OCConnectivityType connectivityType,
512 SubscribeCallback presenceHandler)
514 return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
518 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
519 const std::string& host,
520 const std::string& resourceType,
521 OCConnectivityType connectivityType,
522 SubscribeCallback presenceHandler)
524 return checked_guard(m_client, &IClientWrapper::SubscribePresence,
525 &presenceHandle, host, resourceType, connectivityType,
529 OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
531 return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
532 std::ref(presenceHandle));
536 OCStackResult OCPlatform_impl::subscribeDevicePresence(OCPresenceHandle& presenceHandle,
537 const std::string& host,
538 const std::vector<std::string>& di,
539 OCConnectivityType connectivityType,
540 ObserveCallback callback)
542 return checked_guard(m_client, &IClientWrapper::SubscribeDevicePresence,
543 &presenceHandle, host, di, connectivityType, callback);
547 OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
549 return checked_guard(m_server, &IServerWrapper::sendResponse,
552 std::weak_ptr<std::recursive_mutex> OCPlatform_impl::csdkLock()
557 OCStackResult OCPlatform_impl::findDirectPairingDevices(unsigned short waittime,
558 GetDirectPairedCallback directPairingHandler)
560 return checked_guard(m_client, &IClientWrapper::FindDirectPairingDevices,
561 waittime, directPairingHandler);
565 OCStackResult OCPlatform_impl::getDirectPairedDevices(
566 GetDirectPairedCallback directPairingHandler)
569 return checked_guard(m_client, &IClientWrapper::GetDirectPairedDevices,
570 directPairingHandler);
573 OCStackResult OCPlatform_impl::doDirectPairing(std::shared_ptr<OCDirectPairing> peer,
575 const std::string& pinNumber,
576 DirectPairingCallback resultCallback)
578 return checked_guard(m_client, &IClientWrapper::DoDirectPairing,
579 peer, pmSel, pinNumber, resultCallback);
582 OCAccountManager::Ptr OCPlatform_impl::constructAccountManagerObject(const std::string& host,
583 OCConnectivityType connectivityType)
587 return std::shared_ptr<OCAccountManager>();
590 return std::shared_ptr<OCAccountManager>(new OCAccountManager(m_client,
596 OCStackResult OCPlatform_impl::findKeepAliveResource(std::string host, KeepAliveCallback resultCallback)
598 return checked_guard(m_client, &IClientWrapper::findKeepAliveResource, host, resultCallback);
601 OCStackResult OCPlatform_impl::sendKeepAliveRequest(std::string host, const OCRepresentation& rep,
602 KeepAliveCallback resultCallback)
604 return checked_guard(m_client, &IClientWrapper::sendKeepAliveRequest, host,
605 rep, resultCallback);
608 OCStackResult OCPlatform_impl::getDeviceId(OCUUIdentity *myUuid)
610 return OCGetDeviceId(myUuid);
613 OCStackResult OCPlatform_impl::setDeviceId(const OCUUIdentity *myUuid)
615 return OCSetDeviceId(myUuid);