aboutsummaryrefslogtreecommitdiffstats
path: root/src/gmr1_rx.c
blob: c0817d6dbbb541faacd9aaa75b019d7d8eb79679 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
/* GMR-1 Demo RX application */

/* (C) 2011-2016 by Sylvain Munaut <tnt@246tNt.com>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <complex.h>
#include <errno.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <osmocom/core/bits.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/gsmtap.h>
#include <osmocom/core/gsmtap_util.h>

#include <osmocom/dsp/cfile.h>
#include <osmocom/dsp/cxvec.h>
#include <osmocom/dsp/cxvec_math.h>

#include <osmocom/gmr1/gsmtap.h>
#include <osmocom/gmr1/l1/a5.h>
#include <osmocom/gmr1/l1/bcch.h>
#include <osmocom/gmr1/l1/ccch.h>
#include <osmocom/gmr1/l1/facch3.h>
#include <osmocom/gmr1/l1/facch9.h>
#include <osmocom/gmr1/l1/interleave.h>
#include <osmocom/gmr1/l1/tch3.h>
#include <osmocom/gmr1/l1/tch9.h>
#include <osmocom/gmr1/sdr/defs.h>
#include <osmocom/gmr1/sdr/dkab.h>
#include <osmocom/gmr1/sdr/fcch.h>
#include <osmocom/gmr1/sdr/pi4cxpsk.h>
#include <osmocom/gmr1/sdr/nb.h>


#define START_DISCARD	8000


static struct gsmtap_inst *g_gti;

static const struct gmr1_fcch_burst *fcch_type = &gmr1_fcch_burst;


struct tch3_state {
	/* Status */
	int active;

	/* Channel params */
	int tn;
	int p;
	int ciph;

	/* Energy */
	float energy_dkab;
	float energy_burst;

	int weak_cnt;

	/* FACCH state */
	sbit_t ebits[104*4];
	uint32_t bi_fn[4];
	int sync_id;
	int burst_cnt;
};

struct tch9_state {
	/* Status */
	int active;

	/* Channel params */
	int tn;

	/* Interleaver */
	struct gmr1_interleaver il;
};

struct chan_desc {
	/* Sample source */
	struct cfile *bcch;
	struct cfile *tch;
	struct cfile *tch_csd;
	int sps;

	/* SDR alignement */
	int align;
	float freq_err;

	/* TDMA alignement */
	int fn;
	int sa_sirfn_delay;
	int sa_bcch_stn;

	/* TCH */
	struct tch3_state tch3_state;
	struct tch9_state tch9_state;

	/* A5 */
	uint8_t kc[8];
};


/* Helpers ---------------------------------------------------------------- */

static inline float
to_ms(struct chan_desc *cd, int s)
{
	return (1000.0f * (float)s) / (cd->sps * GMR1_SYM_RATE);
}

static inline float
to_hz(float f_rps)
{
	return (GMR1_SYM_RATE * f_rps) / (2.0f * M_PIf);
}

static inline float
to_db(float v)
{
	return 10.0f * log10f(v);
}

static int
win_map(struct osmo_cxvec *win, struct cfile *cf, int begin, int len)
{
	if ((begin + len) > cf->len)
		return -1;

	osmo_cxvec_init_from_data(win, &cf->data[begin], len);

	return 0;
}

static int
burst_map(struct osmo_cxvec *burst, struct chan_desc *cd,
          struct gmr1_pi4cxpsk_burst *burst_type, int tn, int win, int tch)
{
	int begin, len;
	int etoa;
	struct cfile *df = tch == 2 ? cd->tch_csd : (tch ? cd->tch : cd->bcch);

	if (!df)
		return -EINVAL;

	etoa  = win >> 1;
	begin = cd->align + (cd->sps * tn * 39) - etoa;
	len   = (burst_type->len * cd->sps) + win;

	if ((begin + len) > cd->bcch->len)
		return -EIO;

	osmo_cxvec_init_from_data(burst, &df->data[begin], len);

	return etoa;
}

static float
burst_energy(struct osmo_cxvec *burst)
{
	int i;
	float e = 0.0f;
	int b = (burst->len >> 5); /* exclude the borders */
	for (i=b; i<burst->len-b; i++)
		e += osmo_normsqf(burst->data[i]);
	e /= burst->len;
	return e;
}


/* Message parsing -------------------------------------------------------- */

