e6c5771b8014abc00ee9567b76afeb68dfe7dd5a
[osmocom-bb.git] / src / host / layer23 / src / misc / cell_log.c
1 /* Cell Scanning code for OsmocomBB */
2
3 /* (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
4  *
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <errno.h>
29
30 #include <l1ctl_proto.h>
31
32 #include <osmocore/logging.h>
33 #include <osmocore/timer.h>
34 #include <osmocore/signal.h>
35 #include <osmocore/msgb.h>
36 #include <osmocore/gsm_utils.h>
37 #include <osmocore/protocol/gsm_04_08.h>
38 #include <osmocore/rsl.h>
39
40 #include <osmocom/bb/common/l1ctl.h>
41 #include <osmocom/bb/common/osmocom_data.h>
42 #include <osmocom/bb/common/lapdm.h>
43 #include <osmocom/bb/common/logging.h>
44 #include <osmocom/bb/common/networks.h>
45 #include <osmocom/bb/common/gps.h>
46 #include <osmocom/bb/misc/cell_log.h>
47 #include "../../../gsmmap/geo.h"
48
49 #define READ_WAIT       2, 0
50 #define RACH_MAX        2
51 #define RACH_WAIT       0, 900000
52 #define MIN_RXLEV       -106
53 #define MAX_DIST        2000
54
55 enum {
56         SCAN_STATE_PM,
57         SCAN_STATE_SYNC,
58         SCAN_STATE_READ,
59         SCAN_STATE_RACH,
60 };
61
62 /* ranges of bands */
63 static uint16_t band_range[][2] = {{0, 124}, {512, 885}, {955, 1023}, {0, 0}};
64
65 #define INFO_FLG_PM     1
66 #define INFO_FLG_SYNC   2
67 #define INFO_FLG_SI1    4
68 #define INFO_FLG_SI2    8
69 #define INFO_FLG_SI2bis 16
70 #define INFO_FLG_SI2ter 32
71 #define INFO_FLG_SI3    64
72 #define INFO_FLG_SI4    128
73
74 static struct osmocom_ms *ms;
75 static struct timer_list timer;
76
77 static struct pm_info {
78         uint16_t flags;
79         int8_t rxlev;
80 } pm[1024];
81
82 static int started = 0;
83 static int state;
84 static int8_t min_rxlev = MIN_RXLEV;
85 static int sync_count;
86 static int pm_index, pm_gps_valid;
87 static double pm_gps_x, pm_gps_y, pm_gps_z;
88 static int arfcn;
89 static int rach_count;
90 static FILE *logfp = NULL;
91 static char *logname = "/var/log/osmocom.log";
92
93 static struct gsm48_sysinfo sysinfo;
94
95 static struct log_si {
96         uint16_t flags;
97         uint8_t bsic;
98         int8_t rxlev;
99         uint16_t mcc, mnc, lac, cellid;
100         uint8_t ta;
101         double latitude, longitude;
102 } log_si;
103
104 struct rach_ref {
105         uint8_t valid;
106         uint8_t cr;
107         uint8_t t1, t2, t3;
108 } rach_ref;
109
110 #define LOGFILE(fmt, args...) \
111         fprintf(logfp, fmt, ## args);
112 #define LOGFLUSH() \
113         fflush(logfp);
114
115 static void start_sync(void);
116 static void start_rach(void);
117 static void start_pm(void);
118
119 static void log_gps(void)
120 {
121         if (!gps.enable || !gps.valid)
122                 return;
123         LOGFILE("position %.8f %.8f\n", gps.longitude, gps.latitude);
124 }
125
126 static void log_time(void)
127 {
128         time_t now;
129
130         if (gps.enable && gps.valid)
131                 now = gps.gmt;
132         else
133                 time(&now);
134         LOGFILE("time %lu\n", now);
135 }
136
137 static void log_frame(char *tag, uint8_t *data)
138 {
139         int i;
140
141         LOGFILE("%s", tag);
142         for (i = 0; i < 23; i++)
143                 LOGFILE(" %02x", *data++);
144         LOGFILE("\n");
145 }
146
147 static void log_pm(void)
148 {
149         int count = 0, i;
150
151         LOGFILE("[power]\n");
152         log_time();
153         log_gps();
154         for (i = 0; i <= 1023; i++) {
155                 if ((pm[i].flags & INFO_FLG_PM)) {
156                         if (!count)
157                                 LOGFILE("arfcn %d", i);
158                         LOGFILE(" %d", pm[i].rxlev);
159                         count++;
160                         if (count == 12) {
161                                 LOGFILE("\n");
162                                 count = 0;
163                         }
164                 } else {
165                         if (count) {
166                                 LOGFILE("\n");
167                                 count = 0;
168                         }
169                 }
170         }
171         if (count)
172                 LOGFILE("\n");
173
174         LOGFILE("\n");
175         LOGFLUSH();
176 }
177
178 static void log_sysinfo(void)
179 {
180         struct rx_meas_stat *meas = &ms->meas;
181         struct gsm48_sysinfo *s = &sysinfo;
182         int8_t rxlev;
183         char ta_str[32] = "";
184
185         if (log_si.ta != 0xff)
186                 sprintf(ta_str, " TA=%d", log_si.ta);
187
188         LOGP(DSUM, LOGL_INFO, "Cell: ARFCN=%d MCC=%s MNC=%s (%s, %s)%s\n",
189                 arfcn, gsm_print_mcc(s->mcc), gsm_print_mnc(s->mnc),
190                 gsm_get_mcc(s->mcc), gsm_get_mnc(s->mcc, s->mnc), ta_str);
191
192         LOGFILE("[sysinfo]\n");
193         LOGFILE("arfcn %d\n", s->arfcn);
194         log_time();
195         log_gps();
196         LOGFILE("bsic %d,%d\n", s->bsic >> 3, s->bsic & 7);
197         rxlev = meas->rxlev / meas->frames - 110;
198         LOGFILE("rxlev %d\n", rxlev);
199         if (s->si1)
200                 log_frame("si1", s->si1_msg);
201         if (s->si2)
202                 log_frame("si2", s->si2_msg);
203         if (s->si2bis)
204                 log_frame("si2bis", s->si2b_msg);
205         if (s->si2ter)
206                 log_frame("si2ter", s->si2t_msg);
207         if (s->si3)
208                 log_frame("si3", s->si3_msg);
209         if (s->si4)
210                 log_frame("si4", s->si4_msg);
211         if (log_si.ta != 0xff)
212                 LOGFILE("ta %d\n", log_si.ta);
213
214         LOGFILE("\n");
215         LOGFLUSH();
216 }
217
218 static void timeout_cb(void *arg)
219 {
220         switch (state) {
221         case SCAN_STATE_READ:
222                 LOGP(DRR, LOGL_INFO, "Timeout reading BCCH\n");
223                 start_sync();
224                 break;
225         case SCAN_STATE_RACH:
226                 LOGP(DRR, LOGL_INFO, "Timeout on RACH\n");
227                 rach_count++;
228                 start_rach();
229                 break;
230         }
231 }
232
233 static void stop_timer(void)
234 {
235         if (bsc_timer_pending(&timer))
236                 bsc_del_timer(&timer);
237 }
238
239 static void start_timer(int sec, int micro)
240 {
241         stop_timer();
242         timer.cb = timeout_cb;
243         timer.data = ms;
244         bsc_schedule_timer(&timer, sec, micro);
245 }
246
247 static void start_rach(void)
248 {
249         struct gsm48_sysinfo *s = &sysinfo;
250         uint8_t chan_req_val, chan_req_mask;
251         struct msgb *nmsg;
252         struct abis_rsl_cchan_hdr *ncch;
253
254         if (rach_count == RACH_MAX) {
255                 log_sysinfo();
256                 start_sync();
257                 return;
258         }
259
260         state = SCAN_STATE_RACH;
261
262         if (s->neci) {
263                 chan_req_mask = 0x0f;
264                 chan_req_val = 0x01;
265                 LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x "
266                         "(OTHER with NECI)\n", chan_req_val);
267         } else {
268                 chan_req_mask = 0x1f;
269                 chan_req_val = 0xe0;
270                 LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x (OTHER no NECI)\n",
271                         chan_req_val);
272         }
273
274         rach_ref.valid = 0;
275         rach_ref.cr = random();
276         rach_ref.cr &= chan_req_mask;
277         rach_ref.cr |= chan_req_val;
278
279         nmsg = msgb_alloc_headroom(RSL_ALLOC_SIZE+RSL_ALLOC_HEADROOM,
280                 RSL_ALLOC_HEADROOM, "GSM 04.06 RSL");
281         if (!nmsg)
282                 return;
283         nmsg->l2h = nmsg->data;
284         ncch = (struct abis_rsl_cchan_hdr *) msgb_put(nmsg, sizeof(*ncch)
285                                                         + 4 + 2 + 2);
286         rsl_init_cchan_hdr(ncch, RSL_MT_CHAN_RQD);
287         ncch->chan_nr = RSL_CHAN_RACH;
288         ncch->data[0] = RSL_IE_REQ_REFERENCE;
289         ncch->data[1] = rach_ref.cr;
290         ncch->data[2] = (s->ccch_conf == 1) << 7;
291         ncch->data[3] = 0;
292         ncch->data[4] = RSL_IE_ACCESS_DELAY;
293         ncch->data[5] = 0; /* no delay */ 
294         ncch->data[6] = RSL_IE_MS_POWER;
295         ncch->data[7] = 0; /* full power */
296
297         start_timer(RACH_WAIT);
298
299         rslms_recvmsg(nmsg, ms);
300 }
301
302 static void start_sync(void)
303 {
304         int rxlev = -128;
305         int i, dist = 0;
306         char dist_str[32] = "";
307
308         arfcn = 0xffff;
309         for (i = 0; i <= 1023; i++) {
310                 if ((pm[i].flags & INFO_FLG_PM)
311                  && !(pm[i].flags & INFO_FLG_SYNC)) {
312                         if (pm[i].rxlev > rxlev) {
313                                 rxlev = pm[i].rxlev;
314                                 arfcn = i;
315                         }
316                 }
317         }
318         if (pm_gps_valid && gps.valid) {
319                 double x, y, z;
320
321                 geo2space(&x, &y, &z, gps.longitude, gps.latitude);
322                 dist = distinspace(pm_gps_x, pm_gps_y, pm_gps_z, x, y, z);
323                 sprintf(dist_str, "  dist %d", (int)dist);
324         }
325         if (dist > MAX_DIST || arfcn == 0xffff || rxlev < min_rxlev) {
326                 memset(pm, 0, sizeof(pm));
327                 pm_index = 0;
328                 sync_count = 0;
329                 start_pm();
330                 return;
331         }
332         pm[arfcn].flags |= INFO_FLG_SYNC;
333         LOGP(DSUM, LOGL_INFO, "Sync ARFCN %d (rxlev %d, %d syncs "
334                 "left)%s\n", arfcn, pm[arfcn].rxlev, sync_count--, dist_str);
335         memset(&sysinfo, 0, sizeof(sysinfo));
336         sysinfo.arfcn = arfcn;
337         state = SCAN_STATE_SYNC;
338         l1ctl_tx_reset_req(ms, L1CTL_RES_T_FULL);
339         l1ctl_tx_fbsb_req(ms, arfcn, L1CTL_FBSB_F_FB01SB, 100, 0,
340                 CCCH_MODE_NONE);
341 }
342
343 static void start_pm(void)
344 {
345         uint16_t from, to;
346
347         state = SCAN_STATE_PM;
348         from = band_range[pm_index][0];
349         to = band_range[pm_index][1];
350
351         if (from == 0 && to == 0) {
352                 LOGP(DSUM, LOGL_INFO, "Measurement done\n");
353                 pm_gps_valid = gps.enable && gps.valid;
354                 if (pm_gps_valid)
355                         geo2space(&pm_gps_x, &pm_gps_y, &pm_gps_z,
356                                 gps.longitude, gps.latitude);
357                 log_pm();
358                 start_sync();
359                 return;
360         }
361         LOGP(DSUM, LOGL_INFO, "Measure from %d to %d\n", from, to);
362         l1ctl_tx_reset_req(ms, L1CTL_RES_T_FULL);
363         l1ctl_tx_pm_req_range(ms, from, to);
364 }
365
366 static int signal_cb(unsigned int subsys, unsigned int signal,
367                      void *handler_data, void *signal_data)
368 {
369         struct osmobb_meas_res *mr;
370         struct osmobb_fbsb_res *fr;
371         uint16_t index;
372
373         if (subsys != SS_L1CTL)
374                 return 0;
375
376         switch (signal) {
377         case S_L1CTL_PM_RES:
378                 mr = signal_data;
379                 index = mr->band_arfcn & 0x3ff;
380                 pm[index].flags |= INFO_FLG_PM;
381                 pm[index].rxlev = mr->rx_lev - 110;
382                 if (pm[index].rxlev >= min_rxlev)
383                         sync_count++;
384 //              printf("rxlev %d = %d (sync_count %d)\n", index, pm[index].rxlev, sync_count);
385                 break;
386         case S_L1CTL_PM_DONE:
387                 pm_index++;
388                 start_pm();
389                 break;
390         case S_L1CTL_FBSB_RESP:
391                 fr = signal_data;
392                 sysinfo.bsic = fr->bsic;
393                 state = SCAN_STATE_READ;
394                 memset(&ms->meas, 0, sizeof(ms->meas));
395                 memset(&log_si, 0, sizeof(log_si));
396                 log_si.flags |= INFO_FLG_SYNC;
397                 log_si.ta = 0xff; /* invalid */
398                 start_timer(READ_WAIT);
399                 LOGP(DRR, LOGL_INFO, "Synchronized, start reading\n");
400                 break;
401         case S_L1CTL_FBSB_ERR:
402                 LOGP(DRR, LOGL_INFO, "Sync failed\n");
403                 start_sync();
404                 break;
405         case S_L1CTL_RESET:
406                 if (started)
407                         break;
408                 started = 1;
409                 memset(pm, 0, sizeof(pm));
410                 pm_index = 0;
411                 sync_count = 0;
412                 start_pm();
413         }
414         return 0;
415 }
416
417 static int ta_result(uint8_t ta)
418 {
419         stop_timer();
420
421         if (ta == 0xff)
422                 LOGP(DSUM, LOGL_INFO, "Got assignment reject\n");
423         else {
424                 LOGP(DSUM, LOGL_DEBUG, "Got assignment TA = %d\n", ta);
425                 log_si.ta = ta;
426         }
427
428         log_sysinfo();
429         start_sync();
430
431         return 0;
432 }
433
434 /* match request reference agains request */
435 static int match_ra(struct osmocom_ms *ms, struct gsm48_req_ref *ref)
436 {
437         uint8_t ia_t1, ia_t2, ia_t3;
438
439         /* filter confirmed RACH requests only */
440         if (rach_ref.valid && ref->ra == rach_ref.cr) {
441                 ia_t1 = ref->t1;
442                 ia_t2 = ref->t2;
443                 ia_t3 = (ref->t3_high << 3) | ref->t3_low;
444                 if (ia_t1 == rach_ref.t1 && ia_t2 == rach_ref.t2
445                          && ia_t3 == rach_ref.t3) {
446                         LOGP(DRR, LOGL_INFO, "request %02x matches "
447                                 "(fn=%d,%d,%d)\n", ref->ra, ia_t1, ia_t2,
448                                         ia_t3);
449                         return 1;
450                 } else
451                         LOGP(DRR, LOGL_INFO, "request %02x matches but not "
452                                 "frame number (IMM.ASS fn=%d,%d,%d != RACH "
453                                 "fn=%d,%d,%d)\n", ref->ra, ia_t1, ia_t2, ia_t3,
454                                 rach_ref.t1, rach_ref.t2, rach_ref.t3);
455         }
456
457         return 0;
458 }
459
460 /* 9.1.18 IMMEDIATE ASSIGNMENT is received */
461 static int imm_ass(struct osmocom_ms *ms, struct msgb *msg)
462 {
463         struct gsm48_imm_ass *ia = msgb_l3(msg);
464
465         LOGP(DRR, LOGL_INFO, "IMMEDIATE ASSIGNMENT:\n");
466
467         if (state != SCAN_STATE_RACH) {
468                 LOGP(DRR, LOGL_INFO, "Not for us, no request.\n");
469                 return 0;
470         }
471
472         /* request ref */
473         if (match_ra(ms, &ia->req_ref)) {
474                 return ta_result(ia->timing_advance);
475         }
476         LOGP(DRR, LOGL_INFO, "Request, but not for us.\n");
477
478         return 0;
479 }
480
481 /* 9.1.19 IMMEDIATE ASSIGNMENT EXTENDED is received */
482 static int imm_ass_ext(struct osmocom_ms *ms, struct msgb *msg)
483 {
484         struct gsm48_imm_ass_ext *ia = msgb_l3(msg);
485
486         LOGP(DRR, LOGL_INFO, "IMMEDIATE ASSIGNMENT EXTENDED:\n");
487
488         if (state != SCAN_STATE_RACH) {
489                 LOGP(DRR, LOGL_INFO, "Not for us, no request.\n");
490                 return 0;
491         }
492
493         /* request ref 1 */
494         if (match_ra(ms, &ia->req_ref1)) {
495                 return ta_result(ia->timing_advance1);
496         }
497         /* request ref 2 */
498         if (match_ra(ms, &ia->req_ref2)) {
499                 return ta_result(ia->timing_advance2);
500         }
501         LOGP(DRR, LOGL_INFO, "Request, but not for us.\n");
502
503         return 0;
504 }
505
506 /* 9.1.20 IMMEDIATE ASSIGNMENT REJECT is received */
507 static int imm_ass_rej(struct osmocom_ms *ms, struct msgb *msg)
508 {
509         struct gsm48_imm_ass_rej *ia = msgb_l3(msg);
510         int i;
511         struct gsm48_req_ref *req_ref;
512
513         LOGP(DRR, LOGL_INFO, "IMMEDIATE ASSIGNMENT REJECT:\n");
514
515         if (state != SCAN_STATE_RACH) {
516                 LOGP(DRR, LOGL_INFO, "Not for us, no request.\n");
517                 return 0;
518         }
519
520         for (i = 0; i < 4; i++) {
521                 /* request reference */
522                 req_ref = (struct gsm48_req_ref *)
523                                 (((uint8_t *)&ia->req_ref1) + i * 4);
524                 LOGP(DRR, LOGL_INFO, "IMMEDIATE ASSIGNMENT REJECT "
525                         "(ref 0x%02x)\n", req_ref->ra);
526                 if (match_ra(ms, req_ref)) {
527                         return ta_result(0xff);
528                 }
529         }
530
531         return 0;
532 }
533
534 /* receive CCCH at RR layer */
535 static int pch_agch(struct osmocom_ms *ms, struct msgb *msg)
536 {
537         struct gsm48_system_information_type_header *sih = msgb_l3(msg);
538
539         switch (sih->system_information) {
540         case GSM48_MT_RR_PAG_REQ_1:
541         case GSM48_MT_RR_PAG_REQ_2:
542         case GSM48_MT_RR_PAG_REQ_3:
543                 return 0;
544         case GSM48_MT_RR_IMM_ASS:
545                 return imm_ass(ms, msg);
546         case GSM48_MT_RR_IMM_ASS_EXT:
547                 return imm_ass_ext(ms, msg);
548         case GSM48_MT_RR_IMM_ASS_REJ:
549                 return imm_ass_rej(ms, msg);
550         default:
551                 return -EINVAL;
552         }
553 }
554
555 /* check if sysinfo is complete, change to RACH state */
556 static int new_sysinfo(void)
557 {
558         struct gsm48_sysinfo *s = &sysinfo;
559
560         /* restart timer */
561         start_timer(READ_WAIT);
562
563         /* mandatory */
564         if (!s->si1 || !s->si2 || !s->si3 || !s->si4) {
565                 LOGP(DRR, LOGL_INFO, "not all mandatory SI received\n");
566                 return 0;
567         }
568
569         /* extended band */
570         if (s->nb_ext_ind_si2 && !s->si2bis) {
571                 LOGP(DRR, LOGL_INFO, "extended ba, but si2bis not received\n");
572                 return 0;
573         }
574
575         /* 2ter */
576         if (s->si2ter_ind && !s->si2ter) {
577                 LOGP(DRR, LOGL_INFO, "si2ter_ind, but si2ter not received\n");
578                 return 0;
579         }
580
581         LOGP(DRR, LOGL_INFO, "Sysinfo complete\n");
582
583         stop_timer();
584
585         rach_count = 0;
586         start_rach();
587
588         return 0;
589 }
590
591 /* receive BCCH at RR layer */
592 static int bcch(struct osmocom_ms *ms, struct msgb *msg)
593 {
594         struct gsm48_sysinfo *s = &sysinfo;
595         struct gsm48_system_information_type_header *sih = msgb_l3(msg);
596         uint8_t ccch_mode;
597
598         if (msgb_l3len(msg) != 23) {
599                 LOGP(DRR, LOGL_NOTICE, "Invalid BCCH message length\n");
600                 return -EINVAL;
601         }
602         switch (sih->system_information) {
603         case GSM48_MT_RR_SYSINFO_1:
604                 if (!memcmp(sih, s->si1_msg, sizeof(s->si1_msg)))
605                         return 0;
606                 LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 1\n");
607                 gsm48_decode_sysinfo1(s,
608                         (struct gsm48_system_information_type_1 *) sih,
609                         msgb_l3len(msg));
610                 return new_sysinfo();
611         case GSM48_MT_RR_SYSINFO_2:
612                 if (!memcmp(sih, s->si2_msg, sizeof(s->si2_msg)))
613                         return 0;
614                 LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 2\n");
615                 gsm48_decode_sysinfo2(s,
616                         (struct gsm48_system_information_type_2 *) sih,
617                         msgb_l3len(msg));
618                 return new_sysinfo();
619         case GSM48_MT_RR_SYSINFO_2bis:
620                 if (!memcmp(sih, s->si2b_msg, sizeof(s->si2b_msg)))
621                         return 0;
622                 LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 2bis\n");
623                 gsm48_decode_sysinfo2bis(s,
624                         (struct gsm48_system_information_type_2bis *) sih,
625                         msgb_l3len(msg));
626                 return new_sysinfo();
627         case GSM48_MT_RR_SYSINFO_2ter:
628                 if (!memcmp(sih, s->si2t_msg, sizeof(s->si2t_msg)))
629                         return 0;
630                 LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 2ter\n");
631                 gsm48_decode_sysinfo2ter(s,
632                         (struct gsm48_system_information_type_2ter *) sih,
633                         msgb_l3len(msg));
634                 return new_sysinfo();
635         case GSM48_MT_RR_SYSINFO_3:
636                 if (!memcmp(sih, s->si3_msg, sizeof(s->si3_msg)))
637                         return 0;
638                 LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 3\n");
639                 gsm48_decode_sysinfo3(s,
640                         (struct gsm48_system_information_type_3 *) sih,
641                         msgb_l3len(msg));
642                 ccch_mode = (s->ccch_conf == 1) ? CCCH_MODE_COMBINED :
643                         CCCH_MODE_NON_COMBINED;
644                 LOGP(DRR, LOGL_INFO, "Changing CCCH_MODE to %d\n", ccch_mode);
645                 l1ctl_tx_ccch_mode_req(ms, ccch_mode);
646                 return new_sysinfo();
647         case GSM48_MT_RR_SYSINFO_4:
648                 if (!memcmp(sih, s->si4_msg, sizeof(s->si4_msg)))
649                         return 0;
650                 LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 4\n");
651                 gsm48_decode_sysinfo4(s,
652                         (struct gsm48_system_information_type_4 *) sih,
653                         msgb_l3len(msg));
654                 return new_sysinfo();
655         default:
656                 return -EINVAL;
657         }
658 }
659
660 static int unit_data_ind(struct osmocom_ms *ms, struct msgb *msg)
661 {
662         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
663         struct tlv_parsed tv;
664         uint8_t ch_type, ch_subch, ch_ts;
665         
666         DEBUGP(DRSL, "RSLms UNIT DATA IND chan_nr=0x%02x link_id=0x%02x\n",
667                 rllh->chan_nr, rllh->link_id);
668
669         rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
670         if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
671                 DEBUGP(DRSL, "UNIT_DATA_IND without L3 INFO ?!?\n");
672                 return -EIO;
673         }
674         msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
675
676         if (state != SCAN_STATE_READ && state != SCAN_STATE_RACH) {
677                 return -EINVAL;
678         }
679
680         rsl_dec_chan_nr(rllh->chan_nr, &ch_type, &ch_subch, &ch_ts);
681         switch (ch_type) {
682         case RSL_CHAN_PCH_AGCH:
683                 return pch_agch(ms, msg);
684         case RSL_CHAN_BCCH:
685                 return bcch(ms, msg);
686 #if 0
687         case RSL_CHAN_Bm_ACCHs:
688         case RSL_CHAN_Lm_ACCHs:
689         case RSL_CHAN_SDCCH4_ACCH:
690         case RSL_CHAN_SDCCH8_ACCH:
691                 return rx_acch(ms, msg);
692 #endif
693         default:
694                 LOGP(DRSL, LOGL_NOTICE, "RSL with chan_nr 0x%02x unknown.\n",
695                         rllh->chan_nr);
696                 return -EINVAL;
697         }
698 }
699
700 static int rcv_rll(struct osmocom_ms *ms, struct msgb *msg)
701 {
702         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
703         int msg_type = rllh->c.msg_type;
704
705         if (msg_type == RSL_MT_UNIT_DATA_IND) {
706                 unit_data_ind(ms, msg);
707         } else
708                 LOGP(DRSL, LOGL_NOTICE, "RSLms message unhandled\n");
709
710         msgb_free(msg);
711
712         return 0;
713 }
714
715 int chan_conf(struct osmocom_ms *ms, struct msgb *msg)
716 {
717         struct abis_rsl_cchan_hdr *ch = msgb_l2(msg);
718         struct gsm48_req_ref *ref = (struct gsm48_req_ref *) (ch->data + 1);
719
720         if (msgb_l2len(msg) < sizeof(*ch) + sizeof(*ref)) {
721                 LOGP(DRR, LOGL_ERROR, "CHAN_CNF too slort\n");
722                 return -EINVAL;
723         }
724
725         rach_ref.valid = 1;
726         rach_ref.t1 = ref->t1;
727         rach_ref.t2 = ref->t2;
728         rach_ref.t3 = ref->t3_low | (ref->t3_high << 3);
729
730         return 0;
731 }
732
733 static int rcv_cch(struct osmocom_ms *ms, struct msgb *msg)
734 {
735         struct abis_rsl_cchan_hdr *ch = msgb_l2(msg);
736         int msg_type = ch->c.msg_type;
737         int rc;
738
739         LOGP(DRSL, LOGL_INFO, "Received '%s' from layer1\n",
740                 get_rsl_name(msg_type));
741
742         if (state == SCAN_STATE_RACH && msg_type == RSL_MT_CHAN_CONF) {
743                 rc = chan_conf(ms, msg);
744                 msgb_free(msg);
745                 return rc;
746         }
747
748         LOGP(DRSL, LOGL_NOTICE, "RSLms message unhandled\n");
749         msgb_free(msg);
750         return 0;
751 }
752
753 static int rcv_rsl(struct msgb *msg, struct osmocom_ms *ms)
754 {
755         struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
756         int rc = 0;
757
758         switch (rslh->msg_discr & 0xfe) {
759         case ABIS_RSL_MDISC_RLL:
760                 rc = rcv_rll(ms, msg);
761                 break;
762         case ABIS_RSL_MDISC_COM_CHAN:
763                 rc = rcv_cch(ms, msg);
764                 break;
765         default:
766                 LOGP(DRSL, LOGL_NOTICE, "unknown RSLms msg_discr 0x%02x\n",
767                         rslh->msg_discr);
768                 msgb_free(msg);
769                 rc = -EINVAL;
770                 break;
771         }
772
773         return rc;
774 }
775
776 int scan_init(struct osmocom_ms *_ms)
777 {
778         ms = _ms;
779         register_signal_handler(SS_L1CTL, &signal_cb, NULL);
780         memset(&timer, 0, sizeof(timer));
781         osmol2_register_handler(ms, &rcv_rsl);
782         gps.enable = 1;
783         gps_init();
784         if (gps_open())
785                 gps.enable = 0;
786
787         if (!strcmp(logname, "-"))
788                 logfp = stdout;
789         else
790                 logfp = fopen(logname, "a");
791         if (!logfp) {
792                 fprintf(stderr, "Failed to open logfile '%s'\n", logname);
793                 scan_exit();
794                 return -errno;
795         }
796         LOGP(DSUM, LOGL_INFO, "Scanner initialized\n");
797
798         return 0;
799 }
800
801 int scan_exit(void)
802 {
803         LOGP(DSUM, LOGL_INFO, "Scanner exit\n");
804         if (gps.valid)
805                 gps_close();
806         if (logfp)
807                 fclose(logfp);
808         unregister_signal_handler(SS_L1CTL, &signal_cb, NULL);
809         stop_timer();
810
811         return 0;
812 }