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