static int
bcch_tdma_align(struct chan_desc *cd, uint8_t *l2)
{
	int sa_sirfn_delay, sa_bcch_stn;
	int superframe_num, multiframe_num, mffn_high_bit;
	int fn;

	/* Check if it's a SI1 */
	if ((l2[0] & 0xf8) != 0x08)
		return 0;

	/* Check if it contains a Seg 2A bis */
	if ((l2[9] & 0xfc) != 0x80)
		return 0;

	/* Retrieve SA_SIRFN_DELAY, SA_BCCH_STN,
	 * Superframe number, Multiframe number, MFFN high bit */
	sa_sirfn_delay =  (l2[10] >> 3) & 0x0f;
	sa_bcch_stn    = ((l2[10] << 2) & 0x1c) | (l2[11] >> 6);

	superframe_num = ((l2[11] & 0x3f) << 7) | (l2[12] >> 1);
	multiframe_num = ((l2[12] & 0x01) << 1) | (l2[13] >> 7);
	mffn_high_bit  = ((l2[13] & 0x40) >> 6);

	/* Compute frame number */
	fn = (superframe_num << 6) |
	     (multiframe_num << 4) |
	     (mffn_high_bit << 3) |
	     ((2 + sa_sirfn_delay) & 7);

	/* Fix SDR alignement */
	cd->align += (cd->sa_bcch_stn - sa_bcch_stn) * 39 * cd->sps;

	/* Align TDMA */
	cd->fn = fn;
	cd->sa_sirfn_delay = sa_sirfn_delay;
	cd->sa_bcch_stn = sa_bcch_stn;

	return 0;
}

static inline int
ccch_is_imm_ass(const uint8_t *l2)
{
	return (l2[1] == 0x06) && (l2[2] == 0x3f);
}

static void
ccch_imm_ass_parse(const uint8_t *l2, int *rx_tn, int *p)
{
	*p = (l2[8] & 0xfc) >> 2;
	*rx_tn = ((l2[8] & 0x03) << 3) | (l2[9] >> 5);
}

static inline int
facch3_is_ass_cmd_1(const uint8_t *l2)
{
	return (l2[3] == 0x06) && (l2[4] == 0x2e);
}

static void
facch3_ass_cmd_1_parse(const uint8_t *l2, int *rx_tn)
{
	*rx_tn = ((l2[5] & 0x03) << 3) | (l2[6] >> 5);
}


/* TCH9 Procesing --------------------------------------------------------- */

static void
rx_tch9_init(struct chan_desc *cd, const uint8_t *ass_cmd)
{
	/* Activate */
	cd->tch9_state.active = 1;

	/* Extract TN */
	facch3_ass_cmd_1_parse(ass_cmd, &cd->tch9_state.tn);

	/* Init interleaver */
	gmr1_interleaver_init(&cd->tch9_state.il, 3, 648);
}

