blob: 9d2369b34e8d9d064d94228f55b25e12dbb69da4 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
2
Ed Tanous8a57ec02020-10-09 12:46:52 -07003#include <Thresholds.hpp>
James Feist8086aba2020-08-25 16:00:59 -07004#include <boost/asio/streambuf.hpp>
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -07005#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -07006#include <sdbusplus/asio/object_server.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -07007#include <sensor.hpp>
James Feist38fb5982020-05-28 10:09:54 -07008
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:
Zev Weissf72eb832021-06-25 05:55:08 +000018 BridgeGpio(const std::string& name, const int polarity,
19 const float setupTime) :
20 setupTimeMs(static_cast<unsigned int>(setupTime * 1000))
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070021 {
22 line = gpiod::find_line(name);
23 if (!line)
24 {
25 std::cerr << "Error finding gpio: " << name << "\n";
26 }
27 else
28 {
29 try
30 {
31 line.request({"adcsensor",
32 gpiod::line_request::DIRECTION_OUTPUT,
33 polarity == gpiod::line::ACTIVE_HIGH
34 ? 0
35 : gpiod::line_request::FLAG_ACTIVE_LOW});
36 }
Patrick Williams26601e82021-10-06 12:43:25 -050037 catch (const std::system_error&)
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070038 {
39 std::cerr << "Error requesting gpio: " << name << "\n";
40 }
41 }
42 }
43
44 void set(int value)
45 {
46 if (line)
47 {
Yong Li17aba772020-04-09 15:48:46 +080048 try
49 {
50 line.set_value(value);
51 }
Patrick Williams26601e82021-10-06 12:43:25 -050052 catch (const std::system_error& exc)
Yong Li17aba772020-04-09 15:48:46 +080053 {
54 std::cerr << "Error set_value: " << exc.what() << "\n";
55 }
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070056 }
57 }
58
Zev Weissf72eb832021-06-25 05:55:08 +000059 unsigned int setupTimeMs;
60
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070061 private:
62 gpiod::line line;
63};
64
Yong Li1afda6b2020-04-09 19:24:13 +080065class ADCSensor : public Sensor, public std::enable_shared_from_this<ADCSensor>
James Feist6714a252018-09-10 15:26:18 -070066{
67 public:
James Feistd8705872019-02-08 13:26:09 -080068 ADCSensor(const std::string& path,
69 sdbusplus::asio::object_server& objectServer,
70 std::shared_ptr<sdbusplus::asio::connection>& conn,
71 boost::asio::io_service& io, const std::string& sensorName,
72 std::vector<thresholds::Threshold>&& thresholds,
Jeff Lind9cd7042020-11-20 15:49:28 +080073 const double scaleFactor, const float pollRate,
74 PowerState readState, const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070075 std::optional<BridgeGpio>&& bridgeGpio);
Ed Tanous8a57ec02020-10-09 12:46:52 -070076 ~ADCSensor() override;
Yong Li1afda6b2020-04-09 19:24:13 +080077 void setupRead(void);
James Feist6714a252018-09-10 15:26:18 -070078
79 private:
James Feistd8705872019-02-08 13:26:09 -080080 sdbusplus::asio::object_server& objServer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070081 boost::asio::posix::stream_descriptor inputDev;
82 boost::asio::deadline_timer waitTimer;
Yong Li1afda6b2020-04-09 19:24:13 +080083 std::shared_ptr<boost::asio::streambuf> readBuf;
James Feist930fcde2019-05-28 12:58:43 -070084 std::string path;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070085 double scaleFactor;
Jeff Lind9cd7042020-11-20 15:49:28 +080086 unsigned int sensorPollMs;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070087 std::optional<BridgeGpio> bridgeGpio;
James Feist46342ec2019-03-06 14:03:41 -080088 thresholds::ThresholdTimer thresholdTimer;
James Feistd8705872019-02-08 13:26:09 -080089 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080090 void checkThresholds(void) override;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070091};