xref: /openbmc/phosphor-time-manager/manager.hpp (revision e101030b37eb8f64616a33eb0ef71d67a19bf7d7)
1415b964fSLei YU #pragma once
2415b964fSLei YU 
31cd4248dSLei YU #include "config.h"
4ab4cc6a5SGunnar Mills 
5415b964fSLei YU #include "property_change_listener.hpp"
6710d49beSLei YU #include "settings.hpp"
7ab4cc6a5SGunnar Mills #include "types.hpp"
8415b964fSLei YU 
9415b964fSLei YU #include <sdbusplus/bus.hpp>
10415b964fSLei YU #include <sdbusplus/bus/match.hpp>
11c6d33972SGeorge Liu 
12415b964fSLei YU #include <string>
13415b964fSLei YU 
14415b964fSLei YU namespace phosphor
15415b964fSLei YU {
16415b964fSLei YU namespace time
17415b964fSLei YU {
18415b964fSLei YU 
19415b964fSLei YU /** @class Manager
20415b964fSLei YU  *  @brief The manager to handle OpenBMC time.
21415b964fSLei YU  *  @details It registers various time related settings and properties signals
22415b964fSLei YU  *  on DBus and handle the changes.
23415b964fSLei YU  *  For certain properties it also notifies the changed events to listeners.
24415b964fSLei YU  */
25415b964fSLei YU class Manager
26415b964fSLei YU {
27415b964fSLei YU   public:
28415b964fSLei YU     friend class TestManager;
297f4fca55SLei YU 
3038679266SPatrick Williams     explicit Manager(sdbusplus::bus_t& bus);
31c6fe8693SLei YU     Manager(const Manager&) = delete;
32c6fe8693SLei YU     Manager& operator=(const Manager&) = delete;
33c6fe8693SLei YU     Manager(Manager&&) = delete;
34c6fe8693SLei YU     Manager& operator=(Manager&&) = delete;
35710d49beSLei YU     ~Manager() = default;
36415b964fSLei YU 
setTimeMode(Mode mode)37cb421097SGeorge Liu     void setTimeMode(Mode mode)
38cb421097SGeorge Liu     {
39cb421097SGeorge Liu         this->timeMode = mode;
40cb421097SGeorge Liu     }
41cb421097SGeorge Liu 
getTimeMode()42cb421097SGeorge Liu     Mode getTimeMode()
43cb421097SGeorge Liu     {
44cb421097SGeorge Liu         return this->timeMode;
45cb421097SGeorge Liu     }
46cb421097SGeorge Liu 
47415b964fSLei YU   private:
48415b964fSLei YU     /** @brief Persistent sdbusplus DBus connection */
4938679266SPatrick Williams     sdbusplus::bus_t& bus;
50415b964fSLei YU 
51*e101030bSJason Zhu     /** @brief The match of systemd timedate property change */
52*e101030bSJason Zhu     std::vector<sdbusplus::bus::match_t> timedateMatches;
53*e101030bSJason Zhu 
54415b964fSLei YU     /** @brief The match of settings property change */
5538679266SPatrick Williams     std::vector<sdbusplus::bus::match_t> settingsMatches;
56710d49beSLei YU 
57710d49beSLei YU     /** @brief Settings objects of intereset */
58710d49beSLei YU     settings::Objects settings;
59710d49beSLei YU 
60415b964fSLei YU     /** @brief The current time mode */
611cd4248dSLei YU     Mode timeMode = DEFAULT_TIME_MODE;
62415b964fSLei YU 
63710d49beSLei YU     /** @brief Get setting from settingsd service
64710d49beSLei YU      *
65710d49beSLei YU      * @param[in] path - The dbus object path
66710d49beSLei YU      * @param[in] interface - The dbus interface
67710d49beSLei YU      * @param[in] setting - The string of the setting
68710d49beSLei YU      *
69710d49beSLei YU      * @return The setting value in string
70710d49beSLei YU      */
71ab4cc6a5SGunnar Mills     std::string getSetting(const char* path, const char* interface,
72710d49beSLei YU                            const char* setting) const;
73710d49beSLei YU 
74a5003cebSLei YU     /** @brief Set current time mode from the time mode string
75a5003cebSLei YU      *
76a5003cebSLei YU      * @param[in] mode - The string of time mode
77a5003cebSLei YU      *
78a5003cebSLei YU      * @return - true if the mode is updated
79a5003cebSLei YU      *           false if it's the same as before
80a5003cebSLei YU      */
81a5003cebSLei YU     bool setCurrentTimeMode(const std::string& mode);
82a5003cebSLei YU 
83a5003cebSLei YU     /** @brief Called on time mode is changed
84a5003cebSLei YU      *
85a5003cebSLei YU      * Notify listeners that time mode is changed and update ntp setting
86415b964fSLei YU      *
87415b964fSLei YU      * @param[in] mode - The string of time mode
88415b964fSLei YU      */
89a5003cebSLei YU     void onTimeModeChanged(const std::string& mode);
90415b964fSLei YU 
91*e101030bSJason Zhu     /** @brief Callback to handle change in NTP
92*e101030bSJason Zhu      *
93*e101030bSJason Zhu      *  @param[in] msg - sdbusplus dbusmessage
94*e101030bSJason Zhu      *
95*e101030bSJason Zhu      *  @return 0 on success, < 0 on failure.
96*e101030bSJason Zhu      */
97*e101030bSJason Zhu     int onTimedateChanged(sdbusplus::message_t& msg);
98*e101030bSJason Zhu 
99710d49beSLei YU     /** @brief Callback to handle change in a setting
100710d49beSLei YU      *
101710d49beSLei YU      *  @param[in] msg - sdbusplus dbusmessage
102710d49beSLei YU      *
103710d49beSLei YU      *  @return 0 on success, < 0 on failure.
104710d49beSLei YU      */
10538679266SPatrick Williams     int onSettingsChanged(sdbusplus::message_t& msg);
106710d49beSLei YU 
107415b964fSLei YU     /** @brief Notified on settings property changed
108415b964fSLei YU      *
109415b964fSLei YU      * @param[in] key - The name of property that is changed
110415b964fSLei YU      * @param[in] value - The value of the property
111*e101030bSJason Zhu      * @param[in] forceSet - true: Force sync NTP Settings to systemd time
112*e101030bSJason Zhu      *                             service, only be used during initialization
113*e101030bSJason Zhu      *                       false: This is default value
114415b964fSLei YU      */
115*e101030bSJason Zhu     void onPropertyChanged(const std::string& key, const std::string& value,
116*e101030bSJason Zhu                            bool forceSet = false);
117415b964fSLei YU 
118a741713cSLei YU     /** @brief Update the NTP setting to systemd time service
119a741713cSLei YU      *
120a741713cSLei YU      * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
121a741713cSLei YU      */
122a741713cSLei YU     void updateNtpSetting(const std::string& value);
123a741713cSLei YU 
124415b964fSLei YU     /** @brief The static function called on settings property changed
125415b964fSLei YU      *
126415b964fSLei YU      * @param[in] msg - Data associated with subscribed signal
127415b964fSLei YU      * @param[in] userData - Pointer to this object instance
128415b964fSLei YU      * @param[out] retError  - Not used but required with signal API
129415b964fSLei YU      */
130ab4cc6a5SGunnar Mills     static int onPropertyChanged(sd_bus_message* msg, void* userData,
131415b964fSLei YU                                  sd_bus_error* retError);
132415b964fSLei YU 
1337f4fca55SLei YU     /** @brief The string of time mode property */
134864e173eSPavithra Barithaya     static constexpr auto propertyTimeMode = "TimeSyncMethod";
135415b964fSLei YU };
136415b964fSLei YU 
137ab4cc6a5SGunnar Mills } // namespace time
138ab4cc6a5SGunnar Mills } // namespace phosphor
139