blob: e39f8d9361555435bcb0bd3500a8d2b29762ab44 [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{};
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700119
120 /** @brief Read config from json object and initialize sensor data
121 * for each virtual sensor
122 */
123 void initVirtualSensor(const Json& sensorConfig);
124 /** @brief Set Sensor Threshold to D-bus at beginning */
Vijay Khemkac62a5542020-09-10 14:45:49 -0700125 void setSensorThreshold(Threshold& sensorThreshold);
Vijay Khemka32a71562020-09-10 15:29:18 -0700126 /** @brief Check Sensor threshold and update alarm and log */
127 void checkSensorThreshold(const double value);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700128};
129
130class VirtualSensors
131{
132 public:
133 VirtualSensors() = delete;
134 virtual ~VirtualSensors() = default;
135
136 /** @brief Constructs VirtualSensors
137 *
138 * @param[in] bus - Handle to system dbus
139 */
140 explicit VirtualSensors(sdbusplus::bus::bus& bus) : bus(bus)
141 {
142 createVirtualSensors();
143 }
144
145 private:
146 /** @brief sdbusplus bus client connection. */
147 sdbusplus::bus::bus& bus;
148 /** @brief Parsing virtual sensor config JSON file */
149 Json parseConfigFile(const std::string configFile);
150
151 /** @brief Map of the object VirtualSensor */
152 std::unordered_map<std::string, std::unique_ptr<VirtualSensor>>
153 virtualSensorsMap;
154
155 /** @brief Create list of virtual sensors */
156 void createVirtualSensors();
157};
158
159} // namespace virtualSensor
160} // namespace phosphor