James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 3 | #include "Thresholds.hpp" |
| 4 | #include "sensor.hpp" |
| 5 | |
James Feist | 8086aba | 2020-08-25 16:00:59 -0700 | [diff] [blame] | 6 | #include <boost/asio/streambuf.hpp> |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 7 | #include <gpiod.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 8 | #include <sdbusplus/asio/object_server.hpp> |
| 9 | |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 10 | #include <memory> |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 11 | #include <optional> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 12 | #include <stdexcept> |
| 13 | #include <string> |
| 14 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 15 | |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 16 | class BridgeGpio |
| 17 | { |
| 18 | public: |
Zev Weiss | f72eb83 | 2021-06-25 05:55:08 +0000 | [diff] [blame] | 19 | BridgeGpio(const std::string& name, const int polarity, |
| 20 | const float setupTime) : |
| 21 | setupTimeMs(static_cast<unsigned int>(setupTime * 1000)) |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 22 | { |
| 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 Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 38 | catch (const std::system_error&) |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 39 | { |
| 40 | std::cerr << "Error requesting gpio: " << name << "\n"; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void set(int value) |
| 46 | { |
| 47 | if (line) |
| 48 | { |
Yong Li | 17aba77 | 2020-04-09 15:48:46 +0800 | [diff] [blame] | 49 | try |
| 50 | { |
| 51 | line.set_value(value); |
| 52 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 53 | catch (const std::system_error& exc) |
Yong Li | 17aba77 | 2020-04-09 15:48:46 +0800 | [diff] [blame] | 54 | { |
| 55 | std::cerr << "Error set_value: " << exc.what() << "\n"; |
| 56 | } |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Zev Weiss | f72eb83 | 2021-06-25 05:55:08 +0000 | [diff] [blame] | 60 | unsigned int setupTimeMs; |
| 61 | |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 62 | private: |
| 63 | gpiod::line line; |
| 64 | }; |
| 65 | |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 66 | class ADCSensor : public Sensor, public std::enable_shared_from_this<ADCSensor> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 67 | { |
| 68 | public: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 69 | ADCSensor(const std::string& path, |
| 70 | sdbusplus::asio::object_server& objectServer, |
| 71 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 72 | boost::asio::io_context& io, const std::string& sensorName, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 73 | std::vector<thresholds::Threshold>&& thresholds, |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 74 | double scaleFactor, float pollRate, PowerState readState, |
| 75 | const std::string& sensorConfiguration, |
Jae Hyun Yoo | 1cbd1c6 | 2019-07-29 15:02:43 -0700 | [diff] [blame] | 76 | std::optional<BridgeGpio>&& bridgeGpio); |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 77 | ~ADCSensor() override; |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 78 | void setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 79 | |
| 80 | private: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 81 | sdbusplus::asio::object_server& objServer; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 82 | boost::asio::posix::stream_descriptor inputDev; |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 83 | boost::asio::steady_timer waitTimer; |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 84 | std::shared_ptr<boost::asio::streambuf> readBuf; |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 85 | std::string path; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 86 | double scaleFactor; |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 87 | unsigned int sensorPollMs; |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 88 | std::optional<BridgeGpio> bridgeGpio; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 89 | thresholds::ThresholdTimer thresholdTimer; |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 90 | void handleResponse(const boost::system::error_code& err); |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 91 | void checkThresholds() override; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 92 | }; |