blob: 93dfcbafe58263fac630a2340aed2894ee4b3b62 [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
Brad Bishop7e5ec462016-09-21 09:09:48 -040013import obmc.inventory
Norman James89de9162015-08-27 21:41:36 -050014
15DBUS_NAME = 'org.openbmc.managers.System'
16OBJ_NAME = '/org/openbmc/managers/System'
Norman James66c02692015-10-15 10:23:12 -050017INTF_SENSOR = 'org.openbmc.SensorValue'
18INTF_ITEM = 'org.openbmc.InventoryItem'
Norman James89de9162015-08-27 21:41:36 -050019
Norman Jamescf74f952015-10-28 12:45:18 -050020
Brad Bishop416539d2016-07-22 07:22:42 -040021class SystemManager(DbusProperties, DbusObjectManager):
22 def __init__(self, bus, obj_name):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040023 super(SystemManager, self).__init__(
24 conn=bus,
25 object_path=obj_name)
Brad Bishop416539d2016-07-22 07:22:42 -040026 self.bus = bus
Norman James9e6acf92015-09-08 07:00:04 -050027
Brad Bishop416539d2016-07-22 07:22:42 -040028 bus.add_signal_receiver(
Brad Bishop416539d2016-07-22 07:22:42 -040029 self.NewObjectHandler,
30 signal_name="InterfacesAdded", sender_keyword='bus_name')
31 bus.add_signal_receiver(
32 self.SystemStateHandler, signal_name="GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050033
Brad Bishop416539d2016-07-22 07:22:42 -040034 self.Set(DBUS_NAME, "current_state", "")
Norman Jamescfc2b442015-10-31 17:31:46 -050035
Brad Bishop416539d2016-07-22 07:22:42 -040036 ## replace symbolic path in ID_LOOKUP
37 for category in System.ID_LOOKUP:
38 for key in System.ID_LOOKUP[category]:
39 val = System.ID_LOOKUP[category][key]
40 new_val = val.replace(
Brad Bishop7e5ec462016-09-21 09:09:48 -040041 "<inventory_root>", obmc.inventory.INVENTORY_ROOT)
Brad Bishop416539d2016-07-22 07:22:42 -040042 System.ID_LOOKUP[category][key] = new_val
Norman Jamescfc2b442015-10-31 17:31:46 -050043
Brad Bishop416539d2016-07-22 07:22:42 -040044 self.SystemStateHandler(System.SYSTEM_STATES[0])
Brad Bishopfa736492016-06-29 22:21:49 -040045
Brad Bishop416539d2016-07-22 07:22:42 -040046 print "SystemManager Init Done"
Brad Bishop4de42642016-06-29 21:55:47 -040047
Brad Bishop416539d2016-07-22 07:22:42 -040048 def SystemStateHandler(self, state_name):
Brad Bishop416539d2016-07-22 07:22:42 -040049 print "Running System State: "+state_name
Norman James7aeefa72015-10-19 11:13:25 -050050
Brad Bishop416539d2016-07-22 07:22:42 -040051 self.Set(DBUS_NAME, "current_state", state_name)
Norman Jamesd9f1d4e2015-10-14 09:40:18 -050052
Brad Bishop6173a892016-07-24 10:04:09 -040053 waitlist = System.EXIT_STATE_DEPEND.get(state_name, {}).keys()
54 if waitlist:
55 self.waiter = obmc.mapper.utils.Wait(
56 self.bus, waitlist,
57 callback=self.gotoNextState)
58
Brad Bishop416539d2016-07-22 07:22:42 -040059 def gotoNextState(self):
60 s = 0
61 current_state = self.Get(DBUS_NAME, "current_state")
62 for i in range(len(System.SYSTEM_STATES)):
63 if (System.SYSTEM_STATES[i] == current_state):
64 s = i+1
Norman Jamesd9f1d4e2015-10-14 09:40:18 -050065
Brad Bishop416539d2016-07-22 07:22:42 -040066 if (s == len(System.SYSTEM_STATES)):
67 print "ERROR SystemManager: No more system states"
68 else:
69 new_state_name = System.SYSTEM_STATES[s]
70 print "SystemManager Goto System State: "+new_state_name
71 self.SystemStateHandler(new_state_name)
Norman James7aeefa72015-10-19 11:13:25 -050072
Brad Bishop416539d2016-07-22 07:22:42 -040073 @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s')
74 def getSystemState(self):
75 return self.Get(DBUS_NAME, "current_state")
Yi Li5cf1e112016-04-15 15:30:30 +080076
Brad Bishop416539d2016-07-22 07:22:42 -040077 def doObjectLookup(self, category, key):
Brad Bishop416539d2016-07-22 07:22:42 -040078 obj_path = ""
79 intf_name = INTF_ITEM
80 try:
81 obj_path = System.ID_LOOKUP[category][key]
Brad Bishop416539d2016-07-22 07:22:42 -040082 parts = obj_path.split('/')
83 if (parts[3] == 'sensors'):
84 intf_name = INTF_SENSOR
85 except Exception as e:
86 print "ERROR SystemManager: "+str(e)+" not found in lookup"
Yi Li5cf1e112016-04-15 15:30:30 +080087
Brad Bishopfc38a572016-07-22 08:56:09 -040088 return [obj_path, intf_name]
Norman James471ab592015-08-30 22:29:40 -050089
Brad Bishopfc38a572016-07-22 08:56:09 -040090 @dbus.service.method(DBUS_NAME, in_signature='ss', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -040091 def getObjectFromId(self, category, key):
92 return self.doObjectLookup(category, key)
Norman James89de9162015-08-27 21:41:36 -050093
Brad Bishopfc38a572016-07-22 08:56:09 -040094 @dbus.service.method(DBUS_NAME, in_signature='sy', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -040095 def getObjectFromByteId(self, category, key):
96 byte = int(key)
97 return self.doObjectLookup(category, byte)
Brad Bishopfa736492016-06-29 22:21:49 -040098
Brad Bishop416539d2016-07-22 07:22:42 -040099 # Get the FRU area names defined in ID_LOOKUP table given a fru_id.
100 # If serval areas are defined for a fru_id, the areas are returned
101 # together as a string with each area name seperated with ','.
102 # If no fru area defined in ID_LOOKUP, an empty string will be returned.
103 @dbus.service.method(DBUS_NAME, in_signature='y', out_signature='s')
104 def getFRUArea(self, fru_id):
105 ret_str = ''
106 fru_id = '_' + str(fru_id)
107 area_list = [
108 area for area in System.ID_LOOKUP['FRU_STR'].keys()
109 if area.endswith(fru_id)]
110 for area in area_list:
111 ret_str = area + ',' + ret_str
112 # remove the last ','
113 return ret_str[:-1]
Brad Bishopfa736492016-06-29 22:21:49 -0400114
Brad Bishop416539d2016-07-22 07:22:42 -0400115 def NewObjectHandler(self, obj_path, iprops, bus_name=None):
116 current_state = self.Get(DBUS_NAME, "current_state")
Brad Bishop416539d2016-07-22 07:22:42 -0400117 if current_state not in System.EXIT_STATE_DEPEND:
118 return
119
120 if obj_path in System.EXIT_STATE_DEPEND[current_state]:
121 print "New object: "+obj_path+" ("+bus_name+")"
Brad Bishop416539d2016-07-22 07:22:42 -0400122
123 @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis')
124 def gpioInit(self, name):
125 gpio_path = ''
126 gpio_num = -1
127 r = ['', gpio_num, '']
128 if name not in System.GPIO_CONFIG:
129 # TODO: Error handling
130 print "ERROR: "+name+" not found in GPIO config table"
131 else:
132
133 gpio_num = -1
134 gpio = System.GPIO_CONFIG[name]
135 if 'gpio_num' in System.GPIO_CONFIG[name]:
136 gpio_num = gpio['gpio_num']
137 else:
138 if 'gpio_pin' in System.GPIO_CONFIG[name]:
139 gpio_num = System.convertGpio(gpio['gpio_pin'])
140 else:
141 print "ERROR: SystemManager - GPIO lookup failed for "+name
142
143 if (gpio_num != -1):
144 r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
145 return r
146
Norman James89de9162015-08-27 21:41:36 -0500147
148if __name__ == '__main__':
149 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400150 bus = get_dbus()
Brad Bishop416539d2016-07-22 07:22:42 -0400151 obj = SystemManager(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500152 mainloop = gobject.MainLoop()
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400153 obj.unmask_signals()
Brad Bishop416539d2016-07-22 07:22:42 -0400154 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James89de9162015-08-27 21:41:36 -0500155
Norman James89de9162015-08-27 21:41:36 -0500156 print "Running SystemManager"
157 mainloop.run()
Brad Bishop53066752016-09-21 08:48:04 -0400158
159# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4