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 | |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 20 | using BasicVariantType = |
| 21 | std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t, |
| 22 | int16_t, uint16_t, uint8_t, bool, std::vector<std::string>>; |
| 23 | |
| 24 | using PropertyMap = std::map<std::string, BasicVariantType>; |
| 25 | |
| 26 | using InterfaceMap = std::map<std::string, PropertyMap>; |
| 27 | |
| 28 | using ManagedObjectType = |
| 29 | std::map<sdbusplus::message::object_path, InterfaceMap>; |
| 30 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 31 | using Json = nlohmann::json; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 32 | |
| 33 | template <typename... T> |
| 34 | using ServerObject = typename sdbusplus::server::object::object<T...>; |
| 35 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 36 | using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 37 | using ValueObject = ServerObject<ValueIface>; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 38 | |
Lei YU | 0fcf0e1 | 2021-06-04 11:14:17 +0800 | [diff] [blame] | 39 | using AssociationIface = |
| 40 | sdbusplus::xyz::openbmc_project::Association::server::Definitions; |
| 41 | using AssociationObject = ServerObject<AssociationIface>; |
| 42 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 43 | class SensorParam |
| 44 | { |
| 45 | public: |
| 46 | SensorParam() = delete; |
| 47 | virtual ~SensorParam() = default; |
| 48 | |
| 49 | enum ParamType |
| 50 | { |
| 51 | constParam, |
| 52 | dbusParam |
| 53 | }; |
| 54 | |
| 55 | /** @brief Constructs SensorParam (type = constParam) |
| 56 | * |
| 57 | * @param[in] value - Value of constant parameter |
| 58 | */ |
| 59 | explicit SensorParam(double value) : value(value), paramType(constParam) |
| 60 | {} |
| 61 | |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 62 | /** @brief Constructs SensorParam (type = dbusParam) |
| 63 | * |
| 64 | * @param[in] bus - Handle to system dbus |
| 65 | * @param[in] path - The Dbus path of sensor |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 66 | * @param[in] ctx - sensor context for update |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 67 | */ |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 68 | SensorParam(sdbusplus::bus::bus& bus, std::string path, void* ctx) : |
| 69 | dbusSensor(std::make_unique<DbusSensor>(bus, path, ctx)), |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 70 | paramType(dbusParam) |
| 71 | {} |
| 72 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 73 | /** @brief Get sensor value property from D-bus interface */ |
| 74 | double getParamValue(); |
| 75 | |
| 76 | private: |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 77 | std::unique_ptr<DbusSensor> dbusSensor = nullptr; |
| 78 | double value = 0; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 79 | ParamType paramType; |
| 80 | }; |
| 81 | |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 82 | class VirtualSensor : public ValueObject |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 83 | { |
| 84 | public: |
| 85 | VirtualSensor() = delete; |
| 86 | virtual ~VirtualSensor() = default; |
| 87 | |
| 88 | /** @brief Constructs VirtualSensor |
| 89 | * |
| 90 | * @param[in] bus - Handle to system dbus |
| 91 | * @param[in] objPath - The Dbus path of sensor |
| 92 | * @param[in] sensorConfig - Json object for sensor config |
| 93 | */ |
| 94 | VirtualSensor(sdbusplus::bus::bus& bus, const char* objPath, |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 95 | const Json& sensorConfig, const std::string& name) : |
Rashmica Gupta | a2fa63a | 2021-08-06 12:21:13 +1000 | [diff] [blame] | 96 | ValueObject(bus, objPath, action::defer_emit), |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 97 | bus(bus), name(name) |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 98 | { |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 99 | initVirtualSensor(sensorConfig, objPath); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 102 | /** @brief Constructs VirtualSensor |
| 103 | * |
| 104 | * @param[in] bus - Handle to system dbus |
| 105 | * @param[in] objPath - The Dbus path of sensor |
| 106 | * @param[in] ifacemap - All the sensor information |
| 107 | * @param[in] name - Virtual sensor name |
| 108 | * @param[in] type - Virtual sensor type/unit |
| 109 | * @param[in] calcType - Calculation used to calculate sensor value |
| 110 | * |
| 111 | */ |
| 112 | VirtualSensor(sdbusplus::bus::bus& bus, const char* objPath, |
| 113 | const InterfaceMap& ifacemap, const std::string& name, |
| 114 | const std::string& type, const std::string& calculationType) : |
| 115 | ValueObject(bus, objPath, action::defer_emit), |
| 116 | bus(bus), name(name) |
| 117 | { |
| 118 | initVirtualSensor(ifacemap, objPath, type, calculationType); |
| 119 | } |
| 120 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 121 | /** @brief Set sensor value */ |
| 122 | void setSensorValue(double value); |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 123 | /** @brief Update sensor at regular intrval */ |
| 124 | void updateVirtualSensor(); |
Rashmica Gupta | 304fd0e | 2021-08-10 16:50:44 +1000 | [diff] [blame] | 125 | /** @brief Check if sensor value is in valid range */ |
| 126 | bool sensorInRange(double value); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 127 | |
| 128 | /** @brief Map of list of parameters */ |
| 129 | using ParamMap = |
| 130 | std::unordered_map<std::string, std::unique_ptr<SensorParam>>; |
| 131 | ParamMap paramMap; |
| 132 | |
| 133 | private: |
| 134 | /** @brief sdbusplus bus client connection. */ |
| 135 | sdbusplus::bus::bus& bus; |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 136 | /** @brief name of sensor */ |
| 137 | std::string name; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 138 | /** @brief Expression string for virtual sensor value calculations */ |
| 139 | std::string exprStr; |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 140 | /** @brief symbol table from exprtk */ |
| 141 | exprtk::symbol_table<double> symbols{}; |
| 142 | /** @brief expression from exprtk to calculate sensor value */ |
| 143 | exprtk::expression<double> expression{}; |
Matt Spinler | 9f1ef4f | 2020-11-09 15:59:11 -0600 | [diff] [blame] | 144 | /** @brief The vecops package so the expression can use vectors */ |
| 145 | exprtk::rtl::vecops::package<double> vecopsPackage; |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 146 | /** @brief The maximum valid value for an input sensor **/ |
| 147 | double maxValidInput = std::numeric_limits<double>::infinity(); |
| 148 | /** @brief The minimum valid value for an input sensor **/ |
| 149 | double minValidInput = -std::numeric_limits<double>::infinity(); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 150 | |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 151 | /** @brief The critical threshold interface object */ |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 152 | std::unique_ptr<Threshold<CriticalObject>> criticalIface; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 153 | /** @brief The warning threshold interface object */ |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 154 | std::unique_ptr<Threshold<WarningObject>> warningIface; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 155 | /** @brief The soft shutdown threshold interface object */ |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 156 | std::unique_ptr<Threshold<SoftShutdownObject>> softShutdownIface; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 157 | /** @brief The hard shutdown threshold interface object */ |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 158 | std::unique_ptr<Threshold<HardShutdownObject>> hardShutdownIface; |
Matt Spinler | b306b03 | 2021-02-01 10:05:46 -0600 | [diff] [blame] | 159 | /** @brief The performance loss threshold interface object */ |
| 160 | std::unique_ptr<Threshold<PerformanceLossObject>> perfLossIface; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 161 | |
Lei YU | 0fcf0e1 | 2021-06-04 11:14:17 +0800 | [diff] [blame] | 162 | /** @brief The association interface object */ |
| 163 | std::unique_ptr<AssociationObject> associationIface; |
| 164 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 165 | /** @brief Read config from json object and initialize sensor data |
| 166 | * for each virtual sensor |
| 167 | */ |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 168 | void initVirtualSensor(const Json& sensorConfig, |
| 169 | const std::string& objPath); |
| 170 | |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 171 | /** @brief Read config from interface map and initialize sensor data |
| 172 | * for each virtual sensor |
| 173 | */ |
| 174 | void initVirtualSensor(const InterfaceMap& interfaceMap, |
| 175 | const std::string& objPath, |
| 176 | const std::string& sensorType, |
| 177 | const std::string& calculationType); |
| 178 | |
| 179 | /** @brief Returns which calculation function or expression to use */ |
Rashmica Gupta | 304fd0e | 2021-08-10 16:50:44 +1000 | [diff] [blame] | 180 | double calculateValue(const std::string& sensortype, |
| 181 | const VirtualSensor::ParamMap& paramMap); |
| 182 | /** @brief Calculate median value from sensors */ |
| 183 | double |
| 184 | calculateModifiedMedianValue(const VirtualSensor::ParamMap& paramMap); |
Rashmica Gupta | 3e99919 | 2021-06-09 16:17:04 +1000 | [diff] [blame] | 185 | /** @brief create threshold objects from json config */ |
| 186 | void createThresholds(const Json& threshold, const std::string& objPath); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 187 | /** @brief parse config from entity manager **/ |
| 188 | void parseConfigInterface(const PropertyMap& propertyMap, |
| 189 | const std::string& sensorType, |
| 190 | const std::string& interface); |
Rashmica Gupta | 3e99919 | 2021-06-09 16:17:04 +1000 | [diff] [blame] | 191 | |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 192 | /** @brief Check Sensor threshold and update alarm and log */ |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 193 | template <typename V, typename T> |
| 194 | void checkThresholds(V value, T& threshold) |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 195 | { |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 196 | if (!threshold) |
| 197 | return; |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 198 | |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 199 | static constexpr auto tname = T::element_type::name; |
| 200 | |
| 201 | auto alarmHigh = threshold->alarmHigh(); |
Matt Spinler | a4fe665 | 2021-01-28 14:02:59 -0600 | [diff] [blame] | 202 | if ((!alarmHigh && value >= threshold->high()) || |
| 203 | (alarmHigh && value < threshold->high())) |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 204 | { |
| 205 | if (!alarmHigh) |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 206 | { |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 207 | constexpr auto msg = |
| 208 | "ASSERT: {} has exceeded the {} high threshold"; |
| 209 | log<level::ERR>(fmt::format(msg, name, tname).c_str()); |
George Hung | 4294e6d | 2021-04-14 20:58:21 +0800 | [diff] [blame] | 210 | threshold->alarmHighSignalAsserted(value); |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 211 | } |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 212 | else |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 213 | { |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 214 | constexpr auto msg = |
| 215 | "DEASSERT: {} is under the {} high threshold"; |
| 216 | log<level::INFO>(fmt::format(msg, name, tname).c_str()); |
George Hung | 4294e6d | 2021-04-14 20:58:21 +0800 | [diff] [blame] | 217 | threshold->alarmHighSignalDeasserted(value); |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 218 | } |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 219 | threshold->alarmHigh(!alarmHigh); |
| 220 | } |
| 221 | |
| 222 | auto alarmLow = threshold->alarmLow(); |
Matt Spinler | a4fe665 | 2021-01-28 14:02:59 -0600 | [diff] [blame] | 223 | if ((!alarmLow && value <= threshold->low()) || |
| 224 | (alarmLow && value > threshold->low())) |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 225 | { |
| 226 | if (!alarmLow) |
| 227 | { |
| 228 | constexpr auto msg = "ASSERT: {} is under the {} low threshold"; |
| 229 | log<level::ERR>(fmt::format(msg, name, tname).c_str()); |
George Hung | 4294e6d | 2021-04-14 20:58:21 +0800 | [diff] [blame] | 230 | threshold->alarmLowSignalAsserted(value); |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 231 | } |
| 232 | else |
| 233 | { |
| 234 | constexpr auto msg = |
| 235 | "DEASSERT: {} is above the {} low threshold"; |
| 236 | log<level::INFO>(fmt::format(msg, name, tname).c_str()); |
George Hung | 4294e6d | 2021-04-14 20:58:21 +0800 | [diff] [blame] | 237 | threshold->alarmLowSignalDeasserted(value); |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 238 | } |
| 239 | threshold->alarmLow(!alarmLow); |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 240 | } |
| 241 | } |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | class VirtualSensors |
| 245 | { |
| 246 | public: |
| 247 | VirtualSensors() = delete; |
| 248 | virtual ~VirtualSensors() = default; |
| 249 | |
| 250 | /** @brief Constructs VirtualSensors |
| 251 | * |
| 252 | * @param[in] bus - Handle to system dbus |
| 253 | */ |
| 254 | explicit VirtualSensors(sdbusplus::bus::bus& bus) : bus(bus) |
| 255 | { |
| 256 | createVirtualSensors(); |
| 257 | } |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 258 | /** @brief Calls createVirtualSensor when interface added */ |
| 259 | void propertiesChanged(sdbusplus::message::message& msg); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 260 | |
| 261 | private: |
| 262 | /** @brief sdbusplus bus client connection. */ |
| 263 | sdbusplus::bus::bus& bus; |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 264 | /** @brief Get virual sensor config from DBus**/ |
| 265 | ManagedObjectType getObjectsFromDBus(); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 266 | /** @brief Parsing virtual sensor config JSON file */ |
| 267 | Json parseConfigFile(const std::string configFile); |
| 268 | |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 269 | /** @brief Matches for virtual sensors */ |
| 270 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 271 | /** @brief Map of the object VirtualSensor */ |
| 272 | std::unordered_map<std::string, std::unique_ptr<VirtualSensor>> |
| 273 | virtualSensorsMap; |
| 274 | |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 275 | /** @brief Create list of virtual sensors from JSON config*/ |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 276 | void createVirtualSensors(); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 277 | /** @brief Create list of virtual sensors from DBus config */ |
| 278 | void createVirtualSensorsFromDBus(const std::string& calculationType); |
| 279 | /** @brief Setup matches for virtual sensors */ |
| 280 | void setupMatches(); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | } // namespace virtualSensor |
| 284 | } // namespace phosphor |