static int
rx_tch9(struct chan_desc *cd)
{
	struct osmo_cxvec _burst, *burst = &_burst;
	int e_toa, rv, sync_id, crc, conv;
	sbit_t ebits[662], bits_sacch[10], bits_status[4];
	ubit_t ciph[658];
	float toa;

	/* Is TCH active at all ? */
	if (!cd->tch9_state.active)
		return 0;

	/* Map potential burst */
	e_toa = burst_map(burst, cd, &gmr1_nt9_burst,
	                  cd->tch9_state.tn, cd->sps + (cd->sps/2), 2);
	if (e_toa < 0)
		return e_toa;

	/* Demodulate burst */
	rv = gmr1_pi4cxpsk_demod(
		&gmr1_nt9_burst,
		burst, cd->sps, -cd->freq_err,
		ebits, &sync_id, &toa, NULL
	);

	fprintf(stderr, "[.]   %s\n", sync_id ? "TCH9" : "FACCH9");
	fprintf(stderr, "toa=%.1f, sync_id=%d\n", toa, sync_id);

	/* Process depending on type */
	if (!sync_id) { /* FACCH9 */
		uint8_t l2[38];

		/* Generate cipher stream */
		gmr1_a5(1, cd->kc, cd->fn, 658, ciph, NULL);

		/* Decode */
		crc = gmr1_facch9_decode(l2, bits_sacch, bits_status, ebits, ciph, &conv);
		fprintf(stderr, "crc=%d, conv=%d\n", crc, conv);

		/* Send to GSMTap if correct */
		if (!crc)
			gsmtap_sendmsg(g_gti, gmr1_gsmtap_makemsg(
				GSMTAP_GMR1_TCH9 | GSMTAP_GMR1_FACCH,
				cd->fn, cd->tch9_state.tn, l2, 38));
	} else { /* TCH9 */
		uint8_t l2[60];
		int i, s = 0;

		/* Generate cipher stream */
		gmr1_a5(1, cd->kc, cd->fn, 658, ciph, NULL);

		for (i=0; i<662; i++)
			s += ebits[i] < 0 ? -ebits[i] : ebits[i];
		s /= 662;

		/* Decode */
		gmr1_tch9_decode(l2, bits_sacch, bits_status, ebits, GMR1_TCH9_9k6, ciph, &cd->tch9_state.il, &conv);
		fprintf(stderr, "fn=%d, conv9=%d, avg=%d\n", cd->fn, conv, s);

		/* Forward to GSMTap (no CRC to validate :( ) */
		gsmtap_sendmsg(g_gti, gmr1_gsmtap_makemsg(
			GSMTAP_GMR1_TCH9,
			cd->fn, cd->tch9_state.tn, l2, 60));

		/* Save to file */
		{
			static FILE *f = NULL;
			if (!f)
				f = fopen("/tmp/csd.data", "wb");
			fwrite(l2, 60, 1, f);
		}
	}

	/* Done */
	return rv;

}


/* TCH3 Procesing --------------------------------------------------------- */

static void
rx_tch3_init(struct chan_desc *cd, const uint8_t *imm_ass, float ref_energy)
{
	/* Activate */
	cd->tch3_state.active = 1;

	/* Extract TN & DKAB position */
	ccch_imm_ass_parse(imm_ass, &cd->tch3_state.tn, &cd->tch3_state.p);

	/* Estimate energy threshold */
	cd->tch3_state.energy_burst = ref_energy * 0.75f;
	cd->tch3_state.energy_dkab  = cd->tch3_state.energy_burst / 8.0f; /* ~ 8 times less pwr */

	cd->tch3_state.weak_cnt = 0;

	/* Init FACCH state */
	cd->tch3_state.sync_id = 0;
	memset(&cd->tch3_state.ebits, 0x00, sizeof(sbit_t) * 104 * 4);
}

static int
_rx_tch3_dkab(struct chan_desc *cd, struct osmo_cxvec *burst)
{
	sbit_t ebits[8];
	float toa;
	int rv;

	fprintf(stderr, "[.]   DKAB\n");

	rv = gmr1_dkab_demod(burst, cd->sps, -cd->freq_err, cd->tch3_state.p, ebits, &toa);

	fprintf(stderr, "toa=%f\n", toa);

	return rv;
}

static int
_rx_tch3_facch_flush(struct chan_desc *cd)
{
	struct tch3_state *st = &cd->tch3_state;
	ubit_t _ciph[96*4], *ciph;
	uint8_t l2[10];
	ubit_t sbits[8*4];
	int i, crc, conv;

	/* Cipher stream ? */
	if (st->ciph) {
		ciph = _ciph;
		for (i=0; i<4; i++)
			gmr1_a5(1, cd->kc, st->bi_fn[i], 96, ciph+(96*i), NULL);
	} else
		ciph = NULL;

	/* Decode the burst */
	crc = gmr1_facch3_decode(l2, sbits, st->ebits, ciph, &conv);

	fprintf(stderr, "crc=%d, conv=%d\n", crc, conv);

	/* Retry with ciphering ? */
	if (!st->ciph && crc) {
		ciph = _ciph;
		for (i=0; i<4; i++)
			gmr1_a5(1, cd->kc, st->bi_fn[i], 96, ciph+(96*i), NULL);

		crc = gmr1_facch3_decode(l2, sbits, st->ebits, ciph, &conv);

		fprintf(stderr, "crc=%d, conv=%d\n", crc, conv);

		if (!crc)
			st->ciph = 1;
	}

	/* Send to GSMTap if correct */
	if (!crc)
		gsmtap_sendmsg(g_gti, gmr1_gsmtap_makemsg(
			GSMTAP_GMR1_TCH3 | GSMTAP_GMR1_FACCH,
			cd->fn-3, st->tn, l2, 10));

	/* Parse for assignement */
	if (!crc && facch3_is_ass_cmd_1(l2))
	{
		/* Follow if we have the data */
		if (cd->tch_csd)
			rx_tch9_init(cd, l2);
	}

	/* Clear state */
	st->sync_id ^= 1;
	st->burst_cnt = 0;
	memset(st->bi_fn, 0xff, sizeof(uint32_t) * 4);
	memset(st->ebits, 0x00, sizeof(sbit_t) * 104 * 4);

	/* Done */
	return 0;
}

