1 #pragma once
2 
3 #include "config.h"
4 
5 #include "property_change_listener.hpp"
6 #include "settings.hpp"
7 #include "types.hpp"
8 
9 #include <sdbusplus/bus.hpp>
10 #include <sdbusplus/bus/match.hpp>
11 
12 #include <string>
13 
14 namespace phosphor
15 {
16 namespace 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  */
25 class Manager
26 {
27   public:
28     friend class TestManager;
29 
30     explicit Manager(sdbusplus::bus_t& bus);
31     Manager(const Manager&) = delete;
32     Manager& operator=(const Manager&) = delete;
33     Manager(Manager&&) = delete;
34     Manager& operator=(Manager&&) = delete;
35     ~Manager() = default;
36 
37     void setTimeMode(Mode mode)
38     {
39         this->timeMode = mode;
40     }
41 
42     Mode getTimeMode()
43     {
44         return this->timeMode;
45     }
46 
47   private:
48     /** @brief Persistent sdbusplus DBus connection */
49     sdbusplus::bus_t& bus;
50 
51     /** @brief The match of settings property change */
52     std::vector<sdbusplus::bus::match_t> settingsMatches;
53 
54     /** @brief Settings objects of intereset */
55     settings::Objects settings;
56 
57     /** @brief The current time mode */
58     Mode timeMode = DEFAULT_TIME_MODE;
59 
60     /** @brief Get setting from settingsd service
61      *
62      * @param[in] path - The dbus object path
63      * @param[in] interface - The dbus interface
64      * @param[in] setting - The string of the setting
65      *
66      * @return The setting value in string
67      */
68     std::string getSetting(const char* path, const char* interface,
69                            const char* setting) const;
70 
71     /** @brief Set current time mode from the time mode string
72      *
73      * @param[in] mode - The string of time mode
74      *
75      * @return - true if the mode is updated
76      *           false if it's the same as before
77      */
78     bool setCurrentTimeMode(const std::string& mode);
79 
80     /** @brief Called on time mode is changed
81      *
82      * Notify listeners that time mode is changed and update ntp setting
83      *
84      * @param[in] mode - The string of time mode
85      */
86     void onTimeModeChanged(const std::string& mode);
87 
88     /** @brief Callback to handle change in a setting
89      *
90      *  @param[in] msg - sdbusplus dbusmessage
91      *
92      *  @return 0 on success, < 0 on failure.
93      */
94     int onSettingsChanged(sdbusplus::message_t& msg);
95 
96     /** @brief Notified on settings property changed
97      *
98      * @param[in] key - The name of property that is changed
99      * @param[in] value - The value of the property
100      */
101     void onPropertyChanged(const std::string& key, const std::string& value);
102 
103     /** @brief Update the NTP setting to systemd time service
104      *
105      * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
106      */
107     void updateNtpSetting(const std::string& value);
108 
109     /** @brief The static function called on settings property changed
110      *
111      * @param[in] msg - Data associated with subscribed signal
112      * @param[in] userData - Pointer to this object instance
113      * @param[out] retError  - Not used but required with signal API
114      */
115     static int onPropertyChanged(sd_bus_message* msg, void* userData,
116                                  sd_bus_error* retError);
117 
118     /** @brief The string of time mode property */
119     static constexpr auto propertyTimeMode = "TimeSyncMethod";
120 };
121 
122 } // namespace time
123 } // namespace phosphor
124