blob: af2439682eebd84928692bf8895f4764f632b410 [file] [log] [blame]
Cheng C Yang209ec562019-03-12 16:37:44 +08001#pragma once
2
Ed Tanous8a57ec02020-10-09 12:46:52 -07003#include <PwmSensor.hpp>
4#include <Thresholds.hpp>
James Feist8086aba2020-08-25 16:00:59 -07005#include <boost/asio/streambuf.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +08006#include <sdbusplus/asio/object_server.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -07007#include <sensor.hpp>
James Feist38fb5982020-05-28 10:09:54 -07008
9#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070010#include <string>
Ed Tanous8a57ec02020-10-09 12:46:52 -070011#include <utility>
Cheng C Yang209ec562019-03-12 16:37:44 +080012
Yong Libf8b1da2020-04-15 16:32:50 +080013class PSUSensor : public Sensor, public std::enable_shared_from_this<PSUSensor>
Cheng C Yang209ec562019-03-12 16:37:44 +080014{
15 public:
16 PSUSensor(const std::string& path, const std::string& objectType,
17 sdbusplus::asio::object_server& objectServer,
18 std::shared_ptr<sdbusplus::asio::connection>& conn,
19 boost::asio::io_service& io, const std::string& sensorName,
20 std::vector<thresholds::Threshold>&& thresholds,
21 const std::string& sensorConfiguration,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000022 const PowerState& powerState, const std::string& sensorUnits,
23 unsigned int factor, double max, double min, double offset,
24 const std::string& label, size_t tSize, double pollRate);
Ed Tanous8a57ec02020-10-09 12:46:52 -070025 ~PSUSensor() override;
Yong Libf8b1da2020-04-15 16:32:50 +080026 void setupRead(void);
Cheng C Yang209ec562019-03-12 16:37:44 +080027
28 private:
29 sdbusplus::asio::object_server& objServer;
30 boost::asio::posix::stream_descriptor inputDev;
31 boost::asio::deadline_timer waitTimer;
James Feist930fcde2019-05-28 12:58:43 -070032 std::string path;
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +020033 std::string pathRatedMax;
Brad Bishopfeb19ef2019-11-07 18:02:16 -050034 std::string pathRatedMin;
Cheng C Yang209ec562019-03-12 16:37:44 +080035 unsigned int sensorFactor;
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +020036 uint8_t minMaxReadCounter;
Jeff Line41d52f2021-04-07 19:38:51 +080037 double sensorOffset;
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000038 thresholds::ThresholdTimer thresholdTimer;
39 void restartRead();
Cheng C Yang209ec562019-03-12 16:37:44 +080040 void handleResponse(const boost::system::error_code& err);
41 void checkThresholds(void) override;
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +020042 void updateMinMaxValues(void);
Lei YU7170a232021-02-04 16:19:27 +080043 unsigned int sensorPollMs = defaultSensorPollMs;
Cheng C Yang209ec562019-03-12 16:37:44 +080044
Cheng C Yanga97f1342020-02-11 15:10:41 +080045 int fd;
Cheng C Yang209ec562019-03-12 16:37:44 +080046 static constexpr size_t warnAfterErrorCount = 10;
Lei YU7170a232021-02-04 16:19:27 +080047
48 public:
49 static constexpr double defaultSensorPoll = 1.0;
50 static constexpr unsigned int defaultSensorPollMs =
51 static_cast<unsigned int>(defaultSensorPoll * 1000);
Cheng C Yang209ec562019-03-12 16:37:44 +080052};
53
54class PSUProperty
55{
56 public:
Jeff Line41d52f2021-04-07 19:38:51 +080057 PSUProperty(std::string name, double max, double min, unsigned int factor,
58 double offset) :
59 labelTypeName(std::move(name)),
60 maxReading(max), minReading(min), sensorScaleFactor(factor),
61 sensorOffset(offset)
James Feist38fb5982020-05-28 10:09:54 -070062 {}
Cheng C Yang209ec562019-03-12 16:37:44 +080063 ~PSUProperty() = default;
64
Cheng C Yange50345b2019-04-02 17:26:15 +080065 std::string labelTypeName;
Cheng C Yang209ec562019-03-12 16:37:44 +080066 double maxReading;
67 double minReading;
68 unsigned int sensorScaleFactor;
Jeff Line41d52f2021-04-07 19:38:51 +080069 double sensorOffset;
Cheng C Yang209ec562019-03-12 16:37:44 +080070};