blob: 921aee25777aacc8691a2d4ca866b95826ba27b0 [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 */
60 explicit SensorParam(double value) : value(value), paramType(constParam)
61 {}
62
Vijay Khemka7452a862020-08-11 16:01:23 -070063 /** @brief Constructs SensorParam (type = dbusParam)
64 *
65 * @param[in] bus - Handle to system dbus
66 * @param[in] path - The Dbus path of sensor
Vijay Khemka51f898e2020-09-09 22:24:18 -070067 * @param[in] ctx - sensor context for update
Vijay Khemka7452a862020-08-11 16:01:23 -070068 */
Patrick Williams8e11ccc2022-07-22 19:26:57 -050069 SensorParam(sdbusplus::bus_t& bus, const std::string& path, void* 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;
79 double value = 0;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070080 ParamType paramType;
81};
82
Matt Spinlerce675222021-01-14 16:38:09 -060083class VirtualSensor : public ValueObject
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070084{
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 */
Patrick Williams8e11ccc2022-07-22 19:26:57 -050095 VirtualSensor(sdbusplus::bus_t& bus, const char* objPath,
Vijay Khemka32a71562020-09-10 15:29:18 -070096 const Json& sensorConfig, const std::string& name) :
Rashmica Guptaa2fa63a2021-08-06 12:21:13 +100097 ValueObject(bus, objPath, action::defer_emit),
Vijay Khemka32a71562020-09-10 15:29:18 -070098 bus(bus), name(name)
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070099 {
Matt Spinlerce675222021-01-14 16:38:09 -0600100 initVirtualSensor(sensorConfig, objPath);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700101 }
102
Rashmica Guptae7efe132021-07-27 19:42:11 +1000103 /** @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
Tao Lindc777012022-07-27 20:41:46 +0800111 * @param[in] entityPath - Virtual sensor path in entityManager Dbus
Rashmica Guptae7efe132021-07-27 19:42:11 +1000112 *
113 */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500114 VirtualSensor(sdbusplus::bus_t& bus, const char* objPath,
Rashmica Guptae7efe132021-07-27 19:42:11 +1000115 const InterfaceMap& ifacemap, const std::string& name,
Tao Lindc777012022-07-27 20:41:46 +0800116 const std::string& type, const std::string& calculationType,
117 const std::string& entityPath) :
Rashmica Guptae7efe132021-07-27 19:42:11 +1000118 ValueObject(bus, objPath, action::defer_emit),
Tao Lindc777012022-07-27 20:41:46 +0800119 bus(bus), name(name), entityPath(entityPath)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000120 {
121 initVirtualSensor(ifacemap, objPath, type, calculationType);
122 }
123
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700124 /** @brief Set sensor value */
125 void setSensorValue(double value);
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700126 /** @brief Update sensor at regular intrval */
127 void updateVirtualSensor();
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000128 /** @brief Check if sensor value is in valid range */
129 bool sensorInRange(double value);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700130
131 /** @brief Map of list of parameters */
132 using ParamMap =
133 std::unordered_map<std::string, std::unique_ptr<SensorParam>>;
134 ParamMap paramMap;
135
136 private:
137 /** @brief sdbusplus bus client connection. */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500138 sdbusplus::bus_t& bus;
Vijay Khemka32a71562020-09-10 15:29:18 -0700139 /** @brief name of sensor */
140 std::string name;
Tao Lindc777012022-07-27 20:41:46 +0800141
142 /** @brief Virtual sensor path in entityManager Dbus.
143 * This value is used to set thresholds/create association
144 */
145 std::string entityPath;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700146 /** @brief Expression string for virtual sensor value calculations */
147 std::string exprStr;
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700148 /** @brief symbol table from exprtk */
149 exprtk::symbol_table<double> symbols{};
150 /** @brief expression from exprtk to calculate sensor value */
151 exprtk::expression<double> expression{};
Matt Spinler9f1ef4f2020-11-09 15:59:11 -0600152 /** @brief The vecops package so the expression can use vectors */
153 exprtk::rtl::vecops::package<double> vecopsPackage;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000154 /** @brief The maximum valid value for an input sensor **/
155 double maxValidInput = std::numeric_limits<double>::infinity();
156 /** @brief The minimum valid value for an input sensor **/
157 double minValidInput = -std::numeric_limits<double>::infinity();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700158
Matt Spinlerce675222021-01-14 16:38:09 -0600159 /** @brief The critical threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600160 std::unique_ptr<Threshold<CriticalObject>> criticalIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600161 /** @brief The warning threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600162 std::unique_ptr<Threshold<WarningObject>> warningIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600163 /** @brief The soft shutdown threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600164 std::unique_ptr<Threshold<SoftShutdownObject>> softShutdownIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600165 /** @brief The hard shutdown threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600166 std::unique_ptr<Threshold<HardShutdownObject>> hardShutdownIface;
Matt Spinlerb306b032021-02-01 10:05:46 -0600167 /** @brief The performance loss threshold interface object */
168 std::unique_ptr<Threshold<PerformanceLossObject>> perfLossIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600169
Lei YU0fcf0e12021-06-04 11:14:17 +0800170 /** @brief The association interface object */
171 std::unique_ptr<AssociationObject> associationIface;
172
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700173 /** @brief Read config from json object and initialize sensor data
174 * for each virtual sensor
175 */
Matt Spinlerce675222021-01-14 16:38:09 -0600176 void initVirtualSensor(const Json& sensorConfig,
177 const std::string& objPath);
178
Rashmica Guptae7efe132021-07-27 19:42:11 +1000179 /** @brief Read config from interface map and initialize sensor data
180 * for each virtual sensor
181 */
182 void initVirtualSensor(const InterfaceMap& interfaceMap,
183 const std::string& objPath,
184 const std::string& sensorType,
185 const std::string& calculationType);
186
187 /** @brief Returns which calculation function or expression to use */
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000188 double calculateValue(const std::string& sensortype,
189 const VirtualSensor::ParamMap& paramMap);
190 /** @brief Calculate median value from sensors */
191 double
192 calculateModifiedMedianValue(const VirtualSensor::ParamMap& paramMap);
Tao Linf6b7e0a2022-10-09 09:35:44 +0800193 /** @brief Calculate maximum value from sensors */
194 double calculateMaximumValue(const VirtualSensor::ParamMap& paramMap);
Rashmica Gupta3e999192021-06-09 16:17:04 +1000195 /** @brief create threshold objects from json config */
196 void createThresholds(const Json& threshold, const std::string& objPath);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000197 /** @brief parse config from entity manager **/
198 void parseConfigInterface(const PropertyMap& propertyMap,
199 const std::string& sensorType,
200 const std::string& interface);
Rashmica Gupta3e999192021-06-09 16:17:04 +1000201
Vijay Khemka32a71562020-09-10 15:29:18 -0700202 /** @brief Check Sensor threshold and update alarm and log */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600203 template <typename V, typename T>
204 void checkThresholds(V value, T& threshold)
Matt Spinler8f5e6112021-01-15 10:44:32 -0600205 {
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600206 if (!threshold)
207 return;
Matt Spinler8f5e6112021-01-15 10:44:32 -0600208
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600209 static constexpr auto tname = T::element_type::name;
210
211 auto alarmHigh = threshold->alarmHigh();
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000212 auto highHysteresis = threshold->getHighHysteresis();
Matt Spinlera4fe6652021-01-28 14:02:59 -0600213 if ((!alarmHigh && value >= threshold->high()) ||
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000214 (alarmHigh && value < (threshold->high() - highHysteresis)))
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600215 {
216 if (!alarmHigh)
Matt Spinler8f5e6112021-01-15 10:44:32 -0600217 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500218 error("ASSERT: sensor {SENSOR} is above the upper threshold "
219 "{THRESHOLD}.",
220 "SENSOR", name, "THRESHOLD", tname);
George Hung4294e6d2021-04-14 20:58:21 +0800221 threshold->alarmHighSignalAsserted(value);
Matt Spinler8f5e6112021-01-15 10:44:32 -0600222 }
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600223 else
Matt Spinler8f5e6112021-01-15 10:44:32 -0600224 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500225 info("DEASSERT: sensor {SENSOR} is under the upper threshold "
226 "{THRESHOLD}.",
227 "SENSOR", name, "THRESHOLD", tname);
George Hung4294e6d2021-04-14 20:58:21 +0800228 threshold->alarmHighSignalDeasserted(value);
Matt Spinler8f5e6112021-01-15 10:44:32 -0600229 }
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600230 threshold->alarmHigh(!alarmHigh);
231 }
232
233 auto alarmLow = threshold->alarmLow();
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000234 auto lowHysteresis = threshold->getLowHysteresis();
Matt Spinlera4fe6652021-01-28 14:02:59 -0600235 if ((!alarmLow && value <= threshold->low()) ||
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000236 (alarmLow && value > (threshold->low() + lowHysteresis)))
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600237 {
238 if (!alarmLow)
239 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500240 error("ASSERT: sensor {SENSOR} is below the lower threshold "
241 "{THRESHOLD}.",
242 "SENSOR", name, "THRESHOLD", tname);
George Hung4294e6d2021-04-14 20:58:21 +0800243 threshold->alarmLowSignalAsserted(value);
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600244 }
245 else
246 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500247 info("DEASSERT: sensor {SENSOR} is above the lower threshold "
248 "{THRESHOLD}.",
249 "SENSOR", name, "THRESHOLD", tname);
George Hung4294e6d2021-04-14 20:58:21 +0800250 threshold->alarmLowSignalDeasserted(value);
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600251 }
252 threshold->alarmLow(!alarmLow);
Matt Spinler8f5e6112021-01-15 10:44:32 -0600253 }
254 }
Tao Lindc777012022-07-27 20:41:46 +0800255
256 /** @brief Create Association from entityPath*/
257 void createAssociation(const std::string& objPath,
258 const std::string& entityPath);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700259};
260
261class VirtualSensors
262{
263 public:
264 VirtualSensors() = delete;
265 virtual ~VirtualSensors() = default;
266
267 /** @brief Constructs VirtualSensors
268 *
269 * @param[in] bus - Handle to system dbus
270 */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500271 explicit VirtualSensors(sdbusplus::bus_t& bus) : bus(bus)
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700272 {
273 createVirtualSensors();
274 }
Rashmica Guptae7efe132021-07-27 19:42:11 +1000275 /** @brief Calls createVirtualSensor when interface added */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500276 void propertiesChanged(sdbusplus::message_t& msg);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700277
278 private:
279 /** @brief sdbusplus bus client connection. */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500280 sdbusplus::bus_t& bus;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000281 /** @brief Get virual sensor config from DBus**/
282 ManagedObjectType getObjectsFromDBus();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700283 /** @brief Parsing virtual sensor config JSON file */
Patrick Williams32dff212023-02-09 13:54:18 -0600284 Json parseConfigFile();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700285
Rashmica Guptae7efe132021-07-27 19:42:11 +1000286 /** @brief Matches for virtual sensors */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500287 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700288 /** @brief Map of the object VirtualSensor */
289 std::unordered_map<std::string, std::unique_ptr<VirtualSensor>>
290 virtualSensorsMap;
291
Rashmica Guptae7efe132021-07-27 19:42:11 +1000292 /** @brief Create list of virtual sensors from JSON config*/
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700293 void createVirtualSensors();
Rashmica Guptae7efe132021-07-27 19:42:11 +1000294 /** @brief Create list of virtual sensors from DBus config */
295 void createVirtualSensorsFromDBus(const std::string& calculationType);
296 /** @brief Setup matches for virtual sensors */
297 void setupMatches();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700298};
299
300} // namespace virtualSensor
301} // namespace phosphor