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