diff --git a/include/hdlc.h b/include/hdlc.h index 3ab4390aec5c1ca8ddcda98ca1b3f073732b94ce..3e3cf79685622e58dbae6fd94b8f95db58d9a193 100644 --- a/include/hdlc.h +++ b/include/hdlc.h @@ -1,8 +1,7 @@ -#ifndef HDLC_H -#define HDLC_H +#ifndef _HDLC_H_ +#define _HDLC_H_ #include "operations.h" -#include <stddef.h> #include <stdint.h> #include <zephyr/device.h> @@ -17,8 +16,15 @@ static const struct device *const uart_dev = DEVICE_DT_GET(UART_DEVICE_NODE); typedef void (*greybus_message_callback)(struct gb_message *); +/* + * HDLC block + * + * @param HDLC address + * @param HDLC control + * @param hdlc block buffer length + * @param hdlc block buffer + */ struct hdlc_block { - void *fifo_reserved; uint8_t address; uint8_t control; uint8_t length; @@ -32,18 +38,6 @@ struct hdlc_block { */ int hdlc_init(); -/* - * Submit an HDLC block to be transmitted. This is async - * - * @param buffer - * @param buffer_length - * @param address - * @param control - * - * @return block size (> 0) if successful. Negative in case of error - */ -int hdlc_block_submit(uint8_t *, size_t, uint8_t, uint8_t); - /* * Submit an HDLC Block syncronously * diff --git a/include/svc.h b/include/svc.h index 51dd07efb21426a312407b822f6a9aac8c4fa5a3..97073f5a5a6f4619625cf65e8f46a073b27c279d 100644 --- a/include/svc.h +++ b/include/svc.h @@ -1,5 +1,5 @@ -#ifndef SVC_H -#define SVC_H +#ifndef _SVC_H_ +#define _SVC_H_ #include "operations.h" diff --git a/src/hdlc.c b/src/hdlc.c index 22723c41c179f46450068ae5392c3b4c7c95a37f..6ceffaac8f1fafe919be1cb7f18ca2386182d1cd 100644 --- a/src/hdlc.c +++ b/src/hdlc.c @@ -18,13 +18,11 @@ #define HDLC_ESC_FRAME 0x5E #define HDLC_ESC_ESC 0x5D -static void hdlc_tx_handler(struct k_work *); static void hdlc_rx_handler(struct k_work *); LOG_MODULE_DECLARE(cc1352_greybus, CONFIG_BEAGLEPLAY_GREYBUS_LOG_LEVEL); -K_FIFO_DEFINE(hdlc_tx_queue); -K_WORK_DEFINE(hdlc_tx_work, hdlc_tx_handler); +// TODO: Probably switch to a higher priority thread than apbridge K_WORK_DEFINE(hdlc_rx_work, hdlc_rx_handler); RING_BUF_DECLARE(hdlc_rx_ringbuf, HDLC_RX_BUF_SIZE); @@ -72,8 +70,6 @@ static void block_out(struct hdlc_driver *drv, const struct hdlc_block *block) { uart_poll_out(uart_dev, HDLC_FRAME); } -static void hdlc_dealloc_block(struct hdlc_block *block) { k_free(block); } - static void hdlc_process_greybus_frame(struct hdlc_driver *drv, const char *buffer, size_t buffer_len) { // Do something with hdlc information. Starts at hdlc->rx_buffer[3] @@ -190,15 +186,6 @@ static void hdlc_rx_input_byte(struct hdlc_driver *drv, uint8_t byte) { } } -static void hdlc_tx_handler(struct k_work *work) { - struct hdlc_block *block = k_fifo_get(&hdlc_tx_queue, K_NO_WAIT); - while (block) { - block_out(&hdlc_driver, block); - hdlc_dealloc_block(block); - block = k_fifo_get(&hdlc_tx_queue, K_NO_WAIT); - } -} - static int hdlc_process_buffer(uint8_t *buf, size_t len) { for (size_t i = 0; i < len; ++i) { hdlc_rx_input_byte(&hdlc_driver, buf[i]); @@ -223,30 +210,27 @@ static void hdlc_rx_handler(struct k_work *work) { } } -int hdlc_block_send_sync(const uint8_t *buffer, size_t buffer_len, - uint8_t address, uint8_t control) { - size_t block_size = sizeof(struct hdlc_block) + sizeof(uint8_t) * buffer_len; - struct hdlc_block *block = k_malloc(block_size); - - if (block == NULL) { - return -1; - } +static int smp_hdlc_tx_cb(const void *data, int len) { + hdlc_block_send_sync(data, len, ADDRESS_MCUMGR, 0x03); + return 0; +} - block->length = buffer_len; - memcpy(block->buffer, buffer, buffer_len); - block->address = address; - block->control = control; +static int smp_hdlc_tx_pkt(struct net_buf *nb) { + int rc; - block_out(&hdlc_driver, block); + rc = mcumgr_serial_tx_pkt(nb->data, nb->len, smp_hdlc_tx_cb); + smp_packet_free(nb); - return block_size; + LOG_DBG("SMP TX %d", rc); + return rc; } -int hdlc_block_submit(uint8_t *buffer, size_t buffer_len, uint8_t address, - uint8_t control) { +static uint16_t smp_hdlc_get_mtu(const struct net_buf *nb) { return 256; } + +int hdlc_block_send_sync(const uint8_t *buffer, size_t buffer_len, + uint8_t address, uint8_t control) { size_t block_size = sizeof(struct hdlc_block) + sizeof(uint8_t) * buffer_len; struct hdlc_block *block = k_malloc(block_size); - if (block == NULL) { return -1; } @@ -256,29 +240,13 @@ int hdlc_block_submit(uint8_t *buffer, size_t buffer_len, uint8_t address, block->address = address; block->control = control; - k_fifo_put(&hdlc_tx_queue, block); - k_work_submit(&hdlc_tx_work); - - return block_size; -} - -static int smp_hdlc_tx_cb(const void *data, int len) { - hdlc_block_send_sync(data, len, ADDRESS_MCUMGR, 0x03); - return 0; -} - -static int smp_hdlc_tx_pkt(struct net_buf *nb) { - int rc; + block_out(&hdlc_driver, block); - rc = mcumgr_serial_tx_pkt(nb->data, nb->len, smp_hdlc_tx_cb); - smp_packet_free(nb); + k_free(block); - LOG_DBG("SMP TX %d", rc); - return rc; + return block_size; } -static uint16_t smp_hdlc_get_mtu(const struct net_buf *nb) { return 256; } - int hdlc_init() { int rc;