blob: 7a2819f660a79508b4b423f8e29724ed16d72879 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
2
James Feist58295ad2019-05-30 15:01:41 -07003#include "Utils.hpp"
4
James Feist6714a252018-09-10 15:26:18 -07005#include <Thresholds.hpp>
James Feist58295ad2019-05-30 15:01:41 -07006#include <boost/container/flat_map.hpp>
7#include <filesystem>
8#include <fstream>
9#include <gpiod.hpp>
James Feist6714a252018-09-10 15:26:18 -070010#include <sdbusplus/asio/object_server.hpp>
James Feist251c7822018-09-12 12:54:15 -070011#include <sensor.hpp>
James Feist6714a252018-09-10 15:26:18 -070012
James Feist251c7822018-09-12 12:54:15 -070013class CPUSensor : public Sensor
James Feist6714a252018-09-10 15:26:18 -070014{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070015 public:
James Feistd8705872019-02-08 13:26:09 -080016 CPUSensor(const std::string& path, const std::string& objectType,
17 sdbusplus::asio::object_server& objectServer,
18 std::shared_ptr<sdbusplus::asio::connection>& conn,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080019 boost::asio::io_service& io, const std::string& sensorName,
James Feistd8705872019-02-08 13:26:09 -080020 std::vector<thresholds::Threshold>&& thresholds,
Vijay Khemka86dea2b2019-06-06 11:14:37 -070021 const std::string& configuration, int cpuId, bool show,
22 double dtsOffset);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070023 ~CPUSensor();
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070024 static constexpr unsigned int sensorScaleFactor = 1000;
25 static constexpr unsigned int sensorPollMs = 1000;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080026 static constexpr size_t warnAfterErrorCount = 10;
27 static constexpr double maxReading = 127;
28 static constexpr double minReading = -128;
29 static constexpr const char* labelTcontrol = "Tcontrol";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070030
James Feist6714a252018-09-10 15:26:18 -070031 private:
James Feistd8705872019-02-08 13:26:09 -080032 sdbusplus::asio::object_server& objServer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070033 boost::asio::posix::stream_descriptor inputDev;
34 boost::asio::deadline_timer waitTimer;
35 boost::asio::streambuf readBuf;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080036 std::string nameTcontrol;
James Feist930fcde2019-05-28 12:58:43 -070037 std::string path;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080038 double privTcontrol;
Vijay Khemka86dea2b2019-06-06 11:14:37 -070039 double dtsOffset;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080040 bool show;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070041 int errCount;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070042 void setupRead(void);
James Feistd8705872019-02-08 13:26:09 -080043 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080044 void checkThresholds(void) override;
James Feist6714a252018-09-10 15:26:18 -070045};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080046
47extern boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>
48 gCpuSensors;
James Feist58295ad2019-05-30 15:01:41 -070049
50// this is added to cpusensor.hpp to avoid having every sensor have to link
51// against libgpiod, if another sensor needs it we may move it to utils
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070052inline bool cpuIsPresent(
53 const boost::container::flat_map<std::string, BasicVariantType>& gpioConfig)
James Feist58295ad2019-05-30 15:01:41 -070054{
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070055 static boost::container::flat_map<std::string, bool> cpuPresence;
James Feist58295ad2019-05-30 15:01:41 -070056
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070057 auto findName = gpioConfig.find("Name");
58 if (findName == gpioConfig.end())
59 {
60 return false;
61 }
62 std::string gpioName =
63 std::visit(VariantToStringVisitor(), findName->second);
64
65 auto findIndex = cpuPresence.find(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070066 if (findIndex != cpuPresence.end())
67 {
68 return findIndex->second;
69 }
70
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070071 bool activeHigh = true;
72 auto findPolarity = gpioConfig.find("Polarity");
73 if (findPolarity != gpioConfig.end())
James Feist58295ad2019-05-30 15:01:41 -070074 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070075 if (std::string("Low") ==
76 std::visit(VariantToStringVisitor(), findPolarity->second))
James Feist58295ad2019-05-30 15:01:41 -070077 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070078 activeHigh = false;
James Feist58295ad2019-05-30 15:01:41 -070079 }
80 }
81
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070082 auto line = gpiod::find_line(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070083 if (!line)
84 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070085 std::cerr << "Error requesting gpio: " << gpioName << "\n";
86 return false;
James Feist58295ad2019-05-30 15:01:41 -070087 }
88
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070089 bool resp;
James Feist58295ad2019-05-30 15:01:41 -070090 try
91 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070092 line.request({"cpusensor", gpiod::line_request::DIRECTION_INPUT,
93 activeHigh ? 0 : gpiod::line_request::FLAG_ACTIVE_LOW});
94 resp = line.get_value();
James Feist58295ad2019-05-30 15:01:41 -070095 }
96 catch (std::system_error&)
97 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070098 std::cerr << "Error reading gpio: " << gpioName << "\n";
99 return false;
James Feist58295ad2019-05-30 15:01:41 -0700100 }
101
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700102 cpuPresence[gpioName] = resp;
103
James Feist58295ad2019-05-30 15:01:41 -0700104 return resp;
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700105}