blob: 64661791b99314b50882ef254cc6d477b842f33c [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>
Ed Tanous16966b52021-09-15 15:06:59 -07005#include <boost/asio/random_access_file.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
Ed Tanous16966b52021-09-15 15:06:59 -07009#include <array>
James Feist38fb5982020-05-28 10:09:54 -070010#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070011#include <string>
Ed Tanous8a57ec02020-10-09 12:46:52 -070012#include <utility>
Cheng C Yang209ec562019-03-12 16:37:44 +080013
Yong Libf8b1da2020-04-15 16:32:50 +080014class PSUSensor : public Sensor, public std::enable_shared_from_this<PSUSensor>
Cheng C Yang209ec562019-03-12 16:37:44 +080015{
16 public:
17 PSUSensor(const std::string& path, const std::string& objectType,
18 sdbusplus::asio::object_server& objectServer,
19 std::shared_ptr<sdbusplus::asio::connection>& conn,
20 boost::asio::io_service& io, const std::string& sensorName,
21 std::vector<thresholds::Threshold>&& thresholds,
22 const std::string& sensorConfiguration,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000023 const PowerState& powerState, const std::string& sensorUnits,
24 unsigned int factor, double max, double min, double offset,
25 const std::string& label, size_t tSize, double pollRate);
Ed Tanous8a57ec02020-10-09 12:46:52 -070026 ~PSUSensor() override;
Yong Libf8b1da2020-04-15 16:32:50 +080027 void setupRead(void);
Cheng C Yang209ec562019-03-12 16:37:44 +080028
29 private:
Ed Tanous16966b52021-09-15 15:06:59 -070030 // Note, this buffer is a shared_ptr because during a read, its lifetime
31 // might have to outlive the PSUSensor class if the object gets destroyed
32 // while in the middle of a read operation
33 std::shared_ptr<std::array<char, 128>> buffer;
Cheng C Yang209ec562019-03-12 16:37:44 +080034 sdbusplus::asio::object_server& objServer;
Ed Tanous16966b52021-09-15 15:06:59 -070035 boost::asio::random_access_file inputDev;
Cheng C Yang209ec562019-03-12 16:37:44 +080036 boost::asio::deadline_timer waitTimer;
James Feist930fcde2019-05-28 12:58:43 -070037 std::string path;
Cheng C Yang209ec562019-03-12 16:37:44 +080038 unsigned int sensorFactor;
Jeff Line41d52f2021-04-07 19:38:51 +080039 double sensorOffset;
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000040 thresholds::ThresholdTimer thresholdTimer;
41 void restartRead();
Ed Tanous16966b52021-09-15 15:06:59 -070042 void handleResponse(const boost::system::error_code& err, size_t bytesRead);
Cheng C Yang209ec562019-03-12 16:37:44 +080043 void checkThresholds(void) override;
Lei YU7170a232021-02-04 16:19:27 +080044 unsigned int sensorPollMs = defaultSensorPollMs;
Cheng C Yang209ec562019-03-12 16:37:44 +080045
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};