blob: 9a888cdca306523e36a0349a025fb11335d14f7d [file] [log] [blame]
Brad Bishopde6ed122016-08-30 20:14:57 -04001#!/usr/bin/env python
Norman James89de9162015-08-27 21:41:36 -05002
Norman James6f8d0422015-09-14 18:48:00 -05003import gobject
Norman James89de9162015-08-27 21:41:36 -05004import dbus
5import dbus.service
6import dbus.mainloop.glib
Norman James90baede2015-09-02 20:32:49 -05007import os
Brad Bishopee1b1542016-05-12 16:55:00 -04008import obmc.dbuslib.propertycacher as PropertyCacher
Brad Bishop84e73b52016-05-12 15:57:52 -04009from obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus
10import obmc.enums
Brad Bishop0b380f72016-06-10 00:29:50 -040011import obmc_system_config as System
Brad Bishop6173a892016-07-24 10:04:09 -040012import obmc.mapper.utils
Norman James89de9162015-08-27 21:41:36 -050013
14DBUS_NAME = 'org.openbmc.managers.System'
15OBJ_NAME = '/org/openbmc/managers/System'
Norman James66c02692015-10-15 10:23:12 -050016INTF_SENSOR = 'org.openbmc.SensorValue'
17INTF_ITEM = 'org.openbmc.InventoryItem'
Norman James89de9162015-08-27 21:41:36 -050018
Norman Jamescf74f952015-10-28 12:45:18 -050019
Brad Bishop416539d2016-07-22 07:22:42 -040020class SystemManager(DbusProperties, DbusObjectManager):
21 def __init__(self, bus, obj_name):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040022 super(SystemManager, self).__init__(
23 conn=bus,
24 object_path=obj_name)
Brad Bishop416539d2016-07-22 07:22:42 -040025 self.bus = bus
Norman James9e6acf92015-09-08 07:00:04 -050026
Brad Bishop416539d2016-07-22 07:22:42 -040027 bus.add_signal_receiver(
Brad Bishop416539d2016-07-22 07:22:42 -040028 self.NewObjectHandler,
29 signal_name="InterfacesAdded", sender_keyword='bus_name')
30 bus.add_signal_receiver(
31 self.SystemStateHandler, signal_name="GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050032
Brad Bishop416539d2016-07-22 07:22:42 -040033 self.Set(DBUS_NAME, "current_state", "")
Norman Jamescfc2b442015-10-31 17:31:46 -050034
Brad Bishop416539d2016-07-22 07:22:42 -040035 ## replace symbolic path in ID_LOOKUP
36 for category in System.ID_LOOKUP:
37 for key in System.ID_LOOKUP[category]:
38 val = System.ID_LOOKUP[category][key]
39 new_val = val.replace(
40 "<inventory_root>", System.INVENTORY_ROOT)
41 System.ID_LOOKUP[category][key] = new_val
Norman Jamescfc2b442015-10-31 17:31:46 -050042
Brad Bishop416539d2016-07-22 07:22:42 -040043 self.SystemStateHandler(System.SYSTEM_STATES[0])
Brad Bishopfa736492016-06-29 22:21:49 -040044
Brad Bishop416539d2016-07-22 07:22:42 -040045 print "SystemManager Init Done"
Brad Bishop4de42642016-06-29 21:55:47 -040046
Brad Bishop416539d2016-07-22 07:22:42 -040047 def SystemStateHandler(self, state_name):
Brad Bishop416539d2016-07-22 07:22:42 -040048 print "Running System State: "+state_name
Norman James7aeefa72015-10-19 11:13:25 -050049
Brad Bishop416539d2016-07-22 07:22:42 -040050 self.Set(DBUS_NAME, "current_state", state_name)
Norman Jamesd9f1d4e2015-10-14 09:40:18 -050051
Brad Bishop6173a892016-07-24 10:04:09 -040052 waitlist = System.EXIT_STATE_DEPEND.get(state_name, {}).keys()
53 if waitlist:
54 self.waiter = obmc.mapper.utils.Wait(
55 self.bus, waitlist,
56 callback=self.gotoNextState)
57
Brad Bishop416539d2016-07-22 07:22:42 -040058 def gotoNextState(self):
59 s = 0
60 current_state = self.Get(DBUS_NAME, "current_state")
61 for i in range(len(System.SYSTEM_STATES)):
62 if (System.SYSTEM_STATES[i] == current_state):
63 s = i+1
Norman Jamesd9f1d4e2015-10-14 09:40:18 -050064
Brad Bishop416539d2016-07-22 07:22:42 -040065 if (s == len(System.SYSTEM_STATES)):
66 print "ERROR SystemManager: No more system states"
67 else:
68 new_state_name = System.SYSTEM_STATES[s]
69 print "SystemManager Goto System State: "+new_state_name
70 self.SystemStateHandler(new_state_name)
Norman James7aeefa72015-10-19 11:13:25 -050071
Brad Bishop416539d2016-07-22 07:22:42 -040072 @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s')
73 def getSystemState(self):
74 return self.Get(DBUS_NAME, "current_state")
Yi Li5cf1e112016-04-15 15:30:30 +080075
Brad Bishop416539d2016-07-22 07:22:42 -040076 def doObjectLookup(self, category, key):
Brad Bishop416539d2016-07-22 07:22:42 -040077 obj_path = ""
78 intf_name = INTF_ITEM
79 try:
80 obj_path = System.ID_LOOKUP[category][key]
Brad Bishop416539d2016-07-22 07:22:42 -040081 parts = obj_path.split('/')
82 if (parts[3] == 'sensors'):
83 intf_name = INTF_SENSOR
84 except Exception as e:
85 print "ERROR SystemManager: "+str(e)+" not found in lookup"
Yi Li5cf1e112016-04-15 15:30:30 +080086
Brad Bishopfc38a572016-07-22 08:56:09 -040087 return [obj_path, intf_name]
Norman James471ab592015-08-30 22:29:40 -050088
Brad Bishopfc38a572016-07-22 08:56:09 -040089 @dbus.service.method(DBUS_NAME, in_signature='ss', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -040090 def getObjectFromId(self, category, key):
91 return self.doObjectLookup(category, key)
Norman James89de9162015-08-27 21:41:36 -050092
Brad Bishopfc38a572016-07-22 08:56:09 -040093 @dbus.service.method(DBUS_NAME, in_signature='sy', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -040094 def getObjectFromByteId(self, category, key):
95 byte = int(key)
96 return self.doObjectLookup(category, byte)
Brad Bishopfa736492016-06-29 22:21:49 -040097
Brad Bishop416539d2016-07-22 07:22:42 -040098 # Get the FRU area names defined in ID_LOOKUP table given a fru_id.
99 # If serval areas are defined for a fru_id, the areas are returned
100 # together as a string with each area name seperated with ','.
101 # If no fru area defined in ID_LOOKUP, an empty string will be returned.
102 @dbus.service.method(DBUS_NAME, in_signature='y', out_signature='s')
103 def getFRUArea(self, fru_id):
104 ret_str = ''
105 fru_id = '_' + str(fru_id)
106 area_list = [
107 area for area in System.ID_LOOKUP['FRU_STR'].keys()
108 if area.endswith(fru_id)]
109 for area in area_list:
110 ret_str = area + ',' + ret_str
111 # remove the last ','
112 return ret_str[:-1]
Brad Bishopfa736492016-06-29 22:21:49 -0400113
Brad Bishop416539d2016-07-22 07:22:42 -0400114 def NewObjectHandler(self, obj_path, iprops, bus_name=None):
115 current_state = self.Get(DBUS_NAME, "current_state")
Brad Bishop416539d2016-07-22 07:22:42 -0400116 if current_state not in System.EXIT_STATE_DEPEND:
117 return
118
119 if obj_path in System.EXIT_STATE_DEPEND[current_state]:
120 print "New object: "+obj_path+" ("+bus_name+")"
Brad Bishop416539d2016-07-22 07:22:42 -0400121
122 @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis')
123 def gpioInit(self, name):
124 gpio_path = ''
125 gpio_num = -1
126 r = ['', gpio_num, '']
127 if name not in System.GPIO_CONFIG:
128 # TODO: Error handling
129 print "ERROR: "+name+" not found in GPIO config table"
130 else:
131
132 gpio_num = -1
133 gpio = System.GPIO_CONFIG[name]
134 if 'gpio_num' in System.GPIO_CONFIG[name]:
135 gpio_num = gpio['gpio_num']
136 else:
137 if 'gpio_pin' in System.GPIO_CONFIG[name]:
138 gpio_num = System.convertGpio(gpio['gpio_pin'])
139 else:
140 print "ERROR: SystemManager - GPIO lookup failed for "+name
141
142 if (gpio_num != -1):
143 r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
144 return r
145
Norman James89de9162015-08-27 21:41:36 -0500146
147if __name__ == '__main__':
148 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400149 bus = get_dbus()
Brad Bishop416539d2016-07-22 07:22:42 -0400150 obj = SystemManager(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500151 mainloop = gobject.MainLoop()
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400152 obj.unmask_signals()
Brad Bishop416539d2016-07-22 07:22:42 -0400153 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James89de9162015-08-27 21:41:36 -0500154
Norman James89de9162015-08-27 21:41:36 -0500155 print "Running SystemManager"
156 mainloop.run()
Brad Bishop53066752016-09-21 08:48:04 -0400157
158# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4