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, |
| 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 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; |
| 111 | /** @brief Expression string for virtual sensor value calculations */ |
| 112 | std::string exprStr; |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 113 | /** @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 Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 117 | |
| 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 Khemka | c62a554 | 2020-09-10 14:45:49 -0700 | [diff] [blame^] | 123 | void setSensorThreshold(Threshold& sensorThreshold); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | class 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 |