Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame^] | 3 | #include "PwmSensor.hpp" |
| 4 | #include "Thresholds.hpp" |
| 5 | #include "sensor.hpp" |
| 6 | |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 7 | #include <boost/asio/random_access_file.hpp> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 8 | #include <sdbusplus/asio/object_server.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 9 | |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 10 | #include <array> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 11 | #include <memory> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 12 | #include <string> |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 13 | #include <utility> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 14 | |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame] | 15 | class PSUSensor : public Sensor, public std::enable_shared_from_this<PSUSensor> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 16 | { |
| 17 | public: |
| 18 | PSUSensor(const std::string& path, const std::string& objectType, |
| 19 | sdbusplus::asio::object_server& objectServer, |
| 20 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 21 | boost::asio::io_service& io, const std::string& sensorName, |
| 22 | std::vector<thresholds::Threshold>&& thresholds, |
| 23 | const std::string& sensorConfiguration, |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 24 | const PowerState& powerState, const std::string& sensorUnits, |
| 25 | unsigned int factor, double max, double min, double offset, |
| 26 | const std::string& label, size_t tSize, double pollRate); |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 27 | ~PSUSensor() override; |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame] | 28 | void setupRead(void); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 29 | |
| 30 | private: |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 31 | // Note, this buffer is a shared_ptr because during a read, its lifetime |
| 32 | // might have to outlive the PSUSensor class if the object gets destroyed |
| 33 | // while in the middle of a read operation |
| 34 | std::shared_ptr<std::array<char, 128>> buffer; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 35 | sdbusplus::asio::object_server& objServer; |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 36 | boost::asio::random_access_file inputDev; |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 37 | boost::asio::steady_timer waitTimer; |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 38 | std::string path; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 39 | unsigned int sensorFactor; |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 40 | double sensorOffset; |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 41 | thresholds::ThresholdTimer thresholdTimer; |
| 42 | void restartRead(); |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 43 | void handleResponse(const boost::system::error_code& err, size_t bytesRead); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 44 | void checkThresholds(void) override; |
Lei YU | 7170a23 | 2021-02-04 16:19:27 +0800 | [diff] [blame] | 45 | unsigned int sensorPollMs = defaultSensorPollMs; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 46 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 47 | static constexpr size_t warnAfterErrorCount = 10; |
Lei YU | 7170a23 | 2021-02-04 16:19:27 +0800 | [diff] [blame] | 48 | |
| 49 | public: |
| 50 | static constexpr double defaultSensorPoll = 1.0; |
| 51 | static constexpr unsigned int defaultSensorPollMs = |
| 52 | static_cast<unsigned int>(defaultSensorPoll * 1000); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | class PSUProperty |
| 56 | { |
| 57 | public: |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 58 | PSUProperty(std::string name, double max, double min, unsigned int factor, |
| 59 | double offset) : |
| 60 | labelTypeName(std::move(name)), |
| 61 | maxReading(max), minReading(min), sensorScaleFactor(factor), |
| 62 | sensorOffset(offset) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 63 | {} |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 64 | ~PSUProperty() = default; |
| 65 | |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 66 | std::string labelTypeName; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 67 | double maxReading; |
| 68 | double minReading; |
| 69 | unsigned int sensorScaleFactor; |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 70 | double sensorOffset; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 71 | }; |