| 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" | 
| Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 3 | #include "thresholds.hpp" | 
|  | 4 |  | 
|  | 5 | #include <fmt/format.h> | 
| Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 6 |  | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 7 | #include <nlohmann/json.hpp> | 
|  | 8 | #include <sdbusplus/bus.hpp> | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 9 | #include <xyz/openbmc_project/Sensor/Value/server.hpp> | 
|  | 10 |  | 
|  | 11 | #include <map> | 
|  | 12 | #include <string> | 
|  | 13 |  | 
|  | 14 | namespace phosphor | 
|  | 15 | { | 
|  | 16 | namespace virtualSensor | 
|  | 17 | { | 
|  | 18 |  | 
|  | 19 | using Json = nlohmann::json; | 
| Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 20 |  | 
|  | 21 | template <typename... T> | 
|  | 22 | using ServerObject = typename sdbusplus::server::object::object<T...>; | 
|  | 23 |  | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 24 | using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; | 
| Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 25 | using ValueObject = ServerObject<ValueIface>; | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 26 |  | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 27 | class SensorParam | 
|  | 28 | { | 
|  | 29 | public: | 
|  | 30 | SensorParam() = delete; | 
|  | 31 | virtual ~SensorParam() = default; | 
|  | 32 |  | 
|  | 33 | enum ParamType | 
|  | 34 | { | 
|  | 35 | constParam, | 
|  | 36 | dbusParam | 
|  | 37 | }; | 
|  | 38 |  | 
|  | 39 | /** @brief Constructs SensorParam (type = constParam) | 
|  | 40 | * | 
|  | 41 | * @param[in] value - Value of constant parameter | 
|  | 42 | */ | 
|  | 43 | explicit SensorParam(double value) : value(value), paramType(constParam) | 
|  | 44 | {} | 
|  | 45 |  | 
| Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 46 | /** @brief Constructs SensorParam (type = dbusParam) | 
|  | 47 | * | 
|  | 48 | * @param[in] bus     - Handle to system dbus | 
|  | 49 | * @param[in] path    - The Dbus path of sensor | 
| Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 50 | * @param[in] ctx     - sensor context for update | 
| Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 51 | */ | 
| Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 52 | SensorParam(sdbusplus::bus::bus& bus, std::string path, void* ctx) : | 
|  | 53 | dbusSensor(std::make_unique<DbusSensor>(bus, path, ctx)), | 
| Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 54 | paramType(dbusParam) | 
|  | 55 | {} | 
|  | 56 |  | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 57 | /** @brief Get sensor value property from D-bus interface */ | 
|  | 58 | double getParamValue(); | 
|  | 59 |  | 
|  | 60 | private: | 
| Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 61 | std::unique_ptr<DbusSensor> dbusSensor = nullptr; | 
|  | 62 | double value = 0; | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 63 | ParamType paramType; | 
|  | 64 | }; | 
|  | 65 |  | 
| Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 66 | class VirtualSensor : public ValueObject | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 67 | { | 
|  | 68 | public: | 
|  | 69 | VirtualSensor() = delete; | 
|  | 70 | virtual ~VirtualSensor() = default; | 
|  | 71 |  | 
|  | 72 | /** @brief Constructs VirtualSensor | 
|  | 73 | * | 
|  | 74 | * @param[in] bus          - Handle to system dbus | 
|  | 75 | * @param[in] objPath      - The Dbus path of sensor | 
|  | 76 | * @param[in] sensorConfig - Json object for sensor config | 
|  | 77 | */ | 
|  | 78 | VirtualSensor(sdbusplus::bus::bus& bus, const char* objPath, | 
| Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 79 | const Json& sensorConfig, const std::string& name) : | 
| Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 80 | ValueObject(bus, objPath), | 
| Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 81 | bus(bus), name(name) | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 82 | { | 
| Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 83 | initVirtualSensor(sensorConfig, objPath); | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 86 | /** @brief Set sensor value */ | 
|  | 87 | void setSensorValue(double value); | 
| Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 88 | /** @brief Update sensor at regular intrval */ | 
|  | 89 | void updateVirtualSensor(); | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 90 |  | 
|  | 91 | /** @brief Map of list of parameters */ | 
|  | 92 | using ParamMap = | 
|  | 93 | std::unordered_map<std::string, std::unique_ptr<SensorParam>>; | 
|  | 94 | ParamMap paramMap; | 
|  | 95 |  | 
|  | 96 | private: | 
|  | 97 | /** @brief sdbusplus bus client connection. */ | 
|  | 98 | sdbusplus::bus::bus& bus; | 
| Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 99 | /** @brief name of sensor */ | 
|  | 100 | std::string name; | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 101 | /** @brief Expression string for virtual sensor value calculations */ | 
|  | 102 | std::string exprStr; | 
| Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 103 | /** @brief symbol table from exprtk */ | 
|  | 104 | exprtk::symbol_table<double> symbols{}; | 
|  | 105 | /** @brief expression from exprtk to calculate sensor value */ | 
|  | 106 | exprtk::expression<double> expression{}; | 
| Matt Spinler | 9f1ef4f | 2020-11-09 15:59:11 -0600 | [diff] [blame] | 107 | /** @brief The vecops package so the expression can use vectors */ | 
|  | 108 | exprtk::rtl::vecops::package<double> vecopsPackage; | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 109 |  | 
| Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 110 | /** @brief The critical threshold interface object */ | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 111 | std::unique_ptr<Threshold<CriticalObject>> criticalIface; | 
| Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 112 | /** @brief The warning threshold interface object */ | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 113 | std::unique_ptr<Threshold<WarningObject>> warningIface; | 
| Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 114 | /** @brief The soft shutdown threshold interface object */ | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 115 | std::unique_ptr<Threshold<SoftShutdownObject>> softShutdownIface; | 
| Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 116 | /** @brief The hard shutdown threshold interface object */ | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 117 | std::unique_ptr<Threshold<HardShutdownObject>> hardShutdownIface; | 
| Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 118 |  | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 119 | /** @brief Read config from json object and initialize sensor data | 
|  | 120 | * for each virtual sensor | 
|  | 121 | */ | 
| Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 122 | void initVirtualSensor(const Json& sensorConfig, | 
|  | 123 | const std::string& objPath); | 
|  | 124 |  | 
| Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 125 | /** @brief Check Sensor threshold and update alarm and log */ | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 126 | template <typename V, typename T> | 
|  | 127 | void checkThresholds(V value, T& threshold) | 
| Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 128 | { | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 129 | if (!threshold) | 
|  | 130 | return; | 
| Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 131 |  | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 132 | static constexpr auto tname = T::element_type::name; | 
|  | 133 |  | 
|  | 134 | auto alarmHigh = threshold->alarmHigh(); | 
| Matt Spinler | a4fe665 | 2021-01-28 14:02:59 -0600 | [diff] [blame^] | 135 | if ((!alarmHigh && value >= threshold->high()) || | 
|  | 136 | (alarmHigh && value < threshold->high())) | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 137 | { | 
|  | 138 | if (!alarmHigh) | 
| Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 139 | { | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 140 | constexpr auto msg = | 
|  | 141 | "ASSERT: {} has exceeded the {} high threshold"; | 
|  | 142 | log<level::ERR>(fmt::format(msg, name, tname).c_str()); | 
| Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 143 | } | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 144 | else | 
| Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 145 | { | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 146 | constexpr auto msg = | 
|  | 147 | "DEASSERT: {} is under the {} high threshold"; | 
|  | 148 | log<level::INFO>(fmt::format(msg, name, tname).c_str()); | 
| Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 149 | } | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 150 | threshold->alarmHigh(!alarmHigh); | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | auto alarmLow = threshold->alarmLow(); | 
| Matt Spinler | a4fe665 | 2021-01-28 14:02:59 -0600 | [diff] [blame^] | 154 | if ((!alarmLow && value <= threshold->low()) || | 
|  | 155 | (alarmLow && value > threshold->low())) | 
| Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 156 | { | 
|  | 157 | if (!alarmLow) | 
|  | 158 | { | 
|  | 159 | constexpr auto msg = "ASSERT: {} is under the {} low threshold"; | 
|  | 160 | log<level::ERR>(fmt::format(msg, name, tname).c_str()); | 
|  | 161 | } | 
|  | 162 | else | 
|  | 163 | { | 
|  | 164 | constexpr auto msg = | 
|  | 165 | "DEASSERT: {} is above the {} low threshold"; | 
|  | 166 | log<level::INFO>(fmt::format(msg, name, tname).c_str()); | 
|  | 167 | } | 
|  | 168 | threshold->alarmLow(!alarmLow); | 
| Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 169 | } | 
|  | 170 | } | 
| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 171 | }; | 
|  | 172 |  | 
|  | 173 | class VirtualSensors | 
|  | 174 | { | 
|  | 175 | public: | 
|  | 176 | VirtualSensors() = delete; | 
|  | 177 | virtual ~VirtualSensors() = default; | 
|  | 178 |  | 
|  | 179 | /** @brief Constructs VirtualSensors | 
|  | 180 | * | 
|  | 181 | * @param[in] bus     - Handle to system dbus | 
|  | 182 | */ | 
|  | 183 | explicit VirtualSensors(sdbusplus::bus::bus& bus) : bus(bus) | 
|  | 184 | { | 
|  | 185 | createVirtualSensors(); | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | private: | 
|  | 189 | /** @brief sdbusplus bus client connection. */ | 
|  | 190 | sdbusplus::bus::bus& bus; | 
|  | 191 | /** @brief Parsing virtual sensor config JSON file  */ | 
|  | 192 | Json parseConfigFile(const std::string configFile); | 
|  | 193 |  | 
|  | 194 | /** @brief Map of the object VirtualSensor */ | 
|  | 195 | std::unordered_map<std::string, std::unique_ptr<VirtualSensor>> | 
|  | 196 | virtualSensorsMap; | 
|  | 197 |  | 
|  | 198 | /** @brief Create list of virtual sensors */ | 
|  | 199 | void createVirtualSensors(); | 
|  | 200 | }; | 
|  | 201 |  | 
|  | 202 | } // namespace virtualSensor | 
|  | 203 | } // namespace phosphor |