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