blob: 5f232084fed17ec5f004ce7e8854aae708ed0487 [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),
30 std::bind(std::mem_fn(&Manager::onSettingsChanged), this,
31 std::placeholders::_1));
Lei YU710d49b2017-08-01 17:10:17 +080032
Lei YU7f4fca52017-02-23 15:15:51 +080033 // Check the settings daemon to process the new settings
Lei YU710d49b2017-08-01 17:10:17 +080034 auto mode = getSetting(settings.timeSyncMethod.c_str(),
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050035 settings::timeSyncIntf, PROPERTY_TIME_MODE);
Lei YU710d49b2017-08-01 17:10:17 +080036
37 onPropertyChanged(PROPERTY_TIME_MODE, mode);
Lei YU415b9642017-02-09 11:37:26 +080038}
39
Lei YU415b9642017-02-09 11:37:26 +080040void Manager::onPropertyChanged(const std::string& key,
41 const std::string& value)
42{
George Liu0a704522020-04-13 14:51:40 +080043 assert(key == PROPERTY_TIME_MODE);
44
45 // Notify listeners
46 setCurrentTimeMode(value);
47 onTimeModeChanged(value);
Lei YU415b9642017-02-09 11:37:26 +080048}
49
Patrick Williams38679262022-07-22 19:26:55 -050050int Manager::onSettingsChanged(sdbusplus::message_t& msg)
Lei YU710d49b2017-08-01 17:10:17 +080051{
52 using Interface = std::string;
53 using Property = std::string;
54 using Value = std::string;
Patrick Williamsc09ac3f2020-05-13 18:01:29 -050055 using Properties = std::map<Property, std::variant<Value>>;
Lei YU710d49b2017-08-01 17:10:17 +080056
57 Interface interface;
58 Properties properties;
59
60 msg.read(interface, properties);
61
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050062 for (const auto& p : properties)
Lei YU710d49b2017-08-01 17:10:17 +080063 {
Patrick Williams5b746c72020-05-13 11:49:35 -050064 onPropertyChanged(p.first, std::get<std::string>(p.second));
Lei YU710d49b2017-08-01 17:10:17 +080065 }
66
67 return 0;
68}
69
Lei YUa7417132017-02-23 15:24:05 +080070void Manager::updateNtpSetting(const std::string& value)
71{
Lei YU89efe6e2018-07-24 10:38:01 +080072 try
Lei YUa7417132017-02-23 15:24:05 +080073 {
George Liu7e5f9f72022-08-17 12:32:24 +080074 bool isNtp =
75 (value == "xyz.openbmc_project.Time.Synchronization.Method.NTP");
76 auto method =
77 bus.new_method_call(SYSTEMD_TIME_SERVICE, SYSTEMD_TIME_PATH,
78 SYSTEMD_TIME_INTERFACE, METHOD_SET_NTP);
79 method.append(isNtp, false); // isNtp: 'true/false' means Enable/Disable
80 // 'false' meaning no policy-kit
81
Lei YU89efe6e2018-07-24 10:38:01 +080082 bus.call_noreply(method);
George Liu947b5342022-07-01 16:12:18 +080083 lg2::info("Updated NTP setting: {ENABLED}", "ENABLED", isNtp);
Lei YUa7417132017-02-23 15:24:05 +080084 }
Patrick Williams38679262022-07-22 19:26:55 -050085 catch (const sdbusplus::exception_t& ex)
Lei YUa7417132017-02-23 15:24:05 +080086 {
George Liu947b5342022-07-01 16:12:18 +080087 lg2::error("Failed to update NTP setting: {ERROR}", "ERROR", ex);
Lei YUa7417132017-02-23 15:24:05 +080088 }
89}
90
Lei YUa5003ce2017-02-24 15:35:25 +080091bool Manager::setCurrentTimeMode(const std::string& mode)
Lei YU415b9642017-02-09 11:37:26 +080092{
George Liu7e5f9f72022-08-17 12:32:24 +080093 try
Lei YUa5003ce2017-02-24 15:35:25 +080094 {
George Liu7e5f9f72022-08-17 12:32:24 +080095 auto newMode = utils::strToMode(mode);
96 if (newMode != timeMode)
97 {
98 lg2::info("Time mode has been changed to {MODE}", "MODE", newMode);
99 timeMode = newMode;
100 return true;
101 }
Lei YUa5003ce2017-02-24 15:35:25 +0800102 }
George Liu7e5f9f72022-08-17 12:32:24 +0800103 catch (const sdbusplus::exception_t& ex)
Lei YUa5003ce2017-02-24 15:35:25 +0800104 {
George Liu7e5f9f72022-08-17 12:32:24 +0800105 lg2::error("Failed to convert mode from string: {ERROR}", "ERROR", ex);
Lei YUa5003ce2017-02-24 15:35:25 +0800106 }
George Liu7e5f9f72022-08-17 12:32:24 +0800107
108 return false;
Lei YU415b9642017-02-09 11:37:26 +0800109}
110
Lei YUa5003ce2017-02-24 15:35:25 +0800111void Manager::onTimeModeChanged(const std::string& mode)
112{
Lei YUa5003ce2017-02-24 15:35:25 +0800113 // When time_mode is updated, update the NTP setting
114 updateNtpSetting(mode);
115}
116
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500117std::string Manager::getSetting(const char* path, const char* interface,
Lei YU710d49b2017-08-01 17:10:17 +0800118 const char* setting) const
119{
George Liu7e5f9f72022-08-17 12:32:24 +0800120 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 {
128 lg2::error(
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 YU710d49b2017-08-01 17:10:17 +0800133}
134
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500135} // namespace time
136} // namespace phosphor