Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / src / InProcServerWrapper.cpp
index 0159bb8..522ef82 100644 (file)
 #include <OCResourceResponse.h>
 #include <ocstack.h>
 #include <OCApi.h>
+#include <oic_malloc.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)
                 {
@@ -99,14 +104,12 @@ void formResourceRequest(OCEntityHandlerFlag flag,
             else if(OC_REST_PUT == entityHandlerRequest->method)
             {
                 pRequest->setRequestType(OC::PlatformCommands::PUT);
-                pRequest->setPayload(std::string(reinterpret_cast<const char*>
-                                            (entityHandlerRequest->reqJSONPayload)));
+                pRequest->setPayload(entityHandlerRequest->payload);
             }
             else if(OC_REST_POST == entityHandlerRequest->method)
             {
                 pRequest->setRequestType(OC::PlatformCommands::POST);
-                pRequest->setPayload(std::string(reinterpret_cast<const char*>
-                                            (entityHandlerRequest->reqJSONPayload)));
+                pRequest->setPayload(entityHandlerRequest->payload);
             }
             else if(OC_REST_DELETE == entityHandlerRequest->method)
             {
@@ -125,6 +128,12 @@ void formResourceRequest(OCEntityHandlerFlag flag,
             OC::ObservationInfo observationInfo;
             observationInfo.action = (OC::ObserveAction) entityHandlerRequest->obsInfo.action;
             observationInfo.obsId = entityHandlerRequest->obsInfo.obsId;
+
+            observationInfo.connectivityType = static_cast<OCConnectivityType>(
+                    (entityHandlerRequest->devAddr.adapter << CT_ADAPTER_SHIFT) |
+                    (entityHandlerRequest->devAddr.flags & CT_MASK_FLAGS));
+            observationInfo.address = entityHandlerRequest->devAddr.addr;
+            observationInfo.port = entityHandlerRequest->devAddr.port;
             pRequest->setObservationInfo(observationInfo);
         }
     }
@@ -132,7 +141,8 @@ void formResourceRequest(OCEntityHandlerFlag flag,
 
 OCEntityHandlerResult DefaultEntityHandlerWrapper(OCEntityHandlerFlag flag,
                                                   OCEntityHandlerRequest * entityHandlerRequest,
-                                                  char* uri)
+                                                  char* uri,
+                                                  void * /*callbackParam*/)
 {
     OCEntityHandlerResult result = OC_EH_ERROR;
 
@@ -150,9 +160,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
     {
@@ -165,7 +181,8 @@ OCEntityHandlerResult DefaultEntityHandlerWrapper(OCEntityHandlerFlag flag,
 
 
 OCEntityHandlerResult EntityHandlerWrapper(OCEntityHandlerFlag flag,
-                                           OCEntityHandlerRequest * entityHandlerRequest)
+                                           OCEntityHandlerRequest * entityHandlerRequest,
+                                           void* /*callbackParam*/)
 {
     OCEntityHandlerResult result = OC_EH_ERROR;
 
@@ -181,9 +198,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 +216,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)
@@ -234,13 +263,21 @@ namespace OC
         {
             initType = OC_CLIENT_SERVER;
         }
+        else if (cfg.mode == ModeType::Gateway)
+        {
+            initType = OC_GATEWAY;
+        }
         else
         {
             throw InitializeException(OC::InitException::NOT_CONFIGURED_AS_SERVER,
-                                      OC_STACK_INVALID_PARAM);
+                                         OC_STACK_INVALID_PARAM);
         }
 
-        OCStackResult result = OCInit(cfg.ipAddress.c_str(), cfg.port, initType);
+        OCTransportFlags serverFlags =
+                            static_cast<OCTransportFlags>(cfg.serverConnectivity & CT_MASK_FLAGS);
+        OCTransportFlags clientFlags =
+                            static_cast<OCTransportFlags>(cfg.clientConnectivity & CT_MASK_FLAGS);
+        OCStackResult result = OCInit1(initType, serverFlags, clientFlags);
 
         if(OC_STACK_OK != result)
         {
@@ -263,9 +300,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 +322,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,
@@ -307,6 +358,7 @@ namespace OC
                             resourceInterface.c_str(), //const char * resourceInterfaceName //TODO fix this
                             resourceURI.c_str(), // const char * uri
                             EntityHandlerWrapper, // OCEntityHandler entityHandler
+                            NULL,
                             resourceProperties // uint8_t resourceProperties
                             );
             }
@@ -317,6 +369,7 @@ namespace OC
                             resourceInterface.c_str(), //const char * resourceInterfaceName //TODO fix this
                             resourceURI.c_str(), // const char * uri
                             NULL, // OCEntityHandler entityHandler
+                            NULL,
                             resourceProperties // uint8_t resourceProperties
                             );
             }
@@ -327,8 +380,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
@@ -339,80 +393,26 @@ namespace OC
         return result;
     }
 
-    OCStackResult InProcServerWrapper::registerResourceWithHost(
-                    OCResourceHandle& resourceHandle,
-                    std::string& resourceHOST,
-                    std::string& resourceURI,
-                    const std::string& resourceTypeName,
-                    const std::string& resourceInterface,
-                    EntityHandler& eHandler,
-                    uint8_t resourceProperties)
 
