blob: 81f630de1fce2a18a8176833f4124910fc5dbf63 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
2
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10303#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:
Zev Weissf72eb832021-06-25 05:55:08 +000019 BridgeGpio(const std::string& name, const int polarity,
20 const float setupTime) :
21 setupTimeMs(static_cast<unsigned int>(setupTime * 1000))
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070022 {
23 line = gpiod::find_line(name);
24 if (!line)
25 {
26 std::cerr << "Error finding gpio: " << name << "\n";
27 }
28 else
29 {
30 try
31 {
32 line.request({"adcsensor",
33 gpiod::line_request::DIRECTION_OUTPUT,
34 polarity == gpiod::line::ACTIVE_HIGH
35 ? 0
36 : gpiod::line_request::FLAG_ACTIVE_LOW});
37 }
Patrick Williams26601e82021-10-06 12:43:25 -050038 catch (const std::system_error&)
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070039 {
40 std::cerr << "Error requesting gpio: " << name << "\n";
41 }
42 }
43 }
44
45 void set(int value)
46 {
47 if (line)
48 {
Yong Li17aba772020-04-09 15:48:46 +080049 try
50 {
51 line.set_value(value);
52 }
Patrick Williams26601e82021-10-06 12:43:25 -050053 catch (const std::system_error& exc)
Yong Li17aba772020-04-09 15:48:46 +080054 {
55 std::cerr << "Error set_value: " << exc.what() << "\n";
56 }
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070057 }
58 }
59
Zev Weissf72eb832021-06-25 05:55:08 +000060 unsigned int setupTimeMs;
61
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070062 private:
63 gpiod::line line;
64};
65
Yong Li1afda6b2020-04-09 19:24:13 +080066class ADCSensor : public Sensor, public std::enable_shared_from_this<ADCSensor>
James Feist6714a252018-09-10 15:26:18 -070067{
68 public:
James Feistd8705872019-02-08 13:26:09 -080069 ADCSensor(const std::string& path,
70 sdbusplus::asio::object_server& objectServer,
71 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080072 boost::asio::io_context& io, const std::string& sensorName,
James Feistd8705872019-02-08 13:26:09 -080073 std::vector<thresholds::Threshold>&& thresholds,
Ed Tanous2049bd22022-07-09 07:20:26 -070074 double scaleFactor, float pollRate, PowerState readState,
75 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070076 std::optional<BridgeGpio>&& bridgeGpio);
Ed Tanous8a57ec02020-10-09 12:46:52 -070077 ~ADCSensor() override;
Ed Tanous201a1012024-04-03 18:07:28 -070078 void setupRead();
James Feist6714a252018-09-10 15:26:18 -070079
80 private:
James Feistd8705872019-02-08 13:26:09 -080081 sdbusplus::asio::object_server& objServer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070082 boost::asio::posix::stream_descriptor inputDev;
Ed Tanous9b4a20e2022-09-06 08:47:11 -070083 boost::asio::steady_timer waitTimer;
Yong Li1afda6b2020-04-09 19:24:13 +080084 std::shared_ptr<boost::asio::streambuf> readBuf;
James Feist930fcde2019-05-28 12:58:43 -070085 std::string path;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070086 double scaleFactor;
Jeff Lind9cd7042020-11-20 15:49:28 +080087 unsigned int sensorPollMs;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070088 std::optional<BridgeGpio> bridgeGpio;
James Feist46342ec2019-03-06 14:03:41 -080089 thresholds::ThresholdTimer thresholdTimer;
James Feistd8705872019-02-08 13:26:09 -080090 void handleResponse(const boost::system::error_code& err);
Ed Tanous201a1012024-04-03 18:07:28 -070091 void checkThresholds() override;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070092};