blob: 72e70e36b209c1fc4d8ecd47565ba74c94a0ffec [file] [log] [blame]
George Liu1c737af2020-10-16 09:07:02 +08001#pragma once
2#include <sdbusplus/server.hpp>
3
Patrick Williamsf2044032022-03-17 05:12:30 -05004#include <unordered_map>
George Liu1c737af2020-10-16 09:07:02 +08005#include <vector>
6namespace phosphor
7{
8namespace led
9{
10namespace utils
11{
George Liu1f0b7152023-07-18 09:24:34 +080012static constexpr auto mapperBusName = "xyz.openbmc_project.ObjectMapper";
13static constexpr auto mapperObjPath = "/xyz/openbmc_project/object_mapper";
14static constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
15static constexpr auto proIntf = "org.freedesktop.DBus.Properties";
George Liu1c737af2020-10-16 09:07:02 +080016
17// The value of the property(type: variant, contains some basic types)
George Liu4c5f5332020-10-10 17:04:28 +080018// Eg: uint8_t : dutyOn, uint16_t : Period, std::string : Name,
19// std::vector<std::string> : endpoints, bool : Functional
20using PropertyValue = std::variant<uint8_t, uint16_t, std::string,
21 std::vector<std::string>, bool>;
George Liu1c737af2020-10-16 09:07:02 +080022
George Liub6151622020-11-23 18:16:18 +080023// The name of the property
24using DbusProperty = std::string;
25
26// The Map to constructs all properties values of the interface
Patrick Williamsf2044032022-03-17 05:12:30 -050027using PropertyMap = std::unordered_map<DbusProperty, PropertyValue>;
George Liub6151622020-11-23 18:16:18 +080028
George Liu1c737af2020-10-16 09:07:02 +080029/**
30 * @class DBusHandler
31 *
32 * Wrapper class to handle the D-Bus calls
33 *
34 * This class contains the APIs to handle the D-Bus calls.
35 */
36class DBusHandler
37{
38 public:
39 /** @brief Get the bus connection. */
40 static auto& getBus()
41 {
42 static auto bus = sdbusplus::bus::new_default();
43 return bus;
44 }
45
46 /**
47 * @brief Get service name by the path and interface of the DBus.
48 *
49 * @param[in] path - D-Bus object path
50 * @param[in] interface - D-Bus Interface
51 *
52 * @return std::string - the D-Bus service name
53 *
54 */
55 const std::string getService(const std::string& path,
56 const std::string& interface) const;
57
George Liub6151622020-11-23 18:16:18 +080058 /** @brief Get All properties
59 *
60 * @param[in] objectPath - D-Bus object path
61 * @param[in] interface - D-Bus interface
62 *
63 * @return The Map to constructs all properties values
64 *
Patrick Williams3e073ba2022-07-22 19:26:52 -050065 * @throw sdbusplus::exception_t when it fails
George Liub6151622020-11-23 18:16:18 +080066 */
67 const PropertyMap getAllProperties(const std::string& objectPath,
68 const std::string& interface) const;
69
George Liu4c5f5332020-10-10 17:04:28 +080070 /** @brief Get property(type: variant)
71 *
72 * @param[in] objectPath - D-Bus object path
73 * @param[in] interface - D-Bus interface
74 * @param[in] propertyName - D-Bus property name
75 *
76 * @return The value of the property(type: variant)
77 *
Patrick Williams3e073ba2022-07-22 19:26:52 -050078 * @throw sdbusplus::exception_t when it fails
George Liu4c5f5332020-10-10 17:04:28 +080079 */
80 const PropertyValue getProperty(const std::string& objectPath,
81 const std::string& interface,
82 const std::string& propertyName) const;
83
George Liu1c737af2020-10-16 09:07:02 +080084 /** @brief Set D-Bus property
85 *
86 * @param[in] objectPath - D-Bus object path
87 * @param[in] interface - D-Bus interface
88 * @param[in] propertyName - D-Bus property name
89 * @param[in] value - The value to be set
90 *
Patrick Williams3e073ba2022-07-22 19:26:52 -050091 * @throw sdbusplus::exception_t when it fails
George Liu1c737af2020-10-16 09:07:02 +080092 */
93 void setProperty(const std::string& objectPath,
94 const std::string& interface,
95 const std::string& propertyName,
96 const PropertyValue& value) const;
George Liu616a0712021-02-18 10:50:24 +080097
98 /** @brief Get sub tree paths by the path and interface of the DBus.
99 *
100 * @param[in] objectPath - D-Bus object path
101 * @param[in] interface - D-Bus object interface
102 *
103 * @return std::vector<std::string> vector of subtree paths
104 */
105 const std::vector<std::string>
106 getSubTreePaths(const std::string& objectPath,
107 const std::string& interface);
George Liu1c737af2020-10-16 09:07:02 +0800108};
109
110} // namespace utils
111} // namespace led
112} // namespace phosphor