blob: ceaed7e237f557a1a264fd7911b84bcebd1dd197 [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>
George Liu947b5342022-07-01 16:12:18 +08009#include <phosphor-logging/lg2.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);
George Liu947b5342022-07-01 16:12:18 +080087 lg2::info("Updated NTP setting: {ENABLED}", "ENABLED", 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 {
George Liu947b5342022-07-01 16:12:18 +080091 lg2::error("Failed to update NTP setting: {ERROR}", "ERROR", ex);
Lei YUa7417132017-02-23 15:24:05 +080092 }
93}
94
Lei YUa5003ce2017-02-24 15:35:25 +080095bool Manager::setCurrentTimeMode(const std::string& mode)
Lei YU415b9642017-02-09 11:37:26 +080096{
Lei YUddd54422017-04-18 16:38:44 +080097 auto newMode = utils::strToMode(mode);
Lei YUa5003ce2017-02-24 15:35:25 +080098 if (newMode != timeMode)
99 {
George Liu947b5342022-07-01 16:12:18 +0800100 lg2::info("Time mode has been changed to {MODE}", "MODE", newMode);
Lei YUa5003ce2017-02-24 15:35:25 +0800101 timeMode = newMode;
Lei YUa5003ce2017-02-24 15:35:25 +0800102 return true;
103 }
104 else
105 {
106 return false;
107 }
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{
119 std::string settingManager = utils::getService(bus, path, interface);
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500120 return utils::getProperty<std::string>(bus, settingManager.c_str(), path,
121 interface, setting);
Lei YU710d49b2017-08-01 17:10:17 +0800122}
123
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500124} // namespace time
125} // namespace phosphor