1 #pragma once 2 3 #include <sdbusplus/bus.hpp> 4 5 #include <optional> 6 #include <string> 7 #include <tuple> 8 9 namespace open_power 10 { 11 namespace occ 12 { 13 namespace utils 14 { 15 16 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; 17 constexpr auto MAPPER_OBJ_PATH = "/xyz/openbmc_project/object_mapper"; 18 constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper"; 19 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties"; 20 21 // The value of the property(type: variant, contains some basic types) 22 using PropertyValue = 23 std::variant<uint32_t, bool, double, std::string, std::vector<std::string>>; 24 25 /** @brief Get the bus connection. */ 26 static auto& getBus() 27 { 28 static auto bus = sdbusplus::bus::new_default(); 29 return bus; 30 } 31 32 /** 33 * @brief Get service name by the path and interface of the DBus. 34 * 35 * @param[in] path - D-Bus object path 36 * @param[in] interface - D-Bus Interface 37 * 38 * @return std::string - the D-Bus service name 39 * 40 */ 41 const std::string getService(const std::string& path, 42 const std::string& interface); 43 44 /** @brief Get property(type: variant) 45 * 46 * @param[in] objectPath - D-Bus object path 47 * @param[in] interface - D-Bus interface 48 * @param[in] propertyName - D-Bus property name 49 * 50 * @return The value of the property(type: variant) 51 * 52 * @throw sdbusplus::exception_t when it fails 53 */ 54 const PropertyValue getProperty(const std::string& objectPath, 55 const std::string& interface, 56 const std::string& propertyName); 57 58 /** 59 * @brief Sets a given object's property value 60 * 61 * @param[in] object - Name of the object containing the property 62 * @param[in] interface - Interface name containing the property 63 * @param[in] property - Property name 64 * @param[in] value - Property value 65 */ 66 void setProperty(const std::string& objectPath, const std::string& interface, 67 const std::string& propertyName, PropertyValue&& value); 68 69 /** @brief Get subtree paths 70 * 71 * @param[in] interfaces - D-Bus interfaces 72 * @param[in] path - D-Bus object path 73 * 74 * @return The D-Bus paths from the GetSubTree method 75 * 76 * @throw sdbusplus::exception_t when it fails 77 */ 78 std::vector<std::string> 79 getSubtreePaths(const std::vector<std::string>& interfaces, 80 const std::string& path = "/"); 81 82 /** 83 * @brief Get the D-Bus service and object path for an interface 84 * 85 * @param[in] interface - D-Bus interface name 86 * @param[in,out] path - D-Bus object path 87 * 88 * @return D-Bus service name 89 */ 90 std::string getServiceUsingSubTree(const std::string& interface, 91 std::string& path); 92 93 /** 94 * @brief Get status of the host 95 * 96 * @return true is the host is running, else false 97 */ 98 bool isHostRunning(); 99 100 } // namespace utils 101 } // namespace occ 102 } // namespace open_power 103