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" |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 4 | #include "Utils.hpp" |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 5 | #include "sensor.hpp" |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 6 | |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 7 | #include <boost/container/flat_map.hpp> |
| 8 | #include <filesystem> |
| 9 | #include <fstream> |
| 10 | #include <gpiod.hpp> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame^] | 11 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 12 | #include <sdbusplus/asio/object_server.hpp> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame^] | 13 | #include <stdexcept> |
| 14 | #include <string> |
| 15 | #include <variant> |
| 16 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 17 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 18 | class CPUSensor : public Sensor |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 19 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 20 | public: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 21 | CPUSensor(const std::string& path, const std::string& objectType, |
| 22 | sdbusplus::asio::object_server& objectServer, |
| 23 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 24 | boost::asio::io_service& io, const std::string& sensorName, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 25 | std::vector<thresholds::Threshold>&& thresholds, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 26 | const std::string& configuration, int cpuId, bool show, |
| 27 | double dtsOffset); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 28 | ~CPUSensor(); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 29 | static constexpr unsigned int sensorScaleFactor = 1000; |
| 30 | static constexpr unsigned int sensorPollMs = 1000; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 31 | static constexpr size_t warnAfterErrorCount = 10; |
| 32 | static constexpr double maxReading = 127; |
| 33 | static constexpr double minReading = -128; |
| 34 | static constexpr const char* labelTcontrol = "Tcontrol"; |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 35 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 36 | private: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 37 | sdbusplus::asio::object_server& objServer; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 38 | boost::asio::posix::stream_descriptor inputDev; |
| 39 | boost::asio::deadline_timer waitTimer; |
| 40 | boost::asio::streambuf readBuf; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 41 | std::string nameTcontrol; |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 42 | std::string path; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 43 | double privTcontrol; |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 44 | double dtsOffset; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 45 | bool show; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 46 | int errCount; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 47 | void setupRead(void); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 48 | void handleResponse(const boost::system::error_code& err); |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 49 | void checkThresholds(void) override; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 50 | }; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 51 | |
| 52 | extern boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> |
| 53 | gCpuSensors; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 54 | |
| 55 | // this is added to cpusensor.hpp to avoid having every sensor have to link |
| 56 | // against libgpiod, if another sensor needs it we may move it to utils |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 57 | inline bool cpuIsPresent( |
| 58 | const boost::container::flat_map<std::string, BasicVariantType>& gpioConfig) |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 59 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 60 | static boost::container::flat_map<std::string, bool> cpuPresence; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 61 | |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 62 | auto findName = gpioConfig.find("Name"); |
| 63 | if (findName == gpioConfig.end()) |
| 64 | { |
| 65 | return false; |
| 66 | } |
| 67 | std::string gpioName = |
| 68 | std::visit(VariantToStringVisitor(), findName->second); |
| 69 | |
| 70 | auto findIndex = cpuPresence.find(gpioName); |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 71 | if (findIndex != cpuPresence.end()) |
| 72 | { |
| 73 | return findIndex->second; |
| 74 | } |
| 75 | |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 76 | bool activeHigh = true; |
| 77 | auto findPolarity = gpioConfig.find("Polarity"); |
| 78 | if (findPolarity != gpioConfig.end()) |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 79 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 80 | if (std::string("Low") == |
| 81 | std::visit(VariantToStringVisitor(), findPolarity->second)) |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 82 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 83 | activeHigh = false; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 87 | auto line = gpiod::find_line(gpioName); |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 88 | if (!line) |
| 89 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 90 | std::cerr << "Error requesting gpio: " << gpioName << "\n"; |
| 91 | return false; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 94 | bool resp; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 95 | try |
| 96 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 97 | line.request({"cpusensor", gpiod::line_request::DIRECTION_INPUT, |
| 98 | activeHigh ? 0 : gpiod::line_request::FLAG_ACTIVE_LOW}); |
| 99 | resp = line.get_value(); |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 100 | } |
| 101 | catch (std::system_error&) |
| 102 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 103 | std::cerr << "Error reading gpio: " << gpioName << "\n"; |
| 104 | return false; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 107 | cpuPresence[gpioName] = resp; |
| 108 | |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 109 | return resp; |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 110 | } |