Change logging method
[platform/upstream/iotivity.git] / resource / src / InProcServerWrapper.cpp
index bd7639d..6bc0bae 100644 (file)
 #include <OCResourceRequest.h>
 #include <OCResourceResponse.h>
 #include <ocstack.h>
+#include <ocpayload.h>
+
 #include <OCApi.h>
 #include <oic_malloc.h>
 #include <OCPlatform.h>
 #include <OCUtilities.h>
+#include "logger.h"
 
-#ifdef RD_CLIENT
-#include <oicresourcedirectory.h>
-#endif
+#define TAG "OIC_SERVER_WRAPPER"
 
 using namespace std;
 using namespace OC;
@@ -86,16 +87,17 @@ void formResourceRequest(OCEntityHandlerFlag flag,
             {
                 //Set the header options here.
                 uint16_t optionID;
-                std::string optionData;
+                char optionData[MAX_HEADER_OPTION_DATA_LENGTH + 1];
                 HeaderOptions headerOptions;
 
+                optionData[MAX_HEADER_OPTION_DATA_LENGTH] = '\0';
                 for(int i = 0;
                     i < entityHandlerRequest->numRcvdVendorSpecificHeaderOptions;
                     i++)
                 {
                     optionID = entityHandlerRequest->rcvdVendorSpecificHeaderOptions[i].optionID;
-                    optionData = reinterpret_cast<const char*>
-                             (entityHandlerRequest->rcvdVendorSpecificHeaderOptions[i].optionData);
+                    memcpy(optionData, entityHandlerRequest->rcvdVendorSpecificHeaderOptions[i].optionData,
+                        MAX_HEADER_OPTION_DATA_LENGTH);
                     HeaderOption::OCHeaderOption headerOption(optionID, optionData);
                     headerOptions.push_back(headerOption);
                 }
@@ -256,19 +258,27 @@ namespace OC
 {
     InProcServerWrapper::InProcServerWrapper(
         std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg)
-     : m_csdkLock(csdkLock)
+     : m_threadRun(false),
+       m_csdkLock(csdkLock),
+       m_cfg { cfg }       
     {
-        OCMode initType;
+        start();
+    }
 
-        if(cfg.mode == ModeType::Server)
+    OCStackResult InProcServerWrapper::start()
+    {
+        OIC_LOG_V(INFO, TAG, "start ocplatform for server : %d", m_cfg.transportType);
+
+        OCMode initType;
+        if(m_cfg.mode == ModeType::Server)
         {
             initType = OC_SERVER;
         }
-        else if (cfg.mode == ModeType::Both)
+        else if (m_cfg.mode == ModeType::Both)
         {
             initType = OC_CLIENT_SERVER;
         }
-        else if (cfg.mode == ModeType::Gateway)
+        else if (m_cfg.mode == ModeType::Gateway)
         {
             initType = OC_GATEWAY;
         }
@@ -279,18 +289,43 @@ namespace OC
         }
 
         OCTransportFlags serverFlags =
-                            static_cast<OCTransportFlags>(cfg.serverConnectivity & CT_MASK_FLAGS);
+                            static_cast<OCTransportFlags>(m_cfg.serverConnectivity & CT_MASK_FLAGS);
         OCTransportFlags clientFlags =
-                            static_cast<OCTransportFlags>(cfg.clientConnectivity & CT_MASK_FLAGS);
-        OCStackResult result = OCInit1(initType, serverFlags, clientFlags);
+                            static_cast<OCTransportFlags>(m_cfg.clientConnectivity & CT_MASK_FLAGS);
+        OCStackResult result = OCInit2(initType, serverFlags, clientFlags,
+                                       m_cfg.transportType);
 
         if(OC_STACK_OK != result)
         {
             throw InitializeException(OC::InitException::STACK_INIT_ERROR, result);
         }
 
-        m_threadRun = true;
-        m_processThread = std::thread(&InProcServerWrapper::processFunc, this);
+        if (false == m_threadRun)
+        {
+            m_threadRun = true;
+            m_processThread = std::thread(&InProcServerWrapper::processFunc, this);
+        }
+        return OC_STACK_OK;
+    }
+
+    OCStackResult InProcServerWrapper::stop()
+    {
+        OIC_LOG(INFO, TAG, "stop");
+
+        if(m_processThread.joinable())
+        {
+            m_threadRun = false;
+            m_processThread.join();
+        }
+
+        OCStackResult res = OCStop();
+
+        if (OC_STACK_OK != res)
+        {
+           throw InitializeException(OC::InitException::STACK_TERMINATE_ERROR, res);
+        }
+
+        return OC_STACK_OK;
     }
 
     void InProcServerWrapper::processFunc()
@@ -327,7 +362,7 @@ namespace OC
         return result;
     }
 
-     OCStackResult InProcServerWrapper::registerPlatformInfo(const OCPlatformInfo platformInfo)
+    OCStackResult InProcServerWrapper::registerPlatformInfo(const OCPlatformInfo platformInfo)
     {
         auto cLock = m_csdkLock.lock();
         OCStackResult result = OC_STACK_ERROR;
@@ -339,6 +374,32 @@ namespace OC
         return result;
     }
 
+    OCStackResult InProcServerWrapper::setPropertyValue(OCPayloadType type, const std::string& propName,
+        const std::string& propValue)
+    {
+        auto cLock = m_csdkLock.lock();
+        OCStackResult result = OC_STACK_ERROR;
+        if (cLock)
+        {
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            result = OCSetPropertyValue(type, propName.c_str(), (void *)propValue.c_str());
+        }
+        return result;
+    }
+
+    OCStackResult InProcServerWrapper::getPropertyValue(OCPayloadType type, const std::string& propName,
+        std::string& propValue)
+    {
+        auto cLock = m_csdkLock.lock();
+        OCStackResult result = OC_STACK_ERROR;
+        if (cLock)
+        {
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            result = OCGetPropertyValue(type, propName.c_str(), (void **)propValue.c_str());
+        }
+        return result;
+    }
+
     OCStackResult InProcServerWrapper::registerResource(
                     OCResourceHandle& resourceHandle,
                     std::string& resourceURI,
@@ -473,6 +534,28 @@ namespace OC
         return result;
     }
 
+    OCStackResult InProcServerWrapper::resetResourceTypes(const OCResourceHandle& resourceHandle,
+                     const std::string& newResourceType)
+    {
+        auto cLock = m_csdkLock.lock();
+        OCStackResult result;
+        if(cLock)
+        {
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            result = OCResetResourceTypes(resourceHandle, newResourceType.c_str());
+        }
+        else
+        {
+            result = OC_STACK_ERROR;
+        }
+
+        if (result != OC_STACK_OK)
+        {
+            throw OCException(OC::Exception::BIND_TYPE_FAILED, result);
+        }
+        return result;
+    }
+
     OCStackResult InProcServerWrapper::bindInterfaceToResource(
                      const OCResourceHandle& resourceHandle,
                      const std::string& resourceInterfaceName)
@@ -497,8 +580,33 @@ namespace OC
         return result;
     }
 
