Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame^] | 1 | #!/usr/bin/python -u |
| 2 | |
| 3 | import gobject |
| 4 | import dbus |
| 5 | import dbus.service |
| 6 | import dbus.mainloop.glib |
| 7 | import Openbmc |
| 8 | import settings_file as s |
| 9 | |
| 10 | DBUS_NAME = 'org.openbmc.settings.Host' |
| 11 | OBJ_NAME = '/org/openbmc/settings/host0' |
| 12 | CONTROL_INTF = 'org.openbmc.Settings' |
| 13 | |
| 14 | # TODO Save settings in tmp until persistant storage is available |
| 15 | # Path where the settings are stored in the BMC |
| 16 | SETTINGS_PATH = '/tmp/' |
| 17 | |
| 18 | class HostSettingsObject(Openbmc.DbusProperties): |
| 19 | def __init__(self,bus,name): |
| 20 | Openbmc.DbusProperties.__init__(self) |
| 21 | dbus.service.Object.__init__(self,bus,name) |
| 22 | |
| 23 | # Listen to changes in the property values and sync them to the BMC |
| 24 | bus.add_signal_receiver(self.settings_signal_handler, |
| 25 | dbus_interface = "org.freedesktop.DBus.Properties", |
| 26 | signal_name = "PropertiesChanged", |
| 27 | path = "/org/openbmc/settings/host0") |
| 28 | |
| 29 | # Create the dbus properties |
| 30 | for i in s.SETTINGS['host'].iterkeys(): |
| 31 | self.sname = s.SETTINGS['host'][i]['name'] |
| 32 | self.stype = s.SETTINGS['host'][i]['type'] |
| 33 | self.svalue = s.SETTINGS['host'][i]['default'] |
| 34 | self.bmcvalue = self.svalue # Default BMC value to file value |
| 35 | self.set_settings_property() |
| 36 | |
| 37 | # Check if the requested value is the same as the current one in the BMC |
| 38 | def check_settings_need_update(self): |
| 39 | filepath = SETTINGS_PATH + self.sname |
| 40 | update = True |
| 41 | try: |
| 42 | with open(filepath, 'r') as f: |
| 43 | self.bmcvalue = f.read() # Upate BMC value with value on system |
| 44 | if self.bmcvalue == self.svalue: |
| 45 | update = False |
| 46 | except (IOError): |
| 47 | pass |
| 48 | return update |
| 49 | |
| 50 | # Create dbus properties based on bmc value. This will be either a value |
| 51 | # previously set, or the default file value if the BMC value does not exist. |
| 52 | def set_settings_property(self): |
| 53 | update = self.check_settings_need_update() |
| 54 | if update == True: |
| 55 | self.svalue = self.bmcvalue # Update svalue with the value that will be used |
| 56 | if self.stype=="i": |
| 57 | self.Set(DBUS_NAME,self.sname,self.svalue) |
| 58 | elif self.stype=="s": |
| 59 | self.Set(DBUS_NAME,self.sname,str(self.svalue)) |
| 60 | |
| 61 | # Save the settings to the BMC. This will write the settings value in |
| 62 | # individual files named by the property name to the BMC. |
| 63 | def set_system_settings(self): |
| 64 | update = self.check_settings_need_update() |
| 65 | if update == True: |
| 66 | filepath = SETTINGS_PATH + self.sname |
| 67 | with open(filepath, 'w') as f: |
| 68 | f.write(str(self.svalue)) |
| 69 | |
| 70 | # Signal handler for when one ore more settings properties were updated. |
| 71 | # This will sync the changes to the BMC. |
| 72 | def settings_signal_handler(self, interface_name, changed_properties, invalidated_properties): |
| 73 | data = changed_properties |
| 74 | for i in data: |
| 75 | self.sname = i |
| 76 | self.svalue = data[i] |
| 77 | self.set_system_settings() |
| 78 | |
| 79 | # Placeholder signal. Needed to register the settings interface. |
| 80 | @dbus.service.signal(DBUS_NAME,signature='s') |
| 81 | def SettingsUpdated(self,sname): |
| 82 | pass |
| 83 | |
| 84 | if __name__ == '__main__': |
| 85 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 86 | |
| 87 | bus = Openbmc.getDBus() |
| 88 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 89 | obj = HostSettingsObject(bus, OBJ_NAME) |
| 90 | mainloop = gobject.MainLoop() |
| 91 | |
| 92 | print "Running HostSettingsService" |
| 93 | mainloop.run() |
| 94 | |