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