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