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