Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import subprocess |
| 4 | import gobject |
| 5 | import dbus |
| 6 | import dbus.service |
| 7 | import dbus.mainloop.glib |
| 8 | import xml.etree.ElementTree as ET |
| 9 | import System |
| 10 | |
| 11 | DBUS_NAME = 'org.openbmc.managers.System' |
| 12 | OBJ_NAME = '/org/openbmc/managers/System' |
| 13 | |
| 14 | gpio_dev = '/sys/class/gpio' |
| 15 | process_config = System.BarreleyeProcesses() |
| 16 | gpio_config = System.BarreleyeGpios() |
| 17 | |
| 18 | def findConfigInstance(bus_name,obj_path): |
| 19 | line = obj_path.split('/') |
| 20 | instance_name = line[len(line)-1] |
| 21 | if (process_config.has_key(bus_name) == False): |
| 22 | return {} |
| 23 | for instance in process_config[bus_name]['instances']: |
| 24 | if (instance['name'] == instance_name): |
| 25 | return instance |
| 26 | |
| 27 | def parseIntrospection(bus_name,obj_name,interfaces): |
| 28 | obj = bus.get_object(bus_name, obj_name) |
| 29 | introspect_iface = dbus.Interface(obj,'org.freedesktop.DBus.Introspectable') |
| 30 | tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect())) |
| 31 | root = tree.getroot() |
| 32 | interfaces[obj_name] = [] |
| 33 | for intf in root.iter('interface'): |
| 34 | intf_name = intf.attrib['name'] |
| 35 | if (intf_name == 'org.freedesktop.DBus.ObjectManager'): |
| 36 | manager = dbus.Interface(obj,'org.freedesktop.DBus.ObjectManager') |
| 37 | for managed_obj in manager.GetManagedObjects(): |
| 38 | parseIntrospection(bus_name,managed_obj,interfaces) |
| 39 | elif (intf_name.find('org.openbmc') == 0): |
| 40 | interfaces[obj_name].append(intf_name) |
| 41 | |
| 42 | |
| 43 | class SystemManager(dbus.service.Object): |
| 44 | def __init__(self,bus,name): |
| 45 | dbus.service.Object.__init__(self,bus,name) |
| 46 | bus.add_signal_receiver(self.request_name, |
| 47 | dbus_interface = 'org.freedesktop.DBus', |
| 48 | signal_name = "NameOwnerChanged") |
| 49 | # launch dbus object processes |
| 50 | for bus_name in process_config.keys(): |
| 51 | exe_name = process_config[bus_name]['exe_name'] |
| 52 | cmdline = [ ] |
| 53 | cmdline.append(exe_name) |
| 54 | for instance in process_config[bus_name]['instances']: |
| 55 | cmdline.append(instance['name']) |
| 56 | subprocess.Popen(cmdline); |
| 57 | |
| 58 | gobject.timeout_add(5000, self.heartbeat_check) |
| 59 | |
| 60 | def heartbeat_check(self): |
| 61 | print "heartbeat check" |
| 62 | for bus_name in process_config.keys(): |
| 63 | if (process_config[bus_name]['heartbeat'] == 'yes'): |
| 64 | if (process_config[bus_name]['heartbeat_count'] == 0): |
| 65 | print "Heartbeat error: "+bus_name |
| 66 | else: |
| 67 | process_config[bus_name]['heartbeat_count'] == 0 |
| 68 | print "Heartbeat ok" |
| 69 | |
| 70 | def heartbeat_update(self,bus_name): |
| 71 | process_config[bus_name]['heartbeat_count']=1 |
| 72 | |
| 73 | def setup_sensor(self,intf): |
| 74 | pass |
| 75 | |
| 76 | def request_name(self, bus_name, a, b): |
| 77 | if (len(b) > 0 and bus_name.find('org.openbmc') == 0): |
| 78 | try: |
| 79 | if (process_config.has_key(bus_name)): |
| 80 | process_config[bus_name]['heartbeat_count'] = 0 |
| 81 | obj_name = "/"+bus_name.replace('.','/') |
| 82 | interfaces = {} |
| 83 | parseIntrospection(bus_name,obj_name,interfaces) |
| 84 | for obj_path in interfaces.keys(): |
| 85 | instance = findConfigInstance(bus_name,obj_path) |
| 86 | for intf_name in interfaces[obj_path]: |
| 87 | obj = bus.get_object(bus_name,obj_path) |
| 88 | intf = dbus.Interface(obj,intf_name) |
| 89 | if (intf_name == 'org.openbmc.SensorIntegerThreshold'): |
| 90 | intf.set(instance['lower_critical'], |
| 91 | instance['lower_warning'], |
| 92 | instance['upper_warning'], |
| 93 | instance['upper_critical']) |
| 94 | |
| 95 | if (intf_name == 'org.openbmc.SensorInteger'): |
| 96 | if (process_config[bus_name]['heartbeat'] == 'yes'): |
| 97 | bus.add_signal_receiver(self.heartbeat_update, |
| 98 | dbus_interface = intf_name, |
| 99 | signal_name = "Heartbeat") |
| 100 | |
| 101 | if (instance.has_key('parameters')): |
| 102 | intf.setConfigData(instance['parameters']) |
| 103 | |
| 104 | except Exception as e: |
| 105 | print e |
| 106 | |
| 107 | if (len(b)==0 and bus_name.find('org.openbmc.sensors') == 0): |
| 108 | exe_name = process_config[bus_name]['exe_name'] |
| 109 | cmdline = [ ] |
| 110 | cmdline.append(exe_name) |
| 111 | for instance in process_config[bus_name]['instances']: |
| 112 | cmdline.append(instance['name']) |
| 113 | |
| 114 | subprocess.Popen(cmdline); |
| 115 | |
| 116 | @dbus.service.signal(DBUS_NAME) |
| 117 | def CriticalThreshold(self, obj): |
| 118 | print "Critical: "+obj |
| 119 | |
| 120 | @dbus.service.signal(DBUS_NAME) |
| 121 | def WarningThreshold(self, obj): |
| 122 | print "Warning: "+obj |
| 123 | |
| 124 | @dbus.service.method(DBUS_NAME, |
| 125 | in_signature='s', out_signature='sis') |
| 126 | def gpioInit(self,name): |
| 127 | gpio_path = '' |
| 128 | gpio_num = 0 |
| 129 | if (gpio_config.has_key(name) == False): |
| 130 | # TODO: Error handling |
| 131 | print "ERROR: "+name+" not found in GPIO config table" |
| 132 | return ['',0,''] |
| 133 | else: |
| 134 | gpio_num = gpio_config[name]['gpio_num'] |
| 135 | |
| 136 | return [gpio_dev, gpio_num, gpio_config[name]['direction']] |
| 137 | |
| 138 | |
| 139 | if __name__ == '__main__': |
| 140 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 141 | bus = dbus.SessionBus() |
| 142 | name = dbus.service.BusName(DBUS_NAME,bus) |
| 143 | obj = SystemManager(bus,OBJ_NAME) |
| 144 | mainloop = gobject.MainLoop() |
| 145 | |
| 146 | |
| 147 | print "Running SystemManager" |
| 148 | mainloop.run() |
| 149 | |