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