blob: e1f133041e0806b32a829445e8ffb35cb85ea3c9 [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 {
Yong Li17aba772020-04-09 15:48:46 +080045 try
46 {
47 line.set_value(value);
48 }
49 catch (std::system_error& exc)
50 {
51 std::cerr << "Error set_value: " << exc.what() << "\n";
52 }
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070053 }
54 }
55
56 private:
57 gpiod::line line;
58};
59
James Feist251c7822018-09-12 12:54:15 -070060class ADCSensor : public Sensor
James Feist6714a252018-09-10 15:26:18 -070061{
62 public:
James Feistd8705872019-02-08 13:26:09 -080063 ADCSensor(const std::string& path,
64 sdbusplus::asio::object_server& objectServer,
65 std::shared_ptr<sdbusplus::asio::connection>& conn,
66 boost::asio::io_service& io, const std::string& sensorName,
67 std::vector<thresholds::Threshold>&& thresholds,
James Feist71d31b22019-01-02 16:57:54 -080068 const double scaleFactor, PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040069 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070070 std::optional<BridgeGpio>&& bridgeGpio);
James Feist6714a252018-09-10 15:26:18 -070071 ~ADCSensor();
72
73 private:
James Feistd8705872019-02-08 13:26:09 -080074 sdbusplus::asio::object_server& objServer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070075 boost::asio::posix::stream_descriptor inputDev;
76 boost::asio::deadline_timer waitTimer;
77 boost::asio::streambuf readBuf;
James Feist930fcde2019-05-28 12:58:43 -070078 std::string path;
Brad Bishopfbb44ad2019-11-08 09:42:37 -050079 size_t errCount;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070080 double scaleFactor;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070081 std::optional<BridgeGpio> bridgeGpio;
James Feist71d31b22019-01-02 16:57:54 -080082 PowerState readState;
James Feist46342ec2019-03-06 14:03:41 -080083 thresholds::ThresholdTimer thresholdTimer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070084 void setupRead(void);
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};