blob: 09f553930714e7239efbd233fd4c868bcf99fcce [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 Khemkae2795302020-07-15 17:28:45 -070078
79 private:
Vijay Khemkab38fd582020-07-23 13:21:23 -070080 /** @brief sdbusplus bus client connection. */
Vijay Khemkae2795302020-07-15 17:28:45 -070081 sdbusplus::bus::bus& bus;
Vijay Khemkab38fd582020-07-23 13:21:23 -070082 /** @brief Sensor config from config file */
Vijay Khemka15537762020-07-22 11:44:56 -070083 HealthConfig& sensorConfig;
Vijay Khemkab38fd582020-07-23 13:21:23 -070084 /** @brief the Event Loop structure */
85 sdeventplus::Event timerEvent;
86 /** @brief Sensor Read Timer */
87 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> readTimer;
88
89 /** @brief Read sensor at regular intrval */
90 void readHealthSensor();
Vijay Khemkae2795302020-07-15 17:28:45 -070091};
92
93class HealthMon
94{
95 public:
96 HealthMon() = delete;
97 HealthMon(const HealthMon&) = delete;
98 HealthMon& operator=(const HealthMon&) = delete;
99 HealthMon(HealthMon&&) = delete;
100 HealthMon& operator=(HealthMon&&) = delete;
101 virtual ~HealthMon() = default;
102
103 /** @brief Constructs HealthMon
104 *
105 * @param[in] bus - Handle to system dbus
106 */
107 HealthMon(sdbusplus::bus::bus& bus) : bus(bus)
108 {
109 // read json file
110 sensorConfigs = getHealthConfig();
111 createHealthSensors();
112 }
113
Vijay Khemkae2795302020-07-15 17:28:45 -0700114 /** @brief Parsing Health config JSON file */
115 Json parseConfigFile(std::string configFile);
116
117 /** @brief reading config for each health sensor component */
118 void getConfigData(Json& data, HealthConfig& cfg);
119
120 /** @brief Map of the object HealthSensor */
121 std::unordered_map<std::string, std::shared_ptr<HealthSensor>>
122 healthSensors;
123
124 /** @brief Create sensors for health monitoring */
125 void createHealthSensors();
126
127 private:
128 sdbusplus::bus::bus& bus;
129 std::vector<HealthConfig> sensorConfigs;
130 std::vector<HealthConfig> getHealthConfig();
131};
132
133} // namespace health
134} // namespace phosphor