blob: 815a4c519806d063d358368c56e19ead603499da [file] [log] [blame]
Harald Welte4a5484c2018-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 Pedrolcdbe1e72018-04-25 12:17:10 +020027#include <osmocom/core/utils.h>
28
Harald Welte4a5484c2018-03-07 07:50:57 +010029#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33using namespace std;
34
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +020035constexpr double LMSDevice::masterClockRate;
Harald Welte4a5484c2018-03-07 07:50:57 +010036
Pau Espin Pedrolcdbe1e72018-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 Welte9939ccd2018-06-13 23:21:57 +020043LMSDevice::LMSDevice(size_t tx_sps, size_t rx_sps, InterfaceType iface, size_t chans, double lo_offset,
Harald Welte66efb7c2018-06-13 21:55:09 +020044 const std::vector<std::string>& tx_paths,
45 const std::vector<std::string>& rx_paths):
Harald Welte9939ccd2018-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 Welte4a5484c2018-03-07 07:50:57 +010048{
49 LOG(INFO) << "creating LMS device...";
50
Pau Espin Pedrolcdbe1e72018-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 Welte4a5484c2018-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 Pedrolcdbe1e72018-04-25 12:17:10 +020063 static const int lvl_map[5] = {
Harald Welte4a5484c2018-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 Pedrolcdbe1e72018-04-25 12:17:10 +020071 if ((unsigned int) lvl >= ARRAY_SIZE(lvl_map))
Harald Welte4a5484c2018-03-07 07:50:57 +010072 lvl = ARRAY_SIZE(lvl_map)-1;
73
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +020074 LOGLV(DLMS, lvl) << msg;
Harald Welte4a5484c2018-03-07 07:50:57 +010075}
76
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +020077static void thread_enable_cancel(bool cancel)
Harald Welte4a5484c2018-03-07 07:50:57 +010078{
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +020079 cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) :
80 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
81}
Harald Welte4a5484c2018-03-07 07:50:57 +010082
Pau Espin Pedrolc9ea6e32018-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 Pedrolcdbe1e72018-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 Pedrolc9ea6e32018-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 Pedrolcdbe1e72018-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 Welte4a5484c2018-03-07 07:50:57 +0100100
101 LMS_RegisterLogHandler(&lms_log_callback);
102
Harald Welte35a067e2018-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 Welte4a5484c2018-03-07 07:50:57 +0100108
Harald Welte35a067e2018-06-13 23:32:42 +0200109 info_list = new lms_info_str_t[n];
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200110
Harald Welte35a067e2018-06-13 23:32:42 +0200111 if (LMS_GetDeviceList(info_list) < 0)
112 LOG(ERROR) << "LMS_GetDeviceList(info_list) failed";
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200113
Harald Welte35a067e2018-06-13 23:32:42 +0200114 for (i = 0; i < n; i++)
115 LOG(DEBUG) << "Device [" << i << "]: " << info_list[i];
Pau Espin Pedrolcdbe1e72018-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 Welte7ca30372018-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 Pedrolc9ea6e32018-05-29 19:00:30 +0200132 if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range_sr))
Harald Weltee0d2f502018-04-28 21:41:07 +0200133 goto out_close;
Pau Espin Pedrolc9ea6e32018-05-29 19:00:30 +0200134 print_range("Sample Rate", &range_sr);
Harald Weltee0d2f502018-04-28 21:41:07 +0200135
Harald Welte9eda0222018-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 Welte4a5484c2018-03-07 07:50:57 +0100138 goto out_close;
Pau Espin Pedrolc9ea6e32018-05-29 19:00:30 +0200139
Harald Weltee0d2f502018-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 Pedrolc9ea6e32018-05-29 19:00:30 +0200143
Harald Welte4a5484c2018-03-07 07:50:57 +0100144 /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */
Harald Welte9eda0222018-06-13 22:47:48 +0200145 ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE * tx_sps); /* time * sample_rate */
Harald Welte4a5484c2018-03-07 07:50:57 +0100146
147 switch (ref) {
148 case REF_INTERNAL:
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200149 LOG(DEBUG) << "Setting Internal clock reference";
Harald Welte4a5484c2018-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 Pedrolcdbe1e72018-04-25 12:17:10 +0200153 LOG(DEBUG) << "Setting VCTCXO to " << dac_val;
Harald Welte4a5484c2018-03-07 07:50:57 +0100154 if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0)
155 goto out_close;
156 break;
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200157 case REF_EXTERNAL:
Harald Welte2b764c32018-04-28 21:41:34 +0200158 LOG(DEBUG) << "Setting External clock reference to " << 10000000.0;
Harald Welte4a5484c2018-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 Pedrolc9ea6e32018-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 Weltea1031f12018-06-13 21:56:24 +0200179 if (!set_antennas()) {
180 LOG(ALERT) << "LMS antenna setting failed";
181 return -1;
182 }
183
Harald Welte4a5484c2018-03-07 07:50:57 +0100184 /* Perform Rx and Tx calibration */
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200185 for (i=0; i<chans; i++) {
Pau Espin Pedrolc9ea6e32018-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 Welte6d000ba2018-04-28 21:52:57 +0200188 goto out_close;
Pau Espin Pedrolc9ea6e32018-05-29 19:00:30 +0200189 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, lpfbw_tx) < 0)
Harald Welte6d000ba2018-04-28 21:52:57 +0200190 goto out_close;
Pau Espin Pedrolcdbe1e72018-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 Welte4a5484c2018-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 Pedrolcdbe1e72018-04-25 12:17:10 +0200214 unsigned int i;
Harald Welte4a5484c2018-03-07 07:50:57 +0100215
Zydrunas Tamosevicius1568f872018-06-12 00:29:16 +0200216 /* configure the channels/streams */
Pau Espin Pedrolcdbe1e72018-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 Welte4a5484c2018-03-07 07:50:57 +0100220
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200221 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
222 return false;
Harald Welte4a5484c2018-03-07 07:50:57 +0100223
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200224 // Set gains to midpoint
225 setTxGain((minTxGain() + maxTxGain()) / 2, i);
226 setRxGain((minRxGain() + maxRxGain()) / 2, i);
227
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 Tamosevicius1568f872018-06-12 00:29:16 +0200247 }
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200248
Zydrunas Tamosevicius1568f872018-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 Pedrolcdbe1e72018-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 Welte4a5484c2018-03-07 07:50:57 +0100257 }
258
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200259 flush_recv(10);
Harald Welte4a5484c2018-03-07 07:50:57 +0100260
261 started = true;
262 return true;
263}
264
265bool LMSDevice::stop()
266{
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200267 unsigned int i;
268
Harald Welte4a5484c2018-03-07 07:50:57 +0100269 if (!started)
270 return true;
271
Pau Espin Pedrolcdbe1e72018-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 Welte4a5484c2018-03-07 07:50:57 +0100275
Pau Espin Pedrolcdbe1e72018-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 Welte4a5484c2018-03-07 07:50:57 +0100279
280 return true;
281}
282
283double LMSDevice::maxTxGain()
284{
Pau Espin Pedrol09aa5a32018-05-08 18:46:28 +0200285 return 73.0;
Harald Welte4a5484c2018-03-07 07:50:57 +0100286}
287
288double LMSDevice::minTxGain()
289{
290 return 0.0;
291}
292
293double LMSDevice::maxRxGain()
294{
Pau Espin Pedrol09aa5a32018-05-08 18:46:28 +0200295 return 73.0;
Harald Welte4a5484c2018-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
Zydrunas Tamosevicius70621b72018-06-12 00:35:27 +0200330 dB = 34.0;
Harald Welte4a5484c2018-03-07 07:50:57 +0100331
332 if (dB > maxRxGain())
333 dB = maxRxGain();
334 if (dB < minRxGain())
335 dB = minRxGain();
336
337 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
338
339 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
340 LOG(ERR) << "Error setting RX gain";
341
342 return dB;
343}
344
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200345int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte4a5484c2018-03-07 07:50:57 +0100346{
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200347 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
348 const char* c_name = name.c_str();
Harald Welte4a5484c2018-03-07 07:50:57 +0100349 int num_names;
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200350 int i;
351
352 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte4a5484c2018-03-07 07:50:57 +0100353 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200354 if (!strcmp(c_name, name_list[i]))
Harald Welte4a5484c2018-03-07 07:50:57 +0100355 return i;
356 }
357 return -1;
358}
359
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200360bool LMSDevice::flush_recv(size_t num_pkts)
361{
362 #define CHUNK 625
Harald Welte9eda0222018-06-13 22:47:48 +0200363 int len = CHUNK * tx_sps;
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200364 short *buffer = new short[len * 2];
365 int rc;
366 lms_stream_meta_t rx_metadata = {};
367 rx_metadata.flushPartialPacket = false;
368 rx_metadata.waitForTimestamp = false;
369
370 ts_initial = 0;
371
372 while (!ts_initial || (num_pkts-- > 0)) {
373 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
374 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
375 if (rc != len) {
376 LOG(ALERT) << "LMS: Device receive timed out";
377 delete[] buffer;
378 return false;
379 }
380
Harald Welte380067e2018-04-28 21:58:03 +0200381 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200382 }
383
384 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
385 delete[] buffer;
386 return true;
387}
388
Harald Welte4a5484c2018-03-07 07:50:57 +0100389bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
390{
391 int idx;
392
393 if (chan >= rx_paths.size()) {
394 LOG(ALERT) << "Requested non-existent channel " << chan;
395 return false;
396 }
397
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200398 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte4a5484c2018-03-07 07:50:57 +0100399 if (idx < 0) {
400 LOG(ALERT) << "Invalid Rx Antenna";
401 return false;
402 }
403
404 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
405 LOG(ALERT) << "Unable to set Rx Antenna";
406 }
407
408 return true;
409}
410
411std::string LMSDevice::getRxAntenna(size_t chan)
412{
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200413 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
414 int idx;
415
Harald Welte4a5484c2018-03-07 07:50:57 +0100416 if (chan >= rx_paths.size()) {
417 LOG(ALERT) << "Requested non-existent channel " << chan;
418 return "";
419 }
420
421 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
422 if (idx < 0) {
423 LOG(ALERT) << "Error getting Rx Antenna";
424 return "";
425 }
426
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200427 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte4a5484c2018-03-07 07:50:57 +0100428 LOG(ALERT) << "Error getting Rx Antenna List";
429 return "";
430 }
431
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200432 return name_list[idx];
Harald Welte4a5484c2018-03-07 07:50:57 +0100433}
434
435bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
436{
437 int idx;
438
439 if (chan >= tx_paths.size()) {
440 LOG(ALERT) << "Requested non-existent channel " << chan;
441 return false;
442 }
443
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200444 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte4a5484c2018-03-07 07:50:57 +0100445 if (idx < 0) {
446 LOG(ALERT) << "Invalid Rx Antenna";
447 return false;
448 }
449
450 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
451 LOG(ALERT) << "Unable to set Rx Antenna";
452 }
453
454 return true;
455}
456
457std::string LMSDevice::getTxAntenna(size_t chan)
458{
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200459 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte4a5484c2018-03-07 07:50:57 +0100460 int idx;
461
462 if (chan >= tx_paths.size()) {
463 LOG(ALERT) << "Requested non-existent channel " << chan;
464 return "";
465 }
466
467 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
468 if (idx < 0) {
469 LOG(ALERT) << "Error getting Tx Antenna";
470 return "";
471 }
472
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200473 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte4a5484c2018-03-07 07:50:57 +0100474 LOG(ALERT) << "Error getting Tx Antenna List";
475 return "";
476 }
477
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200478 return name_list[idx];
479}
480
481bool LMSDevice::requiresRadioAlign()
482{
483 return false;
484}
485
486GSM::Time LMSDevice::minLatency() {
487 /* Empirical data from a handful of
488 relatively recent machines shows that the B100 will underrun when
489 the transmit threshold is reduced to a time of 6 and a half frames,
490 so we set a minimum 7 frame threshold. */
491 return GSM::Time(6,7);
Harald Welte4a5484c2018-03-07 07:50:57 +0100492}
493
494// NOTE: Assumes sequential reads
495int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
496 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
497{
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200498 int rc = 0;
499 unsigned int i;
500 lms_stream_status_t status;
501 lms_stream_meta_t rx_metadata = {};
502 rx_metadata.flushPartialPacket = false;
503 rx_metadata.waitForTimestamp = false;
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200504 rx_metadata.timestamp = 0;
Harald Welte4a5484c2018-03-07 07:50:57 +0100505
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200506 if (bufs.size() != chans) {
Harald Welte4a5484c2018-03-07 07:50:57 +0100507 LOG(ALERT) << "Invalid channel combination " << bufs.size();
508 return -1;
509 }
510
Harald Welte4a5484c2018-03-07 07:50:57 +0100511 *overrun = false;
512 *underrun = false;
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200513 for (i = 0; i<chans; i++) {
514 thread_enable_cancel(false);
515 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
Harald Welte1a090b62018-04-28 22:13:31 +0200516 if (timestamp != (TIMESTAMP)rx_metadata.timestamp)
517 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 Pedrolcdbe1e72018-04-25 12:17:10 +0200518 if (rc != len) {
519 LOG(ALERT) << "LMS: Device receive timed out";
520 }
Harald Welte4a5484c2018-03-07 07:50:57 +0100521
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200522 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
523 if (status.underrun > m_last_rx_underruns[i])
524 *underrun = true;
525 m_last_rx_underruns[i] = status.underrun;
Harald Welte4a5484c2018-03-07 07:50:57 +0100526
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200527 if (status.overrun > m_last_rx_overruns[i])
528 *overrun = true;
529 m_last_rx_overruns[i] = status.overrun;
530 }
531 thread_enable_cancel(true);
Harald Welte4a5484c2018-03-07 07:50:57 +0100532 }
533
534 samplesRead += rc;
535
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200536 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
537 rc = 0;
538
Harald Welte4a5484c2018-03-07 07:50:57 +0100539 return rc;
540}
541
542int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
543 bool * underrun, unsigned long long timestamp,
544 bool isControl)
545{
Harald Welte4a5484c2018-03-07 07:50:57 +0100546 int rc;
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200547 unsigned int i;
548 lms_stream_status_t status;
549 lms_stream_meta_t tx_metadata = {};
550 tx_metadata.flushPartialPacket = false;
551 tx_metadata.waitForTimestamp = true;
Zydrunas Tamosevicius59437da2018-06-12 00:27:09 +0200552 tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */
Harald Welte4a5484c2018-03-07 07:50:57 +0100553
554 if (isControl) {
555 LOG(ERR) << "Control packets not supported";
556 return 0;
557 }
558
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200559 if (bufs.size() != chans) {
Harald Welte4a5484c2018-03-07 07:50:57 +0100560 LOG(ALERT) << "Invalid channel combination " << bufs.size();
561 return -1;
562 }
563
Harald Welte4a5484c2018-03-07 07:50:57 +0100564 *underrun = false;
565
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200566 for (i = 0; i<chans; i++) {
Zydrunas Tamoseviciuscace86e2018-06-12 00:27:53 +0200567 LOG(DEBUG) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200568 thread_enable_cancel(false);
569 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
570 if (rc != len) {
571 LOG(ALERT) << "LMS: Device send timed out";
572 }
573
574 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
575 if (status.underrun > m_last_tx_underruns[i])
576 *underrun = true;
577 m_last_tx_underruns[i] = status.underrun;
578 }
579 thread_enable_cancel(true);
Harald Welte4a5484c2018-03-07 07:50:57 +0100580 }
581
582 samplesWritten += rc;
583
584 return rc;
585}
586
587bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
588{
Pau Espin Pedrolcdbe1e72018-04-25 12:17:10 +0200589 return true;
Harald Welte4a5484c2018-03-07 07:50:57 +0100590}
591
592bool LMSDevice::setTxFreq(double wFreq, size_t chan)
593{
594
595 if (chan) {
596 LOG(ALERT) << "Invalid channel " << chan;
597 return false;
598 }
599
600 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
601 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
602 return false;
603 }
604
605 return true;
606}
607
608bool LMSDevice::setRxFreq(double wFreq, size_t chan)
609{
610 if (chan) {
611 LOG(ALERT) << "Invalid channel " << chan;
612 return false;
613 }
614
615 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
616 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
617 return false;
618 }
619
620 return true;
621}
622
623RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
Harald Welte9939ccd2018-06-13 23:21:57 +0200624 InterfaceType iface, size_t chans, double lo_offset,
Harald Welte4a5484c2018-03-07 07:50:57 +0100625 const std::vector < std::string > &tx_paths,
626 const std::vector < std::string > &rx_paths)
627{
Harald Welte9939ccd2018-06-13 23:21:57 +0200628 return new LMSDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths);
Harald Welte4a5484c2018-03-07 07:50:57 +0100629}