James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 3 | #include "Thresholds.hpp" |
| 4 | #include "Utils.hpp" |
| 5 | |
James Feist | 8086aba | 2020-08-25 16:00:59 -0700 | [diff] [blame] | 6 | #include <boost/asio/streambuf.hpp> |
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> |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 10 | #include <sensor.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 11 | |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 12 | #include <filesystem> |
| 13 | #include <fstream> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 14 | #include <memory> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 15 | #include <stdexcept> |
| 16 | #include <string> |
| 17 | #include <variant> |
| 18 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 19 | |
Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 20 | class IntelCPUSensor : |
| 21 | public Sensor, |
| 22 | public std::enable_shared_from_this<IntelCPUSensor> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 23 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 24 | public: |
Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 25 | IntelCPUSensor(const std::string& path, const std::string& objectType, |
| 26 | sdbusplus::asio::object_server& objectServer, |
| 27 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 28 | boost::asio::io_context& io, const std::string& sensorName, |
Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 29 | std::vector<thresholds::Threshold>&& thresholds, |
| 30 | const std::string& configuration, int cpuId, bool show, |
| 31 | double dtsOffset); |
| 32 | ~IntelCPUSensor() override; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 33 | static constexpr unsigned int sensorScaleFactor = 1000; |
| 34 | static constexpr unsigned int sensorPollMs = 1000; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 35 | static constexpr size_t warnAfterErrorCount = 10; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 36 | static constexpr const char* labelTcontrol = "Tcontrol"; |
Arun P. Mohanan | 04d0506 | 2021-10-29 20:30:26 +0530 | [diff] [blame] | 37 | void setupRead(void); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 38 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 39 | private: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 40 | sdbusplus::asio::object_server& objServer; |
Ed Tanous | 77518b2 | 2021-11-05 13:02:23 -0700 | [diff] [blame] | 41 | boost::asio::streambuf readBuf; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 42 | boost::asio::posix::stream_descriptor inputDev; |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 43 | boost::asio::steady_timer waitTimer; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 44 | std::string nameTcontrol; |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 45 | std::string path; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 46 | double privTcontrol; |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 47 | double dtsOffset; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 48 | bool show; |
James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 49 | size_t pollTime; |
James Feist | 838529b | 2020-09-02 16:57:05 -0700 | [diff] [blame] | 50 | bool loggedInterfaceDown = false; |
Ed Tanous | b429f31 | 2022-06-27 16:09:53 -0700 | [diff] [blame] | 51 | uint8_t minMaxReadCounter{0}; |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 52 | int fd{}; |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 53 | void handleResponse(const boost::system::error_code& err); |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 54 | void checkThresholds(void) override; |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 55 | void updateMinMaxValues(void); |
Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 56 | void restartRead(void); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 57 | }; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 58 | |
Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 59 | extern boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>> |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 60 | gCpuSensors; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 61 | |
Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 62 | // this is added to intelcpusensor.hpp to avoid having every sensor have to link |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 63 | // against libgpiod, if another sensor needs it we may move it to utils |
Zev Weiss | afd1504 | 2022-07-18 12:28:40 -0700 | [diff] [blame] | 64 | inline bool cpuIsPresent(const SensorBaseConfigMap& gpioConfig) |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 65 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 66 | static boost::container::flat_map<std::string, bool> cpuPresence; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 67 | |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 68 | auto findName = gpioConfig.find("Name"); |
| 69 | if (findName == gpioConfig.end()) |
| 70 | { |
| 71 | return false; |
| 72 | } |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 73 | std::string gpioName = std::visit(VariantToStringVisitor(), |
| 74 | findName->second); |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 75 | |
| 76 | auto findIndex = cpuPresence.find(gpioName); |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 77 | if (findIndex != cpuPresence.end()) |
| 78 | { |
| 79 | return findIndex->second; |
| 80 | } |
| 81 | |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 82 | bool activeHigh = true; |
| 83 | auto findPolarity = gpioConfig.find("Polarity"); |
| 84 | if (findPolarity != gpioConfig.end()) |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 85 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 86 | if (std::string("Low") == |
| 87 | std::visit(VariantToStringVisitor(), findPolarity->second)) |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 88 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 89 | activeHigh = false; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 93 | auto line = gpiod::find_line(gpioName); |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 94 | if (!line) |
| 95 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 96 | std::cerr << "Error requesting gpio: " << gpioName << "\n"; |
| 97 | return false; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 100 | bool resp = false; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 101 | try |
| 102 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 103 | line.request({"cpusensor", gpiod::line_request::DIRECTION_INPUT, |
| 104 | activeHigh ? 0 : gpiod::line_request::FLAG_ACTIVE_LOW}); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 105 | resp = (line.get_value() != 0); |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 106 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 107 | catch (const std::system_error&) |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 108 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 109 | std::cerr << "Error reading gpio: " << gpioName << "\n"; |
| 110 | return false; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 113 | cpuPresence[gpioName] = resp; |
| 114 | |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 115 | return resp; |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 116 | } |