blob: 01e3221c40355b2a46eabd949b7273ac167d155a [file] [log] [blame]
Vijay Khemka7452a862020-08-11 16:01:23 -07001#include "dbusSensor.hpp"
Vijay Khemka92d648b2020-08-21 18:55:07 -07002#include "exprtkTools.hpp"
Matt Spinler8f5e6112021-01-15 10:44:32 -06003#include "thresholds.hpp"
4
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07005#include <nlohmann/json.hpp>
Patrick Williams82b39c62021-07-28 16:22:27 -05006#include <phosphor-logging/lg2.hpp>
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07007#include <sdbusplus/bus.hpp>
Lei YU0fcf0e12021-06-04 11:14:17 +08008#include <xyz/openbmc_project/Association/Definitions/server.hpp>
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07009#include <xyz/openbmc_project/Sensor/Value/server.hpp>
10
11#include <map>
12#include <string>
13
14namespace phosphor
15{
16namespace virtualSensor
17{
18
Patrick Williams82b39c62021-07-28 16:22:27 -050019PHOSPHOR_LOG2_USING_WITH_FLAGS;
20
Rashmica Guptae7efe132021-07-27 19:42:11 +100021using 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
25using PropertyMap = std::map<std::string, BasicVariantType>;
26
27using InterfaceMap = std::map<std::string, PropertyMap>;
28
29using ManagedObjectType =
30 std::map<sdbusplus::message::object_path, InterfaceMap>;
31
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070032using Json = nlohmann::json;
Matt Spinlerce675222021-01-14 16:38:09 -060033
34template <typename... T>
Patrick Williams8e11ccc2022-07-22 19:26:57 -050035using ServerObject = typename sdbusplus::server::object_t<T...>;
Matt Spinlerce675222021-01-14 16:38:09 -060036
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070037using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
Matt Spinlerce675222021-01-14 16:38:09 -060038using ValueObject = ServerObject<ValueIface>;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070039
Lei YU0fcf0e12021-06-04 11:14:17 +080040using AssociationIface =
41 sdbusplus::xyz::openbmc_project::Association::server::Definitions;
42using AssociationObject = ServerObject<AssociationIface>;
43
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070044class 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 */
Patrick Williams1226f202023-05-10 07:51:16 -050060 explicit SensorParam(double value) : value(value), paramType(constParam) {}
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070061
Vijay Khemka7452a862020-08-11 16:01:23 -070062 /** @brief Constructs SensorParam (type = dbusParam)
63 *
64 * @param[in] bus - Handle to system dbus
65 * @param[in] path - The Dbus path of sensor
Vijay Khemka51f898e2020-09-09 22:24:18 -070066 * @param[in] ctx - sensor context for update
Vijay Khemka7452a862020-08-11 16:01:23 -070067 */
Patrick Williams8e11ccc2022-07-22 19:26:57 -050068 SensorParam(sdbusplus::bus_t& bus, const std::string& path, void* ctx) :
Vijay Khemka51f898e2020-09-09 22:24:18 -070069 dbusSensor(std::make_unique<DbusSensor>(bus, path, ctx)),
Vijay Khemka7452a862020-08-11 16:01:23 -070070 paramType(dbusParam)
71 {}
72
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070073 /** @brief Get sensor value property from D-bus interface */
74 double getParamValue();
75
76 private:
Vijay Khemka7452a862020-08-11 16:01:23 -070077 std::unique_ptr<DbusSensor> dbusSensor = nullptr;
78 double value = 0;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070079 ParamType paramType;
80};
81
Matt Spinlerce675222021-01-14 16:38:09 -060082class VirtualSensor : public ValueObject
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070083{
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 */
Patrick Williams8e11ccc2022-07-22 19:26:57 -050094 VirtualSensor(sdbusplus::bus_t& bus, const char* objPath,
Vijay Khemka32a71562020-09-10 15:29:18 -070095 const Json& sensorConfig, const std::string& name) :
Rashmica Guptaa2fa63a2021-08-06 12:21:13 +100096 ValueObject(bus, objPath, action::defer_emit),
Vijay Khemka32a71562020-09-10 15:29:18 -070097 bus(bus), name(name)
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070098 {
Matt Spinlerce675222021-01-14 16:38:09 -060099 initVirtualSensor(sensorConfig, objPath);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700100 }
101
Rashmica Guptae7efe132021-07-27 19:42:11 +1000102 /** @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
Tao Lindc777012022-07-27 20:41:46 +0800110 * @param[in] entityPath - Virtual sensor path in entityManager Dbus
Rashmica Guptae7efe132021-07-27 19:42:11 +1000111 *
112 */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500113 VirtualSensor(sdbusplus::bus_t& bus, const char* objPath,
Rashmica Guptae7efe132021-07-27 19:42:11 +1000114 const InterfaceMap& ifacemap, const std::string& name,
Tao Lindc777012022-07-27 20:41:46 +0800115 const std::string& type, const std::string& calculationType,
116 const std::string& entityPath) :
Rashmica Guptae7efe132021-07-27 19:42:11 +1000117 ValueObject(bus, objPath, action::defer_emit),
Tao Lindc777012022-07-27 20:41:46 +0800118 bus(bus), name(name), entityPath(entityPath)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000119 {
120 initVirtualSensor(ifacemap, objPath, type, calculationType);
121 }
122
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700123 /** @brief Set sensor value */
124 void setSensorValue(double value);
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700125 /** @brief Update sensor at regular intrval */
126 void updateVirtualSensor();
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000127 /** @brief Check if sensor value is in valid range */
128 bool sensorInRange(double value);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700129
130 /** @brief Map of list of parameters */
131 using ParamMap =
132 std::unordered_map<std::string, std::unique_ptr<SensorParam>>;
133 ParamMap paramMap;
134
135 private:
136 /** @brief sdbusplus bus client connection. */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500137 sdbusplus::bus_t& bus;
Vijay Khemka32a71562020-09-10 15:29:18 -0700138 /** @brief name of sensor */
139 std::string name;
Tao Lindc777012022-07-27 20:41:46 +0800140
141 /** @brief Virtual sensor path in entityManager Dbus.
142 * This value is used to set thresholds/create association
143 */
144 std::string entityPath;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700145 /** @brief Expression string for virtual sensor value calculations */
146 std::string exprStr;
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700147 /** @brief symbol table from exprtk */
148 exprtk::symbol_table<double> symbols{};
149 /** @brief expression from exprtk to calculate sensor value */
150 exprtk::expression<double> expression{};
Matt Spinler9f1ef4f2020-11-09 15:59:11 -0600151 /** @brief The vecops package so the expression can use vectors */
152 exprtk::rtl::vecops::package<double> vecopsPackage;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000153 /** @brief The maximum valid value for an input sensor **/
154 double maxValidInput = std::numeric_limits<double>::infinity();
155 /** @brief The minimum valid value for an input sensor **/
156 double minValidInput = -std::numeric_limits<double>::infinity();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700157
Matt Spinlerce675222021-01-14 16:38:09 -0600158 /** @brief The critical threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600159 std::unique_ptr<Threshold<CriticalObject>> criticalIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600160 /** @brief The warning threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600161 std::unique_ptr<Threshold<WarningObject>> warningIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600162 /** @brief The soft shutdown threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600163 std::unique_ptr<Threshold<SoftShutdownObject>> softShutdownIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600164 /** @brief The hard shutdown threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600165 std::unique_ptr<Threshold<HardShutdownObject>> hardShutdownIface;
Matt Spinlerb306b032021-02-01 10:05:46 -0600166 /** @brief The performance loss threshold interface object */
167 std::unique_ptr<Threshold<PerformanceLossObject>> perfLossIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600168
Lei YU0fcf0e12021-06-04 11:14:17 +0800169 /** @brief The association interface object */
170 std::unique_ptr<AssociationObject> associationIface;
171
Lei YU0ab9d832022-07-19 07:12:50 +0000172 static FuncMaxIgnoreNaN<double> funcMaxIgnoreNaN;
Lei YU87d35112022-10-24 05:54:25 +0000173 static FuncSumIgnoreNaN<double> funcSumIgnoreNaN;
Lei YUc77b6b32023-06-08 12:02:05 +0000174 static FuncIfNan<double> funcIfNan;
Lei YU0ab9d832022-07-19 07:12:50 +0000175
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700176 /** @brief Read config from json object and initialize sensor data
177 * for each virtual sensor
178 */
Matt Spinlerce675222021-01-14 16:38:09 -0600179 void initVirtualSensor(const Json& sensorConfig,
180 const std::string& objPath);
181
Rashmica Guptae7efe132021-07-27 19:42:11 +1000182 /** @brief Read config from interface map and initialize sensor data
183 * for each virtual sensor
184 */
185 void initVirtualSensor(const InterfaceMap& interfaceMap,
186 const std::string& objPath,
187 const std::string& sensorType,
188 const std::string& calculationType);
189
190 /** @brief Returns which calculation function or expression to use */
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000191 double calculateValue(const std::string& sensortype,
192 const VirtualSensor::ParamMap& paramMap);
193 /** @brief Calculate median value from sensors */
194 double
195 calculateModifiedMedianValue(const VirtualSensor::ParamMap& paramMap);
Tao Linf6b7e0a2022-10-09 09:35:44 +0800196 /** @brief Calculate maximum value from sensors */
197 double calculateMaximumValue(const VirtualSensor::ParamMap& paramMap);
Rashmica Gupta3e999192021-06-09 16:17:04 +1000198 /** @brief create threshold objects from json config */
199 void createThresholds(const Json& threshold, const std::string& objPath);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000200 /** @brief parse config from entity manager **/
201 void parseConfigInterface(const PropertyMap& propertyMap,
202 const std::string& sensorType,
203 const std::string& interface);
Rashmica Gupta3e999192021-06-09 16:17:04 +1000204
Vijay Khemka32a71562020-09-10 15:29:18 -0700205 /** @brief Check Sensor threshold and update alarm and log */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600206 template <typename V, typename T>
207 void checkThresholds(V value, T& threshold)
Matt Spinler8f5e6112021-01-15 10:44:32 -0600208 {
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600209 if (!threshold)
210 return;
Matt Spinler8f5e6112021-01-15 10:44:32 -0600211
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600212 static constexpr auto tname = T::element_type::name;
213
214 auto alarmHigh = threshold->alarmHigh();
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000215 auto highHysteresis = threshold->getHighHysteresis();
Matt Spinlera4fe6652021-01-28 14:02:59 -0600216 if ((!alarmHigh && value >= threshold->high()) ||
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000217 (alarmHigh && value < (threshold->high() - highHysteresis)))
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600218 {
219 if (!alarmHigh)
Matt Spinler8f5e6112021-01-15 10:44:32 -0600220 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500221 error("ASSERT: sensor {SENSOR} is above the upper threshold "
222 "{THRESHOLD}.",
223 "SENSOR", name, "THRESHOLD", tname);
George Hung4294e6d2021-04-14 20:58:21 +0800224 threshold->alarmHighSignalAsserted(value);
Matt Spinler8f5e6112021-01-15 10:44:32 -0600225 }
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600226 else
Matt Spinler8f5e6112021-01-15 10:44:32 -0600227 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500228 info("DEASSERT: sensor {SENSOR} is under the upper threshold "
229 "{THRESHOLD}.",
230 "SENSOR", name, "THRESHOLD", tname);
George Hung4294e6d2021-04-14 20:58:21 +0800231 threshold->alarmHighSignalDeasserted(value);
Matt Spinler8f5e6112021-01-15 10:44:32 -0600232 }
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600233 threshold->alarmHigh(!alarmHigh);
234 }
235
236 auto alarmLow = threshold->alarmLow();
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000237 auto lowHysteresis = threshold->getLowHysteresis();
Matt Spinlera4fe6652021-01-28 14:02:59 -0600238 if ((!alarmLow && value <= threshold->low()) ||
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000239 (alarmLow && value > (threshold->low() + lowHysteresis)))
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600240 {
241 if (!alarmLow)
242 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500243 error("ASSERT: sensor {SENSOR} is below the lower threshold "
244 "{THRESHOLD}.",
245 "SENSOR", name, "THRESHOLD", tname);
George Hung4294e6d2021-04-14 20:58:21 +0800246 threshold->alarmLowSignalAsserted(value);
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600247 }
248 else
249 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500250 info("DEASSERT: sensor {SENSOR} is above the lower threshold "
251 "{THRESHOLD}.",
252 "SENSOR", name, "THRESHOLD", tname);
George Hung4294e6d2021-04-14 20:58:21 +0800253 threshold->alarmLowSignalDeasserted(value);
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600254 }
255 threshold->alarmLow(!alarmLow);
Matt Spinler8f5e6112021-01-15 10:44:32 -0600256 }
257 }
Tao Lindc777012022-07-27 20:41:46 +0800258
259 /** @brief Create Association from entityPath*/
260 void createAssociation(const std::string& objPath,
261 const std::string& entityPath);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700262};
263
264class VirtualSensors
265{
266 public:
267 VirtualSensors() = delete;
268 virtual ~VirtualSensors() = default;
269
270 /** @brief Constructs VirtualSensors
271 *
272 * @param[in] bus - Handle to system dbus
273 */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500274 explicit VirtualSensors(sdbusplus::bus_t& bus) : bus(bus)
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700275 {
276 createVirtualSensors();
277 }
Rashmica Guptae7efe132021-07-27 19:42:11 +1000278 /** @brief Calls createVirtualSensor when interface added */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500279 void propertiesChanged(sdbusplus::message_t& msg);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700280
281 private:
282 /** @brief sdbusplus bus client connection. */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500283 sdbusplus::bus_t& bus;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000284 /** @brief Get virual sensor config from DBus**/
285 ManagedObjectType getObjectsFromDBus();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700286 /** @brief Parsing virtual sensor config JSON file */
Patrick Williams32dff212023-02-09 13:54:18 -0600287 Json parseConfigFile();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700288
Rashmica Guptae7efe132021-07-27 19:42:11 +1000289 /** @brief Matches for virtual sensors */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500290 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700291 /** @brief Map of the object VirtualSensor */
292 std::unordered_map<std::string, std::unique_ptr<VirtualSensor>>
293 virtualSensorsMap;
294
Rashmica Guptae7efe132021-07-27 19:42:11 +1000295 /** @brief Create list of virtual sensors from JSON config*/
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700296 void createVirtualSensors();
Rashmica Guptae7efe132021-07-27 19:42:11 +1000297 /** @brief Create list of virtual sensors from DBus config */
298 void createVirtualSensorsFromDBus(const std::string& calculationType);
299 /** @brief Setup matches for virtual sensors */
300 void setupMatches();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700301};
302
303} // namespace virtualSensor
304} // namespace phosphor