blob: 3e3c5f66f878cab74f79f2f95f77e26d4c172d13 [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
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200208 for (i=0; i<chans; i++) {
209 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
210 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100211
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200212 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
213 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100214
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200215 // Set gains to midpoint
216 setTxGain((minTxGain() + maxTxGain()) / 2, i);
217 setRxGain((minRxGain() + maxRxGain()) / 2, i);
218
219 m_lms_stream_rx[i] = {};
220 m_lms_stream_rx[i].isTx = false;
221 m_lms_stream_rx[i].channel = i;
222 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
223 m_lms_stream_rx[i].throughputVsLatency = 0.3;
224 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
225
226 m_lms_stream_tx[i] = {};
227 m_lms_stream_tx[i].isTx = true;
228 m_lms_stream_tx[i].channel = i;
229 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
230 m_lms_stream_tx[i].throughputVsLatency = 0.3;
231 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
232
233 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
234 return false;
235
236 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
237 return false;
238
239 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
240 return false;
241
242 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
243 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100244 }
245
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200246 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100247
248 started = true;
249 return true;
250}
251
252bool LMSDevice::stop()
253{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200254 unsigned int i;
255
Harald Welte940738e2018-03-07 07:50:57 +0100256 if (!started)
257 return true;
258
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200259 for (i=0; i<chans; i++) {
260 LMS_StopStream(&m_lms_stream_tx[i]);
261 LMS_StopStream(&m_lms_stream_rx[i]);
Harald Welte940738e2018-03-07 07:50:57 +0100262
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200263 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
264 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
265 }
Harald Welte940738e2018-03-07 07:50:57 +0100266
267 return true;
268}
269
270double LMSDevice::maxTxGain()
271{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200272 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100273}
274
275double LMSDevice::minTxGain()
276{
277 return 0.0;
278}
279
280double LMSDevice::maxRxGain()
281{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200282 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100283}
284
285double LMSDevice::minRxGain()
286{
287 return 0.0;
288}
289
290double LMSDevice::setTxGain(double dB, size_t chan)
291{
292 if (chan) {
293 LOG(ALERT) << "Invalid channel " << chan;
294 return 0.0;
295 }
296
297 if (dB > maxTxGain())
298 dB = maxTxGain();
299 if (dB < minTxGain())
300 dB = minTxGain();
301
302 LOG(NOTICE) << "Setting TX gain to " << dB << " dB.";
303
304 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
305 LOG(ERR) << "Error setting TX gain";
306
307 return dB;
308}
309
310double LMSDevice::setRxGain(double dB, size_t chan)
311{
312 if (chan) {
313 LOG(ALERT) << "Invalid channel " << chan;
314 return 0.0;
315 }
316
317 dB = 47.0;
318
319 if (dB > maxRxGain())
320 dB = maxRxGain();
321 if (dB < minRxGain())
322 dB = minRxGain();
323
324 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
325
326 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
327 LOG(ERR) << "Error setting RX gain";
328
329 return dB;
330}
331
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200332int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100333{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200334 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
335 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100336 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200337 int i;
338
339 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100340 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200341 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100342 return i;
343 }
344 return -1;
345}
346
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200347bool LMSDevice::flush_recv(size_t num_pkts)
348{
349 #define CHUNK 625
350 int len = CHUNK * sps;
351 short *buffer = new short[len * 2];
352 int rc;
353 lms_stream_meta_t rx_metadata = {};
354 rx_metadata.flushPartialPacket = false;
355 rx_metadata.waitForTimestamp = false;
356
357 ts_initial = 0;
358
359 while (!ts_initial || (num_pkts-- > 0)) {
360 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
361 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
362 if (rc != len) {
363 LOG(ALERT) << "LMS: Device receive timed out";
364 delete[] buffer;
365 return false;
366 }
367
Harald Welte68f05412018-04-28 21:58:03 +0200368 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200369 }
370
371 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
372 delete[] buffer;
373 return true;
374}
375
Harald Welte940738e2018-03-07 07:50:57 +0100376bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
377{
378 int idx;
379
380 if (chan >= rx_paths.size()) {
381 LOG(ALERT) << "Requested non-existent channel " << chan;
382 return false;
383 }
384
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200385 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100386 if (idx < 0) {
387 LOG(ALERT) << "Invalid Rx Antenna";
388 return false;
389 }
390
391 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
392 LOG(ALERT) << "Unable to set Rx Antenna";
393 }
394
395 return true;
396}
397
398std::string LMSDevice::getRxAntenna(size_t chan)
399{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200400 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
401 int idx;
402
Harald Welte940738e2018-03-07 07:50:57 +0100403 if (chan >= rx_paths.size()) {
404 LOG(ALERT) << "Requested non-existent channel " << chan;
405 return "";
406 }
407
408 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
409 if (idx < 0) {
410 LOG(ALERT) << "Error getting Rx Antenna";
411 return "";
412 }
413
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200414 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100415 LOG(ALERT) << "Error getting Rx Antenna List";
416 return "";
417 }
418
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200419 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100420}
421
422bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
423{
424 int idx;
425
426 if (chan >= tx_paths.size()) {
427 LOG(ALERT) << "Requested non-existent channel " << chan;
428 return false;
429 }
430
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200431 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100432 if (idx < 0) {
433 LOG(ALERT) << "Invalid Rx Antenna";
434 return false;
435 }
436
437 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
438 LOG(ALERT) << "Unable to set Rx Antenna";
439 }
440
441 return true;
442}
443
444std::string LMSDevice::getTxAntenna(size_t chan)
445{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200446 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100447 int idx;
448
449 if (chan >= tx_paths.size()) {
450 LOG(ALERT) << "Requested non-existent channel " << chan;
451 return "";
452 }
453
454 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
455 if (idx < 0) {
456 LOG(ALERT) << "Error getting Tx Antenna";
457 return "";
458 }
459
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200460 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100461 LOG(ALERT) << "Error getting Tx Antenna List";
462 return "";
463 }
464
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200465 return name_list[idx];
466}
467
468bool LMSDevice::requiresRadioAlign()
469{
470 return false;
471}
472
473GSM::Time LMSDevice::minLatency() {
474 /* Empirical data from a handful of
475 relatively recent machines shows that the B100 will underrun when
476 the transmit threshold is reduced to a time of 6 and a half frames,
477 so we set a minimum 7 frame threshold. */
478 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100479}
480
481// NOTE: Assumes sequential reads
482int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
483 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
484{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200485 int rc = 0;
486 unsigned int i;
487 lms_stream_status_t status;
488 lms_stream_meta_t rx_metadata = {};
489 rx_metadata.flushPartialPacket = false;
490 rx_metadata.waitForTimestamp = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200491 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100492
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200493 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100494 LOG(ALERT) << "Invalid channel combination " << bufs.size();
495 return -1;
496 }
497
Harald Welte940738e2018-03-07 07:50:57 +0100498 *overrun = false;
499 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200500 for (i = 0; i<chans; i++) {
501 thread_enable_cancel(false);
502 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
Harald Welte79024862018-04-28 22:13:31 +0200503 if (timestamp != (TIMESTAMP)rx_metadata.timestamp)
504 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 +0200505 if (rc != len) {
506 LOG(ALERT) << "LMS: Device receive timed out";
507 }
Harald Welte940738e2018-03-07 07:50:57 +0100508
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200509 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
510 if (status.underrun > m_last_rx_underruns[i])
511 *underrun = true;
512 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100513
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200514 if (status.overrun > m_last_rx_overruns[i])
515 *overrun = true;
516 m_last_rx_overruns[i] = status.overrun;
517 }
518 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100519 }
520
521 samplesRead += rc;
522
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200523 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
524 rc = 0;
525
Harald Welte940738e2018-03-07 07:50:57 +0100526 return rc;
527}
528
529int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
530 bool * underrun, unsigned long long timestamp,
531 bool isControl)
532{
Harald Welte940738e2018-03-07 07:50:57 +0100533 int rc;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200534 unsigned int i;
535 lms_stream_status_t status;
536 lms_stream_meta_t tx_metadata = {};
537 tx_metadata.flushPartialPacket = false;
538 tx_metadata.waitForTimestamp = true;
Zydrunas Tamosevicius43f36782018-06-12 00:27:09 +0200539 tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */
Harald Welte940738e2018-03-07 07:50:57 +0100540
541 if (isControl) {
542 LOG(ERR) << "Control packets not supported";
543 return 0;
544 }
545
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200546 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100547 LOG(ALERT) << "Invalid channel combination " << bufs.size();
548 return -1;
549 }
550
Harald Welte940738e2018-03-07 07:50:57 +0100551 *underrun = false;
552
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200553 for (i = 0; i<chans; i++) {
554 LOG(ALERT) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
555 thread_enable_cancel(false);
556 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
557 if (rc != len) {
558 LOG(ALERT) << "LMS: Device send timed out";
559 }
560
561 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
562 if (status.underrun > m_last_tx_underruns[i])
563 *underrun = true;
564 m_last_tx_underruns[i] = status.underrun;
565 }
566 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100567 }
568
569 samplesWritten += rc;
570
571 return rc;
572}
573
574bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
575{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200576 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100577}
578
579bool LMSDevice::setTxFreq(double wFreq, size_t chan)
580{
581
582 if (chan) {
583 LOG(ALERT) << "Invalid channel " << chan;
584 return false;
585 }
586
587 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
588 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
589 return false;
590 }
591
592 return true;
593}
594
595bool LMSDevice::setRxFreq(double wFreq, size_t chan)
596{
597 if (chan) {
598 LOG(ALERT) << "Invalid channel " << chan;
599 return false;
600 }
601
602 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
603 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
604 return false;
605 }
606
607 return true;
608}
609
610RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
611 InterfaceType iface, size_t chans, double offset,
612 const std::vector < std::string > &tx_paths,
613 const std::vector < std::string > &rx_paths)
614{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200615 return new LMSDevice(tx_sps, chans);
Harald Welte940738e2018-03-07 07:50:57 +0100616}