blob: 65f30e78f67e8e7586df5e0ede444a4011fc51b7 [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
Jae Hyun Yoobe73e6a2019-06-04 14:19:38 -070062 // todo: need to change this number based setting to name based
James Feist58295ad2019-05-30 15:01:41 -070063 constexpr size_t sgpioBase = 232;
Jae Hyun Yoobe73e6a2019-06-04 14:19:38 -070064 constexpr const char* labelSgpiochipIn = "1e780200.sgpio";
James Feist58295ad2019-05-30 15:01:41 -070065
66 // check if sysfs has device
67 bool sysfs = std::filesystem::exists(gpioPath + std::string("gpio") +
68 std::to_string(gpioNum));
69
70 // todo: delete this when we remove all sysfs code
71 if (sysfs)
72 {
73 // close it, we'll reopen it at the end
74 std::ofstream unexport(gpioPath + std::string("unexport"));
75 if (unexport.good())
76 {
77 unexport << gpioNum;
78 }
79 else
80 {
81 std::cerr << "Error cleaning up sysfs device\n";
82 }
83 }
84
Jae Hyun Yoobe73e6a2019-06-04 14:19:38 -070085 size_t index = gpioNum - sgpioBase;
86 gpiod::chip chip(labelSgpiochipIn);
James Feist58295ad2019-05-30 15:01:41 -070087 auto line = chip.get_line(index);
88
89 if (!line)
90 {
91 std::cerr << "Error requesting gpio\n";
92 return true;
93 }
94
95 bool resp = true;
96 try
97 {
98 line.request({"adcsensor", gpiod::line_request::DIRECTION_INPUT});
99 resp = !line.get_value();
100 }
101 catch (std::system_error&)
102 {
103 std::cerr << "Error reading gpio\n";
104 return true;
105 }
106
107 // todo: delete this when we remove all sysfs code
108 if (sysfs)
109 {
110 // reopen it
111 std::ofstream populate(gpioPath + std::string("export"));
112 if (populate.good())
113 {
114 populate << gpioNum;
115 }
116 else
117 {
118 std::cerr << "Error cleaning up sysfs device\n";
119 }
120 }
121 cpuPresence[gpioNum] = resp;
122 return resp;
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700123}