Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 1 | #ifndef _SERCOMM_H |
| 2 | #define _SERCOMM_H |
| 3 | |
| 4 | #include <osmocom/core/msgb.h> |
| 5 | |
| 6 | #define HDLC_FLAG 0x7E |
| 7 | #define HDLC_ESCAPE 0x7D |
| 8 | |
| 9 | #define HDLC_C_UI 0x03 |
| 10 | #define HDLC_C_P_BIT (1 << 4) |
| 11 | #define HDLC_C_F_BIT (1 << 4) |
| 12 | |
| 13 | /* a low sercomm_dlci means high priority. A high DLCI means low priority */ |
| 14 | enum sercomm_dlci { |
| 15 | SC_DLCI_HIGHEST = 0, |
| 16 | SC_DLCI_DEBUG = 4, |
| 17 | SC_DLCI_L1A_L23 = 5, |
| 18 | SC_DLCI_LOADER = 9, |
| 19 | SC_DLCI_CONSOLE = 10, |
| 20 | SC_DLCI_ECHO = 128, |
| 21 | _SC_DLCI_MAX |
| 22 | }; |
| 23 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 24 | struct osmo_sercomm_inst; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 25 | /*! \brief call-back function for per-DLC receive handler |
| 26 | * \param[in] sercomm instance on which msg was received |
| 27 | * \param[in] dlci DLC Identifier of received msg |
| 28 | * \param[in] msg received message that needs to be processed */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 29 | typedef void (*dlci_cb_t)(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 30 | |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 31 | /*! \brief one instance of a sercomm multiplex/demultiplex */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 32 | struct osmo_sercomm_inst { |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 33 | /*! \brief Has this instance been initialized? */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 34 | int initialized; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 35 | /*! \brief UART Identifier */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 36 | int uart_id; |
| 37 | |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 38 | /*! \brief transmit side */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 39 | struct { |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 40 | /*! \brief per-DLC queue of pending transmit msgbs */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 41 | struct llist_head dlci_queues[_SC_DLCI_MAX]; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 42 | /*! \brief msgb currently being transmitted */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 43 | struct msgb *msg; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 44 | /*! \brief transmit state */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 45 | int state; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 46 | /*! \brief next to-be-transmitted char in msg */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 47 | uint8_t *next_char; |
| 48 | } tx; |
| 49 | |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 50 | /*! \brief receive side */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 51 | struct { |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 52 | /*! \brief per-DLC handler call-back functions */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 53 | dlci_cb_t dlci_handler[_SC_DLCI_MAX]; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 54 | /*! \brief msgb allocation size for rx msgs */ |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 55 | unsigned int msg_size; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 56 | /*! \brief currently received msgb */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 57 | struct msgb *msg; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 58 | /*! \brief receive state */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 59 | int state; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 60 | /*! \brief DLCI of currently received msgb */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 61 | uint8_t dlci; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame^] | 62 | /*! \brief CTRL of currently received msgb */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 63 | uint8_t ctrl; |
| 64 | } rx; |
| 65 | }; |
| 66 | |
| 67 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 68 | #ifndef HOST_BUILD |
| 69 | #include <uart.h> |
| 70 | /* helper functions for target */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 71 | void osmo_sercomm_bind_uart(struct osmo_sercomm_inst *sercomm, int uart); |
| 72 | int osmo_sercomm_get_uart(struct osmo_sercomm_inst *sercomm); |
| 73 | void osmo_sercomm_change_speed(struct osmo_sercomm_inst *sercomm, enum uart_baudrate bdrt); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 74 | #endif |
| 75 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 76 | void osmo_sercomm_init(struct osmo_sercomm_inst *sercomm); |
| 77 | int osmo_sercomm_initialized(struct osmo_sercomm_inst *sercomm); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 78 | |
| 79 | /* User Interface: Tx */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 80 | void osmo_sercomm_sendmsg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg); |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 81 | unsigned int osmo_sercomm_tx_queue_depth(struct osmo_sercomm_inst *sercomm, uint8_t dlci); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 82 | |
| 83 | /* User Interface: Rx */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 84 | int osmo_sercomm_register_rx_cb(struct osmo_sercomm_inst *sercomm, uint8_t dlci, dlci_cb_t cb); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 85 | |
| 86 | /* Driver Interface */ |
| 87 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 88 | int osmo_sercomm_drv_pull(struct osmo_sercomm_inst *sercomm, uint8_t *ch); |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 89 | int osmo_sercomm_drv_rx_char(struct osmo_sercomm_inst *sercomm, uint8_t ch); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 90 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 91 | static inline struct msgb *osmo_sercomm_alloc_msgb(unsigned int len) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 92 | { |
| 93 | return msgb_alloc_headroom(len+4, 4, "sercomm_tx"); |
| 94 | } |
| 95 | |
| 96 | #endif /* _SERCOMM_H */ |