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::getDeviceInfo(const std::string& host,
213 const std::string& deviceURI,
214 OCConnectivityType connectivityType,
215 FindDeviceCallback deviceInfoHandler)
217 return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
218 deviceInfoHandler, m_cfg.QoS));
221 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
222 const std::string& deviceURI,
223 OCConnectivityType connectivityType,
224 FindDeviceCallback deviceInfoHandler,
225 QualityOfService QoS)
227 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
228 host, deviceURI, connectivityType, deviceInfoHandler, QoS);
231 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
232 const std::string& platformURI,
233 OCConnectivityType connectivityType,
234 FindPlatformCallback platformInfoHandler)
236 return result_guard(getPlatformInfo(host, platformURI, connectivityType,
237 platformInfoHandler, m_cfg.QoS));
240 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
241 const std::string& platformURI,
242 OCConnectivityType connectivityType,
243 FindPlatformCallback platformInfoHandler,
244 QualityOfService QoS)
246 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
247 host, platformURI, connectivityType, platformInfoHandler, QoS);
250 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
251 std::string& resourceURI,
252 const std::string& resourceTypeName,
253 const std::string& resourceInterface,
254 EntityHandler entityHandler,
255 uint8_t resourceProperty)
257 return checked_guard(m_server, &IServerWrapper::registerResource,
258 std::ref(resourceHandle), resourceURI, resourceTypeName,
259 resourceInterface, entityHandler, resourceProperty);
262 OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
264 return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
267 OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
269 return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
272 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
273 const std::shared_ptr< OCResource > resource)
275 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
276 std::vector<std::string> resourceTypes = resource->getResourceTypes();
278 return checked_guard(m_server, &IServerWrapper::registerResource,
279 std::ref(resourceHandle), resource->host() + resource->uri(),
280 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
281 (EntityHandler) nullptr, resourceProperty);
284 OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
286 return checked_guard(m_server, &IServerWrapper::unregisterResource,
290 OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
291 OCResourceHandle resourceHandle)
293 return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
296 OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
297 const std::vector<OCResourceHandle>& resourceHandles)
299 for(const auto& h : resourceHandles)
303 if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
312 OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
313 const OCResourceHandle resourceHandle)
315 return result_guard(OCBindResource(collectionHandle, resourceHandle));
318 OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
319 const std::vector<OCResourceHandle>& resourceHandles)
321 for(const auto& h : resourceHandles)
325 if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
334 OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
335 const std::string& resourceTypeName) const
337 return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
338 resourceHandle, resourceTypeName);
341 OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
342 const std::string& resourceInterfaceName) const
344 return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
345 resourceHandle, resourceInterfaceName);
348 OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
350 return checked_guard(m_server, &IServerWrapper::startPresence,
351 announceDurationSeconds);
354 OCStackResult OCPlatform_impl::stopPresence()
356 return checked_guard(m_server, &IServerWrapper::stopPresence);
359 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
360 const std::string& host,
361 OCConnectivityType connectivityType,
362 SubscribeCallback presenceHandler)
364 return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
368 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
369 const std::string& host,
370 const std::string& resourceType,
371 OCConnectivityType connectivityType,
372 SubscribeCallback presenceHandler)
374 return checked_guard(m_client, &IClientWrapper::SubscribePresence,
375 &presenceHandle, host, resourceType, connectivityType,
379 OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
381 return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
382 std::ref(presenceHandle));
386 OCStackResult OCPlatform_impl::subscribeDevicePresence(OCPresenceHandle& presenceHandle,
387 const std::string& host,
388 const QueryParamsList& queryParams,
389 OCConnectivityType connectivityType,
390 ObserveCallback callback)
392 return checked_guard(m_client, &IClientWrapper::SubscribeDevicePresence,
393 &presenceHandle, host, queryParams, connectivityType, callback);
397 OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
399 return checked_guard(m_server, &IServerWrapper::sendResponse,
403 std::weak_ptr<std::recursive_mutex> OCPlatform_impl::csdkLock()
408 OCStackResult OCPlatform_impl::findDirectPairingDevices(unsigned short waittime,
409 GetDirectPairedCallback directPairingHandler)
411 return checked_guard(m_client, &IClientWrapper::FindDirectPairingDevices,
412 waittime, directPairingHandler);
416 OCStackResult OCPlatform_impl::getDirectPairedDevices(
417 GetDirectPairedCallback directPairingHandler)
420 return checked_guard(m_client, &IClientWrapper::GetDirectPairedDevices,
421 directPairingHandler);
424 OCStackResult OCPlatform_impl::doDirectPairing(std::shared_ptr<OCDirectPairing> peer,
426 const std::string& pinNumber,
427 DirectPairingCallback resultCallback)
429 return checked_guard(m_client, &IClientWrapper::DoDirectPairing,
430 peer, pmSel, pinNumber, resultCallback);
433 OCStackResult OCPlatform_impl::signUp(const std::string& host,
434 const std::string& authProvider,
435 const std::string& authCode,
436 OCConnectivityType connectivityType,
437 PostCallback cloudConnectHandler)
439 const char* di = OCGetServerInstanceIDString();
442 oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
443 return result_guard(OC_STACK_ERROR);
445 std::string deviceId(di);
447 OCRepresentation rep;
448 rep.setValue(OC_RSRVD_DEVICE_ID, deviceId);
449 rep.setValue(OC_RSRVD_AUTHPROVIDER, authProvider);
450 rep.setValue(OC_RSRVD_AUTHCODE, authCode);
452 std::string uri = host + OC_RSRVD_ACCOUNT_URI;
455 QueryParamsMap queryParams;
456 HeaderOptions headerOptions;
458 QualityOfService defaultQos = OC::QualityOfService::NaQos;
459 checked_guard(m_client, &IClientWrapper::GetDefaultQos, defaultQos);
461 return checked_guard(m_client, &IClientWrapper::PostResourceRepresentation,
462 devAddr, uri, rep, queryParams, headerOptions,
463 connectivityType, cloudConnectHandler, defaultQos);
466 OCStackResult OCPlatform_impl::signIn(const std::string& host,
467 const std::string& accessToken,
468 OCConnectivityType connectivityType,
469 PostCallback cloudConnectHandler)
471 return signInOut(host, accessToken, true, connectivityType, cloudConnectHandler);
474 OCStackResult OCPlatform_impl::signOut(const std::string& host,
475 const std::string& accessToken,
476 OCConnectivityType connectivityType,
477 PostCallback cloudConnectHandler)
479 return signInOut(host, accessToken, false, connectivityType, cloudConnectHandler);
482 OCStackResult OCPlatform_impl::signInOut(const std::string& host,
483 const std::string& accessToken,
485 OCConnectivityType connectivityType,
486 PostCallback cloudConnectHandler)
488 const char* di = OCGetServerInstanceIDString();
491 oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
492 return result_guard(OC_STACK_ERROR);
494 std::string deviceId(di);
496 OCRepresentation rep;
497 rep.setValue(OC_RSRVD_DEVICE_ID, deviceId);
498 rep.setValue(OC_RSRVD_ACCESS_TOKEN, accessToken);
499 rep.setValue(OC_RSRVD_STATUS, isSignIn);
501 std::string uri = host + OC_RSRVD_ACCOUNT_SESSION_URI;
504 QueryParamsMap queryParams;
505 HeaderOptions headerOptions;
507 QualityOfService defaultQos = OC::QualityOfService::NaQos;
508 checked_guard(m_client, &IClientWrapper::GetDefaultQos, defaultQos);
510 return checked_guard(m_client, &IClientWrapper::PostResourceRepresentation,
511 devAddr, uri, rep, queryParams, headerOptions,
512 connectivityType, cloudConnectHandler, defaultQos);
515 OCStackResult OCPlatform_impl::refreshAccessToken(const std::string& host,
516 const std::string& refreshToken,
517 OCConnectivityType connectivityType,
518 PostCallback cloudConnectHandler)
520 const char* di = OCGetServerInstanceIDString();
523 oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
524 return result_guard(OC_STACK_ERROR);
526 std::string deviceId(di);
528 OCRepresentation rep;
529 rep.setValue(OC_RSRVD_DEVICE_ID, deviceId);
530 rep.setValue(OC_RSRVD_REFRESH_TOKEN, refreshToken);
531 rep.setValue(OC_RSRVD_GRANT_TYPE, OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN);
533 std::string uri = host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
536 QueryParamsMap queryParams;
537 HeaderOptions headerOptions;
539 QualityOfService defaultQos = OC::QualityOfService::NaQos;
540 checked_guard(m_client, &IClientWrapper::GetDefaultQos, defaultQos);
542 return checked_guard(m_client, &IClientWrapper::PostResourceRepresentation,
543 devAddr, uri, rep, queryParams, headerOptions,
544 connectivityType, cloudConnectHandler, defaultQos);