Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 1 | #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 Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame^] | 8 | #include <deque> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 9 | #include <map> |
| 10 | #include <string> |
| 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace health |
| 15 | { |
| 16 | |
| 17 | using Json = nlohmann::json; |
| 18 | using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; |
| 19 | |
| 20 | using CriticalInterface = |
| 21 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical; |
| 22 | |
| 23 | using WarningInterface = |
| 24 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning; |
| 25 | |
| 26 | using healthIfaces = |
| 27 | sdbusplus::server::object::object<ValueIface, CriticalInterface, |
| 28 | WarningInterface>; |
| 29 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame^] | 30 | struct 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 Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 43 | class 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 Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame^] | 58 | 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 Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 65 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame^] | 66 | /** @brief list of sensor data values */ |
| 67 | std::deque<double> valQueue; |
| 68 | |
| 69 | void initHealthSensor(); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 70 | /** @brief Set sensor value utilization to health sensor D-bus */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame^] | 71 | void setSensorValueToDbus(const double value); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 72 | /** @brief Set Sensor Threshold to D-bus at beginning */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame^] | 73 | void setSensorThreshold(double criticalHigh, double warningHigh); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 74 | |
| 75 | private: |
| 76 | sdbusplus::bus::bus& bus; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame^] | 77 | HealthConfig& sensorConfig; |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | class 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 Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 101 | /** @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 |