blob: 0aa2c9ee748028f92ce9981efb70d9b57b639f40 [file] [log] [blame]
Cheng C Yang209ec562019-03-12 16:37:44 +08001#pragma once
2
Matt Simmeringcafd72f2022-12-16 15:35:12 -08003#include "DeviceMgmt.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10304#include "Thresholds.hpp"
Ed Tanous18b61862025-01-30 10:56:28 -08005#include "Utils.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10306#include "sensor.hpp"
7
Ed Tanous18b61862025-01-30 10:56:28 -08008#include <boost/asio/io_context.hpp>
Ed Tanous16966b52021-09-15 15:06:59 -07009#include <boost/asio/random_access_file.hpp>
Ed Tanous18b61862025-01-30 10:56:28 -080010#include <boost/asio/steady_timer.hpp>
11#include <sdbusplus/asio/connection.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080012#include <sdbusplus/asio/object_server.hpp>
James Feist38fb5982020-05-28 10:09:54 -070013
Ed Tanous16966b52021-09-15 15:06:59 -070014#include <array>
Ed Tanous18b61862025-01-30 10:56:28 -080015#include <cstddef>
James Feist38fb5982020-05-28 10:09:54 -070016#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070017#include <string>
Ed Tanous8a57ec02020-10-09 12:46:52 -070018#include <utility>
Ed Tanous18b61862025-01-30 10:56:28 -080019#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080020
Yong Libf8b1da2020-04-15 16:32:50 +080021class PSUSensor : public Sensor, public std::enable_shared_from_this<PSUSensor>
Cheng C Yang209ec562019-03-12 16:37:44 +080022{
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 Tanous1f978632023-02-28 18:16:39 -080027 boost::asio::io_context& io, const std::string& sensorName,
Cheng C Yang209ec562019-03-12 16:37:44 +080028 std::vector<thresholds::Threshold>&& thresholds,
29 const std::string& sensorConfiguration,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000030 const PowerState& powerState, const std::string& sensorUnits,
31 unsigned int factor, double max, double min, double offset,
Matt Simmeringcafd72f2022-12-16 15:35:12 -080032 const std::string& label, size_t tSize, double pollRate,
33 const std::shared_ptr<I2CDevice>& i2cDevice);
Ed Tanous8a57ec02020-10-09 12:46:52 -070034 ~PSUSensor() override;
Ed Tanous201a1012024-04-03 18:07:28 -070035 void setupRead();
Matt Simmeringcafd72f2022-12-16 15:35:12 -080036 void activate(const std::string& newPath,
37 const std::shared_ptr<I2CDevice>& newI2CDevice);
Ed Tanous201a1012024-04-03 18:07:28 -070038 void deactivate();
39 bool isActive();
Matt Simmeringcafd72f2022-12-16 15:35:12 -080040
41 std::shared_ptr<I2CDevice> getI2CDevice() const
42 {
43 return i2cDevice;
44 }
Cheng C Yang209ec562019-03-12 16:37:44 +080045
46 private:
Ed Tanous16966b52021-09-15 15:06:59 -070047 // 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 Simmeringcafd72f2022-12-16 15:35:12 -080051 std::shared_ptr<I2CDevice> i2cDevice;
Cheng C Yang209ec562019-03-12 16:37:44 +080052 sdbusplus::asio::object_server& objServer;
Ed Tanous16966b52021-09-15 15:06:59 -070053 boost::asio::random_access_file inputDev;
Ed Tanous9b4a20e2022-09-06 08:47:11 -070054 boost::asio::steady_timer waitTimer;
James Feist930fcde2019-05-28 12:58:43 -070055 std::string path;
Cheng C Yang209ec562019-03-12 16:37:44 +080056 unsigned int sensorFactor;
Jeff Line41d52f2021-04-07 19:38:51 +080057 double sensorOffset;
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000058 thresholds::ThresholdTimer thresholdTimer;
59 void restartRead();
Ed Tanous16966b52021-09-15 15:06:59 -070060 void handleResponse(const boost::system::error_code& err, size_t bytesRead);
Ed Tanous201a1012024-04-03 18:07:28 -070061 void checkThresholds() override;
Lei YU7170a232021-02-04 16:19:27 +080062 unsigned int sensorPollMs = defaultSensorPollMs;
Cheng C Yang209ec562019-03-12 16:37:44 +080063
Cheng C Yang209ec562019-03-12 16:37:44 +080064 static constexpr size_t warnAfterErrorCount = 10;
Lei YU7170a232021-02-04 16:19:27 +080065
66 public:
67 static constexpr double defaultSensorPoll = 1.0;
68 static constexpr unsigned int defaultSensorPollMs =
69 static_cast<unsigned int>(defaultSensorPoll * 1000);
Cheng C Yang209ec562019-03-12 16:37:44 +080070};
71
72class PSUProperty
73{
74 public:
Jeff Line41d52f2021-04-07 19:38:51 +080075 PSUProperty(std::string name, double max, double min, unsigned int factor,
76 double offset) :
Patrick Williams2aaf7172024-08-16 15:20:40 -040077 labelTypeName(std::move(name)), maxReading(max), minReading(min),
78 sensorScaleFactor(factor), sensorOffset(offset)
James Feist38fb5982020-05-28 10:09:54 -070079 {}
Cheng C Yang209ec562019-03-12 16:37:44 +080080 ~PSUProperty() = default;
81
Cheng C Yange50345b2019-04-02 17:26:15 +080082 std::string labelTypeName;
Cheng C Yang209ec562019-03-12 16:37:44 +080083 double maxReading;
84 double minReading;
85 unsigned int sensorScaleFactor;
Jeff Line41d52f2021-04-07 19:38:51 +080086 double sensorOffset;
Cheng C Yang209ec562019-03-12 16:37:44 +080087};