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"; |
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 | |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 25 | PHOSPHOR_LOG2_USING; |
| 26 | |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 27 | Manager::Manager(sdbusplus::bus_t& bus) : bus(bus), settings(bus) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 28 | { |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 29 | using namespace sdbusplus::bus::match::rules; |
| 30 | settingsMatches.emplace_back( |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 31 | bus, propertiesChanged(settings.timeSyncMethod, settings::timeSyncIntf), |
William A. Kennington III | 5660800 | 2022-11-22 15:22:39 -0800 | [diff] [blame] | 32 | [&](sdbusplus::message_t& m) { onSettingsChanged(m); }); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 33 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 34 | // Check the settings daemon to process the new settings |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 35 | auto mode = getSetting(settings.timeSyncMethod.c_str(), |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 36 | settings::timeSyncIntf, propertyTimeMode); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 37 | |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 38 | onPropertyChanged(propertyTimeMode, mode); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 39 | } |
| 40 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 41 | void Manager::onPropertyChanged(const std::string& key, |
| 42 | const std::string& value) |
| 43 | { |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 44 | assert(key == propertyTimeMode); |
George Liu | 0a70452 | 2020-04-13 14:51:40 +0800 | [diff] [blame] | 45 | |
| 46 | // Notify listeners |
| 47 | setCurrentTimeMode(value); |
| 48 | onTimeModeChanged(value); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 49 | } |
| 50 | |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 51 | int Manager::onSettingsChanged(sdbusplus::message_t& msg) |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 52 | { |
| 53 | using Interface = std::string; |
| 54 | using Property = std::string; |
| 55 | using Value = std::string; |
Patrick Williams | c09ac3f | 2020-05-13 18:01:29 -0500 | [diff] [blame] | 56 | using Properties = std::map<Property, std::variant<Value>>; |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 57 | |
| 58 | Interface interface; |
| 59 | Properties properties; |
| 60 | |
| 61 | msg.read(interface, properties); |
| 62 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 63 | for (const auto& p : properties) |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 64 | { |
Patrick Williams | 5b746c7 | 2020-05-13 11:49:35 -0500 | [diff] [blame] | 65 | onPropertyChanged(p.first, std::get<std::string>(p.second)); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 71 | void Manager::updateNtpSetting(const std::string& value) |
| 72 | { |
Lei YU | 89efe6e | 2018-07-24 10:38:01 +0800 | [diff] [blame] | 73 | try |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 74 | { |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 75 | bool isNtp = |
| 76 | (value == "xyz.openbmc_project.Time.Synchronization.Method.NTP"); |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 77 | auto method = bus.new_method_call(systemdTimeService, systemdTimePath, |
| 78 | systemdTimeInterface, methodSetNtp); |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 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); |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 83 | 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 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 87 | 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 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 98 | info("Time mode has been changed to {MODE}", "MODE", newMode); |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 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 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 105 | 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 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 128 | error( |
George Liu | 7e5f9f7 | 2022-08-17 12:32:24 +0800 | [diff] [blame] | 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 |