Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e11eda16 authored by Chris Manton's avatar Chris Manton
Browse files

Protect hci library cleanup with mutex

hci library cleanup is no longer thread safe
and must have access serialized.

Change-Id: I155a09f6b0ab60dcc5e076ee1108a2bec8b96251
parent 86658b0a
Loading
Loading
Loading
Loading
+12 −2
Original line number Original line Diff line number Diff line
@@ -74,6 +74,8 @@ void init_vnd_if(unsigned char *local_bdaddr);
bt_hc_callbacks_t *bt_hc_cbacks = NULL;
bt_hc_callbacks_t *bt_hc_cbacks = NULL;
tHCI_IF *p_hci_if;
tHCI_IF *p_hci_if;
volatile bool fwcfg_acked;
volatile bool fwcfg_acked;
// Cleanup state indication.
volatile bool has_cleaned_up = false;


/******************************************************************************
/******************************************************************************
**  Local type definitions
**  Local type definitions
@@ -325,6 +327,7 @@ static int init(const bt_hc_callbacks_t* p_cb, unsigned char *local_bdaddr)


    hc_cb.epilog_timer_created = false;
    hc_cb.epilog_timer_created = false;
    fwcfg_acked = false;
    fwcfg_acked = false;
    has_cleaned_up = false;


    pthread_mutex_init(&hc_cb.worker_thread_lock, NULL);
    pthread_mutex_init(&hc_cb.worker_thread_lock, NULL);


@@ -446,9 +449,15 @@ static int tx_hc_cmd(TRANSAC transac, char *p_buf, int len) {
  return BT_HC_STATUS_SUCCESS;
  return BT_HC_STATUS_SUCCESS;
}
}


/** Closes the interface */
// Closes the interface.
// This routine is not thread safe.
static void cleanup(void)
static void cleanup(void)
{
{
    if (has_cleaned_up) {
        ALOGW("%s Already cleaned up for this session\n", __func__);
        return;
    }

    BTHCDBG("cleanup");
    BTHCDBG("cleanup");


    if (hc_cb.worker_thread)
    if (hc_cb.worker_thread)
@@ -484,6 +493,7 @@ static void cleanup( void )


    fwcfg_acked = false;
    fwcfg_acked = false;
    bt_hc_cbacks = NULL;
    bt_hc_cbacks = NULL;
    has_cleaned_up = true;
}
}


static const bt_hc_interface_t bluetoothHCLibInterface = {
static const bt_hc_interface_t bluetoothHCLibInterface = {
+18 −7
Original line number Original line Diff line number Diff line
@@ -84,7 +84,6 @@ BOOLEAN hci_logging_enabled = FALSE; /* by default, turn hci log off */
BOOLEAN hci_logging_config = FALSE;    /* configured from bluetooth framework */
BOOLEAN hci_logging_config = FALSE;    /* configured from bluetooth framework */
char hci_logfile[256] = HCI_LOGGING_FILENAME;
char hci_logfile[256] = HCI_LOGGING_FILENAME;



/*******************************************************************************
/*******************************************************************************
**  Static variables
**  Static variables
*******************************************************************************/
*******************************************************************************/
@@ -92,6 +91,8 @@ static bt_hc_interface_t *bt_hc_if=NULL;
static const bt_hc_callbacks_t hc_callbacks;
static const bt_hc_callbacks_t hc_callbacks;
static BOOLEAN lpm_enabled = FALSE;
static BOOLEAN lpm_enabled = FALSE;
static bt_preload_retry_cb_t preload_retry_cb;
static bt_preload_retry_cb_t preload_retry_cb;
// Lock to serialize cleanup requests from upper layer.
static pthread_mutex_t cleanup_lock;


/*******************************************************************************
/*******************************************************************************
**  Static functions
**  Static functions
@@ -171,6 +172,9 @@ void bte_main_boot_entry(void)
    /* Initialize trace feature */
    /* Initialize trace feature */
    BTTRC_TraceInit(MAX_TRACE_RAM_SIZE, &BTE_TraceLogBuf[0], BTTRC_METHOD_RAM);
    BTTRC_TraceInit(MAX_TRACE_RAM_SIZE, &BTE_TraceLogBuf[0], BTTRC_METHOD_RAM);
#endif
#endif

    pthread_mutex_init(&cleanup_lock, NULL);

}
}


/******************************************************************************
/******************************************************************************
@@ -184,6 +188,8 @@ void bte_main_boot_entry(void)
******************************************************************************/
******************************************************************************/
void bte_main_shutdown()
void bte_main_shutdown()
{
{
    pthread_mutex_destroy(&cleanup_lock);

    GKI_shutdown();
    GKI_shutdown();
}
}


@@ -325,12 +331,17 @@ static void bte_hci_disable(void)
{
{
    APPL_TRACE_DEBUG("%s", __FUNCTION__);
    APPL_TRACE_DEBUG("%s", __FUNCTION__);


    if (bt_hc_if)
    if (!bt_hc_if)
    {
        return;

    // Cleanup is not thread safe and must be protected.
    pthread_mutex_lock(&cleanup_lock);

    if (hci_logging_enabled == TRUE ||  hci_logging_config == TRUE)
    if (hci_logging_enabled == TRUE ||  hci_logging_config == TRUE)
        bt_hc_if->logging(BT_HC_LOGGING_OFF, hci_logfile);
        bt_hc_if->logging(BT_HC_LOGGING_OFF, hci_logfile);
    bt_hc_if->cleanup();
    bt_hc_if->cleanup();
    }

    pthread_mutex_unlock(&cleanup_lock);
}
}


/*******************************************************************************
/*******************************************************************************