blob: 270bd4a17349fcd694ece59fe3e6a37408c082b5 [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"
Oliver Smith871713b2018-12-10 17:10:36 +010024#include "Utils.h"
Harald Welte940738e2018-03-07 07:50:57 +010025
26#include <lime/LimeSuite.h>
27
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020028#include <osmocom/core/utils.h>
29
Harald Welte940738e2018-03-07 07:50:57 +010030#ifdef HAVE_CONFIG_H
31#include "config.h"
32#endif
33
34using namespace std;
35
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020036constexpr double LMSDevice::masterClockRate;
Harald Welte940738e2018-03-07 07:50:57 +010037
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020038#define MAX_ANTENNA_LIST_SIZE 10
39#define LMS_SAMPLE_RATE GSMRATE*32
40#define GSM_CARRIER_BW 270000.0 /* 270kHz */
41#define LMS_MIN_BW_SUPPORTED 2.5e6 /* 2.5mHz, minimum supported by LMS */
42#define LMS_CALIBRATE_BW_HZ OSMO_MAX(GSM_CARRIER_BW, LMS_MIN_BW_SUPPORTED)
43
Harald Welte61707e82018-06-13 23:21:57 +020044LMSDevice::LMSDevice(size_t tx_sps, size_t rx_sps, InterfaceType iface, size_t chans, double lo_offset,
Harald Welte105a61e2018-06-13 21:55:09 +020045 const std::vector<std::string>& tx_paths,
46 const std::vector<std::string>& rx_paths):
Harald Welte61707e82018-06-13 23:21:57 +020047 RadioDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths),
48 m_lms_dev(NULL)
Harald Welte940738e2018-03-07 07:50:57 +010049{
Harald Welte5cc88582018-08-17 19:55:38 +020050 LOGC(DDEV, INFO) << "creating LMS device...";
Harald Welte940738e2018-03-07 07:50:57 +010051
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020052 m_lms_stream_rx.resize(chans);
53 m_lms_stream_tx.resize(chans);
54
55 m_last_rx_underruns.resize(chans, 0);
56 m_last_rx_overruns.resize(chans, 0);
57 m_last_tx_underruns.resize(chans, 0);
58 m_last_tx_overruns.resize(chans, 0);
Harald Welte940738e2018-03-07 07:50:57 +010059}
60
Pau Espin Pedrol5cea18e2018-12-03 18:17:18 +010061LMSDevice::~LMSDevice()
62{
63 LOGC(DDEV, INFO) << "Closing LMS device";
64 if (m_lms_dev) {
65 LMS_Close(m_lms_dev);
66 m_lms_dev = NULL;
67 }
68}
69
Harald Welte940738e2018-03-07 07:50:57 +010070static void lms_log_callback(int lvl, const char *msg)
71{
72 /* map lime specific log levels */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020073 static const int lvl_map[5] = {
Harald Welte940738e2018-03-07 07:50:57 +010074 [0] = LOGL_FATAL,
Pau Espin Pedrol32b3c2e2018-11-23 14:39:06 +010075 [LMS_LOG_ERROR] = LOGL_ERROR,
76 [LMS_LOG_WARNING] = LOGL_NOTICE,
77 [LMS_LOG_INFO] = LOGL_INFO,
78 [LMS_LOG_DEBUG] = LOGL_DEBUG,
Harald Welte940738e2018-03-07 07:50:57 +010079 };
80 /* protect against future higher log level values (lower importance) */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020081 if ((unsigned int) lvl >= ARRAY_SIZE(lvl_map))
Harald Welte940738e2018-03-07 07:50:57 +010082 lvl = ARRAY_SIZE(lvl_map)-1;
83
Pau Espin Pedrol1e2c0102018-11-23 14:39:51 +010084 LOGLV(DLMS, lvl_map[lvl]) << msg;
Harald Welte940738e2018-03-07 07:50:57 +010085}
86
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020087static void thread_enable_cancel(bool cancel)
Harald Welte940738e2018-03-07 07:50:57 +010088{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020089 cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) :
90 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
91}
Harald Welte940738e2018-03-07 07:50:57 +010092
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020093static void print_range(const char* name, lms_range_t *range)
94{
Harald Welte5cc88582018-08-17 19:55:38 +020095 LOGC(DDEV, INFO) << name << ": Min=" << range->min << " Max=" << range->max
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020096 << " Step=" << range->step;
97}
98
Oliver Smith871713b2018-12-10 17:10:36 +010099/*! Find the device string that matches all filters from \a args.
100 * \param[in] info_list device addresses found by LMS_GetDeviceList()
101 * \param[in] count length of info_list
102 * \param[in] args dev-args value from osmo-trx.cfg, containing comma separated key=value pairs
103 * \return index of first matching device or -1 (no match) */
104int info_list_find(lms_info_str_t* info_list, unsigned int count, const std::string &args)
105{
106 unsigned int i, j;
107 vector<string> filters;
108
109 filters = comma_delimited_to_vector(args.c_str());
110
111 /* iterate over device addresses */
112 for (i=0; i < count; i++) {
113 /* check if all filters match */
114 bool match = true;
115 for (j=0; j < filters.size(); j++) {
116 if (!strstr(info_list[i], filters[j].c_str())) {
117 match = false;
118 break;
119 }
120 }
121
122 if (match)
123 return i;
124 }
125 return -1;
126}
127
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200128int LMSDevice::open(const std::string &args, int ref, bool swap_channels)
129{
130 //lms_info_str_t dev_str;
131 lms_info_str_t* info_list;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200132 lms_range_t range_lpfbw_rx, range_lpfbw_tx, range_sr;
133 float_type sr_host, sr_rf, lpfbw_rx, lpfbw_tx;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200134 uint16_t dac_val;
135 unsigned int i, n;
Oliver Smith871713b2018-12-10 17:10:36 +0100136 int rc, dev_id;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200137
Harald Welte5cc88582018-08-17 19:55:38 +0200138 LOGC(DDEV, INFO) << "Opening LMS device..";
Harald Welte940738e2018-03-07 07:50:57 +0100139
140 LMS_RegisterLogHandler(&lms_log_callback);
141
Harald Welte62b79002018-06-13 23:32:42 +0200142 if ((n = LMS_GetDeviceList(NULL)) < 0)
Harald Welte5cc88582018-08-17 19:55:38 +0200143 LOGC(DDEV, ERROR) << "LMS_GetDeviceList(NULL) failed";
144 LOGC(DDEV, INFO) << "Devices found: " << n;
Harald Welte62b79002018-06-13 23:32:42 +0200145 if (n < 1)
146 return -1;
Harald Welte940738e2018-03-07 07:50:57 +0100147
Harald Welte62b79002018-06-13 23:32:42 +0200148 info_list = new lms_info_str_t[n];
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200149
Harald Welte62b79002018-06-13 23:32:42 +0200150 if (LMS_GetDeviceList(info_list) < 0)
Harald Welte5cc88582018-08-17 19:55:38 +0200151 LOGC(DDEV, ERROR) << "LMS_GetDeviceList(info_list) failed";
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200152
Harald Welte62b79002018-06-13 23:32:42 +0200153 for (i = 0; i < n; i++)
Harald Welte5cc88582018-08-17 19:55:38 +0200154 LOGC(DDEV, INFO) << "Device [" << i << "]: " << info_list[i];
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200155
Oliver Smith871713b2018-12-10 17:10:36 +0100156 dev_id = info_list_find(info_list, n, args);
157 if (dev_id == -1) {
158 LOGC(DDEV, ERROR) << "No LMS device found with address '" << args << "'";
159 delete[] info_list;
160 return -1;
161 }
162
163 LOGC(DDEV, INFO) << "Using device[" << dev_id << "]";
164 rc = LMS_Open(&m_lms_dev, info_list[dev_id], NULL);
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200165 if (rc != 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200166 LOGC(DDEV, ERROR) << "LMS_GetDeviceList() failed)";
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200167 delete [] info_list;
168 return -1;
169 }
170
171 delete [] info_list;
172
Harald Welte5cc88582018-08-17 19:55:38 +0200173 LOGC(DDEV, INFO) << "Init LMS device";
Harald Welte9cb4f272018-04-28 21:38:58 +0200174 if (LMS_Init(m_lms_dev) != 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200175 LOGC(DDEV, ERROR) << "LMS_Init() failed";
Pau Espin Pedroled361f92018-12-04 12:42:30 +0100176 goto out_close;
Harald Welte9cb4f272018-04-28 21:38:58 +0200177 }
178
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200179 if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range_sr))
Harald Weltea4381142018-04-28 21:41:07 +0200180 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200181 print_range("Sample Rate", &range_sr);
Harald Weltea4381142018-04-28 21:41:07 +0200182
Harald Welte5cc88582018-08-17 19:55:38 +0200183 LOGC(DDEV, INFO) << "Setting sample rate to " << GSMRATE*tx_sps << " " << tx_sps;
Harald Weltece70ba52018-06-13 22:47:48 +0200184 if (LMS_SetSampleRate(m_lms_dev, GSMRATE*tx_sps, 32) < 0)
Harald Welte940738e2018-03-07 07:50:57 +0100185 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200186
Harald Weltea4381142018-04-28 21:41:07 +0200187 if (LMS_GetSampleRate(m_lms_dev, LMS_CH_RX, 0, &sr_host, &sr_rf))
188 goto out_close;
Harald Welte5cc88582018-08-17 19:55:38 +0200189 LOGC(DDEV, INFO) << "Sample Rate: Host=" << sr_host << " RF=" << sr_rf;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200190
Harald Welte940738e2018-03-07 07:50:57 +0100191 /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */
Harald Weltece70ba52018-06-13 22:47:48 +0200192 ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE * tx_sps); /* time * sample_rate */
Harald Welte940738e2018-03-07 07:50:57 +0100193
194 switch (ref) {
195 case REF_INTERNAL:
Harald Welte5cc88582018-08-17 19:55:38 +0200196 LOGC(DDEV, INFO) << "Setting Internal clock reference";
Harald Welte940738e2018-03-07 07:50:57 +0100197 /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */
198 if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0)
199 goto out_close;
Harald Welte5cc88582018-08-17 19:55:38 +0200200 LOGC(DDEV, INFO) << "Setting VCTCXO to " << dac_val;
Harald Welte940738e2018-03-07 07:50:57 +0100201 if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0)
202 goto out_close;
203 break;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200204 case REF_EXTERNAL:
Harald Welte5cc88582018-08-17 19:55:38 +0200205 LOGC(DDEV, INFO) << "Setting External clock reference to " << 10000000.0;
Harald Welte940738e2018-03-07 07:50:57 +0100206 /* Assume an external 10 MHz reference clock */
207 if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0)
208 goto out_close;
209 break;
210 default:
Harald Welte5cc88582018-08-17 19:55:38 +0200211 LOGC(DDEV, ALERT) << "Invalid reference type";
Harald Welte940738e2018-03-07 07:50:57 +0100212 goto out_close;
213 }
214
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200215 if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_rx))
216 goto out_close;
217 print_range("LPFBWRange Rx", &range_lpfbw_rx);
218 if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_tx))
219 goto out_close;
220 print_range("LPFBWRange Tx", &range_lpfbw_tx);
221 lpfbw_rx = OSMO_MIN(OSMO_MAX(1.4001e6, range_lpfbw_rx.min), range_lpfbw_rx.max);
222 lpfbw_tx = OSMO_MIN(OSMO_MAX(5.2e6, range_lpfbw_tx.min), range_lpfbw_tx.max);
223
Harald Welte5cc88582018-08-17 19:55:38 +0200224 LOGC(DDEV, INFO) << "LPFBW: Rx=" << lpfbw_rx << " Tx=" << lpfbw_tx;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200225
Harald Weltecfb9dac2018-06-13 21:56:24 +0200226 if (!set_antennas()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200227 LOGC(DDEV, ALERT) << "LMS antenna setting failed";
Harald Weltecfb9dac2018-06-13 21:56:24 +0200228 return -1;
229 }
230
Harald Welte940738e2018-03-07 07:50:57 +0100231 /* Perform Rx and Tx calibration */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200232 for (i=0; i<chans; i++) {
Harald Welte5cc88582018-08-17 19:55:38 +0200233 LOGC(DDEV, INFO) << "Setting LPFBW chan " << i;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200234 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, i, lpfbw_rx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200235 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200236 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, lpfbw_tx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200237 goto out_close;
Harald Welte5cc88582018-08-17 19:55:38 +0200238 LOGC(DDEV, INFO) << "Calibrating chan " << i;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200239 if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
240 goto out_close;
241 if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
242 goto out_close;
243 }
Harald Welte940738e2018-03-07 07:50:57 +0100244
245 samplesRead = 0;
246 samplesWritten = 0;
247 started = false;
248
249 return NORMAL;
250
251out_close:
Harald Welte5cc88582018-08-17 19:55:38 +0200252 LOGC(DDEV, ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
Harald Welte940738e2018-03-07 07:50:57 +0100253 LMS_Close(m_lms_dev);
Pau Espin Pedrol5cea18e2018-12-03 18:17:18 +0100254 m_lms_dev = NULL;
Harald Welte940738e2018-03-07 07:50:57 +0100255 return -1;
256}
257
258bool LMSDevice::start()
259{
Harald Welte5cc88582018-08-17 19:55:38 +0200260 LOGC(DDEV, INFO) << "starting LMS...";
Harald Welte940738e2018-03-07 07:50:57 +0100261
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200262 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100263
Pau Espin Pedrol69869bd2018-12-03 11:19:52 +0100264 if (started) {
265 LOGC(DDEV, ERR) << "Device already started";
266 return false;
267 }
268
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200269 /* configure the channels/streams */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200270 for (i=0; i<chans; i++) {
271 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
272 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100273
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200274 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
275 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100276
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200277 // Set gains to midpoint
278 setTxGain((minTxGain() + maxTxGain()) / 2, i);
Harald Welte55928f22018-11-26 19:26:52 +0100279 setRxGain((minRxGain() + maxRxGain()) / 2, i);
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200280
281 m_lms_stream_rx[i] = {};
282 m_lms_stream_rx[i].isTx = false;
283 m_lms_stream_rx[i].channel = i;
284 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
285 m_lms_stream_rx[i].throughputVsLatency = 0.3;
286 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
287
288 m_lms_stream_tx[i] = {};
289 m_lms_stream_tx[i].isTx = true;
290 m_lms_stream_tx[i].channel = i;
291 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
292 m_lms_stream_tx[i].throughputVsLatency = 0.3;
293 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
294
295 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
296 return false;
297
298 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
299 return false;
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200300 }
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200301
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200302 /* now start the streams in a second loop, as we can no longer call
303 * LMS_SetupStream() after LMS_StartStream() of the first stream */
304 for (i = 0; i < chans; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200305 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
306 return false;
307
308 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
309 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100310 }
311
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200312 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100313
314 started = true;
315 return true;
316}
317
318bool LMSDevice::stop()
319{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200320 unsigned int i;
321
Harald Welte940738e2018-03-07 07:50:57 +0100322 if (!started)
323 return true;
324
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200325 for (i=0; i<chans; i++) {
326 LMS_StopStream(&m_lms_stream_tx[i]);
327 LMS_StopStream(&m_lms_stream_rx[i]);
Pau Espin Pedrolb4ea7b52018-12-03 11:34:23 +0100328 }
Harald Welte940738e2018-03-07 07:50:57 +0100329
Pau Espin Pedrolb4ea7b52018-12-03 11:34:23 +0100330 for (i=0; i<chans; i++) {
331 LMS_DestroyStream(m_lms_dev, &m_lms_stream_tx[i]);
332 LMS_DestroyStream(m_lms_dev, &m_lms_stream_rx[i]);
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200333 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
334 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
335 }
Harald Welte940738e2018-03-07 07:50:57 +0100336
Pau Espin Pedrol69869bd2018-12-03 11:19:52 +0100337 started = false;
Harald Welte940738e2018-03-07 07:50:57 +0100338 return true;
339}
340
341double LMSDevice::maxTxGain()
342{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200343 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100344}
345
346double LMSDevice::minTxGain()
347{
348 return 0.0;
349}
350
351double LMSDevice::maxRxGain()
352{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200353 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100354}
355
356double LMSDevice::minRxGain()
357{
358 return 0.0;
359}
360
361double LMSDevice::setTxGain(double dB, size_t chan)
362{
Harald Welte940738e2018-03-07 07:50:57 +0100363 if (dB > maxTxGain())
364 dB = maxTxGain();
365 if (dB < minTxGain())
366 dB = minTxGain();
367
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100368 LOGC(DDEV, NOTICE) << "chan " << chan <<": Setting TX gain to " << dB << " dB";
Harald Welte940738e2018-03-07 07:50:57 +0100369
370 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100371 LOGC(DDEV, ERR) << "chan " << chan <<": Error setting TX gain to " << dB << " dB";
Harald Welte940738e2018-03-07 07:50:57 +0100372
373 return dB;
374}
375
376double LMSDevice::setRxGain(double dB, size_t chan)
377{
Harald Welte940738e2018-03-07 07:50:57 +0100378 if (dB > maxRxGain())
379 dB = maxRxGain();
380 if (dB < minRxGain())
381 dB = minRxGain();
382
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100383 LOGC(DDEV, NOTICE) << "chan "<< chan << ": Setting RX gain to " << dB << " dB";
Harald Welte940738e2018-03-07 07:50:57 +0100384
385 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100386 LOGC(DDEV, ERR) << "chan "<< chan << ": Error setting RX gain to " << dB << " dB";
Harald Welte940738e2018-03-07 07:50:57 +0100387
388 return dB;
389}
390
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200391int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100392{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200393 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
394 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100395 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200396 int i;
397
398 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100399 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200400 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100401 return i;
402 }
403 return -1;
404}
405
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200406bool LMSDevice::flush_recv(size_t num_pkts)
407{
408 #define CHUNK 625
Harald Weltece70ba52018-06-13 22:47:48 +0200409 int len = CHUNK * tx_sps;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200410 short *buffer = new short[len * 2];
411 int rc;
412 lms_stream_meta_t rx_metadata = {};
413 rx_metadata.flushPartialPacket = false;
414 rx_metadata.waitForTimestamp = false;
415
416 ts_initial = 0;
417
418 while (!ts_initial || (num_pkts-- > 0)) {
419 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
Harald Welte5cc88582018-08-17 19:55:38 +0200420 LOGC(DDEV, DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200421 if (rc != len) {
Harald Welte5cc88582018-08-17 19:55:38 +0200422 LOGC(DDEV, ALERT) << "LMS: Device receive timed out";
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200423 delete[] buffer;
424 return false;
425 }
426
Harald Welte68f05412018-04-28 21:58:03 +0200427 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200428 }
429
Harald Welte5cc88582018-08-17 19:55:38 +0200430 LOGC(DDEV, INFO) << "Initial timestamp " << ts_initial << std::endl;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200431 delete[] buffer;
432 return true;
433}
434
Harald Welte940738e2018-03-07 07:50:57 +0100435bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
436{
437 int idx;
438
439 if (chan >= rx_paths.size()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200440 LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100441 return false;
442 }
443
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200444 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100445 if (idx < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200446 LOGC(DDEV, ALERT) << "Invalid Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100447 return false;
448 }
449
450 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200451 LOGC(DDEV, ALERT) << "Unable to set Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100452 }
453
454 return true;
455}
456
457std::string LMSDevice::getRxAntenna(size_t chan)
458{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200459 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
460 int idx;
461
Harald Welte940738e2018-03-07 07:50:57 +0100462 if (chan >= rx_paths.size()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200463 LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100464 return "";
465 }
466
467 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
468 if (idx < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200469 LOGC(DDEV, ALERT) << "Error getting Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100470 return "";
471 }
472
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200473 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte5cc88582018-08-17 19:55:38 +0200474 LOGC(DDEV, ALERT) << "Error getting Rx Antenna List";
Harald Welte940738e2018-03-07 07:50:57 +0100475 return "";
476 }
477
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200478 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100479}
480
481bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
482{
483 int idx;
484
485 if (chan >= tx_paths.size()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200486 LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100487 return false;
488 }
489
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200490 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100491 if (idx < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200492 LOGC(DDEV, ALERT) << "Invalid Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100493 return false;
494 }
495
496 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200497 LOGC(DDEV, ALERT) << "Unable to set Rx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100498 }
499
500 return true;
501}
502
503std::string LMSDevice::getTxAntenna(size_t chan)
504{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200505 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100506 int idx;
507
508 if (chan >= tx_paths.size()) {
Harald Welte5cc88582018-08-17 19:55:38 +0200509 LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan;
Harald Welte940738e2018-03-07 07:50:57 +0100510 return "";
511 }
512
513 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
514 if (idx < 0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200515 LOGC(DDEV, ALERT) << "Error getting Tx Antenna";
Harald Welte940738e2018-03-07 07:50:57 +0100516 return "";
517 }
518
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200519 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte5cc88582018-08-17 19:55:38 +0200520 LOGC(DDEV, ALERT) << "Error getting Tx Antenna List";
Harald Welte940738e2018-03-07 07:50:57 +0100521 return "";
522 }
523
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200524 return name_list[idx];
525}
526
527bool LMSDevice::requiresRadioAlign()
528{
529 return false;
530}
531
532GSM::Time LMSDevice::minLatency() {
533 /* Empirical data from a handful of
534 relatively recent machines shows that the B100 will underrun when
535 the transmit threshold is reduced to a time of 6 and a half frames,
536 so we set a minimum 7 frame threshold. */
537 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100538}
539
Pau Espin Pedrole5b66642018-12-04 20:56:32 +0100540void LMSDevice::update_stream_stats(size_t chan, bool * underrun, bool * overrun)
541{
542 lms_stream_status_t status;
543 if (LMS_GetStreamStatus(&m_lms_stream_rx[chan], &status) == 0) {
544 if (status.underrun > m_last_rx_underruns[chan])
545 *underrun = true;
546 m_last_rx_underruns[chan] = status.underrun;
547
548 if (status.overrun > m_last_rx_overruns[chan])
549 *overrun = true;
550 m_last_rx_overruns[chan] = status.overrun;
551 }
552}
553
Harald Welte940738e2018-03-07 07:50:57 +0100554// NOTE: Assumes sequential reads
555int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
556 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
557{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200558 int rc = 0;
559 unsigned int i;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200560 lms_stream_meta_t rx_metadata = {};
561 rx_metadata.flushPartialPacket = false;
562 rx_metadata.waitForTimestamp = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200563 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100564
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200565 if (bufs.size() != chans) {
Harald Welte5cc88582018-08-17 19:55:38 +0200566 LOGC(DDEV, ALERT) << "Invalid channel combination " << bufs.size();
Harald Welte940738e2018-03-07 07:50:57 +0100567 return -1;
568 }
569
Harald Welte940738e2018-03-07 07:50:57 +0100570 *overrun = false;
571 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200572 for (i = 0; i<chans; i++) {
573 thread_enable_cancel(false);
574 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
Pau Espin Pedrolfe865f42018-12-07 11:13:55 +0100575 update_stream_stats(i, underrun, overrun);
Pau Espin Pedrol49ad7592018-09-03 16:46:34 +0200576 if (rc != len) {
577 LOGC(DDEV, ALERT) << "LMS: Device receive timed out (" << rc << " vs exp " << len << ").";
578 thread_enable_cancel(true);
579 return -1;
580 }
Harald Welte79024862018-04-28 22:13:31 +0200581 if (timestamp != (TIMESTAMP)rx_metadata.timestamp)
Harald Welte5cc88582018-08-17 19:55:38 +0200582 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 +0200583 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100584 }
585
586 samplesRead += rc;
587
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200588 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
589 rc = 0;
590
Harald Welte940738e2018-03-07 07:50:57 +0100591 return rc;
592}
593
594int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
595 bool * underrun, unsigned long long timestamp,
596 bool isControl)
597{
Vadim Yanitskiy92fc1862018-09-20 16:17:16 +0700598 int rc = 0;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200599 unsigned int i;
600 lms_stream_status_t status;
601 lms_stream_meta_t tx_metadata = {};
602 tx_metadata.flushPartialPacket = false;
603 tx_metadata.waitForTimestamp = true;
Zydrunas Tamosevicius43f36782018-06-12 00:27:09 +0200604 tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */
Harald Welte940738e2018-03-07 07:50:57 +0100605
606 if (isControl) {
Harald Welte5cc88582018-08-17 19:55:38 +0200607 LOGC(DDEV, ERR) << "Control packets not supported";
Harald Welte940738e2018-03-07 07:50:57 +0100608 return 0;
609 }
610
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200611 if (bufs.size() != chans) {
Harald Welte5cc88582018-08-17 19:55:38 +0200612 LOGC(DDEV, ALERT) << "Invalid channel combination " << bufs.size();
Harald Welte940738e2018-03-07 07:50:57 +0100613 return -1;
614 }
615
Harald Welte940738e2018-03-07 07:50:57 +0100616 *underrun = false;
617
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200618 for (i = 0; i<chans; i++) {
Harald Welte5cc88582018-08-17 19:55:38 +0200619 LOGC(DDEV, DEBUG) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200620 thread_enable_cancel(false);
621 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
622 if (rc != len) {
Harald Welte5cc88582018-08-17 19:55:38 +0200623 LOGC(DDEV, ALERT) << "LMS: Device send timed out";
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200624 }
625
626 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
627 if (status.underrun > m_last_tx_underruns[i])
628 *underrun = true;
629 m_last_tx_underruns[i] = status.underrun;
630 }
631 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100632 }
633
634 samplesWritten += rc;
635
636 return rc;
637}
638
639bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
640{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200641 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100642}
643
644bool LMSDevice::setTxFreq(double wFreq, size_t chan)
645{
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100646 LOGC(DDEV, NOTICE) << "chan "<< chan << ": Setting Tx Freq to " << wFreq << " Hz";
647
Harald Welte940738e2018-03-07 07:50:57 +0100648 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100649 LOGC(DDEV, ERROR) << "chan "<< chan << ": Error setting Tx Freq to " << wFreq << " Hz";
Harald Welte940738e2018-03-07 07:50:57 +0100650 return false;
651 }
652
653 return true;
654}
655
656bool LMSDevice::setRxFreq(double wFreq, size_t chan)
657{
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100658 LOGC(DDEV, NOTICE) << "chan "<< chan << ": Setting Rx Freq to " << wFreq << " Hz";
659
Harald Welte940738e2018-03-07 07:50:57 +0100660 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
Pau Espin Pedrol138caaf2018-12-04 14:09:53 +0100661 LOGC(DDEV, ERROR) << "chan "<< chan << ": Error setting Rx Freq to " << wFreq << " Hz";
Harald Welte940738e2018-03-07 07:50:57 +0100662 return false;
663 }
664
665 return true;
666}
667
668RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
Harald Welte61707e82018-06-13 23:21:57 +0200669 InterfaceType iface, size_t chans, double lo_offset,
Harald Welte940738e2018-03-07 07:50:57 +0100670 const std::vector < std::string > &tx_paths,
671 const std::vector < std::string > &rx_paths)
672{
Harald Welteffb33012018-06-13 23:41:35 +0200673 if (tx_sps != rx_sps) {
Harald Welte5cc88582018-08-17 19:55:38 +0200674 LOGC(DDEV, ERROR) << "LMS Requires tx_sps == rx_sps";
Harald Welteffb33012018-06-13 23:41:35 +0200675 return NULL;
676 }
677 if (lo_offset != 0.0) {
Harald Welte5cc88582018-08-17 19:55:38 +0200678 LOGC(DDEV, ERROR) << "LMS doesn't support lo_offset";
Harald Welteffb33012018-06-13 23:41:35 +0200679 return NULL;
680 }
Harald Welte61707e82018-06-13 23:21:57 +0200681 return new LMSDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths);
Harald Welte940738e2018-03-07 07:50:57 +0100682}