Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 3 | #include <limits> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 4 | #include <sdbusplus/bus.hpp> |
| 5 | |
| 6 | struct SensorProperties |
| 7 | { |
| 8 | int64_t scale; |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 9 | double value; |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 10 | double min; |
| 11 | double max; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 12 | std::string unit; |
| 13 | }; |
| 14 | |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 15 | struct SensorThresholds |
| 16 | { |
| 17 | double lowerThreshold = std::numeric_limits<double>::quiet_NaN(); |
| 18 | double upperThreshold = std::numeric_limits<double>::quiet_NaN(); |
| 19 | }; |
| 20 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 21 | const std::string sensorintf = "xyz.openbmc_project.Sensor.Value"; |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 22 | const std::string criticalThreshInf = |
| 23 | "xyz.openbmc_project.Sensor.Threshold.Critical"; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 24 | const std::string propertiesintf = "org.freedesktop.DBus.Properties"; |
| 25 | |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 26 | class DbusHelperInterface |
| 27 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 28 | public: |
| 29 | virtual ~DbusHelperInterface() = default; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 30 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 31 | /** @brief Get the service providing the interface for the path. |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 32 | * |
| 33 | * @warning Throws exception on dbus failure. |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 34 | */ |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 35 | virtual std::string getService(sdbusplus::bus::bus& bus, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 36 | const std::string& intf, |
| 37 | const std::string& path) = 0; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 38 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 39 | /** @brief Get all Sensor.Value properties for a service and path. |
| 40 | * |
| 41 | * @param[in] bus - A bus to use for the call. |
| 42 | * @param[in] service - The service providing the interface. |
| 43 | * @param[in] path - The dbus path. |
| 44 | * @param[out] prop - A pointer to a properties struct to fill out. |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 45 | * |
| 46 | * @warning Throws exception on dbus failure. |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 47 | */ |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 48 | virtual void getProperties(sdbusplus::bus::bus& bus, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 49 | const std::string& service, |
| 50 | const std::string& path, |
| 51 | struct SensorProperties* prop) = 0; |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 52 | |
| 53 | /** @brief Get Critical Threshold current assert status |
| 54 | * |
| 55 | * @param[in] bus - A bus to use for the call. |
| 56 | * @param[in] service - The service providing the interface. |
| 57 | * @param[in] path - The dbus path. |
| 58 | */ |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 59 | virtual bool thresholdsAsserted(sdbusplus::bus::bus& bus, |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 60 | const std::string& service, |
| 61 | const std::string& path) = 0; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | class DbusHelper : public DbusHelperInterface |
| 65 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 66 | public: |
| 67 | DbusHelper() = default; |
| 68 | ~DbusHelper() = default; |
| 69 | DbusHelper(const DbusHelper&) = default; |
| 70 | DbusHelper& operator=(const DbusHelper&) = default; |
| 71 | DbusHelper(DbusHelper&&) = default; |
| 72 | DbusHelper& operator=(DbusHelper&&) = default; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 73 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 74 | std::string getService(sdbusplus::bus::bus& bus, const std::string& intf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 75 | const std::string& path) override; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 76 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 77 | void getProperties(sdbusplus::bus::bus& bus, const std::string& service, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 78 | const std::string& path, |
| 79 | struct SensorProperties* prop) override; |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 80 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 81 | bool thresholdsAsserted(sdbusplus::bus::bus& bus, |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 82 | const std::string& service, |
| 83 | const std::string& path) override; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 86 | std::string getSensorPath(const std::string& type, const std::string& id); |
| 87 | std::string getMatch(const std::string& type, const std::string& id); |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 88 | void scaleSensorReading(const double min, const double max, double& value); |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 89 | bool validType(const std::string& type); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 90 | |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 91 | struct VariantToDoubleVisitor |
| 92 | { |
| 93 | template <typename T> |
| 94 | std::enable_if_t<std::is_arithmetic<T>::value, double> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 95 | operator()(const T& t) const |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 96 | { |
| 97 | return static_cast<double>(t); |
| 98 | } |
| 99 | |
| 100 | template <typename T> |
| 101 | std::enable_if_t<!std::is_arithmetic<T>::value, double> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 102 | operator()(const T& t) const |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 103 | { |
| 104 | throw std::invalid_argument("Cannot translate type to double"); |
| 105 | } |
| 106 | }; |