xref: /openbmc/phosphor-time-manager/manager.hpp (revision 864e173e246383b5bb9c08917d286eed540639e4)
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 
37cb421097SGeorge Liu     void setTimeMode(Mode mode)
38cb421097SGeorge Liu     {
39cb421097SGeorge Liu         this->timeMode = mode;
40cb421097SGeorge Liu     }
41cb421097SGeorge Liu 
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 
51415b964fSLei YU     /** @brief The match of settings property change */
5238679266SPatrick Williams     std::vector<sdbusplus::bus::match_t> settingsMatches;
53710d49beSLei YU 
54710d49beSLei YU     /** @brief Settings objects of intereset */
55710d49beSLei YU     settings::Objects settings;
56710d49beSLei YU 
57415b964fSLei YU     /** @brief The current time mode */
581cd4248dSLei YU     Mode timeMode = DEFAULT_TIME_MODE;
59415b964fSLei YU 
60710d49beSLei YU     /** @brief Get setting from settingsd service
61710d49beSLei YU      *
62710d49beSLei YU      * @param[in] path - The dbus object path
63710d49beSLei YU      * @param[in] interface - The dbus interface
64710d49beSLei YU      * @param[in] setting - The string of the setting
65710d49beSLei YU      *
66710d49beSLei YU      * @return The setting value in string
67710d49beSLei YU      */
68ab4cc6a5SGunnar Mills     std::string getSetting(const char* path, const char* interface,
69710d49beSLei YU                            const char* setting) const;
70710d49beSLei YU 
71a5003cebSLei YU     /** @brief Set current time mode from the time mode string
72a5003cebSLei YU      *
73a5003cebSLei YU      * @param[in] mode - The string of time mode
74a5003cebSLei YU      *
75a5003cebSLei YU      * @return - true if the mode is updated
76a5003cebSLei YU      *           false if it's the same as before
77a5003cebSLei YU      */
78a5003cebSLei YU     bool setCurrentTimeMode(const std::string& mode);
79a5003cebSLei YU 
80a5003cebSLei YU     /** @brief Called on time mode is changed
81a5003cebSLei YU      *
82a5003cebSLei YU      * Notify listeners that time mode is changed and update ntp setting
83415b964fSLei YU      *
84415b964fSLei YU      * @param[in] mode - The string of time mode
85415b964fSLei YU      */
86a5003cebSLei YU     void onTimeModeChanged(const std::string& mode);
87415b964fSLei YU 
88710d49beSLei YU     /** @brief Callback to handle change in a setting
89710d49beSLei YU      *
90710d49beSLei YU      *  @param[in] msg - sdbusplus dbusmessage
91710d49beSLei YU      *
92710d49beSLei YU      *  @return 0 on success, < 0 on failure.
93710d49beSLei YU      */
9438679266SPatrick Williams     int onSettingsChanged(sdbusplus::message_t& msg);
95710d49beSLei YU 
96415b964fSLei YU     /** @brief Notified on settings property changed
97415b964fSLei YU      *
98415b964fSLei YU      * @param[in] key - The name of property that is changed
99415b964fSLei YU      * @param[in] value - The value of the property
100415b964fSLei YU      */
101ab4cc6a5SGunnar Mills     void onPropertyChanged(const std::string& key, const std::string& value);
102415b964fSLei YU 
103a741713cSLei YU     /** @brief Update the NTP setting to systemd time service
104a741713cSLei YU      *
105a741713cSLei YU      * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
106a741713cSLei YU      */
107a741713cSLei YU     void updateNtpSetting(const std::string& value);
108a741713cSLei YU 
109415b964fSLei YU     /** @brief The static function called on settings property changed
110415b964fSLei YU      *
111415b964fSLei YU      * @param[in] msg - Data associated with subscribed signal
112415b964fSLei YU      * @param[in] userData - Pointer to this object instance
113415b964fSLei YU      * @param[out] retError  - Not used but required with signal API
114415b964fSLei YU      */
115ab4cc6a5SGunnar Mills     static int onPropertyChanged(sd_bus_message* msg, void* userData,
116415b964fSLei YU                                  sd_bus_error* retError);
117415b964fSLei YU 
1187f4fca55SLei YU     /** @brief The string of time mode property */
119*864e173eSPavithra Barithaya     static constexpr auto propertyTimeMode = "TimeSyncMethod";
120415b964fSLei YU };
121415b964fSLei YU 
122ab4cc6a5SGunnar Mills } // namespace time
123ab4cc6a5SGunnar Mills } // namespace phosphor
124