blob: 3534623a21acb7a63722279cd909f8c3d46cb180 [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;
43};
44
Vijay Khemkae2795302020-07-15 17:28:45 -070045class HealthSensor : public healthIfaces
46{
47 public:
48 HealthSensor() = delete;
49 HealthSensor(const HealthSensor&) = delete;
50 HealthSensor& operator=(const HealthSensor&) = delete;
51 HealthSensor(HealthSensor&&) = delete;
52 HealthSensor& operator=(HealthSensor&&) = delete;
53 virtual ~HealthSensor() = default;
54
55 /** @brief Constructs HealthSensor
56 *
57 * @param[in] bus - Handle to system dbus
58 * @param[in] objPath - The Dbus path of health sensor
59 */
Vijay Khemka15537762020-07-22 11:44:56 -070060 HealthSensor(sdbusplus::bus::bus& bus, const char* objPath,
61 HealthConfig& sensorConfig) :
62 healthIfaces(bus, objPath),
Vijay Khemkab38fd582020-07-23 13:21:23 -070063 bus(bus), sensorConfig(sensorConfig),
64 timerEvent(sdeventplus::Event::get_default()),
65 readTimer(timerEvent, std::bind(&HealthSensor::readHealthSensor, this))
Vijay Khemka15537762020-07-22 11:44:56 -070066 {
67 initHealthSensor();
68 }
Vijay Khemkae2795302020-07-15 17:28:45 -070069
Vijay Khemka15537762020-07-22 11:44:56 -070070 /** @brief list of sensor data values */
71 std::deque<double> valQueue;
72
73 void initHealthSensor();
Vijay Khemkae2795302020-07-15 17:28:45 -070074 /** @brief Set sensor value utilization to health sensor D-bus */
Vijay Khemka15537762020-07-22 11:44:56 -070075 void setSensorValueToDbus(const double value);
Vijay Khemkae2795302020-07-15 17:28:45 -070076 /** @brief Set Sensor Threshold to D-bus at beginning */
Vijay Khemka15537762020-07-22 11:44:56 -070077 void setSensorThreshold(double criticalHigh, double warningHigh);
Vijay Khemkab7a7b8a2020-07-29 12:22:01 -070078 /** @brief Check Sensor threshold and update alarm and log */
79 void checkSensorThreshold(const double value);
Vijay Khemkae2795302020-07-15 17:28:45 -070080
81 private:
Vijay Khemkab38fd582020-07-23 13:21:23 -070082 /** @brief sdbusplus bus client connection. */
Vijay Khemkae2795302020-07-15 17:28:45 -070083 sdbusplus::bus::bus& bus;
Vijay Khemkab38fd582020-07-23 13:21:23 -070084 /** @brief Sensor config from config file */
Vijay Khemka15537762020-07-22 11:44:56 -070085 HealthConfig& sensorConfig;
Vijay Khemkab38fd582020-07-23 13:21:23 -070086 /** @brief the Event Loop structure */
87 sdeventplus::Event timerEvent;
88 /** @brief Sensor Read Timer */
89 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> readTimer;
90
91 /** @brief Read sensor at regular intrval */
92 void readHealthSensor();
Vijay Khemkae2795302020-07-15 17:28:45 -070093};
94
95class HealthMon
96{
97 public:
98 HealthMon() = delete;
99 HealthMon(const HealthMon&) = delete;
100 HealthMon& operator=(const HealthMon&) = delete;
101 HealthMon(HealthMon&&) = delete;
102 HealthMon& operator=(HealthMon&&) = delete;
103 virtual ~HealthMon() = default;
104
105 /** @brief Constructs HealthMon
106 *
107 * @param[in] bus - Handle to system dbus
108 */
109 HealthMon(sdbusplus::bus::bus& bus) : bus(bus)
110 {
111 // read json file
112 sensorConfigs = getHealthConfig();
113 createHealthSensors();
114 }
115
Vijay Khemkae2795302020-07-15 17:28:45 -0700116 /** @brief Parsing Health config JSON file */
117 Json parseConfigFile(std::string configFile);
118
119 /** @brief reading config for each health sensor component */
120 void getConfigData(Json& data, HealthConfig& cfg);
121
122 /** @brief Map of the object HealthSensor */
123 std::unordered_map<std::string, std::shared_ptr<HealthSensor>>
124 healthSensors;
125
126 /** @brief Create sensors for health monitoring */
127 void createHealthSensors();
128
129 private:
130 sdbusplus::bus::bus& bus;
131 std::vector<HealthConfig> sensorConfigs;
132 std::vector<HealthConfig> getHealthConfig();
133};
134
135} // namespace health
136} // namespace phosphor