blob: dc0d56d9b2398355740376e3a3d82f842ef2e84f [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
22#include "sigProcLib.h"
23#include "signalVector.h"
24#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
43#if !defined(SYNCTHINGONLY) //|| !defined(NODAMNLOG)
44#define DBGLG(...) ms_trx::dummy_log()
45#else
46#define DBGLG(...) std::cerr
47#endif
48
49#if !defined(SYNCTHINGONLY) || !defined(NODAMNLOG)
50#define DBGLG2(...) ms_trx::dummy_log()
51#else
52#define DBGLG2(...) std::cerr
53#endif
54
55#define PRINT_Q_OVERFLOW
56bool ms_trx::decode_sch(float *bits, bool update_global_clock)
57{
58 int fn;
59 struct sch_info sch;
60 ubit_t info[GSM_SCH_INFO_LEN];
61 sbit_t data[GSM_SCH_CODED_LEN];
62
63 float_to_sbit(&bits[3], &data[0], 1, 39);
64 float_to_sbit(&bits[106], &data[39], 1, 39);
65
66 if (!gsm_sch_decode(info, data)) {
67 gsm_sch_parse(info, &sch);
68
69 if (update_global_clock) {
70 DBGLG() << "SCH : Decoded values" << std::endl;
71 DBGLG() << " BSIC: " << sch.bsic << std::endl;
72 DBGLG() << " TSC: " << (sch.bsic & 0x7) << std::endl;
73 DBGLG() << " T1 : " << sch.t1 << std::endl;
74 DBGLG() << " T2 : " << sch.t2 << std::endl;
75 DBGLG() << " T3p : " << sch.t3p << std::endl;
76 DBGLG() << " FN : " << gsm_sch_to_fn(&sch) << std::endl;
77 }
78
79 fn = gsm_sch_to_fn(&sch);
80 if (fn < 0) { // how? wh?
81 DBGLG() << "SCH : Failed to convert FN " << std::endl;
82 return false;
83 }
84
85 if (update_global_clock) {
86 mBSIC = sch.bsic;
87 mTSC = sch.bsic & 0x7;
88 timekeeper.set(fn, 0);
89 // global_time_keeper.FN(fn);
90 // global_time_keeper.TN(0);
91 }
92#ifdef SYNCTHINGONLY
93 else {
94 int t3 = sch.t3p * 10 + 1;
95 if (t3 == 11) {
96 // timeslot hitter attempt @ fn 21 in mf
97 DBGLG2() << "sch @ " << t3 << std::endl;
98 auto e = GSM::Time(fn, 0);
99 e += 10;
100 ts_hitter_q.spsc_push(&e);
101 }
102 }
103#endif
104
105 return true;
106 }
107 return false;
108}
109
110void ms_trx::maybe_update_gain(one_burst &brst)
111{
112 static_assert((sizeof(brst.burst) / sizeof(brst.burst[0])) == ONE_TS_BURST_LEN, "wtf, buffer size mismatch?");
113 const int avgburst_num = 8 * 20; // ~ 50*4.5ms = 90ms?
114 static_assert(avgburst_num * 577 > (50 * 1000), "can't update faster then blade wait time?");
115 const unsigned int rx_max_cutoff = (rxFullScale * 2) / 3;
116 static int gain_check = 0;
117 static float runmean = 0;
118 float sum = 0;
119 for (auto i : brst.burst)
120 sum += abs(i.real()) + abs(i.imag());
121 sum /= ONE_TS_BURST_LEN * 2;
122
123 runmean = gain_check ? (runmean * (gain_check + 2) - 1 + sum) / (gain_check + 2) : sum;
124
125 if (gain_check == avgburst_num - 1) {
126 DBGLG2() << "\x1B[32m #RXG \033[0m" << rxgain << " " << runmean << " " << sum << std::endl;
127 auto gainoffset = runmean < (rxFullScale / 4 ? 4 : 2);
128 gainoffset = runmean < (rxFullScale / 2 ? 2 : 1);
129 float newgain = runmean < rx_max_cutoff ? rxgain + gainoffset : rxgain - gainoffset;
130 // FIXME: gian cutoff
Eric3e7f4b02023-05-23 12:50:25 +0200131 if (newgain != rxgain && newgain <= 60) {
132 auto gain_fun = [this, newgain] { setRxGain(newgain); };
133 worker_thread.add_task(gain_fun);
134 }
135
Ericb7253c62022-11-28 19:21:08 +0100136 runmean = 0;
137 }
138 gain_check = (gain_check + 1) % avgburst_num;
139}
140
141static char sch_demod_bits[148];
142
143bool ms_trx::handle_sch_or_nb()
144{
145 one_burst brst;
146 const auto current_gsm_time = timekeeper.gsmtime();
147 const auto is_sch = gsm_sch_check_ts(current_gsm_time.TN(), current_gsm_time.FN());
148
149 //either pass burst to upper layer for demod, OR pass demodded SCH to upper layer so we don't waste time processing it twice
150 brst.gsmts = current_gsm_time;
151
152 if (!is_sch) {
153 memcpy(brst.burst, burst_copy_buffer, sizeof(blade_sample_type) * ONE_TS_BURST_LEN);
154 } else {
155 handle_sch(false);
156 memcpy(brst.sch_bits, sch_demod_bits, sizeof(sch_demod_bits));
157 }
158#ifndef SYNCTHINGONLY
159 if (upper_is_ready) { // this is blocking, so only submit if there is a reader - only if upper exists!
160#endif
161 while (!rxqueue.spsc_push(&brst))
162 ;
163#ifndef SYNCTHINGONLY
164 }
165#endif
166
167 if (do_auto_gain)
168 maybe_update_gain(brst);
169
170 return false;
171}
172
173static float sch_acq_buffer[SCH_LEN_SPS * 2];
174
175bool ms_trx::handle_sch(bool is_first_sch_acq)
176{
177 auto current_gsm_time = timekeeper.gsmtime();
178 const auto buf_len = is_first_sch_acq ? SCH_LEN_SPS : ONE_TS_BURST_LEN;
179 const auto which_in_buffer = is_first_sch_acq ? first_sch_buf : burst_copy_buffer;
180 const auto which_out_buffer = is_first_sch_acq ? sch_acq_buffer : &sch_acq_buffer[40 * 2];
181 const auto ss = reinterpret_cast<std::complex<float> *>(which_out_buffer);
182 std::complex<float> channel_imp_resp[CHAN_IMP_RESP_LENGTH * d_OSR];
183
184 int start;
185 memset((void *)&sch_acq_buffer[0], 0, sizeof(sch_acq_buffer));
186 if (is_first_sch_acq) {
187 float max_corr = 0;
Eric Wild621a49e2023-01-12 16:21:58 +0100188 convert_and_scale(which_out_buffer, which_in_buffer, buf_len * 2, 1.f / float(rxFullScale));
Ericb7253c62022-11-28 19:21:08 +0100189 start = get_sch_buffer_chan_imp_resp(ss, &channel_imp_resp[0], buf_len, &max_corr);
190 detect_burst(&ss[start], &channel_imp_resp[0], 0, sch_demod_bits);
191 } else {
Eric Wild621a49e2023-01-12 16:21:58 +0100192 convert_and_scale(which_out_buffer, which_in_buffer, buf_len * 2, 1.f / float(rxFullScale));
Ericb7253c62022-11-28 19:21:08 +0100193 start = get_sch_chan_imp_resp(ss, &channel_imp_resp[0]);
194 start = start < 39 ? start : 39;
195 start = start > -39 ? start : -39;
196 detect_burst(&ss[start], &channel_imp_resp[0], 0, sch_demod_bits);
197 }
198
199 SoftVector bitss(148);
200 for (int i = 0; i < 148; i++) {
201 bitss[i] = (sch_demod_bits[i]);
202 }
203
204 auto sch_decode_success = decode_sch(bitss.begin(), is_first_sch_acq);
205
206 if (sch_decode_success) {
207 const auto ts_offset_symb = 0;
208 if (is_first_sch_acq) {
209 // update ts to first sample in sch buffer, to allow delay calc for current ts
210 first_sch_ts_start = first_sch_buf_rcv_ts + start - (ts_offset_symb * 4) - 1;
211 } else if (abs(start) > 1) {
212 // continuous sch tracking, only update if off too much
213 temp_ts_corr_offset += -start;
214 std::cerr << "offs: " << start << " " << temp_ts_corr_offset << std::endl;
215 }
216
217 return true;
218 } else {
219 DBGLG2() << "L SCH : \x1B[31m decode fail \033[0m @ toa:" << start << " " << current_gsm_time.FN()
220 << ":" << current_gsm_time.TN() << std::endl;
221 }
222 return false;
223}
224
Eric3e7f4b02023-05-23 12:50:25 +0200225/*
226accumulates a full big buffer consisting of 8*12 timeslots, then:
227either
2281) adjusts gain if necessary and starts over
2292) searches and finds SCH and is done
230*/
Ericb7253c62022-11-28 19:21:08 +0100231SCH_STATE ms_trx::search_for_sch(dev_buf_t *rcd)
232{
233 static unsigned int sch_pos = 0;
Eric3e7f4b02023-05-23 12:50:25 +0200234 auto to_copy = SCH_LEN_SPS - sch_pos;
235
Ericb7253c62022-11-28 19:21:08 +0100236 if (sch_thread_done)
237 return SCH_STATE::FOUND;
238
239 if (rcv_done)
240 return SCH_STATE::SEARCHING;
241
Eric3e7f4b02023-05-23 12:50:25 +0200242 if (sch_pos == 0) // keep first ts for time delta calc
Ericb7253c62022-11-28 19:21:08 +0100243 first_sch_buf_rcv_ts = rcd->get_first_ts();
244
Eric3e7f4b02023-05-23 12:50:25 +0200245 if (to_copy) {
246 auto spsmax = rcd->actual_samples_per_buffer();
247 if (to_copy > (unsigned int)spsmax)
248 sch_pos += rcd->readall(first_sch_buf + sch_pos);
249 else
250 sch_pos += rcd->read_n(first_sch_buf + sch_pos, 0, to_copy);
251 } else { // (!to_copy)
Ericb7253c62022-11-28 19:21:08 +0100252 sch_pos = 0;
253 rcv_done = true;
Eric3e7f4b02023-05-23 12:50:25 +0200254 auto sch_search_fun = [this] {
Ericb7253c62022-11-28 19:21:08 +0100255 auto ptr = reinterpret_cast<const int16_t *>(first_sch_buf);
256 const auto target_val = rxFullScale / 8;
257 float sum = 0;
258 for (unsigned int i = 0; i < SCH_LEN_SPS * 2; i++)
259 sum += std::abs(ptr[i]);
260 sum /= SCH_LEN_SPS * 2;
261
262 //FIXME: arbitrary value, gain cutoff
263 if (sum > target_val || rxgain >= 60) // enough ?
264 sch_thread_done = this->handle_sch(true);
265 else {
266 std::cerr << "\x1B[32m #RXG \033[0m gain " << rxgain << " -> " << rxgain + 4
267 << " sample avg:" << sum << " target: >=" << target_val << std::endl;
268 setRxGain(rxgain + 4);
269 }
270
271 if (!sch_thread_done)
272 rcv_done = false; // retry!
Eric3e7f4b02023-05-23 12:50:25 +0200273 };
274 worker_thread.add_task(sch_search_fun);
Ericb7253c62022-11-28 19:21:08 +0100275 }
Ericb7253c62022-11-28 19:21:08 +0100276 return SCH_STATE::SEARCHING;
277}
278
279void ms_trx::grab_bursts(dev_buf_t *rcd)
280{
281 // partial burst samples read from the last buffer
282 static int partial_rdofs = 0;
283 static bool first_call = true;
284 int to_skip = 0;
285
286 // round up to next burst by calculating the time between sch detection and now
287 if (first_call) {
288 const auto next_burst_start = rcd->get_first_ts() - first_sch_ts_start;
289 const auto fullts = next_burst_start / ONE_TS_BURST_LEN;
290 const auto fracts = next_burst_start % ONE_TS_BURST_LEN;
291 to_skip = ONE_TS_BURST_LEN - fracts;
292
293 for (unsigned int i = 0; i < fullts; i++)
294 timekeeper.inc_and_update(first_sch_ts_start + i * ONE_TS_BURST_LEN);
295
296 if (fracts)
297 timekeeper.inc_both();
298 // timekeeper.inc_and_update(first_sch_ts_start + 1 * ONE_TS_BURST_LEN);
299
300 timekeeper.dec_by_one(); // oops, off by one?
301
302 timekeeper.set(timekeeper.gsmtime(), rcd->get_first_ts() - ONE_TS_BURST_LEN + to_skip);
303
304 DBGLG() << "this ts: " << rcd->get_first_ts() << " diff full TN: " << fullts << " frac TN: " << fracts
305 << " GSM now: " << timekeeper.gsmtime().FN() << ":" << timekeeper.gsmtime().TN() << " is sch? "
306 << gsm_sch_check_fn(timekeeper.gsmtime().FN()) << std::endl;
307 first_call = false;
308 }
309
310 if (partial_rdofs) {
311 auto first_remaining = ONE_TS_BURST_LEN - partial_rdofs;
312 auto rd = rcd->read_n(burst_copy_buffer + partial_rdofs, 0, first_remaining);
313 if (rd != (int)first_remaining) {
314 partial_rdofs += rd;
315 return;
316 }
317
318 timekeeper.inc_and_update_safe(rcd->get_first_ts() - partial_rdofs);
319 handle_sch_or_nb();
320 to_skip = first_remaining;
321 }
322
323 // apply sample rate slippage compensation
324 to_skip -= temp_ts_corr_offset;
325
326 // FIXME: happens rarely, read_n start -1 blows up
327 // this is fine: will just be corrected one buffer later
328 if (to_skip < 0)
329 to_skip = 0;
330 else
331 temp_ts_corr_offset = 0;
332
333 const auto left_after_burst = rcd->actual_samples_per_buffer() - to_skip;
334
335 const int full = left_after_burst / ONE_TS_BURST_LEN;
336 const int frac = left_after_burst % ONE_TS_BURST_LEN;
337
338 for (int i = 0; i < full; i++) {
339 rcd->read_n(burst_copy_buffer, to_skip + i * ONE_TS_BURST_LEN, ONE_TS_BURST_LEN);
340 timekeeper.inc_and_update_safe(rcd->get_first_ts() + to_skip + i * ONE_TS_BURST_LEN);
341 handle_sch_or_nb();
342 }
343
344 if (frac)
345 rcd->read_n(burst_copy_buffer, to_skip + full * ONE_TS_BURST_LEN, frac);
346 partial_rdofs = frac;
347}