blob: 0417c6ebfbd6f6fbb72e4a36dfc9760bb2306fed [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
2
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10303#include "Thresholds.hpp"
4#include "Utils.hpp"
5
James Feist8086aba2020-08-25 16:00:59 -07006#include <boost/asio/streambuf.hpp>
James Feist58295ad2019-05-30 15:01:41 -07007#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -07008#include <gpiod.hpp>
9#include <sdbusplus/asio/object_server.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -070010#include <sensor.hpp>
James Feist38fb5982020-05-28 10:09:54 -070011
James Feist58295ad2019-05-30 15:01:41 -070012#include <filesystem>
13#include <fstream>
Patrick Venturefd6ba732019-10-31 14:27:39 -070014#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070015#include <stdexcept>
16#include <string>
17#include <variant>
18#include <vector>
James Feist6714a252018-09-10 15:26:18 -070019
Thu Nguyen255da6b2022-07-29 10:05:52 +070020class IntelCPUSensor :
21 public Sensor,
22 public std::enable_shared_from_this<IntelCPUSensor>
James Feist6714a252018-09-10 15:26:18 -070023{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070024 public:
Thu Nguyen255da6b2022-07-29 10:05:52 +070025 IntelCPUSensor(const std::string& path, const std::string& objectType,
26 sdbusplus::asio::object_server& objectServer,
27 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080028 boost::asio::io_context& io, const std::string& sensorName,
Thu Nguyen255da6b2022-07-29 10:05:52 +070029 std::vector<thresholds::Threshold>&& thresholds,
30 const std::string& configuration, int cpuId, bool show,
31 double dtsOffset);
32 ~IntelCPUSensor() override;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070033 static constexpr unsigned int sensorScaleFactor = 1000;
34 static constexpr unsigned int sensorPollMs = 1000;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080035 static constexpr size_t warnAfterErrorCount = 10;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080036 static constexpr const char* labelTcontrol = "Tcontrol";
Arun P. Mohanan04d05062021-10-29 20:30:26 +053037 void setupRead(void);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070038
James Feist6714a252018-09-10 15:26:18 -070039 private:
James Feistd8705872019-02-08 13:26:09 -080040 sdbusplus::asio::object_server& objServer;
Ed Tanous77518b22021-11-05 13:02:23 -070041 boost::asio::streambuf readBuf;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070042 boost::asio::posix::stream_descriptor inputDev;
Ed Tanous9b4a20e2022-09-06 08:47:11 -070043 boost::asio::steady_timer waitTimer;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080044 std::string nameTcontrol;
James Feist930fcde2019-05-28 12:58:43 -070045 std::string path;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080046 double privTcontrol;
Vijay Khemka86dea2b2019-06-06 11:14:37 -070047 double dtsOffset;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080048 bool show;
James Feistc22b8422020-07-07 14:40:39 -070049 size_t pollTime;
James Feist838529b2020-09-02 16:57:05 -070050 bool loggedInterfaceDown = false;
Ed Tanousb429f312022-06-27 16:09:53 -070051 uint8_t minMaxReadCounter{0};
Ed Tanous2049bd22022-07-09 07:20:26 -070052 int fd{};
James Feistd8705872019-02-08 13:26:09 -080053 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080054 void checkThresholds(void) override;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +020055 void updateMinMaxValues(void);
Arun Lal K M84545712021-12-31 13:29:56 +000056 void restartRead(void);
James Feist6714a252018-09-10 15:26:18 -070057};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080058
Thu Nguyen255da6b2022-07-29 10:05:52 +070059extern boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>>
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080060 gCpuSensors;
James Feist58295ad2019-05-30 15:01:41 -070061
Thu Nguyen255da6b2022-07-29 10:05:52 +070062// this is added to intelcpusensor.hpp to avoid having every sensor have to link
James Feist58295ad2019-05-30 15:01:41 -070063// against libgpiod, if another sensor needs it we may move it to utils
Zev Weissafd15042022-07-18 12:28:40 -070064inline bool cpuIsPresent(const SensorBaseConfigMap& gpioConfig)
James Feist58295ad2019-05-30 15:01:41 -070065{
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070066 static boost::container::flat_map<std::string, bool> cpuPresence;
James Feist58295ad2019-05-30 15:01:41 -070067
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070068 auto findName = gpioConfig.find("Name");
69 if (findName == gpioConfig.end())
70 {
71 return false;
72 }
Patrick Williams779c96a2023-05-10 07:50:42 -050073 std::string gpioName = std::visit(VariantToStringVisitor(),
74 findName->second);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070075
76 auto findIndex = cpuPresence.find(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070077 if (findIndex != cpuPresence.end())
78 {
79 return findIndex->second;
80 }
81
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070082 bool activeHigh = true;
83 auto findPolarity = gpioConfig.find("Polarity");
84 if (findPolarity != gpioConfig.end())
James Feist58295ad2019-05-30 15:01:41 -070085 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070086 if (std::string("Low") ==
87 std::visit(VariantToStringVisitor(), findPolarity->second))
James Feist58295ad2019-05-30 15:01:41 -070088 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070089 activeHigh = false;
James Feist58295ad2019-05-30 15:01:41 -070090 }
91 }
92
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070093 auto line = gpiod::find_line(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070094 if (!line)
95 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070096 std::cerr << "Error requesting gpio: " << gpioName << "\n";
97 return false;
James Feist58295ad2019-05-30 15:01:41 -070098 }
99
Ed Tanous74cffa82022-01-25 13:00:28 -0800100 bool resp = false;
James Feist58295ad2019-05-30 15:01:41 -0700101 try
102 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700103 line.request({"cpusensor", gpiod::line_request::DIRECTION_INPUT,
104 activeHigh ? 0 : gpiod::line_request::FLAG_ACTIVE_LOW});
Ed Tanous2049bd22022-07-09 07:20:26 -0700105 resp = (line.get_value() != 0);
James Feist58295ad2019-05-30 15:01:41 -0700106 }
Patrick Williams26601e82021-10-06 12:43:25 -0500107 catch (const std::system_error&)
James Feist58295ad2019-05-30 15:01:41 -0700108 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700109 std::cerr << "Error reading gpio: " << gpioName << "\n";
110 return false;
James Feist58295ad2019-05-30 15:01:41 -0700111 }
112
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700113 cpuPresence[gpioName] = resp;
114
James Feist58295ad2019-05-30 15:01:41 -0700115 return resp;
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700116}