blob: 49e181ee511594d948b5b0ae66bcdff7e5dbe529 [file] [log] [blame]
Cheng C Yang209ec562019-03-12 16:37:44 +08001#pragma once
2
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10303#include "PwmSensor.hpp"
4#include "Thresholds.hpp"
5#include "sensor.hpp"
6
Ed Tanous16966b52021-09-15 15:06:59 -07007#include <boost/asio/random_access_file.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +08008#include <sdbusplus/asio/object_server.hpp>
James Feist38fb5982020-05-28 10:09:54 -07009
Ed Tanous16966b52021-09-15 15:06:59 -070010#include <array>
James Feist38fb5982020-05-28 10:09:54 -070011#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070012#include <string>
Ed Tanous8a57ec02020-10-09 12:46:52 -070013#include <utility>
Cheng C Yang209ec562019-03-12 16:37:44 +080014
Yong Libf8b1da2020-04-15 16:32:50 +080015class PSUSensor : public Sensor, public std::enable_shared_from_this<PSUSensor>
Cheng C Yang209ec562019-03-12 16:37:44 +080016{
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,
Ed Tanous1f978632023-02-28 18:16:39 -080021 boost::asio::io_context& io, const std::string& sensorName,
Cheng C Yang209ec562019-03-12 16:37:44 +080022 std::vector<thresholds::Threshold>&& thresholds,
23 const std::string& sensorConfiguration,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000024 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);
Ed Tanous8a57ec02020-10-09 12:46:52 -070027 ~PSUSensor() override;
Yong Libf8b1da2020-04-15 16:32:50 +080028 void setupRead(void);
Cheng C Yang209ec562019-03-12 16:37:44 +080029
30 private:
Ed Tanous16966b52021-09-15 15:06:59 -070031 // 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;
Cheng C Yang209ec562019-03-12 16:37:44 +080035 sdbusplus::asio::object_server& objServer;
Ed Tanous16966b52021-09-15 15:06:59 -070036 boost::asio::random_access_file inputDev;
Ed Tanous9b4a20e2022-09-06 08:47:11 -070037 boost::asio::steady_timer waitTimer;
James Feist930fcde2019-05-28 12:58:43 -070038 std::string path;
Cheng C Yang209ec562019-03-12 16:37:44 +080039 unsigned int sensorFactor;
Jeff Line41d52f2021-04-07 19:38:51 +080040 double sensorOffset;
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000041 thresholds::ThresholdTimer thresholdTimer;
42 void restartRead();
Ed Tanous16966b52021-09-15 15:06:59 -070043 void handleResponse(const boost::system::error_code& err, size_t bytesRead);
Cheng C Yang209ec562019-03-12 16:37:44 +080044 void checkThresholds(void) override;
Lei YU7170a232021-02-04 16:19:27 +080045 unsigned int sensorPollMs = defaultSensorPollMs;
Cheng C Yang209ec562019-03-12 16:37:44 +080046
Cheng C Yang209ec562019-03-12 16:37:44 +080047 static constexpr size_t warnAfterErrorCount = 10;
Lei YU7170a232021-02-04 16:19:27 +080048
49 public:
50 static constexpr double defaultSensorPoll = 1.0;
51 static constexpr unsigned int defaultSensorPollMs =
52 static_cast<unsigned int>(defaultSensorPoll * 1000);
Cheng C Yang209ec562019-03-12 16:37:44 +080053};
54
55class PSUProperty
56{
57 public:
Jeff Line41d52f2021-04-07 19:38:51 +080058 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)
James Feist38fb5982020-05-28 10:09:54 -070063 {}
Cheng C Yang209ec562019-03-12 16:37:44 +080064 ~PSUProperty() = default;
65
Cheng C Yange50345b2019-04-02 17:26:15 +080066 std::string labelTypeName;
Cheng C Yang209ec562019-03-12 16:37:44 +080067 double maxReading;
68 double minReading;
69 unsigned int sensorScaleFactor;
Jeff Line41d52f2021-04-07 19:38:51 +080070 double sensorOffset;
Cheng C Yang209ec562019-03-12 16:37:44 +080071};