-    {
-        OCStackResult result = OC_STACK_ERROR;
-
-        auto cLock = m_csdkLock.lock();
-
-        if (cLock)
-        {
-            std::lock_guard < std::recursive_mutex > lock(*cLock);
-
-            if (NULL != eHandler)
-            {
-                result = OCCreateResourceWithHost(&resourceHandle, // OCResourceHandle *handle
-                        resourceTypeName.c_str(), // const char * resourceTypeName
-                        resourceInterface.c_str(), //const char * resourceInterfaceName //TODO fix
-                        resourceHOST.c_str(), // const char * host
-                        (resourceHOST + resourceURI).c_str(), // const char * uri
-                        EntityHandlerWrapper, // OCEntityHandler entityHandler
-                        resourceProperties // uint8_t resourceProperties
-                        );
-            }
-            else
-            {
-                result = OCCreateResourceWithHost(&resourceHandle, // OCResourceHandle *handle
-                        resourceTypeName.c_str(), // const char * resourceTypeName
-                        resourceInterface.c_str(), //const char * resourceInterfaceName //TODO fix
-                        resourceHOST.c_str(), // const char * host
-                        (resourceHOST + resourceURI).c_str(), // const char * uri
-                        nullptr, // OCEntityHandler entityHandler
-                        resourceProperties // uint8_t resourceProperties
-                        );
-            }
-
-            if (result != OC_STACK_OK)
-            {
-                resourceHandle = nullptr;
-            }
-            else
-            {
-                entityHandlerMap[resourceHandle] = eHandler;
-                resourceUriMap[resourceHandle] = resourceURI;
-            }
-        }
-        else
-        {
-            result = OC_STACK_ERROR;
-        }
-
-        return result;
-    }
 
     OCStackResult InProcServerWrapper::setDefaultDeviceEntityHandler
                                         (EntityHandler entityHandler)
     {
         OCStackResult result = OC_STACK_ERROR;
 
-        defaultDeviceEntityHandler = entityHandler;
+        {
+            std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+            OC::details::defaultDeviceEntityHandler = entityHandler;
+        }
 
         if(entityHandler)
         {
-            result = OCSetDefaultDeviceEntityHandler(DefaultEntityHandlerWrapper);
+            result = OCSetDefaultDeviceEntityHandler(DefaultEntityHandlerWrapper, NULL);
         }
         else
         {
             // If Null passed we unset
-            result = OCSetDefaultDeviceEntityHandler(NULL);
+            result = OCSetDefaultDeviceEntityHandler(NULL, NULL);
         }
 
         return result;
@@ -430,7 +430,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
             {
@@ -539,17 +540,15 @@ namespace OC
         else
         {
             OCEntityHandlerResponse response;
-            std::string payLoad;
-            HeaderOptions serverHeaderOptions;
-
-            payLoad = pResponse->getPayload();
-            serverHeaderOptions = pResponse->getHeaderOptions();
+//            OCRepPayload* payLoad = pResponse->getPayload();
+            HeaderOptions serverHeaderOptions = pResponse->getHeaderOptions();
 
             response.requestHandle = pResponse->getRequestHandle();
             response.resourceHandle = pResponse->getResourceHandle();
             response.ehResult = pResponse->getResponseResult();
-            response.payload = (unsigned char*) payLoad.c_str();
-            response.payloadSize = payLoad.length() + 1;
+
+            response.payload = reinterpret_cast<OCPayload*>(pResponse->getPayload());
+
             response.persistentBufferFlag = 0;
 
             response.numSendVendorSpecificHeaderOptions = serverHeaderOptions.size();
@@ -561,18 +560,20 @@ namespace OC
                     static_cast<uint16_t>(it->getOptionID());
                 response.sendVendorSpecificHeaderOptions[i].optionLength =
                     (it->getOptionData()).length() + 1;
-                memcpy(response.sendVendorSpecificHeaderOptions[i].optionData,
-                    (it->getOptionData()).c_str(),
-                    (it->getOptionData()).length() + 1);
+                std::string optionData = it->getOptionData();
+                std::copy(optionData.begin(),
+                         optionData.end(),
+                         response.sendVendorSpecificHeaderOptions[i].optionData);
+                response.sendVendorSpecificHeaderOptions[i].optionData[it->getOptionData().length()]
+                    = '\0';
                 i++;
             }
 
             if(OC_EH_RESOURCE_CREATED == response.ehResult)
             {
-                std::string createdUri = pResponse->getNewResourceUri();
-                strncpy(reinterpret_cast<char*>(response.resourceUri),
-                        createdUri.c_str(),
-                        createdUri.length() + 1);
+                pResponse->getNewResourceUri().copy(response.resourceUri,
+                        sizeof (response.resourceUri) - 1);
+                response.resourceUri[pResponse->getNewResourceUri().length()] = '\0';
             }
 
             if(cLock)
@@ -582,6 +583,7 @@ namespace OC
             }
             else
             {
+                OICFree(response.payload);
                 result = OC_STACK_ERROR;
             }
 
@@ -604,3 +606,4 @@ namespace OC
         OCStop();
     }
 }
+