b96697256a1e15d1938e616756edd0f9534cb079
[osmocom-bb.git] / src / host / layer23 / include / osmocom / bb / common / lapdm.h
1 #ifndef _OSMOCOM_LAPDM_H
2 #define _OSMOCOM_LAPDM_H
3
4 #include <stdint.h>
5
6 #include <osmocom/core/timer.h>
7 #include <osmocom/core/msgb.h>
8 #include <osmocom/gsm/prim.h>
9
10 /* primitive related sutff */
11
12 enum osmo_ph_prim {
13         PRIM_PH_DATA,           /* PH-DATA */
14         PRIM_PH_RACH,           /* PH-RANDOM_ACCESS */
15         PRIM_PH_CONN,           /* PH-CONNECT */
16         PRIM_PH_EMPTY_FRAME,    /* PH-EMPTY_FRAME */
17         PRIM_PH_RTS,            /* PH-RTS */
18 };
19
20 /* for PH-RANDOM_ACCESS.req */
21 struct ph_rach_req_param {
22         uint8_t ra;
23         uint8_t ta;
24         uint8_t tx_power;
25         uint8_t is_combined_ccch;
26         uint16_t offset;
27 };
28
29 /* for PH-RANDOM_ACCESS.ind */
30 struct ph_rach_ind_param {
31         uint8_t ra;
32         uint32_t fn;
33 };
34
35 /* for PH-[UNIT]DATA.{req,ind} */
36 struct ph_data_param {
37         uint8_t link_id;
38         uint8_t chan_nr;
39 };
40
41 struct ph_conn_ind_param {
42         uint32_t fn;
43 };
44
45 struct osmo_phsap_prim {
46         struct osmo_prim_hdr oph;
47         union {
48                 struct ph_data_param data;
49                 struct ph_rach_req_param rach_req;
50                 struct ph_rach_ind_param rach_ind;
51                 struct ph_conn_ind_param conn_ind;
52         } u;
53 };
54
55 enum lapdm_state {
56         LAPDm_STATE_NULL = 0,
57         LAPDm_STATE_IDLE,
58         LAPDm_STATE_SABM_SENT,
59         LAPDm_STATE_MF_EST,
60         LAPDm_STATE_TIMER_RECOV,
61         LAPDm_STATE_DISC_SENT,
62 };
63
64 struct lapdm_entity;
65
66 struct lapdm_msg_ctx {
67         struct lapdm_datalink *dl;
68         int lapdm_fmt;
69         uint8_t n201;
70         uint8_t chan_nr;
71         uint8_t link_id;
72         uint8_t addr;
73         uint8_t ctrl;
74         uint8_t ta_ind;
75         uint8_t tx_power_ind;
76 };
77
78 /* TS 04.06 / Section 3.5.2 */
79 struct lapdm_datalink {
80         uint8_t V_send; /* seq nr of next I frame to be transmitted */
81         uint8_t V_ack;  /* last frame ACKed by peer */
82         uint8_t N_send; /* ? set to V_send at Tx time*/
83         uint8_t V_recv; /* seq nr of next I frame expected to be received */
84         uint8_t N_recv; /* expected send seq nr of the next received I frame */
85         uint32_t state;
86         int seq_err_cond; /* condition of sequence error */
87         uint8_t own_busy, peer_busy;
88         struct osmo_timer_list t200;
89         uint8_t retrans_ctr;
90         struct llist_head send_queue; /* frames from L3 */
91         struct msgb *send_buffer; /* current frame transmitting */
92         int send_out; /* how much was sent from send_buffer */
93         uint8_t tx_hist[8][200]; /* tx history buffer */
94         int tx_length[8]; /* length in history buffer */
95         struct llist_head tx_queue; /* frames to L1 */
96         struct lapdm_msg_ctx mctx; /* context of established connection */
97         struct msgb *rcv_buffer; /* buffer to assemble the received message */
98
99         struct lapdm_entity *entity;
100 };
101
102 enum lapdm_dl_sapi {
103         DL_SAPI0        = 0,
104         DL_SAPI3        = 1,
105         _NR_DL_SAPI
106 };
107
108 typedef int (*lapdm_cb_t)(struct msgb *msg, struct lapdm_entity *le, void *ctx);
109
110 /* register message handler for messages that are sent from L2->L3 */
111 struct lapdm_entity {
112         struct lapdm_datalink datalink[_NR_DL_SAPI];
113         int last_tx_dequeue; /* last entity that was dequeued */
114         int tx_pending; /* currently a pending frame not confirmed by L1 */
115
116         void *l1_ctx;   /* context for layer1 instance */
117         void *l3_ctx;   /* context for layer3 instance */
118
119         osmo_prim_cb l1_prim_cb;
120         lapdm_cb_t l3_cb;       /* callback for sending stuff to L3 */
121
122         struct lapdm_channel *lapdm_ch;
123 };
124
125 /* the two lapdm_entities that form a GSM logical channel (ACCH + DCCH) */
126 struct lapdm_channel {
127         struct llist_head list;
128         char *name;
129         struct lapdm_entity lapdm_acch;
130         struct lapdm_entity lapdm_dcch;
131 };
132
133 const char *get_rsl_name(int value);
134 extern const char *lapdm_state_names[];
135
136 /* initialize a LAPDm entity */
137 void lapdm_entity_init(struct lapdm_entity *le);
138 void lapdm_channel_init(struct lapdm_channel *lc);
139
140 /* deinitialize a LAPDm entity */
141 void lapdm_entity_exit(struct lapdm_entity *le);
142 void lapdm_channel_exit(struct lapdm_channel *lc);
143
144 /* input into layer2 (from layer 1) */
145 int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le);
146
147 /* input into layer2 (from layer 3) */
148 int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc);
149
150 void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx);
151 void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx);
152
153 #endif /* _OSMOCOM_LAPDM_H */