Merge "Remove unused openssl-devel dependency" into tizen
[platform/upstream/iotivity.git] / resource / src / InProcClientWrapper.cpp
old mode 100644 (file)
new mode 100755 (executable)
index b4a2af8..132d082
@@ -79,6 +79,15 @@ namespace OC
             if (false == m_threadRun)
             {
                 m_threadRun = true;
+#ifdef WITH_PROCESS_EVENT
+                m_processEvent = oc_event_new();
+                if (!m_processEvent)
+                {
+                    OIC_LOG(INFO, TAG, "oc_event_new failed!");
+                    return OC_STACK_ERROR;
+                }
+                OCRegisterProcessEvent(m_processEvent);
+#endif
                 m_listeningThread = std::thread(&InProcClientWrapper::listeningFunc, this);
             }
         }
@@ -89,16 +98,29 @@ namespace OC
     {
         OIC_LOG(INFO, TAG, "stop ocplatform");
 
-        if (m_threadRun && m_listeningThread.joinable())
-        {
-            m_threadRun = false;
-            m_listeningThread.join();
-        }
-
         // only stop if we are the ones who actually called 'start'.  We are counting
         // on the server to do the stop.
         if (m_cfg.mode == ModeType::Client)
         {
+            if (m_threadRun && m_listeningThread.joinable())
+            {
+                m_threadRun = false;
+#ifdef WITH_PROCESS_EVENT
+                if (m_processEvent)
+                {
+                    oc_event_signal(m_processEvent);
+                }
+#endif
+                m_listeningThread.join();
+            }
+
+#ifdef WITH_PROCESS_EVENT
+            if (m_processEvent)
+            {
+                oc_event_free(m_processEvent);
+                m_processEvent = NULL;
+            }
+#endif
             OCStackResult result = OCStop();
 
             if (OC_STACK_OK != result)
@@ -114,11 +136,18 @@ namespace OC
         while(m_threadRun)
         {
             OCStackResult result;
+#ifdef WITH_PROCESS_EVENT
+            uint32_t nextEventTime;
+#endif
             auto cLock = m_csdkLock.lock();
             if (cLock)
             {
                 std::lock_guard<std::recursive_mutex> lock(*cLock);
+#ifdef WITH_PROCESS_EVENT
+                result = OCProcessEvent(&nextEventTime);
+#else
                 result = OCProcess();
+#endif
             }
             else
             {
@@ -130,8 +159,12 @@ namespace OC
                 // TODO: do something with result if failed?
             }
 
+#ifdef WITH_PROCESS_EVENT
+            oc_event_wait_for(m_processEvent, nextEventTime);
+#else
             // To minimize CPU utilization we may wish to do this with sleep
             std::this_thread::sleep_for(std::chrono::milliseconds(10));
+#endif
         }
     }
 
@@ -1433,6 +1466,7 @@ namespace OC
         return result;
     }
 
+#ifdef WITH_PRESENCE
     OCStackApplicationResult subscribePresenceCallback(void* ctx,
                                                        OCDoHandle /*handle*/,
             OCClientResponse* clientResponse)
@@ -1453,11 +1487,13 @@ namespace OC
 
         return OC_STACK_KEEP_TRANSACTION;
     }
+#endif
 
     OCStackResult InProcClientWrapper::SubscribePresence(OCDoHandle* handle,
         const std::string& host, const std::string& resourceType,
         OCConnectivityType connectivityType, SubscribeCallback& presenceHandler)
     {
+#ifdef WITH_PRESENCE
         if (!presenceHandler)
         {
             return OC_STACK_INVALID_PARAM;
@@ -1491,10 +1527,14 @@ namespace OC
                             os.str().c_str(), nullptr,
                             nullptr, connectivityType,
                             OC_LOW_QOS, &cbdata, NULL, 0);
+#else
+        return OC_STACK_NOT_IMPLEMENTED;
+#endif
     }
 
     OCStackResult InProcClientWrapper::UnsubscribePresence(OCDoHandle handle)
     {
+#ifdef WITH_PRESENCE
         OCStackResult result;
         auto cLock = m_csdkLock.lock();
 
@@ -1509,6 +1549,9 @@ namespace OC
         }
 
         return result;
+#else
+        return OC_STACK_NOT_IMPLEMENTED;
+#endif
     }
 
 #ifdef WITH_CLOUD
@@ -1571,18 +1614,19 @@ namespace OC
             return nullptr;
         }
 
-        OCHeaderOption* options = new OCHeaderOption[headerOptions.size()];
-        int i = 0;
+        OCHeaderOption* options = new OCHeaderOption[headerOptions.size()]();
+
+        size_t numOptions = 0;
         for (auto it=headerOptions.begin(); it != headerOptions.end(); ++it)
         {
-            options[i] = OCHeaderOption();
-            options[i].protocolID = OC_COAP_ID;
-            options[i].optionID = it->getOptionID();
-            options[i].optionLength = it->getOptionData().length() + 1;
-            strncpy((char*)options[i].optionData, it->getOptionData().c_str(),
-                sizeof(options[i].optionLength) -1 );
-            options[i].optionData[sizeof(options[i].optionLength) - 1] = 0;
-            i++;
+            OCStackResult ret = OCSetHeaderOption(options, &numOptions, it->getOptionID(),
+                                    it->getOptionData().c_str(), it->getOptionData().length());
+            if (OC_STACK_OK != ret)
+            {
+                OIC_LOG_V(ERROR, TAG, "Failed to convert vnd header options! (error=%d)", ret);
+                delete[] options;
+                return nullptr;
+            }
         }
 
         return options;
@@ -1812,10 +1856,6 @@ namespace OC
             std::lock_guard<std::recursive_mutex> lock(*cLock);
             OCRepPayload *payload = rep.getPayload();
             result = OCSendKeepAliveRequest (nullptr, host.c_str(), (OCPayload*)payload, &cbdata);
-            if (result != OC_STACK_OK)
-            {
-                OCRepPayloadDestroy(payload);
-            }
         }
         else
         {