blob: 523d5002e62530961fbad5e01a352c009ca405c8 [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;
Cheng C Yang209ec562019-03-12 16:37:44 +080033 unsigned int sensorFactor;
Jeff Line41d52f2021-04-07 19:38:51 +080034 double sensorOffset;
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000035 thresholds::ThresholdTimer thresholdTimer;
36 void restartRead();
Cheng C Yang209ec562019-03-12 16:37:44 +080037 void handleResponse(const boost::system::error_code& err);
38 void checkThresholds(void) override;
Lei YU7170a232021-02-04 16:19:27 +080039 unsigned int sensorPollMs = defaultSensorPollMs;
Cheng C Yang209ec562019-03-12 16:37:44 +080040
Cheng C Yanga97f1342020-02-11 15:10:41 +080041 int fd;
Cheng C Yang209ec562019-03-12 16:37:44 +080042 static constexpr size_t warnAfterErrorCount = 10;
Lei YU7170a232021-02-04 16:19:27 +080043
44 public:
45 static constexpr double defaultSensorPoll = 1.0;
46 static constexpr unsigned int defaultSensorPollMs =
47 static_cast<unsigned int>(defaultSensorPoll * 1000);
Cheng C Yang209ec562019-03-12 16:37:44 +080048};
49
50class PSUProperty
51{
52 public:
Jeff Line41d52f2021-04-07 19:38:51 +080053 PSUProperty(std::string name, double max, double min, unsigned int factor,
54 double offset) :
55 labelTypeName(std::move(name)),
56 maxReading(max), minReading(min), sensorScaleFactor(factor),
57 sensorOffset(offset)
James Feist38fb5982020-05-28 10:09:54 -070058 {}
Cheng C Yang209ec562019-03-12 16:37:44 +080059 ~PSUProperty() = default;
60
Cheng C Yange50345b2019-04-02 17:26:15 +080061 std::string labelTypeName;
Cheng C Yang209ec562019-03-12 16:37:44 +080062 double maxReading;
63 double minReading;
64 unsigned int sensorScaleFactor;
Jeff Line41d52f2021-04-07 19:38:51 +080065 double sensorOffset;
Cheng C Yang209ec562019-03-12 16:37:44 +080066};