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