1e73bd0a1SAndrew Jeffery #pragma once 2e73bd0a1SAndrew Jeffery 3*cafd72f6SMatt Simmering #include "DeviceMgmt.hpp" 4e73bd0a1SAndrew Jeffery #include "PwmSensor.hpp" 5e73bd0a1SAndrew Jeffery #include "Thresholds.hpp" 6e73bd0a1SAndrew Jeffery #include "sensor.hpp" 7e73bd0a1SAndrew Jeffery 8e73bd0a1SAndrew Jeffery #include <boost/asio/random_access_file.hpp> 9e73bd0a1SAndrew Jeffery #include <sdbusplus/asio/object_server.hpp> 10e73bd0a1SAndrew Jeffery 11e73bd0a1SAndrew Jeffery #include <array> 12e73bd0a1SAndrew Jeffery #include <memory> 13e73bd0a1SAndrew Jeffery #include <string> 14e73bd0a1SAndrew Jeffery #include <utility> 15e73bd0a1SAndrew Jeffery 16e73bd0a1SAndrew Jeffery class PSUSensor : public Sensor, public std::enable_shared_from_this<PSUSensor> 17e73bd0a1SAndrew Jeffery { 18e73bd0a1SAndrew Jeffery public: 19e73bd0a1SAndrew Jeffery PSUSensor(const std::string& path, const std::string& objectType, 20e73bd0a1SAndrew Jeffery sdbusplus::asio::object_server& objectServer, 21e73bd0a1SAndrew Jeffery std::shared_ptr<sdbusplus::asio::connection>& conn, 221f978631SEd Tanous boost::asio::io_context& io, const std::string& sensorName, 23e73bd0a1SAndrew Jeffery std::vector<thresholds::Threshold>&& thresholds, 24e73bd0a1SAndrew Jeffery const std::string& sensorConfiguration, 25e73bd0a1SAndrew Jeffery const PowerState& powerState, const std::string& sensorUnits, 26e73bd0a1SAndrew Jeffery unsigned int factor, double max, double min, double offset, 27*cafd72f6SMatt Simmering const std::string& label, size_t tSize, double pollRate, 28*cafd72f6SMatt Simmering const std::shared_ptr<I2CDevice>& i2cDevice); 29e73bd0a1SAndrew Jeffery ~PSUSensor() override; 30e73bd0a1SAndrew Jeffery void setupRead(void); 31*cafd72f6SMatt Simmering void activate(const std::string& newPath, 32*cafd72f6SMatt Simmering const std::shared_ptr<I2CDevice>& newI2CDevice); 33*cafd72f6SMatt Simmering void deactivate(void); 34*cafd72f6SMatt Simmering bool isActive(void); 35*cafd72f6SMatt Simmering 36*cafd72f6SMatt Simmering std::shared_ptr<I2CDevice> getI2CDevice() const 37*cafd72f6SMatt Simmering { 38*cafd72f6SMatt Simmering return i2cDevice; 39*cafd72f6SMatt Simmering } 40e73bd0a1SAndrew Jeffery 41e73bd0a1SAndrew Jeffery private: 42e73bd0a1SAndrew Jeffery // Note, this buffer is a shared_ptr because during a read, its lifetime 43e73bd0a1SAndrew Jeffery // might have to outlive the PSUSensor class if the object gets destroyed 44e73bd0a1SAndrew Jeffery // while in the middle of a read operation 45e73bd0a1SAndrew Jeffery std::shared_ptr<std::array<char, 128>> buffer; 46*cafd72f6SMatt Simmering std::shared_ptr<I2CDevice> i2cDevice; 47e73bd0a1SAndrew Jeffery sdbusplus::asio::object_server& objServer; 48e73bd0a1SAndrew Jeffery boost::asio::random_access_file inputDev; 49e73bd0a1SAndrew Jeffery boost::asio::steady_timer waitTimer; 50e73bd0a1SAndrew Jeffery std::string path; 51e73bd0a1SAndrew Jeffery unsigned int sensorFactor; 52e73bd0a1SAndrew Jeffery double sensorOffset; 53e73bd0a1SAndrew Jeffery thresholds::ThresholdTimer thresholdTimer; 54e73bd0a1SAndrew Jeffery void restartRead(); 55e73bd0a1SAndrew Jeffery void handleResponse(const boost::system::error_code& err, size_t bytesRead); 56e73bd0a1SAndrew Jeffery void checkThresholds(void) override; 57e73bd0a1SAndrew Jeffery unsigned int sensorPollMs = defaultSensorPollMs; 58e73bd0a1SAndrew Jeffery 59e73bd0a1SAndrew Jeffery static constexpr size_t warnAfterErrorCount = 10; 60e73bd0a1SAndrew Jeffery 61e73bd0a1SAndrew Jeffery public: 62e73bd0a1SAndrew Jeffery static constexpr double defaultSensorPoll = 1.0; 63e73bd0a1SAndrew Jeffery static constexpr unsigned int defaultSensorPollMs = 64e73bd0a1SAndrew Jeffery static_cast<unsigned int>(defaultSensorPoll * 1000); 65e73bd0a1SAndrew Jeffery }; 66e73bd0a1SAndrew Jeffery 67e73bd0a1SAndrew Jeffery class PSUProperty 68e73bd0a1SAndrew Jeffery { 69e73bd0a1SAndrew Jeffery public: 70e73bd0a1SAndrew Jeffery PSUProperty(std::string name, double max, double min, unsigned int factor, 71e73bd0a1SAndrew Jeffery double offset) : 72e73bd0a1SAndrew Jeffery labelTypeName(std::move(name)), 73e73bd0a1SAndrew Jeffery maxReading(max), minReading(min), sensorScaleFactor(factor), 74e73bd0a1SAndrew Jeffery sensorOffset(offset) 75e73bd0a1SAndrew Jeffery {} 76e73bd0a1SAndrew Jeffery ~PSUProperty() = default; 77e73bd0a1SAndrew Jeffery 78e73bd0a1SAndrew Jeffery std::string labelTypeName; 79e73bd0a1SAndrew Jeffery double maxReading; 80e73bd0a1SAndrew Jeffery double minReading; 81e73bd0a1SAndrew Jeffery unsigned int sensorScaleFactor; 82e73bd0a1SAndrew Jeffery double sensorOffset; 83e73bd0a1SAndrew Jeffery }; 84