blob: 8cf45e1e760721e9b93f18540cf7d4ebe4c820f4 [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;
43 struct {
44 bool running;
45 uint8_t bit_count;
46 uint8_t shift_reg;
47 struct msgb *msg;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +070048 ubit_t parity_bit; /* 0 (even) / 1 (odd) */
Harald Weltedc023cf2022-11-29 23:15:18 +010049 unsigned int flags;
50 unsigned int status;
51 struct osmo_timer_list timer;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +070052 enum suart_flow_state flow_state;
Harald Weltedc023cf2022-11-29 23:15:18 +010053 } rx;
54 struct {
55 bool running;
56 uint8_t bit_count;
57 uint8_t shift_reg;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +070058 ubit_t parity_bit; /* 0 (even) / 1 (odd) */
59 enum suart_flow_state flow_state;
Harald Weltedc023cf2022-11-29 23:15:18 +010060 } tx;
61};
62
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +070063/*! Default soft-UART configuration (8-N-1) */
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +070064const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg = {
Harald Weltedc023cf2022-11-29 23:15:18 +010065 .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 Weltedc023cf2022-11-29 23:15:18 +010070};
71
72/*************************************************************************
73 * Receiver
74 *************************************************************************/
75
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +070076/*! Flush the receive buffer, passing ownership of the msgb to the .rx_cb().
77 * \param[in] suart soft-UART instance holding the receive buffer. */
78void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart)
Harald Weltedc023cf2022-11-29 23:15:18 +010079{
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 Yanitskiyc9fc77f2023-11-16 00:55:35 +070085 suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart_rx");
Harald Weltedc023cf2022-11-29 23:15:18 +010086 } 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 */
93static 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 Yanitskiybf95f822023-11-18 01:46:16 +0700101 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 Weltedc023cf2022-11-29 23:15:18 +0100106 osmo_timer_schedule(&suart->rx.timer, suart->cfg.rx_timeout_ms / 1000,
107 (suart->cfg.rx_timeout_ms % 1000) * 1000);
Harald Weltedc023cf2022-11-29 23:15:18 +0100108 }
109}
110
111/* receive a single bit */
112static inline void osmo_uart_rx_bit(struct osmo_soft_uart *suart, const ubit_t bit)
113{
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700114 switch (suart->rx.flow_state) {
115 case SUART_FLOW_ST_IDLE:
116 if (bit == 0) { /* start bit condition */
117 suart->rx.flow_state = SUART_FLOW_ST_DATA;
118 suart->rx.flags = 0x00;
Harald Weltedc023cf2022-11-29 23:15:18 +0100119 suart->rx.shift_reg = 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100120 suart->rx.bit_count = 0;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700121 suart->rx.parity_bit = 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100122 }
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700123 break;
124 case SUART_FLOW_ST_DATA:
125 suart->rx.bit_count++;
126 suart->rx.shift_reg >>= 1;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700127 if (bit != 0) {
128 suart->rx.parity_bit = !suart->rx.parity_bit; /* flip */
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700129 suart->rx.shift_reg |= 0x80;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700130 }
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700131 if (suart->rx.bit_count >= suart->cfg.num_data_bits) {
132 /* we have accumulated enough data bits */
133 if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE)
134 suart->rx.flow_state = SUART_FLOW_ST_PARITY;
135 else
136 suart->rx.flow_state = SUART_FLOW_ST_STOP;
Vadim Yanitskiy2d2ce492023-11-19 15:37:10 +0700137 /* align the register if needed */
138 if (suart->cfg.num_data_bits < 8)
139 suart->rx.shift_reg >>= (8 - suart->cfg.num_data_bits);
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700140 }
141 break;
142 case SUART_FLOW_ST_PARITY:
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700143 switch (suart->cfg.parity_mode) {
144 case OSMO_SUART_PARITY_EVEN:
145 /* number of 1-bits (in both data and parity) shall be even */
146 if (suart->rx.parity_bit != bit)
147 suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR;
148 break;
149 case OSMO_SUART_PARITY_ODD:
150 /* number of 1-bits (in both data and parity) shall be odd */
151 if (suart->rx.parity_bit == bit)
152 suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR;
153 break;
Vadim Yanitskiy0d78a002023-11-19 16:15:38 +0700154 case OSMO_SUART_PARITY_MARK:
155 /* parity bit must always be 1 */
156 if (bit != 1)
157 suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR;
158 break;
159 case OSMO_SUART_PARITY_SPACE:
160 /* parity bit must always be 0 */
161 if (bit != 0)
162 suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR;
163 break;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700164 case OSMO_SUART_PARITY_NONE: /* shall not happen */
165 default:
166 OSMO_ASSERT(0);
167 }
168
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700169 suart->rx.flow_state = SUART_FLOW_ST_STOP;
170 break;
171 case SUART_FLOW_ST_STOP:
172 suart->rx.bit_count++;
173 if (bit != 1)
174 suart->rx.flags |= OSMO_SUART_F_FRAMING_ERROR;
175
176 if (suart->rx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) {
177 /* we have accumulated enough stop bits */
178 suart_rx_ch(suart, suart->rx.shift_reg);
179 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
180 }
181 break;
Harald Weltedc023cf2022-11-29 23:15:18 +0100182 }
183}
184
185/* receive timer expiration: flush rx-buffer to user call-back */
186static void suart_rx_timer_cb(void *data)
187{
188 struct osmo_soft_uart *suart = data;
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700189 osmo_soft_uart_flush_rx(suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100190}
191
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700192/*! Feed a number of unpacked bits into the soft-UART receiver.
193 * \param[in] suart soft-UART instance to feed bits into.
194 * \param[in] ubits pointer to the unpacked bits.
195 * \param[in] n_ubits number of unpacked bits to be fed.
Vadim Yanitskiy811638c2023-11-30 03:09:05 +0700196 * \returns 0 on success; negative on error.
197 * -EAGAIN indicates that the receiver is disabled. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100198int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits)
199{
Vadim Yanitskiy811638c2023-11-30 03:09:05 +0700200 if (!suart->rx.running)
201 return -EAGAIN;
Harald Weltedc023cf2022-11-29 23:15:18 +0100202 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 Weltedc023cf2022-11-29 23:15:18 +0100211/* pull a single bit out of the UART transmitter */
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700212static inline ubit_t osmo_uart_tx_bit(struct osmo_soft_uart *suart, struct msgb *msg)
Harald Weltedc023cf2022-11-29 23:15:18 +0100213{
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700214 ubit_t tx_bit = 1;
Harald Weltedc023cf2022-11-29 23:15:18 +0100215
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700216 switch (suart->tx.flow_state) {
217 case SUART_FLOW_ST_IDLE:
Vadim Yanitskiyffdd9972023-11-21 02:25:32 +0700218 if (msg && msgb_length(msg) > 0) { /* if we have pending data */
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700219 suart->tx.shift_reg = msgb_pull_u8(msg);
220 suart->tx.flow_state = SUART_FLOW_ST_DATA;
221 suart->tx.bit_count = 0;
222 suart->tx.parity_bit = 0;
223 tx_bit = 0;
224 }
225 break;
226 case SUART_FLOW_ST_DATA:
227 tx_bit = suart->tx.shift_reg & 1;
228 suart->tx.parity_bit ^= tx_bit;
229 suart->tx.shift_reg >>= 1;
230 suart->tx.bit_count++;
231 if (suart->tx.bit_count >= suart->cfg.num_data_bits) {
232 /* we have transmitted all data bits */
233 if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE)
234 suart->tx.flow_state = SUART_FLOW_ST_PARITY;
235 else
236 suart->tx.flow_state = SUART_FLOW_ST_STOP;
237 }
238 break;
239 case SUART_FLOW_ST_PARITY:
240 switch (suart->cfg.parity_mode) {
241 case OSMO_SUART_PARITY_EVEN:
242 /* number of 1-bits (in both data and parity) shall be even */
243 tx_bit = suart->tx.parity_bit;
244 break;
245 case OSMO_SUART_PARITY_ODD:
246 /* number of 1-bits (in both data and parity) shall be odd */
247 tx_bit = !suart->tx.parity_bit;
248 break;
Vadim Yanitskiy0d78a002023-11-19 16:15:38 +0700249 case OSMO_SUART_PARITY_MARK:
250 /* parity bit must always be 1 */
251 tx_bit = 1;
252 break;
253 case OSMO_SUART_PARITY_SPACE:
254 /* parity bit must always be 0 */
255 tx_bit = 0;
256 break;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700257 case OSMO_SUART_PARITY_NONE:
258 default: /* shall not happen */
259 OSMO_ASSERT(0);
260 }
261
262 suart->tx.flow_state = SUART_FLOW_ST_STOP;
263 break;
264 case SUART_FLOW_ST_STOP:
265 suart->tx.bit_count++;
266 if (suart->tx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) {
267 /* we have transmitted all stop bits, we're done */
268 suart->tx.flow_state = SUART_FLOW_ST_IDLE;
269 }
270 break;
Harald Weltedc023cf2022-11-29 23:15:18 +0100271 }
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700272
273 return tx_bit;
Harald Weltedc023cf2022-11-29 23:15:18 +0100274}
275
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700276/*! Pull a number of unpacked bits out of the soft-UART transmitter.
277 * \param[in] suart soft-UART instance to pull the bits from.
278 * \param[out] ubits pointer to a buffer where to store pulled bits.
279 * \param[in] n_ubits number of unpacked bits to be pulled.
Vadim Yanitskiy811638c2023-11-30 03:09:05 +0700280 * \returns 0 on success; negative on error.
281 * -EAGAIN indicates that the transmitter is disabled. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100282int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits)
283{
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700284 const struct osmo_soft_uart_cfg *cfg = &suart->cfg;
Vadim Yanitskiyffdd9972023-11-21 02:25:32 +0700285 size_t n_frame_bits, n_chars;
286 struct msgb *msg = NULL;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700287
Vadim Yanitskiy459cb062023-11-21 20:12:36 +0700288 if (OSMO_UNLIKELY(n_ubits == 0))
289 return -EINVAL;
290
Vadim Yanitskiy811638c2023-11-30 03:09:05 +0700291 if (!suart->tx.running)
292 return -EAGAIN;
293
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700294 /* calculate UART frame size for the effective config */
295 n_frame_bits = 1 + cfg->num_data_bits + cfg->num_stop_bits;
296 if (cfg->parity_mode != OSMO_SUART_PARITY_NONE)
297 n_frame_bits += 1;
298
Vadim Yanitskiyffdd9972023-11-21 02:25:32 +0700299 /* calculate the number of characters we can fit into n_ubits */
300 n_chars = n_ubits / n_frame_bits;
301 if (n_chars == 0) {
302 /* we can transmit at least one character */
303 if (suart->tx.flow_state == SUART_FLOW_ST_IDLE)
304 n_chars = 1;
305 }
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700306
Vadim Yanitskiyffdd9972023-11-21 02:25:32 +0700307 if (n_chars > 0) {
308 /* allocate a Tx buffer msgb */
309 msg = msgb_alloc_c(suart, n_chars, "soft_uart_tx");
310 OSMO_ASSERT(msg != NULL);
311
312 /* call the .tx_cb() to populate the Tx buffer */
313 OSMO_ASSERT(cfg->tx_cb != NULL);
314 suart->cfg.tx_cb(cfg->priv, msg);
315 }
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700316
Harald Weltedc023cf2022-11-29 23:15:18 +0100317 for (size_t i = 0; i < n_ubits; i++)
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700318 ubits[i] = osmo_uart_tx_bit(suart, msg);
319 msgb_free(msg);
320
321 return 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100322}
323
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700324/*! Set the modem status lines of the given soft-UART.
325 * \param[in] suart soft-UART instance to update the modem status.
326 * \param[in] status mask of osmo_soft_uart_status.
327 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100328int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status)
329{
330 /* FIXME: Tx */
331 return 0;
332}
333
334
335/*************************************************************************
336 * Management / Initialization
337 *************************************************************************/
338
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700339/*! Allocate a soft-UART instance.
340 * \param[in] ctx parent talloc context.
341 * \param[in] name name of the soft-UART instance.
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700342 * \param[in] cfg initial configuration of the soft-UART instance.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700343 * \returns pointer to allocated soft-UART instance; NULL on error. */
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700344struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name,
345 const struct osmo_soft_uart_cfg *cfg)
Harald Weltedc023cf2022-11-29 23:15:18 +0100346{
347 struct osmo_soft_uart *suart = talloc_zero(ctx, struct osmo_soft_uart);
348 if (!suart)
349 return NULL;
350 suart->name = talloc_strdup(suart, name);
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700351
352 OSMO_ASSERT(cfg != NULL);
353 suart->cfg = *cfg;
Harald Weltedc023cf2022-11-29 23:15:18 +0100354
355 return suart;
356}
357
Vadim Yanitskiy877cfed2023-11-12 17:21:31 +0700358/*! Release memory taken by the given soft-UART.
359 * \param[in] suart soft-UART instance to be free()d. */
360void osmo_soft_uart_free(struct osmo_soft_uart *suart)
361{
362 if (suart == NULL)
363 return;
364
365 osmo_timer_del(&suart->rx.timer);
366 msgb_free(suart->rx.msg);
367
368 talloc_free((void *)suart->name);
369 talloc_free(suart);
370}
371
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700372/*! Change soft-UART configuration to the user-provided config.
373 * \param[in] suart soft-UART instance to be re-configured.
374 * \param[in] cfg the user-provided config to be applied.
375 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100376int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg)
377{
378 /* consistency checks on the configuration */
379 if (cfg->num_data_bits > 8 || cfg->num_data_bits == 0)
380 return -EINVAL;
381 if (cfg->num_stop_bits == 0)
382 return -EINVAL;
383 if (cfg->parity_mode < 0 || cfg->parity_mode >= _OSMO_SUART_PARITY_NUM)
384 return -EINVAL;
385 if (cfg->rx_buf_size == 0)
386 return -EINVAL;
387
388 if (suart->cfg.rx_buf_size > cfg->rx_buf_size ||
389 suart->cfg.rx_timeout_ms > cfg->rx_timeout_ms) {
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700390 osmo_soft_uart_flush_rx(suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100391 }
392
393 suart->cfg = *cfg;
394
395 osmo_timer_setup(&suart->rx.timer, suart_rx_timer_cb, suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100396
397 return 0;
398}
399
Vadim Yanitskiy59afb8f2023-11-29 23:58:10 +0700400/*! Get a name for the given soft-UART instance.
401 * \param[in] suart soft-UART instance to get the name from.
402 * \returns name of the given soft-UART instance. */
403const char *osmo_soft_uart_get_name(const struct osmo_soft_uart *suart)
404{
405 return suart->name;
406}
407
408/*! Set a new name for the given soft-UART instance.
409 * \param[in] suart soft-UART instance to set the name for.
410 * \param[in] name the new name. */
411void osmo_soft_uart_set_name(struct osmo_soft_uart *suart, const char *name)
412{
413 osmo_talloc_replace_string(suart, (char **)&suart->name, name);
414}
415
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700416/*! Enable/disable receiver of the given soft-UART.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700417 * \param[in] suart soft-UART instance to be re-configured.
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700418 * \param[in] enable enable/disable state of the receiver.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700419 * \returns 0 on success; negative on error. */
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700420int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable)
Harald Weltedc023cf2022-11-29 23:15:18 +0100421{
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700422 if (!enable && suart->rx.running) {
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700423 osmo_soft_uart_flush_rx(suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100424 suart->rx.running = false;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700425 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700426 } else if (enable && !suart->rx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100427 if (!suart->rx.msg)
428 suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx");
429 suart->rx.running = true;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700430 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
Harald Weltedc023cf2022-11-29 23:15:18 +0100431 }
432
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700433 return 0;
434}
435
436/*! Enable/disable transmitter of the given soft-UART.
437 * \param[in] suart soft-UART instance to be re-configured.
438 * \param[in] enable enable/disable state of the transmitter.
439 * \returns 0 on success; negative on error. */
440int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable)
441{
442 if (!enable && suart->tx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100443 suart->tx.running = false;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700444 suart->tx.flow_state = SUART_FLOW_ST_IDLE;
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700445 } else if (enable && !suart->tx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100446 suart->tx.running = true;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700447 suart->tx.flow_state = SUART_FLOW_ST_IDLE;
Harald Weltedc023cf2022-11-29 23:15:18 +0100448 }
449
450 return 0;
451}