blob: 518e5812cefb41413c261d43117188464ab7242f [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 Weltecfb9dac2018-06-13 21:56:24 +0200181 if (!set_antennas()) {
182 LOG(ALERT) << "LMS antenna setting failed";
183 return -1;
184 }
185
Harald Welte940738e2018-03-07 07:50:57 +0100186 /* Perform Rx and Tx calibration */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200187 for (i=0; i<chans; i++) {
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200188 LOG(INFO) << "Setting LPFBW chan " << i;
189 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, i, lpfbw_rx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200190 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200191 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, lpfbw_tx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200192 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200193 LOG(INFO) << "Calibrating chan " << i;
194 if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
195 goto out_close;
196 if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
197 goto out_close;
198 }
Harald Welte940738e2018-03-07 07:50:57 +0100199
200 samplesRead = 0;
201 samplesWritten = 0;
202 started = false;
203
204 return NORMAL;
205
206out_close:
207 LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
208 LMS_Close(m_lms_dev);
209 return -1;
210}
211
212bool LMSDevice::start()
213{
214 LOG(INFO) << "starting LMS...";
215
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200216 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100217
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200218 /* configure the channels/streams */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200219 for (i=0; i<chans; i++) {
220 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
221 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100222
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200223 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
224 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100225
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200226 // Set gains to midpoint
227 setTxGain((minTxGain() + maxTxGain()) / 2, i);
228 setRxGain((minRxGain() + maxRxGain()) / 2, i);
229
230 m_lms_stream_rx[i] = {};
231 m_lms_stream_rx[i].isTx = false;
232 m_lms_stream_rx[i].channel = i;
233 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
234 m_lms_stream_rx[i].throughputVsLatency = 0.3;
235 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
236
237 m_lms_stream_tx[i] = {};
238 m_lms_stream_tx[i].isTx = true;
239 m_lms_stream_tx[i].channel = i;
240 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
241 m_lms_stream_tx[i].throughputVsLatency = 0.3;
242 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
243
244 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
245 return false;
246
247 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
248 return false;
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200249 }
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200250
Zydrunas Tamosevicius0494d052018-06-12 00:29:16 +0200251 /* now start the streams in a second loop, as we can no longer call
252 * LMS_SetupStream() after LMS_StartStream() of the first stream */
253 for (i = 0; i < chans; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200254 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
255 return false;
256
257 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
258 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100259 }
260
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200261 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100262
263 started = true;
264 return true;
265}
266
267bool LMSDevice::stop()
268{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200269 unsigned int i;
270
Harald Welte940738e2018-03-07 07:50:57 +0100271 if (!started)
272 return true;
273
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200274 for (i=0; i<chans; i++) {
275 LMS_StopStream(&m_lms_stream_tx[i]);
276 LMS_StopStream(&m_lms_stream_rx[i]);
Harald Welte940738e2018-03-07 07:50:57 +0100277
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200278 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
279 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
280 }
Harald Welte940738e2018-03-07 07:50:57 +0100281
282 return true;
283}
284
285double LMSDevice::maxTxGain()
286{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200287 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100288}
289
290double LMSDevice::minTxGain()
291{
292 return 0.0;
293}
294
295double LMSDevice::maxRxGain()
296{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200297 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100298}
299
300double LMSDevice::minRxGain()
301{
302 return 0.0;
303}
304
305double LMSDevice::setTxGain(double dB, size_t chan)
306{
307 if (chan) {
308 LOG(ALERT) << "Invalid channel " << chan;
309 return 0.0;
310 }
311
312 if (dB > maxTxGain())
313 dB = maxTxGain();
314 if (dB < minTxGain())
315 dB = minTxGain();
316
317 LOG(NOTICE) << "Setting TX gain to " << dB << " dB.";
318
319 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
320 LOG(ERR) << "Error setting TX gain";
321
322 return dB;
323}
324
325double LMSDevice::setRxGain(double dB, size_t chan)
326{
327 if (chan) {
328 LOG(ALERT) << "Invalid channel " << chan;
329 return 0.0;
330 }
331
Zydrunas Tamoseviciusff441852018-06-12 00:35:27 +0200332 dB = 34.0;
Harald Welte940738e2018-03-07 07:50:57 +0100333
334 if (dB > maxRxGain())
335 dB = maxRxGain();
336 if (dB < minRxGain())
337 dB = minRxGain();
338
339 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
340
341 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
342 LOG(ERR) << "Error setting RX gain";
343
344 return dB;
345}
346
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200347int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100348{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200349 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
350 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100351 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200352 int i;
353
354 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100355 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200356 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100357 return i;
358 }
359 return -1;
360}
361
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200362bool LMSDevice::flush_recv(size_t num_pkts)
363{
364 #define CHUNK 625
365 int len = CHUNK * sps;
366 short *buffer = new short[len * 2];
367 int rc;
368 lms_stream_meta_t rx_metadata = {};
369 rx_metadata.flushPartialPacket = false;
370 rx_metadata.waitForTimestamp = false;
371
372 ts_initial = 0;
373
374 while (!ts_initial || (num_pkts-- > 0)) {
375 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
376 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
377 if (rc != len) {
378 LOG(ALERT) << "LMS: Device receive timed out";
379 delete[] buffer;
380 return false;
381 }
382
Harald Welte68f05412018-04-28 21:58:03 +0200383 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200384 }
385
386 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
387 delete[] buffer;
388 return true;
389}
390
Harald Welte940738e2018-03-07 07:50:57 +0100391bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
392{
393 int idx;
394
395 if (chan >= rx_paths.size()) {
396 LOG(ALERT) << "Requested non-existent channel " << chan;
397 return false;
398 }
399
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200400 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100401 if (idx < 0) {
402 LOG(ALERT) << "Invalid Rx Antenna";
403 return false;
404 }
405
406 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
407 LOG(ALERT) << "Unable to set Rx Antenna";
408 }
409
410 return true;
411}
412
413std::string LMSDevice::getRxAntenna(size_t chan)
414{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200415 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
416 int idx;
417
Harald Welte940738e2018-03-07 07:50:57 +0100418 if (chan >= rx_paths.size()) {
419 LOG(ALERT) << "Requested non-existent channel " << chan;
420 return "";
421 }
422
423 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
424 if (idx < 0) {
425 LOG(ALERT) << "Error getting Rx Antenna";
426 return "";
427 }
428
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200429 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100430 LOG(ALERT) << "Error getting Rx Antenna List";
431 return "";
432 }
433
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200434 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100435}
436
437bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
438{
439 int idx;
440
441 if (chan >= tx_paths.size()) {
442 LOG(ALERT) << "Requested non-existent channel " << chan;
443 return false;
444 }
445
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200446 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100447 if (idx < 0) {
448 LOG(ALERT) << "Invalid Rx Antenna";
449 return false;
450 }
451
452 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
453 LOG(ALERT) << "Unable to set Rx Antenna";
454 }
455
456 return true;
457}
458
459std::string LMSDevice::getTxAntenna(size_t chan)
460{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200461 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100462 int idx;
463
464 if (chan >= tx_paths.size()) {
465 LOG(ALERT) << "Requested non-existent channel " << chan;
466 return "";
467 }
468
469 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
470 if (idx < 0) {
471 LOG(ALERT) << "Error getting Tx Antenna";
472 return "";
473 }
474
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200475 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100476 LOG(ALERT) << "Error getting Tx Antenna List";
477 return "";
478 }
479
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200480 return name_list[idx];
481}
482
483bool LMSDevice::requiresRadioAlign()
484{
485 return false;
486}
487
488GSM::Time LMSDevice::minLatency() {
489 /* Empirical data from a handful of
490 relatively recent machines shows that the B100 will underrun when
491 the transmit threshold is reduced to a time of 6 and a half frames,
492 so we set a minimum 7 frame threshold. */
493 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100494}
495
496// NOTE: Assumes sequential reads
497int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
498 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
499{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200500 int rc = 0;
501 unsigned int i;
502 lms_stream_status_t status;
503 lms_stream_meta_t rx_metadata = {};
504 rx_metadata.flushPartialPacket = false;
505 rx_metadata.waitForTimestamp = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200506 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100507
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200508 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100509 LOG(ALERT) << "Invalid channel combination " << bufs.size();
510 return -1;
511 }
512
Harald Welte940738e2018-03-07 07:50:57 +0100513 *overrun = false;
514 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200515 for (i = 0; i<chans; i++) {
516 thread_enable_cancel(false);
517 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
Harald Welte79024862018-04-28 22:13:31 +0200518 if (timestamp != (TIMESTAMP)rx_metadata.timestamp)
519 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 +0200520 if (rc != len) {
521 LOG(ALERT) << "LMS: Device receive timed out";
522 }
Harald Welte940738e2018-03-07 07:50:57 +0100523
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200524 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
525 if (status.underrun > m_last_rx_underruns[i])
526 *underrun = true;
527 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100528
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200529 if (status.overrun > m_last_rx_overruns[i])
530 *overrun = true;
531 m_last_rx_overruns[i] = status.overrun;
532 }
533 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100534 }
535
536 samplesRead += rc;
537
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200538 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
539 rc = 0;
540
Harald Welte940738e2018-03-07 07:50:57 +0100541 return rc;
542}
543
544int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
545 bool * underrun, unsigned long long timestamp,
546 bool isControl)
547{
Harald Welte940738e2018-03-07 07:50:57 +0100548 int rc;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200549 unsigned int i;
550 lms_stream_status_t status;
551 lms_stream_meta_t tx_metadata = {};
552 tx_metadata.flushPartialPacket = false;
553 tx_metadata.waitForTimestamp = true;
Zydrunas Tamosevicius43f36782018-06-12 00:27:09 +0200554 tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */
Harald Welte940738e2018-03-07 07:50:57 +0100555
556 if (isControl) {
557 LOG(ERR) << "Control packets not supported";
558 return 0;
559 }
560
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200561 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100562 LOG(ALERT) << "Invalid channel combination " << bufs.size();
563 return -1;
564 }
565
Harald Welte940738e2018-03-07 07:50:57 +0100566 *underrun = false;
567
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200568 for (i = 0; i<chans; i++) {
Zydrunas Tamoseviciusfd268b62018-06-12 00:27:53 +0200569 LOG(DEBUG) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200570 thread_enable_cancel(false);
571 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
572 if (rc != len) {
573 LOG(ALERT) << "LMS: Device send timed out";
574 }
575
576 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
577 if (status.underrun > m_last_tx_underruns[i])
578 *underrun = true;
579 m_last_tx_underruns[i] = status.underrun;
580 }
581 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100582 }
583
584 samplesWritten += rc;
585
586 return rc;
587}
588
589bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
590{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200591 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100592}
593
594bool LMSDevice::setTxFreq(double wFreq, size_t chan)
595{
596
597 if (chan) {
598 LOG(ALERT) << "Invalid channel " << chan;
599 return false;
600 }
601
602 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
603 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
604 return false;
605 }
606
607 return true;
608}
609
610bool LMSDevice::setRxFreq(double wFreq, size_t chan)
611{
612 if (chan) {
613 LOG(ALERT) << "Invalid channel " << chan;
614 return false;
615 }
616
617 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
618 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
619 return false;
620 }
621
622 return true;
623}
624
625RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
626 InterfaceType iface, size_t chans, double offset,
627 const std::vector < std::string > &tx_paths,
628 const std::vector < std::string > &rx_paths)
629{
Harald Welte105a61e2018-06-13 21:55:09 +0200630 return new LMSDevice(tx_sps, chans, tx_paths, rx_paths);
Harald Welte940738e2018-03-07 07:50:57 +0100631}