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 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 17 | public: |
| 18 | virtual ~DbusHelperInterface() = default; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 19 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 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; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 25 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 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; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | class DbusHelper : public DbusHelperInterface |
| 40 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 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; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 48 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 49 | std::string GetService(sdbusplus::bus::bus& bus, const std::string& intf, |
| 50 | const std::string& path) override; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 51 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 52 | void GetProperties(sdbusplus::bus::bus& bus, const std::string& service, |
| 53 | const std::string& path, |
| 54 | struct SensorProperties* prop) override; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | std::string GetSensorPath(const std::string& type, const std::string& id); |
| 58 | std::string GetMatch(const std::string& type, const std::string& id); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 59 | bool ValidType(const std::string& type); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 60 | |
| 61 | struct VariantToFloatVisitor |
| 62 | { |
| 63 | template <typename T> |
| 64 | std::enable_if_t<std::is_arithmetic<T>::value, float> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 65 | operator()(const T& t) const |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 66 | { |
| 67 | return static_cast<float>(t); |
| 68 | } |
| 69 | |
| 70 | template <typename T> |
| 71 | std::enable_if_t<!std::is_arithmetic<T>::value, float> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 72 | operator()(const T& t) const |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 73 | { |
| 74 | throw std::invalid_argument("Cannot translate type to float"); |
| 75 | } |
| 76 | }; |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 77 | |
| 78 | struct VariantToDoubleVisitor |
| 79 | { |
| 80 | template <typename T> |
| 81 | std::enable_if_t<std::is_arithmetic<T>::value, double> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 82 | operator()(const T& t) const |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 83 | { |
| 84 | return static_cast<double>(t); |
| 85 | } |
| 86 | |
| 87 | template <typename T> |
| 88 | std::enable_if_t<!std::is_arithmetic<T>::value, double> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 89 | operator()(const T& t) const |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 90 | { |
| 91 | throw std::invalid_argument("Cannot translate type to double"); |
| 92 | } |
| 93 | }; |