blob: 33c6f8fe5fa3e42b3d1238f452a93bfd14ce45d9 [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
Yong Li10306bd2020-04-21 16:08:28 +080048struct TimerUsed
49{
50 bool used;
51 Level level;
52 Direction direction;
53};
54
55using TimerPair = std::pair<struct TimerUsed, boost::asio::deadline_timer>;
James Feist43d32fe2019-09-04 10:35:20 -070056
James Feist46342ec2019-03-06 14:03:41 -080057struct ThresholdTimer
58{
59
James Feist43d32fe2019-09-04 10:35:20 -070060 ThresholdTimer(boost::asio::io_service& ioService, Sensor* sensor) :
61 io(ioService), sensor(sensor)
James Feist46342ec2019-03-06 14:03:41 -080062 {
63 }
64
Yong Li10306bd2020-04-21 16:08:28 +080065 void stopTimer(const Threshold& threshold)
66 {
67 struct TimerUsed timerUsed = {};
68 for (TimerPair& timer : timers)
69 {
70 timerUsed = timer.first;
71 if (timerUsed.used)
72 {
73 if ((timerUsed.level == threshold.level) &&
74 (timerUsed.direction == threshold.direction))
75 {
76 timer.second.cancel();
77 }
78 }
79 }
80 }
81
James Feist46342ec2019-03-06 14:03:41 -080082 void startTimer(const Threshold& threshold)
83 {
Yong Li10306bd2020-04-21 16:08:28 +080084 struct TimerUsed timerUsed = {};
James Feist43d32fe2019-09-04 10:35:20 -070085 constexpr const size_t waitTime = 5;
86 TimerPair* pair = nullptr;
James Feist46342ec2019-03-06 14:03:41 -080087
James Feist43d32fe2019-09-04 10:35:20 -070088 for (TimerPair& timer : timers)
James Feist46342ec2019-03-06 14:03:41 -080089 {
Yong Li10306bd2020-04-21 16:08:28 +080090 if (!timer.first.used)
James Feist43d32fe2019-09-04 10:35:20 -070091 {
92 pair = &timer;
93 break;
94 }
James Feist46342ec2019-03-06 14:03:41 -080095 }
James Feist43d32fe2019-09-04 10:35:20 -070096 if (pair == nullptr)
James Feist46342ec2019-03-06 14:03:41 -080097 {
Yong Li10306bd2020-04-21 16:08:28 +080098 pair = &timers.emplace_back(timerUsed,
99 boost::asio::deadline_timer(io));
James Feist46342ec2019-03-06 14:03:41 -0800100 }
Yong Li10306bd2020-04-21 16:08:28 +0800101 pair->first.used = true;
102 pair->first.level = threshold.level;
103 pair->first.direction = threshold.direction;
James Feist43d32fe2019-09-04 10:35:20 -0700104 pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
105 pair->second.async_wait(
106 [this, pair, threshold](boost::system::error_code ec) {
Yong Li10306bd2020-04-21 16:08:28 +0800107 pair->first.used = false;
James Feist43d32fe2019-09-04 10:35:20 -0700108
109 if (ec == boost::asio::error::operation_aborted)
110 {
111 return; // we're being canceled
112 }
113 else if (ec)
114 {
115 std::cerr << "timer error: " << ec.message() << "\n";
116 return;
117 }
118 if (isPowerOn())
119 {
120 assertThresholds(sensor, threshold.level,
121 threshold.direction, true);
122 }
123 });
James Feist46342ec2019-03-06 14:03:41 -0800124 }
125
James Feist43d32fe2019-09-04 10:35:20 -0700126 boost::asio::io_service& io;
127 std::list<TimerPair> timers;
James Feist46342ec2019-03-06 14:03:41 -0800128 Sensor* sensor;
129};
130
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200131// The common scheme for sysfs files naming is: <type><number>_<item>.
132// This function returns optionally these 3 elements as a tuple.
133std::optional<std::tuple<std::string, std::string, std::string>>
134 splitFileName(const std::filesystem::path& filePath);
135
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700136bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -0800137 const SensorData& sensorData,
138 std::vector<thresholds::Threshold>& thresholdVector,
139 const std::string* matchLabel = nullptr);
James Feist6714a252018-09-10 15:26:18 -0700140
James Feistd8705872019-02-08 13:26:09 -0800141bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds,
142 const std::string& inputPath,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700143 const double& scaleFactor,
144 const double& offset = 0);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700145bool hasCriticalInterface(
James Feistd8705872019-02-08 13:26:09 -0800146 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist251c7822018-09-12 12:54:15 -0700147
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700148bool hasWarningInterface(
James Feistd8705872019-02-08 13:26:09 -0800149 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist6714a252018-09-10 15:26:18 -0700150
James Feistd8705872019-02-08 13:26:09 -0800151void persistThreshold(const std::string& baseInterface, const std::string& path,
152 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800153 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800154 size_t thresholdCount, const std::string& label);
James Feist251c7822018-09-12 12:54:15 -0700155
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800156void updateThresholds(Sensor* sensor);
James Feistdc6c55f2018-10-31 12:53:20 -0700157// returns false if a critical threshold has been crossed, true otherwise
James Feistd8705872019-02-08 13:26:09 -0800158bool checkThresholds(Sensor* sensor);
James Feist46342ec2019-03-06 14:03:41 -0800159void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800160
James Feist6714a252018-09-10 15:26:18 -0700161} // namespace thresholds