blob: 263dbfb726e3f42d3e2daef4933d8e5f03fdad20 [file] [log] [blame]
Lei YU415b9642017-02-09 11:37:26 +08001#pragma once
2
Lei YU1cd42482018-07-24 10:40:59 +08003#include "config.h"
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05004
Lei YU415b9642017-02-09 11:37:26 +08005#include "property_change_listener.hpp"
Lei YU710d49b2017-08-01 17:10:17 +08006#include "settings.hpp"
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05007#include "types.hpp"
Lei YU415b9642017-02-09 11:37:26 +08008
9#include <sdbusplus/bus.hpp>
10#include <sdbusplus/bus/match.hpp>
George Liuc6d33972020-06-22 10:35:29 +080011
Lei YU415b9642017-02-09 11:37:26 +080012#include <string>
13
14namespace phosphor
15{
16namespace time
17{
18
19/** @class Manager
20 * @brief The manager to handle OpenBMC time.
21 * @details It registers various time related settings and properties signals
22 * on DBus and handle the changes.
23 * For certain properties it also notifies the changed events to listeners.
24 */
25class Manager
26{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050027 public:
28 friend class TestManager;
Lei YU7f4fca52017-02-23 15:15:51 +080029
Patrick Williams38679262022-07-22 19:26:55 -050030 explicit Manager(sdbusplus::bus_t& bus);
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050031 Manager(const Manager&) = delete;
32 Manager& operator=(const Manager&) = delete;
33 Manager(Manager&&) = delete;
34 Manager& operator=(Manager&&) = delete;
35 ~Manager() = default;
Lei YU415b9642017-02-09 11:37:26 +080036
George Liucb421092022-08-16 17:02:31 +080037 void setTimeMode(Mode mode)
38 {
39 this->timeMode = mode;
40 }
41
42 Mode getTimeMode()
43 {
44 return this->timeMode;
45 }
46
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050047 private:
48 /** @brief Persistent sdbusplus DBus connection */
Patrick Williams38679262022-07-22 19:26:55 -050049 sdbusplus::bus_t& bus;
Lei YU415b9642017-02-09 11:37:26 +080050
Jason Zhue1010302024-04-02 04:20:43 +000051 /** @brief The match of systemd timedate property change */
52 std::vector<sdbusplus::bus::match_t> timedateMatches;
53
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050054 /** @brief The match of settings property change */
Patrick Williams38679262022-07-22 19:26:55 -050055 std::vector<sdbusplus::bus::match_t> settingsMatches;
Lei YU710d49b2017-08-01 17:10:17 +080056
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050057 /** @brief Settings objects of intereset */
58 settings::Objects settings;
Lei YU710d49b2017-08-01 17:10:17 +080059
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050060 /** @brief The current time mode */
61 Mode timeMode = DEFAULT_TIME_MODE;
Lei YU415b9642017-02-09 11:37:26 +080062
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050063 /** @brief Get setting from settingsd service
64 *
65 * @param[in] path - The dbus object path
66 * @param[in] interface - The dbus interface
67 * @param[in] setting - The string of the setting
68 *
69 * @return The setting value in string
70 */
71 std::string getSetting(const char* path, const char* interface,
72 const char* setting) const;
Lei YU710d49b2017-08-01 17:10:17 +080073
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050074 /** @brief Set current time mode from the time mode string
75 *
76 * @param[in] mode - The string of time mode
77 *
78 * @return - true if the mode is updated
79 * false if it's the same as before
80 */
81 bool setCurrentTimeMode(const std::string& mode);
Lei YUa5003ce2017-02-24 15:35:25 +080082
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050083 /** @brief Called on time mode is changed
84 *
85 * Notify listeners that time mode is changed and update ntp setting
86 *
87 * @param[in] mode - The string of time mode
88 */
89 void onTimeModeChanged(const std::string& mode);
Lei YU415b9642017-02-09 11:37:26 +080090
Jason Zhue1010302024-04-02 04:20:43 +000091 /** @brief Callback to handle change in NTP
92 *
93 * @param[in] msg - sdbusplus dbusmessage
94 *
95 * @return 0 on success, < 0 on failure.
96 */
97 int onTimedateChanged(sdbusplus::message_t& msg);
98
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050099 /** @brief Callback to handle change in a setting
100 *
101 * @param[in] msg - sdbusplus dbusmessage
102 *
103 * @return 0 on success, < 0 on failure.
104 */
Patrick Williams38679262022-07-22 19:26:55 -0500105 int onSettingsChanged(sdbusplus::message_t& msg);
Lei YU710d49b2017-08-01 17:10:17 +0800106
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500107 /** @brief Notified on settings property changed
108 *
109 * @param[in] key - The name of property that is changed
110 * @param[in] value - The value of the property
Jason Zhue1010302024-04-02 04:20:43 +0000111 * @param[in] forceSet - true: Force sync NTP Settings to systemd time
112 * service, only be used during initialization
113 * false: This is default value
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500114 */
Jason Zhue1010302024-04-02 04:20:43 +0000115 void onPropertyChanged(const std::string& key, const std::string& value,
116 bool forceSet = false);
Lei YU415b9642017-02-09 11:37:26 +0800117
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500118 /** @brief Update the NTP setting to systemd time service
119 *
120 * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
121 */
122 void updateNtpSetting(const std::string& value);
Lei YUa7417132017-02-23 15:24:05 +0800123
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500124 /** @brief The static function called on settings property changed
125 *
126 * @param[in] msg - Data associated with subscribed signal
127 * @param[in] userData - Pointer to this object instance
128 * @param[out] retError - Not used but required with signal API
129 */
130 static int onPropertyChanged(sd_bus_message* msg, void* userData,
131 sd_bus_error* retError);
Lei YU415b9642017-02-09 11:37:26 +0800132
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500133 /** @brief The string of time mode property */
Pavithra Barithaya864e1732023-04-11 04:30:23 -0500134 static constexpr auto propertyTimeMode = "TimeSyncMethod";
Lei YU415b9642017-02-09 11:37:26 +0800135};
136
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500137} // namespace time
138} // namespace phosphor