hackrf_source: Support selecting device with index number (hackrf=0)

Signed-off-by: Heikki Hannikainen <hessu@hes.iki.fi>
This commit is contained in:
Heikki Hannikainen 2015-03-03 02:30:45 +02:00 committed by Dimitri Stolnikov
parent dd6536757a
commit e847176f3e
1 changed files with 28 additions and 7 deletions

View File

@ -102,13 +102,10 @@ hackrf_source_c::hackrf_source_c (const std::string &args)
_bandwidth(0)
{
int ret;
std::string *hackrf_serial = NULL;
std::string hackrf_serial;
dict_t dict = params_to_dict(args);
if (dict.count("hackrf") && dict["hackrf"].length() > 0)
hackrf_serial = &dict["hackrf"];
_buf_num = _buf_len = _buf_head = _buf_used = _buf_offset = 0;
if (dict.count("buffers"))
@ -146,10 +143,34 @@ hackrf_source_c::hackrf_source_c (const std::string &args)
}
_dev = NULL;
if ( hackrf_serial )
ret = hackrf_open_by_serial( hackrf_serial->c_str(), &_dev );
else
if (dict.count("hackrf") && dict["hackrf"].length() > 0) {
hackrf_serial = dict["hackrf"];
if (hackrf_serial.length() > 1) {
ret = hackrf_open_by_serial( hackrf_serial.c_str(), &_dev );
} else {
unsigned int dev_index = 0;
try {
dev_index = boost::lexical_cast< unsigned int >( hackrf_serial );
} catch ( std::exception &ex ) {
throw std::runtime_error(
"Failed to use '" + hackrf_serial + "' as HackRF device index: " + ex.what());
}
hackrf_device_list_t *list = hackrf_device_list();
if (dev_index < list->devicecount) {
ret = hackrf_device_list_open(list, dev_index, &_dev);
} else {
hackrf_device_list_free(list);
throw std::runtime_error(
"Failed to use '" + hackrf_serial + "' as HackRF device index: not enough devices");
}
hackrf_device_list_free(list);
}
} else
ret = hackrf_open( &_dev );
HACKRF_THROW_ON_ERROR(ret, "Failed to open HackRF device")
uint8_t board_id;