blob: 6b11eb4d9b492f3e04eb07f1aa31614176e47330 [file] [log] [blame]
Vijay Khemka7452a862020-08-11 16:01:23 -07001#include "dbusSensor.hpp"
2
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07003#include <nlohmann/json.hpp>
4#include <sdbusplus/bus.hpp>
5#include <xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp>
6#include <xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp>
7#include <xyz/openbmc_project/Sensor/Value/server.hpp>
8
9#include <map>
10#include <string>
11
12namespace phosphor
13{
14namespace virtualSensor
15{
16
17using Json = nlohmann::json;
18using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
19
20using CriticalInterface =
21 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical;
22
23using WarningInterface =
24 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning;
25
26using sensorIfaces =
27 sdbusplus::server::object::object<ValueIface, CriticalInterface,
28 WarningInterface>;
29
30class SensorParam
31{
32 public:
33 SensorParam() = delete;
34 virtual ~SensorParam() = default;
35
36 enum ParamType
37 {
38 constParam,
39 dbusParam
40 };
41
42 /** @brief Constructs SensorParam (type = constParam)
43 *
44 * @param[in] value - Value of constant parameter
45 */
46 explicit SensorParam(double value) : value(value), paramType(constParam)
47 {}
48
Vijay Khemka7452a862020-08-11 16:01:23 -070049 /** @brief Constructs SensorParam (type = dbusParam)
50 *
51 * @param[in] bus - Handle to system dbus
52 * @param[in] path - The Dbus path of sensor
53 */
54 SensorParam(sdbusplus::bus::bus& bus, std::string path) :
55 dbusSensor(std::make_unique<DbusSensor>(bus, path)),
56 paramType(dbusParam)
57 {}
58
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070059 /** @brief Get sensor value property from D-bus interface */
60 double getParamValue();
61
62 private:
Vijay Khemka7452a862020-08-11 16:01:23 -070063 std::unique_ptr<DbusSensor> dbusSensor = nullptr;
64 double value = 0;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070065 ParamType paramType;
66};
67
68class VirtualSensor : public sensorIfaces
69{
70 public:
71 VirtualSensor() = delete;
72 virtual ~VirtualSensor() = default;
73
74 /** @brief Constructs VirtualSensor
75 *
76 * @param[in] bus - Handle to system dbus
77 * @param[in] objPath - The Dbus path of sensor
78 * @param[in] sensorConfig - Json object for sensor config
79 */
80 VirtualSensor(sdbusplus::bus::bus& bus, const char* objPath,
81 const Json& sensorConfig) :
82 sensorIfaces(bus, objPath),
83 bus(bus)
84 {
85 initVirtualSensor(sensorConfig);
86 }
87
88 struct Threshold
89 {
90 double criticalHigh;
91 double criticalLow;
92 double warningHigh;
93 double warningLow;
94 };
95
96 /** @brief Set sensor value */
97 void setSensorValue(double value);
98
99 /** @brief Map of list of parameters */
100 using ParamMap =
101 std::unordered_map<std::string, std::unique_ptr<SensorParam>>;
102 ParamMap paramMap;
103
104 private:
105 /** @brief sdbusplus bus client connection. */
106 sdbusplus::bus::bus& bus;
107 /** @brief Expression string for virtual sensor value calculations */
108 std::string exprStr;
109 /** @brief Sensor Threshold config values */
110 struct Threshold sensorThreshold;
111
112 /** @brief Read config from json object and initialize sensor data
113 * for each virtual sensor
114 */
115 void initVirtualSensor(const Json& sensorConfig);
116 /** @brief Set Sensor Threshold to D-bus at beginning */
117 void setSensorThreshold();
118 /** @brief Update sensor at regular intrval */
119 void updateVirtualSensor();
120};
121
122class VirtualSensors
123{
124 public:
125 VirtualSensors() = delete;
126 virtual ~VirtualSensors() = default;
127
128 /** @brief Constructs VirtualSensors
129 *
130 * @param[in] bus - Handle to system dbus
131 */
132 explicit VirtualSensors(sdbusplus::bus::bus& bus) : bus(bus)
133 {
134 createVirtualSensors();
135 }
136
137 private:
138 /** @brief sdbusplus bus client connection. */
139 sdbusplus::bus::bus& bus;
140 /** @brief Parsing virtual sensor config JSON file */
141 Json parseConfigFile(const std::string configFile);
142
143 /** @brief Map of the object VirtualSensor */
144 std::unordered_map<std::string, std::unique_ptr<VirtualSensor>>
145 virtualSensorsMap;
146
147 /** @brief Create list of virtual sensors */
148 void createVirtualSensors();
149};
150
151} // namespace virtualSensor
152} // namespace phosphor