blob: f8cd289d8c285202f8a7aaf2884c810123b621a8 [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 YU710d49b2017-08-01 17:10:17 +080072 bool isNtp =
73 (value == "xyz.openbmc_project.Time.Synchronization.Method.NTP");
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050074 auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE, SYSTEMD_TIME_PATH,
75 SYSTEMD_TIME_INTERFACE, METHOD_SET_NTP);
Lei YUa7417132017-02-23 15:24:05 +080076 method.append(isNtp, false); // isNtp: 'true/false' means Enable/Disable
77 // 'false' meaning no policy-kit
78
Lei YU89efe6e2018-07-24 10:38:01 +080079 try
Lei YUa7417132017-02-23 15:24:05 +080080 {
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{
Lei YUddd54422017-04-18 16:38:44 +080092 auto newMode = utils::strToMode(mode);
Lei YUa5003ce2017-02-24 15:35:25 +080093 if (newMode != timeMode)
94 {
George Liu947b5342022-07-01 16:12:18 +080095 lg2::info("Time mode has been changed to {MODE}", "MODE", newMode);
Lei YUa5003ce2017-02-24 15:35:25 +080096 timeMode = newMode;
Lei YUa5003ce2017-02-24 15:35:25 +080097 return true;
98 }
99 else
100 {
101 return false;
102 }
Lei YU415b9642017-02-09 11:37:26 +0800103}
104
Lei YUa5003ce2017-02-24 15:35:25 +0800105void Manager::onTimeModeChanged(const std::string& mode)
106{
Lei YUa5003ce2017-02-24 15:35:25 +0800107 // When time_mode is updated, update the NTP setting
108 updateNtpSetting(mode);
109}
110
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500111std::string Manager::getSetting(const char* path, const char* interface,
Lei YU710d49b2017-08-01 17:10:17 +0800112 const char* setting) const
113{
114 std::string settingManager = utils::getService(bus, path, interface);
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500115 return utils::getProperty<std::string>(bus, settingManager.c_str(), path,
116 interface, setting);
Lei YU710d49b2017-08-01 17:10:17 +0800117}
118
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500119} // namespace time
120} // namespace phosphor