replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / src / OCPlatform_impl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 00ddad2..d782d6b
@@ -30,6 +30,8 @@
 //
 //*********************************************************************
 
+#include "OCPlatform_impl.h"
+
 #include <random>
 #include <utility>
 #include <functional>
 #include "OCApi.h"
 #include "OCException.h"
 #include "OCUtilities.h"
+#include "ocpayload.h"
 
+#include "logger.h"
 #include "oc_logger.hpp"
 
+#define TAG "OIC_PLATFORM"
+
 namespace OC
 {
 
@@ -52,9 +58,24 @@ namespace OC
         return s_config;
     }
 
-    void OCPlatform_impl::Configure(const PlatformConfig& config)
+    OCStackResult OCPlatform_impl::Configure(const PlatformConfig& config)
     {
+        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()
@@ -63,21 +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;
          }
     }
@@ -85,7 +190,7 @@ namespace OC
     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);
     }
@@ -103,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)
@@ -124,23 +228,13 @@ namespace OC
                                        const std::shared_ptr<OCResourceResponse> pResponse,
                                        QualityOfService QoS)
     {
-        if(!pResponse)
-        {
-         return result_guard(OC_STACK_ERROR);
-        }
-
-        std::string payload(pResponse->getResourceRepresentation().getJSONRepresentation());
-
-        return result_guard(
-                   OCNotifyListOfObservers(resourceHandle,
-                            &observationIds[0], observationIds.size(),
-                            reinterpret_cast<unsigned char *>(const_cast<char *>(payload.c_str())),
-                            static_cast<OCQualityOfService>(QoS)));
+        return checked_guard(m_server, &IServerWrapper::notifyListOfObservers, resourceHandle,
+                             observationIds, pResponse, QoS);
     }
 
-#ifdef CA_INT
     OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
-                                                const std::string& uri, uint8_t connectivityType,
+                                                const std::string& uri,
+                                                OCConnectivityType connectivityType,
                                                 bool isObservable,
                                                 const std::vector<std::string>& resourceTypes,
                                                 const std::vector<std::string>& interfaces)
@@ -150,17 +244,22 @@ namespace OC
             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, "", connectivityType,
-                                            isObservable,
+                                            resourceProperty,
                                             resourceTypes,
-                                            interfaces));
+                                            interfaces, ""));
     }
 
     OCStackResult OCPlatform_impl::findResource(const std::string& host,
                                             const std::string& resourceName,
-                                            uint8_t connectivityType,
+                                            OCConnectivityType connectivityType,
                                             FindCallback resourceHandler)
     {
         return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
@@ -168,88 +267,95 @@ namespace OC
 
     OCStackResult OCPlatform_impl::findResource(const std::string& host,
                                             const std::string& resourceName,
-                                            uint8_t connectivityType,
-                                            FindCallback resourceHandler, QualityOfService QoS)
+                                            OCConnectivityType connectivityType,
+                                            FindCallback resourceHandler,
+                                            QualityOfService QoS)
     {
-
         return checked_guard(m_client, &IClientWrapper::ListenForResource,
                              host, resourceName, connectivityType, resourceHandler, QoS);
     }
 
-    OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
-                                            const std::string& deviceURI,
-                                            uint8_t connectivityType,
-                                            FindDeviceCallback deviceInfoHandler)
+    OCStackResult OCPlatform_impl::findResource(const std::string& host,
+                                            const std::string& resourceName,
+                                            OCConnectivityType connectivityType,
+                                            FindCallback resourceHandler,
+                                            FindErrorCallback errorHandler)
     {
-        return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
-               deviceInfoHandler, m_cfg.QoS));
+        return findResource(host, resourceName, connectivityType, resourceHandler,
+                            errorHandler, m_cfg.QoS);
     }
 
-    OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
-                                            const std::string& deviceURI,
-                                            uint8_t connectivityType,
-                                            FindDeviceCallback deviceInfoHandler,
+    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::ListenForDevice,
-                             host, deviceURI, connectivityType, deviceInfoHandler, QoS);
-    }
-
-#else
-    OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
-                                                const std::string& uri,
-                                                bool isObservable,
-                                                const std::vector<std::string>& resourceTypes,
-                                                const std::vector<std::string>& interfaces)
-    {
-        if(!m_client)
-        {
-            return std::shared_ptr<OCResource>();
-        }
-
-        return std::shared_ptr<OCResource>(new OCResource(m_client,
-                                            host,
-                                            uri,
-                                            "", // 'created' Resources have no way of knowing their
-                                                // server ID, so this has to be blank initially.
-                                            isObservable,
-                                            resourceTypes,
-                                            interfaces));
+        return checked_guard(m_client, &IClientWrapper::ListenErrorForResource,
+                             host, resourceName, connectivityType, resourceHandler,
+                             errorHandler, QoS);
     }
 
