Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bluetooth: host: handle error case on sync cmd #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions drivers/bluetooth/hci/h4.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,13 @@ static int h4_open(void)
return 0;
}

static int h4_close(void)
{
k_thread_abort(&rx_thread_data);

return 0;
}

#if defined(CONFIG_BT_HCI_SETUP)
static int h4_setup(const struct bt_hci_setup_params *params)
{
Expand All @@ -550,6 +557,7 @@ static const struct bt_hci_driver drv = {
.name = "H:4",
.bus = BT_HCI_DRIVER_BUS_UART,
.open = h4_open,
.close = h4_close,
.send = h4_send,
#if defined(CONFIG_BT_HCI_SETUP)
.setup = h4_setup
Expand Down
19 changes: 19 additions & 0 deletions drivers/bluetooth/hci/h5.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,10 +768,29 @@ static int h5_open(void)
return 0;
}

static void h5_deinit(void)
{
k_work_cancel_delayable(&retx_work);
k_work_cancel_delayable(&ack_work);

k_thread_abort(&rx_thread_data);
k_thread_abort(&tx_thread_data);
}

static int h5_close(void)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's void type

{
uart_irq_rx_disable(h5_dev);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disable the interrupt first to be sure that we can cancel the works and kill the rx thread.


h5_deinit();

return 0;
}

static const struct bt_hci_driver drv = {
.name = "H:5",
.bus = BT_HCI_DRIVER_BUS_UART,
.open = h5_open,
.close = h5_close,
.send = h5_queue,
};

Expand Down
41 changes: 37 additions & 4 deletions subsys/bluetooth/host/hci_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ int bt_hci_cmd_send_sync(uint16_t opcode, struct net_buf *buf,

err = k_sem_take(&sync_sem, HCI_CMD_TIMEOUT);
BT_ASSERT_MSG(err == 0, "command opcode 0x%04x timeout with err %d", opcode, err);
if (err) {
net_buf_unref(buf);
return err;
}

status = cmd(buf)->status;
if (status) {
Expand Down Expand Up @@ -3940,6 +3944,19 @@ static void init_work(struct k_work *work)
int err;

err = bt_init();
if (err) {
if (bt_dev.drv->close) {
bt_dev.drv->close();
}

#if defined(CONFIG_BT_RECV_WORKQ_BT)
/* Abort RX thread */
k_thread_abort(&bt_workq.thread);
#endif

k_thread_abort(&tx_thread_data);
}

if (ready_cb) {
ready_cb(err);
}
Expand Down Expand Up @@ -4065,17 +4082,33 @@ int bt_enable(bt_ready_cb_t cb)
err = bt_dev.drv->open();
if (err) {
LOG_ERR("HCI driver open failed (%d)", err);
return err;
goto error;
}

bt_monitor_send(BT_MONITOR_OPEN_INDEX, NULL, 0);

if (!cb) {
return bt_init();
if (cb) {
k_work_submit(&bt_dev.init);
} else {
err = bt_init();
if (err) {
if (bt_dev.drv->close) {
bt_dev.drv->close();
}
goto error;
}
}

k_work_submit(&bt_dev.init);
return 0;

error:
#if defined(CONFIG_BT_RECV_WORKQ_BT)
/* Abort RX thread */
k_thread_abort(&bt_workq.thread);
#endif

k_thread_abort(&tx_thread_data);
return err;
}

int bt_disable(void)
Expand Down
Loading