replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / src / OCPlatform_impl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index da48020..d782d6b
 #include "OCUtilities.h"
 #include "ocpayload.h"
 
+#include "logger.h"
 #include "oc_logger.hpp"
 
+#define TAG "OIC_PLATFORM"
+
 namespace OC
 {
 
@@ -55,10 +58,24 @@ namespace OC
         return s_config;
     }
 
-    void OCPlatform_impl::Configure(const PlatformConfig& config)
+    OCStackResult OCPlatform_impl::Configure(const PlatformConfig& config)
     {
-        OCRegisterPersistentStorageHandler(config.ps);
+        OIC_LOG_V(INFO, TAG, "Configure : %d %p", config.transportType, config.ps);
+        OCStackResult res = OC_STACK_OK;
+
+        res = OCSetSecurePSI(config.key, config.ps, config.psEnc, config.psRescue);
+        if (OC_STACK_OK == res && config.psEnc)
+        {
+            OIC_LOG(INFO, TAG, "It uses psEnc for svr db.");
+            res = OCRegisterPersistentStorageHandler(config.psEnc);
+        } else
+        {
+            OIC_LOG(INFO, TAG, "It uses ps for svr db.");
+            res = OCRegisterPersistentStorageHandler(config.ps);
+        }
+
         globalConfig() = config;
+        return res;
     }
 
     OCPlatform_impl& OCPlatform_impl::Instance()
@@ -67,22 +84,105 @@ namespace OC
         return platform;
     }
 
+    OCStackResult OCPlatform_impl::start()
+    {
+        OIC_LOG(INFO, TAG, "start");
+
+        OCStackResult res = OC_STACK_OK;
+        if (OC_CLIENT == m_modeType)
+        {
+            if (OC_STACK_OK != checked_guard(m_client, &IClientWrapper::start))
+            {
+                res = OC_STACK_ERROR;
+            }
+        }
+        else if (OC_SERVER == m_modeType)
+        {
+            if (OC_STACK_OK != checked_guard(m_server, &IServerWrapper::start))
+            {
+                res = OC_STACK_ERROR;
+            }
+        }
+        else if (OC_CLIENT_SERVER == m_modeType || OC_GATEWAY == m_modeType)
+        {
+            if (OC_STACK_OK != checked_guard(m_client, &IClientWrapper::start))
+            {
+                res = OC_STACK_ERROR;
+            }
+
+            if (OC_STACK_OK != checked_guard(m_server, &IServerWrapper::start))
+            {
+                res = OC_STACK_ERROR;
+            }
+        }
+        else
+        {
+            res = OC_STACK_ERROR;
+        }
+
+        return res;
+    }
+
+    OCStackResult OCPlatform_impl::stop()
+    {
+        OIC_LOG(INFO, TAG, "stop");
+
+        OCStackResult res = OC_STACK_OK;
+        if (OC_CLIENT == m_modeType)
+        {
+            if (OC_STACK_OK != checked_guard(m_client, &IClientWrapper::stop))
+            {
+                res = OC_STACK_ERROR;
+            }
+        }
+        else if (OC_SERVER == m_modeType)
+        {
+            if (OC_STACK_OK != checked_guard(m_server, &IServerWrapper::stop))
+            {
+                res = OC_STACK_ERROR;
+            }
+        }
+        else if (OC_CLIENT_SERVER == m_modeType)
+        {
+            if (OC_STACK_OK != checked_guard(m_client, &IClientWrapper::stop))
+            {
+                res = OC_STACK_ERROR;
+            }
+
+            if (OC_STACK_OK != checked_guard(m_server, &IServerWrapper::stop))
+            {
+                res = OC_STACK_ERROR;
+            }
+        }
+        else
+        {
+            res = OC_STACK_ERROR;
+        }
+
+        return res;
+    }
+
     void OCPlatform_impl::init(const PlatformConfig& config)
     {
+        OIC_LOG(INFO, TAG, "init");
+
         switch(config.mode)
         {
             case ModeType::Server:
                 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
+                m_modeType = OC_SERVER;
                 break;
 
             case ModeType::Client:
                 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
+                m_modeType = OC_CLIENT;
                 break;
 
             case ModeType::Both:
             case ModeType::Gateway:
                 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
                 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
+                m_modeType = OC_CLIENT_SERVER;
                 break;
          }
     }
@@ -108,8 +208,7 @@ namespace OC
     OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle,
                                                 QualityOfService QoS)
     {
-        return result_guard(OCNotifyAllObservers(resourceHandle,
-                    static_cast<OCQualityOfService>(QoS)));
+        return checked_guard(m_server, &IServerWrapper::notifyAllObservers, resourceHandle, QoS);
     }
 
     OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle)
@@ -129,19 +228,8 @@ namespace OC
                                        const std::shared_ptr<OCResourceResponse> pResponse,
                                        QualityOfService QoS)
     {
-        if(!pResponse)
-        {
-         return result_guard(OC_STACK_ERROR);
-        }
-
-        OCRepPayload* pl = pResponse->getResourceRepresentation().getPayload();
-        OCStackResult result =
-                   OCNotifyListOfObservers(resourceHandle,
-                            &observationIds[0], observationIds.size(),
-                            pl,
-                            static_cast<OCQualityOfService>(QoS));
-        OCRepPayloadDestroy(pl);
-        return result_guard(result);
+        return checked_guard(m_server, &IServerWrapper::notifyListOfObservers, resourceHandle,
+                             observationIds, pResponse, QoS);
     }
 
     OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
@@ -166,7 +254,7 @@ namespace OC
                                             uri, "", connectivityType,
                                             resourceProperty,
                                             resourceTypes,
