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