Harald Welte | 0c756eb | 2018-05-06 16:01:29 +0200 | [diff] [blame] | 1 | #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> |
Harald Welte | 8a95fd5 | 2018-05-11 21:04:03 +0200 | [diff] [blame] | 8 | #include <osmocom/core/isdnhdlc.h> |
Harald Welte | 0c756eb | 2018-05-06 16:01:29 +0200 | [diff] [blame] | 9 | |
| 10 | struct osmo_e1_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 | |
| 21 | struct osmo_e1_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 | |
| 37 | enum osmo_e1_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 | |
Harald Welte | 8a95fd5 | 2018-05-11 21:04:03 +0200 | [diff] [blame] | 45 | enum osmo_e1_ts_mode { |
| 46 | OSMO_E1_TS_RAW, |
| 47 | OSMO_E1_TS_HDLC_CRC, |
| 48 | }; |
| 49 | |
Harald Welte | 0c756eb | 2018-05-06 16:01:29 +0200 | [diff] [blame] | 50 | struct osmo_e1_instance_ts; |
| 51 | struct osmo_e1_instance; |
| 52 | typedef void (*e1_data_cb)(struct osmo_e1_instance_ts *ts, struct msgb *msg); |
| 53 | typedef void (*e1_notify_cb)(struct osmo_e1_instance *e1i, enum osmo_e1_notify_event evt, |
| 54 | bool present, void *data); |
| 55 | |
| 56 | struct osmo_e1_instance_ts { |
| 57 | /* timeslot number */ |
| 58 | uint8_t ts_nr; |
Harald Welte | 8a95fd5 | 2018-05-11 21:04:03 +0200 | [diff] [blame] | 59 | /* mode in which we operate (RAW/HDLC) */ |
| 60 | enum osmo_e1_ts_mode mode; |
Harald Welte | 0c756eb | 2018-05-06 16:01:29 +0200 | [diff] [blame] | 61 | /* back-pointer to e1 instance */ |
| 62 | struct osmo_e1_instance *inst; |
| 63 | struct { |
Harald Welte | 8a95fd5 | 2018-05-11 21:04:03 +0200 | [diff] [blame] | 64 | /* optional HDLC encoder state */ |
| 65 | struct osmo_isdnhdlc_vars hdlc; |
Harald Welte | 0c756eb | 2018-05-06 16:01:29 +0200 | [diff] [blame] | 66 | /* queue of pending to-be-transmitted messages */ |
| 67 | struct llist_head queue; |
| 68 | unsigned long underruns; |
| 69 | } tx; |
| 70 | struct { |
Harald Welte | 8a95fd5 | 2018-05-11 21:04:03 +0200 | [diff] [blame] | 71 | /* optional HDLC decoder state */ |
| 72 | struct osmo_isdnhdlc_vars hdlc; |
Harald Welte | 0c756eb | 2018-05-06 16:01:29 +0200 | [diff] [blame] | 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 | |
| 84 | struct osmo_e1_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_e1_tx_state tx; |
| 95 | struct osmo_e1_rx_state rx; |
| 96 | |
| 97 | /* our 32 timeslots (only 1..32 are used) */ |
| 98 | struct osmo_e1_instance_ts ts[32]; |
| 99 | |
| 100 | /* private data, relevant to user */ |
| 101 | void *priv; |
| 102 | }; |
| 103 | |
| 104 | extern const struct value_string osmo_e1_notifv_evt_names[]; |
| 105 | |
| 106 | static inline const char *osmo_e1_notify_event_name(enum osmo_e1_notify_event evt) { |
| 107 | return get_value_string(osmo_e1_notifv_evt_names, evt); |
| 108 | } |
| 109 | |
| 110 | int osmo_e1_init(void); |
| 111 | struct osmo_e1_instance_ts *osmo_e1_instance_ts(struct osmo_e1_instance *e1i, uint8_t ts_nr); |
| 112 | int osmo_e1_instance_init(struct osmo_e1_instance *e1i, const char *name, e1_notify_cb cb, |
| 113 | bool crc4_enabled, void *priv); |
| 114 | void osmo_e1_instance_reset(struct osmo_e1_instance *e1i); |
| 115 | int osmo_e1_ts_config(struct osmo_e1_instance_ts *e1t, e1_data_cb cb, unsigned int granularity, |
Harald Welte | 8a95fd5 | 2018-05-11 21:04:03 +0200 | [diff] [blame] | 116 | bool enable, enum osmo_e1_ts_mode mode); |
Harald Welte | 0c756eb | 2018-05-06 16:01:29 +0200 | [diff] [blame] | 117 | void osmo_e1_ts_reset(struct osmo_e1_instance_ts *e1t); |
| 118 | |
| 119 | |
| 120 | void osmo_e1_ts_enqueue(struct osmo_e1_instance_ts *e1t, struct msgb *msg); |
| 121 | int osmo_e1_pull_tx_frame(struct osmo_e1_instance *e1i, uint8_t *out_frame); |
| 122 | int osmo_e1_rx_frame(struct osmo_e1_instance *e1i, const uint8_t *in_frame); |