+    OCStackResult InProcServerWrapper::resetResourceInterfaces(
+                     const OCResourceHandle& resourceHandle,
+                     const std::string& newResourceInterface)
+    {
+        auto cLock = m_csdkLock.lock();
+        OCStackResult result;
+        if(cLock)
+        {
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            result = OCResetResourceInterfaces(resourceHandle,
+                        newResourceInterface.c_str());
+        }
+        else
+        {
+            result = OC_STACK_ERROR;
+        }
+
+        if (result != OC_STACK_OK)
+        {
+            throw OCException(OC::Exception::BIND_INTERFACE_FAILED, result);
+        }
+        return result;
+    }
+
     OCStackResult InProcServerWrapper::startPresence(const unsigned int seconds)
     {
+#ifdef WITH_PRESENCE
         auto cLock = m_csdkLock.lock();
         OCStackResult result = OC_STACK_ERROR;
         if(cLock)
@@ -512,10 +620,14 @@ namespace OC
             throw OCException(OC::Exception::START_PRESENCE_FAILED, result);
         }
         return result;
+#else
+        return OC_STACK_NOT_IMPLEMENTED;
+#endif
     }
 
     OCStackResult InProcServerWrapper::stopPresence()
     {
+#ifdef WITH_PRESENCE
         auto cLock = m_csdkLock.lock();
         OCStackResult result = OC_STACK_ERROR;
         if(cLock)
@@ -529,6 +641,9 @@ namespace OC
             throw OCException(OC::Exception::END_PRESENCE_FAILED, result);
         }
         return result;
