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