zephyr/subsys/bluetooth/host/hci_common.c
Vinayak Kariappa Chettimada d382fca6ff Bluetooth: Controller: Fix HCI command buffer allocation failure
Fix HCI command buffer allocation failure, that can cause
loss of Host Number of Completed Packets command.

Fail by rejecting the HCI Host Buffer Size command if the
required number of HCI command buffers are not allocated in
the Controller implementation.

When Controller to Host data flow control is supported in
the Controller only build, ensure that BT_BUF_CMD_TX_COUNT
is greater than or equal to (BT_BUF_RX_COUNT + Ncmd),
where Ncmd is supported maximum Num_HCI_Command_Packets in
the Controller implementation.

Relates to commit 81614307e9 ("Bluetooth: Add workaround
for no command buffer available")'.

Relates to commit 297f4f481f ("Bluetooth: Split HCI
command & event buffers to two pools").

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
2025-02-21 11:30:38 +00:00

76 lines
1.9 KiB
C

/*
* Copyright (c) 2020 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdint.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/drivers/bluetooth.h>
#include "common/assert.h"
struct net_buf *bt_hci_evt_create(uint8_t evt, uint8_t len)
{
struct bt_hci_evt_hdr *hdr;
struct net_buf *buf;
buf = bt_buf_get_evt(evt, false, K_FOREVER);
BT_ASSERT(buf);
hdr = net_buf_add(buf, sizeof(*hdr));
hdr->evt = evt;
hdr->len = len;
return buf;
}
struct net_buf *bt_hci_cmd_complete_create(uint16_t op, uint8_t plen)
{
struct net_buf *buf;
struct bt_hci_evt_cmd_complete *cc;
buf = bt_hci_evt_create(BT_HCI_EVT_CMD_COMPLETE, sizeof(*cc) + plen);
cc = net_buf_add(buf, sizeof(*cc));
/* The Num_HCI_Command_Packets parameter allows the Controller to
* indicate the number of HCI command packets the Host can send to the
* Controller. If the Controller requires the Host to stop sending
* commands, Num_HCI_Command_Packets will be set to zero.
*
* NOTE: Zephyr Controller (and may be other Controllers) do not support
* higher Number of HCI Command packets than 1.
*/
cc->ncmd = 1U;
cc->opcode = sys_cpu_to_le16(op);
return buf;
}
struct net_buf *bt_hci_cmd_status_create(uint16_t op, uint8_t status)
{
struct net_buf *buf;
struct bt_hci_evt_cmd_status *cs;
buf = bt_hci_evt_create(BT_HCI_EVT_CMD_STATUS, sizeof(*cs));
cs = net_buf_add(buf, sizeof(*cs));
cs->status = status;
/* The Num_HCI_Command_Packets parameter allows the Controller to
* indicate the number of HCI command packets the Host can send to the
* Controller. If the Controller requires the Host to stop sending
* commands, Num_HCI_Command_Packets will be set to zero.
*
* NOTE: Zephyr Controller (and may be other Controllers) do not support
* higher Number of HCI Command packets than 1.
*/
cs->ncmd = 1U;
cs->opcode = sys_cpu_to_le16(op);
return buf;
}