blob: b1f45192b9997e9aefcf46c3adeca0adad03fa7a [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 Welte105a61e2018-06-13 21:55:09 +020043LMSDevice::LMSDevice(size_t sps, size_t chans,
44 const std::vector<std::string>& tx_paths,
45 const std::vector<std::string>& rx_paths):
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020046 m_lms_dev(NULL), sps(sps), chans(chans)
Harald Welte940738e2018-03-07 07:50:57 +010047{
48 LOG(INFO) << "creating LMS device...";
49
Harald Welte105a61e2018-06-13 21:55:09 +020050 this->tx_paths = tx_paths;
51 this->rx_paths = rx_paths;
52
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020053 m_lms_stream_rx.resize(chans);
54 m_lms_stream_tx.resize(chans);
55
56 m_last_rx_underruns.resize(chans, 0);
57 m_last_rx_overruns.resize(chans, 0);
58 m_last_tx_underruns.resize(chans, 0);
59 m_last_tx_overruns.resize(chans, 0);
Harald Welte940738e2018-03-07 07:50:57 +010060}
61
62static void lms_log_callback(int lvl, const char *msg)
63{
64 /* map lime specific log levels */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020065 static const int lvl_map[5] = {
Harald Welte940738e2018-03-07 07:50:57 +010066 [0] = LOGL_FATAL,
67 [1] = LOGL_ERROR,
68 [2] = LOGL_NOTICE,
69 [3] = LOGL_INFO,
70 [4] = LOGL_DEBUG,
71 };
72 /* protect against future higher log level values (lower importance) */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020073 if ((unsigned int) lvl >= ARRAY_SIZE(lvl_map))
Harald Welte940738e2018-03-07 07:50:57 +010074 lvl = ARRAY_SIZE(lvl_map)-1;
75
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020076 LOGLV(DLMS, lvl) << msg;
Harald Welte940738e2018-03-07 07:50:57 +010077}
78
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020079static void thread_enable_cancel(bool cancel)
Harald Welte940738e2018-03-07 07:50:57 +010080{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020081 cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) :
82 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
83}
Harald Welte940738e2018-03-07 07:50:57 +010084
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020085static void print_range(const char* name, lms_range_t *range)
86{
87 LOG(DEBUG) << name << ": Min=" << range->min << " Max=" << range->max
88 << " Step=" << range->step;
89}
90
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020091int LMSDevice::open(const std::string &args, int ref, bool swap_channels)
92{
93 //lms_info_str_t dev_str;
94 lms_info_str_t* info_list;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020095 lms_range_t range_lpfbw_rx, range_lpfbw_tx, range_sr;
96 float_type sr_host, sr_rf, lpfbw_rx, lpfbw_tx;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020097 uint16_t dac_val;
98 unsigned int i, n;
99 int rc;
100
101 LOG(INFO) << "Opening LMS device..";
Harald Welte940738e2018-03-07 07:50:57 +0100102
103 LMS_RegisterLogHandler(&lms_log_callback);
104
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200105 if ((n = LMS_GetDeviceList(NULL)) < 0)
106 LOG(ERROR) << "LMS_GetDeviceList(NULL) failed";
107 LOG(DEBUG) << "Devices found: " << n;
108 if (n < 1)
109 return -1;
Harald Welte940738e2018-03-07 07:50:57 +0100110
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200111 info_list = new lms_info_str_t[n];
112
113 if (LMS_GetDeviceList(info_list) < 0) //Populate device list
114 LOG(ERROR) << "LMS_GetDeviceList(info_list) failed";
115
116 for (i = 0; i < n; i++) //print device list
117 LOG(DEBUG) << "Device [" << i << "]: " << info_list[i];
118
119 rc = LMS_Open(&m_lms_dev, info_list[0], NULL);
120 if (rc != 0) {
121 LOG(ERROR) << "LMS_GetDeviceList() failed)";
122 delete [] info_list;
123 return -1;
124 }
125
126 delete [] info_list;
127
Harald Welte9cb4f272018-04-28 21:38:58 +0200128 LOG(INFO) << "Init LMS device";
129 if (LMS_Init(m_lms_dev) != 0) {
130 LOG(ERROR) << "LMS_Init() failed";
131 return -1;
132 }
133
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200134 if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range_sr))
Harald Weltea4381142018-04-28 21:41:07 +0200135 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200136 print_range("Sample Rate", &range_sr);
Harald Weltea4381142018-04-28 21:41:07 +0200137
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200138 LOG(DEBUG) << "Setting sample rate to " << GSMRATE*sps << " " << sps;
139 if (LMS_SetSampleRate(m_lms_dev, GSMRATE*sps, 32) < 0)
Harald Welte940738e2018-03-07 07:50:57 +0100140 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200141
Harald Weltea4381142018-04-28 21:41:07 +0200142 if (LMS_GetSampleRate(m_lms_dev, LMS_CH_RX, 0, &sr_host, &sr_rf))
143 goto out_close;
144 LOG(DEBUG) << "Sample Rate: Host=" << sr_host << " RF=" << sr_rf;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200145
Harald Welte940738e2018-03-07 07:50:57 +0100146 /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */
Zydrunas Tamosevicius43f36782018-06-12 00:27:09 +0200147 ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE * sps); /* time * sample_rate */
Harald Welte940738e2018-03-07 07:50:57 +0100148
149 switch (ref) {
150 case REF_INTERNAL:
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200151 LOG(DEBUG) << "Setting Internal clock reference";
Harald Welte940738e2018-03-07 07:50:57 +0100152 /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */
153 if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0)
154 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200155 LOG(DEBUG) << "Setting VCTCXO to " << dac_val;
Harald Welte940738e2018-03-07 07:50:57 +0100156 if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0)
157 goto out_close;
158 break;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200159 case REF_EXTERNAL:
Harald Weltea5054b32018-04-28 21:41:34 +0200160 LOG(DEBUG) << "Setting External clock reference to " << 10000000.0;
Harald Welte940738e2018-03-07 07:50:57 +0100161 /* Assume an external 10 MHz reference clock */
162 if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0)
163 goto out_close;
164 break;
165 default:
166 LOG(ALERT) << "Invalid reference type";
167 goto out_close;
168 }
169
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200170 if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_rx))
171 goto out_close;
172 print_range("LPFBWRange Rx", &range_lpfbw_rx);
173 if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_tx))
174 goto out_close;
175 print_range("LPFBWRange Tx", &range_lpfbw_tx);
176 lpfbw_rx = OSMO_MIN(OSMO_MAX(1.4001e6, range_lpfbw_rx.min), range_lpfbw_rx.max);
177 lpfbw_tx = OSMO_MIN(OSMO_MAX(5.2e6, range_lpfbw_tx.min), range_lpfbw_tx.max);
178
179 LOG(DEBUG) << "LPFBW: Rx=" << lpfbw_rx << " Tx=" << lpfbw_tx;
180
Harald Welte940738e2018-03-07 07:50:57 +0100181 /* Perform Rx and Tx calibration */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200182 for (i=0; i<chans; i++) {
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200183 LOG(INFO) << "Setting LPFBW chan " << i;
184 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, i, lpfbw_rx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200185 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200186 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, lpfbw_tx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200187 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200188 LOG(INFO) << "Calibrating chan " << i;
189 if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
190 goto out_close;
191 if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
192 goto out_close;
193 }
Harald Welte940738e2018-03-07 07:50:57 +0100194
195 samplesRead = 0;
196 samplesWritten = 0;
197 started = false;
198
199 return NORMAL;
200
201out_close:
202 LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
203 LMS_Close(m_lms_dev);
204 return -1;
205}
206
207bool LMSDevice::start()
208{
209 LOG(INFO) << "starting LMS...";
210
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200211 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100212
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200213 /* configure the channels/streams */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200214 for (i=0; i<chans; i++) {
215 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
216 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100217
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200218 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
219 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100220
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200221 // Set gains to midpoint
222 setTxGain((minTxGain() + maxTxGain()) / 2, i);
223 setRxGain((minRxGain() + maxRxGain()) / 2, i);
224
225 m_lms_stream_rx[i] = {};
226 m_lms_stream_rx[i].isTx = false;
227 m_lms_stream_rx[i].channel = i;
228 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
229 m_lms_stream_rx[i].throughputVsLatency = 0.3;
230 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
231
232 m_lms_stream_tx[i] = {};
233 m_lms_stream_tx[i].isTx = true;
234 m_lms_stream_tx[i].channel = i;
235 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
236 m_lms_stream_tx[i].throughputVsLatency = 0.3;
237 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
238
239 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
240 return false;
241
242 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
243 return false;
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200244 }
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200245
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200246 /* now start the streams in a second loop, as we can no longer call
247 * LMS_SetupStream() after LMS_StartStream() of the first stream */
248 for (i = 0; i < chans; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200249 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
250 return false;
251
252 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
253 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100254 }
255
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200256 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100257
258 started = true;
259 return true;
260}
261
262bool LMSDevice::stop()
263{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200264 unsigned int i;
265
Harald Welte940738e2018-03-07 07:50:57 +0100266 if (!started)
267 return true;
268
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200269 for (i=0; i<chans; i++) {
270 LMS_StopStream(&m_lms_stream_tx[i]);
271 LMS_StopStream(&m_lms_stream_rx[i]);
Harald Welte940738e2018-03-07 07:50:57 +0100272
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200273 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
274 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
275 }
Harald Welte940738e2018-03-07 07:50:57 +0100276
277 return true;
278}
279
280double LMSDevice::maxTxGain()
281{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200282 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100283}
284
285double LMSDevice::minTxGain()
286{
287 return 0.0;
288}
289
290double LMSDevice::maxRxGain()
291{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200292 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100293}
294
295double LMSDevice::minRxGain()
296{
297 return 0.0;
298}
299
300double LMSDevice::setTxGain(double dB, size_t chan)
301{
302 if (chan) {
303 LOG(ALERT) << "Invalid channel " << chan;
304 return 0.0;
305 }
306
307 if (dB > maxTxGain())
308 dB = maxTxGain();
309 if (dB < minTxGain())
310 dB = minTxGain();
311
312 LOG(NOTICE) << "Setting TX gain to " << dB << " dB.";
313
314 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
315 LOG(ERR) << "Error setting TX gain";
316
317 return dB;
318}
319
320double LMSDevice::setRxGain(double dB, size_t chan)
321{
322 if (chan) {
323 LOG(ALERT) << "Invalid channel " << chan;
324 return 0.0;
325 }
326
Zydrunas Tamoseviciusff441852018-06-12 00:35:27 +0200327 dB = 34.0;
Harald Welte940738e2018-03-07 07:50:57 +0100328
329 if (dB > maxRxGain())
330 dB = maxRxGain();
331 if (dB < minRxGain())
332 dB = minRxGain();
333
334 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
335
336 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
337 LOG(ERR) << "Error setting RX gain";
338
339 return dB;
340}
341
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200342int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100343{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200344 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
345 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100346 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200347 int i;
348
349 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100350 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200351 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100352 return i;
353 }
354 return -1;
355}
356
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200357bool LMSDevice::flush_recv(size_t num_pkts)
358{
359 #define CHUNK 625
360 int len = CHUNK * sps;
361 short *buffer = new short[len * 2];
362 int rc;
363 lms_stream_meta_t rx_metadata = {};
364 rx_metadata.flushPartialPacket = false;
365 rx_metadata.waitForTimestamp = false;
366
367 ts_initial = 0;
368
369 while (!ts_initial || (num_pkts-- > 0)) {
370 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
371 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
372 if (rc != len) {
373 LOG(ALERT) << "LMS: Device receive timed out";
374 delete[] buffer;
375 return false;
376 }
377
Harald Welte68f05412018-04-28 21:58:03 +0200378 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200379 }
380
381 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
382 delete[] buffer;
383 return true;
384}
385
Harald Welte940738e2018-03-07 07:50:57 +0100386bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
387{
388 int idx;
389
390 if (chan >= rx_paths.size()) {
391 LOG(ALERT) << "Requested non-existent channel " << chan;
392 return false;
393 }
394
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200395 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100396 if (idx < 0) {
397 LOG(ALERT) << "Invalid Rx Antenna";
398 return false;
399 }
400
401 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
402 LOG(ALERT) << "Unable to set Rx Antenna";
403 }
404
405 return true;
406}
407
408std::string LMSDevice::getRxAntenna(size_t chan)
409{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200410 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
411 int idx;
412
Harald Welte940738e2018-03-07 07:50:57 +0100413 if (chan >= rx_paths.size()) {
414 LOG(ALERT) << "Requested non-existent channel " << chan;
415 return "";
416 }
417
418 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
419 if (idx < 0) {
420 LOG(ALERT) << "Error getting Rx Antenna";
421 return "";
422 }
423
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200424 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100425 LOG(ALERT) << "Error getting Rx Antenna List";
426 return "";
427 }
428
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200429 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100430}
431
432bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
433{
434 int idx;
435
436 if (chan >= tx_paths.size()) {
437 LOG(ALERT) << "Requested non-existent channel " << chan;
438 return false;
439 }
440
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200441 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100442 if (idx < 0) {
443 LOG(ALERT) << "Invalid Rx Antenna";
444 return false;
445 }
446
447 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
448 LOG(ALERT) << "Unable to set Rx Antenna";
449 }
450
451 return true;
452}
453
454std::string LMSDevice::getTxAntenna(size_t chan)
455{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200456 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100457 int idx;
458
459 if (chan >= tx_paths.size()) {
460 LOG(ALERT) << "Requested non-existent channel " << chan;
461 return "";
462 }
463
464 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
465 if (idx < 0) {
466 LOG(ALERT) << "Error getting Tx Antenna";
467 return "";
468 }
469
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200470 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100471 LOG(ALERT) << "Error getting Tx Antenna List";
472 return "";
473 }
474
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200475 return name_list[idx];
476}
477
478bool LMSDevice::requiresRadioAlign()
479{
480 return false;
481}
482
483GSM::Time LMSDevice::minLatency() {
484 /* Empirical data from a handful of
485 relatively recent machines shows that the B100 will underrun when
486 the transmit threshold is reduced to a time of 6 and a half frames,
487 so we set a minimum 7 frame threshold. */
488 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100489}
490
491// NOTE: Assumes sequential reads
492int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
493 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
494{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200495 int rc = 0;
496 unsigned int i;
497 lms_stream_status_t status;
498 lms_stream_meta_t rx_metadata = {};
499 rx_metadata.flushPartialPacket = false;
500 rx_metadata.waitForTimestamp = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200501 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100502
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200503 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100504 LOG(ALERT) << "Invalid channel combination " << bufs.size();
505 return -1;
506 }
507
Harald Welte940738e2018-03-07 07:50:57 +0100508 *overrun = false;
509 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200510 for (i = 0; i<chans; i++) {
511 thread_enable_cancel(false);
512 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
Harald Welte79024862018-04-28 22:13:31 +0200513 if (timestamp != (TIMESTAMP)rx_metadata.timestamp)
514 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 +0200515 if (rc != len) {
516 LOG(ALERT) << "LMS: Device receive timed out";
517 }
Harald Welte940738e2018-03-07 07:50:57 +0100518
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200519 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
520 if (status.underrun > m_last_rx_underruns[i])
521 *underrun = true;
522 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100523
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200524 if (status.overrun > m_last_rx_overruns[i])
525 *overrun = true;
526 m_last_rx_overruns[i] = status.overrun;
527 }
528 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100529 }
530
531 samplesRead += rc;
532
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200533 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
534 rc = 0;
535
Harald Welte940738e2018-03-07 07:50:57 +0100536 return rc;
537}
538
539int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
540 bool * underrun, unsigned long long timestamp,
541 bool isControl)
542{
Harald Welte940738e2018-03-07 07:50:57 +0100543 int rc;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200544 unsigned int i;
545 lms_stream_status_t status;
546 lms_stream_meta_t tx_metadata = {};
547 tx_metadata.flushPartialPacket = false;
548 tx_metadata.waitForTimestamp = true;
Zydrunas Tamosevicius43f36782018-06-12 00:27:09 +0200549 tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */
Harald Welte940738e2018-03-07 07:50:57 +0100550
551 if (isControl) {
552 LOG(ERR) << "Control packets not supported";
553 return 0;
554 }
555
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200556 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100557 LOG(ALERT) << "Invalid channel combination " << bufs.size();
558 return -1;
559 }
560
Harald Welte940738e2018-03-07 07:50:57 +0100561 *underrun = false;
562
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200563 for (i = 0; i<chans; i++) {
Zydrunas Tamoseviciusfd268b62018-06-12 00:27:53 +0200564 LOG(DEBUG) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200565 thread_enable_cancel(false);
566 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
567 if (rc != len) {
568 LOG(ALERT) << "LMS: Device send timed out";
569 }
570
571 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
572 if (status.underrun > m_last_tx_underruns[i])
573 *underrun = true;
574 m_last_tx_underruns[i] = status.underrun;
575 }
576 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100577 }
578
579 samplesWritten += rc;
580
581 return rc;
582}
583
584bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
585{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200586 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100587}
588
589bool LMSDevice::setTxFreq(double wFreq, size_t chan)
590{
591
592 if (chan) {
593 LOG(ALERT) << "Invalid channel " << chan;
594 return false;
595 }
596
597 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
598 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
599 return false;
600 }
601
602 return true;
603}
604
605bool LMSDevice::setRxFreq(double wFreq, size_t chan)
606{
607 if (chan) {
608 LOG(ALERT) << "Invalid channel " << chan;
609 return false;
610 }
611
612 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
613 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
614 return false;
615 }
616
617 return true;
618}
619
620RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
621 InterfaceType iface, size_t chans, double offset,
622 const std::vector < std::string > &tx_paths,
623 const std::vector < std::string > &rx_paths)
624{
Harald Welte105a61e2018-06-13 21:55:09 +0200625 return new LMSDevice(tx_sps, chans, tx_paths, rx_paths);
Harald Welte940738e2018-03-07 07:50:57 +0100626}