blob: f39a308c3ad8c9f2fa01bf584de6e38a05203c7b [file] [log] [blame]
Harald Welte0c756eb2018-05-06 16:01:29 +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
9struct osmo_e1_tx_state {
10 bool remote_alarm;
11 bool crc4_error;
12 /* lower 5 bits: Sa4..Sa8 */
13 uint8_t sa4_sa8;
14 /* frame number 0..15 */
15 uint8_t frame_nr;
16 uint8_t crc4_last_smf;
17 uint8_t crc4;
18};
19
20struct osmo_e1_rx_state {
21 uint8_t frame_nr;
22 /* history of rceived TS0 octets */
23 uint8_t ts0_history[16];
24 uint8_t ts0_hist_len;
25 /* was a remote alarm received? */
26 bool remote_alarm;
27 bool remote_crc4_error;
28 /* number of TS0 bytes received since entering CRC mframe search */
29 uint8_t num_ts0_in_mframe_search;
30 struct osmo_fsm_inst *fi;
31 /* computed CRC4 */
32 uint8_t crc4_last_smf;
33 uint8_t crc4;
34};
35
36enum osmo_e1_notify_event {
37 E1_NTFY_EVT_ALIGN_FRAME,
38 E1_NTFY_EVT_ALIGN_CRC_MFRAME,
39 E1_NTFY_EVT_CRC_ERROR,
40 E1_NTFY_EVT_REMOTE_CRC_ERROR,
41 E1_NTFY_EVT_REMOTE_ALARM,
42};
43
44struct osmo_e1_instance_ts;
45struct osmo_e1_instance;
46typedef void (*e1_data_cb)(struct osmo_e1_instance_ts *ts, struct msgb *msg);
47typedef void (*e1_notify_cb)(struct osmo_e1_instance *e1i, enum osmo_e1_notify_event evt,
48 bool present, void *data);
49
50struct osmo_e1_instance_ts {
51 /* timeslot number */
52 uint8_t ts_nr;
53 /* back-pointer to e1 instance */
54 struct osmo_e1_instance *inst;
55 struct {
56 /* queue of pending to-be-transmitted messages */
57 struct llist_head queue;
58 unsigned long underruns;
59 } tx;
60 struct {
61 bool enabled;
62 /* how many bytes to buffer before calling call-back */
63 unsigned int granularity;
64 /* current receive buffer */
65 struct msgb *msg;
66 e1_data_cb data_cb;
67 /* private data, relevant to user */
68 void *priv;
69 } rx;
70};
71
72struct osmo_e1_instance {
73 /* list; currently not used yet */
74 struct llist_head list;
75
76 /* is CRC4 generation + parsing enabled? */
77 bool crc4_enabled;
78 /* notification call-back function */
79 e1_notify_cb notify_cb;
80
81 /* Rx + Tx related state */
82 struct osmo_e1_tx_state tx;
83 struct osmo_e1_rx_state rx;
84
85 /* our 32 timeslots (only 1..32 are used) */
86 struct osmo_e1_instance_ts ts[32];
87
88 /* private data, relevant to user */
89 void *priv;
90};
91
92extern const struct value_string osmo_e1_notifv_evt_names[];
93
94static inline const char *osmo_e1_notify_event_name(enum osmo_e1_notify_event evt) {
95 return get_value_string(osmo_e1_notifv_evt_names, evt);
96}
97
98int osmo_e1_init(void);
99struct osmo_e1_instance_ts *osmo_e1_instance_ts(struct osmo_e1_instance *e1i, uint8_t ts_nr);
100int osmo_e1_instance_init(struct osmo_e1_instance *e1i, const char *name, e1_notify_cb cb,
101 bool crc4_enabled, void *priv);
102void osmo_e1_instance_reset(struct osmo_e1_instance *e1i);
103int osmo_e1_ts_config(struct osmo_e1_instance_ts *e1t, e1_data_cb cb, unsigned int granularity,
104 bool enable);
105void osmo_e1_ts_reset(struct osmo_e1_instance_ts *e1t);
106
107
108void osmo_e1_ts_enqueue(struct osmo_e1_instance_ts *e1t, struct msgb *msg);
109int osmo_e1_pull_tx_frame(struct osmo_e1_instance *e1i, uint8_t *out_frame);
110int osmo_e1_rx_frame(struct osmo_e1_instance *e1i, const uint8_t *in_frame);