blob: 589b1f691562704d5a831b130b39eb3474ac723c [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>
8#include <filesystem>
9#include <fstream>
10#include <gpiod.hpp>
Patrick Venturefd6ba732019-10-31 14:27:39 -070011#include <memory>
James Feist6714a252018-09-10 15:26:18 -070012#include <sdbusplus/asio/object_server.hpp>
Patrick Venturefd6ba732019-10-31 14:27:39 -070013#include <stdexcept>
14#include <string>
15#include <variant>
16#include <vector>
James Feist6714a252018-09-10 15:26:18 -070017
James Feist251c7822018-09-12 12:54:15 -070018class CPUSensor : public Sensor
James Feist6714a252018-09-10 15:26:18 -070019{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070020 public:
James Feistd8705872019-02-08 13:26:09 -080021 CPUSensor(const std::string& path, const std::string& objectType,
22 sdbusplus::asio::object_server& objectServer,
23 std::shared_ptr<sdbusplus::asio::connection>& conn,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080024 boost::asio::io_service& io, const std::string& sensorName,
James Feistd8705872019-02-08 13:26:09 -080025 std::vector<thresholds::Threshold>&& thresholds,
Vijay Khemka86dea2b2019-06-06 11:14:37 -070026 const std::string& configuration, int cpuId, bool show,
27 double dtsOffset);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070028 ~CPUSensor();
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070029 static constexpr unsigned int sensorScaleFactor = 1000;
30 static constexpr unsigned int sensorPollMs = 1000;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080031 static constexpr size_t warnAfterErrorCount = 10;
32 static constexpr double maxReading = 127;
33 static constexpr double minReading = -128;
34 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;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070046 int errCount;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070047 void setupRead(void);
James Feistd8705872019-02-08 13:26:09 -080048 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080049 void checkThresholds(void) override;
James Feist6714a252018-09-10 15:26:18 -070050};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080051
52extern boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>
53 gCpuSensors;
James Feist58295ad2019-05-30 15:01:41 -070054
55// this is added to cpusensor.hpp to avoid having every sensor have to link
56// against libgpiod, if another sensor needs it we may move it to utils
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070057inline bool cpuIsPresent(
58 const boost::container::flat_map<std::string, BasicVariantType>& gpioConfig)
James Feist58295ad2019-05-30 15:01:41 -070059{
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070060 static boost::container::flat_map<std::string, bool> cpuPresence;
James Feist58295ad2019-05-30 15:01:41 -070061
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070062 auto findName = gpioConfig.find("Name");
63 if (findName == gpioConfig.end())
64 {
65 return false;
66 }
67 std::string gpioName =
68 std::visit(VariantToStringVisitor(), findName->second);
69
70 auto findIndex = cpuPresence.find(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070071 if (findIndex != cpuPresence.end())
72 {
73 return findIndex->second;
74 }
75
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070076 bool activeHigh = true;
77 auto findPolarity = gpioConfig.find("Polarity");
78 if (findPolarity != gpioConfig.end())
James Feist58295ad2019-05-30 15:01:41 -070079 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070080 if (std::string("Low") ==
81 std::visit(VariantToStringVisitor(), findPolarity->second))
James Feist58295ad2019-05-30 15:01:41 -070082 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070083 activeHigh = false;
James Feist58295ad2019-05-30 15:01:41 -070084 }
85 }
86
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070087 auto line = gpiod::find_line(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070088 if (!line)
89 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070090 std::cerr << "Error requesting gpio: " << gpioName << "\n";
91 return false;
James Feist58295ad2019-05-30 15:01:41 -070092 }
93
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070094 bool resp;
James Feist58295ad2019-05-30 15:01:41 -070095 try
96 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070097 line.request({"cpusensor", gpiod::line_request::DIRECTION_INPUT,
98 activeHigh ? 0 : gpiod::line_request::FLAG_ACTIVE_LOW});
99 resp = line.get_value();
James Feist58295ad2019-05-30 15:01:41 -0700100 }
101 catch (std::system_error&)
102 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700103 std::cerr << "Error reading gpio: " << gpioName << "\n";
104 return false;
James Feist58295ad2019-05-30 15:01:41 -0700105 }
106
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700107 cpuPresence[gpioName] = resp;
108
James Feist58295ad2019-05-30 15:01:41 -0700109 return resp;
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700110}