blob: 383d4168cf7d150ba48ea6e739d42646451da5a9 [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
Lei YU86d80412017-07-12 13:12:12 +08007#include <phosphor-logging/elog-errors.hpp>
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05008#include <phosphor-logging/elog.hpp>
Lei YU415b9642017-02-09 11:37:26 +08009#include <phosphor-logging/log.hpp>
Lei YU86d80412017-07-12 13:12:12 +080010#include <xyz/openbmc_project/Common/error.hpp>
Lei YU415b9642017-02-09 11:37:26 +080011
12namespace rules = sdbusplus::bus::match::rules;
13
14namespace // anonymous
15{
Lei YUa7417132017-02-23 15:24:05 +080016
17constexpr auto SYSTEMD_TIME_SERVICE = "org.freedesktop.timedate1";
18constexpr auto SYSTEMD_TIME_PATH = "/org/freedesktop/timedate1";
Lei YUdd8e9e42017-04-19 17:46:58 +080019constexpr auto SYSTEMD_TIME_INTERFACE = "org.freedesktop.timedate1";
Lei YUa7417132017-02-23 15:24:05 +080020constexpr auto METHOD_SET_NTP = "SetNTP";
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050021} // namespace
Lei YU415b9642017-02-09 11:37:26 +080022
23namespace phosphor
24{
25namespace time
26{
27
28using namespace phosphor::logging;
29
Brad Bishop4e845392018-12-18 18:13:12 -050030Manager::Manager(sdbusplus::bus::bus& bus) : bus(bus), settings(bus)
Lei YU415b9642017-02-09 11:37:26 +080031{
Lei YU710d49b2017-08-01 17:10:17 +080032 using namespace sdbusplus::bus::match::rules;
33 settingsMatches.emplace_back(
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050034 bus, propertiesChanged(settings.timeSyncMethod, settings::timeSyncIntf),
35 std::bind(std::mem_fn(&Manager::onSettingsChanged), this,
36 std::placeholders::_1));
Lei YU710d49b2017-08-01 17:10:17 +080037
Lei YU7f4fca52017-02-23 15:15:51 +080038 // Check the settings daemon to process the new settings
Lei YU710d49b2017-08-01 17:10:17 +080039 auto mode = getSetting(settings.timeSyncMethod.c_str(),
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050040 settings::timeSyncIntf, PROPERTY_TIME_MODE);
Lei YU710d49b2017-08-01 17:10:17 +080041
42 onPropertyChanged(PROPERTY_TIME_MODE, mode);
Lei YU415b9642017-02-09 11:37:26 +080043}
44
Lei YU415b9642017-02-09 11:37:26 +080045void Manager::onPropertyChanged(const std::string& key,
46 const std::string& value)
47{
George Liu0a704522020-04-13 14:51:40 +080048 assert(key == PROPERTY_TIME_MODE);
49
50 // Notify listeners
51 setCurrentTimeMode(value);
52 onTimeModeChanged(value);
Lei YU415b9642017-02-09 11:37:26 +080053}
54
Lei YU710d49b2017-08-01 17:10:17 +080055int Manager::onSettingsChanged(sdbusplus::message::message& msg)
56{
57 using Interface = std::string;
58 using Property = std::string;
59 using Value = std::string;
Patrick Williamsc09ac3f2020-05-13 18:01:29 -050060 using Properties = std::map<Property, std::variant<Value>>;
Lei YU710d49b2017-08-01 17:10:17 +080061
62 Interface interface;
63 Properties properties;
64
65 msg.read(interface, properties);
66
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050067 for (const auto& p : properties)
Lei YU710d49b2017-08-01 17:10:17 +080068 {
Patrick Williams5b746c72020-05-13 11:49:35 -050069 onPropertyChanged(p.first, std::get<std::string>(p.second));
Lei YU710d49b2017-08-01 17:10:17 +080070 }
71
72 return 0;
73}
74
Lei YUa7417132017-02-23 15:24:05 +080075void Manager::updateNtpSetting(const std::string& value)
76{
Lei YU710d49b2017-08-01 17:10:17 +080077 bool isNtp =
78 (value == "xyz.openbmc_project.Time.Synchronization.Method.NTP");
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050079 auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE, SYSTEMD_TIME_PATH,
80 SYSTEMD_TIME_INTERFACE, METHOD_SET_NTP);
Lei YUa7417132017-02-23 15:24:05 +080081 method.append(isNtp, false); // isNtp: 'true/false' means Enable/Disable
82 // 'false' meaning no policy-kit
83
Lei YU89efe6e2018-07-24 10:38:01 +080084 try
Lei YUa7417132017-02-23 15:24:05 +080085 {
Lei YU89efe6e2018-07-24 10:38:01 +080086 bus.call_noreply(method);
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050087 log<level::INFO>("Updated NTP setting", entry("ENABLED=%d", isNtp));
Lei YUa7417132017-02-23 15:24:05 +080088 }
Patrick Williams295b96b2021-09-02 09:50:14 -050089 catch (const sdbusplus::exception::exception& ex)
Lei YUa7417132017-02-23 15:24:05 +080090 {
Lei YU89efe6e2018-07-24 10:38:01 +080091 log<level::ERR>("Failed to update NTP setting",
92 entry("ERR=%s", ex.what()));
Lei YUa7417132017-02-23 15:24:05 +080093 }
94}
95
Lei YUa5003ce2017-02-24 15:35:25 +080096bool Manager::setCurrentTimeMode(const std::string& mode)
Lei YU415b9642017-02-09 11:37:26 +080097{
Lei YUddd54422017-04-18 16:38:44 +080098 auto newMode = utils::strToMode(mode);
Lei YUa5003ce2017-02-24 15:35:25 +080099 if (newMode != timeMode)
100 {
101 log<level::INFO>("Time mode is changed",
102 entry("MODE=%s", mode.c_str()));
103 timeMode = newMode;
Lei YUa5003ce2017-02-24 15:35:25 +0800104 return true;
105 }
106 else
107 {
108 return false;
109 }
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{
121 std::string settingManager = utils::getService(bus, path, interface);
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500122 return utils::getProperty<std::string>(bus, settingManager.c_str(), path,
123 interface, setting);
Lei YU710d49b2017-08-01 17:10:17 +0800124}
125
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500126} // namespace time
127} // namespace phosphor