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/src/Utils.cpp b/src/Utils.cpp
index d659d37..ef70c1b 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -850,3 +850,31 @@
}
return setupPropertiesChangedMatches(bus, {types}, handler);
}
+
+bool getDeviceBusAddr(const std::string& deviceName, size_t& bus, size_t& addr)
+{
+ auto findHyphen = deviceName.find('-');
+ if (findHyphen == std::string::npos)
+ {
+ std::cerr << "found bad device " << deviceName << "\n";
+ return false;
+ }
+ std::string busStr = deviceName.substr(0, findHyphen);
+ std::string addrStr = deviceName.substr(findHyphen + 1);
+
+ std::from_chars_result res{};
+ res = std::from_chars(&*busStr.begin(), &*busStr.end(), bus);
+ if (res.ec != std::errc{} || res.ptr != &*busStr.end())
+ {
+ std::cerr << "Error finding bus for " << deviceName << "\n";
+ return false;
+ }
+ res = std::from_chars(&*addrStr.begin(), &*addrStr.end(), addr, 16);
+ if (res.ec != std::errc{} || res.ptr != &*addrStr.end())
+ {
+ std::cerr << "Error finding addr for " << deviceName << "\n";
+ return false;
+ }
+
+ return true;
+}