James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <Thresholds.hpp> |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame^] | 4 | #include <gpiod.hpp> |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 5 | #include <optional> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 6 | #include <sdbusplus/asio/object_server.hpp> |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 7 | #include <sensor.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 8 | |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame^] | 9 | class BridgeGpio |
| 10 | { |
| 11 | public: |
| 12 | BridgeGpio(const std::string& name, const int polarity) |
| 13 | { |
| 14 | line = gpiod::find_line(name); |
| 15 | if (!line) |
| 16 | { |
| 17 | std::cerr << "Error finding gpio: " << name << "\n"; |
| 18 | } |
| 19 | else |
| 20 | { |
| 21 | try |
| 22 | { |
| 23 | line.request({"adcsensor", |
| 24 | gpiod::line_request::DIRECTION_OUTPUT, |
| 25 | polarity == gpiod::line::ACTIVE_HIGH |
| 26 | ? 0 |
| 27 | : gpiod::line_request::FLAG_ACTIVE_LOW}); |
| 28 | } |
| 29 | catch (std::system_error&) |
| 30 | { |
| 31 | std::cerr << "Error requesting gpio: " << name << "\n"; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void set(int value) |
| 37 | { |
| 38 | if (line) |
| 39 | { |
| 40 | line.set_value(value); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | gpiod::line line; |
| 46 | }; |
| 47 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 48 | class ADCSensor : public Sensor |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 49 | { |
| 50 | public: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 51 | ADCSensor(const std::string& path, |
| 52 | sdbusplus::asio::object_server& objectServer, |
| 53 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 54 | boost::asio::io_service& io, const std::string& sensorName, |
| 55 | std::vector<thresholds::Threshold>&& thresholds, |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 56 | const double scaleFactor, PowerState readState, |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 57 | const std::string& sensorConfiguration, |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame^] | 58 | std::optional<BridgeGpio> bridgeGpio); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 59 | ~ADCSensor(); |
| 60 | |
| 61 | private: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 62 | sdbusplus::asio::object_server& objServer; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 63 | boost::asio::posix::stream_descriptor inputDev; |
| 64 | boost::asio::deadline_timer waitTimer; |
| 65 | boost::asio::streambuf readBuf; |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 66 | std::string path; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 67 | int errCount; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 68 | double scaleFactor; |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame^] | 69 | std::optional<BridgeGpio> bridgeGpio; |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 70 | PowerState readState; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 71 | thresholds::ThresholdTimer thresholdTimer; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 72 | void setupRead(void); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 73 | void handleResponse(const boost::system::error_code& err); |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 74 | void checkThresholds(void) override; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 75 | }; |