blob: 1aeaf4bdac7178d21610b8100170fe6977b04f05 [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 Liu947b5342022-07-01 16:12:18 +08005#include <phosphor-logging/lg2.hpp>
Lei YU415b9642017-02-09 11:37:26 +08006
Pavithra Barithayaf93c4052023-04-26 23:28:13 -05007#include <cassert>
8
Lei YU415b9642017-02-09 11:37:26 +08009namespace rules = sdbusplus::bus::match::rules;
10
11namespace // anonymous
12{
Lei YUa7417132017-02-23 15:24:05 +080013
Pavithra Barithaya864e1732023-04-11 04:30:23 -050014constexpr auto systemdTimeService = "org.freedesktop.timedate1";
15constexpr auto systemdTimePath = "/org/freedesktop/timedate1";
16constexpr auto systemdTimeInterface = "org.freedesktop.timedate1";
17constexpr auto methodSetNtp = "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(),
Pavithra Barithaya864e1732023-04-11 04:30:23 -050036 settings::timeSyncIntf, propertyTimeMode);
Lei YU710d49b2017-08-01 17:10:17 +080037
Pavithra Barithaya864e1732023-04-11 04:30:23 -050038 onPropertyChanged(propertyTimeMode, 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{
Pavithra Barithaya864e1732023-04-11 04:30:23 -050044 assert(key == propertyTimeMode);
George Liu0a704522020-04-13 14:51:40 +080045
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");
Pavithra Barithaya864e1732023-04-11 04:30:23 -050077 auto method = bus.new_method_call(systemdTimeService, systemdTimePath,
78 systemdTimeInterface, methodSetNtp);
George Liu7e5f9f72022-08-17 12:32:24 +080079 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);
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050083 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 {
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050087 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 {
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050098 info("Time mode has been changed to {MODE}", "MODE", newMode);
George Liu7e5f9f72022-08-17 12:32:24 +080099 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 {
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -0500105 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 {
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -0500128 error(
George Liu7e5f9f72022-08-17 12:32:24 +0800129 "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