blob: 2c56c6463c1b75e4030a59734b2e4f18d60b5ede [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,
Jeff Line41d52f2021-04-07 19:38:51 +080023 double min, double offset, const std::string& label, size_t tSize,
Lei YU7170a232021-02-04 16:19:27 +080024 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;
Cheng C Yang209ec562019-03-12 16:37:44 +080038 void handleResponse(const boost::system::error_code& err);
39 void checkThresholds(void) override;
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +020040 void updateMinMaxValues(void);
Lei YU7170a232021-02-04 16:19:27 +080041 unsigned int sensorPollMs = defaultSensorPollMs;
Cheng C Yang209ec562019-03-12 16:37:44 +080042
Cheng C Yanga97f1342020-02-11 15:10:41 +080043 int fd;
Cheng C Yang209ec562019-03-12 16:37:44 +080044 static constexpr size_t warnAfterErrorCount = 10;
Lei YU7170a232021-02-04 16:19:27 +080045
46 public:
47 static constexpr double defaultSensorPoll = 1.0;
48 static constexpr unsigned int defaultSensorPollMs =
49 static_cast<unsigned int>(defaultSensorPoll * 1000);
Cheng C Yang209ec562019-03-12 16:37:44 +080050};
51
52class PSUProperty
53{
54 public:
Jeff Line41d52f2021-04-07 19:38:51 +080055 PSUProperty(std::string name, double max, double min, unsigned int factor,
56 double offset) :
57 labelTypeName(std::move(name)),
58 maxReading(max), minReading(min), sensorScaleFactor(factor),
59 sensorOffset(offset)
James Feist38fb5982020-05-28 10:09:54 -070060 {}
Cheng C Yang209ec562019-03-12 16:37:44 +080061 ~PSUProperty() = default;
62
Cheng C Yange50345b2019-04-02 17:26:15 +080063 std::string labelTypeName;
Cheng C Yang209ec562019-03-12 16:37:44 +080064 double maxReading;
65 double minReading;
66 unsigned int sensorScaleFactor;
Jeff Line41d52f2021-04-07 19:38:51 +080067 double sensorOffset;
Cheng C Yang209ec562019-03-12 16:37:44 +080068};