Norman James | 42c1be8 | 2015-10-22 14:34:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/python -u |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 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 | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 5 | 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 |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 10 | import time |
Brad Bishop | ee1b154 | 2016-05-12 16:55:00 -0400 | [diff] [blame] | 11 | import obmc.dbuslib.propertycacher as PropertyCacher |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 12 | from obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus |
| 13 | import obmc.enums |
Brad Bishop | 0b380f7 | 2016-06-10 00:29:50 -0400 | [diff] [blame] | 14 | import obmc_system_config as System |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 15 | |
| 16 | DBUS_NAME = 'org.openbmc.managers.System' |
| 17 | OBJ_NAME = '/org/openbmc/managers/System' |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 18 | HEARTBEAT_CHECK_INTERVAL = 20000 |
Norman James | 9a60a7b | 2015-10-06 16:53:23 -0500 | [diff] [blame] | 19 | STATE_START_TIMEOUT = 10 |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 20 | INTF_SENSOR = 'org.openbmc.SensorValue' |
| 21 | INTF_ITEM = 'org.openbmc.InventoryItem' |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 22 | INTF_CONTROL = 'org.openbmc.Control' |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 23 | |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 24 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 25 | class SystemManager(DbusProperties,DbusObjectManager): |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 26 | def __init__(self,bus,obj_name): |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 27 | DbusProperties.__init__(self) |
| 28 | DbusObjectManager.__init__(self) |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 29 | dbus.service.Object.__init__(self,bus,obj_name) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 30 | |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 31 | bus.add_signal_receiver(self.NewObjectHandler, |
Brad Bishop | 58e694d | 2016-04-13 16:04:32 -0400 | [diff] [blame] | 32 | signal_name = "InterfacesAdded", sender_keyword = 'bus_name') |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 33 | bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState") |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 34 | |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 35 | self.Set(DBUS_NAME,"current_state","") |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 36 | self.system_states = {} |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 37 | self.bus_name_lookup = {} |
Norman James | c36372d | 2015-10-08 07:03:07 -0500 | [diff] [blame] | 38 | self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 39 | |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 40 | for name in System.APPS.keys(): |
| 41 | sys_state = System.APPS[name]['system_state'] |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 42 | if (self.system_states.has_key(sys_state) == False): |
| 43 | self.system_states[sys_state] = [] |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 44 | self.system_states[sys_state].append(name) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 45 | |
| 46 | ## replace symbolic path in ID_LOOKUP |
| 47 | for category in System.ID_LOOKUP: |
| 48 | for key in System.ID_LOOKUP[category]: |
| 49 | val = System.ID_LOOKUP[category][key] |
| 50 | new_val = val.replace("<inventory_root>",System.INVENTORY_ROOT) |
| 51 | System.ID_LOOKUP[category][key] = new_val |
| 52 | |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 53 | self.SystemStateHandler(System.SYSTEM_STATES[0]) |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 54 | |
| 55 | if not os.path.exists(PropertyCacher.CACHE_PATH): |
| 56 | print "Creating cache directory: "+PropertyCacher.CACHE_PATH |
| 57 | os.makedirs(PropertyCacher.CACHE_PATH) |
| 58 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 59 | self.InterfacesAdded(obj_name,self.properties) |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 60 | print "SystemManager Init Done" |
| 61 | |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 62 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 63 | def SystemStateHandler(self,state_name): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 64 | ## clearing object started flags |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 65 | current_state = self.Get(DBUS_NAME,"current_state") |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 66 | try: |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 67 | for obj_path in System.EXIT_STATE_DEPEND[current_state]: |
| 68 | System.EXIT_STATE_DEPEND[current_state][obj_path] = 0 |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 69 | except: |
| 70 | pass |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 71 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 72 | print "Running System State: "+state_name |
| 73 | if (self.system_states.has_key(state_name)): |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 74 | for name in self.system_states[state_name]: |
| 75 | self.start_process(name) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 76 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 77 | if (state_name == "BMC_INIT"): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 78 | ## Add poll for heartbeat |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 79 | gobject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 80 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 81 | try: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 82 | cb = System.ENTER_STATE_CALLBACK[state_name] |
Norman James | e38859c | 2015-10-29 06:19:40 -0500 | [diff] [blame] | 83 | for methd in cb.keys(): |
Norman James | 85f050b | 2015-12-18 14:58:20 -0600 | [diff] [blame] | 84 | obj = bus.get_object(cb[methd]['bus_name'],cb[methd]['obj_name'],introspect=False) |
Norman James | e38859c | 2015-10-29 06:19:40 -0500 | [diff] [blame] | 85 | method = obj.get_dbus_method(methd,cb[methd]['interface_name']) |
| 86 | method() |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 87 | except: |
| 88 | pass |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 89 | |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 90 | self.Set(DBUS_NAME,"current_state",state_name) |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 91 | |
| 92 | def gotoNextState(self): |
| 93 | s = 0 |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 94 | current_state = self.Get(DBUS_NAME,"current_state") |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 95 | for i in range(len(System.SYSTEM_STATES)): |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 96 | if (System.SYSTEM_STATES[i] == current_state): |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 97 | s = i+1 |
| 98 | |
| 99 | if (s == len(System.SYSTEM_STATES)): |
| 100 | print "ERROR SystemManager: No more system states" |
| 101 | else: |
| 102 | new_state_name = System.SYSTEM_STATES[s] |
| 103 | print "SystemManager Goto System State: "+new_state_name |
| 104 | self.SystemStateHandler(new_state_name) |
| 105 | |
| 106 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 107 | @dbus.service.method(DBUS_NAME, |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 108 | in_signature='', out_signature='s') |
| 109 | def getSystemState(self): |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 110 | return self.Get(DBUS_NAME,"current_state") |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 111 | |
| 112 | def doObjectLookup(self,category,key): |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 113 | bus_name = "" |
| 114 | obj_path = "" |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 115 | intf_name = INTF_ITEM |
| 116 | try: |
| 117 | obj_path = System.ID_LOOKUP[category][key] |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 118 | bus_name = self.bus_name_lookup[obj_path] |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 119 | parts = obj_path.split('/') |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 120 | if (parts[3] == 'sensors'): |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 121 | intf_name = INTF_SENSOR |
Norman James | d9f1d4e | 2015-10-14 09:40:18 -0500 | [diff] [blame] | 122 | except Exception as e: |
| 123 | print "ERROR SystemManager: "+str(e)+" not found in lookup" |
| 124 | |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 125 | return [bus_name,obj_path,intf_name] |
Norman James | d9f1d4e | 2015-10-14 09:40:18 -0500 | [diff] [blame] | 126 | |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 127 | @dbus.service.method(DBUS_NAME, |
| 128 | in_signature='ss', out_signature='(sss)') |
| 129 | def getObjectFromId(self,category,key): |
| 130 | return self.doObjectLookup(category,key) |
| 131 | |
| 132 | @dbus.service.method(DBUS_NAME, |
| 133 | in_signature='sy', out_signature='(sss)') |
| 134 | def getObjectFromByteId(self,category,key): |
| 135 | byte = int(key) |
| 136 | return self.doObjectLookup(category,byte) |
Yi Li | 5cf1e11 | 2016-04-15 15:30:30 +0800 | [diff] [blame] | 137 | |
| 138 | # Get the FRU area names defined in ID_LOOKUP table given a fru_id. |
| 139 | # If serval areas are defined for a fru_id, the areas are returned |
| 140 | # together as a string with each area name seperated with ','. |
| 141 | # If no fru area defined in ID_LOOKUP, an empty string will be returned. |
| 142 | @dbus.service.method(DBUS_NAME, |
| 143 | in_signature='y', out_signature='s') |
| 144 | def getFRUArea(self,fru_id): |
| 145 | ret_str = '' |
| 146 | fru_id = '_' + str(fru_id) |
| 147 | area_list = [area for area in System.ID_LOOKUP['FRU_STR'].keys() \ |
| 148 | if area.endswith(fru_id)] |
| 149 | for area in area_list: |
| 150 | ret_str = area + ',' + ret_str |
| 151 | # remove the last ',' |
| 152 | return ret_str[:-1] |
| 153 | |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 154 | def start_process(self,name): |
| 155 | if (System.APPS[name]['start_process'] == True): |
| 156 | app = System.APPS[name] |
| 157 | process_name = self.bin_path+"/"+app['process_name'] |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 158 | cmdline = [ ] |
| 159 | cmdline.append(process_name) |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 160 | if (app.has_key('args')): |
| 161 | for a in app['args']: |
| 162 | cmdline.append(a) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 163 | try: |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 164 | print "Starting process: "+" ".join(cmdline)+": "+name |
Norman James | bfa561e | 2015-11-01 07:38:14 -0600 | [diff] [blame] | 165 | if (app['monitor_process'] == True): |
| 166 | app['popen'] = subprocess.Popen(cmdline) |
| 167 | else: |
| 168 | subprocess.Popen(cmdline) |
| 169 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 170 | except Exception as e: |
| 171 | ## TODO: error |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 172 | print "ERROR: starting process: "+" ".join(cmdline) |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 173 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 174 | def heartbeat_check(self): |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 175 | for name in System.APPS.keys(): |
| 176 | app = System.APPS[name] |
Norman James | bfa561e | 2015-11-01 07:38:14 -0600 | [diff] [blame] | 177 | if (app['start_process'] == True and app.has_key('popen')): |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 178 | ## make sure process is still alive |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 179 | p = app['popen'] |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 180 | p.poll() |
| 181 | if (p.returncode != None): |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 182 | print "Process for "+name+" appears to be dead" |
| 183 | self.start_process(name) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 184 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 185 | return True |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 186 | |
Brad Bishop | 58e694d | 2016-04-13 16:04:32 -0400 | [diff] [blame] | 187 | def NewObjectHandler(self, obj_path, iprops, bus_name = None): |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 188 | current_state = self.Get(DBUS_NAME,"current_state") |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 189 | if (self.bus_name_lookup.has_key(obj_path)): |
| 190 | if (self.bus_name_lookup[obj_path] == bus_name): |
| 191 | return |
| 192 | self.bus_name_lookup[obj_path] = bus_name |
| 193 | print "New object: "+obj_path+" ("+bus_name+")" |
| 194 | try: |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 195 | if (System.EXIT_STATE_DEPEND[current_state].has_key(obj_path) == True): |
| 196 | System.EXIT_STATE_DEPEND[current_state][obj_path] = 1 |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 197 | ## check if all required objects are started to move to next state |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 198 | state = 1 |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 199 | for obj_path in System.EXIT_STATE_DEPEND[current_state]: |
| 200 | if (System.EXIT_STATE_DEPEND[current_state][obj_path] == 0): |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 201 | state = 0 |
| 202 | ## all required objects have started so go to next state |
| 203 | if (state == 1): |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 204 | print "All required objects started for "+current_state |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 205 | self.gotoNextState() |
| 206 | except: |
| 207 | pass |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 208 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 209 | |
| 210 | @dbus.service.method(DBUS_NAME, |
| 211 | in_signature='s', out_signature='sis') |
| 212 | def gpioInit(self,name): |
| 213 | gpio_path = '' |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 214 | gpio_num = -1 |
| 215 | r = ['',gpio_num,''] |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 216 | if (System.GPIO_CONFIG.has_key(name) == False): |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 217 | # TODO: Error handling |
| 218 | print "ERROR: "+name+" not found in GPIO config table" |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 219 | else: |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 220 | |
| 221 | gpio_num = -1 |
| 222 | gpio = System.GPIO_CONFIG[name] |
| 223 | if (System.GPIO_CONFIG[name].has_key('gpio_num')): |
| 224 | gpio_num = gpio['gpio_num'] |
| 225 | else: |
| 226 | if (System.GPIO_CONFIG[name].has_key('gpio_pin')): |
| 227 | gpio_num = System.convertGpio(gpio['gpio_pin']) |
| 228 | else: |
| 229 | print "ERROR: SystemManager - GPIO lookup failed for "+name |
| 230 | |
| 231 | if (gpio_num != -1): |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 232 | r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']] |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 233 | return r |
Norman James | 5236a8f | 2015-11-05 20:39:31 -0600 | [diff] [blame] | 234 | |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 235 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 236 | |
| 237 | if __name__ == '__main__': |
| 238 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 239 | bus = get_dbus() |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 240 | name = dbus.service.BusName(DBUS_NAME,bus) |
| 241 | obj = SystemManager(bus,OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 242 | mainloop = gobject.MainLoop() |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 243 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 244 | print "Running SystemManager" |
| 245 | mainloop.run() |
| 246 | |