1 #pragma once 2 #include <sdbusplus/server.hpp> 3 4 #include <unordered_map> 5 #include <vector> 6 namespace phosphor 7 { 8 namespace led 9 { 10 namespace utils 11 { 12 static constexpr auto mapperBusName = "xyz.openbmc_project.ObjectMapper"; 13 static constexpr auto mapperObjPath = "/xyz/openbmc_project/object_mapper"; 14 static constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; 15 static constexpr auto proIntf = "org.freedesktop.DBus.Properties"; 16 17 // The value of the property(type: variant, contains some basic types) 18 // Eg: uint8_t : dutyOn, uint16_t : Period, std::string : Name, 19 // std::vector<std::string> : endpoints, bool : Functional 20 using PropertyValue = std::variant<uint8_t, uint16_t, std::string, 21 std::vector<std::string>, bool>; 22 23 // The name of the property 24 using DbusProperty = std::string; 25 26 // The Map to constructs all properties values of the interface 27 using PropertyMap = std::unordered_map<DbusProperty, PropertyValue>; 28 29 /** 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 */ 36 class 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 static std::string getService(const std::string& path, 56 const std::string& interface); 57 58 /** @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 * 65 * @throw sdbusplus::exception_t when it fails 66 */ 67 static PropertyMap getAllProperties(const std::string& objectPath, 68 const std::string& interface); 69 70 /** @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 * 78 * @throw sdbusplus::exception_t when it fails 79 */ 80 static PropertyValue getProperty(const std::string& objectPath, 81 const std::string& interface, 82 const std::string& propertyName); 83 84 /** @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 * 91 * @throw sdbusplus::exception_t when it fails 92 */ 93 static void setProperty(const std::string& objectPath, 94 const std::string& interface, 95 const std::string& propertyName, 96 const PropertyValue& value); 97 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 static std::vector<std::string> getSubTreePaths( 106 const std::string& objectPath, const std::string& interface); 107 }; 108 109 } // namespace utils 110 } // namespace led 111 } // namespace phosphor 112