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