Merge pull request #2 from anoo1/persistent

Add system_state dbus property
diff --git a/settings.yaml b/settings.yaml
index 8bd9543..a8cecb6 100644
--- a/settings.yaml
+++ b/settings.yaml
@@ -12,3 +12,7 @@
         name: boot_flags
         type: s
         default: "0000000000"
+    sysstate:
+        name: system_state
+        type: s
+        default: ""
diff --git a/settings_file.py b/settings_file.py
index 85f04f0..e05bc31 100644
--- a/settings_file.py
+++ b/settings_file.py
@@ -1,2 +1,2 @@
 #!/usr/bin/python -u
-SETTINGS={'host': {'bootflags': {'default': '0000000000', 'type': 's', 'name': 'boot_flags'}, 'powercap': {'name': 'power_cap', 'min': 0, 'default': 0, 'max': 1000, 'type': 'i', 'unit': 'watts'}}}
\ No newline at end of file
+SETTINGS={'host': {'bootflags': {'default': '0000000000', 'type': 's', 'name': 'boot_flags'}, 'powercap': {'name': 'power_cap', 'min': 0, 'default': 0, 'max': 1000, 'type': 'i', 'unit': 'watts'}, 'sysstate': {'default': '', 'type': 's', 'name': 'system_state'}}}
\ No newline at end of file
diff --git a/settings_manager.py b/settings_manager.py
index 53ce7fb..e49b466 100755
--- a/settings_manager.py
+++ b/settings_manager.py
@@ -4,6 +4,8 @@
 import dbus
 import dbus.service
 import dbus.mainloop.glib
+import os
+import os.path as path
 import Openbmc
 import settings_file as s
 
@@ -11,14 +13,14 @@
 OBJ_NAME = '/org/openbmc/settings/host0'
 CONTROL_INTF = 'org.openbmc.Settings'
 
-# TODO Save settings in tmp until persistant storage is available
-# Path where the settings are stored in the BMC
-SETTINGS_PATH = '/tmp/'
-
 class HostSettingsObject(Openbmc.DbusProperties):
-    def __init__(self,bus,name):
+    def __init__(self, bus, name, settings, path):
         Openbmc.DbusProperties.__init__(self)
-        dbus.service.Object.__init__(self,bus,name)
+        dbus.service.Object.__init__(self, bus, name)
+
+        self.path = path
+        if not os.path.exists(path):
+            os.mkdir(path)
 
         # Listen to changes in the property values and sync them to the BMC
         bus.add_signal_receiver(self.settings_signal_handler,
@@ -27,58 +29,49 @@
             path = "/org/openbmc/settings/host0")
 
         # Create the dbus properties
-        for i in s.SETTINGS['host'].iterkeys():
-            self.sname = s.SETTINGS['host'][i]['name']
-            self.stype = s.SETTINGS['host'][i]['type']
-            self.svalue = s.SETTINGS['host'][i]['default']
-            self.bmcvalue = self.svalue # Default BMC value to file value
-            self.set_settings_property()
+        for i in settings['host'].iterkeys():
+            shk = settings['host'][i]
+            self.set_settings_property(shk['name'],
+                                       shk['type'],
+                                       shk['default'])
 
-    # Check if the requested value is the same as the current one in the BMC
-    def check_settings_need_update(self):
-        filepath = SETTINGS_PATH + self.sname
-        update = True
+    def get_bmc_value(self, name):
         try:
-            with open(filepath, 'r') as f:
-                self.bmcvalue = f.read() # Upate BMC value with value on system
-                if self.bmcvalue == self.svalue:
-                    update = False
+            with open(path.join(self.path, name), 'r') as f:
+                return f.read()
         except (IOError):
             pass
-        return update
+        return None
 
     # Create dbus properties based on bmc value. This will be either a value
     # previously set, or the default file value if the BMC value does not exist.
-    def set_settings_property(self):
-        update = self.check_settings_need_update()
-        if update == True:
-            self.svalue = self.bmcvalue # Update svalue with the value that will be used 
-            if self.stype=="i":
-                self.Set(DBUS_NAME,self.sname,self.svalue)
-            elif self.stype=="s":
-                self.Set(DBUS_NAME,self.sname,str(self.svalue))
+    def set_settings_property(self, name, type, value):
+        bmcv = self.get_bmc_value(name)
+        if bmcv:
+            value = bmcv
+        if type=="i":
+            self.Set(DBUS_NAME, name, value)
+        elif type=="s":
+            self.Set(DBUS_NAME, name, str(value))
 
     # Save the settings to the BMC. This will write the settings value in
     # individual files named by the property name to the BMC.
-    def set_system_settings(self):
-        update = self.check_settings_need_update()
-        if update == True:
-            filepath = SETTINGS_PATH + self.sname
+    def set_system_settings(self, name, value):
+        bmcv = self.get_bmc_value(name)
+        if bmcv != value:
+            filepath = path.join(self.path, name)
             with open(filepath, 'w') as f:
-                f.write(str(self.svalue))
+                f.write(str(value))
 
     # Signal handler for when one ore more settings properties were updated.
     # This will sync the changes to the BMC.
     def settings_signal_handler(self, interface_name, changed_properties, invalidated_properties):
-        data = changed_properties                                           
-        for i in data:                                                      
-            self.sname = i
-            self.svalue = data[i]
-            self.set_system_settings()
+        for name, value in changed_properties.items():
+            self.set_system_settings(name, value)
 
     # Placeholder signal. Needed to register the settings interface.
-    @dbus.service.signal(DBUS_NAME,signature='s')
-    def SettingsUpdated(self,sname):
+    @dbus.service.signal(DBUS_NAME, signature='s')
+    def SettingsUpdated(self, sname):
         pass
 
 if __name__ == '__main__':
@@ -86,7 +79,7 @@
 
     bus = Openbmc.getDBus()
     name = dbus.service.BusName(DBUS_NAME, bus)
-    obj = HostSettingsObject(bus, OBJ_NAME)
+    obj = HostSettingsObject(bus, OBJ_NAME, s.SETTINGS, "/var/lib/obmc/")
     mainloop = gobject.MainLoop()
 
     print "Running HostSettingsService"