summaryrefslogtreecommitdiffstats
path: root/src/target/firmware/layer1/prim_sniff.c
blob: 37dcd32abccf3b41d68f5cab0a4b473d4d02477d (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
/* Layer 1 - Sniffing Bursts */

/* (C) 2010 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 General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

// #define DEBUG
#undef DEBUG	/* Very bw hungry */

#include <stdint.h>
#include <string.h>

#include <defines.h>
#include <byteorder.h>

#include <debug.h>

#include <osmocom/gsm/gsm_utils.h>

#include <calypso/dsp.h>
#include <layer1/agc.h>
#include <layer1/l23_api.h>
#include <layer1/rfch.h>
#include <layer1/sync.h>
#include <layer1/tdma_sched.h>
#include <layer1/tpu_window.h>


/* ------------------------------------------------------------------------ */
/* Sniff TASK API                                                           */
/* ------------------------------------------------------------------------ */

#define SNIFF_DSP_TASK		23

#define BASE_API_RAM            0xffd00000
#define BASE_SNIFF_API_RAM	(BASE_API_RAM + (0x2000 - 0x800) * sizeof(uint16_t))

struct sniff_burst {
	uint16_t toa;
	uint16_t pm;
	uint16_t angle;
	uint16_t snr;
	uint16_t dummy_ind;
	uint16_t bits[29];
};

struct sniff_db {
	uint16_t w_nb;
	uint16_t r_nb;
	struct sniff_burst bursts[4];
};

struct sniff_api {
	struct sniff_db db[2];
	uint16_t db_ptr;
	uint16_t burst_ptr;
};

static inline struct sniff_db *
sniff_get_page(int r_wn /* 0=W, 1=R */)
{
	struct sniff_api *sapi = (void*)BASE_SNIFF_API_RAM;
	int idx = r_wn ? dsp_api.r_page : dsp_api.w_page;
	return &sapi->db[idx];
}


/* ------------------------------------------------------------------------ */
/* Local state                                                              */
/* ------------------------------------------------------------------------ */

struct sniff_local_db {
	uint32_t fn;
	uint8_t w_nb;
	uint8_t r_nb;
};

static struct sniff_local_db _ldbs[2];

static inline struct sniff_local_db *
sniff_get_local_page(int r_wn /* 0=W, 1=R */)
{
	struct sniff_local_db *ldb;
	int idx = r_wn ? dsp_api.r_page : dsp_api.w_page;

	/* Get page */
	ldb = &_ldbs[idx];

	/* Clear page if it's not properly in sync */
	if (!r_wn) {
		if (ldb->fn != l1s.next_time.fn) {
			memset(ldb, 0x00, sizeof(struct sniff_local_db));
			ldb->fn = l1s.next_time.fn;
		}
	}

	return ldb;
}


/* ------------------------------------------------------------------------ */
/* Sniff command & response                                                 */
/* ------------------------------------------------------------------------ */

static int
l1s_sniff_resp(uint8_t ul, uint8_t burst_id, uint16_t p3)
{
	struct sniff_db *sp = sniff_get_page(1);
	struct sniff_local_db *lsp = sniff_get_local_page(1);
	struct sniff_api *sapi = (void*)BASE_SNIFF_API_RAM;

	struct msgb *msg;
	struct l1ctl_burst_ind *bi;

	struct gsm_time rx_time;
	uint16_t rf_arfcn;
	uint8_t mf_task_id = p3 & 0xff;
	uint8_t mf_task_flags = p3 >> 8;
	uint8_t tn;

	int bidx, i;

	/* Debug */
	printd("Fn: %d - %d - lsp->w_nb: %d - lsp->r_nb: %d - sp->w_nb: %d - sp->r_nb: %d\n",
		l1s.current_time.fn-1, ul,
		sp->w_nb, lsp->r_nb,
		sp->w_nb, sp->r_nb
	);
	printd(" -> %d %04hx %04hx | %04hx %04hx %04hx %04hx %04hx\n",
		dsp_api.r_page,
		sapi->db_ptr,
		sapi->burst_ptr,
		sp->bursts[bidx].toa,
		sp->bursts[bidx].pm,
		sp->bursts[bidx].angle,
		sp->bursts[bidx].snr,
		sp->bursts[bidx].dummy_ind
	);

	for (i=0; i<29; i++)
		printd("%04hx%c", sp->bursts[bidx].bits[i], i==28?'\n':' ');

	/* Burst index in DSP response */
	bidx = lsp->r_nb++;

	/* The radio parameters for _this_ burst */
	gsm_fn2gsmtime(&rx_time, l1s.current_time.fn - 1);
	rfch_get_params(&rx_time, &rf_arfcn, NULL, &tn);

	/* Create message */
	msg = l1ctl_msgb_alloc(L1CTL_BURST_IND);
	if (!msg)
		goto exit;

	bi = (struct l1ctl_burst_ind *) msgb_put(msg, sizeof(*bi));

	/* Meta data */
		/* Time */
	bi->frame_nr = htonl(rx_time.fn);

		/* ARFCN */
	if (ul)
		rf_arfcn |= ARFCN_UPLINK;
	bi->band_arfcn = htons(rf_arfcn);

		/* Set Channel Number depending on MFrame Task ID */
	bi->chan_nr = mframe_task2chan_nr(mf_task_id, tn);

		/* Set burst id */
	bi->flags = burst_id;

		/* Set SACCH indication */
	if (mf_task_flags & MF_F_SACCH)
	        bi->flags |= BI_FLG_SACCH;

		/* Set dummy indication */
	if (sp->bursts[bidx].dummy_ind)
		bi->flags |= BI_FLG_DUMMY;

		/* DSP measurements */
	bi->rx_level = dbm2rxlev(agc_inp_dbm8_by_pm(sp->bursts[bidx].pm >> 3)>>3);
	bi->snr = (sp->bursts[bidx].snr - 1) >> 6;

	/* Pack bits */
	memset(bi->bits, 0x00, sizeof(bi->bits));

	for (i=0; i<116; i++)
	{
		int sbit  = 0x0008 << ((3 - (i & 3)) << 2);
		int sword = i >> 2;
		int dbit  = 1 << (7 - (i & 7));
		int dbyte = i >> 3;

		if (sp->bursts[bidx].bits[sword] & sbit)
			bi->bits[dbyte] |= dbit;
	}

	/* Send it ! */
	l1_queue_for_l2(msg);

exit:
	/* mark READ page as being used */
	dsp_api.r_page_used = 1;

	return 0;
}

static int
l1s_sniff_cmd(uint8_t ul, __unused uint8_t burst_id, __unused uint16_t p3)
{
	struct sniff_db *sp = sniff_get_page(0);
	struct sniff_local_db *lsp = sniff_get_local_page(0);
	uint16_t arfcn;
	uint8_t tsc, tn;

	printd("CMD: %d %d %d\n", lsp->w_nb);

	sp->w_nb = ++lsp->w_nb;
	sp->r_nb = 0;

	rfch_get_params(&l1s.next_time, &arfcn, &tsc, &tn);

	dsp_load_rx_task(SNIFF_DSP_TASK, 0, tsc);

		/* enable dummy bursts detection */
	dsp_api.db_w->d_ctrl_system |= (1 << B_BCCH_FREQ_IND);

	if (ul) {
		l1s_rx_win_ctrl(arfcn | ARFCN_UPLINK, L1_RXWIN_NB, 3);
	} else {
		l1s_rx_win_ctrl(arfcn, L1_RXWIN_NB, 0);
	}

	return 0;
}

const struct tdma_sched_item sniff_xcch_dl_sched_set[] = {
						SCHED_ITEM_DT(l1s_sniff_cmd, 0, 0, 0),	SCHED_END_FRAME(),
						SCHED_ITEM_DT(l1s_sniff_cmd, 0, 0, 1),	SCHED_END_FRAME(),
	SCHED_ITEM(l1s_sniff_resp, -5, 0, 0),	SCHED_ITEM_DT(l1s_sniff_cmd, 0, 0, 2),	SCHED_END_FRAME(),
	SCHED_ITEM(l1s_sniff_resp, -5, 0, 1),	SCHED_ITEM_DT(l1s_sniff_cmd, 0, 0, 3),	SCHED_END_FRAME(),
	SCHED_ITEM(l1s_sniff_resp, -5, 0, 2),						SCHED_END_FRAME(),
	SCHED_ITEM(l1s_sniff_resp, -5, 0, 3),						SCHED_END_FRAME(),
	SCHED_END_SET()
};

const struct tdma_sched_item sniff_xcch_ul_sched_set[] = {
						SCHED_ITEM_DT(l1s_sniff_cmd, 3, 1, 0),	SCHED_END_FRAME(),
						SCHED_ITEM_DT(l1s_sniff_cmd, 3, 1, 1),	SCHED_END_FRAME(),
	SCHED_ITEM(l1s_sniff_resp, -4, 1, 0),	SCHED_ITEM_DT(l1s_sniff_cmd, 3, 1, 2),	SCHED_END_FRAME(),
	SCHED_ITEM(l1s_sniff_resp, -4, 1, 1),	SCHED_ITEM_DT(l1s_sniff_cmd, 3, 1, 3),	SCHED_END_FRAME(),
	SCHED_ITEM(l1s_sniff_resp, -4, 1, 2),						SCHED_END_FRAME(),
	SCHED_ITEM(l1s_sniff_resp, -4, 1, 3),						SCHED_END_FRAME(),
	SCHED_END_SET()
};

const struct tdma_sched_item sniff_tch_sched_set[] = {
	SCHED_ITEM_DT(l1s_sniff_cmd, 0, 0, 0),	SCHED_ITEM_DT(l1s_sniff_cmd, 3, 1, 0),	SCHED_END_FRAME(),
											SCHED_END_FRAME(),
	SCHED_ITEM(l1s_sniff_resp, -5, 0, 0),	SCHED_ITEM(l1s_sniff_resp, -4, 1, 0),	SCHED_END_FRAME(),
	SCHED_END_SET()
};