blob: 5e21894a3eaae0807781c40b4f8501fba8c471aa [file] [log] [blame]
Harald Welte940738e2018-03-07 07:50:57 +01001/*
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 Pedrolc7a0bf12018-04-25 12:17:10 +020027#include <osmocom/core/utils.h>
28
Harald Welte940738e2018-03-07 07:50:57 +010029#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33using namespace std;
34
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020035constexpr double LMSDevice::masterClockRate;
Harald Welte940738e2018-03-07 07:50:57 +010036
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020037#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 Welte61707e82018-06-13 23:21:57 +020043LMSDevice::LMSDevice(size_t tx_sps, size_t rx_sps, InterfaceType iface, size_t chans, double lo_offset,
Harald Welte105a61e2018-06-13 21:55:09 +020044 const std::vector<std::string>& tx_paths,
45 const std::vector<std::string>& rx_paths):
Harald Welte61707e82018-06-13 23:21:57 +020046 RadioDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths),
47 m_lms_dev(NULL)
Harald Welte940738e2018-03-07 07:50:57 +010048{
Harald Welte5cc88582018-08-17 19:55:38 +020049 LOGC(DDEV, INFO) << "creating LMS device...";
Harald Welte940738e2018-03-07 07:50:57 +010050
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020051 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 Welte940738e2018-03-07 07:50:57 +010058}
59
Pau Espin Pedrol5cea18e2018-12-03 18:17:18 +010060LMSDevice::~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 Welte940738e2018-03-07 07:50:57 +010069static void lms_log_callback(int lvl, const char *msg)
70{
71 /* map lime specific log levels */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020072 static const int lvl_map[5] = {
Harald Welte940738e2018-03-07 07:50:57 +010073 [0] = LOGL_FATAL,
Pau Espin Pedrol32b3c2e2018-11-23 14:39:06 +010074 [LMS_LOG_ERROR] = LOGL_ERROR,
75 [LMS_LOG_WARNING] = LOGL_NOTICE,
76 [LMS_LOG_INFO] = LOGL_INFO,
77 [LMS_LOG_DEBUG] = LOGL_DEBUG,
Harald Welte940738e2018-03-07 07:50:57 +010078 };
79 /* protect against future higher log level values (lower importance) */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020080 if ((unsigned int) lvl >= ARRAY_SIZE(lvl_map))
Harald Welte940738e2018-03-07 07:50:57 +010081 lvl = ARRAY_SIZE(lvl_map)-1;
82
Pau Espin Pedrol1e2c0102018-11-23 14:39:51 +010083 LOGLV(DLMS, lvl_map[lvl]) << msg;
Harald Welte940738e2018-03-07 07:50:57 +010084}
85
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020086static void thread_enable_cancel(bool cancel)
Harald Welte940738e2018-03-07 07:50:57 +010087{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020088 cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) :
89 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
90}
Harald Welte940738e2018-03-07 07:50:57 +010091
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020092static void print_range(const char* name, lms_range_t *range)
93{
Harald Welte5cc88582018-08-17 19:55:38 +020094 LOGC(DDEV, INFO) << name << ": Min=" << range->min << " Max=" << range->max
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020095 << " Step=" << range->step;
96}
97
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020098int 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 Pedrol6493dc82018-05-29 19:00:30 +0200102 lms_range_t range_lpfbw_rx, range_lpfbw_tx, range_sr;
103 float_type sr_host, sr_rf, lpfbw_rx, lpfbw_tx;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200104 uint16_t dac_val;
105 unsigned int i, n;
106 int rc;
107
Harald Welte5cc88582018-08-17 19:55:38 +0200108 LOGC(DDEV, INFO) << "Opening LMS device..";
Harald Welte940738e2018-03-07 07:50:57 +0100109
110 LMS_RegisterLogHandler(&lms_log_callback);
111
Harald Welte62b79002018-06-13 23:32:42 +0200112 if ((n = LMS_GetDeviceList(NULL)) < 0)
Harald Welte5cc88582018-08-17 19:55:38 +0200113 LOGC(DDEV, ERROR) << "LMS_GetDeviceList(NULL) failed";
114 LOGC(DDEV, INFO) << "Devices found: " << n;
Harald Welte62b79002018-06-13 23:32:42 +0200115 if (n < 1)
116 return -1;
Harald Welte940738e2018-03-07 07:50:57 +0100117
Harald Welte62b79002018-06-13 23:32:42 +0200118 info_list = new lms_info_str_t[n];
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200119
Harald Welte62b79002018-06-13 23:32:42 +0200120 if (LMS_GetDeviceList(info_list) < 0)
Harald Welte5cc88582018-08-17 19:55:38 +0200121 LOGC(DDEV, ERROR) << "LMS_GetDeviceList(info_list) failed";
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200122
Harald Welte62b79002018-06-13 23:32:42 +0200123 for (i = 0; i < n; i++)
Harald Welte5cc88582018-08-17 19:55:38 +0200124 LOGC(DDEV, INFO) << "Device [" << i << "]: " << info_list[i];
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200125
126 rc = LMS_Open(&m_lms_dev, info_list[0], NULL);
127 if (rc != 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200128 LOGC(DDEV, ERROR) << "LMS_GetDeviceList() failed)";
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200129 delete [] info_list;
130 return -1;
131 }
132
133 delete [] info_list;
134
Harald Welte5cc88582018-08-17 19:55:38 +0200135 LOGC(DDEV, INFO) << "Init LMS device";
Harald Welte9cb4f272018-04-28 21:38:58 +0200136 if (LMS_Init(m_lms_dev) != 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200137 LOGC(DDEV, ERROR) << "LMS_Init() failed";
Pau Espin Pedroled361f92018-12-04 12:42:30 +0100138 goto out_close;
Harald Welte9cb4f272018-04-28 21:38:58 +0200139 }
140
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200141 if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range_sr))
Harald Weltea4381142018-04-28 21:41:07 +0200142 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200143 print_range("Sample Rate", &range_sr);
Harald Weltea4381142018-04-28 21:41:07 +0200144
Harald Welte5cc88582018-08-17 19:55:38 +0200145 LOGC(DDEV, INFO) << "Setting sample rate to " << GSMRATE*tx_sps << " " << tx_sps;
Harald Weltece70ba52018-06-13 22:47:48 +0200146 if (LMS_SetSampleRate(m_lms_dev, GSMRATE*tx_sps, 32) < 0)
Harald Welte940738e2018-03-07 07:50:57 +0100147 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200148
Harald Weltea4381142018-04-28 21:41:07 +0200149 if (LMS_GetSampleRate(m_lms_dev, LMS_CH_RX, 0, &sr_host, &sr_rf))
150 goto out_close;
Harald Welte5cc88582018-08-17 19:55:38 +0200151 LOGC(DDEV, INFO) << "Sample Rate: Host=" << sr_host << " RF=" << sr_rf;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200152
Harald Welte940738e2018-03-07 07:50:57 +0100153 /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */
Harald Weltece70ba52018-06-13 22:47:48 +0200154 ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE * tx_sps); /* time * sample_rate */
Harald Welte940738e2018-03-07 07:50:57 +0100155
156 switch (ref) {
157 case REF_INTERNAL:
Harald Welte5cc88582018-08-17 19:55:38 +0200158 LOGC(DDEV, INFO) << "Setting Internal clock reference";
Harald Welte940738e2018-03-07 07:50:57 +0100159 /* 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 Welte5cc88582018-08-17 19:55:38 +0200162 LOGC(DDEV, INFO) << "Setting VCTCXO to " << dac_val;
Harald Welte940738e2018-03-07 07:50:57 +0100163 if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0)
164 goto out_close;
165 break;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200166 case REF_EXTERNAL:
Harald Welte5cc88582018-08-17 19:55:38 +0200167 LOGC(DDEV, INFO) << "Setting External clock reference to " << 10000000.0;
Harald Welte940738e2018-03-07 07:50:57 +0100168 /* 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 Welte5cc88582018-08-17 19:55:38 +0200173 LOGC(DDEV, ALERT) << "Invalid reference type";
Harald Welte940738e2018-03-07 07:50:57 +0100174 goto out_close;
175 }
176
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200177 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 Welte5cc88582018-08-17 19:55:38 +0200186 LOGC(DDEV, INFO) << "LPFBW: Rx=" << lpfbw_rx << " Tx=" << lpfbw_tx;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200187
Harald Weltecfb9dac2018-06-13 21:56:24 +0200188 if (!set_antennas()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200189 LOGC(DDEV, ALERT) << "LMS antenna setting failed";
Harald Weltecfb9dac2018-06-13 21:56:24 +0200190 return -1;
191 }
192
Harald Welte940738e2018-03-07 07:50:57 +0100193 /* Perform Rx and Tx calibration */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200194 for (i=0; i<chans; i++) {
Harald Welte5cc88582018-08-17 19:55:38 +0200195 LOGC(DDEV, INFO) << "Setting LPFBW chan " << i;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200196 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, i, lpfbw_rx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200197 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200198 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, lpfbw_tx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200199 goto out_close;
Harald Welte5cc88582018-08-17 19:55:38 +0200200 LOGC(DDEV, INFO) << "Calibrating chan " << i;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200201 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 Welte940738e2018-03-07 07:50:57 +0100206
207 samplesRead = 0;
208 samplesWritten = 0;
209 started = false;
210
211 return NORMAL;
212
213out_close:
Harald Welte5cc88582018-08-17 19:55:38 +0200214 LOGC(DDEV, ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
Harald Welte940738e2018-03-07 07:50:57 +0100215 LMS_Close(m_lms_dev);
Pau Espin Pedrol5cea18e2018-12-03 18:17:18 +0100216 m_lms_dev = NULL;
Harald Welte940738e2018-03-07 07:50:57 +0100217 return -1;
218}
219
220bool LMSDevice::start()
221{
Harald Welte5cc88582018-08-17 19:55:38 +0200222 LOGC(DDEV, INFO) << "starting LMS...";
Harald Welte940738e2018-03-07 07:50:57 +0100223
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200224 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100225
Pau Espin Pedrol69869bd2018-12-03 11:19:52 +0100226 if (started) {
227 LOGC(DDEV, ERR) << "Device already started";
228 return false;
229 }
230
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200231 /* configure the channels/streams */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200232 for (i=0; i<chans; i++) {
233 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
234 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100235
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200236 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
237 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100238
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200239 // Set gains to midpoint
240 setTxGain((minTxGain() + maxTxGain()) / 2, i);
Harald Welte55928f22018-11-26 19:26:52 +0100241 setRxGain((minRxGain() + maxRxGain()) / 2, i);
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200242
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 Tamosevicius0494d052018-06-12 00:29:16 +0200262 }
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200263
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200264 /* 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 Pedrolc7a0bf12018-04-25 12:17:10 +0200267 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 Welte940738e2018-03-07 07:50:57 +0100272 }
273
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200274 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100275
276 started = true;
277 return true;
278}
279
280bool LMSDevice::stop()
281{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200282 unsigned int i;
283
Harald Welte940738e2018-03-07 07:50:57 +0100284 if (!started)
285 return true;
286
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200287 for (i=0; i<chans; i++) {
288 LMS_StopStream(&m_lms_stream_tx[i]);
289 LMS_StopStream(&m_lms_stream_rx[i]);
Pau Espin Pedrolb4ea7b52018-12-03 11:34:23 +0100290 }
Harald Welte940738e2018-03-07 07:50:57 +0100291
Pau Espin Pedrolb4ea7b52018-12-03 11:34:23 +0100292 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 Pedrolc7a0bf12018-04-25 12:17:10 +0200295 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
296 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
297 }
Harald Welte940738e2018-03-07 07:50:57 +0100298
Pau Espin Pedrol69869bd2018-12-03 11:19:52 +0100299 started = false;
Harald Welte940738e2018-03-07 07:50:57 +0100300 return true;
301}
302
303double LMSDevice::maxTxGain()
304{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200305 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100306}
307
308double LMSDevice::minTxGain()
309{
310 return 0.0;
311}
312
313double LMSDevice::maxRxGain()
314{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200315 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100316}
317
318double LMSDevice::minRxGain()
319{
320 return 0.0;
321}
322
323double LMSDevice::setTxGain(double dB, size_t chan)
324{
Harald Welte940738e2018-03-07 07:50:57 +0100325 if (dB > maxTxGain())
326 dB = maxTxGain();
327 if (dB < minTxGain())
328 dB = minTxGain();
329
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100330 LOGC(DDEV, NOTICE) << "chan " << chan <<": Setting TX gain to " << dB << " dB";
Harald Welte940738e2018-03-07 07:50:57 +0100331
332 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100333 LOGC(DDEV, ERR) << "chan " << chan <<": Error setting TX gain to " << dB << " dB";
Harald Welte940738e2018-03-07 07:50:57 +0100334
335 return dB;
336}
337
338double LMSDevice::setRxGain(double dB, size_t chan)
339{
Harald Welte940738e2018-03-07 07:50:57 +0100340 if (dB > maxRxGain())
341 dB = maxRxGain();
342 if (dB < minRxGain())
343 dB = minRxGain();
344
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100345 LOGC(DDEV, NOTICE) << "chan "<< chan << ": Setting RX gain to " << dB << " dB";
Harald Welte940738e2018-03-07 07:50:57 +0100346
347 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100348 LOGC(DDEV, ERR) << "chan "<< chan << ": Error setting RX gain to " << dB << " dB";
Harald Welte940738e2018-03-07 07:50:57 +0100349
350 return dB;
351}
352
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200353int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100354{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200355 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
356 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100357 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200358 int i;
359
360 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100361 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200362 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100363 return i;
364 }
365 return -1;
366}
367
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200368bool LMSDevice::flush_recv(size_t num_pkts)
369{
370 #define CHUNK 625
Harald Weltece70ba52018-06-13 22:47:48 +0200371 int len = CHUNK * tx_sps;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200372 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 Welte5cc88582018-08-17 19:55:38 +0200382 LOGC(DDEV, DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200383 if (rc != len) {
Harald Welte5cc88582018-08-17 19:55:38 +0200384 LOGC(DDEV, ALERT) << "LMS: Device receive timed out";
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200385 delete[] buffer;
386 return false;
387 }
388
Harald Welte68f05412018-04-28 21:58:03 +0200389 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200390 }
391
Harald Welte5cc88582018-08-17 19:55:38 +0200392 LOGC(DDEV, INFO) << "Initial timestamp " << ts_initial << std::endl;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200393 delete[] buffer;
394 return true;
395}
396
Harald Welte940738e2018-03-07 07:50:57 +0100397bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
398{
399 int idx;
400
401 if (chan >= rx_paths.size()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200402 LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100403 return false;
404 }
405
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200406 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100407 if (idx < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200408 LOGC(DDEV, ALERT) << "Invalid Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100409 return false;
410 }
411
412 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200413 LOGC(DDEV, ALERT) << "Unable to set Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100414 }
415
416 return true;
417}
418
419std::string LMSDevice::getRxAntenna(size_t chan)
420{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200421 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
422 int idx;
423
Harald Welte940738e2018-03-07 07:50:57 +0100424 if (chan >= rx_paths.size()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200425 LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100426 return "";
427 }
428
429 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
430 if (idx < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200431 LOGC(DDEV, ALERT) << "Error getting Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100432 return "";
433 }
434
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200435 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte5cc88582018-08-17 19:55:38 +0200436 LOGC(DDEV, ALERT) << "Error getting Rx Antenna List";
Harald Welte940738e2018-03-07 07:50:57 +0100437 return "";
438 }
439
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200440 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100441}
442
443bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
444{
445 int idx;
446
447 if (chan >= tx_paths.size()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200448 LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100449 return false;
450 }
451
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200452 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100453 if (idx < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200454 LOGC(DDEV, ALERT) << "Invalid Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100455 return false;
456 }
457
458 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200459 LOGC(DDEV, ALERT) << "Unable to set Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100460 }
461
462 return true;
463}
464
465std::string LMSDevice::getTxAntenna(size_t chan)
466{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200467 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100468 int idx;
469
470 if (chan >= tx_paths.size()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200471 LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100472 return "";
473 }
474
475 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
476 if (idx < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200477 LOGC(DDEV, ALERT) << "Error getting Tx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100478 return "";
479 }
480
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200481 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte5cc88582018-08-17 19:55:38 +0200482 LOGC(DDEV, ALERT) << "Error getting Tx Antenna List";
Harald Welte940738e2018-03-07 07:50:57 +0100483 return "";
484 }
485
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200486 return name_list[idx];
487}
488
489bool LMSDevice::requiresRadioAlign()
490{
491 return false;
492}
493
494GSM::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 Welte940738e2018-03-07 07:50:57 +0100500}
501
Pau Espin Pedrole5b66642018-12-04 20:56:32 +0100502void 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 Welte940738e2018-03-07 07:50:57 +0100516// NOTE: Assumes sequential reads
517int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
518 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
519{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200520 int rc = 0;
521 unsigned int i;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200522 lms_stream_meta_t rx_metadata = {};
523 rx_metadata.flushPartialPacket = false;
524 rx_metadata.waitForTimestamp = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200525 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100526
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200527 if (bufs.size() != chans) {
Harald Welte5cc88582018-08-17 19:55:38 +0200528 LOGC(DDEV, ALERT) << "Invalid channel combination " << bufs.size();
Harald Welte940738e2018-03-07 07:50:57 +0100529 return -1;
530 }
531
Harald Welte940738e2018-03-07 07:50:57 +0100532 *overrun = false;
533 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200534 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 Pedrolfe865f42018-12-07 11:13:55 +0100537 update_stream_stats(i, underrun, overrun);
Pau Espin Pedrol49ad7592018-09-03 16:46:34 +0200538 if (rc != len) {
539 LOGC(DDEV, ALERT) << "LMS: Device receive timed out (" << rc << " vs exp " << len << ").";
540 thread_enable_cancel(true);
541 return -1;
542 }
Harald Welte79024862018-04-28 22:13:31 +0200543 if (timestamp != (TIMESTAMP)rx_metadata.timestamp)
Harald Welte5cc88582018-08-17 19:55:38 +0200544 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 Pedrolc7a0bf12018-04-25 12:17:10 +0200545 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100546 }
547
548 samplesRead += rc;
549
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200550 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
551 rc = 0;
552
Harald Welte940738e2018-03-07 07:50:57 +0100553 return rc;
554}
555
556int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
557 bool * underrun, unsigned long long timestamp,
558 bool isControl)
559{
Vadim Yanitskiy92fc1862018-09-20 16:17:16 +0700560 int rc = 0;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200561 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 Tamosevicius43f36782018-06-12 00:27:09 +0200566 tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */
Harald Welte940738e2018-03-07 07:50:57 +0100567
568 if (isControl) {
Harald Welte5cc88582018-08-17 19:55:38 +0200569 LOGC(DDEV, ERR) << "Control packets not supported";
Harald Welte940738e2018-03-07 07:50:57 +0100570 return 0;
571 }
572
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200573 if (bufs.size() != chans) {
Harald Welte5cc88582018-08-17 19:55:38 +0200574 LOGC(DDEV, ALERT) << "Invalid channel combination " << bufs.size();
Harald Welte940738e2018-03-07 07:50:57 +0100575 return -1;
576 }
577
Harald Welte940738e2018-03-07 07:50:57 +0100578 *underrun = false;
579
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200580 for (i = 0; i<chans; i++) {
Harald Welte5cc88582018-08-17 19:55:38 +0200581 LOGC(DDEV, DEBUG) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200582 thread_enable_cancel(false);
583 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
584 if (rc != len) {
Harald Welte5cc88582018-08-17 19:55:38 +0200585 LOGC(DDEV, ALERT) << "LMS: Device send timed out";
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200586 }
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 Welte940738e2018-03-07 07:50:57 +0100594 }
595
596 samplesWritten += rc;
597
598 return rc;
599}
600
601bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
602{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200603 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100604}
605
606bool LMSDevice::setTxFreq(double wFreq, size_t chan)
607{
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100608 LOGC(DDEV, NOTICE) << "chan "<< chan << ": Setting Tx Freq to " << wFreq << " Hz";
609
Harald Welte940738e2018-03-07 07:50:57 +0100610 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100611 LOGC(DDEV, ERROR) << "chan "<< chan << ": Error setting Tx Freq to " << wFreq << " Hz";
Harald Welte940738e2018-03-07 07:50:57 +0100612 return false;
613 }
614
615 return true;
616}
617
618bool LMSDevice::setRxFreq(double wFreq, size_t chan)
619{
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100620 LOGC(DDEV, NOTICE) << "chan "<< chan << ": Setting Rx Freq to " << wFreq << " Hz";
621
Harald Welte940738e2018-03-07 07:50:57 +0100622 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100623 LOGC(DDEV, ERROR) << "chan "<< chan << ": Error setting Rx Freq to " << wFreq << " Hz";
Harald Welte940738e2018-03-07 07:50:57 +0100624 return false;
625 }
626
627 return true;
628}
629
630RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
Harald Welte61707e82018-06-13 23:21:57 +0200631 InterfaceType iface, size_t chans, double lo_offset,
Harald Welte940738e2018-03-07 07:50:57 +0100632 const std::vector < std::string > &tx_paths,
633 const std::vector < std::string > &rx_paths)
634{
Harald Welteffb33012018-06-13 23:41:35 +0200635 if (tx_sps != rx_sps) {
Harald Welte5cc88582018-08-17 19:55:38 +0200636 LOGC(DDEV, ERROR) << "LMS Requires tx_sps == rx_sps";
Harald Welteffb33012018-06-13 23:41:35 +0200637 return NULL;
638 }
639 if (lo_offset != 0.0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200640 LOGC(DDEV, ERROR) << "LMS doesn't support lo_offset";
Harald Welteffb33012018-06-13 23:41:35 +0200641 return NULL;
642 }
Harald Welte61707e82018-06-13 23:21:57 +0200643 return new LMSDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths);
Harald Welte940738e2018-03-07 07:50:57 +0100644}