blob: 3d5cf14a70bb4e99b7057987811d2cede130c321 [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 Kobylak4c60e5e2016-01-10 15:22:45 -06009import Openbmc
10import settings_file as s
11
12DBUS_NAME = 'org.openbmc.settings.Host'
13OBJ_NAME = '/org/openbmc/settings/host0'
14CONTROL_INTF = 'org.openbmc.Settings'
15
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060016class HostSettingsObject(Openbmc.DbusProperties):
Adriana Kobylak41a925e2016-01-28 16:44:27 -060017 def __init__(self, bus, name, settings, path):
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060018 Openbmc.DbusProperties.__init__(self)
Adriana Kobylak41a925e2016-01-28 16:44:27 -060019 dbus.service.Object.__init__(self, bus, name)
20
21 self.path = path
22 if not os.path.exists(path):
23 os.mkdir(path)
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060024
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 Kobylak41a925e2016-01-28 16:44:27 -060032 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 Kobylak4c60e5e2016-01-10 15:22:45 -060037
Adriana Kobylak41a925e2016-01-28 16:44:27 -060038 def get_bmc_value(self, name):
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060039 try:
Adriana Kobylak41a925e2016-01-28 16:44:27 -060040 with open(path.join(self.path, name), 'r') as f:
41 return f.read()
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060042 except (IOError):
43 pass
Adriana Kobylak41a925e2016-01-28 16:44:27 -060044 return None
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060045
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 Kobylak41a925e2016-01-28 16:44:27 -060048 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))
tomjose8eb691f2016-03-28 14:52:34 -050056 elif type=="b":
57 self.Set(DBUS_NAME, name, value)
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060058
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 Kobylak41a925e2016-01-28 16:44:27 -060061 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 Kobylak4c60e5e2016-01-10 15:22:45 -060065 with open(filepath, 'w') as f:
Adriana Kobylak41a925e2016-01-28 16:44:27 -060066 f.write(str(value))
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060067
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 Kobylak41a925e2016-01-28 16:44:27 -060071 for name, value in changed_properties.items():
72 self.set_system_settings(name, value)
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060073
74 # Placeholder signal. Needed to register the settings interface.
Adriana Kobylak41a925e2016-01-28 16:44:27 -060075 @dbus.service.signal(DBUS_NAME, signature='s')
76 def SettingsUpdated(self, sname):
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060077 pass
78
79if __name__ == '__main__':
80 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
81
82 bus = Openbmc.getDBus()
83 name = dbus.service.BusName(DBUS_NAME, bus)
Adriana Kobylak41a925e2016-01-28 16:44:27 -060084 obj = HostSettingsObject(bus, OBJ_NAME, s.SETTINGS, "/var/lib/obmc/")
Adriana Kobylak4c60e5e2016-01-10 15:22:45 -060085 mainloop = gobject.MainLoop()
86
87 print "Running HostSettingsService"
88 mainloop.run()
89