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