Improve robustness
diff --git a/st21nfc/nfc_nci_st21nfc.c b/st21nfc/nfc_nci_st21nfc.c
index fce3752..4cbc383 100644
--- a/st21nfc/nfc_nci_st21nfc.c
+++ b/st21nfc/nfc_nci_st21nfc.c
@@ -41,7 +41,7 @@
   HALHANDLE hHAL;
 } st21nfc_dev_t;
 
-char* halVersion = "ST21NFC NCI Version 3.0.1";
+char* halVersion = "ST21NFC NCI Version 3.0.2";
 uint8_t cmd_set_nfc_mode_enable[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
 uint8_t hal_is_closed = 1;
 pthread_mutex_t hal_mtx = PTHREAD_MUTEX_INITIALIZER;
@@ -78,11 +78,12 @@
 
   result = hal_wrapper_open(dev, p_cback, p_data_cback, &dev->hHAL);
 
-  if (!result || !dev->hHAL) {
-    (void)pthread_mutex_unlock(&hal_mtx);
-    dev->p_cback(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_FAILED);
-    return -1;  // We are doomed, stop it here, NOW !
-  }
+  if (!result || !dev->hHAL)
+    {
+      dev->p_cback(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_FAILED);
+      (void) pthread_mutex_unlock(&hal_mtx);
+      return -1;  // We are doomed, stop it here, NOW !
+    }
   hal_is_closed = 0;
   (void)pthread_mutex_unlock(&hal_mtx);
   return 0;
@@ -96,17 +97,24 @@
 
   /* check if HAL is closed */
   int ret = (int)data_len;
-  (void)pthread_mutex_lock(&hal_mtx);
-  if ( hal_is_closed ) {
-    ret = 0;
-  }
-  (void)pthread_mutex_unlock(&hal_mtx);
+  (void) pthread_mutex_lock(&hal_mtx);
+  if (hal_is_closed)
+    {
+      ret = 0;
+    }
+
   if (!ret)
-    return ret;
-  if (!HalSendDownstream(dev->hHAL, p_data, data_len)) {
-    STLOG_HAL_E("NFC-NCI HAL: %s  SendDownstream failed", __func__);
-    return 0;
-  }
+    {
+      (void) pthread_mutex_unlock(&hal_mtx);
+      return ret;
+    }
+  if (!HalSendDownstream(dev->hHAL, p_data, data_len))
+    {
+      STLOG_HAL_E("NFC-NCI HAL: %s  SendDownstream failed", __func__);
+      (void) pthread_mutex_unlock(&hal_mtx);
+      return 0;
+    }
+  (void) pthread_mutex_unlock(&hal_mtx);
 
   return ret;
 }
@@ -115,11 +123,11 @@
                                 uint8_t* p_core_init_rsp_params) {
   STLOG_HAL_D("NFC-NCI HAL: %s", __func__);
 
+  (void)pthread_mutex_lock(&hal_mtx);
   st21nfc_dev_t* dev = (st21nfc_dev_t*)p_dev;
-
   hal_dta_state = *p_core_init_rsp_params;
-
   dev->p_cback(HAL_NFC_POST_INIT_CPLT_EVT, HAL_NFC_STATUS_OK);
+  (void) pthread_mutex_unlock(&hal_mtx);
 
   return 0;  // return != 0 to signal ready immediate
 }
@@ -171,15 +179,20 @@
 
   /* check if HAL is closed */
   int ret = HAL_NFC_STATUS_OK;
-  (void)pthread_mutex_lock(&hal_mtx);
-  if ( hal_is_closed ) {
-    ret = HAL_NFC_STATUS_FAILED;
-  }
-  (void)pthread_mutex_unlock(&hal_mtx);
+  (void) pthread_mutex_lock(&hal_mtx);
+  if (hal_is_closed)
+    {
+      ret = HAL_NFC_STATUS_FAILED;
+    }
+
   if (ret != HAL_NFC_STATUS_OK)
-    return ret;
+    {
+      (void) pthread_mutex_unlock(&hal_mtx);
+      return ret;
+    }
   dev->p_cback(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_OK);
 
+  (void) pthread_mutex_unlock(&hal_mtx);
   return HAL_NFC_STATUS_OK;
 }
 
@@ -189,7 +202,9 @@
 
 /* Close an opened nfc device instance */
 static int nfc_close(hw_device_t* dev) {
+  (void) pthread_mutex_lock(&hal_mtx);
   free(dev);
+  (void) pthread_mutex_unlock(&hal_mtx);
   return 0;
 }