blob: 9dd8b232f10ca064f7fd16752f0f337a911fbe61 [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 Feist8086aba2020-08-25 16:00:59 -07007#include <boost/asio/streambuf.hpp>
James Feist58295ad2019-05-30 15:01:41 -07008#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -07009#include <gpiod.hpp>
10#include <sdbusplus/asio/object_server.hpp>
11
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
James Feist251c7822018-09-12 12:54:15 -070020class CPUSensor : public Sensor
James Feist6714a252018-09-10 15:26:18 -070021{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070022 public:
James Feistd8705872019-02-08 13:26:09 -080023 CPUSensor(const std::string& path, const std::string& objectType,
24 sdbusplus::asio::object_server& objectServer,
25 std::shared_ptr<sdbusplus::asio::connection>& conn,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080026 boost::asio::io_service& io, const std::string& sensorName,
James Feistd8705872019-02-08 13:26:09 -080027 std::vector<thresholds::Threshold>&& thresholds,
Vijay Khemka86dea2b2019-06-06 11:14:37 -070028 const std::string& configuration, int cpuId, bool show,
29 double dtsOffset);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070030 ~CPUSensor();
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070031 static constexpr unsigned int sensorScaleFactor = 1000;
32 static constexpr unsigned int sensorPollMs = 1000;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080033 static constexpr size_t warnAfterErrorCount = 10;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080034 static constexpr const char* labelTcontrol = "Tcontrol";
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;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070038 boost::asio::posix::stream_descriptor inputDev;
39 boost::asio::deadline_timer waitTimer;
40 boost::asio::streambuf readBuf;
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;
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +020048 uint8_t minMaxReadCounter;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070049 void setupRead(void);
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);
James Feist6714a252018-09-10 15:26:18 -070053};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080054
55extern boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>
56 gCpuSensors;
James Feist58295ad2019-05-30 15:01:41 -070057
58// this is added to cpusensor.hpp to avoid having every sensor have to link
59// against libgpiod, if another sensor needs it we may move it to utils
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070060inline bool cpuIsPresent(
61 const boost::container::flat_map<std::string, BasicVariantType>& gpioConfig)
James Feist58295ad2019-05-30 15:01:41 -070062{
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070063 static boost::container::flat_map<std::string, bool> cpuPresence;
James Feist58295ad2019-05-30 15:01:41 -070064
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070065 auto findName = gpioConfig.find("Name");
66 if (findName == gpioConfig.end())
67 {
68 return false;
69 }
70 std::string gpioName =
71 std::visit(VariantToStringVisitor(), findName->second);
72
73 auto findIndex = cpuPresence.find(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070074 if (findIndex != cpuPresence.end())
75 {
76 return findIndex->second;
77 }
78
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070079 bool activeHigh = true;
80 auto findPolarity = gpioConfig.find("Polarity");
81 if (findPolarity != gpioConfig.end())
James Feist58295ad2019-05-30 15:01:41 -070082 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070083 if (std::string("Low") ==
84 std::visit(VariantToStringVisitor(), findPolarity->second))
James Feist58295ad2019-05-30 15:01:41 -070085 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070086 activeHigh = false;
James Feist58295ad2019-05-30 15:01:41 -070087 }
88 }
89
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070090 auto line = gpiod::find_line(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070091 if (!line)
92 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070093 std::cerr << "Error requesting gpio: " << gpioName << "\n";
94 return false;
James Feist58295ad2019-05-30 15:01:41 -070095 }
96
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070097 bool resp;
James Feist58295ad2019-05-30 15:01:41 -070098 try
99 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700100 line.request({"cpusensor", gpiod::line_request::DIRECTION_INPUT,
101 activeHigh ? 0 : gpiod::line_request::FLAG_ACTIVE_LOW});
102 resp = line.get_value();
James Feist58295ad2019-05-30 15:01:41 -0700103 }
104 catch (std::system_error&)
105 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700106 std::cerr << "Error reading gpio: " << gpioName << "\n";
107 return false;
James Feist58295ad2019-05-30 15:01:41 -0700108 }
109
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700110 cpuPresence[gpioName] = resp;
111
James Feist58295ad2019-05-30 15:01:41 -0700112 return resp;
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700113}