blob: 4eedfb3dc290fc0ff3b47ea85f6e4bd7510acc8f [file] [log] [blame]
Vijay Khemkae2795302020-07-15 17:28:45 -07001#include <nlohmann/json.hpp>
2#include <sdbusplus/bus.hpp>
Vijay Khemkab38fd582020-07-23 13:21:23 -07003#include <sdeventplus/clock.hpp>
Vijay Khemkae2795302020-07-15 17:28:45 -07004#include <sdeventplus/event.hpp>
Vijay Khemkab38fd582020-07-23 13:21:23 -07005#include <sdeventplus/utility/timer.hpp>
Vijay Khemkae2795302020-07-15 17:28:45 -07006#include <xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp>
7#include <xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp>
8#include <xyz/openbmc_project/Sensor/Value/server.hpp>
9
Vijay Khemka15537762020-07-22 11:44:56 -070010#include <deque>
Vijay Khemkae2795302020-07-15 17:28:45 -070011#include <map>
12#include <string>
13
14namespace phosphor
15{
16namespace health
17{
18
19using Json = nlohmann::json;
20using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
21
22using CriticalInterface =
23 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical;
24
25using WarningInterface =
26 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning;
27
28using healthIfaces =
29 sdbusplus::server::object::object<ValueIface, CriticalInterface,
30 WarningInterface>;
31
Vijay Khemka15537762020-07-22 11:44:56 -070032struct HealthConfig
33{
34 std::string name;
35 uint16_t freq;
36 uint16_t windowSize;
37 double criticalHigh;
38 double warningHigh;
39 bool criticalLog;
40 bool warningLog;
41 std::string criticalTgt;
42 std::string warningTgt;
Bruceleequantatwaf9acbd2020-10-12 15:21:42 +080043 std::string path;
Vijay Khemka15537762020-07-22 11:44:56 -070044};
45
Vijay Khemkae2795302020-07-15 17:28:45 -070046class HealthSensor : public healthIfaces
47{
48 public:
49 HealthSensor() = delete;
50 HealthSensor(const HealthSensor&) = delete;
51 HealthSensor& operator=(const HealthSensor&) = delete;
52 HealthSensor(HealthSensor&&) = delete;
53 HealthSensor& operator=(HealthSensor&&) = delete;
54 virtual ~HealthSensor() = default;
55
56 /** @brief Constructs HealthSensor
57 *
58 * @param[in] bus - Handle to system dbus
59 * @param[in] objPath - The Dbus path of health sensor
60 */
Vijay Khemka15537762020-07-22 11:44:56 -070061 HealthSensor(sdbusplus::bus::bus& bus, const char* objPath,
62 HealthConfig& sensorConfig) :
63 healthIfaces(bus, objPath),
Vijay Khemkab38fd582020-07-23 13:21:23 -070064 bus(bus), sensorConfig(sensorConfig),
65 timerEvent(sdeventplus::Event::get_default()),
66 readTimer(timerEvent, std::bind(&HealthSensor::readHealthSensor, this))
Vijay Khemka15537762020-07-22 11:44:56 -070067 {
68 initHealthSensor();
69 }
Vijay Khemkae2795302020-07-15 17:28:45 -070070
Vijay Khemka15537762020-07-22 11:44:56 -070071 /** @brief list of sensor data values */
72 std::deque<double> valQueue;
73
74 void initHealthSensor();
Vijay Khemkae2795302020-07-15 17:28:45 -070075 /** @brief Set sensor value utilization to health sensor D-bus */
Vijay Khemka15537762020-07-22 11:44:56 -070076 void setSensorValueToDbus(const double value);
Vijay Khemkae2795302020-07-15 17:28:45 -070077 /** @brief Set Sensor Threshold to D-bus at beginning */
Vijay Khemka15537762020-07-22 11:44:56 -070078 void setSensorThreshold(double criticalHigh, double warningHigh);
Vijay Khemkab7a7b8a2020-07-29 12:22:01 -070079 /** @brief Check Sensor threshold and update alarm and log */
80 void checkSensorThreshold(const double value);
Vijay Khemkae2795302020-07-15 17:28:45 -070081
82 private:
Vijay Khemkab38fd582020-07-23 13:21:23 -070083 /** @brief sdbusplus bus client connection. */
Vijay Khemkae2795302020-07-15 17:28:45 -070084 sdbusplus::bus::bus& bus;
Vijay Khemkab38fd582020-07-23 13:21:23 -070085 /** @brief Sensor config from config file */
Vijay Khemka15537762020-07-22 11:44:56 -070086 HealthConfig& sensorConfig;
Vijay Khemkab38fd582020-07-23 13:21:23 -070087 /** @brief the Event Loop structure */
88 sdeventplus::Event timerEvent;
89 /** @brief Sensor Read Timer */
90 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> readTimer;
91
92 /** @brief Read sensor at regular intrval */
93 void readHealthSensor();
Vijay Khemkae2795302020-07-15 17:28:45 -070094};
95
96class HealthMon
97{
98 public:
99 HealthMon() = delete;
100 HealthMon(const HealthMon&) = delete;
101 HealthMon& operator=(const HealthMon&) = delete;
102 HealthMon(HealthMon&&) = delete;
103 HealthMon& operator=(HealthMon&&) = delete;
104 virtual ~HealthMon() = default;
105
106 /** @brief Constructs HealthMon
107 *
108 * @param[in] bus - Handle to system dbus
109 */
110 HealthMon(sdbusplus::bus::bus& bus) : bus(bus)
111 {
112 // read json file
113 sensorConfigs = getHealthConfig();
114 createHealthSensors();
115 }
116
Vijay Khemkae2795302020-07-15 17:28:45 -0700117 /** @brief Parsing Health config JSON file */
118 Json parseConfigFile(std::string configFile);
119
120 /** @brief reading config for each health sensor component */
121 void getConfigData(Json& data, HealthConfig& cfg);
122
123 /** @brief Map of the object HealthSensor */
124 std::unordered_map<std::string, std::shared_ptr<HealthSensor>>
125 healthSensors;
126
127 /** @brief Create sensors for health monitoring */
128 void createHealthSensors();
129
130 private:
131 sdbusplus::bus::bus& bus;
132 std::vector<HealthConfig> sensorConfigs;
133 std::vector<HealthConfig> getHealthConfig();
134};
135
136} // namespace health
137} // namespace phosphor