1 #pragma once 2 3 #include "dbusSensor.hpp" 4 #include "exprtkTools.hpp" 5 #include "thresholds.hpp" 6 7 #include <nlohmann/json.hpp> 8 #include <phosphor-logging/lg2.hpp> 9 #include <sdbusplus/bus.hpp> 10 #include <xyz/openbmc_project/Association/Definitions/server.hpp> 11 #include <xyz/openbmc_project/Sensor/Value/server.hpp> 12 13 #include <map> 14 #include <string> 15 16 namespace phosphor::virtual_sensor 17 { 18 19 PHOSPHOR_LOG2_USING_WITH_FLAGS; 20 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 32 using Json = nlohmann::json; 33 34 template <typename... T> 35 using ServerObject = typename sdbusplus::server::object_t<T...>; 36 37 using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; 38 using ValueObject = ServerObject<ValueIface>; 39 40 using AssociationIface = 41 sdbusplus::xyz::openbmc_project::Association::server::Definitions; 42 using AssociationObject = ServerObject<AssociationIface>; 43 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 /** @brief Constructs SensorParam (type = dbusParam) 63 * 64 * @param[in] bus - Handle to system dbus 65 * @param[in] path - The Dbus path of sensor 66 * @param[in] ctx - sensor context for update 67 */ 68 SensorParam(sdbusplus::bus_t& bus, const std::string& path, 69 VirtualSensor& ctx) : 70 dbusSensor(std::make_unique<DbusSensor>(bus, path, ctx)), 71 paramType(dbusParam) 72 {} 73 74 /** @brief Get sensor value property from D-bus interface */ 75 double getParamValue(); 76 77 private: 78 std::unique_ptr<DbusSensor> dbusSensor = nullptr; 79 80 /** @brief virtual sensor value */ 81 double value = std::numeric_limits<double>::quiet_NaN(); 82 ParamType paramType; 83 }; 84 85 class VirtualSensor : public ValueObject 86 { 87 public: 88 VirtualSensor() = delete; 89 virtual ~VirtualSensor() = default; 90 91 /** @brief Constructs VirtualSensor 92 * 93 * @param[in] bus - Handle to system dbus 94 * @param[in] objPath - The Dbus path of sensor 95 * @param[in] sensorConfig - Json object for sensor config 96 */ 97 VirtualSensor(sdbusplus::bus_t& bus, const char* objPath, 98 const Json& sensorConfig, const std::string& name) : 99 ValueObject(bus, objPath, action::defer_emit), 100 bus(bus), name(name) 101 { 102 initVirtualSensor(sensorConfig, objPath); 103 } 104 105 /** @brief Constructs VirtualSensor 106 * 107 * @param[in] bus - Handle to system dbus 108 * @param[in] objPath - The Dbus path of sensor 109 * @param[in] ifacemap - All the sensor information 110 * @param[in] name - Virtual sensor name 111 * @param[in] type - Virtual sensor type/unit 112 * @param[in] calcType - Calculation used to calculate sensor value 113 * @param[in] entityPath - Virtual sensor path in entityManager Dbus 114 * 115 */ 116 VirtualSensor(sdbusplus::bus_t& bus, const char* objPath, 117 const InterfaceMap& ifacemap, const std::string& name, 118 const std::string& type, const std::string& calculationType, 119 const std::string& entityPath) : 120 ValueObject(bus, objPath, action::defer_emit), 121 bus(bus), name(name), entityPath(entityPath) 122 { 123 initVirtualSensor(ifacemap, objPath, type, calculationType); 124 } 125 126 /** @brief Set sensor value */ 127 void setSensorValue(double value); 128 129 /** @brief Update sensor at regular intrval */ 130 void updateVirtualSensor(); 131 132 /** @brief Check if sensor value is in valid range */ 133 bool sensorInRange(double value); 134 135 /** @brief Map of list of parameters */ 136 using ParamMap = 137 std::unordered_map<std::string, std::unique_ptr<SensorParam>>; 138 ParamMap paramMap; 139 140 private: 141 /** @brief sdbusplus bus client connection. */ 142 sdbusplus::bus_t& bus; 143 /** @brief name of sensor */ 144 std::string name; 145 146 /** @brief Virtual sensor path in entityManager Dbus. 147 * This value is used to set thresholds/create association 148 */ 149 std::string entityPath; 150 /** @brief Expression string for virtual sensor value calculations */ 151 std::string exprStr; 152 /** @brief symbol table from exprtk */ 153 exprtk::symbol_table<double> symbols{}; 154 /** @brief expression from exprtk to calculate sensor value */ 155 exprtk::expression<double> expression{}; 156 /** @brief The vecops package so the expression can use vectors */ 157 exprtk::rtl::vecops::package<double> vecopsPackage; 158 /** @brief The maximum valid value for an input sensor **/ 159 double maxValidInput = std::numeric_limits<double>::infinity(); 160 /** @brief The minimum valid value for an input sensor **/ 161 double minValidInput = -std::numeric_limits<double>::infinity(); 162 163 /** @brief The critical threshold interface object */ 164 std::unique_ptr<Threshold<CriticalObject>> criticalIface; 165 /** @brief The warning threshold interface object */ 166 std::unique_ptr<Threshold<WarningObject>> warningIface; 167 /** @brief The soft shutdown threshold interface object */ 168 std::unique_ptr<Threshold<SoftShutdownObject>> softShutdownIface; 169 /** @brief The hard shutdown threshold interface object */ 170 std::unique_ptr<Threshold<HardShutdownObject>> hardShutdownIface; 171 /** @brief The performance loss threshold interface object */ 172 std::unique_ptr<Threshold<PerformanceLossObject>> perfLossIface; 173 174 /** @brief The association interface object */ 175 std::unique_ptr<AssociationObject> associationIface; 176 177 static FuncMaxIgnoreNaN<double> funcMaxIgnoreNaN; 178 static FuncSumIgnoreNaN<double> funcSumIgnoreNaN; 179 static FuncIfNan<double> funcIfNan; 180 181 /** @brief Read config from json object and initialize sensor data 182 * for each virtual sensor 183 */ 184 void initVirtualSensor(const Json& sensorConfig, 185 const std::string& objPath); 186 187 /** @brief Read config from interface map and initialize sensor data 188 * for each virtual sensor 189 */ 190 void initVirtualSensor(const InterfaceMap& interfaceMap, 191 const std::string& objPath, 192 const std::string& sensorType, 193 const std::string& calculationType); 194 195 /** @brief Returns which calculation function or expression to use */ 196 double calculateValue(const std::string& sensortype, 197 const VirtualSensor::ParamMap& paramMap); 198 /** @brief Calculate median value from sensors */ 199 double 200 calculateModifiedMedianValue(const VirtualSensor::ParamMap& paramMap); 201 /** @brief Calculate maximum value from sensors */ 202 double calculateMaximumValue(const VirtualSensor::ParamMap& paramMap); 203 /** @brief create threshold objects from json config */ 204 void createThresholds(const Json& threshold, const std::string& objPath); 205 /** @brief parse config from entity manager **/ 206 void parseConfigInterface(const PropertyMap& propertyMap, 207 const std::string& sensorType, 208 const std::string& interface); 209 210 /** @brief Check Sensor threshold and update alarm and log */ 211 template <typename V, typename T> 212 void checkThresholds(V value, T& threshold) 213 { 214 if (!threshold) 215 return; 216 217 static constexpr auto tname = T::element_type::name; 218 219 auto alarmHigh = threshold->alarmHigh(); 220 auto highHysteresis = threshold->getHighHysteresis(); 221 if ((!alarmHigh && value >= threshold->high()) || 222 (alarmHigh && value < (threshold->high() - highHysteresis))) 223 { 224 if (!alarmHigh) 225 { 226 error("ASSERT: sensor {SENSOR} is above the upper threshold " 227 "{THRESHOLD}.", 228 "SENSOR", name, "THRESHOLD", tname); 229 threshold->alarmHighSignalAsserted(value); 230 } 231 else 232 { 233 info("DEASSERT: sensor {SENSOR} is under the upper threshold " 234 "{THRESHOLD}.", 235 "SENSOR", name, "THRESHOLD", tname); 236 threshold->alarmHighSignalDeasserted(value); 237 } 238 threshold->alarmHigh(!alarmHigh); 239 } 240 241 auto alarmLow = threshold->alarmLow(); 242 auto lowHysteresis = threshold->getLowHysteresis(); 243 if ((!alarmLow && value <= threshold->low()) || 244 (alarmLow && value > (threshold->low() + lowHysteresis))) 245 { 246 if (!alarmLow) 247 { 248 error("ASSERT: sensor {SENSOR} is below the lower threshold " 249 "{THRESHOLD}.", 250 "SENSOR", name, "THRESHOLD", tname); 251 threshold->alarmLowSignalAsserted(value); 252 } 253 else 254 { 255 info("DEASSERT: sensor {SENSOR} is above the lower threshold " 256 "{THRESHOLD}.", 257 "SENSOR", name, "THRESHOLD", tname); 258 threshold->alarmLowSignalDeasserted(value); 259 } 260 threshold->alarmLow(!alarmLow); 261 } 262 } 263 264 /** @brief Create Association from entityPath*/ 265 void createAssociation(const std::string& objPath, 266 const std::string& entityPath); 267 }; 268 269 class VirtualSensors 270 { 271 public: 272 VirtualSensors() = delete; 273 virtual ~VirtualSensors() = default; 274 275 /** @brief Constructs VirtualSensors 276 * 277 * @param[in] bus - Handle to system dbus 278 */ 279 explicit VirtualSensors(sdbusplus::bus_t& bus) : bus(bus) 280 { 281 createVirtualSensors(); 282 } 283 /** @brief Calls createVirtualSensor when interface added */ 284 void propertiesChanged(sdbusplus::message_t& msg); 285 286 private: 287 /** @brief sdbusplus bus client connection. */ 288 sdbusplus::bus_t& bus; 289 /** @brief Get virual sensor config from DBus**/ 290 ManagedObjectType getObjectsFromDBus(); 291 /** @brief Parsing virtual sensor config JSON file */ 292 Json parseConfigFile(); 293 294 /** @brief Matches for virtual sensors */ 295 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches; 296 /** @brief Map of the object VirtualSensor */ 297 std::unordered_map<std::string, std::unique_ptr<VirtualSensor>> 298 virtualSensorsMap; 299 300 /** @brief Create list of virtual sensors from JSON config*/ 301 void createVirtualSensors(); 302 /** @brief Create list of virtual sensors from DBus config */ 303 void createVirtualSensorsFromDBus(const std::string& calculationType); 304 /** @brief Setup matches for virtual sensors */ 305 void setupMatches(); 306 }; 307 308 } // namespace phosphor::virtual_sensor 309