[lapdm] Added flow control between L1 and L2, so DM mode does not crash.
[osmocom-bb.git] / src / host / layer23 / include / osmocom / lapdm.h
1 #ifndef _OSMOCOM_LAPDM_H
2 #define _OSMOCOM_LAPDM_H
3
4 #include <stdint.h>
5
6 #include <osmocore/timer.h>
7 #include <osmocore/msgb.h>
8
9 #include <l1a_l23_interface.h>
10
11 enum lapdm_state {
12         LAPDm_STATE_NULL = 0,
13         LAPDm_STATE_IDLE,
14         LAPDm_STATE_SABM_SENT,
15         LAPDm_STATE_MF_EST,
16         LAPDm_STATE_TIMER_RECOV,
17         LAPDm_STATE_DISC_SENT,
18 };
19
20 struct lapdm_entity;
21 struct osmocom_ms;
22
23 struct lapdm_msg_ctx {
24         struct lapdm_datalink *dl;
25         int lapdm_fmt;
26         uint8_t n201;
27         uint8_t chan_nr;
28         uint8_t link_id;
29         uint8_t addr;
30         uint8_t ctrl;
31 };
32
33 /* TS 04.06 / Section 3.5.2 */
34 struct lapdm_datalink {
35         uint8_t V_send; /* seq nr of next I frame to be transmitted */
36         uint8_t V_ack;  /* last frame ACKed by peer */
37         uint8_t N_send; /* ? set to V_send at Tx time*/
38         uint8_t V_recv; /* seq nr of next I frame expected to be received */
39         uint8_t N_recv; /* expected send seq nr of the next received I frame */
40         uint32_t state;
41         int seq_err_cond; /* condition of sequence error */
42         uint8_t own_busy, peer_busy;
43         struct timer_list t200;
44         uint8_t retrans_ctr;
45         struct llist_head send_queue; /* frames from L3 */
46         struct msgb *send_buffer; /* current frame transmitting */
47         int send_out; /* how much was sent from send_buffer */
48         uint8_t tx_hist[8][200]; /* tx history buffer */
49         int tx_length[8]; /* length in history buffer */
50         struct llist_head tx_queue; /* frames to L1 */
51         struct lapdm_msg_ctx mctx; /* context of established connection */
52         struct msgb *rcv_buffer; /* buffer to assemble the received message */
53
54         struct lapdm_entity *entity;
55 };
56
57 enum lapdm_dl_sapi {
58         DL_SAPI0        = 0,
59         DL_SAPI3        = 1,
60         _NR_DL_SAPI
61 };
62
63 struct lapdm_entity {
64         struct lapdm_datalink datalink[_NR_DL_SAPI];
65         int last_tx_dequeue; /* last entity that was dequeued */
66         int tx_pending; /* currently a pending frame not confirmed by L1 */
67         struct osmocom_ms *ms;
68 };
69
70 const char *get_rsl_name(int value);
71 extern const char *lapdm_state_names[];
72
73 /* initialize a LAPDm entity */
74 void lapdm_init(struct lapdm_entity *le, struct osmocom_ms *ms);
75
76 /* deinitialize a LAPDm entity */
77 void lapdm_exit(struct lapdm_entity *le);
78
79 /* input into layer2 (from layer 1) */
80 int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1ctl_info_dl *l1i);
81 int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le);
82
83 /* input into layer2 (from layer 3) */
84 int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms);
85
86 /* sending messages up from L2 to L3 */
87 int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms);
88
89 typedef int (*osmol2_cb_t)(struct msgb *msg, struct osmocom_ms *ms);
90
91 /* register message handler for messages that are sent from L2->L3 */
92 int osmol2_register_handler(struct osmocom_ms *ms, osmol2_cb_t cb);
93
94 #endif /* _OSMOCOM_LAPDM_H */