blob: 88fec9f6406c3e9c220420b03826bb90a015caae [file] [log] [blame]
Norman James89de9162015-08-27 21:41:36 -05001#!/usr/bin/env python
2
Norman James471ab592015-08-30 22:29:40 -05003import sys
Norman James89de9162015-08-27 21:41:36 -05004import subprocess
Norman James6f8d0422015-09-14 18:48:00 -05005import gobject
Norman James89de9162015-08-27 21:41:36 -05006import dbus
7import dbus.service
8import dbus.mainloop.glib
Norman James90baede2015-09-02 20:32:49 -05009import os
10import PropertyManager
Norman James88872672015-09-21 16:51:35 -050011import time
12import json
Norman James9e6acf92015-09-08 07:00:04 -050013import Openbmc
14
Norman James471ab592015-08-30 22:29:40 -050015if (len(sys.argv) < 2):
16 print "Usage: system_manager.py [system name]"
17 exit(1)
18
19System = __import__(sys.argv[1])
20import Openbmc
Norman James89de9162015-08-27 21:41:36 -050021
22DBUS_NAME = 'org.openbmc.managers.System'
23OBJ_NAME = '/org/openbmc/managers/System'
Norman James471ab592015-08-30 22:29:40 -050024HEARTBEAT_CHECK_INTERVAL = 20000
Norman James89de9162015-08-27 21:41:36 -050025
Norman James89de9162015-08-27 21:41:36 -050026
27class SystemManager(dbus.service.Object):
28 def __init__(self,bus,name):
29 dbus.service.Object.__init__(self,bus,name)
Norman James5d78b4d2015-09-05 13:34:34 -050030 self.property_manager = PropertyManager.PropertyManager(bus,System.CACHE_PATH)
Norman James9e6acf92015-09-08 07:00:04 -050031
Norman James90baede2015-09-02 20:32:49 -050032 ## Signal handlers
33 bus.add_signal_receiver(self.NewBusHandler,
Norman James89de9162015-08-27 21:41:36 -050034 dbus_interface = 'org.freedesktop.DBus',
35 signal_name = "NameOwnerChanged")
Norman James9e6acf92015-09-08 07:00:04 -050036 bus.add_signal_receiver(self.HeartbeatHandler, signal_name = "Heartbeat")
Norman James362a80f2015-09-14 14:04:39 -050037 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050038
Norman James362a80f2015-09-14 14:04:39 -050039 self.current_state = ""
40 self.system_states = {}
Norman James19e45912015-10-04 20:19:41 -050041 self.bus_name_lookup = {}
42
Norman James362a80f2015-09-14 14:04:39 -050043 for bus_name in System.SYSTEM_CONFIG.keys():
44 sys_state = System.SYSTEM_CONFIG[bus_name]['system_state']
45 if (self.system_states.has_key(sys_state) == False):
46 self.system_states[sys_state] = []
47 self.system_states[sys_state].append(bus_name)
Norman James19e45912015-10-04 20:19:41 -050048
49 ## replace symbolic path in ID_LOOKUP
50 for category in System.ID_LOOKUP:
51 for key in System.ID_LOOKUP[category]:
52 val = System.ID_LOOKUP[category][key]
53 new_val = val.replace("<inventory_root>",System.INVENTORY_ROOT)
54 System.ID_LOOKUP[category][key] = new_val
55
Norman James362a80f2015-09-14 14:04:39 -050056 self.SystemStateHandler("INIT")
57 print "SystemManager Init Done"
58
Norman James362a80f2015-09-14 14:04:39 -050059 def SystemStateHandler(self,state_name):
Norman James65a295a2015-09-26 22:21:10 -050060 print "Checking previous state started..."
61 i = 0
Norman James7caa10e2015-09-27 22:39:52 -050062 started = self.check_state_started()
Norman Jamesdfdaca92015-09-27 22:11:15 -050063 while(i<10 and started == False):
64 started = self.check_state_started()
Norman James65a295a2015-09-26 22:21:10 -050065 i=i+1
Norman Jamesdfdaca92015-09-27 22:11:15 -050066 time.sleep(1)
67
68 if (i == 10):
Norman James8c6d8382015-10-06 07:47:16 -050069 print "ERROR: Timeout waiting for state to finish: "+self.current_state
Norman Jamesdfdaca92015-09-27 22:11:15 -050070 return
Norman James65a295a2015-09-26 22:21:10 -050071
Norman James362a80f2015-09-14 14:04:39 -050072 print "Running System State: "+state_name
73 if (self.system_states.has_key(state_name)):
74 for bus_name in self.system_states[state_name]:
Norman James90baede2015-09-02 20:32:49 -050075 self.start_process(bus_name)
Norman James90baede2015-09-02 20:32:49 -050076
Norman James362a80f2015-09-14 14:04:39 -050077 if (state_name == "INIT"):
78 ## Add poll for heartbeat
Norman James6f8d0422015-09-14 18:48:00 -050079 gobject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check)
Norman James9e6acf92015-09-08 07:00:04 -050080
Norman James362a80f2015-09-14 14:04:39 -050081 if (System.ENTER_STATE_CALLBACK.has_key(state_name)):
82 cb = System.ENTER_STATE_CALLBACK[state_name]
Norman James8c6d8382015-10-06 07:47:16 -050083 obj = bus.get_object(cb['bus_name'],cb['obj_name'])
Norman James362a80f2015-09-14 14:04:39 -050084 method = obj.get_dbus_method(cb['method_name'],cb['interface_name'])
85 method()
Norman James471ab592015-08-30 22:29:40 -050086
Norman James65a295a2015-09-26 22:21:10 -050087 self.current_state = state_name
Norman James19e45912015-10-04 20:19:41 -050088
89 @dbus.service.method(DBUS_NAME,
90 in_signature='ss', out_signature='a{ss}')
91 def getObjectFromId(self,category,key):
92 bus_name = ""
93 obj_path = ""
94
95 if (System.ID_LOOKUP.has_key(category)):
96 if (System.ID_LOOKUP[category].has_key(key)):
97 obj_path = System.ID_LOOKUP[category][key]
98 else:
99 print "ERROR: key not found: "+category+","+key
100
101 if (self.bus_name_lookup.has_key(obj_path)):
102 bus_name = self.bus_name_lookup[obj_path]
103 else:
104 print "ERROR: bus name not found for: "+obj_path
105 r = { 'bus_name' : bus_name, 'obj_path' : obj_path }
106 return r
107
108
Norman James471ab592015-08-30 22:29:40 -0500109 def start_process(self,bus_name):
Norman James5d78b4d2015-09-05 13:34:34 -0500110 if (System.SYSTEM_CONFIG[bus_name]['start_process'] == True):
111 process_name = System.BIN_PATH+System.SYSTEM_CONFIG[bus_name]['process_name']
112 cmdline = [ ]
113 cmdline.append(process_name)
Norman James65a295a2015-09-26 22:21:10 -0500114 System.SYSTEM_CONFIG[bus_name]['popen'] = None
Norman James5d78b4d2015-09-05 13:34:34 -0500115 for instance in System.SYSTEM_CONFIG[bus_name]['instances']:
116 cmdline.append(instance['name'])
117 try:
Norman James362a80f2015-09-14 14:04:39 -0500118 print "Starting process: "+" ".join(cmdline)+": "+bus_name
Norman James65a295a2015-09-26 22:21:10 -0500119 System.SYSTEM_CONFIG[bus_name]['popen'] = subprocess.Popen(cmdline)
Norman James5d78b4d2015-09-05 13:34:34 -0500120 except Exception as e:
121 ## TODO: error
Norman James8c6d8382015-10-06 07:47:16 -0500122 print "ERROR: starting process: "+" ".join(cmdline)
Norman James471ab592015-08-30 22:29:40 -0500123
Norman James89de9162015-08-27 21:41:36 -0500124 def heartbeat_check(self):
Norman James471ab592015-08-30 22:29:40 -0500125 for bus_name in System.SYSTEM_CONFIG.keys():
Norman James362a80f2015-09-14 14:04:39 -0500126 if (System.SYSTEM_CONFIG[bus_name]['start_process'] == True and
Norman James6f8d0422015-09-14 18:48:00 -0500127 System.SYSTEM_CONFIG[bus_name].has_key('popen') and
128 System.SYSTEM_CONFIG[bus_name]['monitor_process'] == True):
Norman James5d78b4d2015-09-05 13:34:34 -0500129 ## even if process doesn't request heartbeat check,
130 ## make sure process is still alive
131 p = System.SYSTEM_CONFIG[bus_name]['popen']
132 p.poll()
133 if (p.returncode != None):
134 print "Process for "+bus_name+" appears to be dead"
135 self.start_process(bus_name)
136
137 ## process is alive, now check if heartbeat received
138 ## during previous interval
139 elif (System.SYSTEM_CONFIG[bus_name]['heartbeat'] == 'yes'):
140 if (System.SYSTEM_CONFIG[bus_name]['heartbeat_count'] == 0):
141 print "Heartbeat error: "+bus_name
142 p = System.SYSTEM_CONFIG[bus_name]['popen']
143 ## TODO: error checking
144 p.poll()
145 if (p.returncode == None):
146 print "Process must be hung, so killing"
147 p.kill()
Norman James471ab592015-08-30 22:29:40 -0500148
Norman James5d78b4d2015-09-05 13:34:34 -0500149 self.start_process(bus_name)
150 else:
151 System.SYSTEM_CONFIG[bus_name]['heartbeat_count'] = 0
Norman James471ab592015-08-30 22:29:40 -0500152
153 return True
Norman James89de9162015-08-27 21:41:36 -0500154
Norman James90baede2015-09-02 20:32:49 -0500155 def HeartbeatHandler(self,bus_name):
Norman James471ab592015-08-30 22:29:40 -0500156 System.SYSTEM_CONFIG[bus_name]['heartbeat_count']=1
Norman James89de9162015-08-27 21:41:36 -0500157
Norman James65a295a2015-09-26 22:21:10 -0500158 def check_state_started(self):
159 r = True
160 if (self.current_state == ""):
161 return True
162 for bus_name in self.system_states[self.current_state]:
163 if (System.SYSTEM_CONFIG[bus_name].has_key('popen') == False and
164 System.SYSTEM_CONFIG[bus_name]['start_process'] == True):
Norman James65a295a2015-09-26 22:21:10 -0500165 r = False
166 break;
167 return r
168
169
Norman James90baede2015-09-02 20:32:49 -0500170 def NewBusHandler(self, bus_name, a, b):
Norman James471ab592015-08-30 22:29:40 -0500171 if (len(b) > 0 and bus_name.find(Openbmc.BUS_PREFIX) == 0):
Norman James19e45912015-10-04 20:19:41 -0500172 objects = {}
173 Openbmc.get_objs(bus,bus_name,"",objects)
174 for instance_name in objects.keys():
175 self.bus_name_lookup[objects[instance_name]['PATH']] = bus_name
176
Norman James471ab592015-08-30 22:29:40 -0500177 if (System.SYSTEM_CONFIG.has_key(bus_name)):
178 System.SYSTEM_CONFIG[bus_name]['heartbeat_count'] = 0
Norman James362a80f2015-09-14 14:04:39 -0500179 for instance_name in objects.keys():
180 obj_path = objects[instance_name]['PATH']
181 for instance in System.SYSTEM_CONFIG[bus_name]['instances']:
182 if (instance.has_key('properties') and instance['name'] == instance_name):
183 props = instance['properties']
184 print "Load Properties: "+obj_path
185 self.property_manager.loadProperties(bus_name,obj_path,props)
Norman James362a80f2015-09-14 14:04:39 -0500186 ## If object has an init method, call it
187 for init_intf in objects[instance_name]['INIT']:
188 obj = bus.get_object(bus_name,obj_path)
189 intf = dbus.Interface(obj,init_intf)
190 print "Calling init method: " +obj_path+" : "+init_intf
191 intf.init()
Norman Jamesdfdaca92015-09-27 22:11:15 -0500192
Norman James89de9162015-08-27 21:41:36 -0500193
194 @dbus.service.method(DBUS_NAME,
195 in_signature='s', out_signature='sis')
196 def gpioInit(self,name):
197 gpio_path = ''
198 gpio_num = 0
Norman James471ab592015-08-30 22:29:40 -0500199 if (System.GPIO_CONFIG.has_key(name) == False):
Norman James89de9162015-08-27 21:41:36 -0500200 # TODO: Error handling
201 print "ERROR: "+name+" not found in GPIO config table"
202 return ['',0,'']
203 else:
Norman James471ab592015-08-30 22:29:40 -0500204 gpio_num = System.GPIO_CONFIG[name]['gpio_num']
Norman James89de9162015-08-27 21:41:36 -0500205
Norman James471ab592015-08-30 22:29:40 -0500206 return [Openbmc.GPIO_DEV, gpio_num, System.GPIO_CONFIG[name]['direction']]
Norman James89de9162015-08-27 21:41:36 -0500207
Norman James88872672015-09-21 16:51:35 -0500208
Norman James89de9162015-08-27 21:41:36 -0500209
210if __name__ == '__main__':
211 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
212 bus = dbus.SessionBus()
213 name = dbus.service.BusName(DBUS_NAME,bus)
214 obj = SystemManager(bus,OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500215 mainloop = gobject.MainLoop()
Norman James89de9162015-08-27 21:41:36 -0500216
Norman James89de9162015-08-27 21:41:36 -0500217 print "Running SystemManager"
218 mainloop.run()
219