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; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 10 | std::string unit; |
| 11 | }; |
| 12 | |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 13 | struct SensorThresholds |
| 14 | { |
| 15 | double lowerThreshold = std::numeric_limits<double>::quiet_NaN(); |
| 16 | double upperThreshold = std::numeric_limits<double>::quiet_NaN(); |
| 17 | }; |
| 18 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 19 | const std::string sensorintf = "xyz.openbmc_project.Sensor.Value"; |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 20 | const std::string criticalThreshInf = |
| 21 | "xyz.openbmc_project.Sensor.Threshold.Critical"; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 22 | const std::string propertiesintf = "org.freedesktop.DBus.Properties"; |
| 23 | |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 24 | class DbusHelperInterface |
| 25 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 26 | public: |
| 27 | virtual ~DbusHelperInterface() = default; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 28 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 29 | /** @brief Get the service providing the interface for the path. |
| 30 | */ |
| 31 | virtual std::string GetService(sdbusplus::bus::bus& bus, |
| 32 | const std::string& intf, |
| 33 | const std::string& path) = 0; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 34 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 35 | /** @brief Get all Sensor.Value properties for a service and path. |
| 36 | * |
| 37 | * @param[in] bus - A bus to use for the call. |
| 38 | * @param[in] service - The service providing the interface. |
| 39 | * @param[in] path - The dbus path. |
| 40 | * @param[out] prop - A pointer to a properties struct to fill out. |
| 41 | */ |
| 42 | virtual void GetProperties(sdbusplus::bus::bus& bus, |
| 43 | const std::string& service, |
| 44 | const std::string& path, |
| 45 | struct SensorProperties* prop) = 0; |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 46 | |
| 47 | /** @brief Get Critical Threshold current assert status |
| 48 | * |
| 49 | * @param[in] bus - A bus to use for the call. |
| 50 | * @param[in] service - The service providing the interface. |
| 51 | * @param[in] path - The dbus path. |
| 52 | */ |
| 53 | virtual bool ThresholdsAsserted(sdbusplus::bus::bus& bus, |
| 54 | const std::string& service, |
| 55 | const std::string& path) = 0; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | class DbusHelper : public DbusHelperInterface |
| 59 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 60 | public: |
| 61 | DbusHelper() = default; |
| 62 | ~DbusHelper() = default; |
| 63 | DbusHelper(const DbusHelper&) = default; |
| 64 | DbusHelper& operator=(const DbusHelper&) = default; |
| 65 | DbusHelper(DbusHelper&&) = default; |
| 66 | DbusHelper& operator=(DbusHelper&&) = default; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 67 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 68 | std::string GetService(sdbusplus::bus::bus& bus, const std::string& intf, |
| 69 | const std::string& path) override; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 70 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 71 | void GetProperties(sdbusplus::bus::bus& bus, const std::string& service, |
| 72 | const std::string& path, |
| 73 | struct SensorProperties* prop) override; |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 74 | |
| 75 | bool ThresholdsAsserted(sdbusplus::bus::bus& bus, |
| 76 | const std::string& service, |
| 77 | const std::string& path) override; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | std::string GetSensorPath(const std::string& type, const std::string& id); |
| 81 | std::string GetMatch(const std::string& type, const std::string& id); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 82 | bool ValidType(const std::string& type); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 83 | |
| 84 | struct VariantToFloatVisitor |
| 85 | { |
| 86 | template <typename T> |
| 87 | std::enable_if_t<std::is_arithmetic<T>::value, float> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 88 | operator()(const T& t) const |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 89 | { |
| 90 | return static_cast<float>(t); |
| 91 | } |
| 92 | |
| 93 | template <typename T> |
| 94 | std::enable_if_t<!std::is_arithmetic<T>::value, float> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 95 | operator()(const T& t) const |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 96 | { |
| 97 | throw std::invalid_argument("Cannot translate type to float"); |
| 98 | } |
| 99 | }; |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 100 | |
| 101 | struct VariantToDoubleVisitor |
| 102 | { |
| 103 | template <typename T> |
| 104 | std::enable_if_t<std::is_arithmetic<T>::value, double> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 105 | operator()(const T& t) const |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 106 | { |
| 107 | return static_cast<double>(t); |
| 108 | } |
| 109 | |
| 110 | template <typename T> |
| 111 | std::enable_if_t<!std::is_arithmetic<T>::value, double> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 112 | operator()(const T& t) const |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 113 | { |
| 114 | throw std::invalid_argument("Cannot translate type to double"); |
| 115 | } |
| 116 | }; |