libosmocore 1.13.1.1-afb4 Osmocom core library

soft_uart.h
Go to the documentation of this file.
1#pragma once
2
5/*
6 * (C) 2022 by Harald Welte <laforge@gnumonks.org>
7 * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
8 *
9 * All Rights Reserved
10 *
11 * SPDX-License-Identifier: GPL-2.0+
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 */
24
25#include <stdint.h>
26#include <stdbool.h>
27
28#include <osmocom/core/bits.h>
29#include <osmocom/core/msgb.h>
30
33 enum osmo_soft_uart_parity_mode {
34 OSMO_SUART_PARITY_NONE,
35 OSMO_SUART_PARITY_EVEN,
36 OSMO_SUART_PARITY_ODD,
37 OSMO_SUART_PARITY_MARK,
38 OSMO_SUART_PARITY_SPACE,
39 _OSMO_SUART_PARITY_NUM
40};
41
43 enum osmo_soft_uart_flags {
44 OSMO_SUART_F_FRAMING_ERROR = (1 << 0),
45 OSMO_SUART_F_PARITY_ERROR = (1 << 1),
46 OSMO_SUART_F_BREAK = (1 << 2),
47};
48
51 enum osmo_soft_uart_status {
52 OSMO_SUART_STATUS_F_DTR = (1 << 0),
53 OSMO_SUART_STATUS_F_DCD = (1 << 1),
54 OSMO_SUART_STATUS_F_DSR = (1 << 2),
55 OSMO_SUART_STATUS_F_RI = (1 << 3),
56 OSMO_SUART_STATUS_F_RTS_RTR = (1 << 4),
57 OSMO_SUART_STATUS_F_CTS = (1 << 5),
58};
59
62 enum osmo_soft_uart_flow_ctrl_mode {
64 OSMO_SUART_FLOW_CTRL_NONE,
66 OSMO_SUART_FLOW_CTRL_DTR_DSR,
72 OSMO_SUART_FLOW_CTRL_RTS_CTS,
73};
74
76 struct osmo_soft_uart_cfg {
78 uint8_t num_data_bits;
80 uint8_t num_stop_bits;
82 enum osmo_soft_uart_parity_mode parity_mode;
85 unsigned int rx_buf_size;
88 unsigned int rx_timeout_ms;
89
91 void *priv;
92
104 void (*rx_cb)(void *priv, struct msgb *rx_data, unsigned int flags);
105
114 void (*tx_cb)(void *priv, struct msgb *tx_data);
115
119 void (*status_change_cb)(void *priv, unsigned int status);
120
122 enum osmo_soft_uart_flow_ctrl_mode flow_ctrl_mode;
123};
124
125 extern const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg;
126
127 struct osmo_soft_uart;
128
129 struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name,
130 const struct osmo_soft_uart_cfg *cfg);
131 void osmo_soft_uart_free(struct osmo_soft_uart *suart);
132 int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg);
133
134 const char *osmo_soft_uart_get_name(const struct osmo_soft_uart *suart);
135 void osmo_soft_uart_set_name(struct osmo_soft_uart *suart, const char *name);
136
137 int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable);
138 int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable);
139
140 int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits);
141 int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits);
142
143 unsigned int osmo_soft_uart_get_status(const struct osmo_soft_uart *suart);
144 int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status);
145 void osmo_soft_uart_set_status_line(struct osmo_soft_uart *suart,
146 enum osmo_soft_uart_status line,
147 bool active);
148
149 void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart);
bits.h
Osmocom bit level support code.
flags
uint16_t flags
name
const char * name
ubit_t
uint8_t ubit_t
unpacked bit (0 or 1): 1 bit per byte
Definition: bits.h:24
msgb.h
osmo_soft_uart_alloc
struct osmo_soft_uart * osmo_soft_uart_alloc(void *ctx, const char *name, const struct osmo_soft_uart_cfg *cfg)
Allocate a soft-UART instance.
Definition: soft_uart.c:412
osmo_soft_uart_set_tx
int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable)
Enable/disable transmitter of the given soft-UART.
Definition: soft_uart.c:508
osmo_soft_uart_set_status_line
void osmo_soft_uart_set_status_line(struct osmo_soft_uart *suart, enum osmo_soft_uart_status line, bool active)
Activate/deactivate a modem status line of the given soft-UART.
Definition: soft_uart.c:388
osmo_soft_uart_set_name
void osmo_soft_uart_set_name(struct osmo_soft_uart *suart, const char *name)
Set a new name for the given soft-UART instance.
Definition: soft_uart.c:479
osmo_soft_uart_get_name
const char * osmo_soft_uart_get_name(const struct osmo_soft_uart *suart)
Get a name for the given soft-UART instance.
Definition: soft_uart.c:471
osmo_soft_uart_set_rx
int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable)
Enable/disable receiver of the given soft-UART.
Definition: soft_uart.c:488
osmo_soft_uart_flush_rx
void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart)
Flush the receive buffer, passing ownership of the msgb to the .rx_cb().
Definition: soft_uart.c:81
osmo_soft_uart_free
void osmo_soft_uart_free(struct osmo_soft_uart *suart)
Release memory taken by the given soft-UART.
Definition: soft_uart.c:428
osmo_soft_uart_tx_ubits
int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits)
Pull a number of unpacked bits out of the soft-UART transmitter.
Definition: soft_uart.c:299
osmo_soft_uart_default_cfg
const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg
Default soft-UART configuration (8-N-1)
Definition: soft_uart.c:66
osmo_soft_uart_status
osmo_soft_uart_status
Modem status "line" flags.
Definition: soft_uart.h:51
OSMO_SUART_STATUS_F_DCD
@ OSMO_SUART_STATUS_F_DCD
Data Carrier Detect.
Definition: soft_uart.h:53
OSMO_SUART_STATUS_F_DTR
@ OSMO_SUART_STATUS_F_DTR
Data Terminal Ready.
Definition: soft_uart.h:52
OSMO_SUART_STATUS_F_RTS_RTR
@ OSMO_SUART_STATUS_F_RTS_RTR
Request To Send or Ready To Receive.
Definition: soft_uart.h:56
OSMO_SUART_STATUS_F_DSR
@ OSMO_SUART_STATUS_F_DSR
Data Set Ready.
Definition: soft_uart.h:54
OSMO_SUART_STATUS_F_RI
@ OSMO_SUART_STATUS_F_RI
Ring Indicator.
Definition: soft_uart.h:55
OSMO_SUART_STATUS_F_CTS
@ OSMO_SUART_STATUS_F_CTS
Clear To Send.
Definition: soft_uart.h:57
osmo_soft_uart_flow_ctrl_mode
osmo_soft_uart_flow_ctrl_mode
Flow control mode.
Definition: soft_uart.h:62
OSMO_SUART_FLOW_CTRL_RTS_CTS
@ OSMO_SUART_FLOW_CTRL_RTS_CTS
RTS/CTS flow control: Tx if CTS is active and drop RTS if cannot Rx anymore.
Definition: soft_uart.h:72
OSMO_SUART_FLOW_CTRL_NONE
@ OSMO_SUART_FLOW_CTRL_NONE
No flow control.
Definition: soft_uart.h:64
OSMO_SUART_FLOW_CTRL_DTR_DSR
@ OSMO_SUART_FLOW_CTRL_DTR_DSR
DTR/DSR flow control: Tx if DSR is active and drop DTR if cannot Rx anymore.
Definition: soft_uart.h:66
osmo_soft_uart_get_status
unsigned int osmo_soft_uart_get_status(const struct osmo_soft_uart *suart)
Get the modem status bitmask of the given soft-UART.
Definition: soft_uart.c:362
osmo_soft_uart_parity_mode
osmo_soft_uart_parity_mode
Parity mode.
Definition: soft_uart.h:33
_OSMO_SUART_PARITY_NUM
@ _OSMO_SUART_PARITY_NUM
Definition: soft_uart.h:39
OSMO_SUART_PARITY_SPACE
@ OSMO_SUART_PARITY_SPACE
Always 0.
Definition: soft_uart.h:38
OSMO_SUART_PARITY_EVEN
@ OSMO_SUART_PARITY_EVEN
Even parity.
Definition: soft_uart.h:35
OSMO_SUART_PARITY_ODD
@ OSMO_SUART_PARITY_ODD
Odd parity.
Definition: soft_uart.h:36
OSMO_SUART_PARITY_MARK
@ OSMO_SUART_PARITY_MARK
Always 1.
Definition: soft_uart.h:37
OSMO_SUART_PARITY_NONE
@ OSMO_SUART_PARITY_NONE
No parity bit.
Definition: soft_uart.h:34
osmo_soft_uart_flags
osmo_soft_uart_flags
Flags passed to the application.
Definition: soft_uart.h:43
OSMO_SUART_F_BREAK
@ OSMO_SUART_F_BREAK
Break condition (not implemented)
Definition: soft_uart.h:46
OSMO_SUART_F_PARITY_ERROR
@ OSMO_SUART_F_PARITY_ERROR
Parity error occurred.
Definition: soft_uart.h:45
OSMO_SUART_F_FRAMING_ERROR
@ OSMO_SUART_F_FRAMING_ERROR
Framing error occurred.
Definition: soft_uart.h:44
osmo_soft_uart_rx_ubits
int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits)
Feed a number of unpacked bits into the soft-UART receiver.
Definition: soft_uart.c:201
osmo_soft_uart_set_status
int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status)
Set the modem status bitmask of the given soft-UART.
Definition: soft_uart.c:371
osmo_soft_uart_configure
int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg)
Change soft-UART configuration to the user-provided config.
Definition: soft_uart.c:444
msgb
Osmocom message buffer.
Definition: msgb.h:31
osmo_soft_uart_cfg
Configuration for a soft-UART.
Definition: soft_uart.h:76
osmo_soft_uart_cfg::num_stop_bits
uint8_t num_stop_bits
Number of stop bits (typically 1 or 2).
Definition: soft_uart.h:80
osmo_soft_uart_cfg::parity_mode
enum osmo_soft_uart_parity_mode parity_mode
Parity mode (none, even, odd, space, mark).
Definition: soft_uart.h:82
osmo_soft_uart_cfg::tx_cb
void(* tx_cb)(void *priv, struct msgb *tx_data)
Transmit call-back of the application.
Definition: soft_uart.h:114
osmo_soft_uart_cfg::num_data_bits
uint8_t num_data_bits
Number of data bits (typically 5, 6, 7 or 8).
Definition: soft_uart.h:78
osmo_soft_uart_cfg::flow_ctrl_mode
enum osmo_soft_uart_flow_ctrl_mode flow_ctrl_mode
"Hardware" flow control mode.
Definition: soft_uart.h:122
osmo_soft_uart_cfg::rx_cb
void(* rx_cb)(void *priv, struct msgb *rx_data, unsigned int flags)
Receive call-back of the application.
Definition: soft_uart.h:104
osmo_soft_uart_cfg::rx_buf_size
unsigned int rx_buf_size
Size of the receive buffer; UART will buffer up to that number of characters before calling the recei...
Definition: soft_uart.h:85
osmo_soft_uart_cfg::priv
void * priv
Opaque application-private data; passed to call-backs.
Definition: soft_uart.h:91
osmo_soft_uart_cfg::rx_timeout_ms
unsigned int rx_timeout_ms
Receive timeout; UART will flush the receive buffer via the receive call-back after indicated number ...
Definition: soft_uart.h:88
osmo_soft_uart_cfg::status_change_cb
void(* status_change_cb)(void *priv, unsigned int status)
Modem status line change call-back.
Definition: soft_uart.h:119
osmo_soft_uart
Internal state of a soft-UART.
Definition: soft_uart.c:41
osmo_soft_uart::cfg
struct osmo_soft_uart_cfg cfg
Definition: soft_uart.c:42
osmo_soft_uart::status
unsigned int status
Definition: soft_uart.c:45