blob: 1c25655a4e98bf8ae6b93246ed70f8f247d9a76c [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
52inline bool hostIsPresent(size_t gpioNum)
53{
54 static boost::container::flat_map<size_t, bool> cpuPresence;
55
56 auto findIndex = cpuPresence.find(gpioNum);
57 if (findIndex != cpuPresence.end())
58 {
59 return findIndex->second;
60 }
61
62 constexpr size_t sgpioBase = 232;
63
64 // check if sysfs has device
65 bool sysfs = std::filesystem::exists(gpioPath + std::string("gpio") +
66 std::to_string(gpioNum));
67
68 // todo: delete this when we remove all sysfs code
69 if (sysfs)
70 {
71 // close it, we'll reopen it at the end
72 std::ofstream unexport(gpioPath + std::string("unexport"));
73 if (unexport.good())
74 {
75 unexport << gpioNum;
76 }
77 else
78 {
79 std::cerr << "Error cleaning up sysfs device\n";
80 }
81 }
82
Jae Hyun Yoo182798e2019-07-02 10:57:51 -070083 size_t chipNum = (gpioNum - sgpioBase) / 8;
84 size_t index = (gpioNum - sgpioBase) % 8;
85 gpiod::chip chip("gpiochip" + std::to_string(chipNum));
James Feist58295ad2019-05-30 15:01:41 -070086 auto line = chip.get_line(index);
87
88 if (!line)
89 {
90 std::cerr << "Error requesting gpio\n";
91 return true;
92 }
93
94 bool resp = true;
95 try
96 {
97 line.request({"adcsensor", gpiod::line_request::DIRECTION_INPUT});
98 resp = !line.get_value();
99 }
100 catch (std::system_error&)
101 {
102 std::cerr << "Error reading gpio\n";
103 return true;
104 }
105
106 // todo: delete this when we remove all sysfs code
107 if (sysfs)
108 {
109 // reopen it
110 std::ofstream populate(gpioPath + std::string("export"));
111 if (populate.good())
112 {
113 populate << gpioNum;
114 }
115 else
116 {
117 std::cerr << "Error cleaning up sysfs device\n";
118 }
119 }
120 cpuPresence[gpioNum] = resp;
121 return resp;
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700122}