blob: d4ad60138c4f96dfd7d51657b299a6d9f2c3bb64 [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>
James Feist38fb5982020-05-28 10:09:54 -07007#include <sdbusplus/asio/object_server.hpp>
8
Patrick Venturefd6ba732019-10-31 14:27:39 -07009#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040010#include <optional>
Patrick Venturefd6ba732019-10-31 14:27:39 -070011#include <stdexcept>
12#include <string>
13#include <vector>
James Feist6714a252018-09-10 15:26:18 -070014
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070015class BridgeGpio
16{
17 public:
18 BridgeGpio(const std::string& name, const int polarity)
19 {
20 line = gpiod::find_line(name);
21 if (!line)
22 {
23 std::cerr << "Error finding gpio: " << name << "\n";
24 }
25 else
26 {
27 try
28 {
29 line.request({"adcsensor",
30 gpiod::line_request::DIRECTION_OUTPUT,
31 polarity == gpiod::line::ACTIVE_HIGH
32 ? 0
33 : gpiod::line_request::FLAG_ACTIVE_LOW});
34 }
35 catch (std::system_error&)
36 {
37 std::cerr << "Error requesting gpio: " << name << "\n";
38 }
39 }
40 }
41
42 void set(int value)
43 {
44 if (line)
45 {
Yong Li17aba772020-04-09 15:48:46 +080046 try
47 {
48 line.set_value(value);
49 }
50 catch (std::system_error& exc)
51 {
52 std::cerr << "Error set_value: " << exc.what() << "\n";
53 }
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070054 }
55 }
56
57 private:
58 gpiod::line line;
59};
60
Yong Li1afda6b2020-04-09 19:24:13 +080061class ADCSensor : public Sensor, public std::enable_shared_from_this<ADCSensor>
James Feist6714a252018-09-10 15:26:18 -070062{
63 public:
James Feistd8705872019-02-08 13:26:09 -080064 ADCSensor(const std::string& path,
65 sdbusplus::asio::object_server& objectServer,
66 std::shared_ptr<sdbusplus::asio::connection>& conn,
67 boost::asio::io_service& io, const std::string& sensorName,
68 std::vector<thresholds::Threshold>&& thresholds,
James Feist71d31b22019-01-02 16:57:54 -080069 const double scaleFactor, PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040070 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070071 std::optional<BridgeGpio>&& bridgeGpio);
James Feist6714a252018-09-10 15:26:18 -070072 ~ADCSensor();
Yong Li1afda6b2020-04-09 19:24:13 +080073 void setupRead(void);
James Feist6714a252018-09-10 15:26:18 -070074
75 private:
James Feistd8705872019-02-08 13:26:09 -080076 sdbusplus::asio::object_server& objServer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070077 boost::asio::posix::stream_descriptor inputDev;
78 boost::asio::deadline_timer waitTimer;
Yong Li1afda6b2020-04-09 19:24:13 +080079 std::shared_ptr<boost::asio::streambuf> readBuf;
James Feist930fcde2019-05-28 12:58:43 -070080 std::string path;
Brad Bishopfbb44ad2019-11-08 09:42:37 -050081 size_t errCount;
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 Feist71d31b22019-01-02 16:57:54 -080084 PowerState readState;
James Feist46342ec2019-03-06 14:03:41 -080085 thresholds::ThresholdTimer thresholdTimer;
James Feistd8705872019-02-08 13:26:09 -080086 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080087 void checkThresholds(void) override;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088};