-    OCStackResult OCPlatform_impl::findResource(const std::string& host,
+    OCStackResult OCPlatform_impl::findResourceList(const std::string& host,
                                             const std::string& resourceName,
-                                            FindCallback resourceHandler)
+                                            OCConnectivityType connectivityType,
+                                            FindResListCallback resourceHandler,
+                                            QualityOfService QoS)
     {
-        return findResource(host, resourceName, resourceHandler, m_cfg.QoS);
+        return checked_guard(m_client, &IClientWrapper::ListenForResourceList,
+                             host, resourceName, connectivityType, resourceHandler, QoS);
     }
 
-    OCStackResult OCPlatform_impl::findResource(const std::string& host,
+    OCStackResult OCPlatform_impl::findResourceList(const std::string& host,
                                             const std::string& resourceName,
-                                            FindCallback resourceHandler, QualityOfService QoS)
+                                            OCConnectivityType connectivityType,
+                                            FindResListCallback resourceHandler,
+                                            FindErrorCallback errorHandler,
+                                            QualityOfService Qos)
     {
-
-        return checked_guard(m_client, &IClientWrapper::ListenForResource,
-                             host, resourceName, resourceHandler, 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,
                                             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);
     }
 
-#endif
+    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,
                                             std::string& resourceURI,
@@ -259,7 +365,7 @@ namespace OC
                                             uint8_t resourceProperty)
     {
         return checked_guard(m_server, &IServerWrapper::registerResource,
-                             ref(resourceHandle), resourceURI, resourceTypeName,
+                             std::ref(resourceHandle), resourceURI, resourceTypeName,
                              resourceInterface, entityHandler, resourceProperty);
     }
 
@@ -268,14 +374,46 @@ namespace OC
         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::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)
     {
         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);
     }
 
@@ -288,7 +426,7 @@ namespace OC
     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,
@@ -354,9 +492,9 @@ namespace OC
         return checked_guard(m_server, &IServerWrapper::stopPresence);
     }
 
-#ifdef CA_INT
     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
-                                            const std::string& host, uint8_t connectivityType,
+                                            const std::string& host,
+                                            OCConnectivityType connectivityType,
                                             SubscribeCallback presenceHandler)
     {
         return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
@@ -366,41 +504,100 @@ namespace OC
     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
                                             const std::string& host,
                                             const std::string& resourceType,
-                                            uint8_t connectivityType,
+                                            OCConnectivityType connectivityType,
                                             SubscribeCallback presenceHandler)
     {
         return checked_guard(m_client, &IClientWrapper::SubscribePresence,
                              &presenceHandle, host, resourceType, connectivityType,
                              presenceHandler);
     }
-#else
-    OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
-                                            const std::string& host,
-                                            SubscribeCallback presenceHandler)
-    {
-        return subscribePresence(presenceHandle, host, "", presenceHandler);
-    }
 
-    OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
-                                            const std::string& host,
-                                            const std::string& resourceType,
-                                            SubscribeCallback presenceHandler)
+    OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
     {
-        return checked_guard(m_client, &IClientWrapper::SubscribePresence,
-                             &presenceHandle, host, resourceType,
-                             presenceHandler);
+        return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
+                             std::ref(presenceHandle));
     }
-#endif
 
-    OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle 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::UnsubscribePresence,
-                             ref(presenceHandle));
+        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;
+    }
+
+    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
+    OCAccountManager::Ptr OCPlatform_impl::constructAccountManagerObject(const std::string& host,
+                                                            OCConnectivityType connectivityType)
+    {
+        if (!m_client)
+        {
+            return std::shared_ptr<OCAccountManager>();
+        }
+
+        return std::shared_ptr<OCAccountManager>(new OCAccountManager(m_client,
+                                                                      host,
+                                                                      connectivityType));
+    }
+#endif // WITH_CLOUD
+#ifdef TCP_ADAPTER
+    OCStackResult OCPlatform_impl::findKeepAliveResource(std::string host, KeepAliveCallback resultCallback)
+    {
+        return checked_guard(m_client, &IClientWrapper::findKeepAliveResource, host, resultCallback);
+    }
+
+    OCStackResult OCPlatform_impl::sendKeepAliveRequest(std::string host, const OCRepresentation& rep,
+                                                        KeepAliveCallback resultCallback)
+    {
+        return checked_guard(m_client, &IClientWrapper::sendKeepAliveRequest, host,
+                             rep, resultCallback);
+    }
+#endif
+    OCStackResult OCPlatform_impl::getDeviceId(OCUUIdentity *myUuid)
+    {
+        return OCGetDeviceId(myUuid);
+    }
+
+    OCStackResult OCPlatform_impl::setDeviceId(const OCUUIdentity *myUuid)
+    {
+        return OCSetDeviceId(myUuid);
+    }
 } //namespace OC