blob: 71d49aa1beec40e32d26afc9cb86352f8d7eff7b [file] [log] [blame]
Ericb7253c62022-11-28 19:21:08 +01001/*
2 * (C) 2022 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
3 * All Rights Reserved
4 *
5 * Author: Eric Wild <ewild@sysmocom.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
Eric Wildd8a1dee2024-02-21 19:33:09 +010022#include "sigProcLib.h"
23#include "signalVector.h"
Ericb7253c62022-11-28 19:21:08 +010024#include <atomic>
25#include <cassert>
26#include <complex>
27#include <iostream>
28#include <future>
29
30#include "ms.h"
31#include "grgsm_vitac/grgsm_vitac.h"
32
Eric3e7f4b02023-05-23 12:50:25 +020033#include "threadpool.h"
34
Ericb7253c62022-11-28 19:21:08 +010035extern "C" {
36#include "sch.h"
37}
38
39#ifdef LOG
40#undef LOG
41#endif
42
Ericea7bd5f2023-05-02 15:21:45 +020043#if !defined(NODAMNLOG)
Ericb7253c62022-11-28 19:21:08 +010044#define DBGLG(...) ms_trx::dummy_log()
45#else
46#define DBGLG(...) std::cerr
47#endif
48
Ericea7bd5f2023-05-02 15:21:45 +020049#if !defined(NODAMNLOG)
Ericb7253c62022-11-28 19:21:08 +010050#define DBGLG2(...) ms_trx::dummy_log()
51#else
52#define DBGLG2(...) std::cerr
53#endif
54
55#define PRINT_Q_OVERFLOW
Eric Wild2f40abd2023-05-22 22:50:43 +020056
Eric Wild98a50b72024-03-20 19:39:05 +010057extern std::atomic<bool> g_exit_flag;
58
Eric Wild2f40abd2023-05-22 22:50:43 +020059bool ms_trx::decode_sch(char *bits, bool update_global_clock)
Ericb7253c62022-11-28 19:21:08 +010060{
61 int fn;
62 struct sch_info sch;
63 ubit_t info[GSM_SCH_INFO_LEN];
64 sbit_t data[GSM_SCH_CODED_LEN];
65
Eric Wild2f40abd2023-05-22 22:50:43 +020066 memcpy(&data[0], &bits[3], 39);
67 memcpy(&data[39], &bits[106], 39);
Ericb7253c62022-11-28 19:21:08 +010068
69 if (!gsm_sch_decode(info, data)) {
70 gsm_sch_parse(info, &sch);
71
72 if (update_global_clock) {
73 DBGLG() << "SCH : Decoded values" << std::endl;
74 DBGLG() << " BSIC: " << sch.bsic << std::endl;
75 DBGLG() << " TSC: " << (sch.bsic & 0x7) << std::endl;
76 DBGLG() << " T1 : " << sch.t1 << std::endl;
77 DBGLG() << " T2 : " << sch.t2 << std::endl;
78 DBGLG() << " T3p : " << sch.t3p << std::endl;
79 DBGLG() << " FN : " << gsm_sch_to_fn(&sch) << std::endl;
80 }
81
82 fn = gsm_sch_to_fn(&sch);
83 if (fn < 0) { // how? wh?
84 DBGLG() << "SCH : Failed to convert FN " << std::endl;
85 return false;
86 }
87
88 if (update_global_clock) {
89 mBSIC = sch.bsic;
90 mTSC = sch.bsic & 0x7;
91 timekeeper.set(fn, 0);
Ericb7253c62022-11-28 19:21:08 +010092 }
Ericb7253c62022-11-28 19:21:08 +010093
94 return true;
95 }
96 return false;
97}
98
99void ms_trx::maybe_update_gain(one_burst &brst)
100{
101 static_assert((sizeof(brst.burst) / sizeof(brst.burst[0])) == ONE_TS_BURST_LEN, "wtf, buffer size mismatch?");
102 const int avgburst_num = 8 * 20; // ~ 50*4.5ms = 90ms?
103 static_assert(avgburst_num * 577 > (50 * 1000), "can't update faster then blade wait time?");
104 const unsigned int rx_max_cutoff = (rxFullScale * 2) / 3;
105 static int gain_check = 0;
106 static float runmean = 0;
Eric6a3e4b32023-04-26 22:12:06 +0200107 float sum = normed_abs_sum(&brst.burst[0], ONE_TS_BURST_LEN);
Ericb7253c62022-11-28 19:21:08 +0100108 runmean = gain_check ? (runmean * (gain_check + 2) - 1 + sum) / (gain_check + 2) : sum;
109
110 if (gain_check == avgburst_num - 1) {
Eric Wild238891f2024-03-20 19:39:05 +0100111 DBGLG2() << "\x1B[32m #RXG \033[0m" << cfg.rxgain << " " << runmean << " " << sum << std::endl;
Ericb7253c62022-11-28 19:21:08 +0100112 auto gainoffset = runmean < (rxFullScale / 4 ? 4 : 2);
113 gainoffset = runmean < (rxFullScale / 2 ? 2 : 1);
Eric Wild238891f2024-03-20 19:39:05 +0100114 float newgain = runmean < rx_max_cutoff ? cfg.rxgain + gainoffset : cfg.rxgain - gainoffset;
Ericb7253c62022-11-28 19:21:08 +0100115 // FIXME: gian cutoff
Eric Wild238891f2024-03-20 19:39:05 +0100116 if (newgain != cfg.rxgain && newgain <= 60) {
Eric3e7f4b02023-05-23 12:50:25 +0200117 auto gain_fun = [this, newgain] { setRxGain(newgain); };
118 worker_thread.add_task(gain_fun);
119 }
120
Ericb7253c62022-11-28 19:21:08 +0100121 runmean = 0;
122 }
123 gain_check = (gain_check + 1) % avgburst_num;
124}
125
126static char sch_demod_bits[148];
127
128bool ms_trx::handle_sch_or_nb()
129{
130 one_burst brst;
131 const auto current_gsm_time = timekeeper.gsmtime();
132 const auto is_sch = gsm_sch_check_ts(current_gsm_time.TN(), current_gsm_time.FN());
133
134 //either pass burst to upper layer for demod, OR pass demodded SCH to upper layer so we don't waste time processing it twice
135 brst.gsmts = current_gsm_time;
136
137 if (!is_sch) {
138 memcpy(brst.burst, burst_copy_buffer, sizeof(blade_sample_type) * ONE_TS_BURST_LEN);
139 } else {
140 handle_sch(false);
141 memcpy(brst.sch_bits, sch_demod_bits, sizeof(sch_demod_bits));
142 }
Ericea7bd5f2023-05-02 15:21:45 +0200143
Eric Wild98a50b72024-03-20 19:39:05 +0100144 while (!g_exit_flag && upper_is_ready && !rxqueue.spsc_push(&brst))
Ericc3e515a2023-05-08 12:56:56 +0200145 ;
Ericb7253c62022-11-28 19:21:08 +0100146
Eric Wild238891f2024-03-20 19:39:05 +0100147 if (!use_agc)
Ericb7253c62022-11-28 19:21:08 +0100148 maybe_update_gain(brst);
149
150 return false;
151}
152
153static float sch_acq_buffer[SCH_LEN_SPS * 2];
154
155bool ms_trx::handle_sch(bool is_first_sch_acq)
156{
157 auto current_gsm_time = timekeeper.gsmtime();
158 const auto buf_len = is_first_sch_acq ? SCH_LEN_SPS : ONE_TS_BURST_LEN;
159 const auto which_in_buffer = is_first_sch_acq ? first_sch_buf : burst_copy_buffer;
Eric Wildd8a1dee2024-02-21 19:33:09 +0100160 memset((void *)&sch_acq_buffer[0], 0, sizeof(sch_acq_buffer));
Eric Wild238891f2024-03-20 19:39:05 +0100161 if (use_va) {
162 const auto which_out_buffer = is_first_sch_acq ? sch_acq_buffer : &sch_acq_buffer[40 * 2];
163 const auto ss = reinterpret_cast<std::complex<float> *>(which_out_buffer);
164 std::complex<float> channel_imp_resp[CHAN_IMP_RESP_LENGTH * d_OSR];
165 int start;
166 convert_and_scale(which_out_buffer, which_in_buffer, buf_len * 2, 1.f / float(rxFullScale));
167 if (is_first_sch_acq) {
168 float max_corr = 0;
169 start = get_sch_buffer_chan_imp_resp(ss, &channel_imp_resp[0], buf_len, &max_corr);
170 } else {
171 start = get_sch_chan_imp_resp(ss, &channel_imp_resp[0]);
172 start = start < 39 ? start : 39;
173 start = start > -39 ? start : -39;
174 }
175 detect_burst_nb(&ss[start], &channel_imp_resp[0], 0, sch_demod_bits);
Ericb7253c62022-11-28 19:21:08 +0100176
Eric Wild238891f2024-03-20 19:39:05 +0100177 auto sch_decode_success = decode_sch(sch_demod_bits, is_first_sch_acq);
178#if 0 // useful to debug offset shifts
Eric Wildd8a1dee2024-02-21 19:33:09 +0100179 auto burst = new signalVector(buf_len, 50);
180 const auto corr_type = is_first_sch_acq ? sch_detect_type::SCH_DETECT_BUFFER : sch_detect_type::SCH_DETECT_FULL;
181 struct estim_burst_params ebp;
Ericb7253c62022-11-28 19:21:08 +0100182
Eric Wildd8a1dee2024-02-21 19:33:09 +0100183 // scale like uhd, +-2k -> +-32k
184 convert_and_scale(burst->begin(), which_in_buffer, buf_len * 2, SAMPLE_SCALE_FACTOR);
185
186 auto rv = detectSCHBurst(*burst, 4, 4, corr_type, &ebp);
187
188 int howmuchdelay = ebp.toa * 4;
189 std::cerr << "ooffs: " << howmuchdelay << " " << std::endl;
190 std::cerr << "voffs: " << start << " " << sch_decode_success << std::endl;
191#endif
Eric Wild238891f2024-03-20 19:39:05 +0100192 if (sch_decode_success) {
193 const auto ts_offset_symb = 4;
194 if (is_first_sch_acq) {
195 // update ts to first sample in sch buffer, to allow delay calc for current ts
196 first_sch_ts_start = first_sch_buf_rcv_ts + start - (ts_offset_symb * 4) - 1;
197 } else if (abs(start) > 1) {
198 // continuous sch tracking, only update if off too much
199 temp_ts_corr_offset += -start;
200 std::cerr << "offs: " << start << " " << temp_ts_corr_offset << std::endl;
201 }
202
203 return true;
204 } else {
205 DBGLG2() << "L SCH : \x1B[31m decode fail \033[0m @ toa:" << start << " "
206 << current_gsm_time.FN() << ":" << current_gsm_time.TN() << std::endl;
Ericb7253c62022-11-28 19:21:08 +0100207 }
Ericb7253c62022-11-28 19:21:08 +0100208 } else {
Eric Wild238891f2024-03-20 19:39:05 +0100209 const auto ts_offset_symb = 4;
210 auto burst = new signalVector(buf_len, 50);
211 const auto corr_type =
212 is_first_sch_acq ? sch_detect_type::SCH_DETECT_BUFFER : sch_detect_type::SCH_DETECT_FULL;
213 struct estim_burst_params ebp;
Eric Wildd8a1dee2024-02-21 19:33:09 +0100214
Eric Wild238891f2024-03-20 19:39:05 +0100215 // scale like uhd, +-2k -> +-32k
216 convert_and_scale(burst->begin(), which_in_buffer, buf_len * 2, SAMPLE_SCALE_FACTOR);
Eric Wildd8a1dee2024-02-21 19:33:09 +0100217
Eric Wild238891f2024-03-20 19:39:05 +0100218 auto rv = detectSCHBurst(*burst, 4, 4, corr_type, &ebp);
Eric Wildd8a1dee2024-02-21 19:33:09 +0100219
Eric Wild238891f2024-03-20 19:39:05 +0100220 int howmuchdelay = ebp.toa * 4;
Eric Wildd8a1dee2024-02-21 19:33:09 +0100221
Eric Wild238891f2024-03-20 19:39:05 +0100222 if (!rv) {
Eric Wildd8a1dee2024-02-21 19:33:09 +0100223 delete burst;
Eric Wild238891f2024-03-20 19:39:05 +0100224 DBGLG() << "SCH : \x1B[31m detect fail \033[0m NOOOOOOOOOOOOOOOOOO toa:" << ebp.toa << " "
225 << current_gsm_time.FN() << ":" << current_gsm_time.TN() << std::endl;
Eric Wildd8a1dee2024-02-21 19:33:09 +0100226 return false;
227 }
228
Eric Wild238891f2024-03-20 19:39:05 +0100229 SoftVector *bits;
Eric Wildd8a1dee2024-02-21 19:33:09 +0100230 if (is_first_sch_acq) {
Eric Wild238891f2024-03-20 19:39:05 +0100231 // can't be legit with a buf size spanning _at least_ one SCH but delay that implies partial sch burst
232 if (howmuchdelay < 0 || (buf_len - howmuchdelay) < ONE_TS_BURST_LEN) {
233 delete burst;
234 return false;
Eric Wildd8a1dee2024-02-21 19:33:09 +0100235 }
Eric Wild238891f2024-03-20 19:39:05 +0100236
237 struct estim_burst_params ebp2;
238 // auto sch_chunk = new signalVector(ONE_TS_BURST_LEN, 50);
239 // auto sch_chunk_start = sch_chunk->begin();
240 // memcpy(sch_chunk_start, sch_buf_f.data() + howmuchdelay, sizeof(std::complex<float>) * ONE_TS_BURST_LEN);
241
242 auto delay = delayVector(burst, NULL, -howmuchdelay);
243
244 scaleVector(*delay, (complex)1.0 / ebp.amp);
245
246 auto rv2 = detectSCHBurst(*delay, 4, 4, sch_detect_type::SCH_DETECT_FULL, &ebp2);
247 DBGLG() << "FIRST SCH : " << (rv2 ? "yes " : " ") << "Timing offset " << ebp2.toa
248 << " symbols" << std::endl;
249
250 bits = demodAnyBurst(*delay, SCH, 4, &ebp2);
251 delete delay;
252 } else {
253 bits = demodAnyBurst(*burst, SCH, 4, &ebp);
Eric Wildd8a1dee2024-02-21 19:33:09 +0100254 }
255
Eric Wild238891f2024-03-20 19:39:05 +0100256 delete burst;
257
258 // clamp to +-1.5 because +-127 softbits scaled by 64 after -0.5 can be at most +-1.5
259 clamp_array(bits->begin(), 148, 1.5f);
260
261 float_to_sbit(&bits->begin()[0], (signed char *)&sch_demod_bits[0], 62, 148);
Eric Wild238891f2024-03-20 19:39:05 +0100262
263 if (decode_sch((char *)sch_demod_bits, is_first_sch_acq)) {
264 auto current_gsm_time_updated = timekeeper.gsmtime();
265 if (is_first_sch_acq) {
266 // update ts to first sample in sch buffer, to allow delay calc for current ts
267 first_sch_ts_start = first_sch_buf_rcv_ts + howmuchdelay - (ts_offset_symb * 4);
268 } else {
269 // continuous sch tracking, only update if off too much
270 auto diff = [](float x, float y) { return x > y ? x - y : y - x; };
271
272 auto d = diff(ebp.toa, ts_offset_symb);
273 if (abs(d) > 0.3) {
274 if (ebp.toa < ts_offset_symb)
275 ebp.toa = d;
276 else
277 ebp.toa = -d;
278 temp_ts_corr_offset += ebp.toa * 4;
279
280 DBGLG() << "offs: " << ebp.toa << " " << temp_ts_corr_offset << std::endl;
281 }
282 }
283
284 auto a = gsm_sch_check_fn(current_gsm_time_updated.FN() - 1);
285 auto b = gsm_sch_check_fn(current_gsm_time_updated.FN());
286 auto c = gsm_sch_check_fn(current_gsm_time_updated.FN() + 1);
287 DBGLG() << "L SCH : Timing offset " << rv << " " << ebp.toa << " " << a << b << c << "fn "
288 << current_gsm_time_updated.FN() << ":" << current_gsm_time_updated.TN() << std::endl;
289
290 delete bits;
291 return true;
292 } else {
293 DBGLG2() << "L SCH : \x1B[31m decode fail \033[0m @ toa:" << ebp.toa << " "
294 << current_gsm_time.FN() << ":" << current_gsm_time.TN() << std::endl;
295 }
Eric Wildd8a1dee2024-02-21 19:33:09 +0100296
297 delete bits;
Eric Wildd8a1dee2024-02-21 19:33:09 +0100298 }
Ericb7253c62022-11-28 19:21:08 +0100299 return false;
300}
301
Eric3e7f4b02023-05-23 12:50:25 +0200302/*
303accumulates a full big buffer consisting of 8*12 timeslots, then:
304either
3051) adjusts gain if necessary and starts over
3062) searches and finds SCH and is done
307*/
Ericb7253c62022-11-28 19:21:08 +0100308SCH_STATE ms_trx::search_for_sch(dev_buf_t *rcd)
309{
310 static unsigned int sch_pos = 0;
Eric3e7f4b02023-05-23 12:50:25 +0200311 auto to_copy = SCH_LEN_SPS - sch_pos;
312
Ericb7253c62022-11-28 19:21:08 +0100313 if (sch_thread_done)
314 return SCH_STATE::FOUND;
315
316 if (rcv_done)
317 return SCH_STATE::SEARCHING;
318
Eric3e7f4b02023-05-23 12:50:25 +0200319 if (sch_pos == 0) // keep first ts for time delta calc
Ericb7253c62022-11-28 19:21:08 +0100320 first_sch_buf_rcv_ts = rcd->get_first_ts();
321
Eric3e7f4b02023-05-23 12:50:25 +0200322 if (to_copy) {
323 auto spsmax = rcd->actual_samples_per_buffer();
324 if (to_copy > (unsigned int)spsmax)
325 sch_pos += rcd->readall(first_sch_buf + sch_pos);
326 else
327 sch_pos += rcd->read_n(first_sch_buf + sch_pos, 0, to_copy);
328 } else { // (!to_copy)
Ericb7253c62022-11-28 19:21:08 +0100329 sch_pos = 0;
330 rcv_done = true;
Eric3e7f4b02023-05-23 12:50:25 +0200331 auto sch_search_fun = [this] {
Ericb7253c62022-11-28 19:21:08 +0100332 const auto target_val = rxFullScale / 8;
Eric6a3e4b32023-04-26 22:12:06 +0200333 float sum = normed_abs_sum(first_sch_buf, SCH_LEN_SPS);
Ericb7253c62022-11-28 19:21:08 +0100334
335 //FIXME: arbitrary value, gain cutoff
Eric Wild238891f2024-03-20 19:39:05 +0100336 if (sum > target_val || cfg.rxgain >= 60) // enough ?
Ericb7253c62022-11-28 19:21:08 +0100337 sch_thread_done = this->handle_sch(true);
338 else {
Eric Wild238891f2024-03-20 19:39:05 +0100339 std::cerr << "\x1B[32m #RXG \033[0m gain " << cfg.rxgain << " -> " << cfg.rxgain + 4
Ericb7253c62022-11-28 19:21:08 +0100340 << " sample avg:" << sum << " target: >=" << target_val << std::endl;
Eric Wild238891f2024-03-20 19:39:05 +0100341 setRxGain(cfg.rxgain + 4);
Ericb7253c62022-11-28 19:21:08 +0100342 }
343
344 if (!sch_thread_done)
345 rcv_done = false; // retry!
Eric3e7f4b02023-05-23 12:50:25 +0200346 };
347 worker_thread.add_task(sch_search_fun);
Ericb7253c62022-11-28 19:21:08 +0100348 }
Ericb7253c62022-11-28 19:21:08 +0100349 return SCH_STATE::SEARCHING;
350}
351
352void ms_trx::grab_bursts(dev_buf_t *rcd)
353{
354 // partial burst samples read from the last buffer
355 static int partial_rdofs = 0;
356 static bool first_call = true;
357 int to_skip = 0;
358
359 // round up to next burst by calculating the time between sch detection and now
360 if (first_call) {
361 const auto next_burst_start = rcd->get_first_ts() - first_sch_ts_start;
362 const auto fullts = next_burst_start / ONE_TS_BURST_LEN;
363 const auto fracts = next_burst_start % ONE_TS_BURST_LEN;
364 to_skip = ONE_TS_BURST_LEN - fracts;
365
366 for (unsigned int i = 0; i < fullts; i++)
367 timekeeper.inc_and_update(first_sch_ts_start + i * ONE_TS_BURST_LEN);
368
369 if (fracts)
370 timekeeper.inc_both();
Ericb7253c62022-11-28 19:21:08 +0100371
372 timekeeper.dec_by_one(); // oops, off by one?
373
374 timekeeper.set(timekeeper.gsmtime(), rcd->get_first_ts() - ONE_TS_BURST_LEN + to_skip);
375
376 DBGLG() << "this ts: " << rcd->get_first_ts() << " diff full TN: " << fullts << " frac TN: " << fracts
377 << " GSM now: " << timekeeper.gsmtime().FN() << ":" << timekeeper.gsmtime().TN() << " is sch? "
378 << gsm_sch_check_fn(timekeeper.gsmtime().FN()) << std::endl;
379 first_call = false;
380 }
381
382 if (partial_rdofs) {
383 auto first_remaining = ONE_TS_BURST_LEN - partial_rdofs;
384 auto rd = rcd->read_n(burst_copy_buffer + partial_rdofs, 0, first_remaining);
385 if (rd != (int)first_remaining) {
386 partial_rdofs += rd;
387 return;
388 }
389
390 timekeeper.inc_and_update_safe(rcd->get_first_ts() - partial_rdofs);
391 handle_sch_or_nb();
392 to_skip = first_remaining;
393 }
394
395 // apply sample rate slippage compensation
396 to_skip -= temp_ts_corr_offset;
397
398 // FIXME: happens rarely, read_n start -1 blows up
399 // this is fine: will just be corrected one buffer later
400 if (to_skip < 0)
401 to_skip = 0;
402 else
403 temp_ts_corr_offset = 0;
404
405 const auto left_after_burst = rcd->actual_samples_per_buffer() - to_skip;
406
407 const int full = left_after_burst / ONE_TS_BURST_LEN;
408 const int frac = left_after_burst % ONE_TS_BURST_LEN;
409
410 for (int i = 0; i < full; i++) {
411 rcd->read_n(burst_copy_buffer, to_skip + i * ONE_TS_BURST_LEN, ONE_TS_BURST_LEN);
412 timekeeper.inc_and_update_safe(rcd->get_first_ts() + to_skip + i * ONE_TS_BURST_LEN);
413 handle_sch_or_nb();
414 }
415
416 if (frac)
417 rcd->read_n(burst_copy_buffer, to_skip + full * ONE_TS_BURST_LEN, frac);
418 partial_rdofs = frac;
419}