From 9060e0d140f7de9b1158944f6129b9b029db48c6 Mon Sep 17 00:00:00 2001 From: Shawn Nock Date: Sat, 3 Aug 2019 04:36:45 -0500 Subject: [PATCH] Fixes ACK number off-by-one --- src/main.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index 751ebfd..ee10354 100644 --- a/src/main.c +++ b/src/main.c @@ -129,7 +129,7 @@ static struct h5 { struct k_fifo event_queue; u8_t tx_win; - u8_t last_seen_seq_from_host; + u8_t next_expected_seq_from_host; u8_t local_seq; linkstate_t link_state; @@ -280,7 +280,7 @@ void h5_send(const u8_t *payload, u8_t type, int len) h5_hdr_t hdr = {0}; /* Set ACK for outgoing packet and stop delayed work */ - hdr.ack = h5.last_seen_seq_from_host; + hdr.ack = h5.next_expected_seq_from_host; k_delayed_work_cancel(&ack_work); LOG_INF("ACK'd %d", hdr.ack); @@ -468,8 +468,10 @@ void bt_uart_isr(struct device *unused) if (buf->len > 0) { net_buf_put(&h5.unprocessed_queue, buf); buf = NULL; + slip_state = SLIP_NOSYNC; + } else { + slip_state = SLIP_SYNC; } - slip_state = SLIP_SYNC; } else { net_buf_add_u8(buf, byte); } @@ -548,19 +550,12 @@ static void unproc_thread(void) { LOG_DBG("Header: seq %d, ack %d, rel: %s, has_crc16: %s", hdr.seq, hdr.ack, hdr.is_reliable ? "true" : "false", hdr.crc_after_payload ? "true" : "false"); - u8_t expected_seq; - if (h5.initializing) { - expected_seq = 0; - h5.initializing = false; - } else { - expected_seq = next_seq(h5.last_seen_seq_from_host); - } - if (hdr.seq != expected_seq) { - LOG_ERR("Seq expected %u got %u. Drop packet", expected_seq, hdr.seq); + if (hdr.seq != h5.next_expected_seq_from_host) { + LOG_ERR("Seq expected %u got %u. Drop packet", h5.next_expected_seq_from_host, hdr.seq); goto next; } k_delayed_work_submit(&ack_work, H5_RX_ACK_TIMEOUT); - h5.last_seen_seq_from_host = hdr.seq; + h5.next_expected_seq_from_host = next_seq(hdr.seq); } if (h5.link_state < ACTIVE && hdr.packet_type != HCI_3WIRE_LINK_PKT) { @@ -889,7 +884,7 @@ static linkstate_t do_init(struct net_buf const *buf) { return INIT; case H5_CONFIG_RESPONSE: h5.tx_win = conf_rsp[2] & 0x7; - h5.last_seen_seq_from_host = 0; + h5.next_expected_seq_from_host = 0; h5.local_seq = 0; h5.initializing = true; LOG_DBG("Finished H5 configuration, tx_win %u: INIT -> ACTIVE", h5.tx_win); @@ -921,7 +916,6 @@ static linkstate_t do_active(struct net_buf *buf) { // Presumably something from the controller if (bt_buf_get_type(buf) == BT_BUF_EVT) { h5_send(buf->data, HCI_EVENT_PKT, buf->len); - k_yield(); } else { LOG_HEXDUMP_ERR(buf->data, buf->len, "Unexpected buffer in host_queue"); }