blob: 864ee9dd560bb9a403109a27bb2e2e28debe8f7b [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
Adriana Kobylak256be782016-08-24 15:43:16 -05009import sys
Brad Bishop5ef7fe62016-05-17 08:39:17 -040010from obmc.dbuslib.bindings import DbusProperties, get_dbus
Adriana Kobylak256be782016-08-24 15:43:16 -050011
12settings_file_path = os.path.join(sys.prefix, 'share/obmc-phosphor-settings')
13sys.path.insert(1, settings_file_path)
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060014import settings_file as s
15
16DBUS_NAME = 'org.openbmc.settings.Host'
17OBJ_NAME = '/org/openbmc/settings/host0'
18CONTROL_INTF = 'org.openbmc.Settings'
19
Brad Bishop2a9fe662016-08-31 12:37:35 -040020
Brad Bishop5ef7fe62016-05-17 08:39:17 -040021class HostSettingsObject(DbusProperties):
Adriana Kobylak41a925e2016-01-28 16:44:27 -060022 def __init__(self, bus, name, settings, path):
Brad Bishop5ef7fe62016-05-17 08:39:17 -040023 DbusProperties.__init__(self)
Adriana Kobylak41a925e2016-01-28 16:44:27 -060024 dbus.service.Object.__init__(self, bus, name)
25
26 self.path = path
27 if not os.path.exists(path):
28 os.mkdir(path)
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060029
30 # Listen to changes in the property values and sync them to the BMC
Brad Bishop2a9fe662016-08-31 12:37:35 -040031 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 Kobylak4c60e5e2016-01-10 15:22:45 -060036
37 # Create the dbus properties
Adriana Kobylak41a925e2016-01-28 16:44:27 -060038 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 Kobylak4c60e5e2016-01-10 15:22:45 -060043
Adriana Kobylak41a925e2016-01-28 16:44:27 -060044 def get_bmc_value(self, name):
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060045 try:
Adriana Kobylak41a925e2016-01-28 16:44:27 -060046 with open(path.join(self.path, name), 'r') as f:
47 return f.read()
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060048 except (IOError):
49 pass
Adriana Kobylak41a925e2016-01-28 16:44:27 -060050 return None
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060051
Brad Bishop2a9fe662016-08-31 12:37:35 -040052 # 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 Kobylak41a925e2016-01-28 16:44:27 -060056 def set_settings_property(self, name, type, value):
57 bmcv = self.get_bmc_value(name)
58 if bmcv:
59 value = bmcv
Brad Bishop2a9fe662016-08-31 12:37:35 -040060 if type == "i":
Adriana Kobylak41a925e2016-01-28 16:44:27 -060061 self.Set(DBUS_NAME, name, value)
Brad Bishop2a9fe662016-08-31 12:37:35 -040062 elif type == "s":
Adriana Kobylak41a925e2016-01-28 16:44:27 -060063 self.Set(DBUS_NAME, name, str(value))
Brad Bishop2a9fe662016-08-31 12:37:35 -040064 elif type == "b":
tomjose8eb691f2016-03-28 14:52:34 -050065 self.Set(DBUS_NAME, name, value)
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060066
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 Kobylak41a925e2016-01-28 16:44:27 -060069 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 Kobylak4c60e5e2016-01-10 15:22:45 -060073 with open(filepath, 'w') as f:
Adriana Kobylak41a925e2016-01-28 16:44:27 -060074 f.write(str(value))
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060075
76 # Signal handler for when one ore more settings properties were updated.
77 # This will sync the changes to the BMC.
Brad Bishop2a9fe662016-08-31 12:37:35 -040078 def settings_signal_handler(
79 self, interface_name, changed_properties, invalidated_properties):
Adriana Kobylak41a925e2016-01-28 16:44:27 -060080 for name, value in changed_properties.items():
81 self.set_system_settings(name, value)
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060082
83 # Placeholder signal. Needed to register the settings interface.
Adriana Kobylak41a925e2016-01-28 16:44:27 -060084 @dbus.service.signal(DBUS_NAME, signature='s')
85 def SettingsUpdated(self, sname):
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060086 pass
87
88if __name__ == '__main__':
89 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
90
Brad Bishop5ef7fe62016-05-17 08:39:17 -040091 bus = get_dbus()
Adriana Kobylak41a925e2016-01-28 16:44:27 -060092 obj = HostSettingsObject(bus, OBJ_NAME, s.SETTINGS, "/var/lib/obmc/")
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060093 mainloop = gobject.MainLoop()
94
Brad Bishop2cbef3d2016-08-31 12:40:13 -040095 obj.unmask_signals()
96 name = dbus.service.BusName(DBUS_NAME, bus)
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060097 print "Running HostSettingsService"
98 mainloop.run()