blob: cb595c9b42e03b04aafab2d03007ba9f42fa844a [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
2
Patrick Ventureca44b2f2019-10-31 11:02:26 -07003#include "Thresholds.hpp"
4#include "sensor.hpp"
5
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -07006#include <gpiod.hpp>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -04007#include <optional>
James Feist6714a252018-09-10 15:26:18 -07008#include <sdbusplus/asio/object_server.hpp>
9
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070010class BridgeGpio
11{
12 public:
13 BridgeGpio(const std::string& name, const int polarity)
14 {
15 line = gpiod::find_line(name);
16 if (!line)
17 {
18 std::cerr << "Error finding gpio: " << name << "\n";
19 }
20 else
21 {
22 try
23 {
24 line.request({"adcsensor",
25 gpiod::line_request::DIRECTION_OUTPUT,
26 polarity == gpiod::line::ACTIVE_HIGH
27 ? 0
28 : gpiod::line_request::FLAG_ACTIVE_LOW});
29 }
30 catch (std::system_error&)
31 {
32 std::cerr << "Error requesting gpio: " << name << "\n";
33 }
34 }
35 }
36
37 void set(int value)
38 {
39 if (line)
40 {
41 line.set_value(value);
42 }
43 }
44
45 private:
46 gpiod::line line;
47};
48
James Feist251c7822018-09-12 12:54:15 -070049class ADCSensor : public Sensor
James Feist6714a252018-09-10 15:26:18 -070050{
51 public:
James Feistd8705872019-02-08 13:26:09 -080052 ADCSensor(const std::string& path,
53 sdbusplus::asio::object_server& objectServer,
54 std::shared_ptr<sdbusplus::asio::connection>& conn,
55 boost::asio::io_service& io, const std::string& sensorName,
56 std::vector<thresholds::Threshold>&& thresholds,
James Feist71d31b22019-01-02 16:57:54 -080057 const double scaleFactor, PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040058 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070059 std::optional<BridgeGpio>&& bridgeGpio);
James Feist6714a252018-09-10 15:26:18 -070060 ~ADCSensor();
61
62 private:
James Feistd8705872019-02-08 13:26:09 -080063 sdbusplus::asio::object_server& objServer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070064 boost::asio::posix::stream_descriptor inputDev;
65 boost::asio::deadline_timer waitTimer;
66 boost::asio::streambuf readBuf;
James Feist930fcde2019-05-28 12:58:43 -070067 std::string path;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070068 int errCount;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070069 double scaleFactor;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070070 std::optional<BridgeGpio> bridgeGpio;
James Feist71d31b22019-01-02 16:57:54 -080071 PowerState readState;
James Feist46342ec2019-03-06 14:03:41 -080072 thresholds::ThresholdTimer thresholdTimer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070073 void setupRead(void);
James Feistd8705872019-02-08 13:26:09 -080074 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080075 void checkThresholds(void) override;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070076};