Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / src / InProcServerWrapper.cpp
index 0159bb8..f42a18c 100644 (file)
 #include <OCResourceResponse.h>
 #include <ocstack.h>
 #include <OCApi.h>
+#include <ocmalloc.h>
 #include <OCPlatform.h>
 #include <OCUtilities.h>
 
 using namespace std;
 using namespace OC;
 
-std::map <OCResourceHandle, OC::EntityHandler>  entityHandlerMap;
-std::map <OCResourceHandle, std::string> resourceUriMap;
-EntityHandler defaultDeviceEntityHandler = 0;
+namespace OC
+{
+    namespace details
+    {
+        std::mutex serverWrapperLock;
+        std::map <OCResourceHandle, OC::EntityHandler>  entityHandlerMap;
+        std::map <OCResourceHandle, std::string> resourceUriMap;
+        EntityHandler defaultDeviceEntityHandler = 0;
+    }
+}
 
 void formResourceRequest(OCEntityHandlerFlag flag,
                          OCEntityHandlerRequest * entityHandlerRequest,
                          std::shared_ptr<OCResourceRequest> pRequest)
 {
-    pRequest->setRequestHandle(entityHandlerRequest->requestHandle);
-    pRequest->setResourceHandle(entityHandlerRequest->resource);
-
-    if(flag & OC_INIT_FLAG)
+    if(pRequest && entityHandlerRequest)
     {
-        pRequest->setRequestHandlerFlag(OC::RequestHandlerFlag::InitFlag);
+        pRequest->setRequestHandle(entityHandlerRequest->requestHandle);
+        pRequest->setResourceHandle(entityHandlerRequest->resource);
     }
 
     if(flag & OC_REQUEST_FLAG)
@@ -63,9 +69,8 @@ void formResourceRequest(OCEntityHandlerFlag flag,
         {
             if(entityHandlerRequest->query)
             {
-                std::string querystr(reinterpret_cast<char*>(entityHandlerRequest->query));
-
-                OC::Utilities::QueryParamsKeyVal qp = OC::Utilities::getQueryParams(querystr);
+                OC::Utilities::QueryParamsKeyVal qp = OC::Utilities::getQueryParams(
+                        entityHandlerRequest->query);
 
                 if(qp.size() > 0)
                 {
@@ -150,9 +155,15 @@ OCEntityHandlerResult DefaultEntityHandlerWrapper(OCEntityHandlerFlag flag,
 
     pRequest->setResourceUri(std::string(uri));
 
-    if(defaultDeviceEntityHandler)
+    EntityHandler defHandler;
     {
-        result = defaultDeviceEntityHandler(pRequest);
+        std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+        defHandler = OC::details::defaultDeviceEntityHandler;
+    }
+
+    if(defHandler)
+    {
+        result = defHandler(pRequest);
     }
     else
     {
@@ -181,9 +192,15 @@ OCEntityHandlerResult EntityHandlerWrapper(OCEntityHandlerFlag flag,
 
     formResourceRequest(flag, entityHandlerRequest, pRequest);
 
+    std::map <OCResourceHandle, std::string>::iterator resourceUriEntry;
+    std::map <OCResourceHandle, std::string>::iterator resourceUriEnd;
+    {
+        std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+        resourceUriEntry = OC::details::resourceUriMap.find(entityHandlerRequest->resource);
+        resourceUriEnd = OC::details::resourceUriMap.end();
+    }
     // Finding the corresponding URI for a resource handle and set the URI in the request
-    auto resourceUriEntry = resourceUriMap.find(entityHandlerRequest->resource);
-    if(resourceUriEntry != resourceUriMap.end())
+    if(resourceUriEntry != resourceUriEnd)
     {
         pRequest->setResourceUri(resourceUriEntry->second);
     }
@@ -193,10 +210,16 @@ OCEntityHandlerResult EntityHandlerWrapper(OCEntityHandlerFlag flag,
         return OC_EH_ERROR;
     }
 
-    // Finding the corresponding CPP Application entityHandler for a given resource
-    auto entityHandlerEntry = entityHandlerMap.find(entityHandlerRequest->resource);
+    std::map <OCResourceHandle, OC::EntityHandler>::iterator entityHandlerEntry;
+    std::map <OCResourceHandle, OC::EntityHandler>::iterator entityHandlerEnd;
+    {
+        // Finding the corresponding CPP Application entityHandler for a given resource
+        std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+        entityHandlerEntry = OC::details::entityHandlerMap.find(entityHandlerRequest->resource);
+        entityHandlerEnd = OC::details::entityHandlerMap.end();
+    }
 
-    if(entityHandlerEntry != entityHandlerMap.end())
+    if(entityHandlerEntry != entityHandlerEnd)
     {
         // Call CPP Application Entity Handler
         if(entityHandlerEntry->second)
@@ -263,9 +286,11 @@ namespace OC
                 result = OCProcess();
             }
 
-            // ...the value of variable result is simply ignored for now.
             if(OC_STACK_ERROR == result)
-             ;
+            {
+                oclog() << "OCProcess failed with result " << result <<std::flush;
+                // ...the value of variable result is simply ignored for now.
+            }
 
             std::this_thread::sleep_for(std::chrono::milliseconds(10));
         }
@@ -283,6 +308,18 @@ namespace OC
         return result;
     }
 
+     OCStackResult InProcServerWrapper::registerPlatformInfo(const OCPlatformInfo platformInfo)
+    {
+        auto cLock = m_csdkLock.lock();
+        OCStackResult result = OC_STACK_ERROR;
+        if(cLock)
+        {
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            result = OCSetPlatformInfo(platformInfo);
+        }
+        return result;
+    }
+
     OCStackResult InProcServerWrapper::registerResource(
                     OCResourceHandle& resourceHandle,
                     std::string& resourceURI,
@@ -327,8 +364,9 @@ namespace OC
             }
             else
             {
-                entityHandlerMap[resourceHandle] = eHandler;
-                resourceUriMap[resourceHandle] = resourceURI;
+                std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+                OC::details::entityHandlerMap[resourceHandle] = eHandler;
+                OC::details::resourceUriMap[resourceHandle] = resourceURI;
             }
         }
         else
@@ -386,8 +424,9 @@ namespace OC
             }
             else
             {
-                entityHandlerMap[resourceHandle] = eHandler;
-                resourceUriMap[resourceHandle] = resourceURI;
+                std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+                OC::details::entityHandlerMap[resourceHandle] = eHandler;
+                OC::details::resourceUriMap[resourceHandle] = resourceURI;
             }
         }
         else
@@ -403,7 +442,10 @@ namespace OC
     {
         OCStackResult result = OC_STACK_ERROR;
 
-        defaultDeviceEntityHandler = entityHandler;
+        {
+            std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+            OC::details::defaultDeviceEntityHandler = entityHandler;
+        }
 
         if(entityHandler)
         {
@@ -430,7 +472,8 @@ namespace OC
 
             if(result == OC_STACK_OK)
             {
-                resourceUriMap.erase(resourceHandle);
+                std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+                OC::details::resourceUriMap.erase(resourceHandle);
             }
             else
             {
@@ -548,7 +591,15 @@ namespace OC
             response.requestHandle = pResponse->getRequestHandle();
             response.resourceHandle = pResponse->getResourceHandle();
             response.ehResult = pResponse->getResponseResult();
-            response.payload = (unsigned char*) payLoad.c_str();
+
+            response.payload = static_cast<char*>(OCMalloc(payLoad.length() + 1));
+            if(!response.payload)
+            {
+                result = OC_STACK_NO_MEMORY;
+                throw OCException(OC::Exception::NO_MEMORY, OC_STACK_NO_MEMORY);
+            }
+
+            strncpy(response.payload, payLoad.c_str(), payLoad.length()+1);
             response.payloadSize = payLoad.length() + 1;
             response.persistentBufferFlag = 0;
 
@@ -582,6 +633,7 @@ namespace OC
             }
             else
             {
+                OCFree(response.payload);
                 result = OC_STACK_ERROR;
             }
 
@@ -604,3 +656,4 @@ namespace OC
         OCStop();
     }
 }
+