James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame^] | 3 | #include "Utils.hpp" |
| 4 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 5 | #include <Thresholds.hpp> |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame^] | 6 | #include <boost/container/flat_map.hpp> |
| 7 | #include <filesystem> |
| 8 | #include <fstream> |
| 9 | #include <gpiod.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 10 | #include <sdbusplus/asio/object_server.hpp> |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 11 | #include <sensor.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 12 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 13 | class CPUSensor : public Sensor |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 14 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 15 | public: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 16 | 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 Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 19 | boost::asio::io_service& io, const std::string& sensorName, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 20 | std::vector<thresholds::Threshold>&& thresholds, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 21 | const std::string& configuration, int cpuId, bool show); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 22 | ~CPUSensor(); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 23 | static constexpr unsigned int sensorScaleFactor = 1000; |
| 24 | static constexpr unsigned int sensorPollMs = 1000; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 25 | static constexpr size_t warnAfterErrorCount = 10; |
| 26 | static constexpr double maxReading = 127; |
| 27 | static constexpr double minReading = -128; |
| 28 | static constexpr const char* labelTcontrol = "Tcontrol"; |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 29 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 30 | private: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 31 | sdbusplus::asio::object_server& objServer; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 32 | boost::asio::posix::stream_descriptor inputDev; |
| 33 | boost::asio::deadline_timer waitTimer; |
| 34 | boost::asio::streambuf readBuf; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 35 | std::string nameTcontrol; |
| 36 | double privTcontrol; |
| 37 | bool show; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 38 | int errCount; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 39 | void setupRead(void); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 40 | void handleResponse(const boost::system::error_code& err); |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 41 | void checkThresholds(void) override; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | }; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 43 | |
| 44 | extern boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> |
| 45 | gCpuSensors; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame^] | 46 | |
| 47 | // this is added to cpusensor.hpp to avoid having every sensor have to link |
| 48 | // against libgpiod, if another sensor needs it we may move it to utils |
| 49 | inline bool hostIsPresent(size_t gpioNum) |
| 50 | { |
| 51 | static boost::container::flat_map<size_t, bool> cpuPresence; |
| 52 | |
| 53 | auto findIndex = cpuPresence.find(gpioNum); |
| 54 | if (findIndex != cpuPresence.end()) |
| 55 | { |
| 56 | return findIndex->second; |
| 57 | } |
| 58 | |
| 59 | constexpr size_t sgpioBase = 232; |
| 60 | |
| 61 | // check if sysfs has device |
| 62 | bool sysfs = std::filesystem::exists(gpioPath + std::string("gpio") + |
| 63 | std::to_string(gpioNum)); |
| 64 | |
| 65 | // todo: delete this when we remove all sysfs code |
| 66 | if (sysfs) |
| 67 | { |
| 68 | // close it, we'll reopen it at the end |
| 69 | std::ofstream unexport(gpioPath + std::string("unexport")); |
| 70 | if (unexport.good()) |
| 71 | { |
| 72 | unexport << gpioNum; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | std::cerr << "Error cleaning up sysfs device\n"; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | size_t chipNum = (gpioNum - sgpioBase) / 8; |
| 81 | size_t index = (gpioNum - sgpioBase) % 8; |
| 82 | gpiod::chip chip("gpiochip" + std::to_string(chipNum)); |
| 83 | auto line = chip.get_line(index); |
| 84 | |
| 85 | if (!line) |
| 86 | { |
| 87 | std::cerr << "Error requesting gpio\n"; |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | bool resp = true; |
| 92 | try |
| 93 | { |
| 94 | line.request({"adcsensor", gpiod::line_request::DIRECTION_INPUT}); |
| 95 | resp = !line.get_value(); |
| 96 | } |
| 97 | catch (std::system_error&) |
| 98 | { |
| 99 | std::cerr << "Error reading gpio\n"; |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | // todo: delete this when we remove all sysfs code |
| 104 | if (sysfs) |
| 105 | { |
| 106 | // reopen it |
| 107 | std::ofstream populate(gpioPath + std::string("export")); |
| 108 | if (populate.good()) |
| 109 | { |
| 110 | populate << gpioNum; |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | std::cerr << "Error cleaning up sysfs device\n"; |
| 115 | } |
| 116 | } |
| 117 | cpuPresence[gpioNum] = resp; |
| 118 | return resp; |
| 119 | } |