static int
_rx_tch3_facch(struct chan_desc *cd, struct osmo_cxvec *burst)
{
	struct tch3_state *st = &cd->tch3_state;
	sbit_t ebits[104];
	int rv, bi, sync_id;
	float toa;

	/* Burst index */
	bi = cd->fn & 3;

	/* Debug */
	fprintf(stderr, "[.]   FACCH3 (bi=%d)\n", bi);

	/* Demodulate burst */
	rv = gmr1_pi4cxpsk_demod(
		&gmr1_nt3_facch_burst,
		burst, cd->sps, -cd->freq_err,
		ebits, &sync_id, &toa, NULL
	);
	if (rv < 0)
		return rv;

	fprintf(stderr, "toa=%.1f, sync_id=%d\n", toa, sync_id);

	/* Does this burst belong with previous ones ? */
	if (sync_id != st->sync_id)
		_rx_tch3_facch_flush(cd);

	/* Store this burst */
	memcpy(&st->ebits[104*bi], ebits, sizeof(sbit_t) * 104);
	st->sync_id = sync_id;
	st->bi_fn[bi] = cd->fn;
	st->burst_cnt += 1;

	/* Is it time to flush ? */
	if (st->burst_cnt == 4)
		_rx_tch3_facch_flush(cd);

	return 0;
}

static int
_rx_tch3_speech(struct chan_desc *cd, struct osmo_cxvec *burst)
{
	sbit_t ebits[212];
	ubit_t sbits[4], ciph[208];
	uint8_t frame0[10], frame1[10];
	int rv, conv[2];
	float toa;

	/* Debug */
	fprintf(stderr, "[.]   TCH3\n");

	/* Demodulate burst */
	rv = gmr1_pi4cxpsk_demod(
		&gmr1_nt3_speech_burst,
		burst, cd->sps, -cd->freq_err,
		ebits, NULL, &toa, NULL
	);
	if (rv < 0)
		return rv;

	/* Decode it */
	gmr1_a5(cd->tch3_state.ciph, cd->kc, cd->fn, 208, ciph, NULL);

	gmr1_tch3_decode(frame0, frame1, sbits, ebits, ciph, 0, &conv[0], &conv[1]);

	/* More debug */
	fprintf(stderr, "toa=%.1f\n", toa);
	fprintf(stderr, "conv=%3d,%3d\n", conv[0], conv[1]);
	fprintf(stderr, "frame0=%s\n", osmo_hexdump_nospc(frame0, 10));
	fprintf(stderr, "frame1=%s\n", osmo_hexdump_nospc(frame1, 10));

	return 0;
}

static int
rx_tch3(struct chan_desc *cd)
{
	static struct gmr1_pi4cxpsk_burst *burst_types[] = {
		&gmr1_nt3_facch_burst,
		&gmr1_nt3_speech_burst,
		NULL
	};

	struct osmo_cxvec _burst, *burst = &_burst;
	int e_toa, rv, btid, sid;
	float be, det, toa;

	/* Is TCH active at all ? */
	if (!cd->tch3_state.active)
		return 0;

	/* Map potential burst (use FACCH3 as reference) */
	e_toa = burst_map(burst, cd, &gmr1_nt3_facch_burst,
	                  cd->tch3_state.tn, cd->sps + (cd->sps/2), 1);
	if (e_toa < 0)
		return e_toa;

	/* Burst energy (and check for DKAB) */
	be = burst_energy(burst);

	det = (cd->tch3_state.energy_dkab + cd->tch3_state.energy_burst) / 4.0f;

	if (be < det) {
		rv = _rx_tch3_dkab(cd, burst);

		if (rv < 0)
			return rv;
		else if (rv == 1) {
			if (cd->tch3_state.weak_cnt++ > 8) {
				fprintf(stderr, "END @%d\n", cd->fn);
				cd->tch3_state.active = 0;
			}
		} else {
			cd->tch3_state.energy_dkab =
				(0.1f * be) +
				(0.9f * cd->tch3_state.energy_dkab);
		}

		return 0;
	} else
		cd->tch3_state.weak_cnt = 0;

	cd->tch3_state.energy_burst =
		(0.1f * be) +
		(0.9f * cd->tch3_state.energy_burst);

	/* Detect burst type */
	rv = gmr1_pi4cxpsk_detect(
		burst_types, (float)e_toa,
		burst, cd->sps, -cd->freq_err,
		&btid, &sid, &toa
	);
	if (rv < 0)
		return rv;

	/* Delegate appropriately */
	if (btid == 0)
		rv = _rx_tch3_facch(cd, burst);
	else
		rv = _rx_tch3_speech(cd, burst);

	/* Done */
	return rv;
}


