diff options
author | Heikki Hannikainen <hessu@hes.iki.fi> | 2015-03-03 02:30:45 +0200 |
---|---|---|
committer | Dimitri Stolnikov <horiz0n@gmx.net> | 2015-06-01 22:01:02 +0200 |
commit | e847176f3ee43434eef7f6cfa1128983801f45c7 (patch) | |
tree | a13a0c7afba42baa6cc51c8253e7729678e1c34e /lib/hackrf/hackrf_source_c.cc | |
parent | dd6536757a3a2ce115491f12d730a592fbc07999 (diff) |
hackrf_source: Support selecting device with index number (hackrf=0)
Signed-off-by: Heikki Hannikainen <hessu@hes.iki.fi>
Diffstat (limited to 'lib/hackrf/hackrf_source_c.cc')
-rw-r--r-- | lib/hackrf/hackrf_source_c.cc | 35 |
1 files changed, 28 insertions, 7 deletions
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; |