blob: fd111ff03bcdaac71ec525946d0441d211e6af56 [file] [log] [blame]
Lei YU5e0dcb32019-08-02 18:04:34 +08001#pragma once
2
Lei YUad90ad52019-08-06 11:19:28 +08003#include <phosphor-logging/log.hpp>
Lei YU5e0dcb32019-08-02 18:04:34 +08004#include <sdbusplus/bus.hpp>
5#include <string>
6#include <vector>
7
8namespace utils
9{
10
Lei YUad90ad52019-08-06 11:19:28 +080011using namespace phosphor::logging;
12
Lei YU5e0dcb32019-08-02 18:04:34 +080013/**
14 * @brief Get PSU inventory object path from DBus
15 */
16std::vector<std::string> getPSUInventoryPath(sdbusplus::bus::bus& bus);
17
Lei YUad90ad52019-08-06 11:19:28 +080018/** @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 */
26std::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 */
39template <typename T>
40T 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 */
72std::string getVersionId(const std::string& version);
73
Lei YU5e0dcb32019-08-02 18:04:34 +080074} // namespace utils