Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 1 | #include <nlohmann/json.hpp> |
| 2 | #include <sdbusplus/bus.hpp> |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 3 | #include <sdeventplus/clock.hpp> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 4 | #include <sdeventplus/event.hpp> |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 5 | #include <sdeventplus/utility/timer.hpp> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 6 | #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 Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 10 | #include <deque> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 11 | #include <map> |
| 12 | #include <string> |
| 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace health |
| 17 | { |
| 18 | |
| 19 | using Json = nlohmann::json; |
| 20 | using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; |
| 21 | |
| 22 | using CriticalInterface = |
| 23 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical; |
| 24 | |
| 25 | using WarningInterface = |
| 26 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning; |
| 27 | |
| 28 | using healthIfaces = |
| 29 | sdbusplus::server::object::object<ValueIface, CriticalInterface, |
| 30 | WarningInterface>; |
| 31 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 32 | struct 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; |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame^] | 43 | std::string path; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 46 | class 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 Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 61 | HealthSensor(sdbusplus::bus::bus& bus, const char* objPath, |
| 62 | HealthConfig& sensorConfig) : |
| 63 | healthIfaces(bus, objPath), |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 64 | bus(bus), sensorConfig(sensorConfig), |
| 65 | timerEvent(sdeventplus::Event::get_default()), |
| 66 | readTimer(timerEvent, std::bind(&HealthSensor::readHealthSensor, this)) |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 67 | { |
| 68 | initHealthSensor(); |
| 69 | } |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 70 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 71 | /** @brief list of sensor data values */ |
| 72 | std::deque<double> valQueue; |
| 73 | |
| 74 | void initHealthSensor(); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 75 | /** @brief Set sensor value utilization to health sensor D-bus */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 76 | void setSensorValueToDbus(const double value); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 77 | /** @brief Set Sensor Threshold to D-bus at beginning */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 78 | void setSensorThreshold(double criticalHigh, double warningHigh); |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 79 | /** @brief Check Sensor threshold and update alarm and log */ |
| 80 | void checkSensorThreshold(const double value); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 81 | |
| 82 | private: |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 83 | /** @brief sdbusplus bus client connection. */ |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 84 | sdbusplus::bus::bus& bus; |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 85 | /** @brief Sensor config from config file */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 86 | HealthConfig& sensorConfig; |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 87 | /** @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 Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | class 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 Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 117 | /** @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 |