blob: 69e10875a8595e25edacea4afbd30511cb7c997c [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file lapd_core.h
2 * primitive related stuff
3 */
Sylvain Munaut12ba7782014-06-16 10:13:40 +02004#pragma once
rootaf48bed2011-09-26 11:23:06 +02005
6#include <stdint.h>
7
8#include <osmocom/core/timer.h>
9#include <osmocom/core/msgb.h>
10#include <osmocom/gsm/prim.h>
11
12/*! \defgroup lapd LAPD implementation common part
13 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020014 * \file lapd_core.h
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010015 */
rootaf48bed2011-09-26 11:23:06 +020016
Harald Welte00b2faf2020-05-02 19:56:36 +020017#define LOGDL(dl, level, fmt, args...) \
18 LOGP(DLLAPD, level, "(%s) " fmt, (dl)->name, ## args)
19
Neels Hofmeyr87e45502017-06-20 00:17:59 +020020/*! LAPD related primitives (L2<->L3 SAP)*/
rootaf48bed2011-09-26 11:23:06 +020021enum osmo_dl_prim {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020022 PRIM_DL_UNIT_DATA, /*!< DL-UNIT-DATA */
23 PRIM_DL_DATA, /*!< DL-DATA */
24 PRIM_DL_EST, /*!< DL-ESTABLISH */
25 PRIM_DL_REL, /*!< DL-RLEEASE */
26 PRIM_DL_SUSP, /*!< DL-SUSPEND */
27 PRIM_DL_RES, /*!< DL-RESUME */
28 PRIM_DL_RECON, /*!< DL-RECONNECT */
29 PRIM_MDL_ERROR, /*!< MDL-ERROR */
rootaf48bed2011-09-26 11:23:06 +020030};
31
32/* Uses the same values as RLL, so no conversion for GSM is required. */
33#define MDL_CAUSE_T200_EXPIRED 0x01
34#define MDL_CAUSE_REEST_REQ 0x02
35#define MDL_CAUSE_UNSOL_UA_RESP 0x03
36#define MDL_CAUSE_UNSOL_DM_RESP 0x04
37#define MDL_CAUSE_UNSOL_DM_RESP_MF 0x05
38#define MDL_CAUSE_UNSOL_SPRV_RESP 0x06
39#define MDL_CAUSE_SEQ_ERR 0x07
40#define MDL_CAUSE_UFRM_INC_PARAM 0x08
41#define MDL_CAUSE_SFRM_INC_PARAM 0x09
42#define MDL_CAUSE_IFRM_INC_MBITS 0x0a
43#define MDL_CAUSE_IFRM_INC_LEN 0x0b
44#define MDL_CAUSE_FRM_UNIMPL 0x0c
45#define MDL_CAUSE_SABM_MF 0x0d
46#define MDL_CAUSE_SABM_INFO_NOTALL 0x0e
47#define MDL_CAUSE_FRMR 0x0f
48
Neels Hofmeyr87e45502017-06-20 00:17:59 +020049/*! for MDL-ERROR.ind */
rootaf48bed2011-09-26 11:23:06 +020050struct mdl_error_ind_param {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020051 uint8_t cause; /*!< generic cause value */
rootaf48bed2011-09-26 11:23:06 +020052};
53
Neels Hofmeyr87e45502017-06-20 00:17:59 +020054/*! for DL-REL.req */
rootaf48bed2011-09-26 11:23:06 +020055struct dl_rel_req_param {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020056 uint8_t mode; /*!< release mode */
rootaf48bed2011-09-26 11:23:06 +020057};
58
Neels Hofmeyr87e45502017-06-20 00:17:59 +020059/*! primitive header for LAPD DL-SAP primitives */
rootaf48bed2011-09-26 11:23:06 +020060struct osmo_dlsap_prim {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020061 struct osmo_prim_hdr oph; /*!< generic primitive header */
rootaf48bed2011-09-26 11:23:06 +020062 union {
63 struct mdl_error_ind_param error_ind;
64 struct dl_rel_req_param rel_req;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020065 } u; /*!< request-specific data */
rootaf48bed2011-09-26 11:23:06 +020066};
67
Neels Hofmeyr87e45502017-06-20 00:17:59 +020068/*! LAPD mode/role */
rootaf48bed2011-09-26 11:23:06 +020069enum lapd_mode {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020070 LAPD_MODE_USER, /*!< behave like user */
71 LAPD_MODE_NETWORK, /*!< behave like network */
rootaf48bed2011-09-26 11:23:06 +020072};
73
Neels Hofmeyr87e45502017-06-20 00:17:59 +020074/*! LAPD state (Figure B.2/Q.921)*/
rootaf48bed2011-09-26 11:23:06 +020075enum lapd_state {
76 LAPD_STATE_NULL = 0,
77 LAPD_STATE_TEI_UNASS,
78 LAPD_STATE_ASS_TEI_WAIT,
79 LAPD_STATE_EST_TEI_WAIT,
80 LAPD_STATE_IDLE,
81 LAPD_STATE_SABM_SENT,
82 LAPD_STATE_DISC_SENT,
83 LAPD_STATE_MF_EST,
84 LAPD_STATE_TIMER_RECOV,
85};
86
Neels Hofmeyr87e45502017-06-20 00:17:59 +020087/*! LAPD message format (I / S / U) */
rootaf48bed2011-09-26 11:23:06 +020088enum lapd_format {
89 LAPD_FORM_UKN = 0,
90 LAPD_FORM_I,
91 LAPD_FORM_S,
92 LAPD_FORM_U,
93};
94
Neels Hofmeyr87e45502017-06-20 00:17:59 +020095/*! LAPD message context */
rootaf48bed2011-09-26 11:23:06 +020096struct lapd_msg_ctx {
97 struct lapd_datalink *dl;
98 int n201;
99 /* address */
100 uint8_t cr;
101 uint8_t sapi;
102 uint8_t tei;
103 uint8_t lpd;
104 /* control */
105 uint8_t format;
106 uint8_t p_f; /* poll / final bit */
107 uint8_t n_send;
108 uint8_t n_recv;
109 uint8_t s_u; /* S or repectivly U function bits */
110 /* length */
111 int length;
112 uint8_t more;
113};
114
115struct lapd_cr_ent {
116 uint8_t cmd;
117 uint8_t resp;
118};
119
120struct lapd_history {
121 struct msgb *msg; /* message to be sent / NULL, if histoy is empty */
122 int more; /* if message is fragmented */
123};
124
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200125/*! LAPD datalink */
rootaf48bed2011-09-26 11:23:06 +0200126struct lapd_datalink {
127 int (*send_dlsap)(struct osmo_dlsap_prim *dp,
128 struct lapd_msg_ctx *lctx);
129 int (*send_ph_data_req)(struct lapd_msg_ctx *lctx, struct msgb *msg);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100130 int (*update_pending_frames)(struct lapd_msg_ctx *lctx);
rootaf48bed2011-09-26 11:23:06 +0200131 struct {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200132 /*! filled-in once we set the lapd_mode above */
rootaf48bed2011-09-26 11:23:06 +0200133 struct lapd_cr_ent loc2rem;
134 struct lapd_cr_ent rem2loc;
135 } cr;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200136 enum lapd_mode mode; /*!< current mode of link */
137 int use_sabme; /*!< use SABME instead of SABM */
138 int reestablish; /*!< enable reestablish support */
139 int n200, n200_est_rel; /*!< number of retranmissions */
140 struct lapd_msg_ctx lctx; /*!< LAPD context */
141 int maxf; /*!< maximum frame size (after defragmentation) */
142 uint8_t k; /*!< maximum number of unacknowledged frames */
143 uint8_t v_range; /*!< range of sequence numbers */
144 uint8_t v_send; /*!< seq nr of next I frame to be transmitted */
145 uint8_t v_ack; /*!< last frame ACKed by peer */
146 uint8_t v_recv; /*!< seq nr of next I frame expected to be received */
147 uint32_t state; /*!< LAPD state (\ref lapd_state) */
148 int seq_err_cond; /*!< condition of sequence error */
149 uint8_t own_busy; /*!< receiver busy on our side */
150 uint8_t peer_busy; /*!< receiver busy on remote side */
151 int t200_sec, t200_usec; /*!< retry timer (default 1 sec) */
152 int t203_sec, t203_usec; /*!< retry timer (default 10 secs) */
153 struct osmo_timer_list t200; /*!< T200 timer */
154 struct osmo_timer_list t203; /*!< T203 timer */
155 uint8_t retrans_ctr; /*!< re-transmission counter */
156 struct llist_head tx_queue; /*!< frames to L1 */
157 struct llist_head send_queue; /*!< frames from L3 */
158 struct msgb *send_buffer; /*!< current frame transmitting */
159 int send_out; /*!< how much was sent from send_buffer */
160 struct lapd_history *tx_hist; /*!< tx history structure array */
161 uint8_t range_hist; /*!< range of history buffer 2..2^n */
162 struct msgb *rcv_buffer; /*!< buffer to assemble the received message */
163 struct msgb *cont_res; /*!< buffer to store content resolution data on network side, to detect multiple phones on same channel */
Harald Welte00b2faf2020-05-02 19:56:36 +0200164 char *name; /*!< user-provided name */
rootaf48bed2011-09-26 11:23:06 +0200165};
166
Harald Welte00b2faf2020-05-02 19:56:36 +0200167void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range, int maxf)
168 OSMO_DEPRECATED("Use lapd_dl_init2() instead");
169void lapd_dl_init2(struct lapd_datalink *dl, uint8_t k, uint8_t v_range, int maxf, const char *name);
170void lapd_dl_set_name(struct lapd_datalink *dl, const char *name);
rootaf48bed2011-09-26 11:23:06 +0200171void lapd_dl_exit(struct lapd_datalink *dl);
172void lapd_dl_reset(struct lapd_datalink *dl);
173int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode);
174int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx);
175int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx);
176
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100177/*! @} */