blob: e061138976e9bb50ba793316856237a6232ff9cc [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
2
3#include <Thresholds.hpp>
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -07004#include <gpiod.hpp>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -04005#include <optional>
James Feist6714a252018-09-10 15:26:18 -07006#include <sdbusplus/asio/object_server.hpp>
James Feist251c7822018-09-12 12:54:15 -07007#include <sensor.hpp>
James Feist6714a252018-09-10 15:26:18 -07008
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -07009class BridgeGpio
10{
11 public:
12 BridgeGpio(const std::string& name, const int polarity)
13 {
14 line = gpiod::find_line(name);
15 if (!line)
16 {
17 std::cerr << "Error finding gpio: " << name << "\n";
18 }
19 else
20 {
21 try
22 {
23 line.request({"adcsensor",
24 gpiod::line_request::DIRECTION_OUTPUT,
25 polarity == gpiod::line::ACTIVE_HIGH
26 ? 0
27 : gpiod::line_request::FLAG_ACTIVE_LOW});
28 }
29 catch (std::system_error&)
30 {
31 std::cerr << "Error requesting gpio: " << name << "\n";
32 }
33 }
34 }
35
36 void set(int value)
37 {
38 if (line)
39 {
40 line.set_value(value);
41 }
42 }
43
44 private:
45 gpiod::line line;
46};
47
James Feist251c7822018-09-12 12:54:15 -070048class ADCSensor : public Sensor
James Feist6714a252018-09-10 15:26:18 -070049{
50 public:
James Feistd8705872019-02-08 13:26:09 -080051 ADCSensor(const std::string& path,
52 sdbusplus::asio::object_server& objectServer,
53 std::shared_ptr<sdbusplus::asio::connection>& conn,
54 boost::asio::io_service& io, const std::string& sensorName,
55 std::vector<thresholds::Threshold>&& thresholds,
James Feist71d31b22019-01-02 16:57:54 -080056 const double scaleFactor, PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040057 const std::string& sensorConfiguration,
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070058 std::optional<BridgeGpio> bridgeGpio);
James Feist6714a252018-09-10 15:26:18 -070059 ~ADCSensor();
60
61 private:
James Feistd8705872019-02-08 13:26:09 -080062 sdbusplus::asio::object_server& objServer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070063 boost::asio::posix::stream_descriptor inputDev;
64 boost::asio::deadline_timer waitTimer;
65 boost::asio::streambuf readBuf;
James Feist930fcde2019-05-28 12:58:43 -070066 std::string path;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070067 int errCount;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070068 double scaleFactor;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070069 std::optional<BridgeGpio> bridgeGpio;
James Feist71d31b22019-01-02 16:57:54 -080070 PowerState readState;
James Feist46342ec2019-03-06 14:03:41 -080071 thresholds::ThresholdTimer thresholdTimer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070072 void setupRead(void);
James Feistd8705872019-02-08 13:26:09 -080073 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080074 void checkThresholds(void) override;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070075};