blob: 9ac2171095764bc94ac62248163cecffe455224b [file] [log] [blame]
Ericb7253c62022-11-28 19:21:08 +01001#pragma once
2/*
3 * (C) 2022 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Eric Wild <ewild@sysmocom.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <atomic>
24#include <cassert>
25#include <complex>
26#include <cstdint>
27#include <mutex>
28#include <iostream>
Eric989fe752023-10-06 16:06:13 +020029// #include <thread>
Ericb7253c62022-11-28 19:21:08 +010030
31#if defined(BUILDBLADE)
32#include "bladerf_specific.h"
33#define BASET blade_hw<ms_trx>
34#elif defined(BUILDUHD)
35#include "uhd_specific.h"
36#define BASET uhd_hw<ms_trx>
37#else
38#error wat? no device..
39#endif
40
Eric Wild621a49e2023-01-12 16:21:58 +010041#include "Complex.h"
Ericb7253c62022-11-28 19:21:08 +010042#include "GSMCommon.h"
43#include "itrq.h"
Eric3e7f4b02023-05-23 12:50:25 +020044#include "threadpool.h"
Eric989fe752023-10-06 16:06:13 +020045#include "threadsched.h"
Ericb7253c62022-11-28 19:21:08 +010046
47const unsigned int ONE_TS_BURST_LEN = (3 + 58 + 26 + 58 + 3 + 8.25) * 4 /*sps*/;
Ericb7253c62022-11-28 19:21:08 +010048const unsigned int SCH_LEN_SPS = (ONE_TS_BURST_LEN * 8 /*ts*/ * 12 /*frames*/);
49
Eric Wild621a49e2023-01-12 16:21:58 +010050template <typename T>
51void clamp_array(T *start2, unsigned int len, T max)
Ericb7253c62022-11-28 19:21:08 +010052{
Eric58294fd2023-08-17 16:27:33 +020053 for (unsigned int i = 0; i < len; i++) {
Ericb7253c62022-11-28 19:21:08 +010054 const T t1 = start2[i] < -max ? -max : start2[i];
55 const T t2 = t1 > max ? max : t1;
56 start2[i] = t2;
57 }
58}
Eric Wild621a49e2023-01-12 16:21:58 +010059
60namespace cvt_internal
61{
62
63template <typename SRC_T, typename ST>
64void convert_and_scale_i(float *dst, const SRC_T *src, unsigned int src_len, ST scale)
Ericb7253c62022-11-28 19:21:08 +010065{
66 for (unsigned int i = 0; i < src_len; i++)
Eric Wild621a49e2023-01-12 16:21:58 +010067 dst[i] = static_cast<float>(src[i]) * scale;
Ericb7253c62022-11-28 19:21:08 +010068}
Eric Wild621a49e2023-01-12 16:21:58 +010069
70template <typename DST_T, typename ST>
71void convert_and_scale_i(DST_T *dst, const float *src, unsigned int src_len, ST scale)
Ericb7253c62022-11-28 19:21:08 +010072{
Eric Wild621a49e2023-01-12 16:21:58 +010073 for (unsigned int i = 0; i < src_len; i++)
74 dst[i] = static_cast<DST_T>(src[i] * scale);
75}
76
77template <typename ST>
78void convert_and_scale_i(float *dst, const float *src, unsigned int src_len, ST scale)
79{
80 for (unsigned int i = 0; i < src_len; i++)
81 dst[i] = src[i] * scale;
82}
83
84template <typename T>
85struct is_complex : std::false_type {
86 using baset = T;
Eric6a3e4b32023-04-26 22:12:06 +020087 static const unsigned int len_mul = 1;
Eric Wild621a49e2023-01-12 16:21:58 +010088};
89
90template <typename T>
91struct is_complex<std::complex<T>> : std::true_type {
92 using baset = typename std::complex<T>::value_type;
Eric6a3e4b32023-04-26 22:12:06 +020093 static const unsigned int len_mul = 2;
Eric Wild621a49e2023-01-12 16:21:58 +010094};
95
96template <typename T>
97struct is_complex<Complex<T>> : std::true_type {
98 using baset = typename Complex<T>::value_type;
Eric6a3e4b32023-04-26 22:12:06 +020099 static const unsigned int len_mul = 2;
Eric Wild621a49e2023-01-12 16:21:58 +0100100};
101
102} // namespace cvt_internal
103
104template <typename DST_T, typename SRC_T, typename ST>
105void convert_and_scale(DST_T *dst, const SRC_T *src, unsigned int src_len, ST scale)
106{
107 using vd = typename cvt_internal::is_complex<DST_T>::baset;
108 using vs = typename cvt_internal::is_complex<SRC_T>::baset;
109 return cvt_internal::convert_and_scale_i((vd *)dst, (vs *)src, src_len, scale);
Ericb7253c62022-11-28 19:21:08 +0100110}
111
Eric6a3e4b32023-04-26 22:12:06 +0200112template <typename array_t>
113float normed_abs_sum(array_t *src, int len)
114{
115 using vd = typename cvt_internal::is_complex<array_t>::baset;
116 auto len_mul = cvt_internal::is_complex<array_t>::len_mul;
117 auto ptr = reinterpret_cast<const vd *>(src);
118 float sum = 0;
119 for (unsigned int i = 0; i < len * len_mul; i++)
120 sum += std::abs(ptr[i]);
121 sum /= len * len_mul;
122 return sum;
123}
124
Ericb7253c62022-11-28 19:21:08 +0100125struct one_burst {
126 one_burst()
127 {
128 }
129 GSM::Time gsmts;
130 union {
131 blade_sample_type burst[ONE_TS_BURST_LEN];
132 char sch_bits[148];
133 };
134};
135
Eric992a49e2023-08-31 20:11:49 +0200136using rx_queue_t = spsc_cond_timeout<4, one_burst, true, false>;
Ericb7253c62022-11-28 19:21:08 +0100137
138enum class SCH_STATE { SEARCHING, FOUND };
139
140class dummylog : private std::streambuf {
141 std::ostream null_stream;
142
143 public:
144 dummylog() : null_stream(this){};
145 ~dummylog() override{};
146 std::ostream &operator()()
147 {
148 return null_stream;
149 }
150 int overflow(int c) override
151 {
152 return c;
153 }
154};
155
156// keeps relationship between gsm time and (continuously adjusted) ts
157class time_keeper {
158 GSM::Time global_time_keeper;
159 int64_t global_ts_keeper;
160 std::mutex m;
161
162 public:
163 time_keeper() : global_time_keeper(0), global_ts_keeper(0)
164 {
165 }
166
167 void set(GSM::Time t, int64_t ts)
168 {
169 std::lock_guard<std::mutex> g(m);
170 global_time_keeper = t;
171 global_ts_keeper = ts;
172 }
173 void inc_both()
174 {
175 std::lock_guard<std::mutex> g(m);
176 global_time_keeper.incTN(1);
177 global_ts_keeper += ONE_TS_BURST_LEN;
178 }
179 void inc_and_update(int64_t new_ts)
180 {
181 std::lock_guard<std::mutex> g(m);
182 global_time_keeper.incTN(1);
183 global_ts_keeper = new_ts;
Ericb7253c62022-11-28 19:21:08 +0100184 }
185 void inc_and_update_safe(int64_t new_ts)
186 {
187 std::lock_guard<std::mutex> g(m);
188 auto diff = new_ts - global_ts_keeper;
189 assert(diff < 1.5 * ONE_TS_BURST_LEN);
190 assert(diff > 0.5 * ONE_TS_BURST_LEN);
191 global_time_keeper.incTN(1);
192 global_ts_keeper = new_ts;
Ericb7253c62022-11-28 19:21:08 +0100193 }
194 void dec_by_one()
195 {
196 std::lock_guard<std::mutex> g(m);
197 global_time_keeper.decTN(1);
198 global_ts_keeper -= ONE_TS_BURST_LEN;
199 }
200 auto get_ts()
201 {
202 std::lock_guard<std::mutex> g(m);
203 return global_ts_keeper;
204 }
205 auto gsmtime()
206 {
207 std::lock_guard<std::mutex> g(m);
208 return global_time_keeper;
209 }
210 void get_both(GSM::Time *t, int64_t *ts)
211 {
212 std::lock_guard<std::mutex> g(m);
213 *t = global_time_keeper;
214 *ts = global_ts_keeper;
215 }
216};
217
Eric989fe752023-10-06 16:06:13 +0200218// used to globally initialize the sched/hw information
219struct sched_hw_info {
220 int hw_cpus;
221 sched_params::target hw_target;
222
223 sched_hw_info()
224 {
225 hw_cpus = std::thread::hardware_concurrency();
226 hw_target = hw_cpus > 4 ? sched_params::target::ODROID : sched_params::target::PI4;
227 set_sched_target(hw_target);
228 std::cerr << "scheduling for: " << (hw_cpus > 4 ? "odroid" : "pi4") << std::endl;
229 }
230};
231
232struct ms_trx : public BASET, public sched_hw_info {
Ericb7253c62022-11-28 19:21:08 +0100233 using base = BASET;
234 static dummylog dummy_log;
235 unsigned int mTSC;
236 unsigned int mBSIC;
237 int timing_advance;
Eric Wild238891f2024-03-20 19:39:05 +0100238 bool use_va;
Ericb7253c62022-11-28 19:21:08 +0100239
Eric989fe752023-10-06 16:06:13 +0200240 pthread_t lower_rx_task;
241 pthread_t lower_tx_task;
Ericb7253c62022-11-28 19:21:08 +0100242
243 // provides bursts to upper rx thread
244 rx_queue_t rxqueue;
Ericea7bd5f2023-05-02 15:21:45 +0200245
Ericb7253c62022-11-28 19:21:08 +0100246 blade_sample_type *first_sch_buf;
247 blade_sample_type *burst_copy_buffer;
248
249 uint64_t first_sch_buf_rcv_ts;
250 std::atomic<bool> rcv_done;
251 std::atomic<bool> sch_thread_done;
252
253 int64_t temp_ts_corr_offset = 0;
254 int64_t first_sch_ts_start = -1;
255
256 time_keeper timekeeper;
Eric989fe752023-10-06 16:06:13 +0200257 single_thread_pool worker_thread; // uses base class sched target hw info
Ericb7253c62022-11-28 19:21:08 +0100258
Ericc3e515a2023-05-08 12:56:56 +0200259 void start_lower_ms();
Ericb7253c62022-11-28 19:21:08 +0100260 std::atomic<bool> upper_is_ready;
261 void set_upper_ready(bool is_ready);
262
263 bool handle_sch_or_nb();
264 bool handle_sch(bool first = false);
Eric Wild2f40abd2023-05-22 22:50:43 +0200265 bool decode_sch(char *bits, bool update_global_clock);
Ericb7253c62022-11-28 19:21:08 +0100266 SCH_STATE search_for_sch(dev_buf_t *rcd);
267 void grab_bursts(dev_buf_t *rcd);
268
Ericb7253c62022-11-28 19:21:08 +0100269 int init_dev_and_streams();
270 void stop_threads();
271 void *rx_cb(ms_trx *t);
272 void *tx_cb();
273 void maybe_update_gain(one_burst &brst);
274
Eric Wild238891f2024-03-20 19:39:05 +0100275 ms_trx(struct mssdr_cfg *cfgdata)
276 : BASET(cfgdata), mTSC(0), mBSIC(0), timing_advance(0), use_va(cfgdata->use_va), rxqueue(),
Eric58294fd2023-08-17 16:27:33 +0200277 first_sch_buf(new blade_sample_type[SCH_LEN_SPS]),
278 burst_copy_buffer(new blade_sample_type[ONE_TS_BURST_LEN]), first_sch_buf_rcv_ts(0),
Eric989fe752023-10-06 16:06:13 +0200279 rcv_done{ false }, sch_thread_done{ false }, upper_is_ready(false)
Ericb7253c62022-11-28 19:21:08 +0100280 {
281 }
282
283 virtual ~ms_trx()
284 {
285 delete[] burst_copy_buffer;
286 delete[] first_sch_buf;
287 }
288 bh_fn_t rx_bh();
289 bh_fn_t tx_bh();
290
291 void submit_burst(blade_sample_type *buffer, int len, GSM::Time);
292 void set_ta(int val)
293 {
294 assert(val > -127 && val < 128);
295 timing_advance = val * 4;
296 }
Ericb7253c62022-11-28 19:21:08 +0100297};