Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 1 | #include "manager.hpp" |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 2 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 3 | #include "utils.hpp" |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 4 | |
George Liu | 0a70452 | 2020-04-13 14:51:40 +0800 | [diff] [blame] | 5 | #include <assert.h> |
| 6 | |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 8 | |
| 9 | namespace rules = sdbusplus::bus::match::rules; |
| 10 | |
| 11 | namespace // anonymous |
| 12 | { |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 13 | |
| 14 | constexpr auto SYSTEMD_TIME_SERVICE = "org.freedesktop.timedate1"; |
| 15 | constexpr auto SYSTEMD_TIME_PATH = "/org/freedesktop/timedate1"; |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 16 | constexpr auto SYSTEMD_TIME_INTERFACE = "org.freedesktop.timedate1"; |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 17 | constexpr auto METHOD_SET_NTP = "SetNTP"; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 18 | } // namespace |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 19 | |
| 20 | namespace phosphor |
| 21 | { |
| 22 | namespace time |
| 23 | { |
| 24 | |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 25 | Manager::Manager(sdbusplus::bus_t& bus) : bus(bus), settings(bus) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 26 | { |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 27 | using namespace sdbusplus::bus::match::rules; |
| 28 | settingsMatches.emplace_back( |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 29 | bus, propertiesChanged(settings.timeSyncMethod, settings::timeSyncIntf), |
| 30 | std::bind(std::mem_fn(&Manager::onSettingsChanged), this, |
| 31 | std::placeholders::_1)); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 32 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 33 | // Check the settings daemon to process the new settings |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 34 | auto mode = getSetting(settings.timeSyncMethod.c_str(), |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 35 | settings::timeSyncIntf, PROPERTY_TIME_MODE); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 36 | |
| 37 | onPropertyChanged(PROPERTY_TIME_MODE, mode); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 38 | } |
| 39 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 40 | void Manager::onPropertyChanged(const std::string& key, |
| 41 | const std::string& value) |
| 42 | { |
George Liu | 0a70452 | 2020-04-13 14:51:40 +0800 | [diff] [blame] | 43 | assert(key == PROPERTY_TIME_MODE); |
| 44 | |
| 45 | // Notify listeners |
| 46 | setCurrentTimeMode(value); |
| 47 | onTimeModeChanged(value); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 48 | } |
| 49 | |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 50 | int Manager::onSettingsChanged(sdbusplus::message_t& msg) |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 51 | { |
| 52 | using Interface = std::string; |
| 53 | using Property = std::string; |
| 54 | using Value = std::string; |
Patrick Williams | c09ac3f | 2020-05-13 18:01:29 -0500 | [diff] [blame] | 55 | using Properties = std::map<Property, std::variant<Value>>; |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 56 | |
| 57 | Interface interface; |
| 58 | Properties properties; |
| 59 | |
| 60 | msg.read(interface, properties); |
| 61 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 62 | for (const auto& p : properties) |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 63 | { |
Patrick Williams | 5b746c7 | 2020-05-13 11:49:35 -0500 | [diff] [blame] | 64 | onPropertyChanged(p.first, std::get<std::string>(p.second)); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 70 | void Manager::updateNtpSetting(const std::string& value) |
| 71 | { |
Lei YU | 89efe6e | 2018-07-24 10:38:01 +0800 | [diff] [blame] | 72 | try |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 73 | { |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 74 | bool isNtp = |
| 75 | (value == "xyz.openbmc_project.Time.Synchronization.Method.NTP"); |
| 76 | auto method = |
| 77 | bus.new_method_call(SYSTEMD_TIME_SERVICE, SYSTEMD_TIME_PATH, |
| 78 | SYSTEMD_TIME_INTERFACE, METHOD_SET_NTP); |
| 79 | method.append(isNtp, false); // isNtp: 'true/false' means Enable/Disable |
| 80 | // 'false' meaning no policy-kit |
| 81 | |
Lei YU | 89efe6e | 2018-07-24 10:38:01 +0800 | [diff] [blame] | 82 | bus.call_noreply(method); |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 83 | lg2::info("Updated NTP setting: {ENABLED}", "ENABLED", isNtp); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 84 | } |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 85 | catch (const sdbusplus::exception_t& ex) |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 86 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 87 | lg2::error("Failed to update NTP setting: {ERROR}", "ERROR", ex); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 91 | bool Manager::setCurrentTimeMode(const std::string& mode) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 92 | { |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 93 | try |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 94 | { |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 95 | auto newMode = utils::strToMode(mode); |
| 96 | if (newMode != timeMode) |
| 97 | { |
| 98 | lg2::info("Time mode has been changed to {MODE}", "MODE", newMode); |
| 99 | timeMode = newMode; |
| 100 | return true; |
| 101 | } |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 102 | } |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 103 | catch (const sdbusplus::exception_t& ex) |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 104 | { |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 105 | lg2::error("Failed to convert mode from string: {ERROR}", "ERROR", ex); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 106 | } |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 107 | |
| 108 | return false; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 109 | } |
| 110 | |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 111 | void Manager::onTimeModeChanged(const std::string& mode) |
| 112 | { |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 113 | // When time_mode is updated, update the NTP setting |
| 114 | updateNtpSetting(mode); |
| 115 | } |
| 116 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 117 | std::string Manager::getSetting(const char* path, const char* interface, |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 118 | const char* setting) const |
| 119 | { |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 120 | try |
| 121 | { |
| 122 | std::string settingManager = utils::getService(bus, path, interface); |
| 123 | return utils::getProperty<std::string>(bus, settingManager.c_str(), |
| 124 | path, interface, setting); |
| 125 | } |
| 126 | catch (const std::exception& ex) |
| 127 | { |
| 128 | lg2::error( |
| 129 | "Failed to get property: {ERROR}, path: {PATH}, interface: {INTERFACE}, name: {NAME}", |
| 130 | "ERROR", ex, "PATH", path, "INTERFACE", interface, "NAME", setting); |
| 131 | return {}; |
| 132 | } |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 133 | } |
| 134 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 135 | } // namespace time |
| 136 | } // namespace phosphor |