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