/* Procesing -------------------------------------------------------------- */

static int
fcch_single_init(struct chan_desc *cd)
{
	struct osmo_cxvec _win, *win = &_win;
	int rv, toa;

	/* FCCH rough detection in the first 330 ms */
	rv = win_map(win, cd->bcch, cd->align, (330 * GMR1_SYM_RATE * cd->sps) / 1000);
	if (rv) {
		fprintf(stderr, "[!] Not enough samples\n");
		return rv;
	}

	rv = gmr1_fcch_rough(fcch_type, win, cd->sps, 0.0f, &toa);
	if (rv) {
		fprintf(stderr, "[!] Error during FCCH rough acquisition (%d)\n", rv);
		return rv;
	}

	cd->align += toa;

	/* Fine FCCH detection*/
	win_map(win, cd->bcch, cd->align, fcch_type->len * cd->sps);

	rv = gmr1_fcch_fine(fcch_type, win, cd->sps, 0.0f, &toa, &cd->freq_err);
	if (rv) {
		fprintf(stderr, "[!] Error during FCCH fine acquisition (%d)\n", rv);
		return rv;
	}

	cd->align += toa;

	/* Done */
	return 0;
}

typedef int (*fcch_multi_cb_t)(struct chan_desc *cd);

static int
fcch_multi_process(struct chan_desc *cd, fcch_multi_cb_t cb)
{
	struct osmo_cxvec _win, *win = &_win;
	int base_align, mtoa[16];
	int i, j, rv, n_fcch;
	float ref_snr, ref_freq_err;

	fprintf(stderr, "[+] FCCH multi acquisition\n");

	/* Multi FCCH detection (need 650 ms of signals) */
	base_align = cd->align - fcch_type->len * cd->sps;
	if (base_align < 0)
		base_align = 0;

	rv = win_map(win, cd->bcch, base_align, (650 * GMR1_SYM_RATE * cd->sps) / 1000);
	if (rv) {
		fprintf(stderr, "[!] Not enough samples\n");
		return rv;
	}

	rv = gmr1_fcch_rough_multi(fcch_type, win, cd->sps, -cd->freq_err, mtoa, 16);
	if (rv < 0) {
		fprintf(stderr, "[!] Error during FCCH rough mutli-acquisition (%d)\n", rv);
		return rv;
	}

	n_fcch = rv;

	/* Check each of them for validity */
	ref_snr = ref_freq_err = 0.0f;

	for (i=0, j=0; i<n_fcch; i++) {
		float freq_err, e_fcch, e_cich, snr;
		int toa;

		/* Perform fine acquisition */
		win_map(win, cd->bcch, base_align + mtoa[i], fcch_type->len * cd->sps);

		rv = gmr1_fcch_fine(fcch_type, win, cd->sps, -cd->freq_err, &toa, &freq_err);
		if (rv) {
			fprintf(stderr, "[!] Error during FCCH fine acquisition (%d)\n", rv);
			return rv;
		}

		/* Compute SNR (comparing energy with neighboring CICH) */
		win_map(win, cd->bcch,
			base_align + mtoa[i] + toa + 5 * cd->sps,
			(117 - 10) * cd->sps
		);

		e_fcch = burst_energy(win);

		win_map(win, cd->bcch,
			base_align + mtoa[i] + toa + (5 + 117) * cd->sps,
			(117 - 10) * cd->sps
		);

		e_cich = burst_energy(win);

		snr = e_fcch / e_cich;

		/* Check against strongest */
		if (i==0) {
			/* This _is_ the reference */
			ref_snr = snr;
			ref_freq_err = freq_err;
		} else {
			/* Check if SNR is 'good enough' */
			if (snr < 2.0f)
				continue;

			if (snr < (ref_snr / 6.0f))
				continue;

			/* Check if frequency error is not too "off" */
			if (to_hz(fabs(ref_freq_err - freq_err)) > 500.0f)
				continue;
		}

		/* Debug print */
		fprintf(stderr, "[.]  Potential FCCH @%d (%.3f ms). [snr = %.1f dB, freq_err = %.1f Hz]\n",
			base_align + mtoa[i] + toa,
			to_ms(cd, base_align + mtoa[i] + toa),
			to_db(snr),
			to_hz(freq_err + cd->freq_err)
		);

		/* Save it */
		mtoa[j++] = mtoa[i] + toa;
	}

	n_fcch = j;

	/* Now process each survivor */
	for (i=0; i<n_fcch; i++) {
		struct chan_desc _cdl, *cdl = &_cdl;

		memcpy(cdl, cd, sizeof(struct chan_desc));
		cdl->align = base_align + mtoa[i];

		rv = cb(cdl);
		if (rv)
			break;
	}

	return rv;
}

