Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 1 | #include "dbusSensor.hpp" |
Vijay Khemka | 92d648b | 2020-08-21 18:55:07 -0700 | [diff] [blame] | 2 | #include "exprtkTools.hpp" |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 3 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 4 | #include <nlohmann/json.hpp> |
| 5 | #include <sdbusplus/bus.hpp> |
| 6 | #include <xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp> |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Sensor/Threshold/HardShutdown/server.hpp> |
| 8 | #include <xyz/openbmc_project/Sensor/Threshold/SoftShutdown/server.hpp> |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 9 | #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 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace virtualSensor |
| 18 | { |
| 19 | |
| 20 | using Json = nlohmann::json; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 21 | |
| 22 | template <typename... T> |
| 23 | using ServerObject = typename sdbusplus::server::object::object<T...>; |
| 24 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 25 | using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 26 | using ValueObject = ServerObject<ValueIface>; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 27 | |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 28 | using CriticalIface = |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 29 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 30 | using CriticalObject = ServerObject<CriticalIface>; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 31 | |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 32 | using WarningIface = |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 33 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning; |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 34 | using WarningObject = ServerObject<WarningIface>; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 35 | |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 36 | using SoftShutdownIface = |
| 37 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::SoftShutdown; |
| 38 | using SoftShutdownObject = ServerObject<SoftShutdownIface>; |
| 39 | |
| 40 | using HardShutdownIface = |
| 41 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::HardShutdown; |
| 42 | using HardShutdownObject = ServerObject<HardShutdownIface>; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 43 | |
| 44 | class 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 Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 63 | /** @brief Constructs SensorParam (type = dbusParam) |
| 64 | * |
| 65 | * @param[in] bus - Handle to system dbus |
| 66 | * @param[in] path - The Dbus path of sensor |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 67 | * @param[in] ctx - sensor context for update |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 68 | */ |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 69 | SensorParam(sdbusplus::bus::bus& bus, std::string path, void* ctx) : |
| 70 | dbusSensor(std::make_unique<DbusSensor>(bus, path, ctx)), |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 71 | paramType(dbusParam) |
| 72 | {} |
| 73 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 74 | /** @brief Get sensor value property from D-bus interface */ |
| 75 | double getParamValue(); |
| 76 | |
| 77 | private: |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 78 | std::unique_ptr<DbusSensor> dbusSensor = nullptr; |
| 79 | double value = 0; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 80 | ParamType paramType; |
| 81 | }; |
| 82 | |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 83 | class VirtualSensor : public ValueObject |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 84 | { |
| 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 Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 96 | const Json& sensorConfig, const std::string& name) : |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 97 | ValueObject(bus, objPath), |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 98 | bus(bus), name(name) |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 99 | { |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 100 | initVirtualSensor(sensorConfig, objPath); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 103 | /** @brief Set sensor value */ |
| 104 | void setSensorValue(double value); |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 105 | /** @brief Update sensor at regular intrval */ |
| 106 | void updateVirtualSensor(); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 107 | |
| 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 Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 116 | /** @brief name of sensor */ |
| 117 | std::string name; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 118 | /** @brief Expression string for virtual sensor value calculations */ |
| 119 | std::string exprStr; |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 120 | /** @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 Spinler | 9f1ef4f | 2020-11-09 15:59:11 -0600 | [diff] [blame] | 124 | /** @brief The vecops package so the expression can use vectors */ |
| 125 | exprtk::rtl::vecops::package<double> vecopsPackage; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 126 | |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 127 | /** @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 Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 136 | /** @brief Read config from json object and initialize sensor data |
| 137 | * for each virtual sensor |
| 138 | */ |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 139 | void initVirtualSensor(const Json& sensorConfig, |
| 140 | const std::string& objPath); |
| 141 | |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 142 | /** @brief Check Sensor threshold and update alarm and log */ |
| 143 | void checkSensorThreshold(const double value); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | class 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 |