blob: c4a47bce76d7242ad9c347ae2e89110d3da576c7 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
2
Ed Tanous8a57ec02020-10-09 12:46:52 -07003#include <Thresholds.hpp>
4#include <Utils.hpp>
James Feist8086aba2020-08-25 16:00:59 -07005#include <boost/asio/streambuf.hpp>
James Feist58295ad2019-05-30 15:01:41 -07006#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -07007#include <gpiod.hpp>
8#include <sdbusplus/asio/object_server.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -07009#include <sensor.hpp>
James Feist38fb5982020-05-28 10:09:54 -070010
James Feist58295ad2019-05-30 15:01:41 -070011#include <filesystem>
12#include <fstream>
Patrick Venturefd6ba732019-10-31 14:27:39 -070013#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070014#include <stdexcept>
15#include <string>
16#include <variant>
17#include <vector>
James Feist6714a252018-09-10 15:26:18 -070018
Arun P. Mohanan04d05062021-10-29 20:30:26 +053019class CPUSensor : public Sensor, public std::enable_shared_from_this<CPUSensor>
James Feist6714a252018-09-10 15:26:18 -070020{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070021 public:
James Feistd8705872019-02-08 13:26:09 -080022 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 Yoo73ca5512019-02-28 21:20:17 -080025 boost::asio::io_service& io, const std::string& sensorName,
James Feistd8705872019-02-08 13:26:09 -080026 std::vector<thresholds::Threshold>&& thresholds,
Vijay Khemka86dea2b2019-06-06 11:14:37 -070027 const std::string& configuration, int cpuId, bool show,
28 double dtsOffset);
Ed Tanous8a57ec02020-10-09 12:46:52 -070029 ~CPUSensor() override;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070030 static constexpr unsigned int sensorScaleFactor = 1000;
31 static constexpr unsigned int sensorPollMs = 1000;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080032 static constexpr size_t warnAfterErrorCount = 10;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080033 static constexpr const char* labelTcontrol = "Tcontrol";
Arun P. Mohanan04d05062021-10-29 20:30:26 +053034 void setupRead(void);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070035
James Feist6714a252018-09-10 15:26:18 -070036 private:
James Feistd8705872019-02-08 13:26:09 -080037 sdbusplus::asio::object_server& objServer;
Ed Tanous77518b22021-11-05 13:02:23 -070038 boost::asio::streambuf readBuf;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070039 boost::asio::posix::stream_descriptor inputDev;
40 boost::asio::deadline_timer waitTimer;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080041 std::string nameTcontrol;
James Feist930fcde2019-05-28 12:58:43 -070042 std::string path;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080043 double privTcontrol;
Vijay Khemka86dea2b2019-06-06 11:14:37 -070044 double dtsOffset;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080045 bool show;
James Feistc22b8422020-07-07 14:40:39 -070046 size_t pollTime;
James Feist838529b2020-09-02 16:57:05 -070047 bool loggedInterfaceDown = false;
Ed Tanousb429f312022-06-27 16:09:53 -070048 uint8_t minMaxReadCounter{0};
Arun Lal K M84545712021-12-31 13:29:56 +000049 int fd;
James Feistd8705872019-02-08 13:26:09 -080050 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080051 void checkThresholds(void) override;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +020052 void updateMinMaxValues(void);
Arun Lal K M84545712021-12-31 13:29:56 +000053 void restartRead(void);
James Feist6714a252018-09-10 15:26:18 -070054};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080055
Arun P. Mohanan04d05062021-10-29 20:30:26 +053056extern boost::container::flat_map<std::string, std::shared_ptr<CPUSensor>>
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080057 gCpuSensors;
James Feist58295ad2019-05-30 15:01:41 -070058
59// this is added to cpusensor.hpp to avoid having every sensor have to link
60// against libgpiod, if another sensor needs it we may move it to utils
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070061inline bool cpuIsPresent(
62 const boost::container::flat_map<std::string, BasicVariantType>& gpioConfig)
James Feist58295ad2019-05-30 15:01:41 -070063{
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070064 static boost::container::flat_map<std::string, bool> cpuPresence;
James Feist58295ad2019-05-30 15:01:41 -070065
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070066 auto findName = gpioConfig.find("Name");
67 if (findName == gpioConfig.end())
68 {
69 return false;
70 }
71 std::string gpioName =
72 std::visit(VariantToStringVisitor(), findName->second);
73
74 auto findIndex = cpuPresence.find(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070075 if (findIndex != cpuPresence.end())
76 {
77 return findIndex->second;
78 }
79
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070080 bool activeHigh = true;
81 auto findPolarity = gpioConfig.find("Polarity");
82 if (findPolarity != gpioConfig.end())
James Feist58295ad2019-05-30 15:01:41 -070083 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070084 if (std::string("Low") ==
85 std::visit(VariantToStringVisitor(), findPolarity->second))
James Feist58295ad2019-05-30 15:01:41 -070086 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070087 activeHigh = false;
James Feist58295ad2019-05-30 15:01:41 -070088 }
89 }
90
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070091 auto line = gpiod::find_line(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070092 if (!line)
93 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070094 std::cerr << "Error requesting gpio: " << gpioName << "\n";
95 return false;
James Feist58295ad2019-05-30 15:01:41 -070096 }
97
Ed Tanous74cffa82022-01-25 13:00:28 -080098 bool resp = false;
James Feist58295ad2019-05-30 15:01:41 -070099 try
100 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700101 line.request({"cpusensor", gpiod::line_request::DIRECTION_INPUT,
102 activeHigh ? 0 : gpiod::line_request::FLAG_ACTIVE_LOW});
103 resp = line.get_value();
James Feist58295ad2019-05-30 15:01:41 -0700104 }
Patrick Williams26601e82021-10-06 12:43:25 -0500105 catch (const std::system_error&)
James Feist58295ad2019-05-30 15:01:41 -0700106 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700107 std::cerr << "Error reading gpio: " << gpioName << "\n";
108 return false;
James Feist58295ad2019-05-30 15:01:41 -0700109 }
110
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700111 cpuPresence[gpioName] = resp;
112
James Feist58295ad2019-05-30 15:01:41 -0700113 return resp;
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700114}