blob: 169fb301a87862c32c5ae2a373d507d2647fc684 [file] [log] [blame]
Lei YU415b9642017-02-09 11:37:26 +08001#include "manager.hpp"
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05002
Lei YU7f4fca52017-02-23 15:15:51 +08003#include "utils.hpp"
Lei YU415b9642017-02-09 11:37:26 +08004
George Liu0a704522020-04-13 14:51:40 +08005#include <assert.h>
6
George Liu947b5342022-07-01 16:12:18 +08007#include <phosphor-logging/lg2.hpp>
Lei YU415b9642017-02-09 11:37:26 +08008
9namespace rules = sdbusplus::bus::match::rules;
10
11namespace // anonymous
12{
Lei YUa7417132017-02-23 15:24:05 +080013
14constexpr auto SYSTEMD_TIME_SERVICE = "org.freedesktop.timedate1";
15constexpr auto SYSTEMD_TIME_PATH = "/org/freedesktop/timedate1";
Lei YUdd8e9e42017-04-19 17:46:58 +080016constexpr auto SYSTEMD_TIME_INTERFACE = "org.freedesktop.timedate1";
Lei YUa7417132017-02-23 15:24:05 +080017constexpr auto METHOD_SET_NTP = "SetNTP";
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050018} // namespace
Lei YU415b9642017-02-09 11:37:26 +080019
20namespace phosphor
21{
22namespace time
23{
24
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050025PHOSPHOR_LOG2_USING;
26
Patrick Williams38679262022-07-22 19:26:55 -050027Manager::Manager(sdbusplus::bus_t& bus) : bus(bus), settings(bus)
Lei YU415b9642017-02-09 11:37:26 +080028{
Lei YU710d49b2017-08-01 17:10:17 +080029 using namespace sdbusplus::bus::match::rules;
30 settingsMatches.emplace_back(
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050031 bus, propertiesChanged(settings.timeSyncMethod, settings::timeSyncIntf),
William A. Kennington III56608002022-11-22 15:22:39 -080032 [&](sdbusplus::message_t& m) { onSettingsChanged(m); });
Lei YU710d49b2017-08-01 17:10:17 +080033
Lei YU7f4fca52017-02-23 15:15:51 +080034 // Check the settings daemon to process the new settings
Lei YU710d49b2017-08-01 17:10:17 +080035 auto mode = getSetting(settings.timeSyncMethod.c_str(),
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050036 settings::timeSyncIntf, PROPERTY_TIME_MODE);
Lei YU710d49b2017-08-01 17:10:17 +080037
38 onPropertyChanged(PROPERTY_TIME_MODE, mode);
Lei YU415b9642017-02-09 11:37:26 +080039}
40
Lei YU415b9642017-02-09 11:37:26 +080041void Manager::onPropertyChanged(const std::string& key,
42 const std::string& value)
43{
George Liu0a704522020-04-13 14:51:40 +080044 assert(key == PROPERTY_TIME_MODE);
45
46 // Notify listeners
47 setCurrentTimeMode(value);
48 onTimeModeChanged(value);
Lei YU415b9642017-02-09 11:37:26 +080049}
50
Patrick Williams38679262022-07-22 19:26:55 -050051int Manager::onSettingsChanged(sdbusplus::message_t& msg)
Lei YU710d49b2017-08-01 17:10:17 +080052{
53 using Interface = std::string;
54 using Property = std::string;
55 using Value = std::string;
Patrick Williamsc09ac3f2020-05-13 18:01:29 -050056 using Properties = std::map<Property, std::variant<Value>>;
Lei YU710d49b2017-08-01 17:10:17 +080057
58 Interface interface;
59 Properties properties;
60
61 msg.read(interface, properties);
62
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050063 for (const auto& p : properties)
Lei YU710d49b2017-08-01 17:10:17 +080064 {
Patrick Williams5b746c72020-05-13 11:49:35 -050065 onPropertyChanged(p.first, std::get<std::string>(p.second));
Lei YU710d49b2017-08-01 17:10:17 +080066 }
67
68 return 0;
69}
70
Lei YUa7417132017-02-23 15:24:05 +080071void Manager::updateNtpSetting(const std::string& value)
72{
Lei YU89efe6e2018-07-24 10:38:01 +080073 try
Lei YUa7417132017-02-23 15:24:05 +080074 {
George Liu7e5f9f72022-08-17 12:32:24 +080075 bool isNtp =
76 (value == "xyz.openbmc_project.Time.Synchronization.Method.NTP");
77 auto method =
78 bus.new_method_call(SYSTEMD_TIME_SERVICE, SYSTEMD_TIME_PATH,
79 SYSTEMD_TIME_INTERFACE, METHOD_SET_NTP);
80 method.append(isNtp, false); // isNtp: 'true/false' means Enable/Disable
81 // 'false' meaning no policy-kit
82
Lei YU89efe6e2018-07-24 10:38:01 +080083 bus.call_noreply(method);
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050084 info("Updated NTP setting: {ENABLED}", "ENABLED", isNtp);
Lei YUa7417132017-02-23 15:24:05 +080085 }
Patrick Williams38679262022-07-22 19:26:55 -050086 catch (const sdbusplus::exception_t& ex)
Lei YUa7417132017-02-23 15:24:05 +080087 {
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050088 error("Failed to update NTP setting: {ERROR}", "ERROR", ex);
Lei YUa7417132017-02-23 15:24:05 +080089 }
90}
91
Lei YUa5003ce2017-02-24 15:35:25 +080092bool Manager::setCurrentTimeMode(const std::string& mode)
Lei YU415b9642017-02-09 11:37:26 +080093{
George Liu7e5f9f72022-08-17 12:32:24 +080094 try
Lei YUa5003ce2017-02-24 15:35:25 +080095 {
George Liu7e5f9f72022-08-17 12:32:24 +080096 auto newMode = utils::strToMode(mode);
97 if (newMode != timeMode)
98 {
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050099 info("Time mode has been changed to {MODE}", "MODE", newMode);
George Liu7e5f9f72022-08-17 12:32:24 +0800100 timeMode = newMode;
101 return true;
102 }
Lei YUa5003ce2017-02-24 15:35:25 +0800103 }
George Liu7e5f9f72022-08-17 12:32:24 +0800104 catch (const sdbusplus::exception_t& ex)
Lei YUa5003ce2017-02-24 15:35:25 +0800105 {
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -0500106 error("Failed to convert mode from string: {ERROR}", "ERROR", ex);
Lei YUa5003ce2017-02-24 15:35:25 +0800107 }
George Liu7e5f9f72022-08-17 12:32:24 +0800108
109 return false;
Lei YU415b9642017-02-09 11:37:26 +0800110}
111
Lei YUa5003ce2017-02-24 15:35:25 +0800112void Manager::onTimeModeChanged(const std::string& mode)
113{
Lei YUa5003ce2017-02-24 15:35:25 +0800114 // When time_mode is updated, update the NTP setting
115 updateNtpSetting(mode);
116}
117
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500118std::string Manager::getSetting(const char* path, const char* interface,
Lei YU710d49b2017-08-01 17:10:17 +0800119 const char* setting) const
120{
George Liu7e5f9f72022-08-17 12:32:24 +0800121 try
122 {
123 std::string settingManager = utils::getService(bus, path, interface);
124 return utils::getProperty<std::string>(bus, settingManager.c_str(),
125 path, interface, setting);
126 }
127 catch (const std::exception& ex)
128 {
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -0500129 error(
George Liu7e5f9f72022-08-17 12:32:24 +0800130 "Failed to get property: {ERROR}, path: {PATH}, interface: {INTERFACE}, name: {NAME}",
131 "ERROR", ex, "PATH", path, "INTERFACE", interface, "NAME", setting);
132 return {};
133 }
Lei YU710d49b2017-08-01 17:10:17 +0800134}
135
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500136} // namespace time
137} // namespace phosphor