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