wrap CAcloseSslConnectionFreeEndpoint method with __TIZEN__ flag
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / caleadapter.c
index 6fe1343..01c80b2 100644 (file)
@@ -1487,6 +1487,8 @@ static void CALEClientSendDataThread(void *threadData)
             return;
         }
     }
+#endif
+#if defined(__TIZEN__) || defined(__ANDROID__)
     g_mtuSize = CALEClientGetMtuSize(bleData->remoteEndpoint->addr);
 #endif
     OIC_LOG_V(INFO, CALEADAPTER_TAG, "MTU size [%d]", g_mtuSize);
@@ -2377,6 +2379,7 @@ static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
 static CAResult_t CALEAdapterGattServerStart()
 {
     OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
+    CAResult_t result = CA_STATUS_FAILED;
 
     if (caglobals.bleFlags & CA_LE_SERVER_DISABLE)
     {
@@ -2385,7 +2388,10 @@ static CAResult_t CALEAdapterGattServerStart()
         return CA_STATUS_OK;
     }
 
-    CAResult_t result = CAStartLEGattServer();
+#ifndef DISABLE_BLE_SERVER
+    OIC_LOG_V(INFO, CALEADAPTER_TAG, "Starting LE GATT Server");
+    result = CAStartLEGattServer();
+#endif
 
 #ifndef SINGLE_THREAD
     /*
@@ -3188,8 +3194,54 @@ static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const cha
 #endif
 
 #ifdef __WITH_DTLS__
+#if defined(__TIZEN__) && !defined(SINGLE_THREAD)
+        // CAcloseSslConnection returns CAResult_t instead of void*, but the size is the same and crash shouldn't occur
+        pthread_t ccThread;
+        pthread_attr_t attr;
+        int initAttrRes = -1;
+        int pthreadCreateRes = -1;
+        int detachStatusRes = -1;
+        int memoryAllocationRes = -1;
+
+        do
+        {
+            initAttrRes = pthread_attr_init(&attr);
+            if (initAttrRes != 0)
+            {
+                break;
+            }
+            CAEndpoint_t *localEndpointCpyPtr = OICMalloc(sizeof(CAEndpoint_t));
+
+            if(NULL == localEndpointCpyPtr)
+            {
+                memoryAllocationRes = -1;
+                break;
+            }
+            else
+            {
+                memoryAllocationRes = 0;
+            }
+
+            (*localEndpointCpyPtr) = localEndpoint;
+            // this piece of code is reached on the main thread
+            // CAcloseSslConnection might wait for too long (network + mutexes) and watchdog might kill it
+            // Asynchronous call protects this function from watchdog
+            pthreadCreateRes = pthread_create(&ccThread, &attr, (void *(*)(void*))&CAcloseSslConnectionFreeEndpoint, (void*)localEndpointCpyPtr);
+
+            if (pthreadCreateRes != 0)
+            {
+                break;
+            }
+            detachStatusRes = pthread_detach(ccThread);
+        }while (0);
+
+        // regardless of CAcloseSslConnection result, the function will continue and g_connectionCallback will be called
+        OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "CAcloseSslConnection pthread_init [%d], mem_alloc [%d] pthread_create [%d], pthread_detach [%d]",
+                initAttrRes, memoryAllocationRes, pthreadCreateRes, detachStatusRes);
+#else
         CAcloseSslConnection(&localEndpoint);
 #endif
+#endif
     }
 
     if (g_connectionCallback)