| #ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_SENSORS_PSU_SENSOR_H_ |
| #define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_SENSORS_PSU_SENSOR_H_ |
| |
| #include <cstddef> |
| #include <memory> |
| #include <optional> |
| #include <string> |
| #include <string_view> |
| #include <tuple> |
| #include <vector> |
| |
| #include "absl/base/thread_annotations.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" |
| #include "i2c_common_config.pb.h" |
| #include "psu_sensor_config.pb.h" |
| #include "reading_range_config.pb.h" |
| #include "threshold_config.pb.h" |
| #include "tlbmc/hal/sysfs/i2c.h" |
| #include "sensor.pb.h" |
| #include "tlbmc/sensors/i2c_hwmon_based_sensor.h" |
| #include "tlbmc/sensors/sensor.h" |
| |
| namespace milotic_tlbmc { |
| |
| class PsuSensor : public I2cHwmonBasedSensor { |
| private: |
| class Token; |
| |
| public: |
| struct ReadingProperties { |
| std::string_view label_type_name; |
| double max_reading; |
| double min_reading; |
| double scale; |
| double offset; |
| bool operator==(const ReadingProperties& other) const { |
| return std::tie(max_reading, min_reading, scale, offset) == |
| std::tie(other.max_reading, other.min_reading, other.scale, |
| other.offset); |
| } |
| }; |
| // Load the device if not already loaded and returns a list of sensors that |
| // can be used to read the temperature from the device. Note: |
| // 1. The `io_context` must run only on a single thread. |
| static absl::StatusOr<std::vector<std::shared_ptr<Sensor>>> Create( |
| const PsuSensorConfig& config, |
| const std::shared_ptr<boost::asio::io_context>& io_context, |
| const I2cSysfs& i2c_sysfs, |
| std::optional<NotificationCb> on_batch_notify = std::nullopt); |
| |
| PsuSensor(Token token, PsuSensorType sensor_type, SensorUnit sensor_unit, |
| const std::string& input_dev_path, const std::string& sensor_name, |
| const ThresholdConfigs& threshold_configs, |
| const ReadingRangeConfigs& reading_range_configs, |
| const EntityCommonConfig& entity_common_config, double scale, |
| double offset, |
| const std::shared_ptr<boost::asio::io_context>& io_context, |
| std::optional<NotificationCb> on_batch_notify); |
| |
| ~PsuSensor() override = default; |
| |
| void HandleRefreshResult(const boost::system::error_code& error, |
| size_t bytes_read) override |
| ABSL_LOCKS_EXCLUDED(sensor_data_mutex_); |
| |
| protected: |
| PsuSensor() = default; |
| |
| // Returns the driver name for the given sensor type. |
| static absl::StatusOr<std::string_view> GetDriverName( |
| PsuSensorType sensor_type); |
| |
| // Given the config and the properties of the sensor, returns the reading |
| // range configs. |
| static absl::StatusOr<ReadingRangeConfigs> GetReadingRangeConfigs( |
| const ReadingRangeConfigs& configs, const ReadingProperties& properties); |
| |
| // Given a label with digits (e.g., "vout1"), returns the reading properties |
| // of the sensor. |
| static absl::StatusOr<ReadingProperties> GetPsuSensorProperties( |
| std::string_view label); |
| |
| // Given an input file, returns the unit of the sensor. |
| static absl::StatusOr<SensorUnit> GetSensorUnit(std::string_view input_file); |
| |
| private: |
| class Token { |
| private: |
| explicit Token() = default; |
| friend PsuSensor; |
| }; |
| |
| const PsuSensorType sensor_type_ = PSU_SENSOR_TYPE_UNKNOWN; |
| const std::string sensor_name_; |
| const std::string board_config_name_; |
| const double scale_ = 1.0; |
| const double offset_ = 0.0; |
| }; |
| |
| } // namespace milotic_tlbmc |
| |
| #endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_SENSORS_PSU_SENSOR_H_ |