blob: 712652a2128867b7636b60b9931f1770c9e4b993 [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{
325 if (chan) {
Harald Welte5cc88582018-08-17 19:55:38 +0200326 LOGC(DDEV, ALERT) << "Invalid channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100327 return 0.0;
328 }
329
330 if (dB > maxTxGain())
331 dB = maxTxGain();
332 if (dB < minTxGain())
333 dB = minTxGain();
334
Harald Welte5cc88582018-08-17 19:55:38 +0200335 LOGC(DDEV, NOTICE) << "Setting TX gain to " << dB << " dB.";
Harald Welte940738e2018-03-07 07:50:57 +0100336
337 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
Harald Welte5cc88582018-08-17 19:55:38 +0200338 LOGC(DDEV, ERR) << "Error setting TX gain";
Harald Welte940738e2018-03-07 07:50:57 +0100339
340 return dB;
341}
342
343double LMSDevice::setRxGain(double dB, size_t chan)
344{
345 if (chan) {
Harald Welte5cc88582018-08-17 19:55:38 +0200346 LOGC(DDEV, ALERT) << "Invalid channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100347 return 0.0;
348 }
349
Harald Welte940738e2018-03-07 07:50:57 +0100350 if (dB > maxRxGain())
351 dB = maxRxGain();
352 if (dB < minRxGain())
353 dB = minRxGain();
354
Harald Welte5cc88582018-08-17 19:55:38 +0200355 LOGC(DDEV, NOTICE) << "Setting RX gain to " << dB << " dB.";
Harald Welte940738e2018-03-07 07:50:57 +0100356
357 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
Harald Welte5cc88582018-08-17 19:55:38 +0200358 LOGC(DDEV, ERR) << "Error setting RX gain";
Harald Welte940738e2018-03-07 07:50:57 +0100359
360 return dB;
361}
362
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200363int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100364{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200365 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
366 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100367 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200368 int i;
369
370 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100371 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200372 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100373 return i;
374 }
375 return -1;
376}
377
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200378bool LMSDevice::flush_recv(size_t num_pkts)
379{
380 #define CHUNK 625
Harald Weltece70ba52018-06-13 22:47:48 +0200381 int len = CHUNK * tx_sps;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200382 short *buffer = new short[len * 2];
383 int rc;
384 lms_stream_meta_t rx_metadata = {};
385 rx_metadata.flushPartialPacket = false;
386 rx_metadata.waitForTimestamp = false;
387
388 ts_initial = 0;
389
390 while (!ts_initial || (num_pkts-- > 0)) {
391 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
Harald Welte5cc88582018-08-17 19:55:38 +0200392 LOGC(DDEV, DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200393 if (rc != len) {
Harald Welte5cc88582018-08-17 19:55:38 +0200394 LOGC(DDEV, ALERT) << "LMS: Device receive timed out";
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200395 delete[] buffer;
396 return false;
397 }
398
Harald Welte68f05412018-04-28 21:58:03 +0200399 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200400 }
401
Harald Welte5cc88582018-08-17 19:55:38 +0200402 LOGC(DDEV, INFO) << "Initial timestamp " << ts_initial << std::endl;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200403 delete[] buffer;
404 return true;
405}
406
Harald Welte940738e2018-03-07 07:50:57 +0100407bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
408{
409 int idx;
410
411 if (chan >= rx_paths.size()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200412 LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100413 return false;
414 }
415
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200416 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100417 if (idx < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200418 LOGC(DDEV, ALERT) << "Invalid Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100419 return false;
420 }
421
422 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200423 LOGC(DDEV, ALERT) << "Unable to set Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100424 }
425
426 return true;
427}
428
429std::string LMSDevice::getRxAntenna(size_t chan)
430{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200431 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
432 int idx;
433
Harald Welte940738e2018-03-07 07:50:57 +0100434 if (chan >= rx_paths.size()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200435 LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100436 return "";
437 }
438
439 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
440 if (idx < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200441 LOGC(DDEV, ALERT) << "Error getting Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100442 return "";
443 }
444
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200445 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte5cc88582018-08-17 19:55:38 +0200446 LOGC(DDEV, ALERT) << "Error getting Rx Antenna List";
Harald Welte940738e2018-03-07 07:50:57 +0100447 return "";
448 }
449
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200450 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100451}
452
453bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
454{
455 int idx;
456
457 if (chan >= tx_paths.size()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200458 LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100459 return false;
460 }
461
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200462 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100463 if (idx < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200464 LOGC(DDEV, ALERT) << "Invalid Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100465 return false;
466 }
467
468 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200469 LOGC(DDEV, ALERT) << "Unable to set Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100470 }
471
472 return true;
473}
474
475std::string LMSDevice::getTxAntenna(size_t chan)
476{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200477 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100478 int idx;
479
480 if (chan >= tx_paths.size()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200481 LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100482 return "";
483 }
484
485 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
486 if (idx < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200487 LOGC(DDEV, ALERT) << "Error getting Tx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100488 return "";
489 }
490
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200491 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte5cc88582018-08-17 19:55:38 +0200492 LOGC(DDEV, ALERT) << "Error getting Tx Antenna List";
Harald Welte940738e2018-03-07 07:50:57 +0100493 return "";
494 }
495
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200496 return name_list[idx];
497}
498
499bool LMSDevice::requiresRadioAlign()
500{
501 return false;
502}
503
504GSM::Time LMSDevice::minLatency() {
505 /* Empirical data from a handful of
506 relatively recent machines shows that the B100 will underrun when
507 the transmit threshold is reduced to a time of 6 and a half frames,
508 so we set a minimum 7 frame threshold. */
509 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100510}
511
512// NOTE: Assumes sequential reads
513int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
514 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
515{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200516 int rc = 0;
517 unsigned int i;
518 lms_stream_status_t status;
519 lms_stream_meta_t rx_metadata = {};
520 rx_metadata.flushPartialPacket = false;
521 rx_metadata.waitForTimestamp = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200522 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100523
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200524 if (bufs.size() != chans) {
Harald Welte5cc88582018-08-17 19:55:38 +0200525 LOGC(DDEV, ALERT) << "Invalid channel combination " << bufs.size();
Harald Welte940738e2018-03-07 07:50:57 +0100526 return -1;
527 }
528
Harald Welte940738e2018-03-07 07:50:57 +0100529 *overrun = false;
530 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200531 for (i = 0; i<chans; i++) {
532 thread_enable_cancel(false);
533 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
Pau Espin Pedrol49ad7592018-09-03 16:46:34 +0200534 if (rc != len) {
535 LOGC(DDEV, ALERT) << "LMS: Device receive timed out (" << rc << " vs exp " << len << ").";
536 thread_enable_cancel(true);
537 return -1;
538 }
Harald Welte79024862018-04-28 22:13:31 +0200539 if (timestamp != (TIMESTAMP)rx_metadata.timestamp)
Harald Welte5cc88582018-08-17 19:55:38 +0200540 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 Welte940738e2018-03-07 07:50:57 +0100541
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200542 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
543 if (status.underrun > m_last_rx_underruns[i])
544 *underrun = true;
545 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100546
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200547 if (status.overrun > m_last_rx_overruns[i])
548 *overrun = true;
549 m_last_rx_overruns[i] = status.overrun;
550 }
551 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100552 }
553
554 samplesRead += rc;
555
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200556 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
557 rc = 0;
558
Harald Welte940738e2018-03-07 07:50:57 +0100559 return rc;
560}
561
562int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
563 bool * underrun, unsigned long long timestamp,
564 bool isControl)
565{
Vadim Yanitskiy92fc1862018-09-20 16:17:16 +0700566 int rc = 0;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200567 unsigned int i;
568 lms_stream_status_t status;
569 lms_stream_meta_t tx_metadata = {};
570 tx_metadata.flushPartialPacket = false;
571 tx_metadata.waitForTimestamp = true;
Zydrunas Tamosevicius43f36782018-06-12 00:27:09 +0200572 tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */
Harald Welte940738e2018-03-07 07:50:57 +0100573
574 if (isControl) {
Harald Welte5cc88582018-08-17 19:55:38 +0200575 LOGC(DDEV, ERR) << "Control packets not supported";
Harald Welte940738e2018-03-07 07:50:57 +0100576 return 0;
577 }
578
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200579 if (bufs.size() != chans) {
Harald Welte5cc88582018-08-17 19:55:38 +0200580 LOGC(DDEV, ALERT) << "Invalid channel combination " << bufs.size();
Harald Welte940738e2018-03-07 07:50:57 +0100581 return -1;
582 }
583
Harald Welte940738e2018-03-07 07:50:57 +0100584 *underrun = false;
585
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200586 for (i = 0; i<chans; i++) {
Harald Welte5cc88582018-08-17 19:55:38 +0200587 LOGC(DDEV, DEBUG) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200588 thread_enable_cancel(false);
589 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
590 if (rc != len) {
Harald Welte5cc88582018-08-17 19:55:38 +0200591 LOGC(DDEV, ALERT) << "LMS: Device send timed out";
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200592 }
593
594 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
595 if (status.underrun > m_last_tx_underruns[i])
596 *underrun = true;
597 m_last_tx_underruns[i] = status.underrun;
598 }
599 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100600 }
601
602 samplesWritten += rc;
603
604 return rc;
605}
606
607bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
608{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200609 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100610}
611
612bool LMSDevice::setTxFreq(double wFreq, size_t chan)
613{
614
615 if (chan) {
Harald Welte5cc88582018-08-17 19:55:38 +0200616 LOGC(DDEV, ALERT) << "Invalid channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100617 return false;
618 }
619
620 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200621 LOGC(DDEV, ALERT) << "set Tx: " << wFreq << " failed!";
Harald Welte940738e2018-03-07 07:50:57 +0100622 return false;
623 }
624
625 return true;
626}
627
628bool LMSDevice::setRxFreq(double wFreq, size_t chan)
629{
630 if (chan) {
Harald Welte5cc88582018-08-17 19:55:38 +0200631 LOGC(DDEV, ALERT) << "Invalid channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100632 return false;
633 }
634
635 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200636 LOGC(DDEV, ALERT) << "set Rx: " << wFreq << " failed!";
Harald Welte940738e2018-03-07 07:50:57 +0100637 return false;
638 }
639
640 return true;
641}
642
643RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
Harald Welte61707e82018-06-13 23:21:57 +0200644 InterfaceType iface, size_t chans, double lo_offset,
Harald Welte940738e2018-03-07 07:50:57 +0100645 const std::vector < std::string > &tx_paths,
646 const std::vector < std::string > &rx_paths)
647{
Harald Welteffb33012018-06-13 23:41:35 +0200648 if (tx_sps != rx_sps) {
Harald Welte5cc88582018-08-17 19:55:38 +0200649 LOGC(DDEV, ERROR) << "LMS Requires tx_sps == rx_sps";
Harald Welteffb33012018-06-13 23:41:35 +0200650 return NULL;
651 }
652 if (lo_offset != 0.0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200653 LOGC(DDEV, ERROR) << "LMS doesn't support lo_offset";
Harald Welteffb33012018-06-13 23:41:35 +0200654 return NULL;
655 }
Harald Welte61707e82018-06-13 23:21:57 +0200656 return new LMSDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths);
Harald Welte940738e2018-03-07 07:50:57 +0100657}