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