blob: ffea505477d84560abdbf1f2558c369c073f8883 [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{
49 LOG(INFO) << "creating LMS device...";
50
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
60static void lms_log_callback(int lvl, const char *msg)
61{
62 /* map lime specific log levels */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020063 static const int lvl_map[5] = {
Harald Welte940738e2018-03-07 07:50:57 +010064 [0] = LOGL_FATAL,
65 [1] = LOGL_ERROR,
66 [2] = LOGL_NOTICE,
67 [3] = LOGL_INFO,
68 [4] = LOGL_DEBUG,
69 };
70 /* protect against future higher log level values (lower importance) */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020071 if ((unsigned int) lvl >= ARRAY_SIZE(lvl_map))
Harald Welte940738e2018-03-07 07:50:57 +010072 lvl = ARRAY_SIZE(lvl_map)-1;
73
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020074 LOGLV(DLMS, lvl) << msg;
Harald Welte940738e2018-03-07 07:50:57 +010075}
76
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020077static void thread_enable_cancel(bool cancel)
Harald Welte940738e2018-03-07 07:50:57 +010078{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020079 cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) :
80 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
81}
Harald Welte940738e2018-03-07 07:50:57 +010082
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020083static void print_range(const char* name, lms_range_t *range)
84{
85 LOG(DEBUG) << name << ": Min=" << range->min << " Max=" << range->max
86 << " Step=" << range->step;
87}
88
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020089int LMSDevice::open(const std::string &args, int ref, bool swap_channels)
90{
91 //lms_info_str_t dev_str;
92 lms_info_str_t* info_list;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020093 lms_range_t range_lpfbw_rx, range_lpfbw_tx, range_sr;
94 float_type sr_host, sr_rf, lpfbw_rx, lpfbw_tx;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020095 uint16_t dac_val;
96 unsigned int i, n;
97 int rc;
98
99 LOG(INFO) << "Opening LMS device..";
Harald Welte940738e2018-03-07 07:50:57 +0100100
101 LMS_RegisterLogHandler(&lms_log_callback);
102
Harald Welte62b79002018-06-13 23:32:42 +0200103 if ((n = LMS_GetDeviceList(NULL)) < 0)
104 LOG(ERROR) << "LMS_GetDeviceList(NULL) failed";
105 LOG(DEBUG) << "Devices found: " << n;
106 if (n < 1)
107 return -1;
Harald Welte940738e2018-03-07 07:50:57 +0100108
Harald Welte62b79002018-06-13 23:32:42 +0200109 info_list = new lms_info_str_t[n];
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200110
Harald Welte62b79002018-06-13 23:32:42 +0200111 if (LMS_GetDeviceList(info_list) < 0)
112 LOG(ERROR) << "LMS_GetDeviceList(info_list) failed";
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200113
Harald Welte62b79002018-06-13 23:32:42 +0200114 for (i = 0; i < n; i++)
115 LOG(DEBUG) << "Device [" << i << "]: " << info_list[i];
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200116
117 rc = LMS_Open(&m_lms_dev, info_list[0], NULL);
118 if (rc != 0) {
119 LOG(ERROR) << "LMS_GetDeviceList() failed)";
120 delete [] info_list;
121 return -1;
122 }
123
124 delete [] info_list;
125
Harald Welte9cb4f272018-04-28 21:38:58 +0200126 LOG(INFO) << "Init LMS device";
127 if (LMS_Init(m_lms_dev) != 0) {
128 LOG(ERROR) << "LMS_Init() failed";
129 return -1;
130 }
131
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200132 if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range_sr))
Harald Weltea4381142018-04-28 21:41:07 +0200133 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200134 print_range("Sample Rate", &range_sr);
Harald Weltea4381142018-04-28 21:41:07 +0200135
Harald Weltece70ba52018-06-13 22:47:48 +0200136 LOG(DEBUG) << "Setting sample rate to " << GSMRATE*tx_sps << " " << tx_sps;
137 if (LMS_SetSampleRate(m_lms_dev, GSMRATE*tx_sps, 32) < 0)
Harald Welte940738e2018-03-07 07:50:57 +0100138 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200139
Harald Weltea4381142018-04-28 21:41:07 +0200140 if (LMS_GetSampleRate(m_lms_dev, LMS_CH_RX, 0, &sr_host, &sr_rf))
141 goto out_close;
142 LOG(DEBUG) << "Sample Rate: Host=" << sr_host << " RF=" << sr_rf;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200143
Harald Welte940738e2018-03-07 07:50:57 +0100144 /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */
Harald Weltece70ba52018-06-13 22:47:48 +0200145 ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE * tx_sps); /* time * sample_rate */
Harald Welte940738e2018-03-07 07:50:57 +0100146
147 switch (ref) {
148 case REF_INTERNAL:
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200149 LOG(DEBUG) << "Setting Internal clock reference";
Harald Welte940738e2018-03-07 07:50:57 +0100150 /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */
151 if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0)
152 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200153 LOG(DEBUG) << "Setting VCTCXO to " << dac_val;
Harald Welte940738e2018-03-07 07:50:57 +0100154 if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0)
155 goto out_close;
156 break;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200157 case REF_EXTERNAL:
Harald Weltea5054b32018-04-28 21:41:34 +0200158 LOG(DEBUG) << "Setting External clock reference to " << 10000000.0;
Harald Welte940738e2018-03-07 07:50:57 +0100159 /* Assume an external 10 MHz reference clock */
160 if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0)
161 goto out_close;
162 break;
163 default:
164 LOG(ALERT) << "Invalid reference type";
165 goto out_close;
166 }
167
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200168 if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_rx))
169 goto out_close;
170 print_range("LPFBWRange Rx", &range_lpfbw_rx);
171 if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_tx))
172 goto out_close;
173 print_range("LPFBWRange Tx", &range_lpfbw_tx);
174 lpfbw_rx = OSMO_MIN(OSMO_MAX(1.4001e6, range_lpfbw_rx.min), range_lpfbw_rx.max);
175 lpfbw_tx = OSMO_MIN(OSMO_MAX(5.2e6, range_lpfbw_tx.min), range_lpfbw_tx.max);
176
177 LOG(DEBUG) << "LPFBW: Rx=" << lpfbw_rx << " Tx=" << lpfbw_tx;
178
Harald Weltecfb9dac2018-06-13 21:56:24 +0200179 if (!set_antennas()) {
180 LOG(ALERT) << "LMS antenna setting failed";
181 return -1;
182 }
183
Harald Welte940738e2018-03-07 07:50:57 +0100184 /* Perform Rx and Tx calibration */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200185 for (i=0; i<chans; i++) {
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200186 LOG(INFO) << "Setting LPFBW chan " << i;
187 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, i, lpfbw_rx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200188 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200189 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, lpfbw_tx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200190 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200191 LOG(INFO) << "Calibrating chan " << i;
192 if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
193 goto out_close;
194 if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
195 goto out_close;
196 }
Harald Welte940738e2018-03-07 07:50:57 +0100197
198 samplesRead = 0;
199 samplesWritten = 0;
200 started = false;
201
202 return NORMAL;
203
204out_close:
205 LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
206 LMS_Close(m_lms_dev);
207 return -1;
208}
209
210bool LMSDevice::start()
211{
212 LOG(INFO) << "starting LMS...";
213
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200214 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100215
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200216 /* configure the channels/streams */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200217 for (i=0; i<chans; i++) {
218 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
219 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100220
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200221 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
222 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100223
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200224 // Set gains to midpoint
225 setTxGain((minTxGain() + maxTxGain()) / 2, i);
Pau Espin Pedrol58c89fb2018-06-19 10:44:03 +0200226 setRxGain(34.0, i);
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200227
228 m_lms_stream_rx[i] = {};
229 m_lms_stream_rx[i].isTx = false;
230 m_lms_stream_rx[i].channel = i;
231 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
232 m_lms_stream_rx[i].throughputVsLatency = 0.3;
233 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
234
235 m_lms_stream_tx[i] = {};
236 m_lms_stream_tx[i].isTx = true;
237 m_lms_stream_tx[i].channel = i;
238 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
239 m_lms_stream_tx[i].throughputVsLatency = 0.3;
240 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
241
242 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
243 return false;
244
245 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
246 return false;
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200247 }
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200248
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200249 /* now start the streams in a second loop, as we can no longer call
250 * LMS_SetupStream() after LMS_StartStream() of the first stream */
251 for (i = 0; i < chans; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200252 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
253 return false;
254
255 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
256 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100257 }
258
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200259 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100260
261 started = true;
262 return true;
263}
264
265bool LMSDevice::stop()
266{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200267 unsigned int i;
268
Harald Welte940738e2018-03-07 07:50:57 +0100269 if (!started)
270 return true;
271
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200272 for (i=0; i<chans; i++) {
273 LMS_StopStream(&m_lms_stream_tx[i]);
274 LMS_StopStream(&m_lms_stream_rx[i]);
Harald Welte940738e2018-03-07 07:50:57 +0100275
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200276 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
277 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
278 }
Harald Welte940738e2018-03-07 07:50:57 +0100279
280 return true;
281}
282
283double LMSDevice::maxTxGain()
284{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200285 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100286}
287
288double LMSDevice::minTxGain()
289{
290 return 0.0;
291}
292
293double LMSDevice::maxRxGain()
294{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200295 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100296}
297
298double LMSDevice::minRxGain()
299{
300 return 0.0;
301}
302
303double LMSDevice::setTxGain(double dB, size_t chan)
304{
305 if (chan) {
306 LOG(ALERT) << "Invalid channel " << chan;
307 return 0.0;
308 }
309
310 if (dB > maxTxGain())
311 dB = maxTxGain();
312 if (dB < minTxGain())
313 dB = minTxGain();
314
315 LOG(NOTICE) << "Setting TX gain to " << dB << " dB.";
316
317 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
318 LOG(ERR) << "Error setting TX gain";
319
320 return dB;
321}
322
323double LMSDevice::setRxGain(double dB, size_t chan)
324{
325 if (chan) {
326 LOG(ALERT) << "Invalid channel " << chan;
327 return 0.0;
328 }
329
Harald Welte940738e2018-03-07 07:50:57 +0100330 if (dB > maxRxGain())
331 dB = maxRxGain();
332 if (dB < minRxGain())
333 dB = minRxGain();
334
335 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
336
337 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
338 LOG(ERR) << "Error setting RX gain";
339
340 return dB;
341}
342
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200343int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100344{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200345 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
346 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100347 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200348 int i;
349
350 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100351 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200352 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100353 return i;
354 }
355 return -1;
356}
357
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200358bool LMSDevice::flush_recv(size_t num_pkts)
359{
360 #define CHUNK 625
Harald Weltece70ba52018-06-13 22:47:48 +0200361 int len = CHUNK * tx_sps;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200362 short *buffer = new short[len * 2];
363 int rc;
364 lms_stream_meta_t rx_metadata = {};
365 rx_metadata.flushPartialPacket = false;
366 rx_metadata.waitForTimestamp = false;
367
368 ts_initial = 0;
369
370 while (!ts_initial || (num_pkts-- > 0)) {
371 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
372 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
373 if (rc != len) {
374 LOG(ALERT) << "LMS: Device receive timed out";
375 delete[] buffer;
376 return false;
377 }
378
Harald Welte68f05412018-04-28 21:58:03 +0200379 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200380 }
381
382 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
383 delete[] buffer;
384 return true;
385}
386
Harald Welte940738e2018-03-07 07:50:57 +0100387bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
388{
389 int idx;
390
391 if (chan >= rx_paths.size()) {
392 LOG(ALERT) << "Requested non-existent channel " << chan;
393 return false;
394 }
395
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200396 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100397 if (idx < 0) {
398 LOG(ALERT) << "Invalid Rx Antenna";
399 return false;
400 }
401
402 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
403 LOG(ALERT) << "Unable to set Rx Antenna";
404 }
405
406 return true;
407}
408
409std::string LMSDevice::getRxAntenna(size_t chan)
410{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200411 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
412 int idx;
413
Harald Welte940738e2018-03-07 07:50:57 +0100414 if (chan >= rx_paths.size()) {
415 LOG(ALERT) << "Requested non-existent channel " << chan;
416 return "";
417 }
418
419 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
420 if (idx < 0) {
421 LOG(ALERT) << "Error getting Rx Antenna";
422 return "";
423 }
424
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200425 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100426 LOG(ALERT) << "Error getting Rx Antenna List";
427 return "";
428 }
429
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200430 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100431}
432
433bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
434{
435 int idx;
436
437 if (chan >= tx_paths.size()) {
438 LOG(ALERT) << "Requested non-existent channel " << chan;
439 return false;
440 }
441
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200442 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100443 if (idx < 0) {
444 LOG(ALERT) << "Invalid Rx Antenna";
445 return false;
446 }
447
448 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
449 LOG(ALERT) << "Unable to set Rx Antenna";
450 }
451
452 return true;
453}
454
455std::string LMSDevice::getTxAntenna(size_t chan)
456{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200457 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100458 int idx;
459
460 if (chan >= tx_paths.size()) {
461 LOG(ALERT) << "Requested non-existent channel " << chan;
462 return "";
463 }
464
465 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
466 if (idx < 0) {
467 LOG(ALERT) << "Error getting Tx Antenna";
468 return "";
469 }
470
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200471 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100472 LOG(ALERT) << "Error getting Tx Antenna List";
473 return "";
474 }
475
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200476 return name_list[idx];
477}
478
479bool LMSDevice::requiresRadioAlign()
480{
481 return false;
482}
483
484GSM::Time LMSDevice::minLatency() {
485 /* Empirical data from a handful of
486 relatively recent machines shows that the B100 will underrun when
487 the transmit threshold is reduced to a time of 6 and a half frames,
488 so we set a minimum 7 frame threshold. */
489 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100490}
491
492// NOTE: Assumes sequential reads
493int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
494 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
495{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200496 int rc = 0;
497 unsigned int i;
498 lms_stream_status_t status;
499 lms_stream_meta_t rx_metadata = {};
500 rx_metadata.flushPartialPacket = false;
501 rx_metadata.waitForTimestamp = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200502 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100503
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200504 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100505 LOG(ALERT) << "Invalid channel combination " << bufs.size();
506 return -1;
507 }
508
Harald Welte940738e2018-03-07 07:50:57 +0100509 *overrun = false;
510 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200511 for (i = 0; i<chans; i++) {
512 thread_enable_cancel(false);
513 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
Harald Welte79024862018-04-28 22:13:31 +0200514 if (timestamp != (TIMESTAMP)rx_metadata.timestamp)
515 LOG(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 +0200516 if (rc != len) {
517 LOG(ALERT) << "LMS: Device receive timed out";
518 }
Harald Welte940738e2018-03-07 07:50:57 +0100519
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200520 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
521 if (status.underrun > m_last_rx_underruns[i])
522 *underrun = true;
523 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100524
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200525 if (status.overrun > m_last_rx_overruns[i])
526 *overrun = true;
527 m_last_rx_overruns[i] = status.overrun;
528 }
529 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100530 }
531
532 samplesRead += rc;
533
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200534 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
535 rc = 0;
536
Harald Welte940738e2018-03-07 07:50:57 +0100537 return rc;
538}
539
540int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
541 bool * underrun, unsigned long long timestamp,
542 bool isControl)
543{
Harald Welte940738e2018-03-07 07:50:57 +0100544 int rc;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200545 unsigned int i;
546 lms_stream_status_t status;
547 lms_stream_meta_t tx_metadata = {};
548 tx_metadata.flushPartialPacket = false;
549 tx_metadata.waitForTimestamp = true;
Zydrunas Tamosevicius43f36782018-06-12 00:27:09 +0200550 tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */
Harald Welte940738e2018-03-07 07:50:57 +0100551
552 if (isControl) {
553 LOG(ERR) << "Control packets not supported";
554 return 0;
555 }
556
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200557 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100558 LOG(ALERT) << "Invalid channel combination " << bufs.size();
559 return -1;
560 }
561
Harald Welte940738e2018-03-07 07:50:57 +0100562 *underrun = false;
563
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200564 for (i = 0; i<chans; i++) {
Zydrunas Tamoseviciusfd268b62018-06-12 00:27:53 +0200565 LOG(DEBUG) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200566 thread_enable_cancel(false);
567 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
568 if (rc != len) {
569 LOG(ALERT) << "LMS: Device send timed out";
570 }
571
572 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
573 if (status.underrun > m_last_tx_underruns[i])
574 *underrun = true;
575 m_last_tx_underruns[i] = status.underrun;
576 }
577 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100578 }
579
580 samplesWritten += rc;
581
582 return rc;
583}
584
585bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
586{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200587 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100588}
589
590bool LMSDevice::setTxFreq(double wFreq, size_t chan)
591{
592
593 if (chan) {
594 LOG(ALERT) << "Invalid channel " << chan;
595 return false;
596 }
597
598 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
599 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
600 return false;
601 }
602
603 return true;
604}
605
606bool LMSDevice::setRxFreq(double wFreq, size_t chan)
607{
608 if (chan) {
609 LOG(ALERT) << "Invalid channel " << chan;
610 return false;
611 }
612
613 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
614 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
615 return false;
616 }
617
618 return true;
619}
620
621RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
Harald Welte61707e82018-06-13 23:21:57 +0200622 InterfaceType iface, size_t chans, double lo_offset,
Harald Welte940738e2018-03-07 07:50:57 +0100623 const std::vector < std::string > &tx_paths,
624 const std::vector < std::string > &rx_paths)
625{
Harald Welteffb33012018-06-13 23:41:35 +0200626 if (tx_sps != rx_sps) {
627 LOG(ERROR) << "LMS Requires tx_sps == rx_sps";
628 return NULL;
629 }
630 if (lo_offset != 0.0) {
631 LOG(ERROR) << "LMS doesn't support lo_offset";
632 return NULL;
633 }
Harald Welte61707e82018-06-13 23:21:57 +0200634 return new LMSDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths);
Harald Welte940738e2018-03-07 07:50:57 +0100635}