Use new settings API

The new time manager code was using the old settings daemon.
Now it uses the new settings API.

Change-Id: Id551d97c28a6cfbb81c87118b26292b1b5574e93
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/Makefile.am b/Makefile.am
index bf7a559..09e8807 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,6 +17,7 @@
 	host_epoch.cpp \
 	manager.cpp \
 	utils.cpp \
+	settings.cpp \
 	${generated_source}
 
 phosphor_timemanager_SOURCES = \
diff --git a/README.md b/README.md
index d86559d..5365349 100644
--- a/README.md
+++ b/README.md
@@ -39,10 +39,10 @@
 Getting BMC or HOST time is always allowed, but setting the time may not be
 allowed depending on below two settings in settings manager.
 
-* time_mode
+* TimeSyncMethod
    * NTP: Time is set via NTP server.
    * MANUAL: Time is set manually.
-* time_owner
+* TimeOwner
    * BMC: BMC owns the time and can set the time.
    * HOST: Host owns the time and can set the time.
    * SPLIT: BMC and Host owns separate time.
diff --git a/manager.cpp b/manager.cpp
index 0fedd09..fc2ca65 100644
--- a/manager.cpp
+++ b/manager.cpp
@@ -55,14 +55,33 @@
       propertyChangeMatch(bus, MATCH_PROPERTY_CHANGE, onPropertyChanged, this),
       pgoodChangeMatch(bus, MATCH_PGOOD_CHANGE, onPgoodChanged, this)
 {
+    using namespace sdbusplus::bus::match::rules;
+    settingsMatches.emplace_back(
+        bus,
+        propertiesChanged(settings.timeOwner, settings::timeOwnerIntf),
+        std::bind(std::mem_fn(&Manager::onSettingsChanged),
+                  this, std::placeholders::_1));
+    settingsMatches.emplace_back(
+        bus,
+        propertiesChanged(settings.timeSyncMethod, settings::timeSyncIntf),
+        std::bind(std::mem_fn(&Manager::onSettingsChanged),
+          this, std::placeholders::_1));
+
     checkHostOn();
 
     // Restore settings from persistent storage
     restoreSettings();
 
     // Check the settings daemon to process the new settings
-    onPropertyChanged(PROPERTY_TIME_MODE, getSettings(PROPERTY_TIME_MODE));
-    onPropertyChanged(PROPERTY_TIME_OWNER, getSettings(PROPERTY_TIME_OWNER));
+    auto mode = getSetting(settings.timeSyncMethod.c_str(),
+                           settings::timeSyncIntf,
+                           PROPERTY_TIME_MODE);
+    auto owner = getSetting(settings.timeOwner.c_str(),
+                            settings::timeOwnerIntf,
+                            PROPERTY_TIME_OWNER);
+
+    onPropertyChanged(PROPERTY_TIME_MODE, mode);
+    onPropertyChanged(PROPERTY_TIME_OWNER, owner);
 
     checkDhcpNtp();
 }
@@ -164,6 +183,26 @@
     return 0;
 }
 
+int Manager::onSettingsChanged(sdbusplus::message::message& msg)
+{
+    using Interface = std::string;
+    using Property = std::string;
+    using Value = std::string;
+    using Properties = std::map<Property, sdbusplus::message::variant<Value>>;
+
+    Interface interface;
+    Properties properties;
+
+    msg.read(interface, properties);
+
+    for(const auto& p : properties)
+    {
+        onPropertyChanged(p.first, p.second.get<std::string>());
+    }
+
+    return 0;
+}
+
 void Manager::setPropertyAsRequested(const std::string& key,
                                      const std::string& value)
 {
@@ -199,7 +238,8 @@
 
 void Manager::updateNtpSetting(const std::string& value)
 {
-    bool isNtp = (value == "NTP");
+    bool isNtp =
+        (value == "xyz.openbmc_project.Time.Synchronization.Method.NTP");
     auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE,
                                       SYSTEMD_TIME_PATH,
                                       SYSTEMD_TIME_INTERFACE,
@@ -340,6 +380,9 @@
     }
 }
 
+// TODO: This function is here only for use_dhcp_ntp.
+// When use_dhcp_ntp is transferred to new settings daemon,
+// this function can be removed.
 std::string Manager::getSettings(const char* setting) const
 {
     std::string settingsService = utils::getService(bus,
@@ -353,5 +396,17 @@
                                            setting);
 }
 
+std::string Manager::getSetting(const char* path,
+                                const char* interface,
+                                const char* setting) const
+{
+    std::string settingManager = utils::getService(bus, path, interface);
+    return utils::getProperty<std::string>(bus,
+                                           settingManager.c_str(),
+                                           path,
+                                           interface,
+                                           setting);
+}
+
 }
 }
diff --git a/manager.hpp b/manager.hpp
index 9b135d9..dc0885e 100644
--- a/manager.hpp
+++ b/manager.hpp
@@ -2,6 +2,7 @@
 
 #include "types.hpp"
 #include "property_change_listener.hpp"
+#include "settings.hpp"
 
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/bus/match.hpp>
@@ -30,6 +31,7 @@
         Manager& operator=(const Manager&) = delete;
         Manager(Manager&&) = delete;
         Manager& operator=(Manager&&) = delete;
