blob: ada20d95bda9ab4200996897c769c18f82f77876 [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
5#include <fmt/format.h>
Vijay Khemka7452a862020-08-11 16:01:23 -07006
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07007#include <nlohmann/json.hpp>
8#include <sdbusplus/bus.hpp>
Lei YU0fcf0e12021-06-04 11:14:17 +08009#include <xyz/openbmc_project/Association/Definitions/server.hpp>
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070010#include <xyz/openbmc_project/Sensor/Value/server.hpp>
11
12#include <map>
13#include <string>
14
15namespace phosphor
16{
17namespace virtualSensor
18{
19
Rashmica Guptae7efe132021-07-27 19:42:11 +100020using BasicVariantType =
21 std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
22 int16_t, uint16_t, uint8_t, bool, std::vector<std::string>>;
23
24using PropertyMap = std::map<std::string, BasicVariantType>;
25
26using InterfaceMap = std::map<std::string, PropertyMap>;
27
28using ManagedObjectType =
29 std::map<sdbusplus::message::object_path, InterfaceMap>;
30
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070031using Json = nlohmann::json;
Matt Spinlerce675222021-01-14 16:38:09 -060032
33template <typename... T>
34using ServerObject = typename sdbusplus::server::object::object<T...>;
35
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070036using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
Matt Spinlerce675222021-01-14 16:38:09 -060037using ValueObject = ServerObject<ValueIface>;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070038
Lei YU0fcf0e12021-06-04 11:14:17 +080039using AssociationIface =
40 sdbusplus::xyz::openbmc_project::Association::server::Definitions;
41using AssociationObject = ServerObject<AssociationIface>;
42
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070043class SensorParam
44{
45 public:
46 SensorParam() = delete;
47 virtual ~SensorParam() = default;
48
49 enum ParamType
50 {
51 constParam,
52 dbusParam
53 };
54
55 /** @brief Constructs SensorParam (type = constParam)
56 *
57 * @param[in] value - Value of constant parameter
58 */
59 explicit SensorParam(double value) : value(value), paramType(constParam)
60 {}
61
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 */
Vijay Khemka51f898e2020-09-09 22:24:18 -070068 SensorParam(sdbusplus::bus::bus& bus, std::string path, void* ctx) :
69 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 */
94 VirtualSensor(sdbusplus::bus::bus& 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
110 *
111 */
112 VirtualSensor(sdbusplus::bus::bus& bus, const char* objPath,
113 const InterfaceMap& ifacemap, const std::string& name,
114 const std::string& type, const std::string& calculationType) :
115 ValueObject(bus, objPath, action::defer_emit),
116 bus(bus), name(name)
117 {
118 initVirtualSensor(ifacemap, objPath, type, calculationType);
119 }
120
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700121 /** @brief Set sensor value */
122 void setSensorValue(double value);
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700123 /** @brief Update sensor at regular intrval */
124 void updateVirtualSensor();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700125
126 /** @brief Map of list of parameters */
127 using ParamMap =
128 std::unordered_map<std::string, std::unique_ptr<SensorParam>>;
129 ParamMap paramMap;
130
131 private:
132 /** @brief sdbusplus bus client connection. */
133 sdbusplus::bus::bus& bus;
Vijay Khemka32a71562020-09-10 15:29:18 -0700134 /** @brief name of sensor */
135 std::string name;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700136 /** @brief Expression string for virtual sensor value calculations */
137 std::string exprStr;
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700138 /** @brief symbol table from exprtk */
139 exprtk::symbol_table<double> symbols{};
140 /** @brief expression from exprtk to calculate sensor value */
141 exprtk::expression<double> expression{};
Matt Spinler9f1ef4f2020-11-09 15:59:11 -0600142 /** @brief The vecops package so the expression can use vectors */
143 exprtk::rtl::vecops::package<double> vecopsPackage;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000144 /** @brief The maximum valid value for an input sensor **/
145 double maxValidInput = std::numeric_limits<double>::infinity();
146 /** @brief The minimum valid value for an input sensor **/
147 double minValidInput = -std::numeric_limits<double>::infinity();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700148
Matt Spinlerce675222021-01-14 16:38:09 -0600149 /** @brief The critical threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600150 std::unique_ptr<Threshold<CriticalObject>> criticalIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600151 /** @brief The warning threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600152 std::unique_ptr<Threshold<WarningObject>> warningIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600153 /** @brief The soft shutdown threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600154 std::unique_ptr<Threshold<SoftShutdownObject>> softShutdownIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600155 /** @brief The hard shutdown threshold interface object */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600156 std::unique_ptr<Threshold<HardShutdownObject>> hardShutdownIface;
Matt Spinlerb306b032021-02-01 10:05:46 -0600157 /** @brief The performance loss threshold interface object */
158 std::unique_ptr<Threshold<PerformanceLossObject>> perfLossIface;
Matt Spinlerce675222021-01-14 16:38:09 -0600159
Lei YU0fcf0e12021-06-04 11:14:17 +0800160 /** @brief The association interface object */
161 std::unique_ptr<AssociationObject> associationIface;
162
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700163 /** @brief Read config from json object and initialize sensor data
164 * for each virtual sensor
165 */
Matt Spinlerce675222021-01-14 16:38:09 -0600166 void initVirtualSensor(const Json& sensorConfig,
167 const std::string& objPath);
168
Rashmica Guptae7efe132021-07-27 19:42:11 +1000169 /** @brief Read config from interface map and initialize sensor data
170 * for each virtual sensor
171 */
172 void initVirtualSensor(const InterfaceMap& interfaceMap,
173 const std::string& objPath,
174 const std::string& sensorType,
175 const std::string& calculationType);
176
177 /** @brief Returns which calculation function or expression to use */
178 double calculateValue();
Rashmica Gupta3e999192021-06-09 16:17:04 +1000179 /** @brief create threshold objects from json config */
180 void createThresholds(const Json& threshold, const std::string& objPath);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000181 /** @brief parse config from entity manager **/
182 void parseConfigInterface(const PropertyMap& propertyMap,
183 const std::string& sensorType,
184 const std::string& interface);
Rashmica Gupta3e999192021-06-09 16:17:04 +1000185
Vijay Khemka32a71562020-09-10 15:29:18 -0700186 /** @brief Check Sensor threshold and update alarm and log */
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600187 template <typename V, typename T>
188 void checkThresholds(V value, T& threshold)
Matt Spinler8f5e6112021-01-15 10:44:32 -0600189 {
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600190 if (!threshold)
191 return;
Matt Spinler8f5e6112021-01-15 10:44:32 -0600192
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600193 static constexpr auto tname = T::element_type::name;
194
195 auto alarmHigh = threshold->alarmHigh();
Matt Spinlera4fe6652021-01-28 14:02:59 -0600196 if ((!alarmHigh && value >= threshold->high()) ||
197 (alarmHigh && value < threshold->high()))
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600198 {
199 if (!alarmHigh)
Matt Spinler8f5e6112021-01-15 10:44:32 -0600200 {
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600201 constexpr auto msg =
202 "ASSERT: {} has exceeded the {} high threshold";
203 log<level::ERR>(fmt::format(msg, name, tname).c_str());
George Hung4294e6d2021-04-14 20:58:21 +0800204 threshold->alarmHighSignalAsserted(value);
Matt Spinler8f5e6112021-01-15 10:44:32 -0600205 }
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600206 else
Matt Spinler8f5e6112021-01-15 10:44:32 -0600207 {
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600208 constexpr auto msg =
209 "DEASSERT: {} is under the {} high threshold";
210 log<level::INFO>(fmt::format(msg, name, tname).c_str());
George Hung4294e6d2021-04-14 20:58:21 +0800211 threshold->alarmHighSignalDeasserted(value);
Matt Spinler8f5e6112021-01-15 10:44:32 -0600212 }
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600213 threshold->alarmHigh(!alarmHigh);
214 }
215
216 auto alarmLow = threshold->alarmLow();
Matt Spinlera4fe6652021-01-28 14:02:59 -0600217 if ((!alarmLow && value <= threshold->low()) ||
218 (alarmLow && value > threshold->low()))
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600219 {
220 if (!alarmLow)
221 {
222 constexpr auto msg = "ASSERT: {} is under the {} low threshold";
223 log<level::ERR>(fmt::format(msg, name, tname).c_str());
George Hung4294e6d2021-04-14 20:58:21 +0800224 threshold->alarmLowSignalAsserted(value);
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600225 }
226 else
227 {
228 constexpr auto msg =
229 "DEASSERT: {} is above the {} low threshold";
230 log<level::INFO>(fmt::format(msg, name, tname).c_str());
George Hung4294e6d2021-04-14 20:58:21 +0800231 threshold->alarmLowSignalDeasserted(value);
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600232 }
233 threshold->alarmLow(!alarmLow);
Matt Spinler8f5e6112021-01-15 10:44:32 -0600234 }
235 }
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700236};
237
238class VirtualSensors
239{
240 public:
241 VirtualSensors() = delete;
242 virtual ~VirtualSensors() = default;
243
244 /** @brief Constructs VirtualSensors
245 *
246 * @param[in] bus - Handle to system dbus
247 */
248 explicit VirtualSensors(sdbusplus::bus::bus& bus) : bus(bus)
249 {
250 createVirtualSensors();
251 }
Rashmica Guptae7efe132021-07-27 19:42:11 +1000252 /** @brief Calls createVirtualSensor when interface added */
253 void propertiesChanged(sdbusplus::message::message& msg);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700254
255 private:
256 /** @brief sdbusplus bus client connection. */
257 sdbusplus::bus::bus& bus;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000258 /** @brief Get virual sensor config from DBus**/
259 ManagedObjectType getObjectsFromDBus();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700260 /** @brief Parsing virtual sensor config JSON file */
261 Json parseConfigFile(const std::string configFile);
262
Rashmica Guptae7efe132021-07-27 19:42:11 +1000263 /** @brief Matches for virtual sensors */
264 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700265 /** @brief Map of the object VirtualSensor */
266 std::unordered_map<std::string, std::unique_ptr<VirtualSensor>>
267 virtualSensorsMap;
268
Rashmica Guptae7efe132021-07-27 19:42:11 +1000269 /** @brief Create list of virtual sensors from JSON config*/
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700270 void createVirtualSensors();
Rashmica Guptae7efe132021-07-27 19:42:11 +1000271 /** @brief Create list of virtual sensors from DBus config */
272 void createVirtualSensorsFromDBus(const std::string& calculationType);
273 /** @brief Setup matches for virtual sensors */
274 void setupMatches();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700275};
276
277} // namespace virtualSensor
278} // namespace phosphor