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