-                                            interfaces));
+                                            interfaces, ""));
     }
 
     OCStackResult OCPlatform_impl::findResource(const std::string& host,
@@ -209,6 +297,28 @@ namespace OC
                              errorHandler, QoS);
     }
 
+    OCStackResult OCPlatform_impl::findResourceList(const std::string& host,
+                                            const std::string& resourceName,
+                                            OCConnectivityType connectivityType,
+                                            FindResListCallback resourceHandler,
+                                            QualityOfService QoS)
+    {
+        return checked_guard(m_client, &IClientWrapper::ListenForResourceList,
+                             host, resourceName, connectivityType, resourceHandler, QoS);
+    }
+
+    OCStackResult OCPlatform_impl::findResourceList(const std::string& host,
+                                            const std::string& resourceName,
+                                            OCConnectivityType connectivityType,
+                                            FindResListCallback resourceHandler,
+                                            FindErrorCallback errorHandler,
+                                            QualityOfService Qos)
+    {
+        return checked_guard(m_client, &IClientWrapper::ListenForResourceListWithError,
+                             host, resourceName, connectivityType, resourceHandler,
+                             errorHandler, Qos);
+    }
+
     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
                                             const std::string& deviceURI,
                                             OCConnectivityType connectivityType,
@@ -269,6 +379,32 @@ namespace OC
         return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
     }
 
+    OCStackResult OCPlatform_impl::setPropertyValue(OCPayloadType type, const std::string& tag, const std::string& value)
+    {
+
+        return checked_guard(m_server, &IServerWrapper::setPropertyValue, type, tag, value);
+    }
+
+    OCStackResult OCPlatform_impl::setPropertyValue(OCPayloadType type, const std::string& tag, const std::vector<std::string>& value)
+    {
+        for (const auto& h : value)
+        {
+           OCStackResult r;
+
+           if (OC_STACK_OK != (r = result_guard(setPropertyValue(type, tag, h))))
+           {
+               return r;
+           }
+        }
+
+        return OC_STACK_OK;
+    }
+
+    OCStackResult OCPlatform_impl::getPropertyValue(OCPayloadType type, const std::string& tag, std::string& value)
+    {
+        return checked_guard(m_server, &IServerWrapper::getPropertyValue, type, tag, value);
+    }
+
     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
                                             const std::shared_ptr< OCResource > resource)
     {
@@ -382,12 +518,23 @@ namespace OC
                              std::ref(presenceHandle));
     }
 
+#ifdef WITH_CLOUD
+    OCStackResult OCPlatform_impl::subscribeDevicePresence(OCPresenceHandle& presenceHandle,
+                                                           const std::string& host,
+                                                           const std::vector<std::string>& di,
+                                                           OCConnectivityType connectivityType,
+                                                           ObserveCallback callback)
+    {
+        return checked_guard(m_client, &IClientWrapper::SubscribeDevicePresence,
+                             &presenceHandle, host, di, 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;
@@ -418,119 +565,39 @@ namespace OC
                              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)
+    OCAccountManager::Ptr OCPlatform_impl::constructAccountManagerObject(const std::string& host,
+                                                            OCConnectivityType connectivityType)
+    {
+        if (!m_client)
         {
-            oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
-            return result_guard(OC_STACK_ERROR);
+            return std::shared_ptr<OCAccountManager>();
         }
-        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);
+        return std::shared_ptr<OCAccountManager>(new OCAccountManager(m_client,
+                                                                      host,
+                                                                      connectivityType));
     }
-
-    OCStackResult OCPlatform_impl::signIn(const std::string& host,
-                                          const std::string& accessToken,
-                                          OCConnectivityType connectivityType,
-                                          PostCallback cloudConnectHandler)
+#endif // WITH_CLOUD
+#ifdef TCP_ADAPTER
+    OCStackResult OCPlatform_impl::findKeepAliveResource(std::string host, KeepAliveCallback resultCallback)
     {
-        return signInOut(host, accessToken, true, connectivityType, cloudConnectHandler);
+        return checked_guard(m_client, &IClientWrapper::findKeepAliveResource, host, resultCallback);
     }
 
-    OCStackResult OCPlatform_impl::signOut(const std::string& host,
-                                           const std::string& accessToken,
-                                           OCConnectivityType connectivityType,
-                                           PostCallback cloudConnectHandler)
+    OCStackResult OCPlatform_impl::sendKeepAliveRequest(std::string host, const OCRepresentation& rep,
+                                                        KeepAliveCallback resultCallback)
     {
-        return signInOut(host, accessToken, false, connectivityType, cloudConnectHandler);
+        return checked_guard(m_client, &IClientWrapper::sendKeepAliveRequest, host,
+                             rep, resultCallback);
     }
-
-    OCStackResult OCPlatform_impl::signInOut(const std::string& host,
-                                             const std::string& accessToken,
-                                             bool isSignIn,
-                                             OCConnectivityType connectivityType,
-                                             PostCallback cloudConnectHandler)
+#endif
+    OCStackResult OCPlatform_impl::getDeviceId(OCUUIdentity *myUuid)
     {
-        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);
+        return OCGetDeviceId(myUuid);
     }
 
-    OCStackResult OCPlatform_impl::refreshAccessToken(const std::string& host,
-                                                      const std::string& refreshToken,
-                                                      OCConnectivityType connectivityType,
-                                                      PostCallback cloudConnectHandler)
+    OCStackResult OCPlatform_impl::setDeviceId(const OCUUIdentity *myUuid)
     {
-        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);
+        return OCSetDeviceId(myUuid);
     }
-#endif // WITH_CLOUD
 } //namespace OC
-