blob: 2205c5e2a8d2dfc534f1c5157b370827d402e204 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
Patrick Ventureca44b2f2019-10-31 11:02:26 -07002#include "Utils.hpp"
3
James Feist46342ec2019-03-06 14:03:41 -08004#include <boost/asio/io_service.hpp>
Patrick Venturefd6ba732019-10-31 14:27:39 -07005#include <list>
6#include <memory>
James Feist6714a252018-09-10 15:26:18 -07007#include <nlohmann/json.hpp>
Patrick Venturefd6ba732019-10-31 14:27:39 -07008#include <string>
9#include <utility>
10#include <vector>
James Feist6714a252018-09-10 15:26:18 -070011
James Feist251c7822018-09-12 12:54:15 -070012struct Sensor;
James Feist6714a252018-09-10 15:26:18 -070013namespace thresholds
14{
15enum Level
16{
17 WARNING,
18 CRITICAL
19};
20enum Direction
21{
22 HIGH,
23 LOW
24};
25struct Threshold
26{
James Feistd8705872019-02-08 13:26:09 -080027 Threshold(const Level& lev, const Direction& dir, const double& val,
James Feist6714a252018-09-10 15:26:18 -070028 bool write = true) :
29 level(lev),
30 direction(dir), value(val), writeable(write)
31 {
32 }
33 Level level;
34 Direction direction;
35 double value;
36 bool writeable;
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -080037
38 bool operator==(const Threshold& rhs) const
39 {
40 return (level == rhs.level && direction == rhs.direction &&
41 value == rhs.value);
42 }
James Feist6714a252018-09-10 15:26:18 -070043};
44
James Feist46342ec2019-03-06 14:03:41 -080045void assertThresholds(Sensor* sensor, thresholds::Level level,
46 thresholds::Direction direction, bool assert);
47
James Feist43d32fe2019-09-04 10:35:20 -070048using TimerPair = std::pair<bool, boost::asio::deadline_timer>;
49
James Feist46342ec2019-03-06 14:03:41 -080050struct ThresholdTimer
51{
52
James Feist43d32fe2019-09-04 10:35:20 -070053 ThresholdTimer(boost::asio::io_service& ioService, Sensor* sensor) :
54 io(ioService), sensor(sensor)
James Feist46342ec2019-03-06 14:03:41 -080055 {
56 }
57
58 void startTimer(const Threshold& threshold)
59 {
James Feist43d32fe2019-09-04 10:35:20 -070060 constexpr const size_t waitTime = 5;
61 TimerPair* pair = nullptr;
James Feist46342ec2019-03-06 14:03:41 -080062
James Feist43d32fe2019-09-04 10:35:20 -070063 for (TimerPair& timer : timers)
James Feist46342ec2019-03-06 14:03:41 -080064 {
James Feist43d32fe2019-09-04 10:35:20 -070065 if (!timer.first)
66 {
67 pair = &timer;
68 break;
69 }
James Feist46342ec2019-03-06 14:03:41 -080070 }
James Feist43d32fe2019-09-04 10:35:20 -070071 if (pair == nullptr)
James Feist46342ec2019-03-06 14:03:41 -080072 {
James Feist43d32fe2019-09-04 10:35:20 -070073 pair = &timers.emplace_back(false, boost::asio::deadline_timer(io));
James Feist46342ec2019-03-06 14:03:41 -080074 }
James Feist43d32fe2019-09-04 10:35:20 -070075 pair->first = true;
76 pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
77 pair->second.async_wait(
78 [this, pair, threshold](boost::system::error_code ec) {
79 pair->first = false;
80
81 if (ec == boost::asio::error::operation_aborted)
82 {
83 return; // we're being canceled
84 }
85 else if (ec)
86 {
87 std::cerr << "timer error: " << ec.message() << "\n";
88 return;
89 }
90 if (isPowerOn())
91 {
92 assertThresholds(sensor, threshold.level,
93 threshold.direction, true);
94 }
95 });
James Feist46342ec2019-03-06 14:03:41 -080096 }
97
James Feist43d32fe2019-09-04 10:35:20 -070098 boost::asio::io_service& io;
99 std::list<TimerPair> timers;
James Feist46342ec2019-03-06 14:03:41 -0800100 Sensor* sensor;
101};
102
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200103// The common scheme for sysfs files naming is: <type><number>_<item>.
104// This function returns optionally these 3 elements as a tuple.
105std::optional<std::tuple<std::string, std::string, std::string>>
106 splitFileName(const std::filesystem::path& filePath);
107
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700108bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -0800109 const SensorData& sensorData,
110 std::vector<thresholds::Threshold>& thresholdVector,
111 const std::string* matchLabel = nullptr);
James Feist6714a252018-09-10 15:26:18 -0700112
James Feistd8705872019-02-08 13:26:09 -0800113bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds,
114 const std::string& inputPath,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700115 const double& scaleFactor,
116 const double& offset = 0);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700117bool hasCriticalInterface(
James Feistd8705872019-02-08 13:26:09 -0800118 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist251c7822018-09-12 12:54:15 -0700119
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700120bool hasWarningInterface(
James Feistd8705872019-02-08 13:26:09 -0800121 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist6714a252018-09-10 15:26:18 -0700122
James Feistd8705872019-02-08 13:26:09 -0800123void persistThreshold(const std::string& baseInterface, const std::string& path,
124 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800125 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800126 size_t thresholdCount, const std::string& label);
James Feist251c7822018-09-12 12:54:15 -0700127
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800128void updateThresholds(Sensor* sensor);
James Feistdc6c55f2018-10-31 12:53:20 -0700129// returns false if a critical threshold has been crossed, true otherwise
James Feistd8705872019-02-08 13:26:09 -0800130bool checkThresholds(Sensor* sensor);
James Feist46342ec2019-03-06 14:03:41 -0800131void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800132
James Feist6714a252018-09-10 15:26:18 -0700133} // namespace thresholds