dbus-sensors: utils: Utility to get device bus & addr from dev name.
This is applicable to all the services which rely on "bus-addr" fmt.
1. FanSensor
2. HwmonTempSensor
3. IntelCPUSensor
4. PSUSensor
In addition this would also fix Fansensor Daemon crashes due to
stoi() exceptions weren't caught earlier.
For example: In a FanSensor Daemon -
Device of f0103000.pwm-fan-controller would be classified as i2cfan
based on new way of defining fan type.
Hence when we parse string for bus-addr, bus=f0103000.pwm and
addr=fan-controller for which stoi() would crash.
This would be true for all non I2c devices defaulting to I2cFan type.
Solution is to use 'std::from_chars' which handles under/overflow
properly. Centralizing this now in Utils would also allow us to manage
this appropriatelty across various services.
Tested:
Tested sanity of all daemons in the system and they work as expected.
Change-Id: I546e967abae7c0fb9fca645867e3037046037647
Signed-off-by: Akshit Shah <shahakshit@google.com>
diff --git a/tests/test_Utils.cpp b/tests/test_Utils.cpp
index 8a52699..67fc3a5 100644
--- a/tests/test_Utils.cpp
+++ b/tests/test_Utils.cpp
@@ -173,3 +173,53 @@
EXPECT_TRUE(ret);
EXPECT_EQ(foundPaths.size(), 3U);
}
+
+TEST(GetDeviceBusAddrTest, DevNameInvalid)
+{
+ size_t bus = 0;
+ size_t addr = 0;
+ std::string devName;
+
+ auto ret = getDeviceBusAddr(devName, bus, addr);
+ EXPECT_FALSE(ret);
+
+ devName = "NoHyphen";
+ ret = getDeviceBusAddr(devName, bus, addr);
+ EXPECT_FALSE(ret);
+
+ devName = "pwm-fan";
+ ret = getDeviceBusAddr(devName, bus, addr);
+ EXPECT_FALSE(ret);
+}
+
+TEST(GetDeviceBusAddrTest, BusInvalid)
+{
+ size_t bus = 0;
+ size_t addr = 0;
+ std::string devName = "FF-00FF";
+
+ auto ret = getDeviceBusAddr(devName, bus, addr);
+ EXPECT_FALSE(ret);
+}
+
+TEST(GetDeviceBusAddrTest, AddrInvalid)
+{
+ size_t bus = 0;
+ size_t addr = 0;
+ std::string devName = "12-fan";
+
+ auto ret = getDeviceBusAddr(devName, bus, addr);
+ EXPECT_FALSE(ret);
+}
+
+TEST(GetDeviceBusAddrTest, AllValid)
+{
+ size_t bus = 0;
+ size_t addr = 0;
+ std::string devName = "12-00af";
+
+ auto ret = getDeviceBusAddr(devName, bus, addr);
+ EXPECT_TRUE(ret);
+ EXPECT_EQ(bus, 12);
+ EXPECT_EQ(addr, 0xaf);
+}