5 #include <osmocore/msgb.h>
6 #include <osmocore/rsl.h>
7 #include <osmocore/tlv.h>
8 #include <osmocore/gsm48_ie.h>
9 #include <osmocore/protocol/gsm_04_08.h>
11 #include <osmocom/bb/common/logging.h>
12 #include <osmocom/bb/common/lapdm.h>
13 #include <osmocom/bb/misc/rslms.h>
14 #include <osmocom/bb/misc/layer3.h>
15 #include <osmocom/bb/common/osmocom_data.h>
16 #include <osmocom/bb/common/l1ctl.h>
23 struct gsm_sysinfo_freq cell_arfcns[1024];
27 static void dump_bcch(struct osmocom_ms *ms, uint8_t tc, const uint8_t *data)
29 struct gsm48_system_information_type_header *si_hdr;
30 si_hdr = (struct gsm48_system_information_type_header *) data;
32 /* GSM 05.02 ยง6.3.1.3 Mapping of BCCH data */
33 switch (si_hdr->system_information) {
34 case GSM48_MT_RR_SYSINFO_1:
35 fprintf(stderr, "\tSI1");
38 fprintf(stderr, " on wrong TC");
40 if (!app_state.has_si1) {
41 struct gsm48_system_information_type_1 *si1 =
42 (struct gsm48_system_information_type_1 *)data;
44 gsm48_decode_freq_list(&app_state.cell_arfcns,
45 si1->cell_channel_description,
46 sizeof(si1->cell_channel_description),
49 app_state.has_si1 = 1;
52 case GSM48_MT_RR_SYSINFO_2:
53 fprintf(stderr, "\tSI2");
56 fprintf(stderr, " on wrong TC");
59 case GSM48_MT_RR_SYSINFO_3:
60 fprintf(stderr, "\tSI3");
62 if (tc != 2 && tc != 6)
63 fprintf(stderr, " on wrong TC");
65 if (app_state.ccch_mode == CCCH_MODE_NONE) {
66 struct gsm48_system_information_type_3 *si3 =
67 (struct gsm48_system_information_type_3 *)data;
69 if (si3->control_channel_desc.ccch_conf == RSL_BCCH_CCCH_CONF_1_C)
70 app_state.ccch_mode = CCCH_MODE_COMBINED;
72 app_state.ccch_mode = CCCH_MODE_NON_COMBINED;
74 l1ctl_tx_ccch_mode_req(ms, app_state.ccch_mode);
77 case GSM48_MT_RR_SYSINFO_4:
78 fprintf(stderr, "\tSI4");
80 if (tc != 3 && tc != 7)
81 fprintf(stderr, " on wrong TC");
84 case GSM48_MT_RR_SYSINFO_5:
85 fprintf(stderr, "\tSI5");
87 case GSM48_MT_RR_SYSINFO_6:
88 fprintf(stderr, "\tSI6");
90 case GSM48_MT_RR_SYSINFO_7:
91 fprintf(stderr, "\tSI7");
94 fprintf(stderr, " on wrong TC");
97 case GSM48_MT_RR_SYSINFO_8:
98 fprintf(stderr, "\tSI8");
101 fprintf(stderr, " on wrong TC");
104 case GSM48_MT_RR_SYSINFO_9:
105 fprintf(stderr, "\tSI9");
108 fprintf(stderr, " on wrong TC");
111 case GSM48_MT_RR_SYSINFO_13:
112 fprintf(stderr, "\tSI13");
114 if (tc != 4 && tc != 0)
115 fprintf(stderr, " on wrong TC");
118 case GSM48_MT_RR_SYSINFO_16:
119 fprintf(stderr, "\tSI16");
122 fprintf(stderr, " on wrong TC");
125 case GSM48_MT_RR_SYSINFO_17:
126 fprintf(stderr, "\tSI17");
129 fprintf(stderr, " on wrong TC");
132 case GSM48_MT_RR_SYSINFO_2bis:
133 fprintf(stderr, "\tSI2bis");
136 fprintf(stderr, " on wrong TC");
139 case GSM48_MT_RR_SYSINFO_2ter:
140 fprintf(stderr, "\tSI2ter");
142 if (tc != 5 && tc != 4)
143 fprintf(stderr, " on wrong TC");
146 case GSM48_MT_RR_SYSINFO_5bis:
147 fprintf(stderr, "\tSI5bis");
149 case GSM48_MT_RR_SYSINFO_5ter:
150 fprintf(stderr, "\tSI5ter");
153 fprintf(stderr, "\tUnknown SI");
157 fprintf(stderr, "\n");
161 /* send location updating request * (as part of RSLms EST IND /
163 static int gsm48_tx_loc_upd_req(struct osmocom_ms *ms, uint8_t chan_nr)
165 struct msgb *msg = msgb_alloc_headroom(256, 16, "loc_upd_req");
166 struct gsm48_hdr *gh;
167 struct gsm48_loc_upd_req *lu_r;
169 DEBUGP(DMM, "chan_nr=%u\n", chan_nr);
171 msg->l3h = msgb_put(msg, sizeof(*gh));
172 gh = (struct gsm48_hdr *) msg->l3h;
173 gh->proto_discr = GSM48_PDISC_MM;
174 gh->msg_type = GSM48_MT_MM_LOC_UPD_REQUEST;
175 lu_r = (struct gsm48_loc_upd_req *) msgb_put(msg, sizeof(*lu_r));
176 lu_r->type = GSM48_LUPD_IMSI_ATT;
178 /* FIXME: set LAI and CM1 */
182 return rslms_tx_rll_req_l3(ms, RSL_MT_EST_REQ, chan_nr, 0, msg);
185 static int gsm48_rx_imm_ass(struct msgb *msg, struct osmocom_ms *ms)
187 struct gsm48_imm_ass *ia = msgb_l3(msg);
188 uint8_t ch_type, ch_subch, ch_ts;
191 /* Discard packet TBF assignement */
192 if (ia->page_mode & 0xf0)
195 /* FIXME: compare RA and GSM time with when we sent RACH req */
197 rsl_dec_chan_nr(ia->chan_desc.chan_nr, &ch_type, &ch_subch, &ch_ts);
199 if (!ia->chan_desc.h0.h) {
203 arfcn = ia->chan_desc.h0.arfcn_low | (ia->chan_desc.h0.arfcn_high << 8);
205 DEBUGP(DRR, "GSM48 IMM ASS (ra=0x%02x, chan_nr=0x%02x, "
206 "ARFCN=%u, TS=%u, SS=%u, TSC=%u) ", ia->req_ref.ra,
207 ia->chan_desc.chan_nr, arfcn, ch_ts, ch_subch,
208 ia->chan_desc.h0.tsc);
210 if (ch_ts >= 4 || ch_subch >= 4) {
211 DEBUGPC(DRR, "UNSUPPORTED!\n");
215 /* request L1 to go to dedicated mode on assigned channel */
216 rv = l1ctl_tx_dm_est_req_h0(ms,
217 arfcn, ia->chan_desc.chan_nr, ia->chan_desc.h0.tsc);
220 uint8_t maio, hsn, ma_len;
221 uint16_t ma[64], arfcn;
224 hsn = ia->chan_desc.h1.hsn;
225 maio = ia->chan_desc.h1.maio_low | (ia->chan_desc.h1.maio_high << 2);
227 DEBUGP(DRR, "GSM48 IMM ASS (ra=0x%02x, chan_nr=0x%02x, "
228 "HSN=%u, MAIO=%u, TS=%u, SS=%u, TSC=%u) ", ia->req_ref.ra,
229 ia->chan_desc.chan_nr, hsn, maio, ch_ts, ch_subch,
230 ia->chan_desc.h1.tsc);
232 /* decode mobile allocation */
234 for (i=1, j=0; i<=1024; i++) {
236 if (app_state.cell_arfcns[arfcn].mask & 0x01) {
237 k = ia->mob_alloc_len - (j>>3) - 1;
238 if (ia->mob_alloc[k] & (1 << (j&7))) {
239 ma[ma_len++] = arfcn;
245 if (ch_ts >= 4 || ch_subch >= 4) {
246 DEBUGPC(DRR, "UNSUPPORTED!\n");
250 /* request L1 to go to dedicated mode on assigned channel */
251 rv = l1ctl_tx_dm_est_req_h1(ms,
252 maio, hsn, ma, ma_len,
253 ia->chan_desc.chan_nr, ia->chan_desc.h1.tsc);
258 rv = gsm48_tx_loc_upd_req(ms, ia->chan_desc.chan_nr);
263 int gsm48_rx_ccch(struct msgb *msg, struct osmocom_ms *ms)
265 struct gsm48_system_information_type_header *sih = msgb_l3(msg);
268 if (sih->rr_protocol_discriminator != GSM48_PDISC_RR)
269 LOGP(DRR, LOGL_ERROR, "PCH pdisc != RR\n");
271 switch (sih->system_information) {
272 case GSM48_MT_RR_PAG_REQ_1:
273 case GSM48_MT_RR_PAG_REQ_2:
274 case GSM48_MT_RR_PAG_REQ_3:
275 /* FIXME: implement decoding of paging request */
277 case GSM48_MT_RR_IMM_ASS:
278 rc = gsm48_rx_imm_ass(msg, ms);
281 LOGP(DRR, LOGL_NOTICE, "unknown PCH/AGCH type 0x%02x\n",
282 sih->system_information);
289 int gsm48_rx_bcch(struct msgb *msg, struct osmocom_ms *ms)
291 /* FIXME: we have lost the gsm frame time until here, need to store it
292 * in some msgb context */
293 //dump_bcch(dl->time.tc, ccch->data);
294 dump_bcch(ms, 0, msg->l3h);
296 /* Req channel logic */
297 if (app_state.ccch_enabled && (app_state.rach_count < 2)) {
298 l1ctl_tx_rach_req(ms, app_state.rach_count,
299 app_state.ccch_mode == CCCH_MODE_COMBINED ? 27 : 51, 0);
300 app_state.rach_count++;
306 void layer3_app_reset(void)
309 app_state.has_si1 = 0;
310 app_state.ccch_mode = CCCH_MODE_NONE;
311 app_state.ccch_enabled = 0;
312 app_state.rach_count = 0;
314 memset(&app_state.cell_arfcns, 0x00, sizeof(app_state.cell_arfcns));