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. |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 30 | * |
| 31 | * @warning Throws exception on dbus failure. |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 32 | */ |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 33 | virtual std::string getService(sdbusplus::bus::bus& bus, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 34 | const std::string& intf, |
| 35 | const std::string& path) = 0; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 36 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 37 | /** @brief Get all Sensor.Value properties for a service and path. |
| 38 | * |
| 39 | * @param[in] bus - A bus to use for the call. |
| 40 | * @param[in] service - The service providing the interface. |
| 41 | * @param[in] path - The dbus path. |
| 42 | * @param[out] prop - A pointer to a properties struct to fill out. |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 43 | * |
| 44 | * @warning Throws exception on dbus failure. |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 45 | */ |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 46 | virtual void getProperties(sdbusplus::bus::bus& bus, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 47 | const std::string& service, |
| 48 | const std::string& path, |
| 49 | struct SensorProperties* prop) = 0; |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 50 | |
| 51 | /** @brief Get Critical Threshold current assert status |
| 52 | * |
| 53 | * @param[in] bus - A bus to use for the call. |
| 54 | * @param[in] service - The service providing the interface. |
| 55 | * @param[in] path - The dbus path. |
| 56 | */ |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 57 | virtual bool thresholdsAsserted(sdbusplus::bus::bus& bus, |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 58 | const std::string& service, |
| 59 | const std::string& path) = 0; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | class DbusHelper : public DbusHelperInterface |
| 63 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 64 | public: |
| 65 | DbusHelper() = default; |
| 66 | ~DbusHelper() = default; |
| 67 | DbusHelper(const DbusHelper&) = default; |
| 68 | DbusHelper& operator=(const DbusHelper&) = default; |
| 69 | DbusHelper(DbusHelper&&) = default; |
| 70 | DbusHelper& operator=(DbusHelper&&) = default; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 71 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 72 | std::string getService(sdbusplus::bus::bus& bus, const std::string& intf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 73 | const std::string& path) override; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 74 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 75 | void getProperties(sdbusplus::bus::bus& bus, const std::string& service, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 76 | const std::string& path, |
| 77 | struct SensorProperties* prop) override; |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 78 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 79 | bool thresholdsAsserted(sdbusplus::bus::bus& bus, |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 80 | const std::string& service, |
| 81 | const std::string& path) override; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 84 | std::string getSensorPath(const std::string& type, const std::string& id); |
| 85 | std::string getMatch(const std::string& type, const std::string& id); |
| 86 | bool validType(const std::string& type); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 87 | |
| 88 | struct VariantToFloatVisitor |
| 89 | { |
| 90 | template <typename T> |
| 91 | std::enable_if_t<std::is_arithmetic<T>::value, float> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 92 | operator()(const T& t) const |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 93 | { |
| 94 | return static_cast<float>(t); |
| 95 | } |
| 96 | |
| 97 | template <typename T> |
| 98 | std::enable_if_t<!std::is_arithmetic<T>::value, float> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 99 | operator()(const T& t) const |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 100 | { |
| 101 | throw std::invalid_argument("Cannot translate type to float"); |
| 102 | } |
| 103 | }; |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 104 | |
| 105 | struct VariantToDoubleVisitor |
| 106 | { |
| 107 | template <typename T> |
| 108 | std::enable_if_t<std::is_arithmetic<T>::value, double> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 109 | operator()(const T& t) const |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 110 | { |
| 111 | return static_cast<double>(t); |
| 112 | } |
| 113 | |
| 114 | template <typename T> |
| 115 | std::enable_if_t<!std::is_arithmetic<T>::value, double> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 116 | operator()(const T& t) const |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 117 | { |
| 118 | throw std::invalid_argument("Cannot translate type to double"); |
| 119 | } |
| 120 | }; |