blob: 67f326098de9f10eaca62b36bbae05dbf0628dd0 [file] [log] [blame]
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -06001#!/usr/bin/python -u
2
3import gobject
4import dbus
5import dbus.service
6import dbus.mainloop.glib
Adriana Kobylak41a925e2016-01-28 16:44:27 -06007import os
8import os.path as path
Brad Bishop5ef7fe62016-05-17 08:39:17 -04009from obmc.dbuslib.bindings import DbusProperties, get_dbus
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060010import settings_file as s
11
12DBUS_NAME = 'org.openbmc.settings.Host'
13OBJ_NAME = '/org/openbmc/settings/host0'
14CONTROL_INTF = 'org.openbmc.Settings'
15
Brad Bishop2a9fe662016-08-31 12:37:35 -040016
Brad Bishop5ef7fe62016-05-17 08:39:17 -040017class HostSettingsObject(DbusProperties):
Adriana Kobylak41a925e2016-01-28 16:44:27 -060018 def __init__(self, bus, name, settings, path):
Brad Bishop5ef7fe62016-05-17 08:39:17 -040019 DbusProperties.__init__(self)
Adriana Kobylak41a925e2016-01-28 16:44:27 -060020 dbus.service.Object.__init__(self, bus, name)
21
22 self.path = path
23 if not os.path.exists(path):
24 os.mkdir(path)
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060025
26 # Listen to changes in the property values and sync them to the BMC
Brad Bishop2a9fe662016-08-31 12:37:35 -040027 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 Kobylak4c60e5e2016-01-10 15:22:45 -060032
33 # Create the dbus properties
Adriana Kobylak41a925e2016-01-28 16:44:27 -060034 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 Kobylak4c60e5e2016-01-10 15:22:45 -060039
Adriana Kobylak41a925e2016-01-28 16:44:27 -060040 def get_bmc_value(self, name):
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060041 try:
Adriana Kobylak41a925e2016-01-28 16:44:27 -060042 with open(path.join(self.path, name), 'r') as f:
43 return f.read()
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060044 except (IOError):
45 pass
Adriana Kobylak41a925e2016-01-28 16:44:27 -060046 return None
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060047
Brad Bishop2a9fe662016-08-31 12:37:35 -040048 # 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 Kobylak41a925e2016-01-28 16:44:27 -060052 def set_settings_property(self, name, type, value):
53 bmcv = self.get_bmc_value(name)
54 if bmcv:
55 value = bmcv
Brad Bishop2a9fe662016-08-31 12:37:35 -040056 if type == "i":
Adriana Kobylak41a925e2016-01-28 16:44:27 -060057 self.Set(DBUS_NAME, name, value)
Brad Bishop2a9fe662016-08-31 12:37:35 -040058 elif type == "s":
Adriana Kobylak41a925e2016-01-28 16:44:27 -060059 self.Set(DBUS_NAME, name, str(value))
Brad Bishop2a9fe662016-08-31 12:37:35 -040060 elif type == "b":
tomjose8eb691f2016-03-28 14:52:34 -050061 self.Set(DBUS_NAME, name, value)
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060062
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 Kobylak41a925e2016-01-28 16:44:27 -060065 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 Kobylak4c60e5e2016-01-10 15:22:45 -060069 with open(filepath, 'w') as f:
Adriana Kobylak41a925e2016-01-28 16:44:27 -060070 f.write(str(value))
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060071
72 # Signal handler for when one ore more settings properties were updated.
73 # This will sync the changes to the BMC.
Brad Bishop2a9fe662016-08-31 12:37:35 -040074 def settings_signal_handler(
75 self, interface_name, changed_properties, invalidated_properties):
Adriana Kobylak41a925e2016-01-28 16:44:27 -060076 for name, value in changed_properties.items():
77 self.set_system_settings(name, value)
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060078
79 # Placeholder signal. Needed to register the settings interface.
Adriana Kobylak41a925e2016-01-28 16:44:27 -060080 @dbus.service.signal(DBUS_NAME, signature='s')
81 def SettingsUpdated(self, sname):
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060082 pass
83
84if __name__ == '__main__':
85 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
86
Brad Bishop5ef7fe62016-05-17 08:39:17 -040087 bus = get_dbus()
Adriana Kobylak41a925e2016-01-28 16:44:27 -060088 obj = HostSettingsObject(bus, OBJ_NAME, s.SETTINGS, "/var/lib/obmc/")
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060089 mainloop = gobject.MainLoop()
90
Brad Bishop2cbef3d2016-08-31 12:40:13 -040091 obj.unmask_signals()
92 name = dbus.service.BusName(DBUS_NAME, bus)
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060093 print "Running HostSettingsService"
94 mainloop.run()