FM: Wait for hal to become NULL from last session

Wait for hal to become NULL from last session to allocate
memory for hal for the current session.

Change-Id: I8af2236edabc28a1b48c6f5bd8a7ad1288b2218e
CRs-fixed: 2106441
diff --git a/helium/radio_helium_hal.c b/helium/radio_helium_hal.c
index f356f5f..4ba1e11 100644
--- a/helium/radio_helium_hal.c
+++ b/helium/radio_helium_hal.c
@@ -61,6 +61,8 @@
 struct fm_hal_t *hal = NULL;
 
 #define LOG_TAG "radio_helium"
+#define WAIT_TIMEOUT 20000 /* 20*1000us */
+
 static void radio_hci_req_complete(char result)
 {
   ALOGD("%s:enetred %s", LOG_TAG, __func__);
@@ -1261,13 +1263,30 @@
 
 int hal_init(fm_hal_callbacks_t *cb)
 {
-    int ret = -FM_HC_STATUS_FAIL;
+    int ret = -FM_HC_STATUS_FAIL, i;
     fm_hci_hal_t hci_hal;
 
     ALOGD("++%s", __func__);
 
     memset(&hci_hal, 0, sizeof(fm_hci_hal_t));
 
+    if (hal) {
+        ALOGE("%s:HAL is still available from the last session, please wait for sometime", __func__);
+        for(i=0; i<10; i++) {
+            if (!hal) {
+                break;
+            } else {
+                usleep(WAIT_TIMEOUT);
+            }
+        }
+    }
+
+    if (hal) {
+        ALOGE("%s:Last FM session didnot end properly, please launch again", __func__);
+        hal = NULL;
+        return ret;
+    }
+
     hal = malloc(sizeof(struct fm_hal_t));
     if (!hal) {
         ALOGE("%s:Failed to allocate memory", __func__);