blob: 9bc2e83bdbe0082c1cb6642abaa401d8154863f9 [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,
Zev Weiss6b6891c2021-04-22 02:46:21 -050022 const std::string& sensorUnits, unsigned int factor, double max,
Lei YU7170a232021-02-04 16:19:27 +080023 double min, const std::string& label, size_t tSize,
24 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;
Cheng C Yang209ec562019-03-12 16:37:44 +080037 void handleResponse(const boost::system::error_code& err);
38 void checkThresholds(void) override;
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +020039 void updateMinMaxValues(void);
Lei YU7170a232021-02-04 16:19:27 +080040 unsigned int sensorPollMs = defaultSensorPollMs;
Cheng C Yang209ec562019-03-12 16:37:44 +080041
Cheng C Yanga97f1342020-02-11 15:10:41 +080042 int fd;
Cheng C Yang209ec562019-03-12 16:37:44 +080043 static constexpr size_t warnAfterErrorCount = 10;
Lei YU7170a232021-02-04 16:19:27 +080044
45 public:
46 static constexpr double defaultSensorPoll = 1.0;
47 static constexpr unsigned int defaultSensorPollMs =
48 static_cast<unsigned int>(defaultSensorPoll * 1000);
Cheng C Yang209ec562019-03-12 16:37:44 +080049};
50
51class PSUProperty
52{
53 public:
54 PSUProperty(std::string name, double max, double min, unsigned int factor) :
Ed Tanous8a57ec02020-10-09 12:46:52 -070055 labelTypeName(std::move(name)), maxReading(max), minReading(min),
Cheng C Yang209ec562019-03-12 16:37:44 +080056 sensorScaleFactor(factor)
James Feist38fb5982020-05-28 10:09:54 -070057 {}
Cheng C Yang209ec562019-03-12 16:37:44 +080058 ~PSUProperty() = default;
59
Cheng C Yange50345b2019-04-02 17:26:15 +080060 std::string labelTypeName;
Cheng C Yang209ec562019-03-12 16:37:44 +080061 double maxReading;
62 double minReading;
63 unsigned int sensorScaleFactor;
64};