Fan: Aggregate I2C bus and address related code
This reduced the duplicated controls and the scope of several
variables.
Tested: the i2c fan sensor is the same as before.
Change-Id: I9834b6e91edf624380ae35c81ed84c4f0c73094d
Signed-off-by: Yong Zhao <yozhao@google.com>
diff --git a/src/FanMain.cpp b/src/FanMain.cpp
index 0b598d9..a34dd4d 100644
--- a/src/FanMain.cpp
+++ b/src/FanMain.cpp
@@ -173,22 +173,6 @@
fs::path directory = path.parent_path();
FanTypes fanType = getFanType(directory);
- size_t bus = 0;
- size_t address = 0;
- if (fanType == FanTypes::i2c)
- {
- std::string link =
- fs::read_symlink(directory / "device").filename();
-
- size_t findDash = link.find('-');
- if (findDash == std::string::npos ||
- link.size() <= findDash + 1)
- {
- std::cerr << "Error finding device from symlink";
- }
- bus = std::stoi(link.substr(0, findDash));
- address = std::stoi(link.substr(findDash + 1), nullptr, 16);
- }
// convert to 0 based
size_t index = std::stoul(indexStr) - 1;
@@ -235,6 +219,22 @@
}
if (fanType == FanTypes::i2c)
{
+ size_t bus = 0;
+ size_t address = 0;
+
+ std::string link =
+ fs::read_symlink(directory / "device").filename();
+
+ size_t findDash = link.find('-');
+ if (findDash == std::string::npos ||
+ link.size() <= findDash + 1)
+ {
+ std::cerr << "Error finding device from symlink";
+ }
+ bus = std::stoi(link.substr(0, findDash));
+ address =
+ std::stoi(link.substr(findDash + 1), nullptr, 16);
+
auto findBus = baseConfiguration->second.find("Bus");
auto findAddress =
baseConfiguration->second.find("Address");