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::bus& 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   private:
38     /** @brief Persistent sdbusplus DBus connection */
39     sdbusplus::bus::bus& bus;
40 
41     /** @brief The match of settings property change */
42     std::vector<sdbusplus::bus::match::match> settingsMatches;
43 
44     /** @brief Settings objects of intereset */
45     settings::Objects settings;
46 
47     /** @brief The current time mode */
48     Mode timeMode = DEFAULT_TIME_MODE;
49 
50     /** @brief Get setting from settingsd service
51      *
52      * @param[in] path - The dbus object path
53      * @param[in] interface - The dbus interface
54      * @param[in] setting - The string of the setting
55      *
56      * @return The setting value in string
57      */
58     std::string getSetting(const char* path, const char* interface,
59                            const char* setting) const;
60 
61     /** @brief Set current time mode from the time mode string
62      *
63      * @param[in] mode - The string of time mode
64      *
65      * @return - true if the mode is updated
66      *           false if it's the same as before
67      */
68     bool setCurrentTimeMode(const std::string& mode);
69 
70     /** @brief Called on time mode is changed
71      *
72      * Notify listeners that time mode is changed and update ntp setting
73      *
74      * @param[in] mode - The string of time mode
75      */
76     void onTimeModeChanged(const std::string& mode);
77 
78     /** @brief Callback to handle change in a setting
79      *
80      *  @param[in] msg - sdbusplus dbusmessage
81      *
82      *  @return 0 on success, < 0 on failure.
83      */
84     int onSettingsChanged(sdbusplus::message::message& msg);
85 
86     /** @brief Notified on settings property changed
87      *
88      * @param[in] key - The name of property that is changed
89      * @param[in] value - The value of the property
90      */
91     void onPropertyChanged(const std::string& key, const std::string& value);
92 
93     /** @brief Update the NTP setting to systemd time service
94      *
95      * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
96      */
97     void updateNtpSetting(const std::string& value);
98 
99     /** @brief The static function called on settings property changed
100      *
101      * @param[in] msg - Data associated with subscribed signal
102      * @param[in] userData - Pointer to this object instance
103      * @param[out] retError  - Not used but required with signal API
104      */
105     static int onPropertyChanged(sd_bus_message* msg, void* userData,
106                                  sd_bus_error* retError);
107 
108     /** @brief The string of time mode property */
109     static constexpr auto PROPERTY_TIME_MODE = "TimeSyncMethod";
110 };
111 
112 } // namespace time
113 } // namespace phosphor
114