diff --git a/lib/hackrf/hackrf_source_c.cc b/lib/hackrf/hackrf_source_c.cc index e9e6891..33ab62d 100644 --- a/lib/hackrf/hackrf_source_c.cc +++ b/lib/hackrf/hackrf_source_c.cc @@ -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;