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 /* request L1 to go to dedicated mode on assigned channel */
211 rv = l1ctl_tx_dm_est_req_h0(ms,
212 arfcn, ia->chan_desc.chan_nr, ia->chan_desc.h0.tsc,
216 uint8_t maio, hsn, ma_len;
217 uint16_t ma[64], arfcn;
220 hsn = ia->chan_desc.h1.hsn;
221 maio = ia->chan_desc.h1.maio_low | (ia->chan_desc.h1.maio_high << 2);
223 DEBUGP(DRR, "GSM48 IMM ASS (ra=0x%02x, chan_nr=0x%02x, "
224 "HSN=%u, MAIO=%u, TS=%u, SS=%u, TSC=%u) ", ia->req_ref.ra,
225 ia->chan_desc.chan_nr, hsn, maio, ch_ts, ch_subch,
226 ia->chan_desc.h1.tsc);
228 /* decode mobile allocation */
230 for (i=1, j=0; i<=1024; i++) {
232 if (app_state.cell_arfcns[arfcn].mask & 0x01) {
233 k = ia->mob_alloc_len - (j>>3) - 1;
234 if (ia->mob_alloc[k] & (1 << (j&7))) {
235 ma[ma_len++] = arfcn;
241 /* request L1 to go to dedicated mode on assigned channel */
242 rv = l1ctl_tx_dm_est_req_h1(ms,
243 maio, hsn, ma, ma_len,
244 ia->chan_desc.chan_nr, ia->chan_desc.h1.tsc,
250 rv = gsm48_tx_loc_upd_req(ms, ia->chan_desc.chan_nr);
255 int gsm48_rx_ccch(struct msgb *msg, struct osmocom_ms *ms)
257 struct gsm48_system_information_type_header *sih = msgb_l3(msg);
260 if (sih->rr_protocol_discriminator != GSM48_PDISC_RR)
261 LOGP(DRR, LOGL_ERROR, "PCH pdisc != RR\n");
263 switch (sih->system_information) {
264 case GSM48_MT_RR_PAG_REQ_1:
265 case GSM48_MT_RR_PAG_REQ_2:
266 case GSM48_MT_RR_PAG_REQ_3:
267 /* FIXME: implement decoding of paging request */
269 case GSM48_MT_RR_IMM_ASS:
270 rc = gsm48_rx_imm_ass(msg, ms);
273 LOGP(DRR, LOGL_NOTICE, "unknown PCH/AGCH type 0x%02x\n",
274 sih->system_information);
281 int gsm48_rx_bcch(struct msgb *msg, struct osmocom_ms *ms)
283 /* FIXME: we have lost the gsm frame time until here, need to store it
284 * in some msgb context */
285 //dump_bcch(dl->time.tc, ccch->data);
286 dump_bcch(ms, 0, msg->l3h);
288 /* Req channel logic */
289 if (app_state.ccch_enabled && (app_state.rach_count < 2)) {
290 l1ctl_tx_rach_req(ms, app_state.rach_count, 0,
291 app_state.ccch_mode == CCCH_MODE_COMBINED);
292 app_state.rach_count++;
298 void layer3_app_reset(void)
301 app_state.has_si1 = 0;
302 app_state.ccch_mode = CCCH_MODE_NONE;
303 app_state.ccch_enabled = 0;
304 app_state.rach_count = 0;
306 memset(&app_state.cell_arfcns, 0x00, sizeof(app_state.cell_arfcns));