unref tx_thread buffers
This commit is contained in:
parent
5887339c44
commit
3a48b0b825
196
src/main.c
196
src/main.c
|
@ -18,7 +18,6 @@
|
|||
#include <logging/log.h>
|
||||
#include <logging/log_ctrl.h>
|
||||
|
||||
|
||||
#include <misc/util.h>
|
||||
|
||||
#include <device.h>
|
||||
|
@ -84,7 +83,8 @@ typedef enum {
|
|||
HCI_3WIRE_LINK_PKT = 0x0f,
|
||||
} h5_pkt_t;
|
||||
|
||||
static bool reliable_packet(h5_pkt_t type)
|
||||
static bool
|
||||
reliable_packet(h5_pkt_t type)
|
||||
{
|
||||
switch (type) {
|
||||
case HCI_COMMAND_PKT:
|
||||
|
@ -160,11 +160,14 @@ NET_BUF_POOL_DEFINE(h5_pool, SIGNAL_COUNT, SIG_BUF_SIZE, 0, NULL);
|
|||
/* H5 Packet Buf */
|
||||
#define MAX_PACKET_LEN 255 // CMD Header + 255 max payload
|
||||
#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);
|
||||
NET_BUF_POOL_DEFINE(h5_pack_pool, MAX_PACKETS_IN_FLIGHT + 10, PACKET_BUF_SIZE,
|
||||
0, NULL);
|
||||
|
||||
NET_BUF_POOL_DEFINE(uart_tx_pool, MAX_PACKETS_IN_FLIGHT + 10, PACKET_BUF_SIZE, 0, NULL);
|
||||
NET_BUF_POOL_DEFINE(uart_tx_pool, MAX_PACKETS_IN_FLIGHT + 10, PACKET_BUF_SIZE,
|
||||
0, NULL);
|
||||
|
||||
static inline void bt_uart_drain(struct device *dev)
|
||||
static inline void
|
||||
bt_uart_drain(struct device *dev)
|
||||
{
|
||||
u8_t c;
|
||||
|
||||
|
@ -175,7 +178,9 @@ static inline void bt_uart_drain(struct device *dev)
|
|||
|
||||
#define REBOOT_IF_NOT(buf) _reboot_if_not(buf, __FILE__, __LINE__)
|
||||
|
||||
void _reboot_if_not(struct net_buf *buf, char const *file, int line) {
|
||||
void
|
||||
_reboot_if_not(struct net_buf *buf, char const *file, int line)
|
||||
{
|
||||
if (!buf) {
|
||||
log_panic();
|
||||
LOG_ERR("Failed alloc at %s:%d", file, line);
|
||||
|
@ -234,7 +239,8 @@ static void process_unack(void)
|
|||
}
|
||||
*/
|
||||
|
||||
static void h5_print_header(h5_hdr_t *hdr)
|
||||
static void
|
||||
h5_print_header(h5_hdr_t *hdr)
|
||||
{
|
||||
if (hdr->is_reliable) {
|
||||
LOG_DBG("REL: seq %d ack %d crc %d type %d len %d",
|
||||
|
@ -248,8 +254,8 @@ static void h5_print_header(h5_hdr_t *hdr)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
static u8_t h5_slip_byte(u8_t byte)
|
||||
static u8_t
|
||||
h5_slip_byte(u8_t byte)
|
||||
{
|
||||
switch (byte) {
|
||||
case SLIP_DELIMITER:
|
||||
|
@ -266,16 +272,21 @@ static u8_t h5_slip_byte(u8_t byte)
|
|||
}
|
||||
}
|
||||
|
||||
void h5_hdr_update_checksum(h5_hdr_t *hdr) {
|
||||
void
|
||||
h5_hdr_update_checksum(h5_hdr_t *hdr)
|
||||
{
|
||||
uint8_t *u8_hdr = (uint8_t *) hdr;
|
||||
u8_hdr[3] = ~((u8_hdr[0] + u8_hdr[1] + u8_hdr[2]) & 0xff);
|
||||
}
|
||||
|
||||
static u8_t next_seq(u8_t i) {
|
||||
static u8_t
|
||||
next_seq(u8_t i)
|
||||
{
|
||||
return (i + 1) % 8;
|
||||
}
|
||||
|
||||
void h5_send(const u8_t *payload, h5_pkt_t type, int len)
|
||||
void
|
||||
h5_send(const u8_t *payload, h5_pkt_t type, int len)
|
||||
{
|
||||
h5_hdr_t hdr = {0};
|
||||
|
||||
|
@ -337,18 +348,24 @@ void h5_send(const u8_t *payload, h5_pkt_t type, int len)
|
|||
}
|
||||
}*/
|
||||
|
||||
static void ack_timeout(struct k_work *work) {
|
||||
static void
|
||||
ack_timeout(struct k_work *work)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
LOG_INF("ACK Timeout, sending H5 ACK packet");
|
||||
h5_send(NULL, HCI_3WIRE_ACK_PKT, 0);
|
||||
}
|
||||
|
||||
bool h5_hdr_checksum_good(h5_hdr_t *hdr) {
|
||||
bool
|
||||
h5_hdr_checksum_good(h5_hdr_t *hdr)
|
||||
{
|
||||
uint8_t *u8_hdr = (uint8_t *) hdr;
|
||||
return ((u8_hdr[3] + u8_hdr[0] + u8_hdr[1] + u8_hdr[2]) & 0xff) == 0xff;
|
||||
}
|
||||
|
||||
bool unslip_header(uint8_t *buf, size_t len, h5_hdr_t *hdr) {
|
||||
bool
|
||||
unslip_header(uint8_t *buf, size_t len, h5_hdr_t *hdr)
|
||||
{
|
||||
uint8_t hdr_idx = 0, *u8_hdr = (uint8_t *) &hdr;
|
||||
|
||||
if (len < sizeof(h5_hdr_t)) {
|
||||
|
@ -374,7 +391,9 @@ bool unslip_header(uint8_t *buf, size_t len, h5_hdr_t *hdr) {
|
|||
return h5_hdr_checksum_good(hdr);
|
||||
}
|
||||
|
||||
int unslip_next_byte(struct net_buf *buf) {
|
||||
int
|
||||
unslip_next_byte(struct net_buf *buf)
|
||||
{
|
||||
if (!buf->len) {
|
||||
// This happens at the end of every buffer, not work logging
|
||||
return -1;
|
||||
|
@ -401,7 +420,9 @@ int unslip_next_byte(struct net_buf *buf) {
|
|||
return -2;
|
||||
}
|
||||
|
||||
int pull_header1(struct net_buf *buf, h5_hdr_t *hdr) {
|
||||
int
|
||||
pull_header1(struct net_buf *buf, h5_hdr_t *hdr)
|
||||
{
|
||||
uint8_t *u8_hdr = (uint8_t *) hdr;
|
||||
|
||||
if (buf->len < sizeof(h5_hdr_t)) {
|
||||
|
@ -422,7 +443,8 @@ int pull_header1(struct net_buf *buf, h5_hdr_t *hdr) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
void bt_uart_isr(struct device *unused)
|
||||
void
|
||||
bt_uart_isr(struct device *unused)
|
||||
{
|
||||
static u8_t byte;
|
||||
static struct net_buf *buf = NULL;
|
||||
|
@ -456,7 +478,8 @@ void bt_uart_isr(struct device *unused)
|
|||
net_buf_reset(buf);
|
||||
slip_state = SLIP_NOSYNC;
|
||||
}
|
||||
if (slip_state == SLIP_NOSYNC && byte != SLIP_DELIMITER) {
|
||||
if (slip_state == SLIP_NOSYNC
|
||||
&& byte != SLIP_DELIMITER) {
|
||||
continue;
|
||||
}
|
||||
if (byte == SLIP_DELIMITER) {
|
||||
|
@ -474,7 +497,6 @@ void bt_uart_isr(struct device *unused)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/*void bt_uart_isr(struct device *unused) {
|
||||
static u8_t byte;
|
||||
static struct net_buf *buf = NULL;
|
||||
|
@ -526,7 +548,9 @@ void bt_uart_isr(struct device *unused)
|
|||
}
|
||||
}*/
|
||||
|
||||
static void unproc_thread(void) {
|
||||
static void
|
||||
unproc_thread(void)
|
||||
{
|
||||
struct net_buf *buf;
|
||||
k_thread_name_set(NULL, "unproc_thread");
|
||||
|
||||
|
@ -542,18 +566,22 @@ static void unproc_thread(void) {
|
|||
|
||||
//LOG_HEXDUMP_DBG((uint8_t*)&hdr, sizeof(hdr), "Header");
|
||||
if (hdr.is_reliable) {
|
||||
LOG_DBG("Header: seq %d, ack %d, rel: %s, has_crc16: %s", hdr.seq, hdr.ack,
|
||||
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");
|
||||
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);
|
||||
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.next_expected_seq_from_host = next_seq(hdr.seq);
|
||||
}
|
||||
|
||||
if (h5.link_state < ACTIVE && hdr.packet_type != HCI_3WIRE_LINK_PKT) {
|
||||
if (h5.link_state < ACTIVE
|
||||
&& hdr.packet_type != HCI_3WIRE_LINK_PKT) {
|
||||
LOG_WRN("Dropping unexpected packet type (%d) in non-ACTIVE state (%d)",
|
||||
hdr.packet_type, h5.link_state);
|
||||
goto next;
|
||||
|
@ -580,7 +608,8 @@ static void unproc_thread(void) {
|
|||
REBOOT_IF_NOT(rx_buf);
|
||||
break;
|
||||
default:
|
||||
LOG_ERR("Wrong packet type from host: %u", hdr.packet_type);
|
||||
LOG_ERR("Wrong packet type from host: %u",
|
||||
hdr.packet_type);
|
||||
goto next;
|
||||
}
|
||||
|
||||
|
@ -616,12 +645,13 @@ next:
|
|||
}
|
||||
}
|
||||
|
||||
static void h5_send_sync(void);
|
||||
static void h5_send_config(void);
|
||||
static void
|
||||
h5_send_sync(void);
|
||||
static void
|
||||
h5_send_config(void);
|
||||
|
||||
|
||||
|
||||
static void tx_thread(void)
|
||||
static void
|
||||
tx_thread(void)
|
||||
{
|
||||
k_thread_name_set(NULL, "tx_thread");
|
||||
|
||||
|
@ -640,7 +670,8 @@ static void tx_thread(void)
|
|||
k_sleep(250);
|
||||
break;
|
||||
case ACTIVE:
|
||||
while ((buf = net_buf_get(&h5.controller_queue, K_FOREVER))) {
|
||||
while ((buf = net_buf_get(&h5.controller_queue,
|
||||
K_FOREVER))) {
|
||||
bt_send(buf);
|
||||
}
|
||||
//LOG_DBG("controller_queue empty");
|
||||
|
@ -659,7 +690,9 @@ static void tx_thread(void)
|
|||
}
|
||||
}
|
||||
|
||||
static void event_thread(void) {
|
||||
static void
|
||||
event_thread(void)
|
||||
{
|
||||
static struct net_buf *buf = NULL;
|
||||
while (true) {
|
||||
buf = net_buf_get(&h5.event_queue, K_MSEC(1000));
|
||||
|
@ -671,7 +704,9 @@ static void event_thread(void) {
|
|||
}
|
||||
}
|
||||
|
||||
static void uart_tx_thread() {
|
||||
static void
|
||||
uart_tx_thread()
|
||||
{
|
||||
while (true) {
|
||||
struct net_buf *buf = net_buf_get(&h5.uart_tx_queue, K_FOREVER);
|
||||
uart_poll_out(hci_uart_dev, SLIP_DELIMITER);
|
||||
|
@ -680,10 +715,12 @@ static void uart_tx_thread() {
|
|||
h5_slip_byte(net_buf_pull_u8(buf));
|
||||
}
|
||||
uart_poll_out(hci_uart_dev, SLIP_DELIMITER);
|
||||
net_buf_unref(buf);
|
||||
}
|
||||
}
|
||||
|
||||
static void h5_init(void)
|
||||
static void
|
||||
h5_init(void)
|
||||
{
|
||||
LOG_DBG("h5_init");
|
||||
|
||||
|
@ -738,7 +775,8 @@ static void h5_init(void)
|
|||
//k_delayed_work_init(&retx_work, retx_timeout);
|
||||
}
|
||||
|
||||
static int h5_open(struct device *unused)
|
||||
static int
|
||||
h5_open(struct device *unused)
|
||||
{
|
||||
LOG_DBG("h5_open");
|
||||
|
||||
|
@ -781,31 +819,45 @@ enum h5_pkt_types {
|
|||
H5_CONFIG_RESPONSE,
|
||||
};
|
||||
|
||||
bool _link_ctrl_memcmp(struct net_buf const * buf, u8_t const * const ref) {
|
||||
bool
|
||||
_link_ctrl_memcmp(struct net_buf const *buf, u8_t const *const ref)
|
||||
{
|
||||
return !memcmp(buf->data, ref, 2);
|
||||
}
|
||||
|
||||
bool packet_is_sync(struct net_buf const *buf) {
|
||||
bool
|
||||
packet_is_sync(struct net_buf const *buf)
|
||||
{
|
||||
return _link_ctrl_memcmp(buf, sync_req);
|
||||
}
|
||||
|
||||
bool packet_is_sync_response(struct net_buf const *buf) {
|
||||
bool
|
||||
packet_is_sync_response(struct net_buf const *buf)
|
||||
{
|
||||
return _link_ctrl_memcmp(buf, sync_rsp);
|
||||
}
|
||||
|
||||
bool packet_is_config(struct net_buf const *buf) {
|
||||
bool
|
||||
packet_is_config(struct net_buf const *buf)
|
||||
{
|
||||
return _link_ctrl_memcmp(buf, conf_req);
|
||||
}
|
||||
|
||||
static bool packet_is_config_response(struct net_buf const *buf) {
|
||||
static bool
|
||||
packet_is_config_response(struct net_buf const *buf)
|
||||
{
|
||||
return _link_ctrl_memcmp(buf, conf_rsp);
|
||||
}
|
||||
|
||||
static void _send_link_control(u8_t const * const buf, u8_t len) {
|
||||
static void
|
||||
_send_link_control(u8_t const *const buf, u8_t len)
|
||||
{
|
||||
h5_send(buf, HCI_3WIRE_LINK_PKT, len);
|
||||
}
|
||||
|
||||
int packet_get_type(struct net_buf const * buf) {
|
||||
int
|
||||
packet_get_type(struct net_buf const *buf)
|
||||
{
|
||||
if (packet_is_sync(buf)) {
|
||||
return H5_SYNC;
|
||||
} else if (packet_is_sync_response(buf)) {
|
||||
|
@ -818,24 +870,34 @@ int packet_get_type(struct net_buf const * buf) {
|
|||
return H5_UNKNOWN;
|
||||
}
|
||||
|
||||
static void h5_send_sync(void) {
|
||||
static void
|
||||
h5_send_sync(void)
|
||||
{
|
||||
_send_link_control(sync_req, sizeof(sync_req));
|
||||
}
|
||||
static void h5_send_sync_response(void) {
|
||||
static void
|
||||
h5_send_sync_response(void)
|
||||
{
|
||||
_send_link_control(sync_rsp, sizeof(sync_rsp));
|
||||
}
|
||||
static void h5_send_config(void) {
|
||||
static void
|
||||
h5_send_config(void)
|
||||
{
|
||||
_send_link_control(conf_req, sizeof(conf_req));
|
||||
}
|
||||
static void h5_send_config_response(void) {
|
||||
static void
|
||||
h5_send_config_response(void)
|
||||
{
|
||||
_send_link_control(conf_rsp, sizeof(conf_rsp));
|
||||
}
|
||||
|
||||
static struct device *gpio_dev;
|
||||
static struct gpio_callback gpio_cb;
|
||||
|
||||
void gpio_callback(struct device *port,
|
||||
struct gpio_callback *cb, u32_t pins) {
|
||||
void
|
||||
gpio_callback(struct device *port,
|
||||
struct gpio_callback *cb, u32_t pins)
|
||||
{
|
||||
LOG_ERR("Host wants the bootloader, rebooting\n");
|
||||
k_sleep(250);
|
||||
sys_reboot(SYS_REBOOT_COLD);
|
||||
|
@ -843,18 +905,23 @@ void gpio_callback(struct device *port,
|
|||
|
||||
#define BOOTLOADER_REQ_GPIO_PIN 8
|
||||
#define BOOTLOADER_STATUS_GPIO_PIN 7
|
||||
void gpio_init() {
|
||||
void
|
||||
gpio_init()
|
||||
{
|
||||
int ret;
|
||||
gpio_dev = device_get_binding("GPIO_0");
|
||||
if (!gpio_dev) {
|
||||
LOG_ERR("Cannot find %s!\n", "GPIO_0");
|
||||
}
|
||||
ret = gpio_pin_configure(gpio_dev, BOOTLOADER_REQ_GPIO_PIN,
|
||||
(GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW | GPIO_PUD_PULL_UP));
|
||||
(GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW
|
||||
| GPIO_PUD_PULL_UP));
|
||||
if (ret) {
|
||||
LOG_ERR("Error configuring GPIO_%d!\n", BOOTLOADER_REQ_GPIO_PIN);
|
||||
LOG_ERR("Error configuring GPIO_%d!\n",
|
||||
BOOTLOADER_REQ_GPIO_PIN);
|
||||
}
|
||||
gpio_init_callback(&gpio_cb, gpio_callback, BIT(BOOTLOADER_REQ_GPIO_PIN));
|
||||
gpio_init_callback(&gpio_cb, gpio_callback,
|
||||
BIT(BOOTLOADER_REQ_GPIO_PIN));
|
||||
|
||||
ret = gpio_add_callback(gpio_dev, &gpio_cb);
|
||||
if (ret) {
|
||||
|
@ -868,7 +935,9 @@ void gpio_init() {
|
|||
|
||||
}
|
||||
|
||||
static h5_linkstate_t do_uninit(struct net_buf const *buf) {
|
||||
static h5_linkstate_t
|
||||
do_uninit(struct net_buf const *buf)
|
||||
{
|
||||
switch (packet_get_type(buf)) {
|
||||
case H5_SYNC:
|
||||
LOG_DBG("RX'd SYNC: UNINIT -> UNINIT");
|
||||
|
@ -886,7 +955,9 @@ static h5_linkstate_t do_uninit(struct net_buf const *buf) {
|
|||
}
|
||||
}
|
||||
|
||||
static h5_linkstate_t do_init(struct net_buf const *buf) {
|
||||
static h5_linkstate_t
|
||||
do_init(struct net_buf const *buf)
|
||||
{
|
||||
switch (packet_get_type(buf)) {
|
||||
case H5_SYNC:
|
||||
LOG_DBG("RX'd SYNC: INIT -> INIT");
|
||||
|
@ -900,7 +971,8 @@ static h5_linkstate_t do_init(struct net_buf const *buf) {
|
|||
h5.tx_win = conf_rsp[2] & 0x7;
|
||||
h5.next_expected_seq_from_host = 0;
|
||||
h5.local_seq = 0;
|
||||
LOG_DBG("Finished H5 configuration, tx_win %u: INIT -> ACTIVE", h5.tx_win);
|
||||
LOG_DBG("Finished H5 configuration, tx_win %u: INIT -> ACTIVE",
|
||||
h5.tx_win);
|
||||
return ACTIVE;
|
||||
default:
|
||||
LOG_DBG("RX'd Other: INIT -> INIT");
|
||||
|
@ -908,7 +980,9 @@ static h5_linkstate_t do_init(struct net_buf const *buf) {
|
|||
}
|
||||
}
|
||||
|
||||
static h5_linkstate_t do_active(struct net_buf *buf) {
|
||||
static h5_linkstate_t
|
||||
do_active(struct net_buf *buf)
|
||||
{
|
||||
switch (packet_get_type(buf)) {
|
||||
case H5_SYNC:
|
||||
LOG_WRN("SYNC in ACTIVE state, peer reset: ACTIVE -> UNINIT");
|
||||
|
@ -930,13 +1004,16 @@ static h5_linkstate_t do_active(struct net_buf *buf) {
|
|||
if (bt_buf_get_type(buf) == BT_BUF_EVT) {
|
||||
h5_send(buf->data, HCI_EVENT_PKT, buf->len);
|
||||
} else {
|
||||
LOG_HEXDUMP_ERR(buf->data, buf->len, "Unexpected buffer in host_queue");
|
||||
LOG_HEXDUMP_ERR(buf->data, buf->len,
|
||||
"Unexpected buffer in host_queue");
|
||||
}
|
||||
return ACTIVE;
|
||||
}
|
||||
}
|
||||
|
||||
void state_change(int old, int new) {
|
||||
void
|
||||
state_change(int old, int new)
|
||||
{
|
||||
if (old == new) {
|
||||
return;
|
||||
}
|
||||
|
@ -948,7 +1025,8 @@ void state_change(int old, int new) {
|
|||
}
|
||||
}
|
||||
|
||||
void main(void)
|
||||
void
|
||||
main(void)
|
||||
{
|
||||
LOG_DBG("Start");
|
||||
gpio_init();
|
||||
|
|
Loading…
Reference in New Issue