+#else
+        return OC_STACK_NOT_IMPLEMENTED;
+#endif
     }
 
     OCStackResult InProcServerWrapper::sendResponse(
@@ -588,7 +703,6 @@ namespace OC
             }
             else
             {
-                OICFree(response.payload);
                 result = OC_STACK_ERROR;
             }
 
@@ -596,161 +710,67 @@ namespace OC
             {
                 oclog() << "Error sending response\n";
             }
-            return result;
-        }
-    }
-#ifdef RD_CLIENT
-    OCRepresentation parseRDResponseCallback(OCClientResponse* clientResponse)
-    {
-        if (nullptr == clientResponse->payload ||
-                    PAYLOAD_TYPE_RD != clientResponse->payload->type)
-        {
-            return OCRepresentation();
-        }
-
-        MessageContainer oc;
-        oc.setPayload(clientResponse->payload);
 
-        std::vector<OCRepresentation>::const_iterator it = oc.representations().begin();
-        if (it == oc.representations().end())
-        {
-            return OCRepresentation();
-        }
-
-        // first one is considered the root, everything else is considered a child of this one.
-        OCRepresentation root = *it;
-        root.setDevAddr(clientResponse->devAddr);
-        root.setUri(clientResponse->resourceUri);
-        ++it;
-
-        std::for_each(it, oc.representations().end(),
-                [&root](const OCRepresentation& repItr)
-                {root.addChild(repItr);});
-        return root;
-
-    }
-
-    OCStackApplicationResult publishResourceToRDCallback(void* ctx, OCDoHandle /*handle*/,
-                                                         OCClientResponse* clientResponse)
-    {
-        ServerCallbackContext::PublishContext* context =
-        static_cast<ServerCallbackContext::PublishContext*>(ctx);
-
-        try
-        {
-            // Update resource unique id in stack.
-            if (clientResponse)
-            {
-                if (clientResponse->payload)
-                {
-                    OCRDPayload *rdPayload = (OCRDPayload *) clientResponse->payload;
-                    OCLinksPayload *links = rdPayload->rdPublish->setLinks;
-
-                    while (links)
-                    {
-                        OCResourceHandle handle = OCGetResourceHandleAtUri(links->href);
-                        OCBindResourceInsToResource(handle, links->ins);
-                        links = links->next;
-                    }
-
-                }
-            }
-
-            OCRepresentation rep = parseRDResponseCallback(clientResponse);
-            std::thread exec(context->callback, rep, clientResponse->result);
-            exec.detach();
-        }
-        catch (OC::OCException& e)
-        {
-            oclog() <<"Exception in publishResourceToRDCallback, ignoring response: "
-                <<e.what() <<std::flush;
+            OCPayloadDestroy(response.payload);
+            return result;
         }
-
-        return OC_STACK_KEEP_TRANSACTION;
     }
 
