blob: ca41144f4f1063511b43fae8e99d976e69446250 [file] [log] [blame]
Ericb7253c62022-11-28 19:21:08 +01001
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#include "GSMCommon.h"
23#include <atomic>
24#include <cassert>
25#include <complex>
26#include <iostream>
27#include <cstdlib>
28#include <cstdio>
29#include <thread>
30#include <fstream>
31
Ericb7253c62022-11-28 19:21:08 +010032#include "ms.h"
Ericb7253c62022-11-28 19:21:08 +010033
34extern "C" {
35#include "sch.h"
Ericb7253c62022-11-28 19:21:08 +010036}
37
38dummylog ms_trx::dummy_log;
39
40#ifdef DBGXX
41const int offsetrange = 200;
42const int offset_start = -15;
43static int offset_ctr = 0;
44#endif
45
Eric2e6c3622023-05-02 14:34:49 +020046template <>
Eric40978042023-05-23 10:59:58 +020047std::atomic<bool> ms_trx::base::stop_lower_threads_flag(false);
Ericb7253c62022-11-28 19:21:08 +010048
Ericb7253c62022-11-28 19:21:08 +010049int ms_trx::init_dev_and_streams()
50{
51 int status = 0;
Ericdf4520d2023-05-03 19:37:48 +020052 status = init_device(rx_bh(), tx_bh());
Ericb7253c62022-11-28 19:21:08 +010053 if (status < 0) {
54 std::cerr << "failed to init dev!" << std::endl;
55 return -1;
56 }
57 return status;
58}
59
60bh_fn_t ms_trx::rx_bh()
61{
62 return [this](dev_buf_t *rcd) -> int {
63 if (this->search_for_sch(rcd) == SCH_STATE::FOUND)
64 this->grab_bursts(rcd);
65 return 0;
66 };
67}
68
69bh_fn_t ms_trx::tx_bh()
70{
71 return [this](dev_buf_t *rcd) -> int {
72#pragma GCC diagnostic push
73#pragma GCC diagnostic ignored "-Wunused-variable"
74 auto y = this;
75#pragma GCC diagnostic pop
76 /* nothing to do here */
77 return 0;
78 };
79}
80
Ericc3e515a2023-05-08 12:56:56 +020081void ms_trx::start_lower_ms()
Ericb7253c62022-11-28 19:21:08 +010082{
Eric40978042023-05-23 10:59:58 +020083 if (stop_lower_threads_flag)
Ericb3157b92023-05-23 11:32:34 +020084 return;
Ericb7253c62022-11-28 19:21:08 +010085 auto fn = get_rx_burst_handler_fn(rx_bh());
Ericda5ffd62023-05-23 11:00:32 +020086 lower_rx_task = std::thread(fn);
87 set_name_aff_sched(lower_rx_task.native_handle(), sched_params::thread_names::RXRUN);
Ericb7253c62022-11-28 19:21:08 +010088
89 usleep(1000);
90 auto fn2 = get_tx_burst_handler_fn(tx_bh());
Ericda5ffd62023-05-23 11:00:32 +020091 lower_tx_task = std::thread(fn2);
92 set_name_aff_sched(lower_tx_task.native_handle(), sched_params::thread_names::TXRUN);
Eric805e0d92023-05-23 11:29:18 +020093
Ericb3157b92023-05-23 11:32:34 +020094 actually_enable_streams();
Ericb7253c62022-11-28 19:21:08 +010095}
96
97void ms_trx::set_upper_ready(bool is_ready)
98{
99 upper_is_ready = is_ready;
100}
101
102void ms_trx::stop_threads()
103{
Ericb3157b92023-05-23 11:32:34 +0200104 std::cerr << "killing threads..." << std::endl;
Eric40978042023-05-23 10:59:58 +0200105 stop_lower_threads_flag = true;
Ericb7253c62022-11-28 19:21:08 +0100106 close_device();
Ericb3157b92023-05-23 11:32:34 +0200107 std::cerr << "dev closed..." << std::endl;
Ericda5ffd62023-05-23 11:00:32 +0200108 lower_rx_task.join();
Ericb3157b92023-05-23 11:32:34 +0200109 std::cerr << "L rx dead..." << std::endl;
Ericda5ffd62023-05-23 11:00:32 +0200110 lower_tx_task.join();
Ericb3157b92023-05-23 11:32:34 +0200111 std::cerr << "L tx dead..." << std::endl;
Ericb7253c62022-11-28 19:21:08 +0100112}
113
114void ms_trx::submit_burst(blade_sample_type *buffer, int len, GSM::Time target)
115{
116 int64_t now_ts;
117 GSM::Time now_time;
118 target.incTN(3); // ul dl offset
119 int target_fn = target.FN();
120 int target_tn = target.TN();
121 timekeeper.get_both(&now_time, &now_ts);
122
123 auto diff_fn = GSM::FNDelta(target_fn, now_time.FN());
124 int diff_tn = (target_tn - (int)now_time.TN()) % 8;
125 auto tosend = GSM::Time(diff_fn, 0);
126
127 if (diff_tn > 0)
128 tosend.incTN(diff_tn);
129 else
130 tosend.decTN(-diff_tn);
131
132 // in theory fn equal and tn+3 equal is also a problem...
133 if (diff_fn < 0 || (diff_fn == 0 && (now_time.TN() - target_tn < 1))) {
134 std::cerr << "## TX too late?! fn DIFF:" << diff_fn << " tn LOCAL: " << now_time.TN()
135 << " tn OTHER: " << target_tn << std::endl;
136 return;
137 }
138
Ericb7253c62022-11-28 19:21:08 +0100139 int64_t send_ts = now_ts + tosend.FN() * 8 * ONE_TS_BURST_LEN + tosend.TN() * ONE_TS_BURST_LEN - timing_advance;
140#ifdef DBGXX
Ericf57a8612023-07-24 20:44:06 +0200141 auto check = now_time + tosend;
Ericb7253c62022-11-28 19:21:08 +0100142 std::cerr << "## fn DIFF: " << diff_fn << " ## tn DIFF: " << diff_tn << " tn LOCAL/OTHER: " << now_time.TN()
143 << "/" << target_tn << " tndiff" << diff_tn << " tosend:" << tosend.FN() << ":" << tosend.TN()
144 << " check: " << check.FN() << ":" << check.TN() << " target: " << target.FN() << ":" << target.TN()
145 << " ts now: " << now_ts << " target ts:" << send_ts << std::endl;
146#endif
Ericf57a8612023-07-24 20:44:06 +0200147#if 0
148 auto check = now_time + tosend;
Ericb7253c62022-11-28 19:21:08 +0100149 unsigned int pad = 4 * 4;
150 blade_sample_type buf2[len + pad];
151 std::fill(buf2, buf2 + pad, 0);
152 memcpy(&buf2[pad], buffer, len * sizeof(blade_sample_type));
153
154 assert(target.FN() == check.FN());
155 assert(target.TN() == check.TN());
156 submit_burst_ts(buf2, len + pad, send_ts - pad);
157#else
158 submit_burst_ts(buffer, len, send_ts);
159#endif
160}