blob: 0667246612de03112c85c5fc0fa8121bb5e4c451 [file] [log] [blame]
Tao Linf2e94222023-10-31 17:38:17 +08001#pragma once
2
Vijay Khemka7452a862020-08-11 16:01:23 -07003#include "dbusSensor.hpp"
Vijay Khemka92d648b2020-08-21 18:55:07 -07004#include "exprtkTools.hpp"
Matt Spinler8f5e6112021-01-15 10:44:32 -06005#include "thresholds.hpp"
6
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07007#include <nlohmann/json.hpp>
Patrick Williams82b39c62021-07-28 16:22:27 -05008#include <phosphor-logging/lg2.hpp>
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07009#include <sdbusplus/bus.hpp>
Lei YU0fcf0e12021-06-04 11:14:17 +080010#include <xyz/openbmc_project/Association/Definitions/server.hpp>
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070011#include <xyz/openbmc_project/Sensor/Value/server.hpp>
12
13#include <map>
14#include <string>
15
Tao Linf2e94222023-10-31 17:38:17 +080016namespace phosphor::virtual_sensor
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070017{
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 */
Tao Linf2e94222023-10-31 17:38:17 +080068 SensorParam(sdbusplus::bus_t& bus, const std::string& path,
69 VirtualSensor& ctx) :
Vijay Khemka51f898e2020-09-09 22:24:18 -070070 dbusSensor(std::make_unique<DbusSensor>(bus, path, ctx)),
Vijay Khemka7452a862020-08-11 16:01:23 -070071 paramType(dbusParam)
72 {}
73
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070074 /** @brief Get sensor value property from D-bus interface */
75 double getParamValue();
76
77 private:
Vijay Khemka7452a862020-08-11 16:01:23 -070078 std::unique_ptr<DbusSensor> dbusSensor = nullptr;
Tao Linf2e94222023-10-31 17:38:17 +080079
80 /** @brief virtual sensor value */
81 double value = std::numeric_limits<double>::quiet_NaN();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070082 ParamType paramType;
83};
84
Matt Spinlerce675222021-01-14 16:38:09 -060085class VirtualSensor : public ValueObject
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070086{
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 */
Patrick Williams8e11ccc2022-07-22 19:26:57 -050097 VirtualSensor(sdbusplus::bus_t& bus, const char* objPath,
Vijay Khemka32a71562020-09-10 15:29:18 -070098 const Json& sensorConfig, const std::string& name) :
Rashmica Guptaa2fa63a2021-08-06 12:21:13 +100099 ValueObject(bus, objPath, action::defer_emit),
Vijay Khemka32a71562020-09-10 15:29:18 -0700100 bus(bus), name(name)
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700101 {
Matt Spinlerce675222021-01-14 16:38:09 -0600102 initVirtualSensor(sensorConfig, objPath);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700103 }
104
Rashmica Guptae7efe132021-07-27 19:42:11 +1000105 /** @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
Tao Lindc777012022-07-27 20:41:46 +0800113 * @param[in] entityPath - Virtual sensor path in entityManager Dbus
Rashmica Guptae7efe132021-07-27 19:42:11 +1000114 *
115 */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500116 VirtualSensor(sdbusplus::bus_t& bus, const char* objPath,
Rashmica Guptae7efe132021-07-27 19:42:11 +1000117 const InterfaceMap& ifacemap, const std::string& name,
Tao Lindc777012022-07-27 20:41:46 +0800118 const std::string& type, const std::string& calculationType,
119 const std::string& entityPath) :
Rashmica Guptae7efe132021-07-27 19:42:11 +1000120 ValueObject(bus, objPath, action::defer_emit),
Tao Lindc777012022-07-27 20:41:46 +0800121 bus(bus), name(name), entityPath(entityPath)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000122 {
123 initVirtualSensor(ifacemap, objPath, type, calculationType);
124 }
125
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700126 /** @brief Set sensor value */
127 void setSensorValue(double value);
Tao Linf2e94222023-10-31 17:38:17 +0800128
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700129 /** @brief Update sensor at regular intrval */
130 void updateVirtualSensor();
Tao Linf2e94222023-10-31 17:38:17 +0800131
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000132 /** @brief Check if sensor value is in valid range */
133 bool sensorInRange(double value);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700134
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. */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500142 sdbusplus::bus_t& bus;
Vijay Khemka32a71562020-09-10 15:29:18 -0700143 /** @brief name of sensor */
144 std::string name;
Tao Lindc777012022-07-27 20:41:46 +0800145
146 /** @brief Virtual sensor path in entityManager Dbus.
147 * This value is used to set thresholds/create association
148 */
149 std::string entityPath;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700150 /** @brief Expression string for virtual sensor value calculations */
151 std::string exprStr;
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700152 /** @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{};
Matt Spinler9f1ef4f2020-11-09 15:59:11 -0600156 /** @brief The vecops package so the expression can use vectors */
157 exprtk::rtl::vecops::package<double> vecopsPackage;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000158 /** @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();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700162
Matt Spinlerce675222021-01-14 16:38:09 -0600163 /** @brief The critical threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600164 std::unique_ptr<Threshold<CriticalObject>> criticalIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600165 /** @brief The warning threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600166 std::unique_ptr<Threshold<WarningObject>> warningIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600167 /** @brief The soft shutdown threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600168 std::unique_ptr<Threshold<SoftShutdownObject>> softShutdownIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600169 /** @brief The hard shutdown threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600170 std::unique_ptr<Threshold<HardShutdownObject>> hardShutdownIface;
Matt Spinlerb306b032021-02-01 10:05:46 -0600171 /** @brief The performance loss threshold interface object */
172 std::unique_ptr<Threshold<PerformanceLossObject>> perfLossIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600173
Lei YU0fcf0e12021-06-04 11:14:17 +0800174 /** @brief The association interface object */
175 std::unique_ptr<AssociationObject> associationIface;
176
Lei YU0ab9d832022-07-19 07:12:50 +0000177 static FuncMaxIgnoreNaN<double> funcMaxIgnoreNaN;
Lei YU87d35112022-10-24 05:54:25 +0000178 static FuncSumIgnoreNaN<double> funcSumIgnoreNaN;
Lei YUc77b6b32023-06-08 12:02:05 +0000179 static FuncIfNan<double> funcIfNan;
Lei YU0ab9d832022-07-19 07:12:50 +0000180
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700181 /** @brief Read config from json object and initialize sensor data
182 * for each virtual sensor
183 */
Matt Spinlerce675222021-01-14 16:38:09 -0600184 void initVirtualSensor(const Json& sensorConfig,
185 const std::string& objPath);
186
Rashmica Guptae7efe132021-07-27 19:42:11 +1000187 /** @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 */
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000196 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);
Tao Linf6b7e0a2022-10-09 09:35:44 +0800201 /** @brief Calculate maximum value from sensors */
202 double calculateMaximumValue(const VirtualSensor::ParamMap& paramMap);
Rashmica Gupta3e999192021-06-09 16:17:04 +1000203 /** @brief create threshold objects from json config */
204 void createThresholds(const Json& threshold, const std::string& objPath);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000205 /** @brief parse config from entity manager **/
206 void parseConfigInterface(const PropertyMap& propertyMap,
207 const std::string& sensorType,
208 const std::string& interface);
Rashmica Gupta3e999192021-06-09 16:17:04 +1000209
Vijay Khemka32a71562020-09-10 15:29:18 -0700210 /** @brief Check Sensor threshold and update alarm and log */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600211 template <typename V, typename T>
212 void checkThresholds(V value, T& threshold)
Matt Spinler8f5e6112021-01-15 10:44:32 -0600213 {
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600214 if (!threshold)
215 return;
Matt Spinler8f5e6112021-01-15 10:44:32 -0600216
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600217 static constexpr auto tname = T::element_type::name;
218
219 auto alarmHigh = threshold->alarmHigh();
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000220 auto highHysteresis = threshold->getHighHysteresis();
Matt Spinlera4fe6652021-01-28 14:02:59 -0600221 if ((!alarmHigh && value >= threshold->high()) ||
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000222 (alarmHigh && value < (threshold->high() - highHysteresis)))
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600223 {
224 if (!alarmHigh)
Matt Spinler8f5e6112021-01-15 10:44:32 -0600225 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500226 error("ASSERT: sensor {SENSOR} is above the upper threshold "
227 "{THRESHOLD}.",
228 "SENSOR", name, "THRESHOLD", tname);
George Hung4294e6d2021-04-14 20:58:21 +0800229 threshold->alarmHighSignalAsserted(value);
Matt Spinler8f5e6112021-01-15 10:44:32 -0600230 }
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600231 else
Matt Spinler8f5e6112021-01-15 10:44:32 -0600232 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500233 info("DEASSERT: sensor {SENSOR} is under the upper threshold "
234 "{THRESHOLD}.",
235 "SENSOR", name, "THRESHOLD", tname);
George Hung4294e6d2021-04-14 20:58:21 +0800236 threshold->alarmHighSignalDeasserted(value);
Matt Spinler8f5e6112021-01-15 10:44:32 -0600237 }
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600238 threshold->alarmHigh(!alarmHigh);
239 }
240
241 auto alarmLow = threshold->alarmLow();
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000242 auto lowHysteresis = threshold->getLowHysteresis();
Matt Spinlera4fe6652021-01-28 14:02:59 -0600243 if ((!alarmLow && value <= threshold->low()) ||
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000244 (alarmLow && value > (threshold->low() + lowHysteresis)))
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600245 {
246 if (!alarmLow)
247 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500248 error("ASSERT: sensor {SENSOR} is below the lower threshold "
249 "{THRESHOLD}.",
250 "SENSOR", name, "THRESHOLD", tname);
George Hung4294e6d2021-04-14 20:58:21 +0800251 threshold->alarmLowSignalAsserted(value);
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600252 }
253 else
254 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500255 info("DEASSERT: sensor {SENSOR} is above the lower threshold "
256 "{THRESHOLD}.",
257 "SENSOR", name, "THRESHOLD", tname);
George Hung4294e6d2021-04-14 20:58:21 +0800258 threshold->alarmLowSignalDeasserted(value);
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600259 }
260 threshold->alarmLow(!alarmLow);
Matt Spinler8f5e6112021-01-15 10:44:32 -0600261 }
262 }
Tao Lindc777012022-07-27 20:41:46 +0800263
264 /** @brief Create Association from entityPath*/
265 void createAssociation(const std::string& objPath,
266 const std::string& entityPath);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700267};
268
269class 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 */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500279 explicit VirtualSensors(sdbusplus::bus_t& bus) : bus(bus)
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700280 {
281 createVirtualSensors();
282 }
Rashmica Guptae7efe132021-07-27 19:42:11 +1000283 /** @brief Calls createVirtualSensor when interface added */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500284 void propertiesChanged(sdbusplus::message_t& msg);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700285
286 private:
287 /** @brief sdbusplus bus client connection. */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500288 sdbusplus::bus_t& bus;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000289 /** @brief Get virual sensor config from DBus**/
290 ManagedObjectType getObjectsFromDBus();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700291 /** @brief Parsing virtual sensor config JSON file */
Patrick Williams32dff212023-02-09 13:54:18 -0600292 Json parseConfigFile();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700293
Rashmica Guptae7efe132021-07-27 19:42:11 +1000294 /** @brief Matches for virtual sensors */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500295 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700296 /** @brief Map of the object VirtualSensor */
297 std::unordered_map<std::string, std::unique_ptr<VirtualSensor>>
298 virtualSensorsMap;
299
Rashmica Guptae7efe132021-07-27 19:42:11 +1000300 /** @brief Create list of virtual sensors from JSON config*/
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700301 void createVirtualSensors();
Rashmica Guptae7efe132021-07-27 19:42:11 +1000302 /** @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();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700306};
307
Tao Linf2e94222023-10-31 17:38:17 +0800308} // namespace phosphor::virtual_sensor