blob: f969ab703029dc467ecffd8dabe2a97992d41677 [file] [log] [blame]
Harald Weltedc023cf2022-11-29 23:15:18 +01001/*! \file soft_uart.c
2 * Software UART implementation. */
3/*
4 * (C) 2022 by Harald Welte <laforge@gnumonks.org>
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +07005 * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
Harald Weltedc023cf2022-11-29 23:15:18 +01006 *
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 Yanitskiy459cb062023-11-21 20:12:36 +070027#include <osmocom/core/utils.h>
Harald Weltedc023cf2022-11-29 23:15:18 +010028#include <osmocom/core/timer.h>
29#include <osmocom/core/soft_uart.h>
30
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +070031/*! Rx/Tx flow state of a soft-UART */
32enum 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 Yanitskiyb72625d2023-11-16 00:32:33 +070039/*! Internal state of a soft-UART */
Harald Weltedc023cf2022-11-29 23:15:18 +010040struct osmo_soft_uart {
41 struct osmo_soft_uart_cfg cfg;
42 const char *name;
Vadim Yanitskiy6587dd02023-11-30 03:04:32 +070043 /* modem status (bitmask of OSMO_SUART_STATUS_F_*) */
44 unsigned int status;
Harald Weltedc023cf2022-11-29 23:15:18 +010045 struct {
46 bool running;
47 uint8_t bit_count;
48 uint8_t shift_reg;
49 struct msgb *msg;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +070050 ubit_t parity_bit; /* 0 (even) / 1 (odd) */
Harald Weltedc023cf2022-11-29 23:15:18 +010051 unsigned int flags;
Harald Weltedc023cf2022-11-29 23:15:18 +010052 struct osmo_timer_list timer;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +070053 enum suart_flow_state flow_state;
Harald Weltedc023cf2022-11-29 23:15:18 +010054 } rx;
55 struct {
56 bool running;
57 uint8_t bit_count;
58 uint8_t shift_reg;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +070059 ubit_t parity_bit; /* 0 (even) / 1 (odd) */
60 enum suart_flow_state flow_state;
Harald Weltedc023cf2022-11-29 23:15:18 +010061 } tx;
62};
63
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +070064/*! Default soft-UART configuration (8-N-1) */
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +070065const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg = {
Harald Weltedc023cf2022-11-29 23:15:18 +010066 .num_data_bits = 8,
67 .num_stop_bits = 1,
68 .parity_mode = OSMO_SUART_PARITY_NONE,
69 .rx_buf_size = 1024,
70 .rx_timeout_ms = 100,
Vadim Yanitskiy6587dd02023-11-30 03:04:32 +070071 .flow_ctrl_mode = OSMO_SUART_FLOW_CTRL_NONE,
Harald Weltedc023cf2022-11-29 23:15:18 +010072};
73
74/*************************************************************************
75 * Receiver
76 *************************************************************************/
77
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +070078/*! Flush the receive buffer, passing ownership of the msgb to the .rx_cb().
79 * \param[in] suart soft-UART instance holding the receive buffer. */
80void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart)
Harald Weltedc023cf2022-11-29 23:15:18 +010081{
Vadim Yanitskiyd76cc372023-12-09 04:38:14 +070082 if (suart->rx.msg && msgb_length(suart->rx.msg)) {
Harald Weltedc023cf2022-11-29 23:15:18 +010083 osmo_timer_del(&suart->rx.timer);
84 if (suart->cfg.rx_cb) {
85 suart->cfg.rx_cb(suart->cfg.priv, suart->rx.msg, suart->rx.flags);
86 /* call-back has taken ownership of msgb, no need to free() here */
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +070087 suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart_rx");
Harald Weltedc023cf2022-11-29 23:15:18 +010088 } else {
89 msgb_reset(suart->rx.msg);
90 }
91 }
92}
93
94/* one character was received; add to receive buffer and notify user, if needed */
95static void suart_rx_ch(struct osmo_soft_uart *suart, uint8_t ch)
96{
97 unsigned int msg_len;
98
99 OSMO_ASSERT(suart->rx.msg);
100 msgb_put_u8(suart->rx.msg, ch);
101 msg_len = msgb_length(suart->rx.msg);
102
Vadim Yanitskiybf95f822023-11-18 01:46:16 +0700103 if (msg_len >= suart->cfg.rx_buf_size || suart->rx.flags) {
104 /* either the buffer is full, or we hit a parity and/or a framing error */
105 osmo_soft_uart_flush_rx(suart);
106 } else if (msg_len == 1) {
107 /* first character in new message: start timer */
Harald Weltedc023cf2022-11-29 23:15:18 +0100108 osmo_timer_schedule(&suart->rx.timer, suart->cfg.rx_timeout_ms / 1000,
109 (suart->cfg.rx_timeout_ms % 1000) * 1000);
Harald Weltedc023cf2022-11-29 23:15:18 +0100110 }
111}
112
113/* receive a single bit */
Vadim Yanitskiy2203db52023-12-04 02:59:28 +0700114static inline void suart_rx_bit(struct osmo_soft_uart *suart, const ubit_t bit)
Harald Weltedc023cf2022-11-29 23:15:18 +0100115{
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700116 switch (suart->rx.flow_state) {
117 case SUART_FLOW_ST_IDLE:
118 if (bit == 0) { /* start bit condition */
119 suart->rx.flow_state = SUART_FLOW_ST_DATA;
120 suart->rx.flags = 0x00;
Harald Weltedc023cf2022-11-29 23:15:18 +0100121 suart->rx.shift_reg = 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100122 suart->rx.bit_count = 0;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700123 suart->rx.parity_bit = 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100124 }
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700125 break;
126 case SUART_FLOW_ST_DATA:
127 suart->rx.bit_count++;
128 suart->rx.shift_reg >>= 1;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700129 if (bit != 0) {
130 suart->rx.parity_bit = !suart->rx.parity_bit; /* flip */
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700131 suart->rx.shift_reg |= 0x80;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700132 }
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700133 if (suart->rx.bit_count >= suart->cfg.num_data_bits) {
134 /* we have accumulated enough data bits */
135 if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE)
136 suart->rx.flow_state = SUART_FLOW_ST_PARITY;
137 else
138 suart->rx.flow_state = SUART_FLOW_ST_STOP;
Vadim Yanitskiy2d2ce492023-11-19 15:37:10 +0700139 /* align the register if needed */
140 if (suart->cfg.num_data_bits < 8)
141 suart->rx.shift_reg >>= (8 - suart->cfg.num_data_bits);
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700142 }
143 break;
144 case SUART_FLOW_ST_PARITY:
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700145 switch (suart->cfg.parity_mode) {
146 case OSMO_SUART_PARITY_EVEN:
147 /* number of 1-bits (in both data and parity) shall be even */
148 if (suart->rx.parity_bit != bit)
149 suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR;
150 break;
151 case OSMO_SUART_PARITY_ODD:
152 /* number of 1-bits (in both data and parity) shall be odd */
153 if (suart->rx.parity_bit == bit)
154 suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR;
155 break;
Vadim Yanitskiy0d78a002023-11-19 16:15:38 +0700156 case OSMO_SUART_PARITY_MARK:
157 /* parity bit must always be 1 */
158 if (bit != 1)
159 suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR;
160 break;
161 case OSMO_SUART_PARITY_SPACE:
162 /* parity bit must always be 0 */
163 if (bit != 0)
164 suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR;
165 break;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700166 case OSMO_SUART_PARITY_NONE: /* shall not happen */
167 default:
168 OSMO_ASSERT(0);
169 }
170
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700171 suart->rx.flow_state = SUART_FLOW_ST_STOP;
172 break;
173 case SUART_FLOW_ST_STOP:
174 suart->rx.bit_count++;
175 if (bit != 1)
176 suart->rx.flags |= OSMO_SUART_F_FRAMING_ERROR;
177
178 if (suart->rx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) {
179 /* we have accumulated enough stop bits */
180 suart_rx_ch(suart, suart->rx.shift_reg);
181 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
182 }
183 break;
Harald Weltedc023cf2022-11-29 23:15:18 +0100184 }
185}
186
187/* receive timer expiration: flush rx-buffer to user call-back */
188static void suart_rx_timer_cb(void *data)
189{
190 struct osmo_soft_uart *suart = data;
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700191 osmo_soft_uart_flush_rx(suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100192}
193
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700194/*! Feed a number of unpacked bits into the soft-UART receiver.
195 * \param[in] suart soft-UART instance to feed bits into.
196 * \param[in] ubits pointer to the unpacked bits.
197 * \param[in] n_ubits number of unpacked bits to be fed.
Vadim Yanitskiy811638c2023-11-30 03:09:05 +0700198 * \returns 0 on success; negative on error.
199 * -EAGAIN indicates that the receiver is disabled. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100200int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits)
201{
Vadim Yanitskiy811638c2023-11-30 03:09:05 +0700202 if (!suart->rx.running)
203 return -EAGAIN;
Harald Weltedc023cf2022-11-29 23:15:18 +0100204 for (size_t i = 0; i < n_ubits; i++)
Vadim Yanitskiy2203db52023-12-04 02:59:28 +0700205 suart_rx_bit(suart, ubits[i]);
Harald Weltedc023cf2022-11-29 23:15:18 +0100206 return 0;
207}
208
209/*************************************************************************
210 * Transmitter
211 *************************************************************************/
212
Harald Weltedc023cf2022-11-29 23:15:18 +0100213/* pull a single bit out of the UART transmitter */
Vadim Yanitskiy2203db52023-12-04 02:59:28 +0700214static inline ubit_t suart_tx_bit(struct osmo_soft_uart *suart, struct msgb *msg)
Harald Weltedc023cf2022-11-29 23:15:18 +0100215{
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700216 ubit_t tx_bit = 1;
Harald Weltedc023cf2022-11-29 23:15:18 +0100217
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700218 switch (suart->tx.flow_state) {
219 case SUART_FLOW_ST_IDLE:
Vadim Yanitskiyffdd9972023-11-21 02:25:32 +0700220 if (msg && msgb_length(msg) > 0) { /* if we have pending data */
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700221 suart->tx.shift_reg = msgb_pull_u8(msg);
222 suart->tx.flow_state = SUART_FLOW_ST_DATA;
223 suart->tx.bit_count = 0;
224 suart->tx.parity_bit = 0;
225 tx_bit = 0;
226 }
227 break;
228 case SUART_FLOW_ST_DATA:
229 tx_bit = suart->tx.shift_reg & 1;
230 suart->tx.parity_bit ^= tx_bit;
231 suart->tx.shift_reg >>= 1;
232 suart->tx.bit_count++;
233 if (suart->tx.bit_count >= suart->cfg.num_data_bits) {
234 /* we have transmitted all data bits */
235 if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE)
236 suart->tx.flow_state = SUART_FLOW_ST_PARITY;
237 else
238 suart->tx.flow_state = SUART_FLOW_ST_STOP;
239 }
240 break;
241 case SUART_FLOW_ST_PARITY:
242 switch (suart->cfg.parity_mode) {
243 case OSMO_SUART_PARITY_EVEN:
244 /* number of 1-bits (in both data and parity) shall be even */
245 tx_bit = suart->tx.parity_bit;
246 break;
247 case OSMO_SUART_PARITY_ODD:
248 /* number of 1-bits (in both data and parity) shall be odd */
249 tx_bit = !suart->tx.parity_bit;
250 break;
Vadim Yanitskiy0d78a002023-11-19 16:15:38 +0700251 case OSMO_SUART_PARITY_MARK:
252 /* parity bit must always be 1 */
253 tx_bit = 1;
254 break;
255 case OSMO_SUART_PARITY_SPACE:
256 /* parity bit must always be 0 */
257 tx_bit = 0;
258 break;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700259 case OSMO_SUART_PARITY_NONE:
260 default: /* shall not happen */
261 OSMO_ASSERT(0);
262 }
263
264 suart->tx.flow_state = SUART_FLOW_ST_STOP;
265 break;
266 case SUART_FLOW_ST_STOP:
267 suart->tx.bit_count++;
268 if (suart->tx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) {
269 /* we have transmitted all stop bits, we're done */
270 suart->tx.flow_state = SUART_FLOW_ST_IDLE;
271 }
272 break;
Harald Weltedc023cf2022-11-29 23:15:18 +0100273 }
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700274
275 return tx_bit;
Harald Weltedc023cf2022-11-29 23:15:18 +0100276}
277
Vadim Yanitskiy6587dd02023-11-30 03:04:32 +0700278/* pull pending bits out of the UART */
279static size_t suart_tx_pending(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits)
280{
281 size_t i;
282
283 for (i = 0; i < n_ubits; i++) {
284 if (suart->tx.flow_state == SUART_FLOW_ST_IDLE)
285 break;
286 ubits[i] = suart_tx_bit(suart, NULL);
287 }
288
289 return i;
290}
291
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700292/*! Pull a number of unpacked bits out of the soft-UART transmitter.
293 * \param[in] suart soft-UART instance to pull the bits from.
294 * \param[out] ubits pointer to a buffer where to store pulled bits.
295 * \param[in] n_ubits number of unpacked bits to be pulled.
Vadim Yanitskiy6587dd02023-11-30 03:04:32 +0700296 * \returns number of bits pulled (may be less than n_ubits); negative on error.
Vadim Yanitskiy811638c2023-11-30 03:09:05 +0700297 * -EAGAIN indicates that the transmitter is disabled. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100298int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits)
299{
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700300 const struct osmo_soft_uart_cfg *cfg = &suart->cfg;
Vadim Yanitskiyffdd9972023-11-21 02:25:32 +0700301 size_t n_frame_bits, n_chars;
302 struct msgb *msg = NULL;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700303
Vadim Yanitskiy459cb062023-11-21 20:12:36 +0700304 if (OSMO_UNLIKELY(n_ubits == 0))
305 return -EINVAL;
306
Vadim Yanitskiy811638c2023-11-30 03:09:05 +0700307 if (!suart->tx.running)
308 return -EAGAIN;
309
Vadim Yanitskiy6587dd02023-11-30 03:04:32 +0700310 switch (suart->cfg.flow_ctrl_mode) {
311 case OSMO_SUART_FLOW_CTRL_DTR_DSR:
312 /* if DSR is de-asserted, Tx pending bits and suspend */
313 if (~suart->status & OSMO_SUART_STATUS_F_DSR)
314 return suart_tx_pending(suart, ubits, n_ubits);
315 /* else: keep transmitting as usual */
316 break;
317 case OSMO_SUART_FLOW_CTRL_RTS_CTS:
318 /* if CTS is de-asserted, Tx pending bits and suspend */
319 if (~suart->status & OSMO_SUART_STATUS_F_CTS)
320 return suart_tx_pending(suart, ubits, n_ubits);
321 /* else: keep transmitting as usual */
322 break;
323 case OSMO_SUART_FLOW_CTRL_NONE:
324 default:
325 break;
326 }
327
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700328 /* calculate UART frame size for the effective config */
329 n_frame_bits = 1 + cfg->num_data_bits + cfg->num_stop_bits;
330 if (cfg->parity_mode != OSMO_SUART_PARITY_NONE)
331 n_frame_bits += 1;
332
Vadim Yanitskiyffdd9972023-11-21 02:25:32 +0700333 /* calculate the number of characters we can fit into n_ubits */
334 n_chars = n_ubits / n_frame_bits;
335 if (n_chars == 0) {
336 /* we can transmit at least one character */
337 if (suart->tx.flow_state == SUART_FLOW_ST_IDLE)
338 n_chars = 1;
339 }
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700340
Vadim Yanitskiyffdd9972023-11-21 02:25:32 +0700341 if (n_chars > 0) {
342 /* allocate a Tx buffer msgb */
343 msg = msgb_alloc_c(suart, n_chars, "soft_uart_tx");
344 OSMO_ASSERT(msg != NULL);
345
346 /* call the .tx_cb() to populate the Tx buffer */
347 OSMO_ASSERT(cfg->tx_cb != NULL);
348 suart->cfg.tx_cb(cfg->priv, msg);
349 }
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700350
Harald Weltedc023cf2022-11-29 23:15:18 +0100351 for (size_t i = 0; i < n_ubits; i++)
Vadim Yanitskiy2203db52023-12-04 02:59:28 +0700352 ubits[i] = suart_tx_bit(suart, msg);
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700353 msgb_free(msg);
354
Vadim Yanitskiyffb8d5e2023-12-04 03:17:26 +0700355 return n_ubits;
Harald Weltedc023cf2022-11-29 23:15:18 +0100356}
357
Vadim Yanitskiy6587dd02023-11-30 03:04:32 +0700358/*! Get the modem status bitmask of the given soft-UART.
359 * \param[in] suart soft-UART instance to get the modem status.
360 * \returns bitmask of OSMO_SUART_STATUS_F_*. */
361unsigned int osmo_soft_uart_get_status(const struct osmo_soft_uart *suart)
362{
363 return suart->status;
364}
365
366/*! Set the modem status bitmask of the given soft-UART.
367 * \param[in] suart soft-UART instance to set the modem status.
368 * \param[in] status bitmask of OSMO_SUART_STATUS_F_*.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700369 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100370int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status)
371{
Vadim Yanitskiy6587dd02023-11-30 03:04:32 +0700372 const struct osmo_soft_uart_cfg *cfg = &suart->cfg;
373
374 if (cfg->status_change_cb != NULL) {
375 if (suart->status != status)
376 cfg->status_change_cb(cfg->priv, status);
377 }
378
379 suart->status = status;
Harald Weltedc023cf2022-11-29 23:15:18 +0100380 return 0;
381}
382
Vadim Yanitskiy6587dd02023-11-30 03:04:32 +0700383/*! Activate/deactivate a modem status line of the given soft-UART.
384 * \param[in] suart soft-UART instance to update the modem status.
385 * \param[in] line a modem status line, one of OSMO_SUART_STATUS_F_*.
386 * \param[in] active activate (true) or deactivate (false) the line. */
387void osmo_soft_uart_set_status_line(struct osmo_soft_uart *suart,
388 enum osmo_soft_uart_status line,
389 bool active)
390{
391 unsigned int status = suart->status;
392
393 if (active) /* assert the given line */
394 status |= line;
395 else /* de-assert the given line */
396 status &= ~line;
397
398 osmo_soft_uart_set_status(suart, status);
399}
400
Harald Weltedc023cf2022-11-29 23:15:18 +0100401
402/*************************************************************************
403 * Management / Initialization
404 *************************************************************************/
405
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700406/*! Allocate a soft-UART instance.
407 * \param[in] ctx parent talloc context.
408 * \param[in] name name of the soft-UART instance.
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700409 * \param[in] cfg initial configuration of the soft-UART instance.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700410 * \returns pointer to allocated soft-UART instance; NULL on error. */
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700411struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name,
412 const struct osmo_soft_uart_cfg *cfg)
Harald Weltedc023cf2022-11-29 23:15:18 +0100413{
414 struct osmo_soft_uart *suart = talloc_zero(ctx, struct osmo_soft_uart);
415 if (!suart)
416 return NULL;
417 suart->name = talloc_strdup(suart, name);
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700418
419 OSMO_ASSERT(cfg != NULL);
420 suart->cfg = *cfg;
Harald Weltedc023cf2022-11-29 23:15:18 +0100421
422 return suart;
423}
424
Vadim Yanitskiy877cfed2023-11-12 17:21:31 +0700425/*! Release memory taken by the given soft-UART.
426 * \param[in] suart soft-UART instance to be free()d. */
427void osmo_soft_uart_free(struct osmo_soft_uart *suart)
428{
429 if (suart == NULL)
430 return;
431
432 osmo_timer_del(&suart->rx.timer);
433 msgb_free(suart->rx.msg);
434
435 talloc_free((void *)suart->name);
436 talloc_free(suart);
437}
438
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700439/*! Change soft-UART configuration to the user-provided config.
440 * \param[in] suart soft-UART instance to be re-configured.
441 * \param[in] cfg the user-provided config to be applied.
442 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100443int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg)
444{
445 /* consistency checks on the configuration */
446 if (cfg->num_data_bits > 8 || cfg->num_data_bits == 0)
447 return -EINVAL;
448 if (cfg->num_stop_bits == 0)
449 return -EINVAL;
450 if (cfg->parity_mode < 0 || cfg->parity_mode >= _OSMO_SUART_PARITY_NUM)
451 return -EINVAL;
452 if (cfg->rx_buf_size == 0)
453 return -EINVAL;
454
455 if (suart->cfg.rx_buf_size > cfg->rx_buf_size ||
456 suart->cfg.rx_timeout_ms > cfg->rx_timeout_ms) {
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700457 osmo_soft_uart_flush_rx(suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100458 }
459
460 suart->cfg = *cfg;
461
462 osmo_timer_setup(&suart->rx.timer, suart_rx_timer_cb, suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100463
464 return 0;
465}
466
Vadim Yanitskiy59afb8f2023-11-29 23:58:10 +0700467/*! Get a name for the given soft-UART instance.
468 * \param[in] suart soft-UART instance to get the name from.
469 * \returns name of the given soft-UART instance. */
470const char *osmo_soft_uart_get_name(const struct osmo_soft_uart *suart)
471{
472 return suart->name;
473}
474
475/*! Set a new name for the given soft-UART instance.
476 * \param[in] suart soft-UART instance to set the name for.
477 * \param[in] name the new name. */
478void osmo_soft_uart_set_name(struct osmo_soft_uart *suart, const char *name)
479{
480 osmo_talloc_replace_string(suart, (char **)&suart->name, name);
481}
482
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700483/*! Enable/disable receiver of the given soft-UART.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700484 * \param[in] suart soft-UART instance to be re-configured.
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700485 * \param[in] enable enable/disable state of the receiver.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700486 * \returns 0 on success; negative on error. */
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700487int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable)
Harald Weltedc023cf2022-11-29 23:15:18 +0100488{
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700489 if (!enable && suart->rx.running) {
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700490 osmo_soft_uart_flush_rx(suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100491 suart->rx.running = false;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700492 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700493 } else if (enable && !suart->rx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100494 if (!suart->rx.msg)
Vadim Yanitskiy26dff792023-12-09 04:08:24 +0700495 suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart_rx");
Harald Weltedc023cf2022-11-29 23:15:18 +0100496 suart->rx.running = true;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700497 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
Harald Weltedc023cf2022-11-29 23:15:18 +0100498 }
499
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700500 return 0;
501}
502
503/*! Enable/disable transmitter of the given soft-UART.
504 * \param[in] suart soft-UART instance to be re-configured.
505 * \param[in] enable enable/disable state of the transmitter.
506 * \returns 0 on success; negative on error. */
507int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable)
508{
509 if (!enable && suart->tx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100510 suart->tx.running = false;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700511 suart->tx.flow_state = SUART_FLOW_ST_IDLE;
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700512 } else if (enable && !suart->tx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100513 suart->tx.running = true;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700514 suart->tx.flow_state = SUART_FLOW_ST_IDLE;
Harald Weltedc023cf2022-11-29 23:15:18 +0100515 }
516
517 return 0;
518}