From 2eebff2717a2a47b1dfa9ee7b99ff29fff96a82c Mon Sep 17 00:00:00 2001 From: Shawn Nock Date: Sat, 2 May 2020 13:00:02 -0400 Subject: [PATCH] Reuse main thread for uart_tx --- src/ack_queue.h | 16 ++++++++++++++++ src/config.h | 11 +++++++++++ src/main.c | 43 ++++++++++++++++++++++++++++++++----------- 3 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 src/ack_queue.h create mode 100644 src/config.h diff --git a/src/ack_queue.h b/src/ack_queue.h new file mode 100644 index 0000000..9e04a78 --- /dev/null +++ b/src/ack_queue.h @@ -0,0 +1,16 @@ +// +// Created by nock on 2020-05-02. +// + +#ifndef HCI_UART_H5_ACK_QUEUE_H +#define HCI_UART_H5_ACK_QUEUE_H + +#include "config.h" + +struct aq_entry { + uint8_t buf[MAX_PACKET_LEN]; + uint8_t type; + size_t len; +}; + +#endif //HCI_UART_H5_ACK_QUEUE_H diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..dad62f9 --- /dev/null +++ b/src/config.h @@ -0,0 +1,11 @@ +// +// Created by nock on 2020-05-02. +// + +#ifndef HCI_UART_H5_CONFIG_H +#define HCI_UART_H5_CONFIG_H + +#define MAX_PACKETS_IN_FLIGHT (7) +#define MAX_PACKET_LEN (255) + +#endif //HCI_UART_H5_CONFIG_H diff --git a/src/main.c b/src/main.c index c88ec1c..e88c58a 100644 --- a/src/main.c +++ b/src/main.c @@ -37,6 +37,9 @@ #include "common/log.h" +#include "config.h" +#include "ack_queue.h" + static struct device *hci_uart_dev; /* HCI command buffers */ @@ -124,11 +127,13 @@ typedef enum { ACTIVE, } h5_linkstate_t; -static struct h5 { - //struct net_buf *rx_buf; +struct ack_s { + uint8_t buf[MAX_PACKETS_IN_FLIGHT][MAX_PACKET_LEN]; +}; + +static struct h5 { struct k_fifo controller_queue; - //struct k_fifo host_queue; struct k_fifo unack_queue; struct k_fifo unprocessed_queue; struct k_fifo event_queue; @@ -139,12 +144,11 @@ static struct h5 { u8_t local_seq; h5_linkstate_t link_state; + struct aq_entry ack_queue[MAX_PACKETS_IN_FLIGHT]; } h5; //static u8_t unack_queue_len; -#define MAX_PACKETS_IN_FLIGHT (0x01) - static const u8_t sync_req[] = {0x01, 0x7e}; static const u8_t sync_rsp[] = {0x02, 0x7d}; /* Third byte may change */ @@ -158,7 +162,6 @@ static const u8_t conf_rsp[] = {0x04, 0x7b, MAX_PACKETS_IN_FLIGHT}; NET_BUF_POOL_DEFINE(h5_pool, SIGNAL_COUNT, SIG_BUF_SIZE, 0, NULL); /* H5 Packet Buf */ -#define MAX_PACKET_LEN 255 #define PACKET_BUF_SIZE (CONFIG_BT_HCI_RESERVE + MAX_PACKET_LEN) NET_BUF_POOL_DEFINE(h5_pack_pool, MAX_PACKETS_IN_FLIGHT + 10, PACKET_BUF_SIZE, 0, NULL); @@ -288,6 +291,10 @@ next_seq(u8_t i) void h5_send(const u8_t *payload, h5_pkt_t type, int len) { + if (len > MAX_PACKET_LEN) { + LOG_ERR("Tried to send too-large packet. Size = %d", len); + return; + } h5_hdr_t hdr = {0}; /* Set ACK for outgoing packet and stop delayed work */ @@ -313,6 +320,9 @@ h5_send(const u8_t *payload, h5_pkt_t type, int len) net_buf_add_mem(buf, &hdr, sizeof(h5_hdr_t)); net_buf_add_mem(buf, payload, len); net_buf_put(&h5.uart_tx_queue, buf); + //memcpy(h5.ack.buf[hdr.seq], payload, len) + LOG_INF("Queued packet (rel: %d, seq: %d, len: %d, sum: %d)", + hdr.is_reliable, hdr.seq, len, hdr.checksum); } /* Delayed work taking care about retransmitting packets */ @@ -862,7 +872,7 @@ uart_tx_thread() } uart_poll_out(hci_uart_dev, SLIP_DELIMITER); - for (size_t i = 0, j = buf->len; i < j; i++) { + while (buf->len > 0) { h5_slip_byte(net_buf_pull_u8(buf)); } uart_poll_out(hci_uart_dev, SLIP_DELIMITER); @@ -876,7 +886,6 @@ h5_init(void) LOG_DBG("h5_init"); h5.link_state = UNINIT; - h5.tx_win = 4; /* TX thread - handles period h5_link messages and forwards commands to controller */ k_fifo_init(&h5.controller_queue); @@ -914,11 +923,11 @@ h5_init(void) /* Thread handles incoming events from BT Controller */ k_fifo_init(&h5.uart_tx_queue); - k_thread_create(&uart_tx_thread_data, uart_tx_stack, + /*k_thread_create(&uart_tx_thread_data, uart_tx_stack, K_THREAD_STACK_SIZEOF(uart_tx_stack), (k_thread_entry_t) uart_tx_thread, NULL, NULL, NULL, K_PRIO_PREEMPT(0), - 0, K_NO_WAIT); + 0, K_NO_WAIT);*/ /* Init delayed work */ @@ -1019,6 +1028,18 @@ main(void) h5_init(); while (true) { - k_sleep(K_FOREVER); + //k_sleep(K_FOREVER); + struct net_buf *buf = net_buf_get(&h5.uart_tx_queue, K_FOREVER); + if (!buf) { + LOG_ERR("uart_tx_queue buf is NULL"); + continue; + } + uart_poll_out(hci_uart_dev, SLIP_DELIMITER); + + while (buf->len > 0) { + h5_slip_byte(net_buf_pull_u8(buf)); + } + uart_poll_out(hci_uart_dev, SLIP_DELIMITER); + net_buf_unref(buf); } }