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