blob: a04a7435d26c481338c4424e49810f2fa37bd2de [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"
Vijay Khemka7452a862020-08-11 16:01:23 -07003
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07004#include <nlohmann/json.hpp>
5#include <sdbusplus/bus.hpp>
6#include <xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp>
7#include <xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp>
8#include <xyz/openbmc_project/Sensor/Value/server.hpp>
9
10#include <map>
11#include <string>
12
13namespace phosphor
14{
15namespace virtualSensor
16{
17
18using Json = nlohmann::json;
19using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
20
21using CriticalInterface =
22 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical;
23
24using WarningInterface =
25 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning;
26
27using sensorIfaces =
28 sdbusplus::server::object::object<ValueIface, CriticalInterface,
29 WarningInterface>;
30
31class SensorParam
32{
33 public:
34 SensorParam() = delete;
35 virtual ~SensorParam() = default;
36
37 enum ParamType
38 {
39 constParam,
40 dbusParam
41 };
42
43 /** @brief Constructs SensorParam (type = constParam)
44 *
45 * @param[in] value - Value of constant parameter
46 */
47 explicit SensorParam(double value) : value(value), paramType(constParam)
48 {}
49
Vijay Khemka7452a862020-08-11 16:01:23 -070050 /** @brief Constructs SensorParam (type = dbusParam)
51 *
52 * @param[in] bus - Handle to system dbus
53 * @param[in] path - The Dbus path of sensor
Vijay Khemka51f898e2020-09-09 22:24:18 -070054 * @param[in] ctx - sensor context for update
Vijay Khemka7452a862020-08-11 16:01:23 -070055 */
Vijay Khemka51f898e2020-09-09 22:24:18 -070056 SensorParam(sdbusplus::bus::bus& bus, std::string path, void* ctx) :
57 dbusSensor(std::make_unique<DbusSensor>(bus, path, ctx)),
Vijay Khemka7452a862020-08-11 16:01:23 -070058 paramType(dbusParam)
59 {}
60
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070061 /** @brief Get sensor value property from D-bus interface */
62 double getParamValue();
63
64 private:
Vijay Khemka7452a862020-08-11 16:01:23 -070065 std::unique_ptr<DbusSensor> dbusSensor = nullptr;
66 double value = 0;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070067 ParamType paramType;
68};
69
70class VirtualSensor : public sensorIfaces
71{
72 public:
73 VirtualSensor() = delete;
74 virtual ~VirtualSensor() = default;
75
76 /** @brief Constructs VirtualSensor
77 *
78 * @param[in] bus - Handle to system dbus
79 * @param[in] objPath - The Dbus path of sensor
80 * @param[in] sensorConfig - Json object for sensor config
81 */
82 VirtualSensor(sdbusplus::bus::bus& bus, const char* objPath,
Vijay Khemka32a71562020-09-10 15:29:18 -070083 const Json& sensorConfig, const std::string& name) :
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070084 sensorIfaces(bus, objPath),
Vijay Khemka32a71562020-09-10 15:29:18 -070085 bus(bus), name(name)
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070086 {
87 initVirtualSensor(sensorConfig);
88 }
89
90 struct Threshold
91 {
92 double criticalHigh;
93 double criticalLow;
94 double warningHigh;
95 double warningLow;
96 };
97
98 /** @brief Set sensor value */
99 void setSensorValue(double value);
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700100 /** @brief Update sensor at regular intrval */
101 void updateVirtualSensor();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700102
103 /** @brief Map of list of parameters */
104 using ParamMap =
105 std::unordered_map<std::string, std::unique_ptr<SensorParam>>;
106 ParamMap paramMap;
107
108 private:
109 /** @brief sdbusplus bus client connection. */
110 sdbusplus::bus::bus& bus;
Vijay Khemka32a71562020-09-10 15:29:18 -0700111 /** @brief name of sensor */
112 std::string name;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700113 /** @brief Expression string for virtual sensor value calculations */
114 std::string exprStr;
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700115 /** @brief symbol table from exprtk */
116 exprtk::symbol_table<double> symbols{};
117 /** @brief expression from exprtk to calculate sensor value */
118 exprtk::expression<double> expression{};
Matt Spinler9f1ef4f2020-11-09 15:59:11 -0600119 /** @brief The vecops package so the expression can use vectors */
120 exprtk::rtl::vecops::package<double> vecopsPackage;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700121
122 /** @brief Read config from json object and initialize sensor data
123 * for each virtual sensor
124 */
125 void initVirtualSensor(const Json& sensorConfig);
126 /** @brief Set Sensor Threshold to D-bus at beginning */
Vijay Khemkac62a5542020-09-10 14:45:49 -0700127 void setSensorThreshold(Threshold& sensorThreshold);
Vijay Khemka32a71562020-09-10 15:29:18 -0700128 /** @brief Check Sensor threshold and update alarm and log */
129 void checkSensorThreshold(const double value);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700130};
131
132class VirtualSensors
133{
134 public:
135 VirtualSensors() = delete;
136 virtual ~VirtualSensors() = default;
137
138 /** @brief Constructs VirtualSensors
139 *
140 * @param[in] bus - Handle to system dbus
141 */
142 explicit VirtualSensors(sdbusplus::bus::bus& bus) : bus(bus)
143 {
144 createVirtualSensors();
145 }
146
147 private:
148 /** @brief sdbusplus bus client connection. */
149 sdbusplus::bus::bus& bus;
150 /** @brief Parsing virtual sensor config JSON file */
151 Json parseConfigFile(const std::string configFile);
152
153 /** @brief Map of the object VirtualSensor */
154 std::unordered_map<std::string, std::unique_ptr<VirtualSensor>>
155 virtualSensorsMap;
156
157 /** @brief Create list of virtual sensors */
158 void createVirtualSensors();
159};
160
161} // namespace virtualSensor
162} // namespace phosphor