blob: e60bb1aa1505a6af6fd8356c3f192d7de3114971 [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>
Matt Spinlerce675222021-01-14 16:38:09 -06007#include <xyz/openbmc_project/Sensor/Threshold/HardShutdown/server.hpp>
8#include <xyz/openbmc_project/Sensor/Threshold/SoftShutdown/server.hpp>
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07009#include <xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp>
10#include <xyz/openbmc_project/Sensor/Value/server.hpp>
11
12#include <map>
13#include <string>
14
15namespace phosphor
16{
17namespace virtualSensor
18{
19
20using Json = nlohmann::json;
Matt Spinlerce675222021-01-14 16:38:09 -060021
22template <typename... T>
23using ServerObject = typename sdbusplus::server::object::object<T...>;
24
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070025using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
Matt Spinlerce675222021-01-14 16:38:09 -060026using ValueObject = ServerObject<ValueIface>;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070027
Matt Spinlerce675222021-01-14 16:38:09 -060028using CriticalIface =
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070029 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical;
Matt Spinlerce675222021-01-14 16:38:09 -060030using CriticalObject = ServerObject<CriticalIface>;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070031
Matt Spinlerce675222021-01-14 16:38:09 -060032using WarningIface =
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070033 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning;
Matt Spinlerce675222021-01-14 16:38:09 -060034using WarningObject = ServerObject<WarningIface>;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070035
Matt Spinlerce675222021-01-14 16:38:09 -060036using SoftShutdownIface =
37 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::SoftShutdown;
38using SoftShutdownObject = ServerObject<SoftShutdownIface>;
39
40using HardShutdownIface =
41 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::HardShutdown;
42using HardShutdownObject = ServerObject<HardShutdownIface>;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070043
44class SensorParam
45{
46 public:
47 SensorParam() = delete;
48 virtual ~SensorParam() = default;
49
50 enum ParamType
51 {
52 constParam,
53 dbusParam
54 };
55
56 /** @brief Constructs SensorParam (type = constParam)
57 *
58 * @param[in] value - Value of constant parameter
59 */
60 explicit SensorParam(double value) : value(value), paramType(constParam)
61 {}
62
Vijay Khemka7452a862020-08-11 16:01:23 -070063 /** @brief Constructs SensorParam (type = dbusParam)
64 *
65 * @param[in] bus - Handle to system dbus
66 * @param[in] path - The Dbus path of sensor
Vijay Khemka51f898e2020-09-09 22:24:18 -070067 * @param[in] ctx - sensor context for update
Vijay Khemka7452a862020-08-11 16:01:23 -070068 */
Vijay Khemka51f898e2020-09-09 22:24:18 -070069 SensorParam(sdbusplus::bus::bus& bus, std::string path, void* ctx) :
70 dbusSensor(std::make_unique<DbusSensor>(bus, path, ctx)),
Vijay Khemka7452a862020-08-11 16:01:23 -070071 paramType(dbusParam)
72 {}
73
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070074 /** @brief Get sensor value property from D-bus interface */
75 double getParamValue();
76
77 private:
Vijay Khemka7452a862020-08-11 16:01:23 -070078 std::unique_ptr<DbusSensor> dbusSensor = nullptr;
79 double value = 0;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070080 ParamType paramType;
81};
82
Matt Spinlerce675222021-01-14 16:38:09 -060083class VirtualSensor : public ValueObject
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070084{
85 public:
86 VirtualSensor() = delete;
87 virtual ~VirtualSensor() = default;
88
89 /** @brief Constructs VirtualSensor
90 *
91 * @param[in] bus - Handle to system dbus
92 * @param[in] objPath - The Dbus path of sensor
93 * @param[in] sensorConfig - Json object for sensor config
94 */
95 VirtualSensor(sdbusplus::bus::bus& bus, const char* objPath,
Vijay Khemka32a71562020-09-10 15:29:18 -070096 const Json& sensorConfig, const std::string& name) :
Matt Spinlerce675222021-01-14 16:38:09 -060097 ValueObject(bus, objPath),
Vijay Khemka32a71562020-09-10 15:29:18 -070098 bus(bus), name(name)
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070099 {
Matt Spinlerce675222021-01-14 16:38:09 -0600100 initVirtualSensor(sensorConfig, objPath);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700101 }
102
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700103 /** @brief Set sensor value */
104 void setSensorValue(double value);
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700105 /** @brief Update sensor at regular intrval */
106 void updateVirtualSensor();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700107
108 /** @brief Map of list of parameters */
109 using ParamMap =
110 std::unordered_map<std::string, std::unique_ptr<SensorParam>>;
111 ParamMap paramMap;
112
113 private:
114 /** @brief sdbusplus bus client connection. */
115 sdbusplus::bus::bus& bus;
Vijay Khemka32a71562020-09-10 15:29:18 -0700116 /** @brief name of sensor */
117 std::string name;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700118 /** @brief Expression string for virtual sensor value calculations */
119 std::string exprStr;
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700120 /** @brief symbol table from exprtk */
121 exprtk::symbol_table<double> symbols{};
122 /** @brief expression from exprtk to calculate sensor value */
123 exprtk::expression<double> expression{};
Matt Spinler9f1ef4f2020-11-09 15:59:11 -0600124 /** @brief The vecops package so the expression can use vectors */
125 exprtk::rtl::vecops::package<double> vecopsPackage;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700126
Matt Spinlerce675222021-01-14 16:38:09 -0600127 /** @brief The critical threshold interface object */
128 std::unique_ptr<CriticalObject> criticalIface;
129 /** @brief The warning threshold interface object */
130 std::unique_ptr<WarningObject> warningIface;
131 /** @brief The soft shutdown threshold interface object */
132 std::unique_ptr<SoftShutdownObject> softShutdownIface;
133 /** @brief The hard shutdown threshold interface object */
134 std::unique_ptr<HardShutdownObject> hardShutdownIface;
135
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700136 /** @brief Read config from json object and initialize sensor data
137 * for each virtual sensor
138 */
Matt Spinlerce675222021-01-14 16:38:09 -0600139 void initVirtualSensor(const Json& sensorConfig,
140 const std::string& objPath);
141
Vijay Khemka32a71562020-09-10 15:29:18 -0700142 /** @brief Check Sensor threshold and update alarm and log */
143 void checkSensorThreshold(const double value);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700144};
145
146class VirtualSensors
147{
148 public:
149 VirtualSensors() = delete;
150 virtual ~VirtualSensors() = default;
151
152 /** @brief Constructs VirtualSensors
153 *
154 * @param[in] bus - Handle to system dbus
155 */
156 explicit VirtualSensors(sdbusplus::bus::bus& bus) : bus(bus)
157 {
158 createVirtualSensors();
159 }
160
161 private:
162 /** @brief sdbusplus bus client connection. */
163 sdbusplus::bus::bus& bus;
164 /** @brief Parsing virtual sensor config JSON file */
165 Json parseConfigFile(const std::string configFile);
166
167 /** @brief Map of the object VirtualSensor */
168 std::unordered_map<std::string, std::unique_ptr<VirtualSensor>>
169 virtualSensorsMap;
170
171 /** @brief Create list of virtual sensors */
172 void createVirtualSensors();
173};
174
175} // namespace virtualSensor
176} // namespace phosphor