-    OCStackResult InProcServerWrapper::publishResourceToRD(const std::string& host,
-                                                           OCConnectivityType connectivityType,
-                                                           ResourceHandles& resourceHandles,
-                                                           PublishResourceCallback& callback,
-                                                           OCQualityOfService qos)
+    OCStackResult InProcServerWrapper::notifyAllObservers(OCResourceHandle resourceHandle,
+                                                          QualityOfService QoS)
     {
-        ServerCallbackContext::PublishContext* ctx =
-            new ServerCallbackContext::PublishContext(callback);
-        OCCallbackData cbdata(
-                static_cast<void*>(ctx),
-                publishResourceToRDCallback,
-                [](void* c)
-                {delete static_cast<ServerCallbackContext::PublishContext*>(c);}
-                );
-
         auto cLock = m_csdkLock.lock();
         OCStackResult result = OC_STACK_ERROR;
         if (cLock)
         {
             std::lock_guard<std::recursive_mutex> lock(*cLock);
-            result = OCRDPublish(host.c_str(), connectivityType, &resourceHandles[0],
-                                 resourceHandles.size(), &cbdata, qos);
+            result = OCNotifyAllObservers(resourceHandle, static_cast<OCQualityOfService>(QoS));
         }
 
-        if (OC_STACK_OK != result)
-        {
-            throw OCException(OC::Exception::PUBLISH_RESOURCE_FAILED, result);
-        }
         return result;
     }
 
-    OCStackApplicationResult deleteResourceFromRDCallback(void* ctx, OCDoHandle /*handle*/,
-                                                          OCClientResponse* clientResponse)
+    OCStackResult InProcServerWrapper::notifyListOfObservers(OCResourceHandle resourceHandle,
+                                               ObservationIds& observationIds,
+                                               const std::shared_ptr<OCResourceResponse> pResponse,
+                                               QualityOfService QoS)
     {
-        ServerCallbackContext::DeleteContext* context =
-        static_cast<ServerCallbackContext::DeleteContext*>(ctx);
-
-        std::thread exec(context->callback, clientResponse->result);
-        exec.detach();
-        return OC_STACK_DELETE_TRANSACTION;
-    }
-
-    OCStackResult InProcServerWrapper::deleteResourceFromRD(const std::string& host,
-                                                            OCConnectivityType connectivityType,
-                                                            ResourceHandles& resourceHandles,
-                                                            DeleteResourceCallback& callback,
-                                                            OCQualityOfService qos)
-    {
-        ServerCallbackContext::DeleteContext* ctx =
-            new ServerCallbackContext::DeleteContext(callback);
-        OCCallbackData cbdata(
-                static_cast<void*>(ctx),
-                deleteResourceFromRDCallback,
-                [](void* c)
-                {delete static_cast<ServerCallbackContext::DeleteContext*>(c);}
-                );
+        if (!pResponse)
+        {
+            return OC_STACK_ERROR;
+        }
 
         auto cLock = m_csdkLock.lock();
         OCStackResult result = OC_STACK_ERROR;
         if (cLock)
         {
             std::lock_guard<std::recursive_mutex> lock(*cLock);
-            result = OCRDDelete(host.c_str(), connectivityType, &resourceHandles[0],
-                                resourceHandles.size(), &cbdata, qos);
+
+            OCRepPayload* pl = pResponse->getResourceRepresentation().getPayload();
+            result = OCNotifyListOfObservers(resourceHandle,
+                                             &observationIds[0],
+                                             observationIds.size(),
+                                             pl,
+                                             static_cast<OCQualityOfService>(QoS));
+            OCRepPayloadDestroy(pl);
         }
 
-        if (OC_STACK_OK != result)
+        if (result != OC_STACK_OK)
         {
-            throw OCException(OC::Exception::PUBLISH_RESOURCE_FAILED, result);
+            throw OCException(OC::Exception::NOTIFY_LIST_OBSERVERS_FAILED, result);
         }
         return result;
     }
-#endif
+
     InProcServerWrapper::~InProcServerWrapper()
     {
-        if(m_processThread.joinable())
+        try
         {
-            m_threadRun = false;
-            m_processThread.join();
+            stop();
+        }
+        catch (InitializeException &e)
+        {
+            OIC_LOG_V(INFO, TAG, "Exception in stop (%s)", e.what());
         }
-
-        OCStop();
     }
 }
-