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