blob: 4f9de1987d0dba52b72aa6504aba35a95902b434 [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,
83 const Json& sensorConfig) :
84 sensorIfaces(bus, objPath),
85 bus(bus)
86 {
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;
111 /** @brief Expression string for virtual sensor value calculations */
112 std::string exprStr;
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700113 /** @brief symbol table from exprtk */
114 exprtk::symbol_table<double> symbols{};
115 /** @brief expression from exprtk to calculate sensor value */
116 exprtk::expression<double> expression{};
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700117
118 /** @brief Read config from json object and initialize sensor data
119 * for each virtual sensor
120 */
121 void initVirtualSensor(const Json& sensorConfig);
122 /** @brief Set Sensor Threshold to D-bus at beginning */
Vijay Khemkac62a5542020-09-10 14:45:49 -0700123 void setSensorThreshold(Threshold& sensorThreshold);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700124};
125
126class VirtualSensors
127{
128 public:
129 VirtualSensors() = delete;
130 virtual ~VirtualSensors() = default;
131
132 /** @brief Constructs VirtualSensors
133 *
134 * @param[in] bus - Handle to system dbus
135 */
136 explicit VirtualSensors(sdbusplus::bus::bus& bus) : bus(bus)
137 {
138 createVirtualSensors();
139 }
140
141 private:
142 /** @brief sdbusplus bus client connection. */
143 sdbusplus::bus::bus& bus;
144 /** @brief Parsing virtual sensor config JSON file */
145 Json parseConfigFile(const std::string configFile);
146
147 /** @brief Map of the object VirtualSensor */
148 std::unordered_map<std::string, std::unique_ptr<VirtualSensor>>
149 virtualSensorsMap;
150
151 /** @brief Create list of virtual sensors */
152 void createVirtualSensors();
153};
154
155} // namespace virtualSensor
156} // namespace phosphor