blob: e76ec89bcd47da41ed965d25cace9b3d5b2b3139 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
2
Patrick Ventureca44b2f2019-10-31 11:02:26 -07003#include "Thresholds.hpp"
James Feist58295ad2019-05-30 15:01:41 -07004#include "Utils.hpp"
Patrick Ventureca44b2f2019-10-31 11:02:26 -07005#include "sensor.hpp"
James Feist58295ad2019-05-30 15:01:41 -07006
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>
10
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
James Feist251c7822018-09-12 12:54:15 -070019class CPUSensor : public Sensor
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);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070029 ~CPUSensor();
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;
33 static constexpr double maxReading = 127;
34 static constexpr double minReading = -128;
35 static constexpr const char* labelTcontrol = "Tcontrol";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070036
James Feist6714a252018-09-10 15:26:18 -070037 private:
James Feistd8705872019-02-08 13:26:09 -080038 sdbusplus::asio::object_server& objServer;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070039 boost::asio::posix::stream_descriptor inputDev;
40 boost::asio::deadline_timer waitTimer;
41 boost::asio::streambuf readBuf;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080042 std::string nameTcontrol;
James Feist930fcde2019-05-28 12:58:43 -070043 std::string path;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080044 double privTcontrol;
Vijay Khemka86dea2b2019-06-06 11:14:37 -070045 double dtsOffset;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080046 bool show;
Brad Bishopfbb44ad2019-11-08 09:42:37 -050047 size_t errCount;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070048 void setupRead(void);
James Feistd8705872019-02-08 13:26:09 -080049 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080050 void checkThresholds(void) override;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +020051 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 Feist6714a252018-09-10 15:26:18 -070055};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080056
57extern boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>
58 gCpuSensors;
James Feist58295ad2019-05-30 15:01:41 -070059
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 Yooffa07e22019-07-17 10:47:18 -070062inline bool cpuIsPresent(
63 const boost::container::flat_map<std::string, BasicVariantType>& gpioConfig)
James Feist58295ad2019-05-30 15:01:41 -070064{
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070065 static boost::container::flat_map<std::string, bool> cpuPresence;
James Feist58295ad2019-05-30 15:01:41 -070066
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070067 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 Feist58295ad2019-05-30 15:01:41 -070076 if (findIndex != cpuPresence.end())
77 {
78 return findIndex->second;
79 }
80
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070081 bool activeHigh = true;
82 auto findPolarity = gpioConfig.find("Polarity");
83 if (findPolarity != gpioConfig.end())
James Feist58295ad2019-05-30 15:01:41 -070084 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070085 if (std::string("Low") ==
86 std::visit(VariantToStringVisitor(), findPolarity->second))
James Feist58295ad2019-05-30 15:01:41 -070087 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070088 activeHigh = false;
James Feist58295ad2019-05-30 15:01:41 -070089 }
90 }
91
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070092 auto line = gpiod::find_line(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070093 if (!line)
94 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070095 std::cerr << "Error requesting gpio: " << gpioName << "\n";
96 return false;
James Feist58295ad2019-05-30 15:01:41 -070097 }
98
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070099 bool resp;
James Feist58295ad2019-05-30 15:01:41 -0700100 try
101 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700102 line.request({"cpusensor", gpiod::line_request::DIRECTION_INPUT,
103 activeHigh ? 0 : gpiod::line_request::FLAG_ACTIVE_LOW});
104 resp = line.get_value();
James Feist58295ad2019-05-30 15:01:41 -0700105 }
106 catch (std::system_error&)
107 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700108 std::cerr << "Error reading gpio: " << gpioName << "\n";
109 return false;
James Feist58295ad2019-05-30 15:01:41 -0700110 }
111
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700112 cpuPresence[gpioName] = resp;
113
James Feist58295ad2019-05-30 15:01:41 -0700114 return resp;
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700115}