James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 3 | #include "Thresholds.hpp" |
| 4 | #include "sensor.hpp" |
| 5 | |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 6 | #include <gpiod.hpp> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 7 | #include <memory> |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 8 | #include <optional> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 9 | #include <sdbusplus/asio/object_server.hpp> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 10 | #include <stdexcept> |
| 11 | #include <string> |
| 12 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 13 | |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 14 | class 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 Li | 17aba77 | 2020-04-09 15:48:46 +0800 | [diff] [blame] | 45 | 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 Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | gpiod::line line; |
| 58 | }; |
| 59 | |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 60 | class ADCSensor : public Sensor, public std::enable_shared_from_this<ADCSensor> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 61 | { |
| 62 | public: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 63 | 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 Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 68 | const double scaleFactor, PowerState readState, |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 69 | const std::string& sensorConfiguration, |
Jae Hyun Yoo | 1cbd1c6 | 2019-07-29 15:02:43 -0700 | [diff] [blame] | 70 | std::optional<BridgeGpio>&& bridgeGpio); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 71 | ~ADCSensor(); |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 72 | void setupRead(void); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 73 | |
| 74 | private: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 75 | sdbusplus::asio::object_server& objServer; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 76 | boost::asio::posix::stream_descriptor inputDev; |
| 77 | boost::asio::deadline_timer waitTimer; |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 78 | std::shared_ptr<boost::asio::streambuf> readBuf; |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 79 | std::string path; |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 80 | size_t errCount; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 81 | double scaleFactor; |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 82 | std::optional<BridgeGpio> bridgeGpio; |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 83 | PowerState readState; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 84 | thresholds::ThresholdTimer thresholdTimer; |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 85 | void handleResponse(const boost::system::error_code& err); |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 86 | void checkThresholds(void) override; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 87 | }; |