blob: 0b423d901333c368e9c0681aa9bf5f7e5c065560 [file] [log] [blame]
Harald Welte24531ce2018-05-12 16:01:30 +02001#pragma once
2
3#include <stdint.h>
4#include <stdbool.h>
5#include <osmocom/core/msgb.h>
6#include <osmocom/core/linuxlist.h>
7#include <osmocom/core/fsm.h>
8#include <osmocom/core/isdnhdlc.h>
9
10struct osmo_e1f_tx_state {
11 bool remote_alarm;
12 bool crc4_error;
Harald Welte9e7a21a2020-12-19 15:41:03 +010013 bool ais;
Harald Welte24531ce2018-05-12 16:01:30 +020014 /* lower 5 bits: Sa4..Sa8 */
15 uint8_t sa4_sa8;
16 /* frame number 0..15 */
17 uint8_t frame_nr;
18 uint8_t crc4_last_smf;
19 uint8_t crc4;
20};
21
22struct osmo_e1f_rx_state {
23 uint8_t frame_nr;
24 /* history of rceived TS0 octets */
25 uint8_t ts0_history[16];
26 uint8_t ts0_hist_len;
27 /* was a remote alarm received? */
28 bool remote_alarm;
29 bool remote_crc4_error;
30 /* number of TS0 bytes received since entering CRC mframe search */
31 uint8_t num_ts0_in_mframe_search;
32 struct osmo_fsm_inst *fi;
33 /* computed CRC4 */
34 uint8_t crc4_last_smf;
35 uint8_t crc4;
36};
37
38enum osmo_e1f_notify_event {
39 E1_NTFY_EVT_ALIGN_FRAME,
40 E1_NTFY_EVT_ALIGN_CRC_MFRAME,
41 E1_NTFY_EVT_CRC_ERROR,
42 E1_NTFY_EVT_REMOTE_CRC_ERROR,
43 E1_NTFY_EVT_REMOTE_ALARM,
44};
45
46enum osmo_e1f_ts_mode {
47 OSMO_E1F_TS_RAW,
48 OSMO_E1F_TS_HDLC_CRC,
49};
50
51struct osmo_e1f_instance_ts;
52struct osmo_e1f_instance;
53typedef void (*e1_data_cb)(struct osmo_e1f_instance_ts *ts, struct msgb *msg);
54typedef void (*e1_notify_cb)(struct osmo_e1f_instance *e1i, enum osmo_e1f_notify_event evt,
55 bool present, void *data);
56
57struct osmo_e1f_instance_ts {
58 /* timeslot number */
59 uint8_t ts_nr;
60 /* mode in which we operate (RAW/HDLC) */
61 enum osmo_e1f_ts_mode mode;
62 /* back-pointer to e1 instance */
63 struct osmo_e1f_instance *inst;
64 struct {
65 /* optional HDLC encoder state */
66 struct osmo_isdnhdlc_vars hdlc;
67 /* queue of pending to-be-transmitted messages */
68 struct llist_head queue;
69 unsigned long underruns;
70 } tx;
71 struct {
72 /* optional HDLC decoder state */
73 struct osmo_isdnhdlc_vars hdlc;
74 bool enabled;
75 /* how many bytes to buffer before calling call-back */
76 unsigned int granularity;
77 /* current receive buffer */
78 struct msgb *msg;
79 e1_data_cb data_cb;
80 /* private data, relevant to user */
81 void *priv;
82 } rx;
83};
84
85struct osmo_e1f_instance {
86 /* list; currently not used yet */
87 struct llist_head list;
88
89 /* is CRC4 generation + parsing enabled? */
90 bool crc4_enabled;
91 /* notification call-back function */
92 e1_notify_cb notify_cb;
93
94 /* Rx + Tx related state */
95 struct osmo_e1f_tx_state tx;
96 struct osmo_e1f_rx_state rx;
97
98 /* our 32 timeslots (only 1..32 are used) */
99 struct osmo_e1f_instance_ts ts[32];
100
101 /* private data, relevant to user */
102 void *priv;
103};
104
105extern const struct value_string osmo_e1f_notifv_evt_names[];
106
107static inline const char *osmo_e1f_notify_event_name(enum osmo_e1f_notify_event evt) {
108 return get_value_string(osmo_e1f_notifv_evt_names, evt);
109}
110
111int osmo_e1f_init(void);
112struct osmo_e1f_instance_ts *osmo_e1f_instance_ts(struct osmo_e1f_instance *e1i, uint8_t ts_nr);
113int osmo_e1f_instance_init(struct osmo_e1f_instance *e1i, const char *name, e1_notify_cb cb,
114 bool crc4_enabled, void *priv);
115void osmo_e1f_instance_reset(struct osmo_e1f_instance *e1i);
116int osmo_e1f_ts_config(struct osmo_e1f_instance_ts *e1t, e1_data_cb cb, unsigned int granularity,
117 bool enable, enum osmo_e1f_ts_mode mode);
118void osmo_e1f_ts_reset(struct osmo_e1f_instance_ts *e1t);
119
120
121void osmo_e1f_ts_enqueue(struct osmo_e1f_instance_ts *e1t, struct msgb *msg);
122int osmo_e1f_pull_tx_frame(struct osmo_e1f_instance *e1i, uint8_t *out_frame);
123int osmo_e1f_rx_frame(struct osmo_e1f_instance *e1i, const uint8_t *in_frame);