blob: 1b3a95bc3614f8d1e0f86e5a0862d5f0c9592906 [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001#pragma once
2
James Feist36b7d8e2018-10-05 15:39:01 -07003#include <limits>
Patrick Venture863b9242018-03-08 08:29:23 -08004#include <sdbusplus/bus.hpp>
5
6struct SensorProperties
7{
8 int64_t scale;
James Feistc065cf12018-07-05 10:23:11 -07009 double value;
Patrick Venture863b9242018-03-08 08:29:23 -080010 std::string unit;
11};
12
James Feist36b7d8e2018-10-05 15:39:01 -070013struct SensorThresholds
14{
15 double lowerThreshold = std::numeric_limits<double>::quiet_NaN();
16 double upperThreshold = std::numeric_limits<double>::quiet_NaN();
17};
18
Patrick Venture863b9242018-03-08 08:29:23 -080019const std::string sensorintf = "xyz.openbmc_project.Sensor.Value";
James Feist36b7d8e2018-10-05 15:39:01 -070020const std::string criticalThreshInf =
21 "xyz.openbmc_project.Sensor.Threshold.Critical";
Patrick Venture863b9242018-03-08 08:29:23 -080022const std::string propertiesintf = "org.freedesktop.DBus.Properties";
23
Patrick Venture0df7c0f2018-06-13 09:02:13 -070024class DbusHelperInterface
25{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070026 public:
27 virtual ~DbusHelperInterface() = default;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070028
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070029 /** @brief Get the service providing the interface for the path.
Patrick Venturef8cb4642018-10-30 12:02:53 -070030 *
31 * @warning Throws exception on dbus failure.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070032 */
Patrick Venture563a3562018-10-30 09:31:26 -070033 virtual std::string getService(sdbusplus::bus::bus& bus,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070034 const std::string& intf,
35 const std::string& path) = 0;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070036
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070037 /** @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 Venturef8cb4642018-10-30 12:02:53 -070043 *
44 * @warning Throws exception on dbus failure.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070045 */
Patrick Venture563a3562018-10-30 09:31:26 -070046 virtual void getProperties(sdbusplus::bus::bus& bus,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070047 const std::string& service,
48 const std::string& path,
49 struct SensorProperties* prop) = 0;
James Feist36b7d8e2018-10-05 15:39:01 -070050
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 Venture563a3562018-10-30 09:31:26 -070057 virtual bool thresholdsAsserted(sdbusplus::bus::bus& bus,
James Feist36b7d8e2018-10-05 15:39:01 -070058 const std::string& service,
59 const std::string& path) = 0;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070060};
61
62class DbusHelper : public DbusHelperInterface
63{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070064 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 Venture0df7c0f2018-06-13 09:02:13 -070071
Patrick Venture563a3562018-10-30 09:31:26 -070072 std::string getService(sdbusplus::bus::bus& bus, const std::string& intf,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070073 const std::string& path) override;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070074
Patrick Venture563a3562018-10-30 09:31:26 -070075 void getProperties(sdbusplus::bus::bus& bus, const std::string& service,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070076 const std::string& path,
77 struct SensorProperties* prop) override;
James Feist36b7d8e2018-10-05 15:39:01 -070078
Patrick Venture563a3562018-10-30 09:31:26 -070079 bool thresholdsAsserted(sdbusplus::bus::bus& bus,
James Feist36b7d8e2018-10-05 15:39:01 -070080 const std::string& service,
81 const std::string& path) override;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070082};
83
Patrick Venture7af157b2018-10-30 11:24:40 -070084std::string getSensorPath(const std::string& type, const std::string& id);
85std::string getMatch(const std::string& type, const std::string& id);
86bool validType(const std::string& type);
James Feist7136a5a2018-07-19 09:52:05 -070087
88struct VariantToFloatVisitor
89{
90 template <typename T>
91 std::enable_if_t<std::is_arithmetic<T>::value, float>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070092 operator()(const T& t) const
James Feist7136a5a2018-07-19 09:52:05 -070093 {
94 return static_cast<float>(t);
95 }
96
97 template <typename T>
98 std::enable_if_t<!std::is_arithmetic<T>::value, float>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070099 operator()(const T& t) const
James Feist7136a5a2018-07-19 09:52:05 -0700100 {
101 throw std::invalid_argument("Cannot translate type to float");
102 }
103};
James Feistc065cf12018-07-05 10:23:11 -0700104
105struct VariantToDoubleVisitor
106{
107 template <typename T>
108 std::enable_if_t<std::is_arithmetic<T>::value, double>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700109 operator()(const T& t) const
James Feistc065cf12018-07-05 10:23:11 -0700110 {
111 return static_cast<double>(t);
112 }
113
114 template <typename T>
115 std::enable_if_t<!std::is_arithmetic<T>::value, double>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700116 operator()(const T& t) const
James Feistc065cf12018-07-05 10:23:11 -0700117 {
118 throw std::invalid_argument("Cannot translate type to double");
119 }
120};