Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 sysmocom - s.f.m.c. GmbH |
| 3 | * |
| 4 | This program is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Affero General Public License as published by |
| 6 | the Free Software Foundation, either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU Affero General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Affero General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <string.h> |
| 20 | #include <stdlib.h> |
| 21 | #include "Logger.h" |
| 22 | #include "Threads.h" |
| 23 | #include "LMSDevice.h" |
| 24 | |
| 25 | #include <lime/LimeSuite.h> |
| 26 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 27 | #include <osmocom/core/utils.h> |
| 28 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 29 | #ifdef HAVE_CONFIG_H |
| 30 | #include "config.h" |
| 31 | #endif |
| 32 | |
| 33 | using namespace std; |
| 34 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 35 | constexpr double LMSDevice::masterClockRate; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 36 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 37 | #define MAX_ANTENNA_LIST_SIZE 10 |
| 38 | #define LMS_SAMPLE_RATE GSMRATE*32 |
| 39 | #define GSM_CARRIER_BW 270000.0 /* 270kHz */ |
| 40 | #define LMS_MIN_BW_SUPPORTED 2.5e6 /* 2.5mHz, minimum supported by LMS */ |
| 41 | #define LMS_CALIBRATE_BW_HZ OSMO_MAX(GSM_CARRIER_BW, LMS_MIN_BW_SUPPORTED) |
| 42 | |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 43 | LMSDevice::LMSDevice(size_t tx_sps, size_t rx_sps, InterfaceType iface, size_t chans, double lo_offset, |
Harald Welte | 105a61e | 2018-06-13 21:55:09 +0200 | [diff] [blame] | 44 | const std::vector<std::string>& tx_paths, |
| 45 | const std::vector<std::string>& rx_paths): |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 46 | RadioDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths), |
| 47 | m_lms_dev(NULL) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 48 | { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 49 | LOGC(DDEV, INFO) << "creating LMS device..."; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 50 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 51 | m_lms_stream_rx.resize(chans); |
| 52 | m_lms_stream_tx.resize(chans); |
| 53 | |
| 54 | m_last_rx_underruns.resize(chans, 0); |
| 55 | m_last_rx_overruns.resize(chans, 0); |
| 56 | m_last_tx_underruns.resize(chans, 0); |
| 57 | m_last_tx_overruns.resize(chans, 0); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void lms_log_callback(int lvl, const char *msg) |
| 61 | { |
| 62 | /* map lime specific log levels */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 63 | static const int lvl_map[5] = { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 64 | [0] = LOGL_FATAL, |
Pau Espin Pedrol | 32b3c2e | 2018-11-23 14:39:06 +0100 | [diff] [blame] | 65 | [LMS_LOG_ERROR] = LOGL_ERROR, |
| 66 | [LMS_LOG_WARNING] = LOGL_NOTICE, |
| 67 | [LMS_LOG_INFO] = LOGL_INFO, |
| 68 | [LMS_LOG_DEBUG] = LOGL_DEBUG, |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 69 | }; |
| 70 | /* protect against future higher log level values (lower importance) */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 71 | if ((unsigned int) lvl >= ARRAY_SIZE(lvl_map)) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 72 | lvl = ARRAY_SIZE(lvl_map)-1; |
| 73 | |
Pau Espin Pedrol | 1e2c010 | 2018-11-23 14:39:51 +0100 | [diff] [blame] | 74 | LOGLV(DLMS, lvl_map[lvl]) << msg; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 77 | static void thread_enable_cancel(bool cancel) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 78 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 79 | cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) : |
| 80 | pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); |
| 81 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 82 | |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 83 | static void print_range(const char* name, lms_range_t *range) |
| 84 | { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 85 | LOGC(DDEV, INFO) << name << ": Min=" << range->min << " Max=" << range->max |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 86 | << " Step=" << range->step; |
| 87 | } |
| 88 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 89 | int LMSDevice::open(const std::string &args, int ref, bool swap_channels) |
| 90 | { |
| 91 | //lms_info_str_t dev_str; |
| 92 | lms_info_str_t* info_list; |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 93 | lms_range_t range_lpfbw_rx, range_lpfbw_tx, range_sr; |
| 94 | float_type sr_host, sr_rf, lpfbw_rx, lpfbw_tx; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 95 | uint16_t dac_val; |
| 96 | unsigned int i, n; |
| 97 | int rc; |
| 98 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 99 | LOGC(DDEV, INFO) << "Opening LMS device.."; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 100 | |
| 101 | LMS_RegisterLogHandler(&lms_log_callback); |
| 102 | |
Harald Welte | 62b7900 | 2018-06-13 23:32:42 +0200 | [diff] [blame] | 103 | if ((n = LMS_GetDeviceList(NULL)) < 0) |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 104 | LOGC(DDEV, ERROR) << "LMS_GetDeviceList(NULL) failed"; |
| 105 | LOGC(DDEV, INFO) << "Devices found: " << n; |
Harald Welte | 62b7900 | 2018-06-13 23:32:42 +0200 | [diff] [blame] | 106 | if (n < 1) |
| 107 | return -1; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 108 | |
Harald Welte | 62b7900 | 2018-06-13 23:32:42 +0200 | [diff] [blame] | 109 | info_list = new lms_info_str_t[n]; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 110 | |
Harald Welte | 62b7900 | 2018-06-13 23:32:42 +0200 | [diff] [blame] | 111 | if (LMS_GetDeviceList(info_list) < 0) |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 112 | LOGC(DDEV, ERROR) << "LMS_GetDeviceList(info_list) failed"; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 113 | |
Harald Welte | 62b7900 | 2018-06-13 23:32:42 +0200 | [diff] [blame] | 114 | for (i = 0; i < n; i++) |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 115 | LOGC(DDEV, INFO) << "Device [" << i << "]: " << info_list[i]; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 116 | |
| 117 | rc = LMS_Open(&m_lms_dev, info_list[0], NULL); |
| 118 | if (rc != 0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 119 | LOGC(DDEV, ERROR) << "LMS_GetDeviceList() failed)"; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 120 | delete [] info_list; |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | delete [] info_list; |
| 125 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 126 | LOGC(DDEV, INFO) << "Init LMS device"; |
Harald Welte | 9cb4f27 | 2018-04-28 21:38:58 +0200 | [diff] [blame] | 127 | if (LMS_Init(m_lms_dev) != 0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 128 | LOGC(DDEV, ERROR) << "LMS_Init() failed"; |
Harald Welte | 9cb4f27 | 2018-04-28 21:38:58 +0200 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 132 | if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range_sr)) |
Harald Welte | a438114 | 2018-04-28 21:41:07 +0200 | [diff] [blame] | 133 | goto out_close; |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 134 | print_range("Sample Rate", &range_sr); |
Harald Welte | a438114 | 2018-04-28 21:41:07 +0200 | [diff] [blame] | 135 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 136 | LOGC(DDEV, INFO) << "Setting sample rate to " << GSMRATE*tx_sps << " " << tx_sps; |
Harald Welte | ce70ba5 | 2018-06-13 22:47:48 +0200 | [diff] [blame] | 137 | if (LMS_SetSampleRate(m_lms_dev, GSMRATE*tx_sps, 32) < 0) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 138 | goto out_close; |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 139 | |
Harald Welte | a438114 | 2018-04-28 21:41:07 +0200 | [diff] [blame] | 140 | if (LMS_GetSampleRate(m_lms_dev, LMS_CH_RX, 0, &sr_host, &sr_rf)) |
| 141 | goto out_close; |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 142 | LOGC(DDEV, INFO) << "Sample Rate: Host=" << sr_host << " RF=" << sr_rf; |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 143 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 144 | /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */ |
Harald Welte | ce70ba5 | 2018-06-13 22:47:48 +0200 | [diff] [blame] | 145 | ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE * tx_sps); /* time * sample_rate */ |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 146 | |
| 147 | switch (ref) { |
| 148 | case REF_INTERNAL: |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 149 | LOGC(DDEV, INFO) << "Setting Internal clock reference"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 150 | /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */ |
| 151 | if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0) |
| 152 | goto out_close; |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 153 | LOGC(DDEV, INFO) << "Setting VCTCXO to " << dac_val; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 154 | if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0) |
| 155 | goto out_close; |
| 156 | break; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 157 | case REF_EXTERNAL: |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 158 | LOGC(DDEV, INFO) << "Setting External clock reference to " << 10000000.0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 159 | /* Assume an external 10 MHz reference clock */ |
| 160 | if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0) |
| 161 | goto out_close; |
| 162 | break; |
| 163 | default: |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 164 | LOGC(DDEV, ALERT) << "Invalid reference type"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 165 | goto out_close; |
| 166 | } |
| 167 | |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 168 | if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_rx)) |
| 169 | goto out_close; |
| 170 | print_range("LPFBWRange Rx", &range_lpfbw_rx); |
| 171 | if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_tx)) |
| 172 | goto out_close; |
| 173 | print_range("LPFBWRange Tx", &range_lpfbw_tx); |
| 174 | lpfbw_rx = OSMO_MIN(OSMO_MAX(1.4001e6, range_lpfbw_rx.min), range_lpfbw_rx.max); |
| 175 | lpfbw_tx = OSMO_MIN(OSMO_MAX(5.2e6, range_lpfbw_tx.min), range_lpfbw_tx.max); |
| 176 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 177 | LOGC(DDEV, INFO) << "LPFBW: Rx=" << lpfbw_rx << " Tx=" << lpfbw_tx; |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 178 | |
Harald Welte | cfb9dac | 2018-06-13 21:56:24 +0200 | [diff] [blame] | 179 | if (!set_antennas()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 180 | LOGC(DDEV, ALERT) << "LMS antenna setting failed"; |
Harald Welte | cfb9dac | 2018-06-13 21:56:24 +0200 | [diff] [blame] | 181 | return -1; |
| 182 | } |
| 183 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 184 | /* Perform Rx and Tx calibration */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 185 | for (i=0; i<chans; i++) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 186 | LOGC(DDEV, INFO) << "Setting LPFBW chan " << i; |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 187 | if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, i, lpfbw_rx) < 0) |
Harald Welte | ea8be22 | 2018-04-28 21:52:57 +0200 | [diff] [blame] | 188 | goto out_close; |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 189 | if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, lpfbw_tx) < 0) |
Harald Welte | ea8be22 | 2018-04-28 21:52:57 +0200 | [diff] [blame] | 190 | goto out_close; |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 191 | LOGC(DDEV, INFO) << "Calibrating chan " << i; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 192 | if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0) |
| 193 | goto out_close; |
| 194 | if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0) |
| 195 | goto out_close; |
| 196 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 197 | |
| 198 | samplesRead = 0; |
| 199 | samplesWritten = 0; |
| 200 | started = false; |
| 201 | |
| 202 | return NORMAL; |
| 203 | |
| 204 | out_close: |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 205 | LOGC(DDEV, ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage(); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 206 | LMS_Close(m_lms_dev); |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | bool LMSDevice::start() |
| 211 | { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 212 | LOGC(DDEV, INFO) << "starting LMS..."; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 213 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 214 | unsigned int i; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 215 | |
Pau Espin Pedrol | 69869bd | 2018-12-03 11:19:52 +0100 | [diff] [blame^] | 216 | if (started) { |
| 217 | LOGC(DDEV, ERR) << "Device already started"; |
| 218 | return false; |
| 219 | } |
| 220 | |
Zydrunas Tamosevicius | 0494d05 | 2018-06-12 00:29:16 +0200 | [diff] [blame] | 221 | /* configure the channels/streams */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 222 | for (i=0; i<chans; i++) { |
| 223 | if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0) |
| 224 | return false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 225 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 226 | if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0) |
| 227 | return false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 228 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 229 | // Set gains to midpoint |
| 230 | setTxGain((minTxGain() + maxTxGain()) / 2, i); |
Harald Welte | 55928f2 | 2018-11-26 19:26:52 +0100 | [diff] [blame] | 231 | setRxGain((minRxGain() + maxRxGain()) / 2, i); |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 232 | |
| 233 | m_lms_stream_rx[i] = {}; |
| 234 | m_lms_stream_rx[i].isTx = false; |
| 235 | m_lms_stream_rx[i].channel = i; |
| 236 | m_lms_stream_rx[i].fifoSize = 1024 * 1024; |
| 237 | m_lms_stream_rx[i].throughputVsLatency = 0.3; |
| 238 | m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16; |
| 239 | |
| 240 | m_lms_stream_tx[i] = {}; |
| 241 | m_lms_stream_tx[i].isTx = true; |
| 242 | m_lms_stream_tx[i].channel = i; |
| 243 | m_lms_stream_tx[i].fifoSize = 1024 * 1024; |
| 244 | m_lms_stream_tx[i].throughputVsLatency = 0.3; |
| 245 | m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16; |
| 246 | |
| 247 | if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0) |
| 248 | return false; |
| 249 | |
| 250 | if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0) |
| 251 | return false; |
Zydrunas Tamosevicius | 0494d05 | 2018-06-12 00:29:16 +0200 | [diff] [blame] | 252 | } |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 253 | |
Zydrunas Tamosevicius | 0494d05 | 2018-06-12 00:29:16 +0200 | [diff] [blame] | 254 | /* now start the streams in a second loop, as we can no longer call |
| 255 | * LMS_SetupStream() after LMS_StartStream() of the first stream */ |
| 256 | for (i = 0; i < chans; i++) { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 257 | if (LMS_StartStream(&m_lms_stream_rx[i]) < 0) |
| 258 | return false; |
| 259 | |
| 260 | if (LMS_StartStream(&m_lms_stream_tx[i]) < 0) |
| 261 | return false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 264 | flush_recv(10); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 265 | |
| 266 | started = true; |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | bool LMSDevice::stop() |
| 271 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 272 | unsigned int i; |
| 273 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 274 | if (!started) |
| 275 | return true; |
| 276 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 277 | for (i=0; i<chans; i++) { |
| 278 | LMS_StopStream(&m_lms_stream_tx[i]); |
| 279 | LMS_StopStream(&m_lms_stream_rx[i]); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 280 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 281 | LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false); |
| 282 | LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false); |
| 283 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 284 | |
Pau Espin Pedrol | 69869bd | 2018-12-03 11:19:52 +0100 | [diff] [blame^] | 285 | started = false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 286 | return true; |
| 287 | } |
| 288 | |
| 289 | double LMSDevice::maxTxGain() |
| 290 | { |
Pau Espin Pedrol | 587916e | 2018-05-08 18:46:28 +0200 | [diff] [blame] | 291 | return 73.0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | double LMSDevice::minTxGain() |
| 295 | { |
| 296 | return 0.0; |
| 297 | } |
| 298 | |
| 299 | double LMSDevice::maxRxGain() |
| 300 | { |
Pau Espin Pedrol | 587916e | 2018-05-08 18:46:28 +0200 | [diff] [blame] | 301 | return 73.0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | double LMSDevice::minRxGain() |
| 305 | { |
| 306 | return 0.0; |
| 307 | } |
| 308 | |
| 309 | double LMSDevice::setTxGain(double dB, size_t chan) |
| 310 | { |
| 311 | if (chan) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 312 | LOGC(DDEV, ALERT) << "Invalid channel " << chan; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 313 | return 0.0; |
| 314 | } |
| 315 | |
| 316 | if (dB > maxTxGain()) |
| 317 | dB = maxTxGain(); |
| 318 | if (dB < minTxGain()) |
| 319 | dB = minTxGain(); |
| 320 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 321 | LOGC(DDEV, NOTICE) << "Setting TX gain to " << dB << " dB."; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 322 | |
| 323 | if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0) |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 324 | LOGC(DDEV, ERR) << "Error setting TX gain"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 325 | |
| 326 | return dB; |
| 327 | } |
| 328 | |
| 329 | double LMSDevice::setRxGain(double dB, size_t chan) |
| 330 | { |
| 331 | if (chan) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 332 | LOGC(DDEV, ALERT) << "Invalid channel " << chan; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 333 | return 0.0; |
| 334 | } |
| 335 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 336 | if (dB > maxRxGain()) |
| 337 | dB = maxRxGain(); |
| 338 | if (dB < minRxGain()) |
| 339 | dB = minRxGain(); |
| 340 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 341 | LOGC(DDEV, NOTICE) << "Setting RX gain to " << dB << " dB."; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 342 | |
| 343 | if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0) |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 344 | LOGC(DDEV, ERR) << "Error setting RX gain"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 345 | |
| 346 | return dB; |
| 347 | } |
| 348 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 349 | int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 350 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 351 | lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */ |
| 352 | const char* c_name = name.c_str(); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 353 | int num_names; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 354 | int i; |
| 355 | |
| 356 | num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 357 | for (i = 0; i < num_names; i++) { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 358 | if (!strcmp(c_name, name_list[i])) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 359 | return i; |
| 360 | } |
| 361 | return -1; |
| 362 | } |
| 363 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 364 | bool LMSDevice::flush_recv(size_t num_pkts) |
| 365 | { |
| 366 | #define CHUNK 625 |
Harald Welte | ce70ba5 | 2018-06-13 22:47:48 +0200 | [diff] [blame] | 367 | int len = CHUNK * tx_sps; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 368 | short *buffer = new short[len * 2]; |
| 369 | int rc; |
| 370 | lms_stream_meta_t rx_metadata = {}; |
| 371 | rx_metadata.flushPartialPacket = false; |
| 372 | rx_metadata.waitForTimestamp = false; |
| 373 | |
| 374 | ts_initial = 0; |
| 375 | |
| 376 | while (!ts_initial || (num_pkts-- > 0)) { |
| 377 | rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100); |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 378 | LOGC(DDEV, DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 379 | if (rc != len) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 380 | LOGC(DDEV, ALERT) << "LMS: Device receive timed out"; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 381 | delete[] buffer; |
| 382 | return false; |
| 383 | } |
| 384 | |
Harald Welte | 68f0541 | 2018-04-28 21:58:03 +0200 | [diff] [blame] | 385 | ts_initial = rx_metadata.timestamp + len; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 386 | } |
| 387 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 388 | LOGC(DDEV, INFO) << "Initial timestamp " << ts_initial << std::endl; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 389 | delete[] buffer; |
| 390 | return true; |
| 391 | } |
| 392 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 393 | bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan) |
| 394 | { |
| 395 | int idx; |
| 396 | |
| 397 | if (chan >= rx_paths.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 398 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 402 | idx = get_ant_idx(ant, LMS_CH_RX, chan); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 403 | if (idx < 0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 404 | LOGC(DDEV, ALERT) << "Invalid Rx Antenna"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 405 | return false; |
| 406 | } |
| 407 | |
| 408 | if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 409 | LOGC(DDEV, ALERT) << "Unable to set Rx Antenna"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | std::string LMSDevice::getRxAntenna(size_t chan) |
| 416 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 417 | lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */ |
| 418 | int idx; |
| 419 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 420 | if (chan >= rx_paths.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 421 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 422 | return ""; |
| 423 | } |
| 424 | |
| 425 | idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan); |
| 426 | if (idx < 0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 427 | LOGC(DDEV, ALERT) << "Error getting Rx Antenna"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 428 | return ""; |
| 429 | } |
| 430 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 431 | if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 432 | LOGC(DDEV, ALERT) << "Error getting Rx Antenna List"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 433 | return ""; |
| 434 | } |
| 435 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 436 | return name_list[idx]; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan) |
| 440 | { |
| 441 | int idx; |
| 442 | |
| 443 | if (chan >= tx_paths.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 444 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 445 | return false; |
| 446 | } |
| 447 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 448 | idx = get_ant_idx(ant, LMS_CH_TX, chan); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 449 | if (idx < 0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 450 | LOGC(DDEV, ALERT) << "Invalid Rx Antenna"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 451 | return false; |
| 452 | } |
| 453 | |
| 454 | if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 455 | LOGC(DDEV, ALERT) << "Unable to set Rx Antenna"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | std::string LMSDevice::getTxAntenna(size_t chan) |
| 462 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 463 | lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */ |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 464 | int idx; |
| 465 | |
| 466 | if (chan >= tx_paths.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 467 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 468 | return ""; |
| 469 | } |
| 470 | |
| 471 | idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan); |
| 472 | if (idx < 0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 473 | LOGC(DDEV, ALERT) << "Error getting Tx Antenna"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 474 | return ""; |
| 475 | } |
| 476 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 477 | if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 478 | LOGC(DDEV, ALERT) << "Error getting Tx Antenna List"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 479 | return ""; |
| 480 | } |
| 481 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 482 | return name_list[idx]; |
| 483 | } |
| 484 | |
| 485 | bool LMSDevice::requiresRadioAlign() |
| 486 | { |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | GSM::Time LMSDevice::minLatency() { |
| 491 | /* Empirical data from a handful of |
| 492 | relatively recent machines shows that the B100 will underrun when |
| 493 | the transmit threshold is reduced to a time of 6 and a half frames, |
| 494 | so we set a minimum 7 frame threshold. */ |
| 495 | return GSM::Time(6,7); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // NOTE: Assumes sequential reads |
| 499 | int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun, |
| 500 | TIMESTAMP timestamp, bool * underrun, unsigned *RSSI) |
| 501 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 502 | int rc = 0; |
| 503 | unsigned int i; |
| 504 | lms_stream_status_t status; |
| 505 | lms_stream_meta_t rx_metadata = {}; |
| 506 | rx_metadata.flushPartialPacket = false; |
| 507 | rx_metadata.waitForTimestamp = false; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 508 | rx_metadata.timestamp = 0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 509 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 510 | if (bufs.size() != chans) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 511 | LOGC(DDEV, ALERT) << "Invalid channel combination " << bufs.size(); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 512 | return -1; |
| 513 | } |
| 514 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 515 | *overrun = false; |
| 516 | *underrun = false; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 517 | for (i = 0; i<chans; i++) { |
| 518 | thread_enable_cancel(false); |
| 519 | rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100); |
Pau Espin Pedrol | 49ad759 | 2018-09-03 16:46:34 +0200 | [diff] [blame] | 520 | if (rc != len) { |
| 521 | LOGC(DDEV, ALERT) << "LMS: Device receive timed out (" << rc << " vs exp " << len << ")."; |
| 522 | thread_enable_cancel(true); |
| 523 | return -1; |
| 524 | } |
Harald Welte | 7902486 | 2018-04-28 22:13:31 +0200 | [diff] [blame] | 525 | if (timestamp != (TIMESTAMP)rx_metadata.timestamp) |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 526 | LOGC(DDEV, ALERT) << "chan "<< i << " recv buffer of len " << rc << " expect " << std::hex << timestamp << " got " << std::hex << (TIMESTAMP)rx_metadata.timestamp << " (" << std::hex << rx_metadata.timestamp <<") diff=" << rx_metadata.timestamp - timestamp; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 527 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 528 | if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) { |
| 529 | if (status.underrun > m_last_rx_underruns[i]) |
| 530 | *underrun = true; |
| 531 | m_last_rx_underruns[i] = status.underrun; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 532 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 533 | if (status.overrun > m_last_rx_overruns[i]) |
| 534 | *overrun = true; |
| 535 | m_last_rx_overruns[i] = status.overrun; |
| 536 | } |
| 537 | thread_enable_cancel(true); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | samplesRead += rc; |
| 541 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 542 | if (((TIMESTAMP) rx_metadata.timestamp) < timestamp) |
| 543 | rc = 0; |
| 544 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 545 | return rc; |
| 546 | } |
| 547 | |
| 548 | int LMSDevice::writeSamples(std::vector < short *>&bufs, int len, |
| 549 | bool * underrun, unsigned long long timestamp, |
| 550 | bool isControl) |
| 551 | { |
Vadim Yanitskiy | 92fc186 | 2018-09-20 16:17:16 +0700 | [diff] [blame] | 552 | int rc = 0; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 553 | unsigned int i; |
| 554 | lms_stream_status_t status; |
| 555 | lms_stream_meta_t tx_metadata = {}; |
| 556 | tx_metadata.flushPartialPacket = false; |
| 557 | tx_metadata.waitForTimestamp = true; |
Zydrunas Tamosevicius | 43f3678 | 2018-06-12 00:27:09 +0200 | [diff] [blame] | 558 | tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */ |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 559 | |
| 560 | if (isControl) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 561 | LOGC(DDEV, ERR) << "Control packets not supported"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 562 | return 0; |
| 563 | } |
| 564 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 565 | if (bufs.size() != chans) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 566 | LOGC(DDEV, ALERT) << "Invalid channel combination " << bufs.size(); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 567 | return -1; |
| 568 | } |
| 569 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 570 | *underrun = false; |
| 571 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 572 | for (i = 0; i<chans; i++) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 573 | LOGC(DDEV, DEBUG) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 574 | thread_enable_cancel(false); |
| 575 | rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100); |
| 576 | if (rc != len) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 577 | LOGC(DDEV, ALERT) << "LMS: Device send timed out"; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) { |
| 581 | if (status.underrun > m_last_tx_underruns[i]) |
| 582 | *underrun = true; |
| 583 | m_last_tx_underruns[i] = status.underrun; |
| 584 | } |
| 585 | thread_enable_cancel(true); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | samplesWritten += rc; |
| 589 | |
| 590 | return rc; |
| 591 | } |
| 592 | |
| 593 | bool LMSDevice::updateAlignment(TIMESTAMP timestamp) |
| 594 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 595 | return true; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | bool LMSDevice::setTxFreq(double wFreq, size_t chan) |
| 599 | { |
| 600 | |
| 601 | if (chan) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 602 | LOGC(DDEV, ALERT) << "Invalid channel " << chan; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 603 | return false; |
| 604 | } |
| 605 | |
| 606 | if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 607 | LOGC(DDEV, ALERT) << "set Tx: " << wFreq << " failed!"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 608 | return false; |
| 609 | } |
| 610 | |
| 611 | return true; |
| 612 | } |
| 613 | |
| 614 | bool LMSDevice::setRxFreq(double wFreq, size_t chan) |
| 615 | { |
| 616 | if (chan) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 617 | LOGC(DDEV, ALERT) << "Invalid channel " << chan; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 618 | return false; |
| 619 | } |
| 620 | |
| 621 | if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 622 | LOGC(DDEV, ALERT) << "set Rx: " << wFreq << " failed!"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 623 | return false; |
| 624 | } |
| 625 | |
| 626 | return true; |
| 627 | } |
| 628 | |
| 629 | RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps, |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 630 | InterfaceType iface, size_t chans, double lo_offset, |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 631 | const std::vector < std::string > &tx_paths, |
| 632 | const std::vector < std::string > &rx_paths) |
| 633 | { |
Harald Welte | ffb3301 | 2018-06-13 23:41:35 +0200 | [diff] [blame] | 634 | if (tx_sps != rx_sps) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 635 | LOGC(DDEV, ERROR) << "LMS Requires tx_sps == rx_sps"; |
Harald Welte | ffb3301 | 2018-06-13 23:41:35 +0200 | [diff] [blame] | 636 | return NULL; |
| 637 | } |
| 638 | if (lo_offset != 0.0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 639 | LOGC(DDEV, ERROR) << "LMS doesn't support lo_offset"; |
Harald Welte | ffb3301 | 2018-06-13 23:41:35 +0200 | [diff] [blame] | 640 | return NULL; |
| 641 | } |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 642 | return new LMSDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 643 | } |