Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame^] | 3 | #include <phosphor-logging/log.hpp> |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 4 | #include <sdbusplus/bus.hpp> |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | |
| 8 | namespace utils |
| 9 | { |
| 10 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame^] | 11 | using namespace phosphor::logging; |
| 12 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 13 | /** |
| 14 | * @brief Get PSU inventory object path from DBus |
| 15 | */ |
| 16 | std::vector<std::string> getPSUInventoryPath(sdbusplus::bus::bus& bus); |
| 17 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame^] | 18 | /** @brief Get service name from object path and interface |
| 19 | * |
| 20 | * @param[in] bus - The Dbus bus object |
| 21 | * @param[in] path - The Dbus object path |
| 22 | * @param[in] interface - The Dbus interface |
| 23 | * |
| 24 | * @return The name of the service |
| 25 | */ |
| 26 | std::string getService(sdbusplus::bus::bus& bus, const char* path, |
| 27 | const char* interface); |
| 28 | |
| 29 | /** @brief The template function to get property from the requested dbus path |
| 30 | * |
| 31 | * @param[in] bus - The Dbus bus object |
| 32 | * @param[in] service - The Dbus service name |
| 33 | * @param[in] path - The Dbus object path |
| 34 | * @param[in] interface - The Dbus interface |
| 35 | * @param[in] propertyName - The property name to get |
| 36 | * |
| 37 | * @return The value of the property |
| 38 | */ |
| 39 | template <typename T> |
| 40 | T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path, |
| 41 | const char* interface, const char* propertyName) |
| 42 | { |
| 43 | auto method = bus.new_method_call(service, path, |
| 44 | "org.freedesktop.DBus.Properties", "Get"); |
| 45 | method.append(interface, propertyName); |
| 46 | try |
| 47 | { |
| 48 | sdbusplus::message::variant<T> value{}; |
| 49 | auto reply = bus.call(method); |
| 50 | reply.read(value); |
| 51 | return sdbusplus::message::variant_ns::get<T>(value); |
| 52 | } |
| 53 | catch (const sdbusplus::exception::SdBusError& ex) |
| 54 | { |
| 55 | log<level::ERR>("GetProperty call failed", entry("PATH=%s", path), |
| 56 | entry("INTERFACE=%s", interface), |
| 57 | entry("PROPERTY=%s", propertyName)); |
| 58 | throw std::runtime_error("GetProperty call failed"); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @brief Calculate the version id from the version string. |
| 64 | * |
| 65 | * @details The version id is a unique 8 hexadecimal digit id |
| 66 | * calculated from the version string. |
| 67 | * |
| 68 | * @param[in] version - The image version string (e.g. v1.99.10-19). |
| 69 | * |
| 70 | * @return The id. |
| 71 | */ |
| 72 | std::string getVersionId(const std::string& version); |
| 73 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 74 | } // namespace utils |