kfsw-comms/src/uart_codec.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <string.h> | ||
| 3 | |||
| 4 | #include <zephyr/kernel.h> | ||
| 5 | #include <csp/csp.h> | ||
| 6 | #include <csp/csp_buffer.h> | ||
| 7 | #include <kfsw/comms/csp.h> | ||
| 8 | #include <kfsw/comms/uart_codec.h> | ||
| 9 | |||
| 10 | static const struct kfsw_uart_codec *codec; | ||
| 11 | static char selected[CSP_IFLIST_NAME_MAX]; | ||
| 12 | static csp_iface_t *bound; | ||
| 13 | static nexthop_t original_tx; | ||
| 14 | K_MSGQ_DEFINE(codec_packets, sizeof(csp_packet_t *), CONFIG_KFSW_CSP_UART_CODEC_QUEUE_DEPTH, | ||
| 15 | sizeof(void *)); | ||
| 16 | BUILD_ASSERT(CONFIG_KFSW_CSP_UART_CODEC_PRIORITY > CONFIG_KFSW_CSP_ROUTER_PRIORITY && | ||
| 17 | CONFIG_KFSW_CSP_UART_CODEC_PRIORITY < CONFIG_NUM_PREEMPT_PRIORITIES, | ||
| 18 | "The UART codec must run below the CSP router"); | ||
| 19 | BUILD_ASSERT(CONFIG_KFSW_CSP_UART_CODEC_QUEUE_DEPTH + 4 <= CSP_BUFFER_COUNT, | ||
| 20 | "Reserve packet buffers outside the codec queue"); | ||
| 21 | |||
| 22 | 2 | static void receive_worker(void *a, void *b, void *c) | |
| 23 | { | ||
| 24 | ARG_UNUSED(a); | ||
| 25 | ARG_UNUSED(b); | ||
| 26 | ARG_UNUSED(c); | ||
| 27 | 10 | for (;;) { | |
| 28 | csp_packet_t *packet; | ||
| 29 | |||
| 30 | 12 | k_msgq_get(&codec_packets, &packet, K_FOREVER); | |
| 31 | 10 | int result = codec->decode(packet); | |
| 32 | |||
| 33 | 10 | if (result == 0) { | |
| 34 | ✗ | csp_qfifo_write(packet, bound, NULL); | |
| 35 | } else { | ||
| 36 | 10 | if (result < 0) { | |
| 37 | ✗ | bound->drop++; | |
| 38 | } | ||
| 39 | 10 | csp_buffer_free(packet); | |
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | K_THREAD_DEFINE(kfsw_uart_codec_rx, CONFIG_KFSW_CSP_UART_CODEC_STACK_SIZE, receive_worker, NULL, | ||
| 45 | NULL, NULL, CONFIG_KFSW_CSP_UART_CODEC_PRIORITY, 0, SYS_FOREVER_MS); | ||
| 46 | |||
| 47 | 4 | static int transmit(csp_iface_t *iface, uint16_t via, csp_packet_t *packet, int from_me) | |
| 48 | { | ||
| 49 | 4 | int result = codec->encode(packet); | |
| 50 | |||
| 51 | /* libcsp frees rejected TX packets; the next-hop call frees accepted ones. */ | ||
| 52 | 4 | return result == 0 ? original_tx(iface, via, packet, from_me) : CSP_ERR_TX; | |
| 53 | } | ||
| 54 | |||
| 55 | 8 | int kfsw_uart_codec_register(const char *name, const struct kfsw_uart_codec *implementation) | |
| 56 | { | ||
| 57 | struct kfsw_csp_info info; | ||
| 58 | 8 | kfsw_csp_get_info(&info); | |
| 59 | 8 | if (name == NULL || implementation == NULL || implementation->encode == NULL || | |
| 60 | 6 | implementation->decode == NULL || name[0] == '\0' || strlen(name) >= sizeof(selected)) { | |
| 61 | 4 | return -EINVAL; | |
| 62 | } | ||
| 63 | 4 | if (codec != NULL || info.initialized) { | |
| 64 | 2 | return -EALREADY; | |
| 65 | } | ||
| 66 | 2 | strcpy(selected, name); | |
| 67 | 2 | codec = implementation; | |
| 68 | 2 | return 0; | |
| 69 | } | ||
| 70 | |||
| 71 | 6 | int kfsw_uart_codec_check(void) | |
| 72 | { | ||
| 73 | 6 | return codec != NULL && bound == NULL ? -ENODEV : 0; | |
| 74 | } | ||
| 75 | |||
| 76 | 6 | int kfsw_uart_codec_attach(csp_iface_t *iface) | |
| 77 | { | ||
| 78 | 6 | if (codec == NULL || strcmp(iface->name, selected) != 0) { | |
| 79 | 2 | return 0; | |
| 80 | } | ||
| 81 | 4 | if (bound != NULL) { | |
| 82 | 2 | return -EALREADY; | |
| 83 | } | ||
| 84 | 2 | bound = iface; | |
| 85 | 2 | original_tx = iface->nexthop; | |
| 86 | 2 | iface->nexthop = transmit; | |
| 87 | 2 | k_thread_start(kfsw_uart_codec_rx); | |
| 88 | 2 | return 0; | |
| 89 | } | ||
| 90 | |||
| 91 | /* Only the maintained KISS decoder calls this entry point. The compile-time | ||
| 92 | * redirect leaves its framing and CRC checks intact and avoids vendor edits. | ||
| 93 | */ | ||
| 94 | 16 | void kfsw_uart_codec_receive(csp_packet_t *packet, csp_iface_t *iface, void *task_woken) | |
| 95 | { | ||
| 96 | 16 | if (codec == NULL || strcmp(iface->name, selected) != 0) { | |
| 97 | ✗ | csp_qfifo_write(packet, iface, task_woken); | |
| 98 | ✗ | return; | |
| 99 | } | ||
| 100 | 16 | if (k_msgq_put(&codec_packets, &packet, K_NO_WAIT) != 0) { | |
| 101 | 6 | iface->drop++; | |
| 102 | 6 | if (task_woken != NULL) { | |
| 103 | ✗ | csp_buffer_free_isr(packet); | |
| 104 | } else { | ||
| 105 | 6 | csp_buffer_free(packet); | |
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | 4 | int kfsw_uart_codec_control(uint16_t peer, const uint8_t *data, size_t size) | |
| 111 | { | ||
| 112 | 6 | if (bound == NULL || data == NULL || size > CSP_BUFFER_SIZE - sizeof(uint32_t) || | |
| 113 | 2 | k_is_in_isr()) { | |
| 114 | 2 | return -EINVAL; | |
| 115 | } | ||
| 116 | 2 | csp_packet_t *packet = csp_buffer_get(size); | |
| 117 | |||
| 118 | 2 | if (packet == NULL) { | |
| 119 | ✗ | return -ENOBUFS; | |
| 120 | } | ||
| 121 | 2 | packet->id = (csp_id_t){.src = bound->addr, .dst = peer}; | |
| 122 | 2 | packet->length = (uint16_t)size; | |
| 123 | 2 | memcpy(packet->data, data, size); | |
| 124 | 2 | int result = original_tx(bound, CSP_NO_VIA_ADDRESS, packet, 1); | |
| 125 | |||
| 126 | 2 | if (result != CSP_ERR_NONE) { | |
| 127 | ✗ | csp_buffer_free(packet); | |
| 128 | ✗ | bound->tx_error++; | |
| 129 | ✗ | return -EIO; | |
| 130 | } | ||
| 131 | 2 | bound->tx++; | |
| 132 | 2 | bound->txbytes += (uint32_t)size; | |
| 133 | 2 | return 0; | |
| 134 | } | ||
| 135 |