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

nimble/ll: Fix handling HCI ACL data #1983

Open
wants to merge 2 commits into
base: master
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
2 changes: 2 additions & 0 deletions nimble/controller/include/controller/ble_ll_conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ struct ble_ll_conn_sm
/* Packet transmit queue */
struct os_mbuf *cur_tx_pdu;
STAILQ_HEAD(conn_txq_head, os_mbuf_pkthdr) conn_txq;
uint8_t conn_txq_num_data_pkt;
uint8_t conn_txq_num_zero_pkt;

/* List entry for active/free connection pools */
union {
Expand Down
4 changes: 2 additions & 2 deletions nimble/controller/src/ble_ll.c
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ ble_ll_tx_pkt_in(void)

/* Do some basic error checking */
pb = handle & 0x3000;
if ((pkthdr->omp_len != length) || (pb > 0x1000) || (length == 0)) {
if ((pkthdr->omp_len != length) || (pb > 0x1000)) {
/* This is a bad ACL packet. Count a stat and free it */
STATS_INC(ble_ll_stats, bad_acl_hdr);
os_mbuf_free_chain(om);
Expand Down Expand Up @@ -1548,7 +1548,7 @@ ble_ll_mbuf_init(struct os_mbuf *m, uint8_t pdulen, uint8_t hdr)

/* Set BLE transmit header */
ble_hdr = BLE_MBUF_HDR_PTR(m);
ble_hdr->txinfo.flags = 0;
ble_hdr->txinfo.num_data_pkt = 0;
ble_hdr->txinfo.offset = 0;
ble_hdr->txinfo.pyld_len = pdulen;
ble_hdr->txinfo.hdr_byte = hdr;
Expand Down
39 changes: 36 additions & 3 deletions nimble/controller/src/ble_ll_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ ble_ll_conn_tx_pdu(struct ble_ll_conn_sm *connsm)
m->om_data = (uint8_t *)&empty_pdu;
m->om_data += BLE_MBUF_MEMBLOCK_OVERHEAD;
ble_hdr = &empty_pdu.ble_hdr;
ble_hdr->txinfo.flags = 0;
ble_hdr->txinfo.num_data_pkt = 0;
ble_hdr->txinfo.offset = 0;
ble_hdr->txinfo.pyld_len = 0;
}
Expand Down Expand Up @@ -2034,6 +2034,8 @@ ble_ll_conn_sm_new(struct ble_ll_conn_sm *connsm)

/* Initialize transmit queue and ack/flow control elements */
STAILQ_INIT(&connsm->conn_txq);
connsm->conn_txq_num_data_pkt = 0;
connsm->conn_txq_num_zero_pkt = 0;
connsm->cur_tx_pdu = NULL;
connsm->tx_seqnum = 0;
connsm->next_exp_seqnum = 0;
Expand Down Expand Up @@ -3845,7 +3847,8 @@ ble_ll_conn_rx_isr_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr)
#if (BLETEST_THROUGHPUT_TEST == 1)
bletest_completed_pkt(connsm->conn_handle);
#endif
++connsm->completed_pkts;
BLE_LL_ASSERT(txhdr->txinfo.num_data_pkt >= 1);
connsm->completed_pkts += txhdr->txinfo.num_data_pkt;
if (connsm->completed_pkts > 2) {
ble_ll_event_add(&g_ble_ll_data.ll_comp_pkt_ev);
}
Expand Down Expand Up @@ -3946,17 +3949,46 @@ ble_ll_conn_enqueue_pkt(struct ble_ll_conn_sm *connsm, struct os_mbuf *om,
os_sr_t sr;
struct os_mbuf_pkthdr *pkthdr;
struct ble_mbuf_hdr *ble_hdr;
uint8_t num_pkt;
int lifo;

/* We cannot send empty payload with LLID=0b10. Instead, we need to wait for
* non-empty payload and combine it together. Since we don't really recombine
* fragments we just count number of consecutive empty payloads and use 1st
* non-empty as a start packet. Empty payloads can be freed immediately as
* we don't need to enqueue them.
*
* Reference: Core 6.0, Vol 6, Part B, 2.4.1
*/
if ((length == 0) &&
((hdr_byte == BLE_LL_LLID_DATA_START) || (connsm->conn_txq_num_zero_pkt > 0))) {
connsm->conn_txq_num_zero_pkt++;
os_mbuf_free_chain(om);
return;
}

/* Set mbuf length and packet length if a control PDU */
if (hdr_byte == BLE_LL_LLID_CTRL) {
om->om_len = length;
OS_MBUF_PKTHDR(om)->omp_len = length;
num_pkt = 0;
} else {
num_pkt = 1;

/* This is the 1st non-empty data fragment so adjust LLID accordingly.
* num_pkt is updated to make sure we send back proper numbber of
* completed packets back to host.
*/
if (connsm->conn_txq_num_zero_pkt) {
hdr_byte = BLE_LL_LLID_DATA_START;
num_pkt += connsm->conn_txq_num_zero_pkt;
connsm->conn_txq_num_zero_pkt = 0;
}
}

/* Set BLE transmit header */
ble_hdr = BLE_MBUF_HDR_PTR(om);
ble_hdr->txinfo.flags = 0;
ble_hdr->txinfo.num_data_pkt = num_pkt;
ble_hdr->txinfo.offset = 0;
ble_hdr->txinfo.hdr_byte = hdr_byte;

Expand Down Expand Up @@ -4015,6 +4047,7 @@ ble_ll_conn_enqueue_pkt(struct ble_ll_conn_sm *connsm, struct os_mbuf *om,
} else {
STAILQ_INSERT_TAIL(&connsm->conn_txq, pkthdr, omp_next);
}
connsm->conn_txq_num_data_pkt += num_pkt;
OS_EXIT_CRITICAL(sr);
}

Expand Down
6 changes: 5 additions & 1 deletion nimble/controller/src/ble_ll_conn_hci.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ ble_ll_conn_num_comp_pkts_event_send(struct ble_ll_conn_sm *connsm)
ev->completed[0].packets = htole16(connsm->completed_pkts);
hci_ev->length += sizeof(ev->completed[0]);

BLE_LL_ASSERT(connsm->conn_txq_num_data_pkt >= connsm->completed_pkts);
connsm->conn_txq_num_data_pkt -= connsm->completed_pkts;
connsm->completed_pkts = 0;

ble_ll_hci_event_send(hci_ev);
Expand All @@ -330,7 +332,7 @@ ble_ll_conn_num_comp_pkts_event_send(struct ble_ll_conn_sm *connsm)
* event and that either has packets enqueued or has completed packets.
*/
if ((connsm->conn_state != BLE_LL_CONN_STATE_IDLE) &&
(connsm->completed_pkts || !STAILQ_EMPTY(&connsm->conn_txq))) {
(connsm->completed_pkts || connsm->conn_txq_num_data_pkt)) {
/* If no buffer, get one, If cant get one, leave. */
if (!hci_ev) {
hci_ev = ble_transport_alloc_evt(0);
Expand All @@ -351,6 +353,8 @@ ble_ll_conn_num_comp_pkts_event_send(struct ble_ll_conn_sm *connsm)
hci_ev->length += sizeof(ev->completed[ev->count]);
ev->count++;

BLE_LL_ASSERT(connsm->conn_txq_num_data_pkt >= connsm->completed_pkts);
connsm->conn_txq_num_data_pkt -= connsm->completed_pkts;
connsm->completed_pkts = 0;

/* Send now if the buffer is full. */
Expand Down
2 changes: 1 addition & 1 deletion nimble/controller/src/ble_ll_dtm.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ ble_ll_dtm_tx_create_ctx(uint8_t packet_payload, uint8_t len,

/* Set BLE transmit header */
ble_hdr = BLE_MBUF_HDR_PTR(m);
ble_hdr->txinfo.flags = 0;
ble_hdr->txinfo.num_data_pkt = 0;
ble_hdr->txinfo.offset = 0;
ble_hdr->txinfo.pyld_len = len;
ble_hdr->txinfo.hdr_byte = packet_payload;
Expand Down
2 changes: 1 addition & 1 deletion nimble/include/nimble/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct ble_mbuf_hdr_rxinfo
/* Transmit info. NOTE: no flags defined */
struct ble_mbuf_hdr_txinfo
{
uint8_t flags;
uint8_t num_data_pkt;
uint8_t hdr_byte;
uint16_t offset;
uint16_t pyld_len;
Expand Down
Loading