Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 1 | /* |
| 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 Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 22 | #include "sigProcLib.h" |
| 23 | #include "signalVector.h" |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 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 | |
Eric | 3e7f4b0 | 2023-05-23 12:50:25 +0200 | [diff] [blame] | 33 | #include "threadpool.h" |
| 34 | |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 35 | extern "C" { |
| 36 | #include "sch.h" |
| 37 | } |
| 38 | |
| 39 | #ifdef LOG |
| 40 | #undef LOG |
| 41 | #endif |
| 42 | |
Eric | ea7bd5f | 2023-05-02 15:21:45 +0200 | [diff] [blame] | 43 | #if !defined(NODAMNLOG) |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 44 | #define DBGLG(...) ms_trx::dummy_log() |
| 45 | #else |
| 46 | #define DBGLG(...) std::cerr |
| 47 | #endif |
| 48 | |
Eric | ea7bd5f | 2023-05-02 15:21:45 +0200 | [diff] [blame] | 49 | #if !defined(NODAMNLOG) |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 50 | #define DBGLG2(...) ms_trx::dummy_log() |
| 51 | #else |
| 52 | #define DBGLG2(...) std::cerr |
| 53 | #endif |
| 54 | |
| 55 | #define PRINT_Q_OVERFLOW |
Eric Wild | 2f40abd | 2023-05-22 22:50:43 +0200 | [diff] [blame] | 56 | |
Eric Wild | 98a50b7 | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 57 | extern std::atomic<bool> g_exit_flag; |
| 58 | |
Eric Wild | 2f40abd | 2023-05-22 22:50:43 +0200 | [diff] [blame] | 59 | bool ms_trx::decode_sch(char *bits, bool update_global_clock) |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 60 | { |
| 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 Wild | 2f40abd | 2023-05-22 22:50:43 +0200 | [diff] [blame] | 66 | memcpy(&data[0], &bits[3], 39); |
| 67 | memcpy(&data[39], &bits[106], 39); |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 68 | |
| 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); |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 92 | } |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 93 | |
| 94 | return true; |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | void 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; |
Eric | 6a3e4b3 | 2023-04-26 22:12:06 +0200 | [diff] [blame] | 107 | float sum = normed_abs_sum(&brst.burst[0], ONE_TS_BURST_LEN); |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 108 | runmean = gain_check ? (runmean * (gain_check + 2) - 1 + sum) / (gain_check + 2) : sum; |
| 109 | |
| 110 | if (gain_check == avgburst_num - 1) { |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 111 | DBGLG2() << "\x1B[32m #RXG \033[0m" << cfg.rxgain << " " << runmean << " " << sum << std::endl; |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 112 | auto gainoffset = runmean < (rxFullScale / 4 ? 4 : 2); |
| 113 | gainoffset = runmean < (rxFullScale / 2 ? 2 : 1); |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 114 | float newgain = runmean < rx_max_cutoff ? cfg.rxgain + gainoffset : cfg.rxgain - gainoffset; |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 115 | // FIXME: gian cutoff |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 116 | if (newgain != cfg.rxgain && newgain <= 60) { |
Eric | 3e7f4b0 | 2023-05-23 12:50:25 +0200 | [diff] [blame] | 117 | auto gain_fun = [this, newgain] { setRxGain(newgain); }; |
| 118 | worker_thread.add_task(gain_fun); |
| 119 | } |
| 120 | |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 121 | runmean = 0; |
| 122 | } |
| 123 | gain_check = (gain_check + 1) % avgburst_num; |
| 124 | } |
| 125 | |
| 126 | static char sch_demod_bits[148]; |
| 127 | |
| 128 | bool 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 | } |
Eric | ea7bd5f | 2023-05-02 15:21:45 +0200 | [diff] [blame] | 143 | |
Eric Wild | 98a50b7 | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 144 | while (!g_exit_flag && upper_is_ready && !rxqueue.spsc_push(&brst)) |
Eric | c3e515a | 2023-05-08 12:56:56 +0200 | [diff] [blame] | 145 | ; |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 146 | |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 147 | if (!use_agc) |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 148 | maybe_update_gain(brst); |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | static float sch_acq_buffer[SCH_LEN_SPS * 2]; |
| 154 | |
| 155 | bool 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 Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 160 | memset((void *)&sch_acq_buffer[0], 0, sizeof(sch_acq_buffer)); |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 161 | 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); |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 176 | |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 177 | auto sch_decode_success = decode_sch(sch_demod_bits, is_first_sch_acq); |
| 178 | #if 0 // useful to debug offset shifts |
Eric Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 179 | 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; |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 182 | |
Eric Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 183 | // 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 Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 192 | 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; |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 207 | } |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 208 | } else { |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 209 | 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 Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 214 | |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 215 | // scale like uhd, +-2k -> +-32k |
| 216 | convert_and_scale(burst->begin(), which_in_buffer, buf_len * 2, SAMPLE_SCALE_FACTOR); |
Eric Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 217 | |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 218 | auto rv = detectSCHBurst(*burst, 4, 4, corr_type, &ebp); |
Eric Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 219 | |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 220 | int howmuchdelay = ebp.toa * 4; |
Eric Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 221 | |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 222 | if (!rv) { |
Eric Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 223 | delete burst; |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 224 | DBGLG() << "SCH : \x1B[31m detect fail \033[0m NOOOOOOOOOOOOOOOOOO toa:" << ebp.toa << " " |
| 225 | << current_gsm_time.FN() << ":" << current_gsm_time.TN() << std::endl; |
Eric Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 229 | SoftVector *bits; |
Eric Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 230 | if (is_first_sch_acq) { |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 231 | // 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 Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 235 | } |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 236 | |
| 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 Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 256 | 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 Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 262 | |
| 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 Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 296 | |
| 297 | delete bits; |
Eric Wild | d8a1dee | 2024-02-21 19:33:09 +0100 | [diff] [blame] | 298 | } |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 299 | return false; |
| 300 | } |
| 301 | |
Eric | 3e7f4b0 | 2023-05-23 12:50:25 +0200 | [diff] [blame] | 302 | /* |
| 303 | accumulates a full big buffer consisting of 8*12 timeslots, then: |
| 304 | either |
| 305 | 1) adjusts gain if necessary and starts over |
| 306 | 2) searches and finds SCH and is done |
| 307 | */ |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 308 | SCH_STATE ms_trx::search_for_sch(dev_buf_t *rcd) |
| 309 | { |
| 310 | static unsigned int sch_pos = 0; |
Eric | 3e7f4b0 | 2023-05-23 12:50:25 +0200 | [diff] [blame] | 311 | auto to_copy = SCH_LEN_SPS - sch_pos; |
| 312 | |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 313 | if (sch_thread_done) |
| 314 | return SCH_STATE::FOUND; |
| 315 | |
| 316 | if (rcv_done) |
| 317 | return SCH_STATE::SEARCHING; |
| 318 | |
Eric | 3e7f4b0 | 2023-05-23 12:50:25 +0200 | [diff] [blame] | 319 | if (sch_pos == 0) // keep first ts for time delta calc |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 320 | first_sch_buf_rcv_ts = rcd->get_first_ts(); |
| 321 | |
Eric | 3e7f4b0 | 2023-05-23 12:50:25 +0200 | [diff] [blame] | 322 | 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) |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 329 | sch_pos = 0; |
| 330 | rcv_done = true; |
Eric | 3e7f4b0 | 2023-05-23 12:50:25 +0200 | [diff] [blame] | 331 | auto sch_search_fun = [this] { |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 332 | const auto target_val = rxFullScale / 8; |
Eric | 6a3e4b3 | 2023-04-26 22:12:06 +0200 | [diff] [blame] | 333 | float sum = normed_abs_sum(first_sch_buf, SCH_LEN_SPS); |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 334 | |
| 335 | //FIXME: arbitrary value, gain cutoff |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 336 | if (sum > target_val || cfg.rxgain >= 60) // enough ? |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 337 | sch_thread_done = this->handle_sch(true); |
| 338 | else { |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 339 | std::cerr << "\x1B[32m #RXG \033[0m gain " << cfg.rxgain << " -> " << cfg.rxgain + 4 |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 340 | << " sample avg:" << sum << " target: >=" << target_val << std::endl; |
Eric Wild | 238891f | 2024-03-20 19:39:05 +0100 | [diff] [blame] | 341 | setRxGain(cfg.rxgain + 4); |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | if (!sch_thread_done) |
| 345 | rcv_done = false; // retry! |
Eric | 3e7f4b0 | 2023-05-23 12:50:25 +0200 | [diff] [blame] | 346 | }; |
| 347 | worker_thread.add_task(sch_search_fun); |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 348 | } |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 349 | return SCH_STATE::SEARCHING; |
| 350 | } |
| 351 | |
| 352 | void 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(); |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 371 | |
| 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 | } |