wrap CAcloseSslConnectionFreeEndpoint method with __TIZEN__ flag
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / caleadapter.c
index 2a6c02d..01c80b2 100644 (file)
@@ -1182,9 +1182,6 @@ static void CALEServerSendDataThread(void *threadData)
 
 #if defined(__TIZEN__) || defined(__ANDROID__)
     // get MTU size
-    OIC_LOG_V(INFO, CALEADAPTER_TAG, "Get MTU size using API");
-
-
     g_mtuSize = CALEServerGetMtuSize(bleData->remoteEndpoint->addr);
 #endif
     OIC_LOG_V(INFO, CALEADAPTER_TAG, "MTU size [%d]", g_mtuSize);
@@ -1329,7 +1326,7 @@ static void CALEServerSendDataThread(void *threadData)
 
         OIC_LOG_V(DEBUG,
                   CALEADAPTER_TAG,
-                  "Server Sent Unicast First Data - data length [%zu]",
+                  "Server Sent Unicast First Data - data length [%u]",
                   length);
 
         result = CAGenerateHeader(dataHeader,
@@ -1490,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);
@@ -1632,7 +1631,7 @@ static void CALEClientSendDataThread(void *threadData)
         }
         OIC_LOG_V(DEBUG,
                   CALEADAPTER_TAG,
-                  "Client Sent Unicast First Data - data length [%zu]",
+                  "Client Sent Unicast First Data - data length [%u]",
                   length);
 
         result = CAGenerateHeader(dataHeader,
@@ -1694,7 +1693,7 @@ static void CALEClientSendDataThread(void *threadData)
             }
             OIC_LOG_V(DEBUG,
                       CALEADAPTER_TAG,
-                      "Client Sent Unicast %d Data - data(mtu) length [%zu]",
+                      "Client Sent Unicast %d Data - data(mtu) length [%hu]",
                       index + 1,
                       g_mtuSize);
         }
@@ -2380,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)
     {
@@ -2388,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
     /*
@@ -2489,7 +2492,7 @@ static ssize_t CALESecureSendDataCB(CAEndpoint_t *endpoint, const void *data, si
     VERIFY_NON_NULL(endpoint, CALEADAPTER_TAG, "endpoint is NULL");
     VERIFY_NON_NULL(data, CALEADAPTER_TAG, "data is NULL");
     OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__);
-    OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "encrypted datalen = %d", dataLen);
+    OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "encrypted datalen = %zd", dataLen);
 
     CAResult_t result;
     CADataType_t dataType = g_dataType;
@@ -2547,7 +2550,7 @@ CAResult_t CALESecureReceiveDataCB(const CASecureEndpoint_t *sep, const void *da
     VERIFY_NON_NULL(data, CALEADAPTER_TAG, "data is NULL");
 
     OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
-              "Secure Data Receive - decrypted datalen = %d", dataLen);
+              "Secure Data Receive - decrypted datalen = %zd", dataLen);
 
     if (dataLen <= 0)
     {
@@ -3191,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)
@@ -3444,7 +3493,7 @@ static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
     // Create bleData to add to queue
     OIC_LOG_V(DEBUG,
               CALEADAPTER_TAG,
-              "Data received from LE Client layer [%zu]",
+              "Data received from LE Client layer [%u]",
               dataLength);
 
     CALEData_t * const bleData =