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 |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 7 | import os |
| 8 | import os.path as path |
Brad Bishop | 5ef7fe6 | 2016-05-17 08:39:17 -0400 | [diff] [blame^] | 9 | from obmc.dbuslib.bindings import DbusProperties, get_dbus |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 10 | import settings_file as s |
| 11 | |
| 12 | DBUS_NAME = 'org.openbmc.settings.Host' |
| 13 | OBJ_NAME = '/org/openbmc/settings/host0' |
| 14 | CONTROL_INTF = 'org.openbmc.Settings' |
| 15 | |
Brad Bishop | 5ef7fe6 | 2016-05-17 08:39:17 -0400 | [diff] [blame^] | 16 | class HostSettingsObject(DbusProperties): |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 17 | def __init__(self, bus, name, settings, path): |
Brad Bishop | 5ef7fe6 | 2016-05-17 08:39:17 -0400 | [diff] [blame^] | 18 | DbusProperties.__init__(self) |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 19 | dbus.service.Object.__init__(self, bus, name) |
| 20 | |
| 21 | self.path = path |
| 22 | if not os.path.exists(path): |
| 23 | os.mkdir(path) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 24 | |
| 25 | # Listen to changes in the property values and sync them to the BMC |
| 26 | bus.add_signal_receiver(self.settings_signal_handler, |
| 27 | dbus_interface = "org.freedesktop.DBus.Properties", |
| 28 | signal_name = "PropertiesChanged", |
| 29 | path = "/org/openbmc/settings/host0") |
| 30 | |
| 31 | # Create the dbus properties |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 32 | for i in settings['host'].iterkeys(): |
| 33 | shk = settings['host'][i] |
| 34 | self.set_settings_property(shk['name'], |
| 35 | shk['type'], |
| 36 | shk['default']) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 37 | |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 38 | def get_bmc_value(self, name): |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 39 | try: |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 40 | with open(path.join(self.path, name), 'r') as f: |
| 41 | return f.read() |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 42 | except (IOError): |
| 43 | pass |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 44 | return None |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 45 | |
| 46 | # Create dbus properties based on bmc value. This will be either a value |
| 47 | # previously set, or the default file value if the BMC value does not exist. |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 48 | def set_settings_property(self, name, type, value): |
| 49 | bmcv = self.get_bmc_value(name) |
| 50 | if bmcv: |
| 51 | value = bmcv |
| 52 | if type=="i": |
| 53 | self.Set(DBUS_NAME, name, value) |
| 54 | elif type=="s": |
| 55 | self.Set(DBUS_NAME, name, str(value)) |
tomjose | 8eb691f | 2016-03-28 14:52:34 -0500 | [diff] [blame] | 56 | elif type=="b": |
| 57 | self.Set(DBUS_NAME, name, value) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 58 | |
| 59 | # Save the settings to the BMC. This will write the settings value in |
| 60 | # individual files named by the property name to the BMC. |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 61 | def set_system_settings(self, name, value): |
| 62 | bmcv = self.get_bmc_value(name) |
| 63 | if bmcv != value: |
| 64 | filepath = path.join(self.path, name) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 65 | with open(filepath, 'w') as f: |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 66 | f.write(str(value)) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 67 | |
| 68 | # Signal handler for when one ore more settings properties were updated. |
| 69 | # This will sync the changes to the BMC. |
| 70 | def settings_signal_handler(self, interface_name, changed_properties, invalidated_properties): |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 71 | for name, value in changed_properties.items(): |
| 72 | self.set_system_settings(name, value) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 73 | |
| 74 | # Placeholder signal. Needed to register the settings interface. |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 75 | @dbus.service.signal(DBUS_NAME, signature='s') |
| 76 | def SettingsUpdated(self, sname): |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 77 | pass |
| 78 | |
| 79 | if __name__ == '__main__': |
| 80 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 81 | |
Brad Bishop | 5ef7fe6 | 2016-05-17 08:39:17 -0400 | [diff] [blame^] | 82 | bus = get_dbus() |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 83 | name = dbus.service.BusName(DBUS_NAME, bus) |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 84 | obj = HostSettingsObject(bus, OBJ_NAME, s.SETTINGS, "/var/lib/obmc/") |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 85 | mainloop = gobject.MainLoop() |
| 86 | |
| 87 | print "Running HostSettingsService" |
| 88 | mainloop.run() |
| 89 | |