aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/utils
diff options
context:
space:
mode:
authorLane Kolbly <lane.kolbly@ni.com>2020-04-06 11:17:29 -0500
committerAaron Rossetto <aaron.rossetto@ni.com>2020-04-08 15:13:53 -0500
commitabf025f0a5c537f0b2ab07da933ffb82f62418ef (patch)
treebd877c33a6264794c2b92d96970d2984b25c7cba /host/lib/utils
parentd398fe465724a3c2efb6abb1c720fc9f3a037701 (diff)
downloaduhd-abf025f0a5c537f0b2ab07da933ffb82f62418ef.tar.gz
uhd-abf025f0a5c537f0b2ab07da933ffb82f62418ef.tar.bz2
uhd-abf025f0a5c537f0b2ab07da933ffb82f62418ef.zip
uhd: Add fuzzy serial number checking
We have integer 32-bit serial numbers for MPM devices, for example "1234abcd". For serial numbers which have less than eight digits, e.g. "123abcd", a user may feel inclined to prefix this number with a 0 when they are searching for devices, e.g. "0123abcd". This change makes it so that specifying "0123abcd" will match a device with serial number "123ABCD".
Diffstat (limited to 'host/lib/utils')
-rw-r--r--host/lib/utils/CMakeLists.txt1
-rw-r--r--host/lib/utils/serial_number.cpp23
2 files changed, 24 insertions, 0 deletions
diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt
index 5d1d22e9b..aa06e2c45 100644
--- a/host/lib/utils/CMakeLists.txt
+++ b/host/lib/utils/CMakeLists.txt
@@ -214,6 +214,7 @@ LIBUHD_APPEND_SOURCES(
${CMAKE_CURRENT_SOURCE_DIR}/pathslib.cpp
${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp
${CMAKE_CURRENT_SOURCE_DIR}/prefs.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/serial_number.cpp
${CMAKE_CURRENT_SOURCE_DIR}/static.cpp
${CMAKE_CURRENT_SOURCE_DIR}/system_time.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tasks.cpp
diff --git a/host/lib/utils/serial_number.cpp b/host/lib/utils/serial_number.cpp
new file mode 100644
index 000000000..7fd0863d6
--- /dev/null
+++ b/host/lib/utils/serial_number.cpp
@@ -0,0 +1,23 @@
+//
+// Copyright 2020 Ettus Research, a National Instruments Brand
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+
+#include <uhdlib/utils/serial_number.hpp>
+#include <stdexcept>
+#include <string>
+
+namespace uhd
+{
+ bool serial_numbers_match(const std::string& serial_a, const std::string& serial_b)
+ {
+ try {
+ const uint32_t a = std::stoi(serial_a, 0, 16);
+ const uint32_t b = std::stoi(serial_b, 0, 16);
+ return a == b;
+ } catch (std::out_of_range& e) {
+ return false;
+ }
+ }
+}