//
//*********************************************************************
+#include "OCPlatform_impl.h"
+
#include <random>
#include <utility>
#include <functional>
#include "OCApi.h"
#include "OCException.h"
#include "OCUtilities.h"
+#include "ocpayload.h"
#include "oc_logger.hpp"
void OCPlatform_impl::Configure(const PlatformConfig& config)
{
+ OCRegisterPersistentStorageHandler(config.ps);
globalConfig() = config;
}
break;
case ModeType::Both:
+ case ModeType::Gateway:
m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
break;
OCPlatform_impl::OCPlatform_impl(const PlatformConfig& config)
: m_cfg { config },
m_WrapperInstance { make_unique<WrapperFactory>() },
- m_csdkLock { make_shared<std::recursive_mutex>() }
+ m_csdkLock { std::make_shared<std::recursive_mutex>() }
{
init(m_cfg);
}
return result_guard(OC_STACK_ERROR);
}
- std::string payload(pResponse->getResourceRepresentation().getJSONRepresentation());
-
- return result_guard(
+ OCRepPayload* pl = pResponse->getResourceRepresentation().getPayload();
+ OCStackResult result =
OCNotifyListOfObservers(resourceHandle,
&observationIds[0], observationIds.size(),
- reinterpret_cast<unsigned char *>(const_cast<char *>(payload.c_str())),
- static_cast<OCQualityOfService>(QoS)));
+ pl,
+ static_cast<OCQualityOfService>(QoS));
+ OCRepPayloadDestroy(pl);
+ return result_guard(result);
}
OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
const std::string& uri,
+ OCConnectivityType connectivityType,
bool isObservable,
const std::vector<std::string>& resourceTypes,
const std::vector<std::string>& interfaces)
return std::shared_ptr<OCResource>();
}
+ uint8_t resourceProperty = 0;
+ if (isObservable)
+ {
+ resourceProperty = (resourceProperty | OC_OBSERVABLE);
+ }
return std::shared_ptr<OCResource>(new OCResource(m_client,
host,
- uri,
- isObservable,
+ uri, "", connectivityType,
+ resourceProperty,
resourceTypes,
interfaces));
}
OCStackResult OCPlatform_impl::findResource(const std::string& host,
const std::string& resourceName,
+ OCConnectivityType connectivityType,
FindCallback resourceHandler)
{
- return findResource(host, resourceName, resourceHandler, m_cfg.QoS);
+ return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
}
OCStackResult OCPlatform_impl::findResource(const std::string& host,
const std::string& resourceName,
- FindCallback resourceHandler, QualityOfService QoS)
+ OCConnectivityType connectivityType,
+ FindCallback resourceHandler,
+ QualityOfService QoS)
{
return checked_guard(m_client, &IClientWrapper::ListenForResource,
- host, resourceName, resourceHandler, QoS);
+ host, resourceName, connectivityType, resourceHandler, QoS);
+ }
+
+ OCStackResult OCPlatform_impl::findResource(const std::string& host,
+ const std::string& resourceName,
+ OCConnectivityType connectivityType,
+ FindCallback resourceHandler,
+ FindErrorCallback errorHandler)
+ {
+ return findResource(host, resourceName, connectivityType, resourceHandler,
+ errorHandler, m_cfg.QoS);
+ }
+
+ OCStackResult OCPlatform_impl::findResource(const std::string& host,
+ const std::string& resourceName,
+ OCConnectivityType connectivityType,
+ FindCallback resourceHandler,
+ FindErrorCallback errorHandler,
+ QualityOfService QoS)
+ {
+ return checked_guard(m_client, &IClientWrapper::ListenErrorForResource,
+ host, resourceName, connectivityType, resourceHandler,
+ errorHandler, QoS);
}
OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
const std::string& deviceURI,
+ OCConnectivityType connectivityType,
FindDeviceCallback deviceInfoHandler)
{
- return result_guard(getDeviceInfo(host, deviceURI, deviceInfoHandler, m_cfg.QoS));
+ return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
+ deviceInfoHandler, m_cfg.QoS));
}
OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
const std::string& deviceURI,
+ OCConnectivityType connectivityType,
FindDeviceCallback deviceInfoHandler,
QualityOfService QoS)
{
return checked_guard(m_client, &IClientWrapper::ListenForDevice,
- host, deviceURI, deviceInfoHandler, QoS);
+ host, deviceURI, connectivityType, deviceInfoHandler, QoS);
+ }
+
+ OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
+ const std::string& platformURI,
+ OCConnectivityType connectivityType,
+ FindPlatformCallback platformInfoHandler)
+ {
+ return result_guard(getPlatformInfo(host, platformURI, connectivityType,
+ platformInfoHandler, m_cfg.QoS));
+ }
+
+ OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
+ const std::string& platformURI,
+ OCConnectivityType connectivityType,
+ FindPlatformCallback platformInfoHandler,
+ QualityOfService QoS)
+ {
+ return checked_guard(m_client, &IClientWrapper::ListenForDevice,
+ host, platformURI, connectivityType, platformInfoHandler, QoS);
}
OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
uint8_t resourceProperty)
{
return checked_guard(m_server, &IServerWrapper::registerResource,
- ref(resourceHandle), resourceURI, resourceTypeName,
+ std::ref(resourceHandle), resourceURI, resourceTypeName,
resourceInterface, entityHandler, resourceProperty);
}
return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
}
+ OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
+ {
+ return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
+ }
+
OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
const std::shared_ptr< OCResource > resource)
{
uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
std::vector<std::string> resourceTypes = resource->getResourceTypes();
- return checked_guard(m_server, &IServerWrapper::registerResourceWithHost,
- ref(resourceHandle), resource->host(), resource->uri(), resourceTypes[0]/*"core.remote"*/, "oc.mi.def",
+ return checked_guard(m_server, &IServerWrapper::registerResource,
+ std::ref(resourceHandle), resource->host() + resource->uri(),
+ resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
(EntityHandler) nullptr, resourceProperty);
}
OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
OCResourceHandle resourceHandle)
{
- return result_guard(OCUnBindResource(ref(collectionHandle), ref(resourceHandle)));
+ return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
}
OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
const std::string& host,
+ OCConnectivityType connectivityType,
SubscribeCallback presenceHandler)
{
- return subscribePresence(presenceHandle, host, "", presenceHandler);
+ return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
}
+
OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
const std::string& host,
const std::string& resourceType,
+ OCConnectivityType connectivityType,
SubscribeCallback presenceHandler)
{
return checked_guard(m_client, &IClientWrapper::SubscribePresence,
- &presenceHandle, host, resourceType, presenceHandler);
+ &presenceHandle, host, resourceType, connectivityType,
+ presenceHandler);
}
OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
{
return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
- ref(presenceHandle));
+ std::ref(presenceHandle));
+ }
+
+#ifdef WITH_CLOUD
+ OCStackResult OCPlatform_impl::subscribeDevicePresence(OCPresenceHandle& presenceHandle,
+ const std::string& host,
+ const QueryParamsList& queryParams,
+ OCConnectivityType connectivityType,
+ ObserveCallback callback)
+ {
+ return checked_guard(m_client, &IClientWrapper::SubscribeDevicePresence,
+ &presenceHandle, host, queryParams, connectivityType, callback);
}
+#endif
OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
{
return checked_guard(m_server, &IServerWrapper::sendResponse,
pResponse);
}
+
+ std::weak_ptr<std::recursive_mutex> OCPlatform_impl::csdkLock()
+ {
+ return m_csdkLock;
+ }
+
+ OCStackResult OCPlatform_impl::findDirectPairingDevices(unsigned short waittime,
+ GetDirectPairedCallback directPairingHandler)
+ {
+ return checked_guard(m_client, &IClientWrapper::FindDirectPairingDevices,
+ waittime, directPairingHandler);
+
+ }
+
+ OCStackResult OCPlatform_impl::getDirectPairedDevices(
+ GetDirectPairedCallback directPairingHandler)
+ {
+
+ return checked_guard(m_client, &IClientWrapper::GetDirectPairedDevices,
+ directPairingHandler);
+ }
+
+ OCStackResult OCPlatform_impl::doDirectPairing(std::shared_ptr<OCDirectPairing> peer,
+ OCPrm_t pmSel,
+ const std::string& pinNumber,
+ DirectPairingCallback resultCallback)
+ {
+ return checked_guard(m_client, &IClientWrapper::DoDirectPairing,
+ peer, pmSel, pinNumber, resultCallback);
+ }
+#ifdef WITH_CLOUD
+ OCStackResult OCPlatform_impl::signUp(const std::string& host,
+ const std::string& authProvider,
+ const std::string& authCode,
+ OCConnectivityType connectivityType,
+ PostCallback cloudConnectHandler)
+ {
+ const char* di = OCGetServerInstanceIDString();
+ if (!di)
+ {
+ oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
+ return result_guard(OC_STACK_ERROR);
+ }
+ std::string deviceId(di);
+
+ OCRepresentation rep;
+ rep.setValue(OC_RSRVD_DEVICE_ID, deviceId);
+ rep.setValue(OC_RSRVD_AUTHPROVIDER, authProvider);
+ rep.setValue(OC_RSRVD_AUTHCODE, authCode);
+
+ std::string uri = host + OC_RSRVD_ACCOUNT_URI;
+
+ OCDevAddr devAddr;
+ QueryParamsMap queryParams;
+ HeaderOptions headerOptions;
+
+ QualityOfService defaultQos = OC::QualityOfService::NaQos;
+ checked_guard(m_client, &IClientWrapper::GetDefaultQos, defaultQos);
+
+ return checked_guard(m_client, &IClientWrapper::PostResourceRepresentation,
+ devAddr, uri, rep, queryParams, headerOptions,
+ connectivityType, cloudConnectHandler, defaultQos);
+ }
+
+ OCStackResult OCPlatform_impl::signIn(const std::string& host,
+ const std::string& accessToken,
+ OCConnectivityType connectivityType,
+ PostCallback cloudConnectHandler)
+ {
+ return signInOut(host, accessToken, true, connectivityType, cloudConnectHandler);
+ }
+
+ OCStackResult OCPlatform_impl::signOut(const std::string& host,
+ const std::string& accessToken,
+ OCConnectivityType connectivityType,
+ PostCallback cloudConnectHandler)
+ {
+ return signInOut(host, accessToken, false, connectivityType, cloudConnectHandler);
+ }
+
+ OCStackResult OCPlatform_impl::signInOut(const std::string& host,
+ const std::string& accessToken,
+ bool isSignIn,
+ OCConnectivityType connectivityType,
+ PostCallback cloudConnectHandler)
+ {
+ const char* di = OCGetServerInstanceIDString();
+ if (!di)
+ {
+ oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
+ return result_guard(OC_STACK_ERROR);
+ }
+ std::string deviceId(di);
+
+ OCRepresentation rep;
+ rep.setValue(OC_RSRVD_DEVICE_ID, deviceId);
+ rep.setValue(OC_RSRVD_ACCESS_TOKEN, accessToken);
+ rep.setValue(OC_RSRVD_STATUS, isSignIn);
+
+ std::string uri = host + OC_RSRVD_ACCOUNT_SESSION_URI;
+
+ OCDevAddr devAddr;
+ QueryParamsMap queryParams;
+ HeaderOptions headerOptions;
+
+ QualityOfService defaultQos = OC::QualityOfService::NaQos;
+ checked_guard(m_client, &IClientWrapper::GetDefaultQos, defaultQos);
+
+ return checked_guard(m_client, &IClientWrapper::PostResourceRepresentation,
+ devAddr, uri, rep, queryParams, headerOptions,
+ connectivityType, cloudConnectHandler, defaultQos);
+ }
+
+ OCStackResult OCPlatform_impl::refreshAccessToken(const std::string& host,
+ const std::string& refreshToken,
+ OCConnectivityType connectivityType,
+ PostCallback cloudConnectHandler)
+ {
+ const char* di = OCGetServerInstanceIDString();
+ if (!di)
+ {
+ oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
+ return result_guard(OC_STACK_ERROR);
+ }
+ std::string deviceId(di);
+
+ OCRepresentation rep;
+ rep.setValue(OC_RSRVD_DEVICE_ID, deviceId);
+ rep.setValue(OC_RSRVD_REFRESH_TOKEN, refreshToken);
+ rep.setValue(OC_RSRVD_GRANT_TYPE, OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN);
+
+ std::string uri = host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
+
+ OCDevAddr devAddr;
+ QueryParamsMap queryParams;
+ HeaderOptions headerOptions;
+
+ QualityOfService defaultQos = OC::QualityOfService::NaQos;
+ checked_guard(m_client, &IClientWrapper::GetDefaultQos, defaultQos);
+
+ return checked_guard(m_client, &IClientWrapper::PostResourceRepresentation,
+ devAddr, uri, rep, queryParams, headerOptions,
+ connectivityType, cloudConnectHandler, defaultQos);
+ }
+#endif // WITH_CLOUD
} //namespace OC
+