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 | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 6 | |
Pavithra Barithaya | f93c405 | 2023-04-26 23:28:13 -0500 | [diff] [blame] | 7 | #include <cassert> |
| 8 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 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 | |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 14 | constexpr auto systemdTimeService = "org.freedesktop.timedate1"; |
| 15 | constexpr auto systemdTimePath = "/org/freedesktop/timedate1"; |
| 16 | constexpr auto systemdTimeInterface = "org.freedesktop.timedate1"; |
| 17 | constexpr auto methodSetNtp = "SetNTP"; |
Jason Zhu | e101030 | 2024-04-02 04:20:43 +0000 | [diff] [blame^] | 18 | constexpr auto propertyNtp = "NTP"; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 19 | } // namespace |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 20 | |
| 21 | namespace phosphor |
| 22 | { |
| 23 | namespace time |
| 24 | { |
| 25 | |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 26 | PHOSPHOR_LOG2_USING; |
| 27 | |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 28 | Manager::Manager(sdbusplus::bus_t& bus) : bus(bus), settings(bus) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 29 | { |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 30 | using namespace sdbusplus::bus::match::rules; |
Jason Zhu | e101030 | 2024-04-02 04:20:43 +0000 | [diff] [blame^] | 31 | timedateMatches.emplace_back( |
| 32 | bus, propertiesChanged(systemdTimePath, systemdTimeInterface), |
| 33 | [&](sdbusplus::message_t& m) { onTimedateChanged(m); }); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 34 | settingsMatches.emplace_back( |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 35 | bus, propertiesChanged(settings.timeSyncMethod, settings::timeSyncIntf), |
William A. Kennington III | 5660800 | 2022-11-22 15:22:39 -0800 | [diff] [blame] | 36 | [&](sdbusplus::message_t& m) { onSettingsChanged(m); }); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 37 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 38 | // Check the settings daemon to process the new settings |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 39 | auto mode = getSetting(settings.timeSyncMethod.c_str(), |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 40 | settings::timeSyncIntf, propertyTimeMode); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 41 | |
Jason Zhu | e101030 | 2024-04-02 04:20:43 +0000 | [diff] [blame^] | 42 | onPropertyChanged(propertyTimeMode, mode, true); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 43 | } |
| 44 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 45 | void Manager::onPropertyChanged(const std::string& key, |
Jason Zhu | e101030 | 2024-04-02 04:20:43 +0000 | [diff] [blame^] | 46 | const std::string& value, bool forceSet) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 47 | { |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 48 | assert(key == propertyTimeMode); |
George Liu | 0a70452 | 2020-04-13 14:51:40 +0800 | [diff] [blame] | 49 | |
Jason Zhu | e101030 | 2024-04-02 04:20:43 +0000 | [diff] [blame^] | 50 | bool newNtpMode = (settings::ntpSync == value); |
| 51 | bool oldNtpMode = (Mode::NTP == getTimeMode()); |
| 52 | if (forceSet || (newNtpMode != oldNtpMode)) |
| 53 | { |
| 54 | // Notify listeners |
| 55 | onTimeModeChanged(value); |
| 56 | setCurrentTimeMode(value); |
| 57 | debug("NTP property changed in phosphor-settings, update to systemd" |
| 58 | " time service."); |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | debug("NTP mode is already the same, skip setting to systemd time" |
| 63 | " service again."); |
| 64 | } |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 65 | } |
| 66 | |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 67 | int Manager::onSettingsChanged(sdbusplus::message_t& msg) |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 68 | { |
| 69 | using Interface = std::string; |
| 70 | using Property = std::string; |
| 71 | using Value = std::string; |
Patrick Williams | c09ac3f | 2020-05-13 18:01:29 -0500 | [diff] [blame] | 72 | using Properties = std::map<Property, std::variant<Value>>; |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 73 | |
| 74 | Interface interface; |
| 75 | Properties properties; |
| 76 | |
| 77 | msg.read(interface, properties); |
| 78 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 79 | for (const auto& p : properties) |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 80 | { |
Patrick Williams | 5b746c7 | 2020-05-13 11:49:35 -0500 | [diff] [blame] | 81 | onPropertyChanged(p.first, std::get<std::string>(p.second)); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Jason Zhu | e101030 | 2024-04-02 04:20:43 +0000 | [diff] [blame^] | 87 | int Manager::onTimedateChanged(sdbusplus::message_t& msg) |
| 88 | { |
| 89 | using Properties = std::map<std::string, std::variant<std::string, bool>>; |
| 90 | |
| 91 | std::string interface; |
| 92 | Properties properties; |
| 93 | |
| 94 | msg.read(interface, properties); |
| 95 | |
| 96 | auto iter = properties.find(propertyNtp); |
| 97 | if (iter == properties.end()) |
| 98 | { |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | try |
| 103 | { |
| 104 | bool newNtpMode = std::get<bool>(iter->second); |
| 105 | bool oldNtpMode = (Mode::NTP == getTimeMode()); |
| 106 | if (newNtpMode != oldNtpMode) |
| 107 | { |
| 108 | const auto& timeMode = newNtpMode ? settings::ntpSync |
| 109 | : settings::manualSync; |
| 110 | std::string settingManager = utils::getService( |
| 111 | bus, settings.timeSyncMethod.c_str(), settings::timeSyncIntf); |
| 112 | utils::setProperty(bus, settingManager, settings.timeSyncMethod, |
| 113 | settings::timeSyncIntf, propertyTimeMode, |
| 114 | timeMode); |
| 115 | setCurrentTimeMode(timeMode); |
| 116 | debug("NTP property changed in systemd time service, update to" |
| 117 | " phosphor-settings."); |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | debug("NTP mode is already the same, skip setting to" |
| 122 | " phosphor-settings again."); |
| 123 | } |
| 124 | } |
| 125 | catch (const std::exception& ex) |
| 126 | { |
| 127 | error("Failed to sync NTP: {ERROR}", "ERROR", ex); |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 133 | void Manager::updateNtpSetting(const std::string& value) |
| 134 | { |
Lei YU | 89efe6e | 2018-07-24 10:38:01 +0800 | [diff] [blame] | 135 | try |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 136 | { |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 137 | bool isNtp = |
| 138 | (value == "xyz.openbmc_project.Time.Synchronization.Method.NTP"); |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 139 | auto method = bus.new_method_call(systemdTimeService, systemdTimePath, |
| 140 | systemdTimeInterface, methodSetNtp); |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 141 | method.append(isNtp, false); // isNtp: 'true/false' means Enable/Disable |
| 142 | // 'false' meaning no policy-kit |
| 143 | |
Lei YU | 89efe6e | 2018-07-24 10:38:01 +0800 | [diff] [blame] | 144 | bus.call_noreply(method); |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 145 | info("Updated NTP setting: {ENABLED}", "ENABLED", isNtp); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 146 | } |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 147 | catch (const sdbusplus::exception_t& ex) |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 148 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 149 | error("Failed to update NTP setting: {ERROR}", "ERROR", ex); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 153 | bool Manager::setCurrentTimeMode(const std::string& mode) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 154 | { |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 155 | try |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 156 | { |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 157 | auto newMode = utils::strToMode(mode); |
| 158 | if (newMode != timeMode) |
| 159 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 160 | info("Time mode has been changed to {MODE}", "MODE", newMode); |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 161 | timeMode = newMode; |
| 162 | return true; |
| 163 | } |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 164 | } |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 165 | catch (const sdbusplus::exception_t& ex) |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 166 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 167 | error("Failed to convert mode from string: {ERROR}", "ERROR", ex); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 168 | } |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 169 | |
| 170 | return false; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 171 | } |
| 172 | |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 173 | void Manager::onTimeModeChanged(const std::string& mode) |
| 174 | { |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 175 | // When time_mode is updated, update the NTP setting |
| 176 | updateNtpSetting(mode); |
| 177 | } |
| 178 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 179 | std::string Manager::getSetting(const char* path, const char* interface, |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 180 | const char* setting) const |
| 181 | { |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 182 | try |
| 183 | { |
| 184 | std::string settingManager = utils::getService(bus, path, interface); |
| 185 | return utils::getProperty<std::string>(bus, settingManager.c_str(), |
| 186 | path, interface, setting); |
| 187 | } |
| 188 | catch (const std::exception& ex) |
| 189 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 190 | error( |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 191 | "Failed to get property: {ERROR}, path: {PATH}, interface: {INTERFACE}, name: {NAME}", |
| 192 | "ERROR", ex, "PATH", path, "INTERFACE", interface, "NAME", setting); |
| 193 | return {}; |
| 194 | } |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 195 | } |
| 196 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 197 | } // namespace time |
| 198 | } // namespace phosphor |