blob: dfd8713de1f8ebe476804a79b36828ba952b20d2 [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>
Patrick Venturefd6ba732019-10-31 14:27:39 -07007#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -04008#include <optional>
James Feist6714a252018-09-10 15:26:18 -07009#include <sdbusplus/asio/object_server.hpp>
Patrick Venturefd6ba732019-10-31 14:27:39 -070010#include <stdexcept>
11#include <string>
12#include <vector>
James Feist6714a252018-09-10 15:26:18 -070013
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070014class BridgeGpio
15{
16 public:
17 BridgeGpio(const std::string& name, const int polarity)
18 {
19 line = gpiod::find_line(name);
20 if (!line)
21 {
22 std::cerr << "Error finding gpio: " << name << "\n";
23 }
24 else
25 {
26 try
27 {
28 line.request({"adcsensor",
29 gpiod::line_request::DIRECTION_OUTPUT,
30 polarity == gpiod::line::ACTIVE_HIGH
31 ? 0
32 : gpiod::line_request::FLAG_ACTIVE_LOW});
33 }
34 catch (std::system_error&)
35 {
36 std::cerr << "Error requesting gpio: " << name << "\n";
37 }
38 }
39 }
40
41 void set(int value)
42 {
43 if (line)
44 {
45 line.set_value(value);
46 }
47 }
48
49 private:
50 gpiod::line line;
51};
52
James Feist251c7822018-09-12 12:54:15 -070053class ADCSensor : public Sensor
James Feist6714a252018-09-10 15:26:18 -070054{
55 public:
James Feistd8705872019-02-08 13:26:09 -080056 ADCSensor(const std::string& path,
57 sdbusplus::asio::object_server& objectServer,
58 std::shared_ptr<sdbusplus::asio::connection>& conn,
59 boost::asio::io_service& io, const std::string& sensorName,
60 std::vector<thresholds::Threshold>&& thresholds,
James Feist71d31b22019-01-02 16:57:54 -080061 const double scaleFactor, PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040062 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070063 std::optional<BridgeGpio>&& bridgeGpio);
James Feist6714a252018-09-10 15:26:18 -070064 ~ADCSensor();
65
66 private:
James Feistd8705872019-02-08 13:26:09 -080067 sdbusplus::asio::object_server& objServer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070068 boost::asio::posix::stream_descriptor inputDev;
69 boost::asio::deadline_timer waitTimer;
70 boost::asio::streambuf readBuf;
James Feist930fcde2019-05-28 12:58:43 -070071 std::string path;
Brad Bishopfbb44ad2019-11-08 09:42:37 -050072 size_t errCount;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070073 double scaleFactor;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070074 std::optional<BridgeGpio> bridgeGpio;
James Feist71d31b22019-01-02 16:57:54 -080075 PowerState readState;
James Feist46342ec2019-03-06 14:03:41 -080076 thresholds::ThresholdTimer thresholdTimer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070077 void setupRead(void);
James Feistd8705872019-02-08 13:26:09 -080078 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080079 void checkThresholds(void) override;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070080};