From 1fa4355c5e3469f8917073a7eed3709980aa81b9 Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 18 Oct 2026 20:51:09 -0400 Subject: [PATCH] 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 (_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. _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 --- devices/plutosdr/deviceplutosdrscan.cpp | 32 +++++++++++++++++++++++-- devices/plutosdr/deviceplutosdrscan.h | 1 + 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/devices/plutosdr/deviceplutosdrscan.cpp b/devices/plutosdr/deviceplutosdrscan.cpp index e8d4e1821..50ced1685 100644 --- a/devices/plutosdr/deviceplutosdrscan.cpp +++ b/devices/plutosdr/deviceplutosdrscan.cpp @@ -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() (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() // 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() #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 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()) { diff --git a/devices/plutosdr/deviceplutosdrscan.h b/devices/plutosdr/deviceplutosdrscan.h index eda8f79b9..73f6c6e28 100644 --- a/devices/plutosdr/deviceplutosdrscan.h +++ b/devices/plutosdr/deviceplutosdrscan.h @@ -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> m_scans; std::map> m_serialMap;