blob: 69a811202f3ae3cc8b80d2147eb56e3f9f258bec [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;
James Feist75eb7692019-02-25 12:50:02 -080010 double min;
11 double max;
Patrick Venture863b9242018-03-08 08:29:23 -080012 std::string unit;
13};
14
James Feist36b7d8e2018-10-05 15:39:01 -070015struct SensorThresholds
16{
17 double lowerThreshold = std::numeric_limits<double>::quiet_NaN();
18 double upperThreshold = std::numeric_limits<double>::quiet_NaN();
19};
20
Patrick Venture863b9242018-03-08 08:29:23 -080021const std::string sensorintf = "xyz.openbmc_project.Sensor.Value";
James Feist36b7d8e2018-10-05 15:39:01 -070022const std::string criticalThreshInf =
23 "xyz.openbmc_project.Sensor.Threshold.Critical";
Patrick Venture863b9242018-03-08 08:29:23 -080024const std::string propertiesintf = "org.freedesktop.DBus.Properties";
25
Patrick Venture0df7c0f2018-06-13 09:02:13 -070026class DbusHelperInterface
27{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070028 public:
29 virtual ~DbusHelperInterface() = default;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070030
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031 /** @brief Get the service providing the interface for the path.
Patrick Venturef8cb4642018-10-30 12:02:53 -070032 *
33 * @warning Throws exception on dbus failure.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070034 */
Patrick Venture563a3562018-10-30 09:31:26 -070035 virtual std::string getService(sdbusplus::bus::bus& bus,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070036 const std::string& intf,
37 const std::string& path) = 0;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070038
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070039 /** @brief Get all Sensor.Value properties for a service and path.
40 *
41 * @param[in] bus - A bus to use for the call.
42 * @param[in] service - The service providing the interface.
43 * @param[in] path - The dbus path.
44 * @param[out] prop - A pointer to a properties struct to fill out.
Patrick Venturef8cb4642018-10-30 12:02:53 -070045 *
46 * @warning Throws exception on dbus failure.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070047 */
Patrick Venture563a3562018-10-30 09:31:26 -070048 virtual void getProperties(sdbusplus::bus::bus& bus,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070049 const std::string& service,
50 const std::string& path,
51 struct SensorProperties* prop) = 0;
James Feist36b7d8e2018-10-05 15:39:01 -070052
53 /** @brief Get Critical Threshold current assert status
54 *
55 * @param[in] bus - A bus to use for the call.
56 * @param[in] service - The service providing the interface.
57 * @param[in] path - The dbus path.
58 */
Patrick Venture563a3562018-10-30 09:31:26 -070059 virtual bool thresholdsAsserted(sdbusplus::bus::bus& bus,
James Feist36b7d8e2018-10-05 15:39:01 -070060 const std::string& service,
61 const std::string& path) = 0;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070062};
63
64class DbusHelper : public DbusHelperInterface
65{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070066 public:
67 DbusHelper() = default;
68 ~DbusHelper() = default;
69 DbusHelper(const DbusHelper&) = default;
70 DbusHelper& operator=(const DbusHelper&) = default;
71 DbusHelper(DbusHelper&&) = default;
72 DbusHelper& operator=(DbusHelper&&) = default;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070073
Patrick Venture563a3562018-10-30 09:31:26 -070074 std::string getService(sdbusplus::bus::bus& bus, const std::string& intf,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070075 const std::string& path) override;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070076
Patrick Venture563a3562018-10-30 09:31:26 -070077 void getProperties(sdbusplus::bus::bus& bus, const std::string& service,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070078 const std::string& path,
79 struct SensorProperties* prop) override;
James Feist36b7d8e2018-10-05 15:39:01 -070080
Patrick Venture563a3562018-10-30 09:31:26 -070081 bool thresholdsAsserted(sdbusplus::bus::bus& bus,
James Feist36b7d8e2018-10-05 15:39:01 -070082 const std::string& service,
83 const std::string& path) override;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070084};
85
Patrick Venture7af157b2018-10-30 11:24:40 -070086std::string getSensorPath(const std::string& type, const std::string& id);
87std::string getMatch(const std::string& type, const std::string& id);
James Feist75eb7692019-02-25 12:50:02 -080088void scaleSensorReading(const double min, const double max, double& value);
Patrick Venture7af157b2018-10-30 11:24:40 -070089bool validType(const std::string& type);
James Feist7136a5a2018-07-19 09:52:05 -070090
James Feistc065cf12018-07-05 10:23:11 -070091struct VariantToDoubleVisitor
92{
93 template <typename T>
94 std::enable_if_t<std::is_arithmetic<T>::value, double>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070095 operator()(const T& t) const
James Feistc065cf12018-07-05 10:23:11 -070096 {
97 return static_cast<double>(t);
98 }
99
100 template <typename T>
101 std::enable_if_t<!std::is_arithmetic<T>::value, double>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700102 operator()(const T& t) const
James Feistc065cf12018-07-05 10:23:11 -0700103 {
104 throw std::invalid_argument("Cannot translate type to double");
105 }
106};