[layer23] CM service is now accepted when ciphering has started
[osmocom-bb.git] / src / host / layer23 / include / osmocom / bb / mobile / gsm48_rr.h
1 #ifndef _GSM48_RR_H
2 #define _GSM48_RR_H
3
4 #include "osmocore/protocol/gsm_04_08.h"
5
6 #define GSM_TA_CM                       55385
7
8 /* GSM 04.07 9.1.2 */
9 #define GSM48_RR_EST_REQ                0x10
10 #define GSM48_RR_EST_IND                0x12
11 #define GSM48_RR_EST_CNF                0x11
12 #define GSM48_RR_REL_IND                0x22
13 #define GSM48_RR_SYNC_IND               0x32
14 #define GSM48_RR_DATA_REQ               0x40
15 #define GSM48_RR_DATA_IND               0x42
16 #define GSM48_RR_UNIT_DATA_IND          0x52
17 #define GSM48_RR_ABORT_REQ              0x60
18 #define GSM48_RR_ABORT_IND              0x62
19 #define GSM48_RR_ACT_REQ                0x70
20
21 #define RR_EST_CAUSE_EMERGENCY          1
22 #define RR_EST_CAUSE_REESTAB_TCH_F      2
23 #define RR_EST_CAUSE_REESTAB_TCH_H      3
24 #define RR_EST_CAUSE_REESTAB_2_TCH_H    4
25 #define RR_EST_CAUSE_ANS_PAG_ANY        5
26 #define RR_EST_CAUSE_ANS_PAG_SDCCH      6
27 #define RR_EST_CAUSE_ANS_PAG_TCH_F      7
28 #define RR_EST_CAUSE_ANS_PAG_TCH_ANY    8
29 #define RR_EST_CAUSE_ORIG_TCHF          9
30 #define RR_EST_CAUSE_LOC_UPD            12
31 #define RR_EST_CAUSE_OTHER_SDCCH        13
32
33 #define RR_REL_CAUSE_UNDEFINED          0
34 #define RR_REL_CAUSE_NORMAL             1
35 #define RR_REL_CAUSE_NOT_AUTHORIZED     2
36 #define RR_REL_CAUSE_RA_FAILURE         3
37 #define RR_REL_CAUSE_T3122              4
38 #define RR_REL_CAUSE_TRY_LATER          5
39 #define RR_REL_CAUSE_EMERGENCY_ONLY     6
40 #define RR_REL_CAUSE_LOST_SIGNAL        7
41 #define RR_REL_CAUSE_LINK_FAILURE       8
42
43 #define RR_SYNC_CAUSE_CIPHERING         1
44
45 #define L3_ALLOC_SIZE                   256
46 #define L3_ALLOC_HEADROOM               64
47
48 #define RSL_ALLOC_SIZE                  256
49 #define RSL_ALLOC_HEADROOM              64
50
51 #define RR_ALLOC_SIZE                   256
52 #define RR_ALLOC_HEADROOM               64
53
54 /* GSM 04.08 RR-SAP header */
55 struct gsm48_rr_hdr {
56         uint32_t                msg_type; /* RR-* primitive */
57         uint8_t                 cause;
58 };
59
60 /* GSM 04.07 9.1.1 */
61 #define GSM48_RR_ST_IDLE                0
62 #define GSM48_RR_ST_CONN_PEND           1
63 #define GSM48_RR_ST_DEDICATED           2
64 #define GSM48_RR_ST_REL_PEND            3
65
66 /* modify state */
67 #define GSM48_RR_MOD_NONE               0
68 #define GSM48_RR_MOD_IMM_ASS            1
69 #define GSM48_RR_MOD_ASSIGN             2
70 #define GSM48_RR_MOD_HANDO              3
71 #define GSM48_RR_MOD_ASSIGN_RESUME      4
72 #define GSM48_RR_MOD_HANDO_RESUME       5
73
74 /* channel description */
75 struct gsm48_rr_cd {
76         uint8_t                 tsc;
77         uint8_t                 h; /* using hopping */
78         uint16_t                arfcn; /* dedicated mode */
79         uint8_t                 maio;
80         uint8_t                 hsn;
81         uint8_t                 chan_nr; /* type, slot, sub slot */
82         uint8_t                 link_id;
83         uint8_t                 ind_tx_power; /* last indicated power */
84         uint8_t                 ind_ta; /* last indicated ta */
85         uint8_t                 mob_alloc_lv[9]; /* len + up to 64 bits */
86         uint8_t                 freq_list_lv[131]; /* len + 130 octets */
87         uint8_t                 freq_seq_lv[10]; /* len + 9 octets */
88         uint8_t                 cell_desc_lv[17]; /* len + 16 octets */
89         uint8_t                 start; /* start time available */
90         struct gsm_time         start_tm; /* start time */
91         uint8_t                 mode; /* mode of channel */
92         uint8_t                 cipher; /* ciphering of channel */
93 };
94
95 /* measurements */
96 struct gsm48_rr_meas {
97         uint8_t                 rxlev_full;
98         uint8_t                 rxlev_sub;
99         uint8_t                 rxqual_full;
100         uint8_t                 rxqual_sub;
101         uint8_t                 dtx;
102         uint8_t                 ba;
103         uint8_t                 meas_valid;
104         uint8_t                 ncell_na;
105         uint8_t                 count;
106         uint8_t                 rxlev_nc[6];
107         uint8_t                 bsic_nc[6];
108         uint8_t                 bcch_f_nc[6];
109 };
110
111 struct gsm48_cr_hist {
112         uint8_t                 valid;
113         struct gsm48_req_ref    ref;
114 };
115
116 /* RR sublayer instance */
117 struct gsm48_rrlayer {
118         struct osmocom_ms       *ms;
119         int                     state;
120
121         /* queue for RSL-SAP message upwards */
122         struct llist_head       rsl_upqueue;
123
124         /* queue for messages while RR connection is built up */
125         struct llist_head       downqueue;
126
127         /* timers */
128         struct timer_list       t_starting; /* starting time for chan. access */
129         struct timer_list       t_rel_wait; /* wait for L2 to transmit UA */
130         struct timer_list       t3110;
131         struct timer_list       t3122;
132         struct timer_list       t3124;
133         struct timer_list       t3126;
134         int                     t3126_value;
135 #ifndef TODO
136         struct timer_list       temp_rach_ti; /* temporary timer */
137 #endif
138
139         /* states if RR-EST-REQ was used */
140         uint8_t                 rr_est_req;
141         struct msgb             *rr_est_msg;
142         uint8_t                 est_cause; /* cause used for establishment */
143
144         /* channel request states */
145         uint8_t                 wait_assign; /* waiting for assignment state */
146         uint8_t                 n_chan_req; /* number left, incl. current */
147         uint8_t                 chan_req_val; /* current request value */ 
148         uint8_t                 chan_req_mask; /* mask of random bits */
149
150         /* cr_hist */
151         uint8_t                 cr_ra; /* stores requested ra until confirmed */
152         struct gsm48_cr_hist    cr_hist[3];
153
154         /* V(SD) sequence numbers */
155         uint16_t                v_sd; /* 16 PD 1-bit sequence numbers packed */
156
157         /* current channel descriptions */
158         struct gsm48_rr_cd      cd_now;
159
160         /* current cipering */
161         uint8_t                 cipher_on;
162         uint8_t                 cipher_type; /* 10.5.2.9 */
163
164         /* special states when assigning channel */
165         uint8_t                 modify_state;
166         uint8_t                 hando_sync_ind, hando_rot, hando_nci, hando_act;
167         struct gsm48_rr_cd      cd_last; /* store last cd in case of failure */
168         struct gsm48_rr_cd      cd_before; /* before start time */
169         struct gsm48_rr_cd      cd_after; /* after start time */
170
171         /* measurements */
172         struct gsm48_rr_meas    meas;
173
174         /* BA range */
175         uint8_t                 ba_ranges;
176         uint32_t                ba_range[16];
177
178         /* monitor */
179         uint8_t                 monitor;
180         struct timer_list       t_monitor;
181 };
182
183 const char *get_rr_name(int value);
184 extern int gsm48_rr_init(struct osmocom_ms *ms);
185 extern int gsm48_rr_exit(struct osmocom_ms *ms);
186 int gsm48_rsl_dequeue(struct osmocom_ms *ms);
187 int gsm48_rr_downmsg(struct osmocom_ms *ms, struct msgb *msg);
188 struct msgb *gsm48_l3_msgb_alloc(void);
189 struct msgb *gsm48_rr_msgb_alloc(int msg_type);
190 int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc,
191         uint16_t *mnc, uint16_t *lac);
192 int gsm48_encode_lai(struct gsm48_loc_area_id *lai, uint16_t mcc,
193         uint16_t mnc, uint16_t lac);
194 int gsm48_rr_enc_cm2(struct osmocom_ms *ms, struct gsm48_classmark2 *cm);
195 int gsm48_rr_tx_rand_acc(struct osmocom_ms *ms, struct msgb *msg);
196 int gsm48_rr_los(struct osmocom_ms *ms);
197 int gsm48_rr_rach_conf(struct osmocom_ms *ms, uint32_t fn);
198 extern const char *gsm48_rr_state_names[];
199 int gsm48_rr_start_monitor(struct osmocom_ms *ms);
200 int gsm48_rr_stop_monitor(struct osmocom_ms *ms);
201
202 #endif /* _GSM48_RR_H */