static int
rx_bcch(struct chan_desc *cd, float *energy)
{
	struct osmo_cxvec _burst, *burst = &_burst;
	sbit_t ebits[424];
	uint8_t l2[24];
	float freq_err, toa;
	int rv, crc, conv, e_toa;

	/* Debug */
	fprintf(stderr, "[.]   BCCH\n");

	/* Demodulate burst */
	e_toa = burst_map(burst, cd, &gmr1_bcch_burst, cd->sa_bcch_stn, 20 * cd->sps, 0);
	if (e_toa < 0)
		return e_toa;

	rv = gmr1_pi4cxpsk_demod(
		&gmr1_bcch_burst,
		burst, cd->sps, -cd->freq_err,
		ebits, NULL, &toa, &freq_err
	);

	if (rv)
		return rv;

	/* Measure energy as a reference */
	if (energy)
		*energy = burst_energy(burst);

	/* Decode burst */
	crc = gmr1_bcch_decode(l2, ebits, &conv);

	fprintf(stderr, "crc=%d, conv=%d\n", crc, conv);

	/* If burst turned out OK, use data to align channel */
	if (!crc) {
		/* SDR alignement */
		cd->align += ((int)roundf(toa)) - e_toa;
		cd->freq_err += freq_err;

		/* Acquire TDMA alignement */
		bcch_tdma_align(cd, l2);
	}

	/* Send to GSMTap if correct */
	if (!crc)
		gsmtap_sendmsg(g_gti, gmr1_gsmtap_makemsg(
			GSMTAP_GMR1_BCCH,
			cd->fn, cd->sa_bcch_stn, l2, 24));

	return 0;
}

static int
rx_ccch(struct chan_desc *cd, float min_energy)
{
	struct osmo_cxvec _burst, *burst = &_burst;
	sbit_t ebits[432];
	uint8_t l2[24];
	int rv, crc, conv, e_toa;

	/* Map potential burst */
	e_toa = burst_map(burst, cd, &gmr1_dc6_burst, cd->sa_bcch_stn, 10 * cd->sps, 0);
	if (e_toa < 0)
		return e_toa;

	/* Energy detection */
	if (burst_energy(burst) < min_energy)
		return 0; /* Nothing to do */

	/* Debug */
	fprintf(stderr, "[.]   CCCH\n");

	/* Demodulate burst */
	rv = gmr1_pi4cxpsk_demod(
		&gmr1_dc6_burst,
		burst, cd->sps, -cd->freq_err,
		ebits, NULL, NULL, NULL
	);

	if (rv)
		return rv;

	/* Decode burst */
	crc = gmr1_ccch_decode(l2, ebits, &conv);

	fprintf(stderr, "crc=%d, conv=%d\n", crc, conv);

	/* Check for IMM.ASS */
	if (!crc) {
		if (ccch_is_imm_ass(l2)) {
			rx_tch3_init(cd, l2, min_energy);
			fprintf(stderr, "\n[+] TCH3 assigned on TN %d\n", cd->tch3_state.tn);
		}
	}

	/* Send to GSMTap if correct */
	if (!crc)
		gsmtap_sendmsg(g_gti, gmr1_gsmtap_makemsg(
			GSMTAP_GMR1_CCCH,
			cd->fn, cd->sa_bcch_stn, l2, 24));

	return 0;
}

