Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 1 | #include "dbusSensor.hpp" |
Vijay Khemka | 92d648b | 2020-08-21 18:55:07 -0700 | [diff] [blame] | 2 | #include "exprtkTools.hpp" |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 3 | #include "thresholds.hpp" |
| 4 | |
| 5 | #include <fmt/format.h> |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 6 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 7 | #include <nlohmann/json.hpp> |
| 8 | #include <sdbusplus/bus.hpp> |
Lei YU | 0fcf0e1 | 2021-06-04 11:14:17 +0800 | [diff] [blame^] | 9 | #include <xyz/openbmc_project/Association/Definitions/server.hpp> |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Sensor/Value/server.hpp> |
| 11 | |
| 12 | #include <map> |
| 13 | #include <string> |
| 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace virtualSensor |
| 18 | { |
| 19 | |
| 20 | using Json = nlohmann::json; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 21 | |
| 22 | template <typename... T> |
| 23 | using ServerObject = typename sdbusplus::server::object::object<T...>; |
| 24 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 25 | using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 26 | using ValueObject = ServerObject<ValueIface>; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 27 | |
Lei YU | 0fcf0e1 | 2021-06-04 11:14:17 +0800 | [diff] [blame^] | 28 | using AssociationIface = |
| 29 | sdbusplus::xyz::openbmc_project::Association::server::Definitions; |
| 30 | using AssociationObject = ServerObject<AssociationIface>; |
| 31 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 32 | class SensorParam |
| 33 | { |
| 34 | public: |
| 35 | SensorParam() = delete; |
| 36 | virtual ~SensorParam() = default; |
| 37 | |
| 38 | enum ParamType |
| 39 | { |
| 40 | constParam, |
| 41 | dbusParam |
| 42 | }; |
| 43 | |
| 44 | /** @brief Constructs SensorParam (type = constParam) |
| 45 | * |
| 46 | * @param[in] value - Value of constant parameter |
| 47 | */ |
| 48 | explicit SensorParam(double value) : value(value), paramType(constParam) |
| 49 | {} |
| 50 | |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 51 | /** @brief Constructs SensorParam (type = dbusParam) |
| 52 | * |
| 53 | * @param[in] bus - Handle to system dbus |
| 54 | * @param[in] path - The Dbus path of sensor |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 55 | * @param[in] ctx - sensor context for update |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 56 | */ |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 57 | SensorParam(sdbusplus::bus::bus& bus, std::string path, void* ctx) : |
| 58 | dbusSensor(std::make_unique<DbusSensor>(bus, path, ctx)), |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 59 | paramType(dbusParam) |
| 60 | {} |
| 61 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 62 | /** @brief Get sensor value property from D-bus interface */ |
| 63 | double getParamValue(); |
| 64 | |
| 65 | private: |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 66 | std::unique_ptr<DbusSensor> dbusSensor = nullptr; |
| 67 | double value = 0; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 68 | ParamType paramType; |
| 69 | }; |
| 70 | |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 71 | class VirtualSensor : public ValueObject |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 72 | { |
| 73 | public: |
| 74 | VirtualSensor() = delete; |
| 75 | virtual ~VirtualSensor() = default; |
| 76 | |
| 77 | /** @brief Constructs VirtualSensor |
| 78 | * |
| 79 | * @param[in] bus - Handle to system dbus |
| 80 | * @param[in] objPath - The Dbus path of sensor |
| 81 | * @param[in] sensorConfig - Json object for sensor config |
| 82 | */ |
| 83 | VirtualSensor(sdbusplus::bus::bus& bus, const char* objPath, |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 84 | const Json& sensorConfig, const std::string& name) : |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 85 | ValueObject(bus, objPath), |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 86 | bus(bus), name(name) |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 87 | { |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 88 | initVirtualSensor(sensorConfig, objPath); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 91 | /** @brief Set sensor value */ |
| 92 | void setSensorValue(double value); |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 93 | /** @brief Update sensor at regular intrval */ |
| 94 | void updateVirtualSensor(); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 95 | |
| 96 | /** @brief Map of list of parameters */ |
| 97 | using ParamMap = |
| 98 | std::unordered_map<std::string, std::unique_ptr<SensorParam>>; |
| 99 | ParamMap paramMap; |
| 100 | |
| 101 | private: |
| 102 | /** @brief sdbusplus bus client connection. */ |
| 103 | sdbusplus::bus::bus& bus; |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 104 | /** @brief name of sensor */ |
| 105 | std::string name; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 106 | /** @brief Expression string for virtual sensor value calculations */ |
| 107 | std::string exprStr; |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 108 | /** @brief symbol table from exprtk */ |
| 109 | exprtk::symbol_table<double> symbols{}; |
| 110 | /** @brief expression from exprtk to calculate sensor value */ |
| 111 | exprtk::expression<double> expression{}; |
Matt Spinler | 9f1ef4f | 2020-11-09 15:59:11 -0600 | [diff] [blame] | 112 | /** @brief The vecops package so the expression can use vectors */ |
| 113 | exprtk::rtl::vecops::package<double> vecopsPackage; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 114 | |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 115 | /** @brief The critical threshold interface object */ |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 116 | std::unique_ptr<Threshold<CriticalObject>> criticalIface; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 117 | /** @brief The warning threshold interface object */ |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 118 | std::unique_ptr<Threshold<WarningObject>> warningIface; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 119 | /** @brief The soft shutdown threshold interface object */ |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 120 | std::unique_ptr<Threshold<SoftShutdownObject>> softShutdownIface; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 121 | /** @brief The hard shutdown threshold interface object */ |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 122 | std::unique_ptr<Threshold<HardShutdownObject>> hardShutdownIface; |
Matt Spinler | b306b03 | 2021-02-01 10:05:46 -0600 | [diff] [blame] | 123 | /** @brief The performance loss threshold interface object */ |
| 124 | std::unique_ptr<Threshold<PerformanceLossObject>> perfLossIface; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 125 | |
Lei YU | 0fcf0e1 | 2021-06-04 11:14:17 +0800 | [diff] [blame^] | 126 | /** @brief The association interface object */ |
| 127 | std::unique_ptr<AssociationObject> associationIface; |
| 128 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 129 | /** @brief Read config from json object and initialize sensor data |
| 130 | * for each virtual sensor |
| 131 | */ |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 132 | void initVirtualSensor(const Json& sensorConfig, |
| 133 | const std::string& objPath); |
| 134 | |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 135 | /** @brief Check Sensor threshold and update alarm and log */ |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 136 | template <typename V, typename T> |
| 137 | void checkThresholds(V value, T& threshold) |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 138 | { |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 139 | if (!threshold) |
| 140 | return; |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 141 | |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 142 | static constexpr auto tname = T::element_type::name; |
| 143 | |
| 144 | auto alarmHigh = threshold->alarmHigh(); |
Matt Spinler | a4fe665 | 2021-01-28 14:02:59 -0600 | [diff] [blame] | 145 | if ((!alarmHigh && value >= threshold->high()) || |
| 146 | (alarmHigh && value < threshold->high())) |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 147 | { |
| 148 | if (!alarmHigh) |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 149 | { |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 150 | constexpr auto msg = |
| 151 | "ASSERT: {} has exceeded the {} high threshold"; |
| 152 | log<level::ERR>(fmt::format(msg, name, tname).c_str()); |
George Hung | 4294e6d | 2021-04-14 20:58:21 +0800 | [diff] [blame] | 153 | threshold->alarmHighSignalAsserted(value); |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 154 | } |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 155 | else |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 156 | { |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 157 | constexpr auto msg = |
| 158 | "DEASSERT: {} is under the {} high threshold"; |
| 159 | log<level::INFO>(fmt::format(msg, name, tname).c_str()); |
George Hung | 4294e6d | 2021-04-14 20:58:21 +0800 | [diff] [blame] | 160 | threshold->alarmHighSignalDeasserted(value); |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 161 | } |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 162 | threshold->alarmHigh(!alarmHigh); |
| 163 | } |
| 164 | |
| 165 | auto alarmLow = threshold->alarmLow(); |
Matt Spinler | a4fe665 | 2021-01-28 14:02:59 -0600 | [diff] [blame] | 166 | if ((!alarmLow && value <= threshold->low()) || |
| 167 | (alarmLow && value > threshold->low())) |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 168 | { |
| 169 | if (!alarmLow) |
| 170 | { |
| 171 | constexpr auto msg = "ASSERT: {} is under the {} low threshold"; |
| 172 | log<level::ERR>(fmt::format(msg, name, tname).c_str()); |
George Hung | 4294e6d | 2021-04-14 20:58:21 +0800 | [diff] [blame] | 173 | threshold->alarmLowSignalAsserted(value); |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 174 | } |
| 175 | else |
| 176 | { |
| 177 | constexpr auto msg = |
| 178 | "DEASSERT: {} is above the {} low threshold"; |
| 179 | log<level::INFO>(fmt::format(msg, name, tname).c_str()); |
George Hung | 4294e6d | 2021-04-14 20:58:21 +0800 | [diff] [blame] | 180 | threshold->alarmLowSignalDeasserted(value); |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 181 | } |
| 182 | threshold->alarmLow(!alarmLow); |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 183 | } |
| 184 | } |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | class VirtualSensors |
| 188 | { |
| 189 | public: |
| 190 | VirtualSensors() = delete; |
| 191 | virtual ~VirtualSensors() = default; |
| 192 | |
| 193 | /** @brief Constructs VirtualSensors |
| 194 | * |
| 195 | * @param[in] bus - Handle to system dbus |
| 196 | */ |
| 197 | explicit VirtualSensors(sdbusplus::bus::bus& bus) : bus(bus) |
| 198 | { |
| 199 | createVirtualSensors(); |
| 200 | } |
| 201 | |
| 202 | private: |
| 203 | /** @brief sdbusplus bus client connection. */ |
| 204 | sdbusplus::bus::bus& bus; |
| 205 | /** @brief Parsing virtual sensor config JSON file */ |
| 206 | Json parseConfigFile(const std::string configFile); |
| 207 | |
| 208 | /** @brief Map of the object VirtualSensor */ |
| 209 | std::unordered_map<std::string, std::unique_ptr<VirtualSensor>> |
| 210 | virtualSensorsMap; |
| 211 | |
| 212 | /** @brief Create list of virtual sensors */ |
| 213 | void createVirtualSensors(); |
| 214 | }; |
| 215 | |
| 216 | } // namespace virtualSensor |
| 217 | } // namespace phosphor |