FM HCI: Use mutex while init, send, close HCI

Use mutex while initializing, send HCI commands
and close HCI. This will help with mutual exclusion
when multiple threads sending and/or closing HCI
interface.

CRs-Fixed: 3221629
Change-Id: Ib898a7a5c277f9dd5192d4e17bd7666128d418e8
diff --git a/fm_hci/fm_hci.cpp b/fm_hci/fm_hci.cpp
index f33275e..0869afc 100644
--- a/fm_hci/fm_hci.cpp
+++ b/fm_hci/fm_hci.cpp
@@ -65,6 +65,7 @@
 static struct fm_hci_t hci;
 
 typedef std::unique_lock<std::mutex> Lock;
+static std::recursive_mutex mtx;
 android::sp<IFmHci> fmHci;
 
 static int enqueue_fm_rx_event(struct fm_event_header_t *hdr);
@@ -597,7 +598,8 @@
 *******************************************************************************/
 static bool hci_initialize()
 {
-    ALOGI("%s", __func__);
+    ALOGI("%s: acquiring mutex", __func__);
+    std::lock_guard<std::recursive_mutex> lk(mtx);
 
     if (fmHci != nullptr) {
         hci.state = FM_RADIO_ENABLING;
@@ -628,7 +630,8 @@
 static void hci_transmit(struct fm_command_header_t *hdr) {
     HciPacket data;
 
-    ALOGI("%s: opcode 0x%x len:%d", __func__, hdr->opcode, hdr->len);
+    ALOGI("%s: opcode 0x%x len:%d, acquiring mutex", __func__, hdr->opcode, hdr->len);
+    std::lock_guard<std::recursive_mutex> lk(mtx);
 
     if (fmHci != nullptr) {
         data.setToExternal((uint8_t *)hdr, 3 + hdr->len);
@@ -658,7 +661,8 @@
 *******************************************************************************/
 static void hci_close()
 {
-    ALOGI("%s", __func__);
+    ALOGI("%s: acquiring mutex", __func__);
+    std::lock_guard<std::recursive_mutex> lk(mtx);
 
     if (fmHci != nullptr) {
         auto death_unlink = fmHci->unlinkToDeath(fmHciDeathRecipient);