blob: 68d4b976a51e1f0681079955a58bbf6a73f89fea [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 Weltece70ba52018-06-13 22:47:48 +020043LMSDevice::LMSDevice(size_t tx_sps, size_t chans,
Harald Welte105a61e2018-06-13 21:55:09 +020044 const std::vector<std::string>& tx_paths,
45 const std::vector<std::string>& rx_paths):
Harald Weltece70ba52018-06-13 22:47:48 +020046 m_lms_dev(NULL), chans(chans)
Harald Welte940738e2018-03-07 07:50:57 +010047{
48 LOG(INFO) << "creating LMS device...";
49
Harald Weltece70ba52018-06-13 22:47:48 +020050 this->tx_sps = tx_sps;
Harald Welte105a61e2018-06-13 21:55:09 +020051 this->tx_paths = tx_paths;
52 this->rx_paths = rx_paths;
53
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020054 m_lms_stream_rx.resize(chans);
55 m_lms_stream_tx.resize(chans);
56
57 m_last_rx_underruns.resize(chans, 0);
58 m_last_rx_overruns.resize(chans, 0);
59 m_last_tx_underruns.resize(chans, 0);
60 m_last_tx_overruns.resize(chans, 0);
Harald Welte940738e2018-03-07 07:50:57 +010061}
62
63static void lms_log_callback(int lvl, const char *msg)
64{
65 /* map lime specific log levels */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020066 static const int lvl_map[5] = {
Harald Welte940738e2018-03-07 07:50:57 +010067 [0] = LOGL_FATAL,
68 [1] = LOGL_ERROR,
69 [2] = LOGL_NOTICE,
70 [3] = LOGL_INFO,
71 [4] = LOGL_DEBUG,
72 };
73 /* protect against future higher log level values (lower importance) */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020074 if ((unsigned int) lvl >= ARRAY_SIZE(lvl_map))
Harald Welte940738e2018-03-07 07:50:57 +010075 lvl = ARRAY_SIZE(lvl_map)-1;
76
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020077 LOGLV(DLMS, lvl) << msg;
Harald Welte940738e2018-03-07 07:50:57 +010078}
79
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020080static void thread_enable_cancel(bool cancel)
Harald Welte940738e2018-03-07 07:50:57 +010081{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020082 cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) :
83 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
84}
Harald Welte940738e2018-03-07 07:50:57 +010085
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020086static void print_range(const char* name, lms_range_t *range)
87{
88 LOG(DEBUG) << name << ": Min=" << range->min << " Max=" << range->max
89 << " Step=" << range->step;
90}
91
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020092int LMSDevice::open(const std::string &args, int ref, bool swap_channels)
93{
94 //lms_info_str_t dev_str;
95 lms_info_str_t* info_list;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020096 lms_range_t range_lpfbw_rx, range_lpfbw_tx, range_sr;
97 float_type sr_host, sr_rf, lpfbw_rx, lpfbw_tx;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020098 uint16_t dac_val;
99 unsigned int i, n;
100 int rc;
101
102 LOG(INFO) << "Opening LMS device..";
Harald Welte940738e2018-03-07 07:50:57 +0100103
104 LMS_RegisterLogHandler(&lms_log_callback);
105
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200106 if ((n = LMS_GetDeviceList(NULL)) < 0)
107 LOG(ERROR) << "LMS_GetDeviceList(NULL) failed";
108 LOG(DEBUG) << "Devices found: " << n;
109 if (n < 1)
110 return -1;
Harald Welte940738e2018-03-07 07:50:57 +0100111
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200112 info_list = new lms_info_str_t[n];
113
114 if (LMS_GetDeviceList(info_list) < 0) //Populate device list
115 LOG(ERROR) << "LMS_GetDeviceList(info_list) failed";
116
117 for (i = 0; i < n; i++) //print device list
118 LOG(DEBUG) << "Device [" << i << "]: " << info_list[i];
119
120 rc = LMS_Open(&m_lms_dev, info_list[0], NULL);
121 if (rc != 0) {
122 LOG(ERROR) << "LMS_GetDeviceList() failed)";
123 delete [] info_list;
124 return -1;
125 }
126
127 delete [] info_list;
128
Harald Welte9cb4f272018-04-28 21:38:58 +0200129 LOG(INFO) << "Init LMS device";
130 if (LMS_Init(m_lms_dev) != 0) {
131 LOG(ERROR) << "LMS_Init() failed";
132 return -1;
133 }
134
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200135 if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range_sr))
Harald Weltea4381142018-04-28 21:41:07 +0200136 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200137 print_range("Sample Rate", &range_sr);
Harald Weltea4381142018-04-28 21:41:07 +0200138
Harald Weltece70ba52018-06-13 22:47:48 +0200139 LOG(DEBUG) << "Setting sample rate to " << GSMRATE*tx_sps << " " << tx_sps;
140 if (LMS_SetSampleRate(m_lms_dev, GSMRATE*tx_sps, 32) < 0)
Harald Welte940738e2018-03-07 07:50:57 +0100141 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200142
Harald Weltea4381142018-04-28 21:41:07 +0200143 if (LMS_GetSampleRate(m_lms_dev, LMS_CH_RX, 0, &sr_host, &sr_rf))
144 goto out_close;
145 LOG(DEBUG) << "Sample Rate: Host=" << sr_host << " RF=" << sr_rf;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200146
Harald Welte940738e2018-03-07 07:50:57 +0100147 /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */
Harald Weltece70ba52018-06-13 22:47:48 +0200148 ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE * tx_sps); /* time * sample_rate */
Harald Welte940738e2018-03-07 07:50:57 +0100149
150 switch (ref) {
151 case REF_INTERNAL:
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200152 LOG(DEBUG) << "Setting Internal clock reference";
Harald Welte940738e2018-03-07 07:50:57 +0100153 /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */
154 if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0)
155 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200156 LOG(DEBUG) << "Setting VCTCXO to " << dac_val;
Harald Welte940738e2018-03-07 07:50:57 +0100157 if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0)
158 goto out_close;
159 break;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200160 case REF_EXTERNAL:
Harald Weltea5054b32018-04-28 21:41:34 +0200161 LOG(DEBUG) << "Setting External clock reference to " << 10000000.0;
Harald Welte940738e2018-03-07 07:50:57 +0100162 /* Assume an external 10 MHz reference clock */
163 if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0)
164 goto out_close;
165 break;
166 default:
167 LOG(ALERT) << "Invalid reference type";
168 goto out_close;
169 }
170
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200171 if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_rx))
172 goto out_close;
173 print_range("LPFBWRange Rx", &range_lpfbw_rx);
174 if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_tx))
175 goto out_close;
176 print_range("LPFBWRange Tx", &range_lpfbw_tx);
177 lpfbw_rx = OSMO_MIN(OSMO_MAX(1.4001e6, range_lpfbw_rx.min), range_lpfbw_rx.max);
178 lpfbw_tx = OSMO_MIN(OSMO_MAX(5.2e6, range_lpfbw_tx.min), range_lpfbw_tx.max);
179
180 LOG(DEBUG) << "LPFBW: Rx=" << lpfbw_rx << " Tx=" << lpfbw_tx;
181
Harald Weltecfb9dac2018-06-13 21:56:24 +0200182 if (!set_antennas()) {
183 LOG(ALERT) << "LMS antenna setting failed";
184 return -1;
185 }
186
Harald Welte940738e2018-03-07 07:50:57 +0100187 /* Perform Rx and Tx calibration */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200188 for (i=0; i<chans; i++) {
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200189 LOG(INFO) << "Setting LPFBW chan " << i;
190 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, i, lpfbw_rx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200191 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200192 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, lpfbw_tx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200193 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200194 LOG(INFO) << "Calibrating chan " << i;
195 if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
196 goto out_close;
197 if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
198 goto out_close;
199 }
Harald Welte940738e2018-03-07 07:50:57 +0100200
201 samplesRead = 0;
202 samplesWritten = 0;
203 started = false;
204
205 return NORMAL;
206
207out_close:
208 LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
209 LMS_Close(m_lms_dev);
210 return -1;
211}
212
213bool LMSDevice::start()
214{
215 LOG(INFO) << "starting LMS...";
216
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200217 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100218
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200219 /* configure the channels/streams */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200220 for (i=0; i<chans; i++) {
221 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
222 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100223
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200224 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
225 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100226
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200227 // Set gains to midpoint
228 setTxGain((minTxGain() + maxTxGain()) / 2, i);
229 setRxGain((minRxGain() + maxRxGain()) / 2, i);
230
231 m_lms_stream_rx[i] = {};
232 m_lms_stream_rx[i].isTx = false;
233 m_lms_stream_rx[i].channel = i;
234 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
235 m_lms_stream_rx[i].throughputVsLatency = 0.3;
236 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
237
238 m_lms_stream_tx[i] = {};
239 m_lms_stream_tx[i].isTx = true;
240 m_lms_stream_tx[i].channel = i;
241 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
242 m_lms_stream_tx[i].throughputVsLatency = 0.3;
243 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
244
245 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
246 return false;
247
248 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
249 return false;
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200250 }
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200251
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200252 /* now start the streams in a second loop, as we can no longer call
253 * LMS_SetupStream() after LMS_StartStream() of the first stream */
254 for (i = 0; i < chans; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200255 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
256 return false;
257
258 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
259 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100260 }
261
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200262 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100263
264 started = true;
265 return true;
266}
267
268bool LMSDevice::stop()
269{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200270 unsigned int i;
271
Harald Welte940738e2018-03-07 07:50:57 +0100272 if (!started)
273 return true;
274
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200275 for (i=0; i<chans; i++) {
276 LMS_StopStream(&m_lms_stream_tx[i]);
277 LMS_StopStream(&m_lms_stream_rx[i]);
Harald Welte940738e2018-03-07 07:50:57 +0100278
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200279 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
280 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
281 }
Harald Welte940738e2018-03-07 07:50:57 +0100282
283 return true;
284}
285
286double LMSDevice::maxTxGain()
287{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200288 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100289}
290
291double LMSDevice::minTxGain()
292{
293 return 0.0;
294}
295
296double LMSDevice::maxRxGain()
297{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200298 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100299}
300
301double LMSDevice::minRxGain()
302{
303 return 0.0;
304}
305
306double LMSDevice::setTxGain(double dB, size_t chan)
307{
308 if (chan) {
309 LOG(ALERT) << "Invalid channel " << chan;
310 return 0.0;
311 }
312
313 if (dB > maxTxGain())
314 dB = maxTxGain();
315 if (dB < minTxGain())
316 dB = minTxGain();
317
318 LOG(NOTICE) << "Setting TX gain to " << dB << " dB.";
319
320 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
321 LOG(ERR) << "Error setting TX gain";
322
323 return dB;
324}
325
326double LMSDevice::setRxGain(double dB, size_t chan)
327{
328 if (chan) {
329 LOG(ALERT) << "Invalid channel " << chan;
330 return 0.0;
331 }
332
Zydrunas Tamoseviciusff441852018-06-12 00:35:27 +0200333 dB = 34.0;
Harald Welte940738e2018-03-07 07:50:57 +0100334
335 if (dB > maxRxGain())
336 dB = maxRxGain();
337 if (dB < minRxGain())
338 dB = minRxGain();
339
340 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
341
342 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
343 LOG(ERR) << "Error setting RX gain";
344
345 return dB;
346}
347
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200348int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100349{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200350 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
351 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100352 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200353 int i;
354
355 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100356 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200357 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100358 return i;
359 }
360 return -1;
361}
362
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200363bool LMSDevice::flush_recv(size_t num_pkts)
364{
365 #define CHUNK 625
Harald Weltece70ba52018-06-13 22:47:48 +0200366 int len = CHUNK * tx_sps;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200367 short *buffer = new short[len * 2];
368 int rc;
369 lms_stream_meta_t rx_metadata = {};
370 rx_metadata.flushPartialPacket = false;
371 rx_metadata.waitForTimestamp = false;
372
373 ts_initial = 0;
374
375 while (!ts_initial || (num_pkts-- > 0)) {
376 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
377 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
378 if (rc != len) {
379 LOG(ALERT) << "LMS: Device receive timed out";
380 delete[] buffer;
381 return false;
382 }
383
Harald Welte68f05412018-04-28 21:58:03 +0200384 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200385 }
386
387 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
388 delete[] buffer;
389 return true;
390}
391
Harald Welte940738e2018-03-07 07:50:57 +0100392bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
393{
394 int idx;
395
396 if (chan >= rx_paths.size()) {
397 LOG(ALERT) << "Requested non-existent channel " << chan;
398 return false;
399 }
400
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200401 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100402 if (idx < 0) {
403 LOG(ALERT) << "Invalid Rx Antenna";
404 return false;
405 }
406
407 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
408 LOG(ALERT) << "Unable to set Rx Antenna";
409 }
410
411 return true;
412}
413
414std::string LMSDevice::getRxAntenna(size_t chan)
415{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200416 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
417 int idx;
418
Harald Welte940738e2018-03-07 07:50:57 +0100419 if (chan >= rx_paths.size()) {
420 LOG(ALERT) << "Requested non-existent channel " << chan;
421 return "";
422 }
423
424 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
425 if (idx < 0) {
426 LOG(ALERT) << "Error getting Rx Antenna";
427 return "";
428 }
429
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200430 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100431 LOG(ALERT) << "Error getting Rx Antenna List";
432 return "";
433 }
434
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200435 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100436}
437
438bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
439{
440 int idx;
441
442 if (chan >= tx_paths.size()) {
443 LOG(ALERT) << "Requested non-existent channel " << chan;
444 return false;
445 }
446
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200447 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100448 if (idx < 0) {
449 LOG(ALERT) << "Invalid Rx Antenna";
450 return false;
451 }
452
453 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
454 LOG(ALERT) << "Unable to set Rx Antenna";
455 }
456
457 return true;
458}
459
460std::string LMSDevice::getTxAntenna(size_t chan)
461{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200462 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100463 int idx;
464
465 if (chan >= tx_paths.size()) {
466 LOG(ALERT) << "Requested non-existent channel " << chan;
467 return "";
468 }
469
470 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
471 if (idx < 0) {
472 LOG(ALERT) << "Error getting Tx Antenna";
473 return "";
474 }
475
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200476 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100477 LOG(ALERT) << "Error getting Tx Antenna List";
478 return "";
479 }
480
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200481 return name_list[idx];
482}
483
484bool LMSDevice::requiresRadioAlign()
485{
486 return false;
487}
488
489GSM::Time LMSDevice::minLatency() {
490 /* Empirical data from a handful of
491 relatively recent machines shows that the B100 will underrun when
492 the transmit threshold is reduced to a time of 6 and a half frames,
493 so we set a minimum 7 frame threshold. */
494 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100495}
496
497// NOTE: Assumes sequential reads
498int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
499 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
500{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200501 int rc = 0;
502 unsigned int i;
503 lms_stream_status_t status;
504 lms_stream_meta_t rx_metadata = {};
505 rx_metadata.flushPartialPacket = false;
506 rx_metadata.waitForTimestamp = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200507 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100508
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200509 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100510 LOG(ALERT) << "Invalid channel combination " << bufs.size();
511 return -1;
512 }
513
Harald Welte940738e2018-03-07 07:50:57 +0100514 *overrun = false;
515 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200516 for (i = 0; i<chans; i++) {
517 thread_enable_cancel(false);
518 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
Harald Welte79024862018-04-28 22:13:31 +0200519 if (timestamp != (TIMESTAMP)rx_metadata.timestamp)
520 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 +0200521 if (rc != len) {
522 LOG(ALERT) << "LMS: Device receive timed out";
523 }
Harald Welte940738e2018-03-07 07:50:57 +0100524
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200525 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
526 if (status.underrun > m_last_rx_underruns[i])
527 *underrun = true;
528 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100529
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200530 if (status.overrun > m_last_rx_overruns[i])
531 *overrun = true;
532 m_last_rx_overruns[i] = status.overrun;
533 }
534 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100535 }
536
537 samplesRead += rc;
538
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200539 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
540 rc = 0;
541
Harald Welte940738e2018-03-07 07:50:57 +0100542 return rc;
543}
544
545int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
546 bool * underrun, unsigned long long timestamp,
547 bool isControl)
548{
Harald Welte940738e2018-03-07 07:50:57 +0100549 int rc;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200550 unsigned int i;
551 lms_stream_status_t status;
552 lms_stream_meta_t tx_metadata = {};
553 tx_metadata.flushPartialPacket = false;
554 tx_metadata.waitForTimestamp = true;
Zydrunas Tamosevicius43f36782018-06-12 00:27:09 +0200555 tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */
Harald Welte940738e2018-03-07 07:50:57 +0100556
557 if (isControl) {
558 LOG(ERR) << "Control packets not supported";
559 return 0;
560 }
561
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200562 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100563 LOG(ALERT) << "Invalid channel combination " << bufs.size();
564 return -1;
565 }
566
Harald Welte940738e2018-03-07 07:50:57 +0100567 *underrun = false;
568
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200569 for (i = 0; i<chans; i++) {
Zydrunas Tamoseviciusfd268b62018-06-12 00:27:53 +0200570 LOG(DEBUG) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200571 thread_enable_cancel(false);
572 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
573 if (rc != len) {
574 LOG(ALERT) << "LMS: Device send timed out";
575 }
576
577 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
578 if (status.underrun > m_last_tx_underruns[i])
579 *underrun = true;
580 m_last_tx_underruns[i] = status.underrun;
581 }
582 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100583 }
584
585 samplesWritten += rc;
586
587 return rc;
588}
589
590bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
591{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200592 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100593}
594
595bool LMSDevice::setTxFreq(double wFreq, size_t chan)
596{
597
598 if (chan) {
599 LOG(ALERT) << "Invalid channel " << chan;
600 return false;
601 }
602
603 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
604 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
605 return false;
606 }
607
608 return true;
609}
610
611bool LMSDevice::setRxFreq(double wFreq, size_t chan)
612{
613 if (chan) {
614 LOG(ALERT) << "Invalid channel " << chan;
615 return false;
616 }
617
618 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
619 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
620 return false;
621 }
622
623 return true;
624}
625
626RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
627 InterfaceType iface, size_t chans, double offset,
628 const std::vector < std::string > &tx_paths,
629 const std::vector < std::string > &rx_paths)
630{
Harald Welte105a61e2018-06-13 21:55:09 +0200631 return new LMSDevice(tx_sps, chans, tx_paths, rx_paths);
Harald Welte940738e2018-03-07 07:50:57 +0100632}