Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 1 | /*! \file soft_uart.c |
| 2 | * Software UART implementation. */ |
| 3 | /* |
| 4 | * (C) 2022 by Harald Welte <laforge@gnumonks.org> |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 5 | * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdbool.h> |
| 24 | #include <stdint.h> |
| 25 | #include <errno.h> |
| 26 | |
Vadim Yanitskiy | 459cb06 | 2023-11-21 20:12:36 +0700 | [diff] [blame] | 27 | #include <osmocom/core/utils.h> |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 28 | #include <osmocom/core/timer.h> |
| 29 | #include <osmocom/core/soft_uart.h> |
| 30 | |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 31 | /*! Rx/Tx flow state of a soft-UART */ |
| 32 | enum suart_flow_state { |
| 33 | SUART_FLOW_ST_IDLE, /*!< waiting for a start bit or Tx data */ |
| 34 | SUART_FLOW_ST_DATA, /*!< receiving/transmitting data bits */ |
| 35 | SUART_FLOW_ST_PARITY, /*!< receiving/transmitting parity bits */ |
| 36 | SUART_FLOW_ST_STOP, /*!< receiving/transmitting stop bits */ |
| 37 | }; |
| 38 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 39 | /*! Internal state of a soft-UART */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 40 | struct osmo_soft_uart { |
| 41 | struct osmo_soft_uart_cfg cfg; |
| 42 | const char *name; |
| 43 | struct { |
| 44 | bool running; |
| 45 | uint8_t bit_count; |
| 46 | uint8_t shift_reg; |
| 47 | struct msgb *msg; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 48 | ubit_t parity_bit; /* 0 (even) / 1 (odd) */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 49 | unsigned int flags; |
| 50 | unsigned int status; |
| 51 | struct osmo_timer_list timer; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 52 | enum suart_flow_state flow_state; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 53 | } rx; |
| 54 | struct { |
| 55 | bool running; |
| 56 | uint8_t bit_count; |
| 57 | uint8_t shift_reg; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 58 | ubit_t parity_bit; /* 0 (even) / 1 (odd) */ |
| 59 | enum suart_flow_state flow_state; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 60 | } tx; |
| 61 | }; |
| 62 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 63 | /*! Default soft-UART configuration (8-N-1) */ |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 64 | const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg = { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 65 | .num_data_bits = 8, |
| 66 | .num_stop_bits = 1, |
| 67 | .parity_mode = OSMO_SUART_PARITY_NONE, |
| 68 | .rx_buf_size = 1024, |
| 69 | .rx_timeout_ms = 100, |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | /************************************************************************* |
| 73 | * Receiver |
| 74 | *************************************************************************/ |
| 75 | |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 76 | /*! Flush the receive buffer, passing ownership of the msgb to the .rx_cb(). |
| 77 | * \param[in] suart soft-UART instance holding the receive buffer. */ |
| 78 | void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 79 | { |
| 80 | if ((suart->rx.msg && msgb_length(suart->rx.msg)) || suart->rx.flags) { |
| 81 | osmo_timer_del(&suart->rx.timer); |
| 82 | if (suart->cfg.rx_cb) { |
| 83 | suart->cfg.rx_cb(suart->cfg.priv, suart->rx.msg, suart->rx.flags); |
| 84 | /* call-back has taken ownership of msgb, no need to free() here */ |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 85 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart_rx"); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 86 | } else { |
| 87 | msgb_reset(suart->rx.msg); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* one character was received; add to receive buffer and notify user, if needed */ |
| 93 | static void suart_rx_ch(struct osmo_soft_uart *suart, uint8_t ch) |
| 94 | { |
| 95 | unsigned int msg_len; |
| 96 | |
| 97 | OSMO_ASSERT(suart->rx.msg); |
| 98 | msgb_put_u8(suart->rx.msg, ch); |
| 99 | msg_len = msgb_length(suart->rx.msg); |
| 100 | |
Vadim Yanitskiy | bf95f82 | 2023-11-18 01:46:16 +0700 | [diff] [blame] | 101 | if (msg_len >= suart->cfg.rx_buf_size || suart->rx.flags) { |
| 102 | /* either the buffer is full, or we hit a parity and/or a framing error */ |
| 103 | osmo_soft_uart_flush_rx(suart); |
| 104 | } else if (msg_len == 1) { |
| 105 | /* first character in new message: start timer */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 106 | osmo_timer_schedule(&suart->rx.timer, suart->cfg.rx_timeout_ms / 1000, |
| 107 | (suart->cfg.rx_timeout_ms % 1000) * 1000); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | /* receive a single bit */ |
| 112 | static inline void osmo_uart_rx_bit(struct osmo_soft_uart *suart, const ubit_t bit) |
| 113 | { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 114 | if (!suart->rx.running) |
| 115 | return; |
| 116 | |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 117 | switch (suart->rx.flow_state) { |
| 118 | case SUART_FLOW_ST_IDLE: |
| 119 | if (bit == 0) { /* start bit condition */ |
| 120 | suart->rx.flow_state = SUART_FLOW_ST_DATA; |
| 121 | suart->rx.flags = 0x00; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 122 | suart->rx.shift_reg = 0; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 123 | suart->rx.bit_count = 0; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 124 | suart->rx.parity_bit = 0; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 125 | } |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 126 | break; |
| 127 | case SUART_FLOW_ST_DATA: |
| 128 | suart->rx.bit_count++; |
| 129 | suart->rx.shift_reg >>= 1; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 130 | if (bit != 0) { |
| 131 | suart->rx.parity_bit = !suart->rx.parity_bit; /* flip */ |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 132 | suart->rx.shift_reg |= 0x80; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 133 | } |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 134 | if (suart->rx.bit_count >= suart->cfg.num_data_bits) { |
| 135 | /* we have accumulated enough data bits */ |
| 136 | if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE) |
| 137 | suart->rx.flow_state = SUART_FLOW_ST_PARITY; |
| 138 | else |
| 139 | suart->rx.flow_state = SUART_FLOW_ST_STOP; |
Vadim Yanitskiy | 2d2ce49 | 2023-11-19 15:37:10 +0700 | [diff] [blame] | 140 | /* align the register if needed */ |
| 141 | if (suart->cfg.num_data_bits < 8) |
| 142 | suart->rx.shift_reg >>= (8 - suart->cfg.num_data_bits); |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 143 | } |
| 144 | break; |
| 145 | case SUART_FLOW_ST_PARITY: |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 146 | switch (suart->cfg.parity_mode) { |
| 147 | case OSMO_SUART_PARITY_EVEN: |
| 148 | /* number of 1-bits (in both data and parity) shall be even */ |
| 149 | if (suart->rx.parity_bit != bit) |
| 150 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 151 | break; |
| 152 | case OSMO_SUART_PARITY_ODD: |
| 153 | /* number of 1-bits (in both data and parity) shall be odd */ |
| 154 | if (suart->rx.parity_bit == bit) |
| 155 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 156 | break; |
Vadim Yanitskiy | 0d78a00 | 2023-11-19 16:15:38 +0700 | [diff] [blame] | 157 | case OSMO_SUART_PARITY_MARK: |
| 158 | /* parity bit must always be 1 */ |
| 159 | if (bit != 1) |
| 160 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 161 | break; |
| 162 | case OSMO_SUART_PARITY_SPACE: |
| 163 | /* parity bit must always be 0 */ |
| 164 | if (bit != 0) |
| 165 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 166 | break; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 167 | case OSMO_SUART_PARITY_NONE: /* shall not happen */ |
| 168 | default: |
| 169 | OSMO_ASSERT(0); |
| 170 | } |
| 171 | |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 172 | suart->rx.flow_state = SUART_FLOW_ST_STOP; |
| 173 | break; |
| 174 | case SUART_FLOW_ST_STOP: |
| 175 | suart->rx.bit_count++; |
| 176 | if (bit != 1) |
| 177 | suart->rx.flags |= OSMO_SUART_F_FRAMING_ERROR; |
| 178 | |
| 179 | if (suart->rx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) { |
| 180 | /* we have accumulated enough stop bits */ |
| 181 | suart_rx_ch(suart, suart->rx.shift_reg); |
| 182 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
| 183 | } |
| 184 | break; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | /* receive timer expiration: flush rx-buffer to user call-back */ |
| 189 | static void suart_rx_timer_cb(void *data) |
| 190 | { |
| 191 | struct osmo_soft_uart *suart = data; |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 192 | osmo_soft_uart_flush_rx(suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 195 | /*! Feed a number of unpacked bits into the soft-UART receiver. |
| 196 | * \param[in] suart soft-UART instance to feed bits into. |
| 197 | * \param[in] ubits pointer to the unpacked bits. |
| 198 | * \param[in] n_ubits number of unpacked bits to be fed. |
| 199 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 200 | int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits) |
| 201 | { |
| 202 | for (size_t i = 0; i < n_ubits; i++) |
| 203 | osmo_uart_rx_bit(suart, ubits[i]); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /************************************************************************* |
| 208 | * Transmitter |
| 209 | *************************************************************************/ |
| 210 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 211 | /* pull a single bit out of the UART transmitter */ |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 212 | static inline ubit_t osmo_uart_tx_bit(struct osmo_soft_uart *suart, struct msgb *msg) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 213 | { |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 214 | ubit_t tx_bit = 1; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 215 | |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 216 | if (!suart->tx.running) |
| 217 | return tx_bit; |
| 218 | |
| 219 | switch (suart->tx.flow_state) { |
| 220 | case SUART_FLOW_ST_IDLE: |
Vadim Yanitskiy | ffdd997 | 2023-11-21 02:25:32 +0700 | [diff] [blame] | 221 | if (msg && msgb_length(msg) > 0) { /* if we have pending data */ |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 222 | suart->tx.shift_reg = msgb_pull_u8(msg); |
| 223 | suart->tx.flow_state = SUART_FLOW_ST_DATA; |
| 224 | suart->tx.bit_count = 0; |
| 225 | suart->tx.parity_bit = 0; |
| 226 | tx_bit = 0; |
| 227 | } |
| 228 | break; |
| 229 | case SUART_FLOW_ST_DATA: |
| 230 | tx_bit = suart->tx.shift_reg & 1; |
| 231 | suart->tx.parity_bit ^= tx_bit; |
| 232 | suart->tx.shift_reg >>= 1; |
| 233 | suart->tx.bit_count++; |
| 234 | if (suart->tx.bit_count >= suart->cfg.num_data_bits) { |
| 235 | /* we have transmitted all data bits */ |
| 236 | if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE) |
| 237 | suart->tx.flow_state = SUART_FLOW_ST_PARITY; |
| 238 | else |
| 239 | suart->tx.flow_state = SUART_FLOW_ST_STOP; |
| 240 | } |
| 241 | break; |
| 242 | case SUART_FLOW_ST_PARITY: |
| 243 | switch (suart->cfg.parity_mode) { |
| 244 | case OSMO_SUART_PARITY_EVEN: |
| 245 | /* number of 1-bits (in both data and parity) shall be even */ |
| 246 | tx_bit = suart->tx.parity_bit; |
| 247 | break; |
| 248 | case OSMO_SUART_PARITY_ODD: |
| 249 | /* number of 1-bits (in both data and parity) shall be odd */ |
| 250 | tx_bit = !suart->tx.parity_bit; |
| 251 | break; |
Vadim Yanitskiy | 0d78a00 | 2023-11-19 16:15:38 +0700 | [diff] [blame] | 252 | case OSMO_SUART_PARITY_MARK: |
| 253 | /* parity bit must always be 1 */ |
| 254 | tx_bit = 1; |
| 255 | break; |
| 256 | case OSMO_SUART_PARITY_SPACE: |
| 257 | /* parity bit must always be 0 */ |
| 258 | tx_bit = 0; |
| 259 | break; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 260 | case OSMO_SUART_PARITY_NONE: |
| 261 | default: /* shall not happen */ |
| 262 | OSMO_ASSERT(0); |
| 263 | } |
| 264 | |
| 265 | suart->tx.flow_state = SUART_FLOW_ST_STOP; |
| 266 | break; |
| 267 | case SUART_FLOW_ST_STOP: |
| 268 | suart->tx.bit_count++; |
| 269 | if (suart->tx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) { |
| 270 | /* we have transmitted all stop bits, we're done */ |
| 271 | suart->tx.flow_state = SUART_FLOW_ST_IDLE; |
| 272 | } |
| 273 | break; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 274 | } |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 275 | |
| 276 | return tx_bit; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 279 | /*! Pull a number of unpacked bits out of the soft-UART transmitter. |
| 280 | * \param[in] suart soft-UART instance to pull the bits from. |
| 281 | * \param[out] ubits pointer to a buffer where to store pulled bits. |
| 282 | * \param[in] n_ubits number of unpacked bits to be pulled. |
Vadim Yanitskiy | 0887188 | 2023-11-30 05:22:40 +0700 | [diff] [blame^] | 283 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 284 | int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits) |
| 285 | { |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 286 | const struct osmo_soft_uart_cfg *cfg = &suart->cfg; |
Vadim Yanitskiy | ffdd997 | 2023-11-21 02:25:32 +0700 | [diff] [blame] | 287 | size_t n_frame_bits, n_chars; |
| 288 | struct msgb *msg = NULL; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 289 | |
Vadim Yanitskiy | 459cb06 | 2023-11-21 20:12:36 +0700 | [diff] [blame] | 290 | if (OSMO_UNLIKELY(n_ubits == 0)) |
| 291 | return -EINVAL; |
| 292 | |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 293 | /* calculate UART frame size for the effective config */ |
| 294 | n_frame_bits = 1 + cfg->num_data_bits + cfg->num_stop_bits; |
| 295 | if (cfg->parity_mode != OSMO_SUART_PARITY_NONE) |
| 296 | n_frame_bits += 1; |
| 297 | |
Vadim Yanitskiy | ffdd997 | 2023-11-21 02:25:32 +0700 | [diff] [blame] | 298 | /* calculate the number of characters we can fit into n_ubits */ |
| 299 | n_chars = n_ubits / n_frame_bits; |
| 300 | if (n_chars == 0) { |
| 301 | /* we can transmit at least one character */ |
| 302 | if (suart->tx.flow_state == SUART_FLOW_ST_IDLE) |
| 303 | n_chars = 1; |
| 304 | } |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 305 | |
Vadim Yanitskiy | ffdd997 | 2023-11-21 02:25:32 +0700 | [diff] [blame] | 306 | if (n_chars > 0) { |
| 307 | /* allocate a Tx buffer msgb */ |
| 308 | msg = msgb_alloc_c(suart, n_chars, "soft_uart_tx"); |
| 309 | OSMO_ASSERT(msg != NULL); |
| 310 | |
| 311 | /* call the .tx_cb() to populate the Tx buffer */ |
| 312 | OSMO_ASSERT(cfg->tx_cb != NULL); |
| 313 | suart->cfg.tx_cb(cfg->priv, msg); |
| 314 | } |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 315 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 316 | for (size_t i = 0; i < n_ubits; i++) |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 317 | ubits[i] = osmo_uart_tx_bit(suart, msg); |
| 318 | msgb_free(msg); |
| 319 | |
| 320 | return 0; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 321 | } |
| 322 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 323 | /*! Set the modem status lines of the given soft-UART. |
| 324 | * \param[in] suart soft-UART instance to update the modem status. |
| 325 | * \param[in] status mask of osmo_soft_uart_status. |
| 326 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 327 | int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status) |
| 328 | { |
| 329 | /* FIXME: Tx */ |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | /************************************************************************* |
| 335 | * Management / Initialization |
| 336 | *************************************************************************/ |
| 337 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 338 | /*! Allocate a soft-UART instance. |
| 339 | * \param[in] ctx parent talloc context. |
| 340 | * \param[in] name name of the soft-UART instance. |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 341 | * \param[in] cfg initial configuration of the soft-UART instance. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 342 | * \returns pointer to allocated soft-UART instance; NULL on error. */ |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 343 | struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name, |
| 344 | const struct osmo_soft_uart_cfg *cfg) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 345 | { |
| 346 | struct osmo_soft_uart *suart = talloc_zero(ctx, struct osmo_soft_uart); |
| 347 | if (!suart) |
| 348 | return NULL; |
| 349 | suart->name = talloc_strdup(suart, name); |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 350 | |
| 351 | OSMO_ASSERT(cfg != NULL); |
| 352 | suart->cfg = *cfg; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 353 | |
| 354 | return suart; |
| 355 | } |
| 356 | |
Vadim Yanitskiy | 877cfed | 2023-11-12 17:21:31 +0700 | [diff] [blame] | 357 | /*! Release memory taken by the given soft-UART. |
| 358 | * \param[in] suart soft-UART instance to be free()d. */ |
| 359 | void osmo_soft_uart_free(struct osmo_soft_uart *suart) |
| 360 | { |
| 361 | if (suart == NULL) |
| 362 | return; |
| 363 | |
| 364 | osmo_timer_del(&suart->rx.timer); |
| 365 | msgb_free(suart->rx.msg); |
| 366 | |
| 367 | talloc_free((void *)suart->name); |
| 368 | talloc_free(suart); |
| 369 | } |
| 370 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 371 | /*! Change soft-UART configuration to the user-provided config. |
| 372 | * \param[in] suart soft-UART instance to be re-configured. |
| 373 | * \param[in] cfg the user-provided config to be applied. |
| 374 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 375 | int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg) |
| 376 | { |
| 377 | /* consistency checks on the configuration */ |
| 378 | if (cfg->num_data_bits > 8 || cfg->num_data_bits == 0) |
| 379 | return -EINVAL; |
| 380 | if (cfg->num_stop_bits == 0) |
| 381 | return -EINVAL; |
| 382 | if (cfg->parity_mode < 0 || cfg->parity_mode >= _OSMO_SUART_PARITY_NUM) |
| 383 | return -EINVAL; |
| 384 | if (cfg->rx_buf_size == 0) |
| 385 | return -EINVAL; |
| 386 | |
| 387 | if (suart->cfg.rx_buf_size > cfg->rx_buf_size || |
| 388 | suart->cfg.rx_timeout_ms > cfg->rx_timeout_ms) { |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 389 | osmo_soft_uart_flush_rx(suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | suart->cfg = *cfg; |
| 393 | |
| 394 | osmo_timer_setup(&suart->rx.timer, suart_rx_timer_cb, suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
Vadim Yanitskiy | 59afb8f | 2023-11-29 23:58:10 +0700 | [diff] [blame] | 399 | /*! Get a name for the given soft-UART instance. |
| 400 | * \param[in] suart soft-UART instance to get the name from. |
| 401 | * \returns name of the given soft-UART instance. */ |
| 402 | const char *osmo_soft_uart_get_name(const struct osmo_soft_uart *suart) |
| 403 | { |
| 404 | return suart->name; |
| 405 | } |
| 406 | |
| 407 | /*! Set a new name for the given soft-UART instance. |
| 408 | * \param[in] suart soft-UART instance to set the name for. |
| 409 | * \param[in] name the new name. */ |
| 410 | void osmo_soft_uart_set_name(struct osmo_soft_uart *suart, const char *name) |
| 411 | { |
| 412 | osmo_talloc_replace_string(suart, (char **)&suart->name, name); |
| 413 | } |
| 414 | |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 415 | /*! Enable/disable receiver of the given soft-UART. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 416 | * \param[in] suart soft-UART instance to be re-configured. |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 417 | * \param[in] enable enable/disable state of the receiver. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 418 | * \returns 0 on success; negative on error. */ |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 419 | int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 420 | { |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 421 | if (!enable && suart->rx.running) { |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 422 | osmo_soft_uart_flush_rx(suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 423 | suart->rx.running = false; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 424 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 425 | } else if (enable && !suart->rx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 426 | if (!suart->rx.msg) |
| 427 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx"); |
| 428 | suart->rx.running = true; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 429 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | /*! Enable/disable transmitter of the given soft-UART. |
| 436 | * \param[in] suart soft-UART instance to be re-configured. |
| 437 | * \param[in] enable enable/disable state of the transmitter. |
| 438 | * \returns 0 on success; negative on error. */ |
| 439 | int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable) |
| 440 | { |
| 441 | if (!enable && suart->tx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 442 | suart->tx.running = false; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 443 | suart->tx.flow_state = SUART_FLOW_ST_IDLE; |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 444 | } else if (enable && !suart->tx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 445 | suart->tx.running = true; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 446 | suart->tx.flow_state = SUART_FLOW_ST_IDLE; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |