| Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame^] | 1 | #include <nlohmann/json.hpp> | 
|  | 2 | #include <sdbusplus/bus.hpp> | 
|  | 3 | #include <xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp> | 
|  | 4 | #include <xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp> | 
|  | 5 | #include <xyz/openbmc_project/Sensor/Value/server.hpp> | 
|  | 6 |  | 
|  | 7 | #include <map> | 
|  | 8 | #include <string> | 
|  | 9 |  | 
|  | 10 | namespace phosphor | 
|  | 11 | { | 
|  | 12 | namespace virtualSensor | 
|  | 13 | { | 
|  | 14 |  | 
|  | 15 | using Json = nlohmann::json; | 
|  | 16 | using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; | 
|  | 17 |  | 
|  | 18 | using CriticalInterface = | 
|  | 19 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical; | 
|  | 20 |  | 
|  | 21 | using WarningInterface = | 
|  | 22 | sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning; | 
|  | 23 |  | 
|  | 24 | using sensorIfaces = | 
|  | 25 | sdbusplus::server::object::object<ValueIface, CriticalInterface, | 
|  | 26 | WarningInterface>; | 
|  | 27 |  | 
|  | 28 | class SensorParam | 
|  | 29 | { | 
|  | 30 | public: | 
|  | 31 | SensorParam() = delete; | 
|  | 32 | virtual ~SensorParam() = default; | 
|  | 33 |  | 
|  | 34 | enum ParamType | 
|  | 35 | { | 
|  | 36 | constParam, | 
|  | 37 | dbusParam | 
|  | 38 | }; | 
|  | 39 |  | 
|  | 40 | /** @brief Constructs SensorParam (type = constParam) | 
|  | 41 | * | 
|  | 42 | * @param[in] value - Value of constant parameter | 
|  | 43 | */ | 
|  | 44 | explicit SensorParam(double value) : value(value), paramType(constParam) | 
|  | 45 | {} | 
|  | 46 |  | 
|  | 47 | /** @brief Get sensor value property from D-bus interface */ | 
|  | 48 | double getParamValue(); | 
|  | 49 |  | 
|  | 50 | private: | 
|  | 51 | double value; | 
|  | 52 | ParamType paramType; | 
|  | 53 | }; | 
|  | 54 |  | 
|  | 55 | class VirtualSensor : public sensorIfaces | 
|  | 56 | { | 
|  | 57 | public: | 
|  | 58 | VirtualSensor() = delete; | 
|  | 59 | virtual ~VirtualSensor() = default; | 
|  | 60 |  | 
|  | 61 | /** @brief Constructs VirtualSensor | 
|  | 62 | * | 
|  | 63 | * @param[in] bus          - Handle to system dbus | 
|  | 64 | * @param[in] objPath      - The Dbus path of sensor | 
|  | 65 | * @param[in] sensorConfig - Json object for sensor config | 
|  | 66 | */ | 
|  | 67 | VirtualSensor(sdbusplus::bus::bus& bus, const char* objPath, | 
|  | 68 | const Json& sensorConfig) : | 
|  | 69 | sensorIfaces(bus, objPath), | 
|  | 70 | bus(bus) | 
|  | 71 | { | 
|  | 72 | initVirtualSensor(sensorConfig); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | struct Threshold | 
|  | 76 | { | 
|  | 77 | double criticalHigh; | 
|  | 78 | double criticalLow; | 
|  | 79 | double warningHigh; | 
|  | 80 | double warningLow; | 
|  | 81 | }; | 
|  | 82 |  | 
|  | 83 | /** @brief Set sensor value */ | 
|  | 84 | void setSensorValue(double value); | 
|  | 85 |  | 
|  | 86 | /** @brief Map of list of parameters */ | 
|  | 87 | using ParamMap = | 
|  | 88 | std::unordered_map<std::string, std::unique_ptr<SensorParam>>; | 
|  | 89 | ParamMap paramMap; | 
|  | 90 |  | 
|  | 91 | private: | 
|  | 92 | /** @brief sdbusplus bus client connection. */ | 
|  | 93 | sdbusplus::bus::bus& bus; | 
|  | 94 | /** @brief Expression string for virtual sensor value calculations */ | 
|  | 95 | std::string exprStr; | 
|  | 96 | /** @brief Sensor Threshold config values */ | 
|  | 97 | struct Threshold sensorThreshold; | 
|  | 98 |  | 
|  | 99 | /** @brief Read config from json object and initialize sensor data | 
|  | 100 | * for each virtual sensor | 
|  | 101 | */ | 
|  | 102 | void initVirtualSensor(const Json& sensorConfig); | 
|  | 103 | /** @brief Set Sensor Threshold to D-bus at beginning */ | 
|  | 104 | void setSensorThreshold(); | 
|  | 105 | /** @brief Update sensor at regular intrval */ | 
|  | 106 | void updateVirtualSensor(); | 
|  | 107 | }; | 
|  | 108 |  | 
|  | 109 | class VirtualSensors | 
|  | 110 | { | 
|  | 111 | public: | 
|  | 112 | VirtualSensors() = delete; | 
|  | 113 | virtual ~VirtualSensors() = default; | 
|  | 114 |  | 
|  | 115 | /** @brief Constructs VirtualSensors | 
|  | 116 | * | 
|  | 117 | * @param[in] bus     - Handle to system dbus | 
|  | 118 | */ | 
|  | 119 | explicit VirtualSensors(sdbusplus::bus::bus& bus) : bus(bus) | 
|  | 120 | { | 
|  | 121 | createVirtualSensors(); | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | private: | 
|  | 125 | /** @brief sdbusplus bus client connection. */ | 
|  | 126 | sdbusplus::bus::bus& bus; | 
|  | 127 | /** @brief Parsing virtual sensor config JSON file  */ | 
|  | 128 | Json parseConfigFile(const std::string configFile); | 
|  | 129 |  | 
|  | 130 | /** @brief Map of the object VirtualSensor */ | 
|  | 131 | std::unordered_map<std::string, std::unique_ptr<VirtualSensor>> | 
|  | 132 | virtualSensorsMap; | 
|  | 133 |  | 
|  | 134 | /** @brief Create list of virtual sensors */ | 
|  | 135 | void createVirtualSensors(); | 
|  | 136 | }; | 
|  | 137 |  | 
|  | 138 | } // namespace virtualSensor | 
|  | 139 | } // namespace phosphor |