Browse Source

PlutoSDR: keep scan keys unique when a device has several IP addresses

A Pluto reachable both on its USB gadget network (192.168.2.1) and on a
LAN address is reported as two "ip" contexts. Both got the same key
(<serial>_ip), so m_serialMap kept one of them arbitrarily and selecting
"PlutoSDR0 (ip)" could open the wrong address.

When keys collide, append the address to the backend part of the key,
e.g. <serial>_ip:192.168.50.20, which also shows in the device name. A
device with a single address keeps its usual key, so saved device
references still match. Exact duplicate URIs are skipped, and the
serial and URI maps are now cleared on each scan instead of keeping
entries from previous scans.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
chirpmodem-v8.0.0
Marcel 1 month ago
parent
commit
1fa4355c5e
  1. 32
      devices/plutosdr/deviceplutosdrscan.cpp
  2. 1
      devices/plutosdr/deviceplutosdrscan.h

32
devices/plutosdr/deviceplutosdrscan.cpp

@ -90,6 +90,8 @@ void DevicePlutoSDRScan::scan() @@ -90,6 +90,8 @@ void DevicePlutoSDRScan::scan()
#endif
m_scans.clear();
m_serialMap.clear();
m_urilMap.clear();
m_scans.reserve(num_contexts);
qDebug("PlutoSDRScan::scan: found %d contexts", num_contexts);
@ -128,7 +130,7 @@ void DevicePlutoSDRScan::scan() @@ -128,7 +130,7 @@ void DevicePlutoSDRScan::scan()
(descriptionView.find("PlutoSDR") != std::string_view::npos) ||
(descriptionView.find("AD93") != std::string_view::npos);
if (isPlutoDescription)
if (isPlutoDescription && (m_urilMap.find(fixedUri) == m_urilMap.end())) // skip exact duplicates
{
// As device scan is used across multiple vectors it's best to use a
// managed pointer, as to keep track of when it's safe to delete.
@ -150,11 +152,12 @@ void DevicePlutoSDRScan::scan() @@ -150,11 +152,12 @@ void DevicePlutoSDRScan::scan()
// A device can be discovered through multiple backends (e.g. USB and IP).
// Keep the backend in the serial key so each scanned entry remains unique.
m_scans.back()->m_serial = desc_match[1].str() + createBackendSuffix(uri);
m_serialMap[m_scans.back()->m_serial] = m_scans.back();
}
}
}
disambiguateSerials();
#ifdef LIBIIO_V1
iio_scan_destroy(scan_ctx);
#else
@ -163,6 +166,31 @@ void DevicePlutoSDRScan::scan() @@ -163,6 +166,31 @@ void DevicePlutoSDRScan::scan()
#endif
}
void DevicePlutoSDRScan::disambiguateSerials()
{
// The same device can be reachable at several addresses on one backend (e.g. both the
// USB gadget network and the LAN). Append the address to the backend part of the key
// only when it collides, so a device with a single address keeps its usual key.
std::map<std::string, int> keyCounts;
for (const auto& scan : m_scans) {
keyCounts[scan->m_serial]++;
}
for (const auto& scan : m_scans)
{
if (scan->m_serial == "TBD") { // no serial in the description: not addressable by serial
continue;
}
if ((keyCounts[scan->m_serial] > 1) && (scan->m_uri.rfind("ip:", 0) == 0)) {
scan->m_serial += ":" + scan->m_uri.substr(3);
}
m_serialMap[scan->m_serial] = scan;
}
}
const std::string* DevicePlutoSDRScan::getURIAt(unsigned int index) const
{
if (index < m_scans.size()) {

1
devices/plutosdr/deviceplutosdrscan.h

@ -58,6 +58,7 @@ private: @@ -58,6 +58,7 @@ private:
std::string createBackendSuffix(const char *uri) const;
QString getBackendLabel(const std::string& serial) const;
std::string getPhysicalSerial(const std::string& serial) const;
void disambiguateSerials();
std::vector<std::shared_ptr<DeviceScan>> m_scans;
std::map<std::string, std::shared_ptr<DeviceScan>> m_serialMap;

Loading…
Cancel
Save