Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 3 | #include "types.hpp" |
| 4 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 5 | #include <fstream> |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 6 | #include <phosphor-logging/log.hpp> |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 8 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 9 | namespace phosphor |
| 10 | { |
| 11 | namespace time |
| 12 | { |
| 13 | namespace utils |
| 14 | { |
| 15 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 16 | using namespace phosphor::logging; |
| 17 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 18 | /** @brief Read data with type T from file |
| 19 | * |
| 20 | * @param[in] fileName - The name of file to read from |
| 21 | * |
| 22 | * @return The data with type T |
| 23 | */ |
| 24 | template <typename T> |
| 25 | T readData(const char* fileName) |
| 26 | { |
| 27 | T data{}; |
| 28 | std::ifstream fs(fileName); |
| 29 | if (fs.is_open()) |
| 30 | { |
| 31 | fs >> data; |
| 32 | } |
| 33 | return data; |
| 34 | } |
| 35 | |
| 36 | /** @brief Write data with type T to file |
| 37 | * |
| 38 | * @param[in] fileName - The name of file to write to |
| 39 | * @param[in] data - The data with type T to write to file |
| 40 | */ |
| 41 | template <typename T> |
| 42 | void writeData(const char* fileName, T&& data) |
| 43 | { |
| 44 | std::ofstream fs(fileName, std::ios::out); |
| 45 | if (fs.is_open()) |
| 46 | { |
| 47 | fs << std::forward<T>(data); |
| 48 | } |
| 49 | } |
| 50 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 51 | /** @brief The template function to get property from the requested dbus path |
| 52 | * |
| 53 | * @param[in] bus - The Dbus bus object |
| 54 | * @param[in] service - The Dbus service name |
| 55 | * @param[in] path - The Dbus object path |
| 56 | * @param[in] interface - The Dbus interface |
| 57 | * @param[in] propertyName - The property name to get |
| 58 | * |
| 59 | * @return The value of the property |
| 60 | */ |
| 61 | template <typename T> |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 62 | T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path, |
| 63 | const char* interface, const char* propertyName) |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 64 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 65 | auto method = bus.new_method_call(service, path, |
| 66 | "org.freedesktop.DBus.Properties", "Get"); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 67 | method.append(interface, propertyName); |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 68 | try |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 69 | { |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 70 | sdbusplus::message::variant<T> value{}; |
| 71 | auto reply = bus.call(method); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 72 | reply.read(value); |
William A. Kennington III | 1f1d8e0 | 2018-11-06 16:06:34 -0800 | [diff] [blame] | 73 | return sdbusplus::message::variant_ns::get<T>(value); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 74 | } |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 75 | catch (const sdbusplus::exception::SdBusError& ex) |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 76 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 77 | log<level::ERR>("GetProperty call failed", entry("PATH=%s", path), |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 78 | entry("INTERFACE=%s", interface), |
| 79 | entry("PROPERTY=%s", propertyName)); |
| 80 | throw std::runtime_error("GetProperty call failed"); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 81 | } |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 82 | } |
| 83 | |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 84 | /** @brief Get service name from object path and interface |
| 85 | * |
| 86 | * @param[in] bus - The Dbus bus object |
| 87 | * @param[in] path - The Dbus object path |
| 88 | * @param[in] interface - The Dbus interface |
| 89 | * |
| 90 | * @return The name of the service |
| 91 | */ |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 92 | std::string getService(sdbusplus::bus::bus& bus, const char* path, |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 93 | const char* interface); |
| 94 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 95 | /** @brief Convert a string to enum Mode |
| 96 | * |
| 97 | * Convert the time mode string to enum. |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 98 | * Valid strings are |
| 99 | * "xyz.openbmc_project.Time.Synchronization.Method.NTP" |
| 100 | * "xyz.openbmc_project.Time.Synchronization.Method.Manual" |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 101 | * If it's not a valid time mode string, it means something |
| 102 | * goes wrong so raise exception. |
| 103 | * |
| 104 | * @param[in] mode - The string of time mode |
| 105 | * |
| 106 | * @return The Mode enum |
| 107 | */ |
| 108 | Mode strToMode(const std::string& mode); |
| 109 | |
| 110 | /** @brief Convert a string to enum Owner |
| 111 | * |
| 112 | * Convert the time owner string to enum. |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 113 | * Valid strings are |
| 114 | * "xyz.openbmc_project.Time.Owner.Owners.BMC" |
| 115 | * "xyz.openbmc_project.Time.Owner.Owners.Host" |
| 116 | * "xyz.openbmc_project.Time.Owner.Owners.Both" |
| 117 | * "xyz.openbmc_project.Time.Owner.Owners.Split" |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 118 | * If it's not a valid time owner string, it means something |
| 119 | * goes wrong so raise exception. |
| 120 | * |
| 121 | * @param[in] owner - The string of time owner |
| 122 | * |
| 123 | * @return The Owner enum |
| 124 | */ |
| 125 | Owner strToOwner(const std::string& owner); |
| 126 | |
| 127 | /** @brief Convert a mode enum to mode string |
| 128 | * |
| 129 | * @param[in] mode - The Mode enum |
| 130 | * |
| 131 | * @return The string of the mode |
| 132 | */ |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 133 | std::string modeToStr(Mode mode); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 134 | |
| 135 | /** @brief Convert a owner enum to owner string |
| 136 | * |
| 137 | * @param[in] owner - The Owner enum |
| 138 | * |
| 139 | * @return The string of the owner |
| 140 | */ |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 141 | std::string ownerToStr(Owner owner); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 142 | |
| 143 | } // namespace utils |
| 144 | } // namespace time |
| 145 | } // namespace phosphor |