static int
process_bcch(struct chan_desc *cd)
{
	int frame_len;
	int sirfn;
	float bcch_energy = nan("inf");

	fprintf(stderr, "[+] Processing BCCH @%d (%.3f ms). [freq_err = %.1f Hz]\n",
		cd->align, to_ms(cd, cd->align), to_hz(cd->freq_err));

	/* Process frame by frame */
	frame_len = cd->sps * 24 * 39;

	while (1) {
		/* Debug */
		fprintf(stderr, "[-]  FN: %6d (%10.3f ms)\n", cd->fn, to_ms(cd, cd->align));

		/* SI relative frame number inside an hyperframe */
		sirfn = (cd->fn - cd->sa_sirfn_delay) & 63;

		/* BCCH */
		if (sirfn % 8 == 2)
			rx_bcch(cd, &bcch_energy);

		/* CCCH */
		if ((sirfn % 8 != 0) && (sirfn % 8 != 2))
			rx_ccch(cd, bcch_energy / 2.0f);

		/* TCH */
		rx_tch3(cd);
		rx_tch9(cd);

		/* Next frame */
		cd->fn++;
		cd->align += frame_len;

		/* Stop if we don't have 2 complete frame
		 * (with TN offset, we can go beyond one) */
		if ((cd->align + 2*frame_len) > cd->bcch->len)
			break;
	}

	return 0;
}


/* Main ------------------------------------------------------------------- */

int main(int argc, char *argv[])
{
	struct chan_desc _cd, *cd = &_cd;
	int rv=0;

	/* Init channel description */
	memset(cd, 0x00, sizeof(struct chan_desc));

	cd->align = START_DISCARD;
	cd->freq_err = 0.0f;

	/* Arg check */
	if (argc < 3 || argc > 6) {
		fprintf(stderr, "Usage: %s sps bcch.cfile [tch.cfile [key [tch_csd.cfile]]]\n", argv[0]);
		return -EINVAL;
	}

	cd->sps = atoi(argv[1]);

	if (cd->sps < 1 || cd->sps > 16) {
		fprintf(stderr, "[!] sps must be within [1,16]\n");
		return -EINVAL;
	}

	cd->bcch = cfile_load(argv[2]);
	if (!cd->bcch) {
		fprintf(stderr, "[!] Failed to load bcch input file\n");
		rv = -EIO;
		goto err;
	}

	if (argc > 3) {
		cd->tch = cfile_load(argv[3]);
		if (!cd->tch) {
			fprintf(stderr, "[!] Failed to load tch input file\n");
			rv = -EIO;
			goto err;
		}
	}

	if (argc > 4) {
		if (osmo_hexparse(argv[4], cd->kc, 8) != 8) {
			fprintf(stderr, "[!] Invalid key\n");
			rv = -EINVAL;
			goto err;
		}
	}

	if (argc > 5) {
		cd->tch_csd = cfile_load(argv[5]);
		if (!cd->tch_csd) {
			fprintf(stderr, "[!] Failed to load tch CSD input file\n");
			rv = -EIO;
			goto err;
		}
	}

	/* Init GSMTap */
	g_gti = gsmtap_source_init("127.0.0.1", GSMTAP_UDP_PORT, 0);
	gsmtap_source_add_sink(g_gti);

	/* Use best FCCH for inital sync / freq error */
	rv = fcch_single_init(cd);
	if (rv) {
		fprintf(stderr, "[!] Failed to acquired primary FCCH\n");
		goto err;
	}

	fprintf(stderr, "[+] Primary FCCH found @%d (%.3f ms). [freq_err = %.1f Hz]\n",
		cd->align, to_ms(cd, cd->align), to_hz(cd->freq_err));

	/* Detect all 'visible' FCCH and process them */
	rv = fcch_multi_process(cd, process_bcch);
	if (rv)
		goto err;

	/* Done ! */
	rv = 0;

	/* Clean up */
err:
	if (cd->tch_csd)
		cfile_release(cd->tch_csd);

	if (cd->tch)
		cfile_release(cd->tch);

	if (cd->bcch)
		cfile_release(cd->bcch);

	return rv;
}