Refactor HwmonTempSensor for Code Dedup and Adding Reading + Thresholds
Quick refactor to remove code duplication along with adding Reading + Thresholds for undetected sensors.
Since Thresholds and Reading Min/Max's are set by config, they dont need detection of the sensor to be populated.
#tlbmc
PiperOrigin-RevId: 805057334
Change-Id: If06048865005d76ead1f68e1768fd759961215be
diff --git a/tlbmc/sensors/hwmon_temp_sensor.cc b/tlbmc/sensors/hwmon_temp_sensor.cc
index 3d7adbd..2355597 100644
--- a/tlbmc/sensors/hwmon_temp_sensor.cc
+++ b/tlbmc/sensors/hwmon_temp_sensor.cc
@@ -80,6 +80,62 @@
return it->second;
}
+absl::flat_hash_map<std::string, std::string>
+HwmonTempSensor::CreateLabelToInputFileMap(
+ const boost::filesystem::path& hwmon_path) {
+ absl::flat_hash_map<std::string, std::string> label_to_input_file;
+ for (const auto& entry : boost::filesystem::directory_iterator(hwmon_path)) {
+ constexpr std::string_view kInputFileSuffix = "_input";
+ constexpr std::string_view kLabelSuffix = "_label";
+ if (entry.is_regular_file() &&
+ absl::EndsWith(entry.path().filename().string(), kInputFileSuffix)) {
+ // e.g., /sys/bus/i2c/devices/i2c-21/21-0040/hwmon/hwmon15/temp100_input
+ std::string input_file = entry.path().filename().string();
+ std::string file_prefix =
+ input_file.substr(0, input_file.size() - kInputFileSuffix.size());
+ std::string label_file = absl::StrCat(file_prefix, kLabelSuffix);
+ // Both input file and label file should exist
+ boost::filesystem::path label_path = hwmon_path / label_file;
+ if (boost::filesystem::exists(label_path)) {
+ std::ifstream file(label_path.string());
+ std::string label = {std::istreambuf_iterator<char>(file),
+ std::istreambuf_iterator<char>()};
+ label = std::string(absl::StripTrailingAsciiWhitespace(label));
+ label_to_input_file[label] = input_file;
+ } else if (boost::filesystem::exists(hwmon_path / input_file)) {
+ // If label file does not exist, use input file name prefix as label.
+ label_to_input_file[file_prefix] = input_file;
+ }
+ }
+ }
+ return label_to_input_file;
+}
+
+std::shared_ptr<HwmonTempSensor> HwmonTempSensor::CreateHwmonTempSensor(
+ const std::string& name, const std::string& label,
+ const std::string& input_dev_path, const HwmonTempSensorConfig& config,
+ const std::shared_ptr<boost::asio::io_context>& io_context,
+ std::optional<NotificationCb> on_batch_notify) {
+ std::string hwmon_temp_sensor_key = absl::StrCat("temperature_", name);
+
+ ThresholdConfigs threshold_configs;
+ if (auto it = config.label_to_thresholds().find(label);
+ it != config.label_to_thresholds().end()) {
+ threshold_configs = it->second;
+ }
+
+ ReadingRangeConfigs reading_range_configs;
+ if (auto it = config.label_to_reading_ranges().find(label);
+ it != config.label_to_reading_ranges().end()) {
+ reading_range_configs = it->second;
+ }
+
+ return std::make_shared<HwmonTempSensor>(
+ Token(), config.type(), input_dev_path, hwmon_temp_sensor_key,
+ config.i2c_common_config(), threshold_configs, reading_range_configs,
+ config.entity_common_config(), io_context, on_batch_notify);
+}
+
absl::StatusOr<std::vector<std::shared_ptr<Sensor>>> HwmonTempSensor::Create(
const HwmonTempSensorConfig& config,
const std::shared_ptr<boost::asio::io_context>& io_context,
@@ -108,13 +164,9 @@
LOG(ERROR) << "Failed to create HwmonTempSensor (NONFATAL): "
<< hwmon_path.status();
for (const auto& [label, name] : config.label_to_name()) {
- std::string hwmon_temp_sensor_key = absl::StrCat("temperature_", name);
std::shared_ptr<HwmonTempSensor> hwmon_temp_sensor =
- std::make_shared<HwmonTempSensor>(
- Token(), config.type(), "", hwmon_temp_sensor_key,
- config.i2c_common_config(), ThresholdConfigs(),
- ReadingRangeConfigs(), config.entity_common_config(), io_context,
- on_batch_notify);
+ CreateHwmonTempSensor(name, label, "", config, io_context,
+ on_batch_notify);
State state;
// If the sensor is not detected, we set its creation as pending otherwise
// set it to failed.
@@ -133,31 +185,8 @@
}
// Now find all labels and their input files.
- absl::flat_hash_map<std::string, std::string> label_to_input_file;
- for (const auto& entry : boost::filesystem::directory_iterator(*hwmon_path)) {
- constexpr std::string_view kInputFileSuffix = "_input";
- constexpr std::string_view kLabelSuffix = "_label";
- if (entry.is_regular_file() &&
- absl::EndsWith(entry.path().filename().string(), kInputFileSuffix)) {
- // e.g., /sys/bus/i2c/devices/i2c-21/21-0040/hwmon/hwmon15/temp100_input
- std::string input_file = entry.path().filename().string();
- std::string file_prefix =
- input_file.substr(0, input_file.size() - kInputFileSuffix.size());
- std::string label_file = absl::StrCat(file_prefix, kLabelSuffix);
- // Both input file and label file should exist
- boost::filesystem::path label_path = *hwmon_path / label_file;
- if (boost::filesystem::exists(label_path)) {
- std::ifstream file(label_path.string());
- std::string label = {std::istreambuf_iterator<char>(file),
- std::istreambuf_iterator<char>()};
- label = std::string(absl::StripTrailingAsciiWhitespace(label));
- label_to_input_file[label] = input_file;
- } else if (boost::filesystem::exists(*hwmon_path / input_file)) {
- // If label file does not exist, use input file name prefix as label.
- label_to_input_file[file_prefix] = input_file;
- }
- }
- }
+ absl::flat_hash_map<std::string, std::string> label_to_input_file =
+ CreateLabelToInputFileMap(*hwmon_path);
for (const auto& [label, name] : config.label_to_name()) {
boost::filesystem::path input_dev_path;
@@ -170,24 +199,9 @@
"Failed to find $0 in hwmon folder $1", label, hwmon_path->string()));
}
- ThresholdConfigs threshold_configs;
- if (auto it = config.label_to_thresholds().find(label);
- it != config.label_to_thresholds().end()) {
- threshold_configs = it->second;
- }
-
- ReadingRangeConfigs reading_range_configs;
- if (auto it = config.label_to_reading_ranges().find(label);
- it != config.label_to_reading_ranges().end()) {
- reading_range_configs = it->second;
- }
-
- std::string hwmon_temp_sensor_key = absl::StrCat("temperature_", name);
-
- sensors.push_back(std::make_shared<HwmonTempSensor>(
- Token(), config.type(), input_dev_path.string(), hwmon_temp_sensor_key,
- config.i2c_common_config(), threshold_configs, reading_range_configs,
- config.entity_common_config(), io_context, on_batch_notify));
+ sensors.push_back(CreateHwmonTempSensor(name, label,
+ input_dev_path.string(), config,
+ io_context, on_batch_notify));
}
return sensors;
diff --git a/tlbmc/sensors/hwmon_temp_sensor.h b/tlbmc/sensors/hwmon_temp_sensor.h
index 7f59246..07dcb84 100644
--- a/tlbmc/sensors/hwmon_temp_sensor.h
+++ b/tlbmc/sensors/hwmon_temp_sensor.h
@@ -9,8 +9,8 @@
#include <vector>
#include "absl/base/thread_annotations.h"
+#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h"
-#include "absl/synchronization/mutex.h"
#include "boost/asio.hpp" //NOLINT: boost::asio is commonly used in BMC
#include "boost/circular_buffer.hpp" //NOLINT: boost is commonly used in BMC
#include "entity_common_config.pb.h"
@@ -61,6 +61,15 @@
static absl::StatusOr<std::string_view> GetDriverName(
HwmonTempSensorType sensor_type);
+ static absl::flat_hash_map<std::string, std::string>
+ CreateLabelToInputFileMap(const boost::filesystem::path& hwmon_path);
+
+ static std::shared_ptr<HwmonTempSensor> CreateHwmonTempSensor(
+ const std::string& name, const std::string& label,
+ const std::string& input_dev_path, const HwmonTempSensorConfig& config,
+ const std::shared_ptr<boost::asio::io_context>& io_context,
+ std::optional<NotificationCb> on_batch_notify);
+
private:
class Token {
private: