blob: ed7a411c5e3f3a3ec870bb1ac725533bdc8c0794 [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.
30 */
31 virtual std::string GetService(sdbusplus::bus::bus& bus,
32 const std::string& intf,
33 const std::string& path) = 0;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070034
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070035 /** @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 Feist36b7d8e2018-10-05 15:39:01 -070046
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 Venture0df7c0f2018-06-13 09:02:13 -070056};
57
58class DbusHelper : public DbusHelperInterface
59{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070060 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 Venture0df7c0f2018-06-13 09:02:13 -070067
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070068 std::string GetService(sdbusplus::bus::bus& bus, const std::string& intf,
69 const std::string& path) override;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070070
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070071 void GetProperties(sdbusplus::bus::bus& bus, const std::string& service,
72 const std::string& path,
73 struct SensorProperties* prop) override;
James Feist36b7d8e2018-10-05 15:39:01 -070074
75 bool ThresholdsAsserted(sdbusplus::bus::bus& bus,
76 const std::string& service,
77 const std::string& path) override;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070078};
79
80std::string GetSensorPath(const std::string& type, const std::string& id);
81std::string GetMatch(const std::string& type, const std::string& id);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070082bool ValidType(const std::string& type);
James Feist7136a5a2018-07-19 09:52:05 -070083
84struct VariantToFloatVisitor
85{
86 template <typename T>
87 std::enable_if_t<std::is_arithmetic<T>::value, float>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070088 operator()(const T& t) const
James Feist7136a5a2018-07-19 09:52:05 -070089 {
90 return static_cast<float>(t);
91 }
92
93 template <typename T>
94 std::enable_if_t<!std::is_arithmetic<T>::value, float>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070095 operator()(const T& t) const
James Feist7136a5a2018-07-19 09:52:05 -070096 {
97 throw std::invalid_argument("Cannot translate type to float");
98 }
99};
James Feistc065cf12018-07-05 10:23:11 -0700100
101struct VariantToDoubleVisitor
102{
103 template <typename T>
104 std::enable_if_t<std::is_arithmetic<T>::value, double>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700105 operator()(const T& t) const
James Feistc065cf12018-07-05 10:23:11 -0700106 {
107 return static_cast<double>(t);
108 }
109
110 template <typename T>
111 std::enable_if_t<!std::is_arithmetic<T>::value, double>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700112 operator()(const T& t) const
James Feistc065cf12018-07-05 10:23:11 -0700113 {
114 throw std::invalid_argument("Cannot translate type to double");
115 }
116};