+        ~Manager() = default;
 
         /** @brief Add a listener that will be called
           * when property is changed
@@ -41,14 +43,22 @@
         sdbusplus::bus::bus& bus;
 
         /** @brief The match of settings property change */
+        // TODO: This is to be removed when all properties are handled in
+        // new settings daemon
         sdbusplus::bus::match::match propertyChangeMatch;
 
+        /** @brief The match of settings property change */
+        std::vector<sdbusplus::bus::match::match> settingsMatches;
+
         /** @brief The match of pgood change */
         sdbusplus::bus::match::match pgoodChangeMatch;
 
         /** @brief The container to hold all the listeners */
         std::set<PropertyChangeListner*> listeners;
 
+        /** @brief Settings objects of intereset */
+        settings::Objects settings;
+
         /** @brief The value to indicate if host is on */
         bool hostOn = false;
 
@@ -81,6 +91,18 @@
          */
         std::string getSettings(const char* setting) const;
 
+        /** @brief Get setting from settingsd service
+         *
+         * @param[in] path - The dbus object path
+         * @param[in] interface - The dbus interface
+         * @param[in] setting - The string of the setting
+         *
+         * @return The setting value in string
+         */
+        std::string getSetting(const char* path,
+                               const char* interface,
+                               const char* setting) const;
+
         /** @brief Set current time mode from the time mode string
          *
          * @param[in] mode - The string of time mode
@@ -113,6 +135,14 @@
          */
         void onTimeOwnerChanged();
 
+        /** @brief Callback to handle change in a setting
+         *
+         *  @param[in] msg - sdbusplus dbusmessage
+         *
+         *  @return 0 on success, < 0 on failure.
+         */
+        int onSettingsChanged(sdbusplus::message::message& msg);
+
         /** @brief Notified on settings property changed
          *
          * @param[in] key - The name of property that is changed
@@ -182,10 +212,10 @@
                                   sd_bus_error* retError);
 
         /** @brief The string of time mode property */
-        static constexpr auto PROPERTY_TIME_MODE = "time_mode";
+        static constexpr auto PROPERTY_TIME_MODE = "TimeSyncMethod";
 
         /** @brief The string of time owner property */
-        static constexpr auto PROPERTY_TIME_OWNER = "time_owner";
+        static constexpr auto PROPERTY_TIME_OWNER = "TimeOwner";
 
         /** @brief The string of use dhcp ntp property */
         static constexpr auto PROPERTY_DHCP_NTP = "use_dhcp_ntp";
diff --git a/test/TestManager.cpp b/test/TestManager.cpp
index d87ca39..d8c51ca 100644
--- a/test/TestManager.cpp
+++ b/test/TestManager.cpp
@@ -31,7 +31,7 @@
         }
 
         // Proxies for Manager's private members and functions
-         Mode getTimeMode()
+        Mode getTimeMode()
         {
             return manager.timeMode;
         }
@@ -94,10 +94,10 @@
     EXPECT_CALL(listener2, onOwnerChanged(Owner::Host)).Times(1);
 
     notifyPropertyChanged(
-        "time_mode",
+        "TimeSyncMethod",
         "xyz.openbmc_project.Time.Synchronization.Method.Manual");
     notifyPropertyChanged(
-        "time_owner",
+        "TimeOwner",
         "xyz.openbmc_project.Time.Owner.Owners.Host");
 
     EXPECT_EQ("", getRequestedMode());
@@ -113,10 +113,10 @@
     EXPECT_CALL(listener2, onOwnerChanged(Owner::Host)).Times(0);
 
     notifyPropertyChanged(
-        "time_mode",
+        "TimeSyncMethod",
         "xyz.openbmc_project.Time.Synchronization.Method.NTP");
     notifyPropertyChanged(
-        "time_owner",
+        "TimeOwner",
         "xyz.openbmc_project.Time.Owner.Owners.Split");
 
     EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP",
@@ -147,10 +147,10 @@
 {
     // Property is now MANUAL/HOST
     notifyPropertyChanged(
-        "time_mode",
+        "TimeSyncMethod",
         "xyz.openbmc_project.Time.Synchronization.Method.Manual");
     notifyPropertyChanged(
-        "time_owner",
+        "TimeOwner",
         "xyz.openbmc_project.Time.Owner.Owners.Host");
 
     // Set host on
@@ -163,10 +163,10 @@
     EXPECT_CALL(listener2, onOwnerChanged(_)).Times(0);
 
     notifyPropertyChanged(
-        "time_mode",
+        "TimeSyncMethod",
         "xyz.openbmc_project.Time.Synchronization.Method.NTP");
     notifyPropertyChanged(
-        "time_owner",
+        "TimeOwner",
         "xyz.openbmc_project.Time.Owner.Owners.Split");
 
     // Saved as requested mode/owner
@@ -177,10 +177,10 @@
 
     // Property changed back to MANUAL/HOST
     notifyPropertyChanged(
-        "time_mode",
+        "TimeSyncMethod",
         "xyz.openbmc_project.Time.Synchronization.Method.Manual");
     notifyPropertyChanged(
-        "time_owner",
+        "TimeOwner",
         "xyz.openbmc_project.Time.Owner.Owners.Host");
 
     // Requested mode/owner shall be updated