blob: 10deb7cc965fb79e4f129b5890935590c2d96fdf [file] [log] [blame]
Harald Weltec68af6a2017-04-30 21:21:52 +02001#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 */
14enum 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 Welte13588362017-04-30 23:57:55 +020024struct osmo_sercomm_inst;
25typedef void (*dlci_cb_t)(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg);
Harald Weltecc95f4b2017-04-30 21:39:33 +020026
Harald Welte13588362017-04-30 23:57:55 +020027struct osmo_sercomm_inst {
Harald Weltecc95f4b2017-04-30 21:39:33 +020028 int initialized;
29 int uart_id;
30
31 /* transmit side */
32 struct {
33 struct llist_head dlci_queues[_SC_DLCI_MAX];
34 struct msgb *msg;
35 int state;
36 uint8_t *next_char;
37 } tx;
38
39 /* receive side */
40 struct {
41 dlci_cb_t dlci_handler[_SC_DLCI_MAX];
42 struct msgb *msg;
43 int state;
44 uint8_t dlci;
45 uint8_t ctrl;
46 } rx;
47};
48
49
Harald Weltec68af6a2017-04-30 21:21:52 +020050#ifndef HOST_BUILD
51#include <uart.h>
52/* helper functions for target */
Harald Welte13588362017-04-30 23:57:55 +020053void osmo_sercomm_bind_uart(struct osmo_sercomm_inst *sercomm, int uart);
54int osmo_sercomm_get_uart(struct osmo_sercomm_inst *sercomm);
55void osmo_sercomm_change_speed(struct osmo_sercomm_inst *sercomm, enum uart_baudrate bdrt);
Harald Weltec68af6a2017-04-30 21:21:52 +020056#endif
57
Harald Welte13588362017-04-30 23:57:55 +020058void osmo_sercomm_init(struct osmo_sercomm_inst *sercomm);
59int osmo_sercomm_initialized(struct osmo_sercomm_inst *sercomm);
Harald Weltec68af6a2017-04-30 21:21:52 +020060
61/* User Interface: Tx */
62
63/* user interface for transmitting messages for a given DLCI */
Harald Welte13588362017-04-30 23:57:55 +020064void osmo_sercomm_sendmsg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg);
Harald Weltec68af6a2017-04-30 21:21:52 +020065/* how deep is the Tx queue for a given DLCI */
Harald Welte13588362017-04-30 23:57:55 +020066unsigned int osmo_sercomm_tx_queue_depth(struct osmo_sercomm_inst *sercomm, uint8_t dlci);
Harald Weltec68af6a2017-04-30 21:21:52 +020067
68/* User Interface: Rx */
69
70/* receiving messages for a given DLCI */
Harald Welte13588362017-04-30 23:57:55 +020071int osmo_sercomm_register_rx_cb(struct osmo_sercomm_inst *sercomm, uint8_t dlci, dlci_cb_t cb);
Harald Weltec68af6a2017-04-30 21:21:52 +020072
73/* Driver Interface */
74
75/* fetch one octet of to-be-transmitted serial data. returns 0 if no more data */
Harald Welte13588362017-04-30 23:57:55 +020076int osmo_sercomm_drv_pull(struct osmo_sercomm_inst *sercomm, uint8_t *ch);
Harald Weltec68af6a2017-04-30 21:21:52 +020077/* the driver has received one byte, pass it into sercomm layer.
78 returns 1 in case of success, 0 in case of unrecognized char */
Harald Welte13588362017-04-30 23:57:55 +020079int osmo_sercomm_drv_rx_char(struct osmo_sercomm_inst *sercomm, uint8_t ch);
Harald Weltec68af6a2017-04-30 21:21:52 +020080
Harald Welte13588362017-04-30 23:57:55 +020081static inline struct msgb *osmo_sercomm_alloc_msgb(unsigned int len)
Harald Weltec68af6a2017-04-30 21:21:52 +020082{
83 return msgb_alloc_headroom(len+4, 4, "sercomm_tx");
84}
85
86#endif /* _SERCOMM_H */