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. |
| 111 | * Valid strings are "NTP", "MANUAL" |
| 112 | * If it's not a valid time mode string, it means something |
| 113 | * goes wrong so raise exception. |
| 114 | * |
| 115 | * @param[in] mode - The string of time mode |
| 116 | * |
| 117 | * @return The Mode enum |
| 118 | */ |
| 119 | Mode strToMode(const std::string& mode); |
| 120 | |
| 121 | /** @brief Convert a string to enum Owner |
| 122 | * |
| 123 | * Convert the time owner string to enum. |
| 124 | * Valid strings are "BMC", "HOST", "SPLIT", "BOTH" |
| 125 | * If it's not a valid time owner string, it means something |
| 126 | * goes wrong so raise exception. |
| 127 | * |
| 128 | * @param[in] owner - The string of time owner |
| 129 | * |
| 130 | * @return The Owner enum |
| 131 | */ |
| 132 | Owner strToOwner(const std::string& owner); |
| 133 | |
| 134 | /** @brief Convert a mode enum to mode string |
| 135 | * |
| 136 | * @param[in] mode - The Mode enum |
| 137 | * |
| 138 | * @return The string of the mode |
| 139 | */ |
| 140 | const char* modeToStr(Mode mode); |
| 141 | |
| 142 | /** @brief Convert a owner enum to owner string |
| 143 | * |
| 144 | * @param[in] owner - The Owner enum |
| 145 | * |
| 146 | * @return The string of the owner |
| 147 | */ |
| 148 | const char* ownerToStr(Owner owner); |
| 149 | |
| 150 | } // namespace utils |
| 151 | } // namespace time |
| 152 | } // namespace phosphor |