Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 1 | #include <nlohmann/json.hpp> |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame^] | 2 | #include <phosphor-logging/lg2.hpp> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 3 | #include <sdbusplus/bus.hpp> |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 4 | #include <sdbusplus/message.hpp> |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 5 | #include <sdeventplus/clock.hpp> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 6 | #include <sdeventplus/event.hpp> |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 7 | #include <sdeventplus/utility/timer.hpp> |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Association/Definitions/server.hpp> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 9 | #include <xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp> |
| 10 | #include <xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp> |
| 11 | #include <xyz/openbmc_project/Sensor/Value/server.hpp> |
| 12 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 13 | #include <deque> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 14 | #include <map> |
| 15 | #include <string> |
| 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace health |
| 20 | { |
| 21 | |
| 22 | using Json = nlohmann::json; |
| 23 | using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; |
| 24 | |
| 25 | using CriticalInterface = |
| 26 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical; |
| 27 | |
| 28 | using WarningInterface = |
| 29 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning; |
| 30 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 31 | using AssociationDefinitionInterface = |
| 32 | sdbusplus::xyz::openbmc_project::Association::server::Definitions; |
| 33 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 34 | using healthIfaces = |
| 35 | sdbusplus::server::object::object<ValueIface, CriticalInterface, |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 36 | WarningInterface, |
| 37 | AssociationDefinitionInterface>; |
| 38 | |
| 39 | using AssociationTuple = std::tuple<std::string, std::string, std::string>; |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 40 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 41 | struct HealthConfig |
| 42 | { |
| 43 | std::string name; |
| 44 | uint16_t freq; |
| 45 | uint16_t windowSize; |
| 46 | double criticalHigh; |
| 47 | double warningHigh; |
| 48 | bool criticalLog; |
| 49 | bool warningLog; |
| 50 | std::string criticalTgt; |
| 51 | std::string warningTgt; |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 52 | std::string path; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 55 | class HealthSensor : public healthIfaces |
| 56 | { |
| 57 | public: |
| 58 | HealthSensor() = delete; |
| 59 | HealthSensor(const HealthSensor&) = delete; |
| 60 | HealthSensor& operator=(const HealthSensor&) = delete; |
| 61 | HealthSensor(HealthSensor&&) = delete; |
| 62 | HealthSensor& operator=(HealthSensor&&) = delete; |
| 63 | virtual ~HealthSensor() = default; |
| 64 | |
| 65 | /** @brief Constructs HealthSensor |
| 66 | * |
| 67 | * @param[in] bus - Handle to system dbus |
| 68 | * @param[in] objPath - The Dbus path of health sensor |
| 69 | */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 70 | HealthSensor(sdbusplus::bus::bus& bus, const char* objPath, |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 71 | HealthConfig& sensorConfig, |
| 72 | const std::vector<std::string>& bmcIds) : |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 73 | healthIfaces(bus, objPath), |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 74 | bus(bus), sensorConfig(sensorConfig), |
| 75 | timerEvent(sdeventplus::Event::get_default()), |
| 76 | readTimer(timerEvent, std::bind(&HealthSensor::readHealthSensor, this)) |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 77 | { |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 78 | initHealthSensor(bmcIds); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 79 | } |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 80 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 81 | /** @brief list of sensor data values */ |
| 82 | std::deque<double> valQueue; |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 83 | /** @brief Initialize sensor, set default value and association */ |
| 84 | void initHealthSensor(const std::vector<std::string>& bmcIds); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 85 | /** @brief Set sensor value utilization to health sensor D-bus */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 86 | void setSensorValueToDbus(const double value); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 87 | /** @brief Set Sensor Threshold to D-bus at beginning */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 88 | void setSensorThreshold(double criticalHigh, double warningHigh); |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 89 | /** @brief Check Sensor threshold and update alarm and log */ |
| 90 | void checkSensorThreshold(const double value); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 91 | |
| 92 | private: |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 93 | /** @brief sdbusplus bus client connection. */ |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 94 | sdbusplus::bus::bus& bus; |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 95 | /** @brief Sensor config from config file */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 96 | HealthConfig& sensorConfig; |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 97 | /** @brief the Event Loop structure */ |
| 98 | sdeventplus::Event timerEvent; |
| 99 | /** @brief Sensor Read Timer */ |
| 100 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> readTimer; |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 101 | /** @brief Read sensor at regular intrval */ |
| 102 | void readHealthSensor(); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | class HealthMon |
| 106 | { |
| 107 | public: |
| 108 | HealthMon() = delete; |
| 109 | HealthMon(const HealthMon&) = delete; |
| 110 | HealthMon& operator=(const HealthMon&) = delete; |
| 111 | HealthMon(HealthMon&&) = delete; |
| 112 | HealthMon& operator=(HealthMon&&) = delete; |
| 113 | virtual ~HealthMon() = default; |
| 114 | |
| 115 | /** @brief Constructs HealthMon |
| 116 | * |
| 117 | * @param[in] bus - Handle to system dbus |
| 118 | */ |
| 119 | HealthMon(sdbusplus::bus::bus& bus) : bus(bus) |
| 120 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame^] | 121 | PHOSPHOR_LOG2_USING; |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 122 | std::vector<std::string> bmcIds = {}; |
| 123 | |
| 124 | // Find all BMCs (DBus objects implementing the |
| 125 | // Inventory.Item.Bmc interface that may be created by |
| 126 | // configuring the Inventory Manager) |
| 127 | sdbusplus::message::message msg = bus.new_method_call( |
| 128 | "xyz.openbmc_project.ObjectMapper", |
| 129 | "/xyz/openbmc_project/object_mapper", |
| 130 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths"); |
| 131 | |
| 132 | // Search term |
| 133 | msg.append("/xyz/openbmc_project/inventory/system"); |
| 134 | |
| 135 | // Limit the depth to 2. Example of "depth": |
| 136 | // /xyz/openbmc_project/inventory/system/chassis has a depth of 1 |
| 137 | // since it has 1 '/' after "/xyz/openbmc_project/inventory/system". |
| 138 | msg.append(2); |
| 139 | |
| 140 | // Must have the Inventory.Item.Bmc interface |
| 141 | msg.append( |
| 142 | std::vector<std::string>{"xyz.openbmc_project.Inventory.Item.Bmc"}); |
| 143 | |
| 144 | sdbusplus::message::message reply = bus.call(msg, 0); |
| 145 | if (reply.get_signature() == std::string("as")) |
| 146 | { |
| 147 | reply.read(bmcIds); |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame^] | 148 | info("BMC inventory found"); |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 149 | } |
| 150 | else |
| 151 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame^] | 152 | warning("Did not find BMC inventory, cannot create association"); |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Read JSON file |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 156 | sensorConfigs = getHealthConfig(); |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 157 | |
| 158 | // Create health sensors |
| 159 | createHealthSensors(bmcIds); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 162 | /** @brief Parse Health config JSON file */ |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 163 | Json parseConfigFile(std::string configFile); |
| 164 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 165 | /** @brief Read config for each health sensor component */ |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 166 | void getConfigData(Json& data, HealthConfig& cfg); |
| 167 | |
| 168 | /** @brief Map of the object HealthSensor */ |
| 169 | std::unordered_map<std::string, std::shared_ptr<HealthSensor>> |
| 170 | healthSensors; |
| 171 | |
| 172 | /** @brief Create sensors for health monitoring */ |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 173 | void createHealthSensors(const std::vector<std::string>& bmcIds); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 174 | |
| 175 | private: |
| 176 | sdbusplus::bus::bus& bus; |
| 177 | std::vector<HealthConfig> sensorConfigs; |
| 178 | std::vector<HealthConfig> getHealthConfig(); |
| 179 | }; |
| 180 | |
| 181 | } // namespace health |
| 182 | } // namespace phosphor |