blob: e8230fa0590ffda17a20260b52db62111780e833 [file] [log] [blame]
Vijay Khemkae2795302020-07-15 17:28:45 -07001#include <nlohmann/json.hpp>
2#include <sdbusplus/bus.hpp>
3#include <sdeventplus/event.hpp>
4#include <xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp>
5#include <xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp>
6#include <xyz/openbmc_project/Sensor/Value/server.hpp>
7
Vijay Khemka15537762020-07-22 11:44:56 -07008#include <deque>
Vijay Khemkae2795302020-07-15 17:28:45 -07009#include <map>
10#include <string>
11
12namespace phosphor
13{
14namespace health
15{
16
17using Json = nlohmann::json;
18using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
19
20using CriticalInterface =
21 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical;
22
23using WarningInterface =
24 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning;
25
26using healthIfaces =
27 sdbusplus::server::object::object<ValueIface, CriticalInterface,
28 WarningInterface>;
29
Vijay Khemka15537762020-07-22 11:44:56 -070030struct HealthConfig
31{
32 std::string name;
33 uint16_t freq;
34 uint16_t windowSize;
35 double criticalHigh;
36 double warningHigh;
37 bool criticalLog;
38 bool warningLog;
39 std::string criticalTgt;
40 std::string warningTgt;
41};
42
Vijay Khemkae2795302020-07-15 17:28:45 -070043class HealthSensor : public healthIfaces
44{
45 public:
46 HealthSensor() = delete;
47 HealthSensor(const HealthSensor&) = delete;
48 HealthSensor& operator=(const HealthSensor&) = delete;
49 HealthSensor(HealthSensor&&) = delete;
50 HealthSensor& operator=(HealthSensor&&) = delete;
51 virtual ~HealthSensor() = default;
52
53 /** @brief Constructs HealthSensor
54 *
55 * @param[in] bus - Handle to system dbus
56 * @param[in] objPath - The Dbus path of health sensor
57 */
Vijay Khemka15537762020-07-22 11:44:56 -070058 HealthSensor(sdbusplus::bus::bus& bus, const char* objPath,
59 HealthConfig& sensorConfig) :
60 healthIfaces(bus, objPath),
61 bus(bus), sensorConfig(sensorConfig)
62 {
63 initHealthSensor();
64 }
Vijay Khemkae2795302020-07-15 17:28:45 -070065
Vijay Khemka15537762020-07-22 11:44:56 -070066 /** @brief list of sensor data values */
67 std::deque<double> valQueue;
68
69 void initHealthSensor();
Vijay Khemkae2795302020-07-15 17:28:45 -070070 /** @brief Set sensor value utilization to health sensor D-bus */
Vijay Khemka15537762020-07-22 11:44:56 -070071 void setSensorValueToDbus(const double value);
Vijay Khemkae2795302020-07-15 17:28:45 -070072 /** @brief Set Sensor Threshold to D-bus at beginning */
Vijay Khemka15537762020-07-22 11:44:56 -070073 void setSensorThreshold(double criticalHigh, double warningHigh);
Vijay Khemkae2795302020-07-15 17:28:45 -070074
75 private:
76 sdbusplus::bus::bus& bus;
Vijay Khemka15537762020-07-22 11:44:56 -070077 HealthConfig& sensorConfig;
Vijay Khemkae2795302020-07-15 17:28:45 -070078};
79
80class HealthMon
81{
82 public:
83 HealthMon() = delete;
84 HealthMon(const HealthMon&) = delete;
85 HealthMon& operator=(const HealthMon&) = delete;
86 HealthMon(HealthMon&&) = delete;
87 HealthMon& operator=(HealthMon&&) = delete;
88 virtual ~HealthMon() = default;
89
90 /** @brief Constructs HealthMon
91 *
92 * @param[in] bus - Handle to system dbus
93 */
94 HealthMon(sdbusplus::bus::bus& bus) : bus(bus)
95 {
96 // read json file
97 sensorConfigs = getHealthConfig();
98 createHealthSensors();
99 }
100
Vijay Khemkae2795302020-07-15 17:28:45 -0700101 /** @brief Parsing Health config JSON file */
102 Json parseConfigFile(std::string configFile);
103
104 /** @brief reading config for each health sensor component */
105 void getConfigData(Json& data, HealthConfig& cfg);
106
107 /** @brief Map of the object HealthSensor */
108 std::unordered_map<std::string, std::shared_ptr<HealthSensor>>
109 healthSensors;
110
111 /** @brief Create sensors for health monitoring */
112 void createHealthSensors();
113
114 private:
115 sdbusplus::bus::bus& bus;
116 std::vector<HealthConfig> sensorConfigs;
117 std::vector<HealthConfig> getHealthConfig();
118};
119
120} // namespace health
121} // namespace phosphor