Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 3 | import sys |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 4 | import subprocess |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 5 | from gi.repository import GObject |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 6 | import dbus |
| 7 | import dbus.service |
| 8 | import dbus.mainloop.glib |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 9 | import os |
| 10 | import PropertyManager |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 11 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame^] | 12 | import Openbmc |
| 13 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 14 | if (len(sys.argv) < 2): |
| 15 | print "Usage: system_manager.py [system name]" |
| 16 | exit(1) |
| 17 | |
| 18 | System = __import__(sys.argv[1]) |
| 19 | import Openbmc |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 20 | |
| 21 | DBUS_NAME = 'org.openbmc.managers.System' |
| 22 | OBJ_NAME = '/org/openbmc/managers/System' |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 23 | HEARTBEAT_CHECK_INTERVAL = 20000 |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 24 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 25 | |
| 26 | class SystemManager(dbus.service.Object): |
| 27 | def __init__(self,bus,name): |
| 28 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 29 | self.property_manager = PropertyManager.PropertyManager(bus,System.CACHE_PATH) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame^] | 30 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 31 | ## Signal handlers |
| 32 | bus.add_signal_receiver(self.NewBusHandler, |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 33 | dbus_interface = 'org.freedesktop.DBus', |
| 34 | signal_name = "NameOwnerChanged") |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 35 | bus.add_signal_receiver(self.CacheMeHandler, |
| 36 | signal_name = 'CacheMe', path_keyword='path',interface_keyword='interface') |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame^] | 37 | bus.add_signal_receiver(self.HeartbeatHandler, signal_name = "Heartbeat") |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 38 | |
| 39 | try: |
| 40 | # launch dbus object processes |
| 41 | for bus_name in System.SYSTEM_CONFIG.keys(): |
| 42 | self.start_process(bus_name) |
| 43 | except Exception as e: |
| 44 | ## TODO: error handling |
| 45 | pass |
| 46 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame^] | 47 | ## Add poll for heartbeat |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 48 | GObject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame^] | 49 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 50 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 51 | def CacheMeHandler(self,busname,path=None,interface=None): |
| 52 | #interface_name = 'org.openbmc.Fru' |
| 53 | print "CacheME: "+busname+","+path+","+interface |
| 54 | data = {} |
| 55 | cache = System.CACHED_INTERFACES.has_key(interface) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 56 | self.property_manager.saveProperties(busname,path,interface,cache,data) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 57 | |
| 58 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 59 | def start_process(self,bus_name): |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 60 | if (System.SYSTEM_CONFIG[bus_name]['start_process'] == True): |
| 61 | process_name = System.BIN_PATH+System.SYSTEM_CONFIG[bus_name]['process_name'] |
| 62 | cmdline = [ ] |
| 63 | cmdline.append(process_name) |
| 64 | for instance in System.SYSTEM_CONFIG[bus_name]['instances']: |
| 65 | cmdline.append(instance['name']) |
| 66 | try: |
| 67 | print "Starting process: "+" ".join(cmdline) |
| 68 | System.SYSTEM_CONFIG[bus_name]['popen'] = subprocess.Popen(cmdline); |
| 69 | except Exception as e: |
| 70 | ## TODO: error |
| 71 | print "Error starting process: "+" ".join(cmdline) |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 72 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 73 | def heartbeat_check(self): |
| 74 | print "heartbeat check" |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 75 | for bus_name in System.SYSTEM_CONFIG.keys(): |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 76 | if (System.SYSTEM_CONFIG[bus_name]['start_process'] == True): |
| 77 | ## even if process doesn't request heartbeat check, |
| 78 | ## make sure process is still alive |
| 79 | p = System.SYSTEM_CONFIG[bus_name]['popen'] |
| 80 | p.poll() |
| 81 | if (p.returncode != None): |
| 82 | print "Process for "+bus_name+" appears to be dead" |
| 83 | self.start_process(bus_name) |
| 84 | |
| 85 | ## process is alive, now check if heartbeat received |
| 86 | ## during previous interval |
| 87 | elif (System.SYSTEM_CONFIG[bus_name]['heartbeat'] == 'yes'): |
| 88 | if (System.SYSTEM_CONFIG[bus_name]['heartbeat_count'] == 0): |
| 89 | print "Heartbeat error: "+bus_name |
| 90 | p = System.SYSTEM_CONFIG[bus_name]['popen'] |
| 91 | ## TODO: error checking |
| 92 | p.poll() |
| 93 | if (p.returncode == None): |
| 94 | print "Process must be hung, so killing" |
| 95 | p.kill() |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 96 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 97 | self.start_process(bus_name) |
| 98 | else: |
| 99 | System.SYSTEM_CONFIG[bus_name]['heartbeat_count'] = 0 |
| 100 | print "Heartbeat ok: "+bus_name |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 101 | |
| 102 | return True |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 103 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 104 | def HeartbeatHandler(self,bus_name): |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 105 | System.SYSTEM_CONFIG[bus_name]['heartbeat_count']=1 |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 106 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 107 | def NewBusHandler(self, bus_name, a, b): |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 108 | if (len(b) > 0 and bus_name.find(Openbmc.BUS_PREFIX) == 0): |
| 109 | if (System.SYSTEM_CONFIG.has_key(bus_name)): |
| 110 | System.SYSTEM_CONFIG[bus_name]['heartbeat_count'] = 0 |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 111 | obj_root = "/"+bus_name.replace('.','/') |
| 112 | obj_paths = [] |
| 113 | |
| 114 | ## Loads object properties from system config file |
| 115 | ## then overlays saved properties from file |
| 116 | for instance in System.SYSTEM_CONFIG[bus_name]['instances']: |
| 117 | obj_path = obj_root+'/'+instance['name'] |
| 118 | obj_paths.append(obj_path) |
| 119 | if (instance.has_key('properties')): |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame^] | 120 | print "load props: "+obj_path |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 121 | self.property_manager.loadProperties(bus_name,obj_path, instance['properties']) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 122 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame^] | 123 | ## scan all used interfaces and get interfaces with init method |
| 124 | |
| 125 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 126 | ## After object properties are setup, call init method if requested |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame^] | 127 | #if (System.SYSTEM_CONFIG[bus_name].has_key('init_methods')): |
| 128 | for obj_path in obj_paths: |
| 129 | obj = bus.get_object(bus_name,obj_path) |
| 130 | methods = Openbmc.get_methods(obj) |
| 131 | for intf_name in methods.keys(): |
| 132 | if (methods[intf_name].has_key('init')): |
| 133 | intf = dbus.Interface(obj,intf_name) |
| 134 | print "Calling init: " +intf_name |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 135 | intf.init() |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 136 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 137 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 138 | |
| 139 | @dbus.service.method(DBUS_NAME, |
| 140 | in_signature='s', out_signature='sis') |
| 141 | def gpioInit(self,name): |
| 142 | gpio_path = '' |
| 143 | gpio_num = 0 |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 144 | if (System.GPIO_CONFIG.has_key(name) == False): |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 145 | # TODO: Error handling |
| 146 | print "ERROR: "+name+" not found in GPIO config table" |
| 147 | return ['',0,''] |
| 148 | else: |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 149 | gpio_num = System.GPIO_CONFIG[name]['gpio_num'] |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 150 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 151 | return [Openbmc.GPIO_DEV, gpio_num, System.GPIO_CONFIG[name]['direction']] |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 152 | |
| 153 | |
| 154 | if __name__ == '__main__': |
| 155 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 156 | bus = dbus.SessionBus() |
| 157 | name = dbus.service.BusName(DBUS_NAME,bus) |
| 158 | obj = SystemManager(bus,OBJ_NAME) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 159 | mainloop = GObject.MainLoop() |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 160 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 161 | print "Running SystemManager" |
| 162 | mainloop.run() |
| 163 | |