blob: e459524a7a543d11043ab850941f733b7ee4229d [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001#pragma once
2
3#include <sdbusplus/bus.hpp>
4
5struct SensorProperties
6{
7 int64_t scale;
James Feistc065cf12018-07-05 10:23:11 -07008 double value;
Patrick Venture863b9242018-03-08 08:29:23 -08009 std::string unit;
10};
11
Patrick Venture863b9242018-03-08 08:29:23 -080012const std::string sensorintf = "xyz.openbmc_project.Sensor.Value";
13const std::string propertiesintf = "org.freedesktop.DBus.Properties";
14
Patrick Venture0df7c0f2018-06-13 09:02:13 -070015class DbusHelperInterface
16{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070017 public:
18 virtual ~DbusHelperInterface() = default;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070019
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070020 /** @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 Venture0df7c0f2018-06-13 09:02:13 -070025
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070026 /** @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 Venture0df7c0f2018-06-13 09:02:13 -070037};
38
39class DbusHelper : public DbusHelperInterface
40{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070041 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 Venture0df7c0f2018-06-13 09:02:13 -070048
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070049 std::string GetService(sdbusplus::bus::bus& bus, const std::string& intf,
50 const std::string& path) override;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070051
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070052 void GetProperties(sdbusplus::bus::bus& bus, const std::string& service,
53 const std::string& path,
54 struct SensorProperties* prop) override;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070055};
56
57std::string GetSensorPath(const std::string& type, const std::string& id);
58std::string GetMatch(const std::string& type, const std::string& id);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070059bool ValidType(const std::string& type);
James Feist7136a5a2018-07-19 09:52:05 -070060
61struct VariantToFloatVisitor
62{
63 template <typename T>
64 std::enable_if_t<std::is_arithmetic<T>::value, float>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070065 operator()(const T& t) const
James Feist7136a5a2018-07-19 09:52:05 -070066 {
67 return static_cast<float>(t);
68 }
69
70 template <typename T>
71 std::enable_if_t<!std::is_arithmetic<T>::value, float>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070072 operator()(const T& t) const
James Feist7136a5a2018-07-19 09:52:05 -070073 {
74 throw std::invalid_argument("Cannot translate type to float");
75 }
76};
James Feistc065cf12018-07-05 10:23:11 -070077
78struct VariantToDoubleVisitor
79{
80 template <typename T>
81 std::enable_if_t<std::is_arithmetic<T>::value, double>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070082 operator()(const T& t) const
James Feistc065cf12018-07-05 10:23:11 -070083 {
84 return static_cast<double>(t);
85 }
86
87 template <typename T>
88 std::enable_if_t<!std::is_arithmetic<T>::value, double>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070089 operator()(const T& t) const
James Feistc065cf12018-07-05 10:23:11 -070090 {
91 throw std::invalid_argument("Cannot translate type to double");
92 }
93};