blob: 800e1fc69ae2d45318cf2076cafeb43e4f3cd119 [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
James Feist8086aba2020-08-25 16:00:59 -07006#include <boost/asio/streambuf.hpp>
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -07007#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -07008#include <sdbusplus/asio/object_server.hpp>
9
Patrick Venturefd6ba732019-10-31 14:27:39 -070010#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040011#include <optional>
Patrick Venturefd6ba732019-10-31 14:27:39 -070012#include <stdexcept>
13#include <string>
14#include <vector>
James Feist6714a252018-09-10 15:26:18 -070015
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070016class BridgeGpio
17{
18 public:
19 BridgeGpio(const std::string& name, const int polarity)
20 {
21 line = gpiod::find_line(name);
22 if (!line)
23 {
24 std::cerr << "Error finding gpio: " << name << "\n";
25 }
26 else
27 {
28 try
29 {
30 line.request({"adcsensor",
31 gpiod::line_request::DIRECTION_OUTPUT,
32 polarity == gpiod::line::ACTIVE_HIGH
33 ? 0
34 : gpiod::line_request::FLAG_ACTIVE_LOW});
35 }
36 catch (std::system_error&)
37 {
38 std::cerr << "Error requesting gpio: " << name << "\n";
39 }
40 }
41 }
42
43 void set(int value)
44 {
45 if (line)
46 {
Yong Li17aba772020-04-09 15:48:46 +080047 try
48 {
49 line.set_value(value);
50 }
51 catch (std::system_error& exc)
52 {
53 std::cerr << "Error set_value: " << exc.what() << "\n";
54 }
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070055 }
56 }
57
58 private:
59 gpiod::line line;
60};
61
Yong Li1afda6b2020-04-09 19:24:13 +080062class ADCSensor : public Sensor, public std::enable_shared_from_this<ADCSensor>
James Feist6714a252018-09-10 15:26:18 -070063{
64 public:
James Feistd8705872019-02-08 13:26:09 -080065 ADCSensor(const std::string& path,
66 sdbusplus::asio::object_server& objectServer,
67 std::shared_ptr<sdbusplus::asio::connection>& conn,
68 boost::asio::io_service& io, const std::string& sensorName,
69 std::vector<thresholds::Threshold>&& thresholds,
James Feist71d31b22019-01-02 16:57:54 -080070 const double scaleFactor, PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040071 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070072 std::optional<BridgeGpio>&& bridgeGpio);
James Feist6714a252018-09-10 15:26:18 -070073 ~ADCSensor();
Yong Li1afda6b2020-04-09 19:24:13 +080074 void setupRead(void);
James Feist6714a252018-09-10 15:26:18 -070075
76 private:
James Feistd8705872019-02-08 13:26:09 -080077 sdbusplus::asio::object_server& objServer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070078 boost::asio::posix::stream_descriptor inputDev;
79 boost::asio::deadline_timer waitTimer;
Yong Li1afda6b2020-04-09 19:24:13 +080080 std::shared_ptr<boost::asio::streambuf> readBuf;
James Feist930fcde2019-05-28 12:58:43 -070081 std::string path;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070082 double scaleFactor;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070083 std::optional<BridgeGpio> bridgeGpio;
James Feist46342ec2019-03-06 14:03:41 -080084 thresholds::ThresholdTimer thresholdTimer;
James Feistd8705872019-02-08 13:26:09 -080085 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080086 void checkThresholds(void) override;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070087};