| 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; | 
 | 43 | }; | 
 | 44 |  | 
| Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 45 | class 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 Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 60 |     HealthSensor(sdbusplus::bus::bus& bus, const char* objPath, | 
 | 61 |                  HealthConfig& sensorConfig) : | 
 | 62 |         healthIfaces(bus, objPath), | 
| Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 63 |         bus(bus), sensorConfig(sensorConfig), | 
 | 64 |         timerEvent(sdeventplus::Event::get_default()), | 
 | 65 |         readTimer(timerEvent, std::bind(&HealthSensor::readHealthSensor, this)) | 
| Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 66 |     { | 
 | 67 |         initHealthSensor(); | 
 | 68 |     } | 
| Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 69 |  | 
| Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 70 |     /** @brief list of sensor data values */ | 
 | 71 |     std::deque<double> valQueue; | 
 | 72 |  | 
 | 73 |     void initHealthSensor(); | 
| Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 74 |     /** @brief Set sensor value utilization to health sensor D-bus  */ | 
| Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 75 |     void setSensorValueToDbus(const double value); | 
| Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 76 |     /** @brief Set Sensor Threshold to D-bus at beginning */ | 
| Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 77 |     void setSensorThreshold(double criticalHigh, double warningHigh); | 
| Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 78 |     /** @brief Check Sensor threshold and update alarm and log */ | 
 | 79 |     void checkSensorThreshold(const double value); | 
| Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 80 |  | 
 | 81 |   private: | 
| Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 82 |     /** @brief sdbusplus bus client connection. */ | 
| Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 83 |     sdbusplus::bus::bus& bus; | 
| Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 84 |     /** @brief Sensor config from config file */ | 
| Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 85 |     HealthConfig& sensorConfig; | 
| Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 86 |     /** @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 Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 93 | }; | 
 | 94 |  | 
 | 95 | class 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 Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 116 |     /** @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 |