blob: 88406f4ba413c7cec08e07fa2de642f16996b31e [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
Patrick Williams38679262022-07-22 19:26:55 -050025Manager::Manager(sdbusplus::bus_t& bus) : bus(bus), settings(bus)
Lei YU415b9642017-02-09 11:37:26 +080026{
Lei YU710d49b2017-08-01 17:10:17 +080027 using namespace sdbusplus::bus::match::rules;
28 settingsMatches.emplace_back(
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050029 bus, propertiesChanged(settings.timeSyncMethod, settings::timeSyncIntf),
William A. Kennington III56608002022-11-22 15:22:39 -080030 [&](sdbusplus::message_t& m) { onSettingsChanged(m); });
Lei YU710d49b2017-08-01 17:10:17 +080031
Lei YU7f4fca52017-02-23 15:15:51 +080032 // Check the settings daemon to process the new settings
Lei YU710d49b2017-08-01 17:10:17 +080033 auto mode = getSetting(settings.timeSyncMethod.c_str(),
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050034 settings::timeSyncIntf, PROPERTY_TIME_MODE);
Lei YU710d49b2017-08-01 17:10:17 +080035
36 onPropertyChanged(PROPERTY_TIME_MODE, mode);
Lei YU415b9642017-02-09 11:37:26 +080037}
38
Lei YU415b9642017-02-09 11:37:26 +080039void Manager::onPropertyChanged(const std::string& key,
40 const std::string& value)
41{
George Liu0a704522020-04-13 14:51:40 +080042 assert(key == PROPERTY_TIME_MODE);
43
44 // Notify listeners
45 setCurrentTimeMode(value);
46 onTimeModeChanged(value);
Lei YU415b9642017-02-09 11:37:26 +080047}
48
Patrick Williams38679262022-07-22 19:26:55 -050049int Manager::onSettingsChanged(sdbusplus::message_t& msg)
Lei YU710d49b2017-08-01 17:10:17 +080050{
51 using Interface = std::string;
52 using Property = std::string;
53 using Value = std::string;
Patrick Williamsc09ac3f2020-05-13 18:01:29 -050054 using Properties = std::map<Property, std::variant<Value>>;
Lei YU710d49b2017-08-01 17:10:17 +080055
56 Interface interface;
57 Properties properties;
58
59 msg.read(interface, properties);
60
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050061 for (const auto& p : properties)
Lei YU710d49b2017-08-01 17:10:17 +080062 {
Patrick Williams5b746c72020-05-13 11:49:35 -050063 onPropertyChanged(p.first, std::get<std::string>(p.second));
Lei YU710d49b2017-08-01 17:10:17 +080064 }
65
66 return 0;
67}
68
Lei YUa7417132017-02-23 15:24:05 +080069void Manager::updateNtpSetting(const std::string& value)
70{
Lei YU89efe6e2018-07-24 10:38:01 +080071 try
Lei YUa7417132017-02-23 15:24:05 +080072 {
George Liu7e5f9f72022-08-17 12:32:24 +080073 bool isNtp =
74 (value == "xyz.openbmc_project.Time.Synchronization.Method.NTP");
75 auto method =
76 bus.new_method_call(SYSTEMD_TIME_SERVICE, SYSTEMD_TIME_PATH,
77 SYSTEMD_TIME_INTERFACE, METHOD_SET_NTP);
78 method.append(isNtp, false); // isNtp: 'true/false' means Enable/Disable
79 // 'false' meaning no policy-kit
80
Lei YU89efe6e2018-07-24 10:38:01 +080081 bus.call_noreply(method);
George Liu947b5342022-07-01 16:12:18 +080082 lg2::info("Updated NTP setting: {ENABLED}", "ENABLED", isNtp);
Lei YUa7417132017-02-23 15:24:05 +080083 }
Patrick Williams38679262022-07-22 19:26:55 -050084 catch (const sdbusplus::exception_t& ex)
Lei YUa7417132017-02-23 15:24:05 +080085 {
George Liu947b5342022-07-01 16:12:18 +080086 lg2::error("Failed to update NTP setting: {ERROR}", "ERROR", ex);
Lei YUa7417132017-02-23 15:24:05 +080087 }
88}
89
Lei YUa5003ce2017-02-24 15:35:25 +080090bool Manager::setCurrentTimeMode(const std::string& mode)
Lei YU415b9642017-02-09 11:37:26 +080091{
George Liu7e5f9f72022-08-17 12:32:24 +080092 try
Lei YUa5003ce2017-02-24 15:35:25 +080093 {
George Liu7e5f9f72022-08-17 12:32:24 +080094 auto newMode = utils::strToMode(mode);
95 if (newMode != timeMode)
96 {
97 lg2::info("Time mode has been changed to {MODE}", "MODE", newMode);
98 timeMode = newMode;
99 return true;
100 }
Lei YUa5003ce2017-02-24 15:35:25 +0800101 }
George Liu7e5f9f72022-08-17 12:32:24 +0800102 catch (const sdbusplus::exception_t& ex)
Lei YUa5003ce2017-02-24 15:35:25 +0800103 {
George Liu7e5f9f72022-08-17 12:32:24 +0800104 lg2::error("Failed to convert mode from string: {ERROR}", "ERROR", ex);
Lei YUa5003ce2017-02-24 15:35:25 +0800105 }
George Liu7e5f9f72022-08-17 12:32:24 +0800106
107 return false;
Lei YU415b9642017-02-09 11:37:26 +0800108}
109
Lei YUa5003ce2017-02-24 15:35:25 +0800110void Manager::onTimeModeChanged(const std::string& mode)
111{
Lei YUa5003ce2017-02-24 15:35:25 +0800112 // When time_mode is updated, update the NTP setting
113 updateNtpSetting(mode);
114}
115
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500116std::string Manager::getSetting(const char* path, const char* interface,
Lei YU710d49b2017-08-01 17:10:17 +0800117 const char* setting) const
118{
George Liu7e5f9f72022-08-17 12:32:24 +0800119 try
120 {
121 std::string settingManager = utils::getService(bus, path, interface);
122 return utils::getProperty<std::string>(bus, settingManager.c_str(),
123 path, interface, setting);
124 }
125 catch (const std::exception& ex)
126 {
127 lg2::error(
128 "Failed to get property: {ERROR}, path: {PATH}, interface: {INTERFACE}, name: {NAME}",
129 "ERROR", ex, "PATH", path, "INTERFACE", interface, "NAME", setting);
130 return {};
131 }
Lei YU710d49b2017-08-01 17:10:17 +0800132}
133
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500134} // namespace time
135} // namespace phosphor