xref: /openbmc/dbus-sensors/src/psu/PSUSensor.hpp (revision d7be555e)
1 #pragma once
2 
3 #include "DeviceMgmt.hpp"
4 #include "PwmSensor.hpp"
5 #include "Thresholds.hpp"
6 #include "sensor.hpp"
7 
8 #include <boost/asio/random_access_file.hpp>
9 #include <sdbusplus/asio/object_server.hpp>
10 
11 #include <array>
12 #include <memory>
13 #include <string>
14 #include <utility>
15 
16 class PSUSensor : public Sensor, public std::enable_shared_from_this<PSUSensor>
17 {
18   public:
19     PSUSensor(const std::string& path, const std::string& objectType,
20               sdbusplus::asio::object_server& objectServer,
21               std::shared_ptr<sdbusplus::asio::connection>& conn,
22               boost::asio::io_context& io, const std::string& sensorName,
23               std::vector<thresholds::Threshold>&& thresholds,
24               const std::string& sensorConfiguration,
25               const PowerState& powerState, const std::string& sensorUnits,
26               unsigned int factor, double max, double min, double offset,
27               const std::string& label, size_t tSize, double pollRate,
28               const std::shared_ptr<I2CDevice>& i2cDevice);
29     ~PSUSensor() override;
30     void setupRead();
31     void activate(const std::string& newPath,
32                   const std::shared_ptr<I2CDevice>& newI2CDevice);
33     void deactivate();
34     bool isActive();
35 
getI2CDevice() const36     std::shared_ptr<I2CDevice> getI2CDevice() const
37     {
38         return i2cDevice;
39     }
40 
41   private:
42     // Note, this buffer is a shared_ptr because during a read, its lifetime
43     // might have to outlive the PSUSensor class if the object gets destroyed
44     // while in the middle of a read operation
45     std::shared_ptr<std::array<char, 128>> buffer;
46     std::shared_ptr<I2CDevice> i2cDevice;
47     sdbusplus::asio::object_server& objServer;
48     boost::asio::random_access_file inputDev;
49     boost::asio::steady_timer waitTimer;
50     std::string path;
51     unsigned int sensorFactor;
52     double sensorOffset;
53     thresholds::ThresholdTimer thresholdTimer;
54     void restartRead();
55     void handleResponse(const boost::system::error_code& err, size_t bytesRead);
56     void checkThresholds() override;
57     unsigned int sensorPollMs = defaultSensorPollMs;
58 
59     static constexpr size_t warnAfterErrorCount = 10;
60 
61   public:
62     static constexpr double defaultSensorPoll = 1.0;
63     static constexpr unsigned int defaultSensorPollMs =
64         static_cast<unsigned int>(defaultSensorPoll * 1000);
65 };
66 
67 class PSUProperty
68 {
69   public:
PSUProperty(std::string name,double max,double min,unsigned int factor,double offset)70     PSUProperty(std::string name, double max, double min, unsigned int factor,
71                 double offset) :
72         labelTypeName(std::move(name)), maxReading(max), minReading(min),
73         sensorScaleFactor(factor), sensorOffset(offset)
74     {}
75     ~PSUProperty() = default;
76 
77     std::string labelTypeName;
78     double maxReading;
79     double minReading;
80     unsigned int sensorScaleFactor;
81     double sensorOffset;
82 };
83