blob: 1872c6c6c8f84e6ee654f54e70657889c9ad2220 [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
Ericb7253c62022-11-28 19:21:08 +010022#include <atomic>
23#include <cassert>
24#include <complex>
25#include <iostream>
26#include <future>
27
28#include "ms.h"
29#include "grgsm_vitac/grgsm_vitac.h"
30
Eric3e7f4b02023-05-23 12:50:25 +020031#include "threadpool.h"
32
Ericb7253c62022-11-28 19:21:08 +010033extern "C" {
34#include "sch.h"
35}
36
37#ifdef LOG
38#undef LOG
39#endif
40
Ericea7bd5f2023-05-02 15:21:45 +020041#if !defined(NODAMNLOG)
Ericb7253c62022-11-28 19:21:08 +010042#define DBGLG(...) ms_trx::dummy_log()
43#else
44#define DBGLG(...) std::cerr
45#endif
46
Ericea7bd5f2023-05-02 15:21:45 +020047#if !defined(NODAMNLOG)
Ericb7253c62022-11-28 19:21:08 +010048#define DBGLG2(...) ms_trx::dummy_log()
49#else
50#define DBGLG2(...) std::cerr
51#endif
52
53#define PRINT_Q_OVERFLOW
54bool ms_trx::decode_sch(float *bits, bool update_global_clock)
55{
56 int fn;
57 struct sch_info sch;
58 ubit_t info[GSM_SCH_INFO_LEN];
59 sbit_t data[GSM_SCH_CODED_LEN];
60
61 float_to_sbit(&bits[3], &data[0], 1, 39);
62 float_to_sbit(&bits[106], &data[39], 1, 39);
63
64 if (!gsm_sch_decode(info, data)) {
65 gsm_sch_parse(info, &sch);
66
67 if (update_global_clock) {
68 DBGLG() << "SCH : Decoded values" << std::endl;
69 DBGLG() << " BSIC: " << sch.bsic << std::endl;
70 DBGLG() << " TSC: " << (sch.bsic & 0x7) << std::endl;
71 DBGLG() << " T1 : " << sch.t1 << std::endl;
72 DBGLG() << " T2 : " << sch.t2 << std::endl;
73 DBGLG() << " T3p : " << sch.t3p << std::endl;
74 DBGLG() << " FN : " << gsm_sch_to_fn(&sch) << std::endl;
75 }
76
77 fn = gsm_sch_to_fn(&sch);
78 if (fn < 0) { // how? wh?
79 DBGLG() << "SCH : Failed to convert FN " << std::endl;
80 return false;
81 }
82
83 if (update_global_clock) {
84 mBSIC = sch.bsic;
85 mTSC = sch.bsic & 0x7;
86 timekeeper.set(fn, 0);
87 // global_time_keeper.FN(fn);
88 // global_time_keeper.TN(0);
89 }
Ericb7253c62022-11-28 19:21:08 +010090
91 return true;
92 }
93 return false;
94}
95
96void ms_trx::maybe_update_gain(one_burst &brst)
97{
98 static_assert((sizeof(brst.burst) / sizeof(brst.burst[0])) == ONE_TS_BURST_LEN, "wtf, buffer size mismatch?");
99 const int avgburst_num = 8 * 20; // ~ 50*4.5ms = 90ms?
100 static_assert(avgburst_num * 577 > (50 * 1000), "can't update faster then blade wait time?");
101 const unsigned int rx_max_cutoff = (rxFullScale * 2) / 3;
102 static int gain_check = 0;
103 static float runmean = 0;
Eric6a3e4b32023-04-26 22:12:06 +0200104 float sum = normed_abs_sum(&brst.burst[0], ONE_TS_BURST_LEN);
Ericb7253c62022-11-28 19:21:08 +0100105 runmean = gain_check ? (runmean * (gain_check + 2) - 1 + sum) / (gain_check + 2) : sum;
106
107 if (gain_check == avgburst_num - 1) {
108 DBGLG2() << "\x1B[32m #RXG \033[0m" << rxgain << " " << runmean << " " << sum << std::endl;
109 auto gainoffset = runmean < (rxFullScale / 4 ? 4 : 2);
110 gainoffset = runmean < (rxFullScale / 2 ? 2 : 1);
111 float newgain = runmean < rx_max_cutoff ? rxgain + gainoffset : rxgain - gainoffset;
112 // FIXME: gian cutoff
Eric3e7f4b02023-05-23 12:50:25 +0200113 if (newgain != rxgain && newgain <= 60) {
114 auto gain_fun = [this, newgain] { setRxGain(newgain); };
115 worker_thread.add_task(gain_fun);
116 }
117
Ericb7253c62022-11-28 19:21:08 +0100118 runmean = 0;
119 }
120 gain_check = (gain_check + 1) % avgburst_num;
121}
122
123static char sch_demod_bits[148];
124
125bool ms_trx::handle_sch_or_nb()
126{
127 one_burst brst;
128 const auto current_gsm_time = timekeeper.gsmtime();
129 const auto is_sch = gsm_sch_check_ts(current_gsm_time.TN(), current_gsm_time.FN());
130
131 //either pass burst to upper layer for demod, OR pass demodded SCH to upper layer so we don't waste time processing it twice
132 brst.gsmts = current_gsm_time;
133
134 if (!is_sch) {
135 memcpy(brst.burst, burst_copy_buffer, sizeof(blade_sample_type) * ONE_TS_BURST_LEN);
136 } else {
137 handle_sch(false);
138 memcpy(brst.sch_bits, sch_demod_bits, sizeof(sch_demod_bits));
139 }
Ericea7bd5f2023-05-02 15:21:45 +0200140
Ericc3e515a2023-05-08 12:56:56 +0200141 while (upper_is_ready && !rxqueue.spsc_push(&brst))
142 ;
Ericb7253c62022-11-28 19:21:08 +0100143
144 if (do_auto_gain)
145 maybe_update_gain(brst);
146
147 return false;
148}
149
150static float sch_acq_buffer[SCH_LEN_SPS * 2];
151
152bool ms_trx::handle_sch(bool is_first_sch_acq)
153{
154 auto current_gsm_time = timekeeper.gsmtime();
155 const auto buf_len = is_first_sch_acq ? SCH_LEN_SPS : ONE_TS_BURST_LEN;
156 const auto which_in_buffer = is_first_sch_acq ? first_sch_buf : burst_copy_buffer;
157 const auto which_out_buffer = is_first_sch_acq ? sch_acq_buffer : &sch_acq_buffer[40 * 2];
158 const auto ss = reinterpret_cast<std::complex<float> *>(which_out_buffer);
159 std::complex<float> channel_imp_resp[CHAN_IMP_RESP_LENGTH * d_OSR];
160
161 int start;
162 memset((void *)&sch_acq_buffer[0], 0, sizeof(sch_acq_buffer));
Ericbcaafca2023-04-26 13:57:26 +0200163 convert_and_scale(which_out_buffer, which_in_buffer, buf_len * 2, 1.f / float(rxFullScale));
Ericb7253c62022-11-28 19:21:08 +0100164 if (is_first_sch_acq) {
165 float max_corr = 0;
Ericb7253c62022-11-28 19:21:08 +0100166 start = get_sch_buffer_chan_imp_resp(ss, &channel_imp_resp[0], buf_len, &max_corr);
Ericb7253c62022-11-28 19:21:08 +0100167 } else {
Ericb7253c62022-11-28 19:21:08 +0100168 start = get_sch_chan_imp_resp(ss, &channel_imp_resp[0]);
169 start = start < 39 ? start : 39;
170 start = start > -39 ? start : -39;
Ericb7253c62022-11-28 19:21:08 +0100171 }
Ericbcaafca2023-04-26 13:57:26 +0200172 detect_burst(&ss[start], &channel_imp_resp[0], 0, sch_demod_bits);
Ericb7253c62022-11-28 19:21:08 +0100173
174 SoftVector bitss(148);
175 for (int i = 0; i < 148; i++) {
176 bitss[i] = (sch_demod_bits[i]);
177 }
178
179 auto sch_decode_success = decode_sch(bitss.begin(), is_first_sch_acq);
180
181 if (sch_decode_success) {
182 const auto ts_offset_symb = 0;
183 if (is_first_sch_acq) {
184 // update ts to first sample in sch buffer, to allow delay calc for current ts
185 first_sch_ts_start = first_sch_buf_rcv_ts + start - (ts_offset_symb * 4) - 1;
186 } else if (abs(start) > 1) {
187 // continuous sch tracking, only update if off too much
188 temp_ts_corr_offset += -start;
189 std::cerr << "offs: " << start << " " << temp_ts_corr_offset << std::endl;
190 }
191
192 return true;
193 } else {
194 DBGLG2() << "L SCH : \x1B[31m decode fail \033[0m @ toa:" << start << " " << current_gsm_time.FN()
195 << ":" << current_gsm_time.TN() << std::endl;
196 }
197 return false;
198}
199
Eric3e7f4b02023-05-23 12:50:25 +0200200/*
201accumulates a full big buffer consisting of 8*12 timeslots, then:
202either
2031) adjusts gain if necessary and starts over
2042) searches and finds SCH and is done
205*/
Ericb7253c62022-11-28 19:21:08 +0100206SCH_STATE ms_trx::search_for_sch(dev_buf_t *rcd)
207{
208 static unsigned int sch_pos = 0;
Eric3e7f4b02023-05-23 12:50:25 +0200209 auto to_copy = SCH_LEN_SPS - sch_pos;
210
Ericb7253c62022-11-28 19:21:08 +0100211 if (sch_thread_done)
212 return SCH_STATE::FOUND;
213
214 if (rcv_done)
215 return SCH_STATE::SEARCHING;
216
Eric3e7f4b02023-05-23 12:50:25 +0200217 if (sch_pos == 0) // keep first ts for time delta calc
Ericb7253c62022-11-28 19:21:08 +0100218 first_sch_buf_rcv_ts = rcd->get_first_ts();
219
Eric3e7f4b02023-05-23 12:50:25 +0200220 if (to_copy) {
221 auto spsmax = rcd->actual_samples_per_buffer();
222 if (to_copy > (unsigned int)spsmax)
223 sch_pos += rcd->readall(first_sch_buf + sch_pos);
224 else
225 sch_pos += rcd->read_n(first_sch_buf + sch_pos, 0, to_copy);
226 } else { // (!to_copy)
Ericb7253c62022-11-28 19:21:08 +0100227 sch_pos = 0;
228 rcv_done = true;
Eric3e7f4b02023-05-23 12:50:25 +0200229 auto sch_search_fun = [this] {
Ericb7253c62022-11-28 19:21:08 +0100230 const auto target_val = rxFullScale / 8;
Eric6a3e4b32023-04-26 22:12:06 +0200231 float sum = normed_abs_sum(first_sch_buf, SCH_LEN_SPS);
Ericb7253c62022-11-28 19:21:08 +0100232
233 //FIXME: arbitrary value, gain cutoff
234 if (sum > target_val || rxgain >= 60) // enough ?
235 sch_thread_done = this->handle_sch(true);
236 else {
237 std::cerr << "\x1B[32m #RXG \033[0m gain " << rxgain << " -> " << rxgain + 4
238 << " sample avg:" << sum << " target: >=" << target_val << std::endl;
239 setRxGain(rxgain + 4);
240 }
241
242 if (!sch_thread_done)
243 rcv_done = false; // retry!
Eric3e7f4b02023-05-23 12:50:25 +0200244 };
245 worker_thread.add_task(sch_search_fun);
Ericb7253c62022-11-28 19:21:08 +0100246 }
Ericb7253c62022-11-28 19:21:08 +0100247 return SCH_STATE::SEARCHING;
248}
249
250void ms_trx::grab_bursts(dev_buf_t *rcd)
251{
252 // partial burst samples read from the last buffer
253 static int partial_rdofs = 0;
254 static bool first_call = true;
255 int to_skip = 0;
256
257 // round up to next burst by calculating the time between sch detection and now
258 if (first_call) {
259 const auto next_burst_start = rcd->get_first_ts() - first_sch_ts_start;
260 const auto fullts = next_burst_start / ONE_TS_BURST_LEN;
261 const auto fracts = next_burst_start % ONE_TS_BURST_LEN;
262 to_skip = ONE_TS_BURST_LEN - fracts;
263
264 for (unsigned int i = 0; i < fullts; i++)
265 timekeeper.inc_and_update(first_sch_ts_start + i * ONE_TS_BURST_LEN);
266
267 if (fracts)
268 timekeeper.inc_both();
269 // timekeeper.inc_and_update(first_sch_ts_start + 1 * ONE_TS_BURST_LEN);
270
271 timekeeper.dec_by_one(); // oops, off by one?
272
273 timekeeper.set(timekeeper.gsmtime(), rcd->get_first_ts() - ONE_TS_BURST_LEN + to_skip);
274
275 DBGLG() << "this ts: " << rcd->get_first_ts() << " diff full TN: " << fullts << " frac TN: " << fracts
276 << " GSM now: " << timekeeper.gsmtime().FN() << ":" << timekeeper.gsmtime().TN() << " is sch? "
277 << gsm_sch_check_fn(timekeeper.gsmtime().FN()) << std::endl;
278 first_call = false;
279 }
280
281 if (partial_rdofs) {
282 auto first_remaining = ONE_TS_BURST_LEN - partial_rdofs;
283 auto rd = rcd->read_n(burst_copy_buffer + partial_rdofs, 0, first_remaining);
284 if (rd != (int)first_remaining) {
285 partial_rdofs += rd;
286 return;
287 }
288
289 timekeeper.inc_and_update_safe(rcd->get_first_ts() - partial_rdofs);
290 handle_sch_or_nb();
291 to_skip = first_remaining;
292 }
293
294 // apply sample rate slippage compensation
295 to_skip -= temp_ts_corr_offset;
296
297 // FIXME: happens rarely, read_n start -1 blows up
298 // this is fine: will just be corrected one buffer later
299 if (to_skip < 0)
300 to_skip = 0;
301 else
302 temp_ts_corr_offset = 0;
303
304 const auto left_after_burst = rcd->actual_samples_per_buffer() - to_skip;
305
306 const int full = left_after_burst / ONE_TS_BURST_LEN;
307 const int frac = left_after_burst % ONE_TS_BURST_LEN;
308
309 for (int i = 0; i < full; i++) {
310 rcd->read_n(burst_copy_buffer, to_skip + i * ONE_TS_BURST_LEN, ONE_TS_BURST_LEN);
311 timekeeper.inc_and_update_safe(rcd->get_first_ts() + to_skip + i * ONE_TS_BURST_LEN);
312 handle_sch_or_nb();
313 }
314
315 if (frac)
316 rcd->read_n(burst_copy_buffer, to_skip + full * ONE_TS_BURST_LEN, frac);
317 partial_rdofs = frac;
318}