blob: ef56f5f16977f82cf53ade82108cb3d4a54a94aa [file] [log] [blame]
George Liu1c737af2020-10-16 09:07:02 +08001#pragma once
2#include <sdbusplus/server.hpp>
3
4#include <map>
5#include <vector>
6namespace phosphor
7{
8namespace led
9{
10namespace utils
11{
12constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
13constexpr auto MAPPER_OBJ_PATH = "/xyz/openbmc_project/object_mapper";
14constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper";
15constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
16
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
23/**
24 * @class DBusHandler
25 *
26 * Wrapper class to handle the D-Bus calls
27 *
28 * This class contains the APIs to handle the D-Bus calls.
29 */
30class DBusHandler
31{
32 public:
33 /** @brief Get the bus connection. */
34 static auto& getBus()
35 {
36 static auto bus = sdbusplus::bus::new_default();
37 return bus;
38 }
39
40 /**
41 * @brief Get service name by the path and interface of the DBus.
42 *
43 * @param[in] path - D-Bus object path
44 * @param[in] interface - D-Bus Interface
45 *
46 * @return std::string - the D-Bus service name
47 *
48 */
49 const std::string getService(const std::string& path,
50 const std::string& interface) const;
51
George Liu4c5f5332020-10-10 17:04:28 +080052 /** @brief Get property(type: variant)
53 *
54 * @param[in] objectPath - D-Bus object path
55 * @param[in] interface - D-Bus interface
56 * @param[in] propertyName - D-Bus property name
57 *
58 * @return The value of the property(type: variant)
59 *
60 * @throw sdbusplus::exception::SdBusError when it fails
61 */
62 const PropertyValue getProperty(const std::string& objectPath,
63 const std::string& interface,
64 const std::string& propertyName) const;
65
George Liu1c737af2020-10-16 09:07:02 +080066 /** @brief Set D-Bus property
67 *
68 * @param[in] objectPath - D-Bus object path
69 * @param[in] interface - D-Bus interface
70 * @param[in] propertyName - D-Bus property name
71 * @param[in] value - The value to be set
72 *
73 * @throw sdbusplus::exception::SdBusError when it fails
74 */
75 void setProperty(const std::string& objectPath,
76 const std::string& interface,
77 const std::string& propertyName,
78 const PropertyValue& value) const;
George Liu616a0712021-02-18 10:50:24 +080079
80 /** @brief Get sub tree paths by the path and interface of the DBus.
81 *
82 * @param[in] objectPath - D-Bus object path
83 * @param[in] interface - D-Bus object interface
84 *
85 * @return std::vector<std::string> vector of subtree paths
86 */
87 const std::vector<std::string>
88 getSubTreePaths(const std::string& objectPath,
89 const std::string& interface);
George Liu1c737af2020-10-16 09:07:02 +080090};
91
92} // namespace utils
93} // namespace led
94} // namespace phosphor