blob: ad6f67bcd27c730527626dea41d5a729155f92d6 [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
43LMSDevice::LMSDevice(size_t sps, size_t chans):
44 m_lms_dev(NULL), sps(sps), chans(chans)
Harald Welte940738e2018-03-07 07:50:57 +010045{
46 LOG(INFO) << "creating LMS device...";
47
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020048 m_lms_stream_rx.resize(chans);
49 m_lms_stream_tx.resize(chans);
50
51 m_last_rx_underruns.resize(chans, 0);
52 m_last_rx_overruns.resize(chans, 0);
53 m_last_tx_underruns.resize(chans, 0);
54 m_last_tx_overruns.resize(chans, 0);
Harald Welte940738e2018-03-07 07:50:57 +010055}
56
57static void lms_log_callback(int lvl, const char *msg)
58{
59 /* map lime specific log levels */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020060 static const int lvl_map[5] = {
Harald Welte940738e2018-03-07 07:50:57 +010061 [0] = LOGL_FATAL,
62 [1] = LOGL_ERROR,
63 [2] = LOGL_NOTICE,
64 [3] = LOGL_INFO,
65 [4] = LOGL_DEBUG,
66 };
67 /* protect against future higher log level values (lower importance) */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020068 if ((unsigned int) lvl >= ARRAY_SIZE(lvl_map))
Harald Welte940738e2018-03-07 07:50:57 +010069 lvl = ARRAY_SIZE(lvl_map)-1;
70
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020071 LOGLV(DLMS, lvl) << msg;
Harald Welte940738e2018-03-07 07:50:57 +010072}
73
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020074static void thread_enable_cancel(bool cancel)
Harald Welte940738e2018-03-07 07:50:57 +010075{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020076 cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) :
77 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
78}
Harald Welte940738e2018-03-07 07:50:57 +010079
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020080static void print_range(const char* name, lms_range_t *range)
81{
82 LOG(DEBUG) << name << ": Min=" << range->min << " Max=" << range->max
83 << " Step=" << range->step;
84}
85
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020086int LMSDevice::open(const std::string &args, int ref, bool swap_channels)
87{
88 //lms_info_str_t dev_str;
89 lms_info_str_t* info_list;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020090 lms_range_t range_lpfbw_rx, range_lpfbw_tx, range_sr;
91 float_type sr_host, sr_rf, lpfbw_rx, lpfbw_tx;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020092 uint16_t dac_val;
93 unsigned int i, n;
94 int rc;
95
96 LOG(INFO) << "Opening LMS device..";
Harald Welte940738e2018-03-07 07:50:57 +010097
98 LMS_RegisterLogHandler(&lms_log_callback);
99
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200100 if ((n = LMS_GetDeviceList(NULL)) < 0)
101 LOG(ERROR) << "LMS_GetDeviceList(NULL) failed";
102 LOG(DEBUG) << "Devices found: " << n;
103 if (n < 1)
104 return -1;
Harald Welte940738e2018-03-07 07:50:57 +0100105
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200106 info_list = new lms_info_str_t[n];
107
108 if (LMS_GetDeviceList(info_list) < 0) //Populate device list
109 LOG(ERROR) << "LMS_GetDeviceList(info_list) failed";
110
111 for (i = 0; i < n; i++) //print device list
112 LOG(DEBUG) << "Device [" << i << "]: " << info_list[i];
113
114 rc = LMS_Open(&m_lms_dev, info_list[0], NULL);
115 if (rc != 0) {
116 LOG(ERROR) << "LMS_GetDeviceList() failed)";
117 delete [] info_list;
118 return -1;
119 }
120
121 delete [] info_list;
122
Harald Welte9cb4f272018-04-28 21:38:58 +0200123 LOG(INFO) << "Init LMS device";
124 if (LMS_Init(m_lms_dev) != 0) {
125 LOG(ERROR) << "LMS_Init() failed";
126 return -1;
127 }
128
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200129 if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range_sr))
Harald Weltea4381142018-04-28 21:41:07 +0200130 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200131 print_range("Sample Rate", &range_sr);
Harald Weltea4381142018-04-28 21:41:07 +0200132
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200133 LOG(DEBUG) << "Setting sample rate to " << GSMRATE*sps << " " << sps;
134 if (LMS_SetSampleRate(m_lms_dev, GSMRATE*sps, 32) < 0)
Harald Welte940738e2018-03-07 07:50:57 +0100135 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200136
Harald Weltea4381142018-04-28 21:41:07 +0200137 if (LMS_GetSampleRate(m_lms_dev, LMS_CH_RX, 0, &sr_host, &sr_rf))
138 goto out_close;
139 LOG(DEBUG) << "Sample Rate: Host=" << sr_host << " RF=" << sr_rf;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200140
Harald Welte940738e2018-03-07 07:50:57 +0100141 /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */
Zydrunas Tamosevicius43f36782018-06-12 00:27:09 +0200142 ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE * sps); /* time * sample_rate */
Harald Welte940738e2018-03-07 07:50:57 +0100143
144 switch (ref) {
145 case REF_INTERNAL:
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200146 LOG(DEBUG) << "Setting Internal clock reference";
Harald Welte940738e2018-03-07 07:50:57 +0100147 /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */
148 if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0)
149 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200150 LOG(DEBUG) << "Setting VCTCXO to " << dac_val;
Harald Welte940738e2018-03-07 07:50:57 +0100151 if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0)
152 goto out_close;
153 break;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200154 case REF_EXTERNAL:
Harald Weltea5054b32018-04-28 21:41:34 +0200155 LOG(DEBUG) << "Setting External clock reference to " << 10000000.0;
Harald Welte940738e2018-03-07 07:50:57 +0100156 /* Assume an external 10 MHz reference clock */
157 if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0)
158 goto out_close;
159 break;
160 default:
161 LOG(ALERT) << "Invalid reference type";
162 goto out_close;
163 }
164
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200165 if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_rx))
166 goto out_close;
167 print_range("LPFBWRange Rx", &range_lpfbw_rx);
168 if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_tx))
169 goto out_close;
170 print_range("LPFBWRange Tx", &range_lpfbw_tx);
171 lpfbw_rx = OSMO_MIN(OSMO_MAX(1.4001e6, range_lpfbw_rx.min), range_lpfbw_rx.max);
172 lpfbw_tx = OSMO_MIN(OSMO_MAX(5.2e6, range_lpfbw_tx.min), range_lpfbw_tx.max);
173
174 LOG(DEBUG) << "LPFBW: Rx=" << lpfbw_rx << " Tx=" << lpfbw_tx;
175
Harald Welte940738e2018-03-07 07:50:57 +0100176 /* Perform Rx and Tx calibration */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200177 for (i=0; i<chans; i++) {
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200178 LOG(INFO) << "Setting LPFBW chan " << i;
179 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, i, lpfbw_rx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200180 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200181 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, lpfbw_tx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200182 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200183 LOG(INFO) << "Calibrating chan " << i;
184 if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
185 goto out_close;
186 if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
187 goto out_close;
188 }
Harald Welte940738e2018-03-07 07:50:57 +0100189
190 samplesRead = 0;
191 samplesWritten = 0;
192 started = false;
193
194 return NORMAL;
195
196out_close:
197 LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
198 LMS_Close(m_lms_dev);
199 return -1;
200}
201
202bool LMSDevice::start()
203{
204 LOG(INFO) << "starting LMS...";
205
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200206 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100207
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200208 /* configure the channels/streams */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200209 for (i=0; i<chans; i++) {
210 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
211 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100212
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200213 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
214 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100215
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200216 // Set gains to midpoint
217 setTxGain((minTxGain() + maxTxGain()) / 2, i);
218 setRxGain((minRxGain() + maxRxGain()) / 2, i);
219
220 m_lms_stream_rx[i] = {};
221 m_lms_stream_rx[i].isTx = false;
222 m_lms_stream_rx[i].channel = i;
223 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
224 m_lms_stream_rx[i].throughputVsLatency = 0.3;
225 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
226
227 m_lms_stream_tx[i] = {};
228 m_lms_stream_tx[i].isTx = true;
229 m_lms_stream_tx[i].channel = i;
230 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
231 m_lms_stream_tx[i].throughputVsLatency = 0.3;
232 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
233
234 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
235 return false;
236
237 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
238 return false;
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200239 }
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200240
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200241 /* now start the streams in a second loop, as we can no longer call
242 * LMS_SetupStream() after LMS_StartStream() of the first stream */
243 for (i = 0; i < chans; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200244 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
245 return false;
246
247 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
248 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100249 }
250
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200251 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100252
253 started = true;
254 return true;
255}
256
257bool LMSDevice::stop()
258{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200259 unsigned int i;
260
Harald Welte940738e2018-03-07 07:50:57 +0100261 if (!started)
262 return true;
263
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200264 for (i=0; i<chans; i++) {
265 LMS_StopStream(&m_lms_stream_tx[i]);
266 LMS_StopStream(&m_lms_stream_rx[i]);
Harald Welte940738e2018-03-07 07:50:57 +0100267
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200268 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
269 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
270 }
Harald Welte940738e2018-03-07 07:50:57 +0100271
272 return true;
273}
274
275double LMSDevice::maxTxGain()
276{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200277 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100278}
279
280double LMSDevice::minTxGain()
281{
282 return 0.0;
283}
284
285double LMSDevice::maxRxGain()
286{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200287 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100288}
289
290double LMSDevice::minRxGain()
291{
292 return 0.0;
293}
294
295double LMSDevice::setTxGain(double dB, size_t chan)
296{
297 if (chan) {
298 LOG(ALERT) << "Invalid channel " << chan;
299 return 0.0;
300 }
301
302 if (dB > maxTxGain())
303 dB = maxTxGain();
304 if (dB < minTxGain())
305 dB = minTxGain();
306
307 LOG(NOTICE) << "Setting TX gain to " << dB << " dB.";
308
309 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
310 LOG(ERR) << "Error setting TX gain";
311
312 return dB;
313}
314
315double LMSDevice::setRxGain(double dB, size_t chan)
316{
317 if (chan) {
318 LOG(ALERT) << "Invalid channel " << chan;
319 return 0.0;
320 }
321
322 dB = 47.0;
323
324 if (dB > maxRxGain())
325 dB = maxRxGain();
326 if (dB < minRxGain())
327 dB = minRxGain();
328
329 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
330
331 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
332 LOG(ERR) << "Error setting RX gain";
333
334 return dB;
335}
336
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200337int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100338{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200339 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
340 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100341 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200342 int i;
343
344 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100345 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200346 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100347 return i;
348 }
349 return -1;
350}
351
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200352bool LMSDevice::flush_recv(size_t num_pkts)
353{
354 #define CHUNK 625
355 int len = CHUNK * sps;
356 short *buffer = new short[len * 2];
357 int rc;
358 lms_stream_meta_t rx_metadata = {};
359 rx_metadata.flushPartialPacket = false;
360 rx_metadata.waitForTimestamp = false;
361
362 ts_initial = 0;
363
364 while (!ts_initial || (num_pkts-- > 0)) {
365 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
366 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
367 if (rc != len) {
368 LOG(ALERT) << "LMS: Device receive timed out";
369 delete[] buffer;
370 return false;
371 }
372
Harald Welte68f05412018-04-28 21:58:03 +0200373 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200374 }
375
376 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
377 delete[] buffer;
378 return true;
379}
380
Harald Welte940738e2018-03-07 07:50:57 +0100381bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
382{
383 int idx;
384
385 if (chan >= rx_paths.size()) {
386 LOG(ALERT) << "Requested non-existent channel " << chan;
387 return false;
388 }
389
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200390 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100391 if (idx < 0) {
392 LOG(ALERT) << "Invalid Rx Antenna";
393 return false;
394 }
395
396 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
397 LOG(ALERT) << "Unable to set Rx Antenna";
398 }
399
400 return true;
401}
402
403std::string LMSDevice::getRxAntenna(size_t chan)
404{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200405 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
406 int idx;
407
Harald Welte940738e2018-03-07 07:50:57 +0100408 if (chan >= rx_paths.size()) {
409 LOG(ALERT) << "Requested non-existent channel " << chan;
410 return "";
411 }
412
413 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
414 if (idx < 0) {
415 LOG(ALERT) << "Error getting Rx Antenna";
416 return "";
417 }
418
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200419 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100420 LOG(ALERT) << "Error getting Rx Antenna List";
421 return "";
422 }
423
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200424 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100425}
426
427bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
428{
429 int idx;
430
431 if (chan >= tx_paths.size()) {
432 LOG(ALERT) << "Requested non-existent channel " << chan;
433 return false;
434 }
435
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200436 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100437 if (idx < 0) {
438 LOG(ALERT) << "Invalid Rx Antenna";
439 return false;
440 }
441
442 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
443 LOG(ALERT) << "Unable to set Rx Antenna";
444 }
445
446 return true;
447}
448
449std::string LMSDevice::getTxAntenna(size_t chan)
450{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200451 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100452 int idx;
453
454 if (chan >= tx_paths.size()) {
455 LOG(ALERT) << "Requested non-existent channel " << chan;
456 return "";
457 }
458
459 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
460 if (idx < 0) {
461 LOG(ALERT) << "Error getting Tx Antenna";
462 return "";
463 }
464
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200465 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100466 LOG(ALERT) << "Error getting Tx Antenna List";
467 return "";
468 }
469
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200470 return name_list[idx];
471}
472
473bool LMSDevice::requiresRadioAlign()
474{
475 return false;
476}
477
478GSM::Time LMSDevice::minLatency() {
479 /* Empirical data from a handful of
480 relatively recent machines shows that the B100 will underrun when
481 the transmit threshold is reduced to a time of 6 and a half frames,
482 so we set a minimum 7 frame threshold. */
483 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100484}
485
486// NOTE: Assumes sequential reads
487int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
488 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
489{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200490 int rc = 0;
491 unsigned int i;
492 lms_stream_status_t status;
493 lms_stream_meta_t rx_metadata = {};
494 rx_metadata.flushPartialPacket = false;
495 rx_metadata.waitForTimestamp = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200496 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100497
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200498 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100499 LOG(ALERT) << "Invalid channel combination " << bufs.size();
500 return -1;
501 }
502
Harald Welte940738e2018-03-07 07:50:57 +0100503 *overrun = false;
504 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200505 for (i = 0; i<chans; i++) {
506 thread_enable_cancel(false);
507 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
Harald Welte79024862018-04-28 22:13:31 +0200508 if (timestamp != (TIMESTAMP)rx_metadata.timestamp)
509 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 +0200510 if (rc != len) {
511 LOG(ALERT) << "LMS: Device receive timed out";
512 }
Harald Welte940738e2018-03-07 07:50:57 +0100513
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200514 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
515 if (status.underrun > m_last_rx_underruns[i])
516 *underrun = true;
517 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100518
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200519 if (status.overrun > m_last_rx_overruns[i])
520 *overrun = true;
521 m_last_rx_overruns[i] = status.overrun;
522 }
523 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100524 }
525
526 samplesRead += rc;
527
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200528 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
529 rc = 0;
530
Harald Welte940738e2018-03-07 07:50:57 +0100531 return rc;
532}
533
534int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
535 bool * underrun, unsigned long long timestamp,
536 bool isControl)
537{
Harald Welte940738e2018-03-07 07:50:57 +0100538 int rc;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200539 unsigned int i;
540 lms_stream_status_t status;
541 lms_stream_meta_t tx_metadata = {};
542 tx_metadata.flushPartialPacket = false;
543 tx_metadata.waitForTimestamp = true;
Zydrunas Tamosevicius43f36782018-06-12 00:27:09 +0200544 tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */
Harald Welte940738e2018-03-07 07:50:57 +0100545
546 if (isControl) {
547 LOG(ERR) << "Control packets not supported";
548 return 0;
549 }
550
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200551 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100552 LOG(ALERT) << "Invalid channel combination " << bufs.size();
553 return -1;
554 }
555
Harald Welte940738e2018-03-07 07:50:57 +0100556 *underrun = false;
557
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200558 for (i = 0; i<chans; i++) {
Zydrunas Tamoseviciusfd268b62018-06-12 00:27:53 +0200559 LOG(DEBUG) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200560 thread_enable_cancel(false);
561 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
562 if (rc != len) {
563 LOG(ALERT) << "LMS: Device send timed out";
564 }
565
566 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
567 if (status.underrun > m_last_tx_underruns[i])
568 *underrun = true;
569 m_last_tx_underruns[i] = status.underrun;
570 }
571 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100572 }
573
574 samplesWritten += rc;
575
576 return rc;
577}
578
579bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
580{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200581 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100582}
583
584bool LMSDevice::setTxFreq(double wFreq, size_t chan)
585{
586
587 if (chan) {
588 LOG(ALERT) << "Invalid channel " << chan;
589 return false;
590 }
591
592 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
593 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
594 return false;
595 }
596
597 return true;
598}
599
600bool LMSDevice::setRxFreq(double wFreq, size_t chan)
601{
602 if (chan) {
603 LOG(ALERT) << "Invalid channel " << chan;
604 return false;
605 }
606
607 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
608 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
609 return false;
610 }
611
612 return true;
613}
614
615RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
616 InterfaceType iface, size_t chans, double offset,
617 const std::vector < std::string > &tx_paths,
618 const std::vector < std::string > &rx_paths)
619{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200620 return new LMSDevice(tx_sps, chans);
Harald Welte940738e2018-03-07 07:50:57 +0100621}