Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 1 | |
| 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 | |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 32 | #include "ms.h" |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 33 | |
| 34 | extern "C" { |
| 35 | #include "sch.h" |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | dummylog ms_trx::dummy_log; |
| 39 | |
| 40 | #ifdef DBGXX |
| 41 | const int offsetrange = 200; |
| 42 | const int offset_start = -15; |
| 43 | static int offset_ctr = 0; |
| 44 | #endif |
| 45 | |
Eric | 2e6c362 | 2023-05-02 14:34:49 +0200 | [diff] [blame] | 46 | template <> |
Eric | 4097804 | 2023-05-23 10:59:58 +0200 | [diff] [blame] | 47 | std::atomic<bool> ms_trx::base::stop_lower_threads_flag(false); |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 48 | |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 49 | int ms_trx::init_dev_and_streams() |
| 50 | { |
| 51 | int status = 0; |
Eric | df4520d | 2023-05-03 19:37:48 +0200 | [diff] [blame] | 52 | status = init_device(rx_bh(), tx_bh()); |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 53 | if (status < 0) { |
| 54 | std::cerr << "failed to init dev!" << std::endl; |
| 55 | return -1; |
| 56 | } |
| 57 | return status; |
| 58 | } |
| 59 | |
| 60 | bh_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 | |
| 69 | bh_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 | |
Eric | c3e515a | 2023-05-08 12:56:56 +0200 | [diff] [blame] | 81 | void ms_trx::start_lower_ms() |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 82 | { |
Eric | 4097804 | 2023-05-23 10:59:58 +0200 | [diff] [blame] | 83 | if (stop_lower_threads_flag) |
Eric | b3157b9 | 2023-05-23 11:32:34 +0200 | [diff] [blame] | 84 | return; |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 85 | auto fn = get_rx_burst_handler_fn(rx_bh()); |
Eric | da5ffd6 | 2023-05-23 11:00:32 +0200 | [diff] [blame] | 86 | lower_rx_task = std::thread(fn); |
| 87 | set_name_aff_sched(lower_rx_task.native_handle(), sched_params::thread_names::RXRUN); |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 88 | |
| 89 | usleep(1000); |
| 90 | auto fn2 = get_tx_burst_handler_fn(tx_bh()); |
Eric | da5ffd6 | 2023-05-23 11:00:32 +0200 | [diff] [blame] | 91 | lower_tx_task = std::thread(fn2); |
| 92 | set_name_aff_sched(lower_tx_task.native_handle(), sched_params::thread_names::TXRUN); |
Eric | 805e0d9 | 2023-05-23 11:29:18 +0200 | [diff] [blame] | 93 | |
Eric | b3157b9 | 2023-05-23 11:32:34 +0200 | [diff] [blame] | 94 | actually_enable_streams(); |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void ms_trx::set_upper_ready(bool is_ready) |
| 98 | { |
| 99 | upper_is_ready = is_ready; |
| 100 | } |
| 101 | |
| 102 | void ms_trx::stop_threads() |
| 103 | { |
Eric | b3157b9 | 2023-05-23 11:32:34 +0200 | [diff] [blame] | 104 | std::cerr << "killing threads..." << std::endl; |
Eric | 4097804 | 2023-05-23 10:59:58 +0200 | [diff] [blame] | 105 | stop_lower_threads_flag = true; |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 106 | close_device(); |
Eric | b3157b9 | 2023-05-23 11:32:34 +0200 | [diff] [blame] | 107 | std::cerr << "dev closed..." << std::endl; |
Eric | da5ffd6 | 2023-05-23 11:00:32 +0200 | [diff] [blame] | 108 | lower_rx_task.join(); |
Eric | b3157b9 | 2023-05-23 11:32:34 +0200 | [diff] [blame] | 109 | std::cerr << "L rx dead..." << std::endl; |
Eric | da5ffd6 | 2023-05-23 11:00:32 +0200 | [diff] [blame] | 110 | lower_tx_task.join(); |
Eric | b3157b9 | 2023-05-23 11:32:34 +0200 | [diff] [blame] | 111 | std::cerr << "L tx dead..." << std::endl; |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void 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 | |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 139 | int64_t send_ts = now_ts + tosend.FN() * 8 * ONE_TS_BURST_LEN + tosend.TN() * ONE_TS_BURST_LEN - timing_advance; |
| 140 | #ifdef DBGXX |
Eric | f57a861 | 2023-07-24 20:44:06 +0200 | [diff] [blame^] | 141 | auto check = now_time + tosend; |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 142 | 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 |
Eric | f57a861 | 2023-07-24 20:44:06 +0200 | [diff] [blame^] | 147 | #if 0 |
| 148 | auto check = now_time + tosend; |
Eric | b7253c6 | 2022-11-28 19:21:08 +0100 | [diff] [blame] | 149 | 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 | } |