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> |
| 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 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace virtualSensor |
| 16 | { |
| 17 | |
| 18 | using Json = nlohmann::json; |
| 19 | using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; |
| 20 | |
| 21 | using CriticalInterface = |
| 22 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical; |
| 23 | |
| 24 | using WarningInterface = |
| 25 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning; |
| 26 | |
| 27 | using sensorIfaces = |
| 28 | sdbusplus::server::object::object<ValueIface, CriticalInterface, |
| 29 | WarningInterface>; |
| 30 | |
| 31 | class 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 Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 50 | /** @brief Constructs SensorParam (type = dbusParam) |
| 51 | * |
| 52 | * @param[in] bus - Handle to system dbus |
| 53 | * @param[in] path - The Dbus path of sensor |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 54 | * @param[in] ctx - sensor context for update |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 55 | */ |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 56 | SensorParam(sdbusplus::bus::bus& bus, std::string path, void* ctx) : |
| 57 | dbusSensor(std::make_unique<DbusSensor>(bus, path, ctx)), |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 58 | paramType(dbusParam) |
| 59 | {} |
| 60 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 61 | /** @brief Get sensor value property from D-bus interface */ |
| 62 | double getParamValue(); |
| 63 | |
| 64 | private: |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 65 | std::unique_ptr<DbusSensor> dbusSensor = nullptr; |
| 66 | double value = 0; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 67 | ParamType paramType; |
| 68 | }; |
| 69 | |
| 70 | class 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 Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 83 | const Json& sensorConfig, const std::string& name) : |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 84 | sensorIfaces(bus, objPath), |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 85 | bus(bus), name(name) |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 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 Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 100 | /** @brief Update sensor at regular intrval */ |
| 101 | void updateVirtualSensor(); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 102 | |
| 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 Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 111 | /** @brief name of sensor */ |
| 112 | std::string name; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 113 | /** @brief Expression string for virtual sensor value calculations */ |
| 114 | std::string exprStr; |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 115 | /** @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 Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 119 | |
| 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 Khemka | c62a554 | 2020-09-10 14:45:49 -0700 | [diff] [blame] | 125 | void setSensorThreshold(Threshold& sensorThreshold); |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 126 | /** @brief Check Sensor threshold and update alarm and log */ |
| 127 | void checkSensorThreshold(const double value); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | class 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 |