1 #pragma once 2 3 #include "Thresholds.hpp" 4 #include "Utils.hpp" 5 #include "sensor.hpp" 6 7 #include <boost/asio/io_context.hpp> 8 #include <boost/asio/posix/stream_descriptor.hpp> 9 #include <boost/asio/steady_timer.hpp> 10 #include <boost/asio/streambuf.hpp> 11 #include <gpiod.hpp> 12 #include <phosphor-logging/lg2.hpp> 13 #include <sdbusplus/asio/connection.hpp> 14 #include <sdbusplus/asio/object_server.hpp> 15 16 #include <memory> 17 #include <optional> 18 #include <string> 19 #include <system_error> 20 #include <vector> 21 22 class BridgeGpio 23 { 24 public: BridgeGpio(const std::string & name,const int polarity,const float setupTime)25 BridgeGpio(const std::string& name, const int polarity, 26 const float setupTime) : 27 setupTimeMs(static_cast<unsigned int>(setupTime * 1000)) 28 { 29 line = gpiod::find_line(name); 30 if (!line) 31 { 32 lg2::error("Error finding gpio: '{NAME}'", "NAME", name); 33 } 34 else 35 { 36 try 37 { 38 line.request( 39 {"adcsensor", gpiod::line_request::DIRECTION_OUTPUT, 40 polarity == gpiod::line::ACTIVE_HIGH 41 ? 0 42 : gpiod::line_request::FLAG_ACTIVE_LOW}); 43 } 44 catch (const std::system_error&) 45 { 46 lg2::error("Error requesting gpio: '{NAME}'", "NAME", name); 47 } 48 } 49 } 50 set(int value)51 void set(int value) 52 { 53 if (line) 54 { 55 try 56 { 57 line.set_value(value); 58 } 59 catch (const std::system_error& exc) 60 { 61 lg2::error("Error set_value: '{EC}'", "EC", exc); 62 } 63 } 64 } 65 66 unsigned int setupTimeMs; 67 68 private: 69 gpiod::line line; 70 }; 71 72 class ADCSensor : public Sensor, public std::enable_shared_from_this<ADCSensor> 73 { 74 public: 75 ADCSensor(const std::string& path, 76 sdbusplus::asio::object_server& objectServer, 77 std::shared_ptr<sdbusplus::asio::connection>& conn, 78 boost::asio::io_context& io, const std::string& sensorName, 79 std::vector<thresholds::Threshold>&& thresholds, 80 double scaleFactor, float pollRate, PowerState readState, 81 const std::string& sensorConfiguration, 82 std::optional<BridgeGpio>&& bridgeGpio); 83 ~ADCSensor() override; 84 void setupRead(); 85 86 private: 87 sdbusplus::asio::object_server& objServer; 88 boost::asio::posix::stream_descriptor inputDev; 89 boost::asio::steady_timer waitTimer; 90 std::shared_ptr<boost::asio::streambuf> readBuf; 91 std::string path; 92 double scaleFactor; 93 unsigned int sensorPollMs; 94 std::optional<BridgeGpio> bridgeGpio; 95 thresholds::ThresholdTimer thresholdTimer; 96 void handleResponse(const boost::system::error_code& err); 97 void checkThresholds() override; 98 void restartRead(); 99 }; 100