blob: 4f9b261f83e9cff1fc4778e019a730c573de95ae [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
Brad Bishopa7ac8052016-09-21 09:17:05 -040014import obmc.system
Norman James89de9162015-08-27 21:41:36 -050015
16DBUS_NAME = 'org.openbmc.managers.System'
17OBJ_NAME = '/org/openbmc/managers/System'
Norman James66c02692015-10-15 10:23:12 -050018INTF_SENSOR = 'org.openbmc.SensorValue'
19INTF_ITEM = 'org.openbmc.InventoryItem'
Norman James89de9162015-08-27 21:41:36 -050020
Norman Jamescf74f952015-10-28 12:45:18 -050021
Brad Bishop416539d2016-07-22 07:22:42 -040022class SystemManager(DbusProperties, DbusObjectManager):
23 def __init__(self, bus, obj_name):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040024 super(SystemManager, self).__init__(
25 conn=bus,
26 object_path=obj_name)
Brad Bishop416539d2016-07-22 07:22:42 -040027 self.bus = bus
Norman James9e6acf92015-09-08 07:00:04 -050028
Brad Bishop416539d2016-07-22 07:22:42 -040029 bus.add_signal_receiver(
Brad Bishop416539d2016-07-22 07:22:42 -040030 self.NewObjectHandler,
31 signal_name="InterfacesAdded", sender_keyword='bus_name')
32 bus.add_signal_receiver(
33 self.SystemStateHandler, signal_name="GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050034
Brad Bishop416539d2016-07-22 07:22:42 -040035 self.Set(DBUS_NAME, "current_state", "")
Norman Jamescfc2b442015-10-31 17:31:46 -050036
Brad Bishop416539d2016-07-22 07:22:42 -040037 ## replace symbolic path in ID_LOOKUP
38 for category in System.ID_LOOKUP:
39 for key in System.ID_LOOKUP[category]:
40 val = System.ID_LOOKUP[category][key]
41 new_val = val.replace(
Brad Bishop7e5ec462016-09-21 09:09:48 -040042 "<inventory_root>", obmc.inventory.INVENTORY_ROOT)
Brad Bishop416539d2016-07-22 07:22:42 -040043 System.ID_LOOKUP[category][key] = new_val
Norman Jamescfc2b442015-10-31 17:31:46 -050044
Brad Bishop416539d2016-07-22 07:22:42 -040045 self.SystemStateHandler(System.SYSTEM_STATES[0])
Brad Bishopfa736492016-06-29 22:21:49 -040046
Brad Bishop416539d2016-07-22 07:22:42 -040047 print "SystemManager Init Done"
Brad Bishop4de42642016-06-29 21:55:47 -040048
Brad Bishop416539d2016-07-22 07:22:42 -040049 def SystemStateHandler(self, state_name):
Brad Bishop416539d2016-07-22 07:22:42 -040050 print "Running System State: "+state_name
Norman James7aeefa72015-10-19 11:13:25 -050051
Brad Bishop416539d2016-07-22 07:22:42 -040052 self.Set(DBUS_NAME, "current_state", state_name)
Norman Jamesd9f1d4e2015-10-14 09:40:18 -050053
Brad Bishop6173a892016-07-24 10:04:09 -040054 waitlist = System.EXIT_STATE_DEPEND.get(state_name, {}).keys()
55 if waitlist:
56 self.waiter = obmc.mapper.utils.Wait(
57 self.bus, waitlist,
58 callback=self.gotoNextState)
59
Brad Bishop416539d2016-07-22 07:22:42 -040060 def gotoNextState(self):
61 s = 0
62 current_state = self.Get(DBUS_NAME, "current_state")
63 for i in range(len(System.SYSTEM_STATES)):
64 if (System.SYSTEM_STATES[i] == current_state):
65 s = i+1
Norman Jamesd9f1d4e2015-10-14 09:40:18 -050066
Brad Bishop416539d2016-07-22 07:22:42 -040067 if (s == len(System.SYSTEM_STATES)):
68 print "ERROR SystemManager: No more system states"
69 else:
70 new_state_name = System.SYSTEM_STATES[s]
71 print "SystemManager Goto System State: "+new_state_name
72 self.SystemStateHandler(new_state_name)
Norman James7aeefa72015-10-19 11:13:25 -050073
Brad Bishop416539d2016-07-22 07:22:42 -040074 @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s')
75 def getSystemState(self):
76 return self.Get(DBUS_NAME, "current_state")
Yi Li5cf1e112016-04-15 15:30:30 +080077
Brad Bishop416539d2016-07-22 07:22:42 -040078 def doObjectLookup(self, category, key):
Brad Bishop416539d2016-07-22 07:22:42 -040079 obj_path = ""
80 intf_name = INTF_ITEM
81 try:
82 obj_path = System.ID_LOOKUP[category][key]
Brad Bishop416539d2016-07-22 07:22:42 -040083 parts = obj_path.split('/')
84 if (parts[3] == 'sensors'):
85 intf_name = INTF_SENSOR
86 except Exception as e:
87 print "ERROR SystemManager: "+str(e)+" not found in lookup"
Yi Li5cf1e112016-04-15 15:30:30 +080088
Brad Bishopfc38a572016-07-22 08:56:09 -040089 return [obj_path, intf_name]
Norman James471ab592015-08-30 22:29:40 -050090
Brad Bishopfc38a572016-07-22 08:56:09 -040091 @dbus.service.method(DBUS_NAME, in_signature='ss', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -040092 def getObjectFromId(self, category, key):
93 return self.doObjectLookup(category, key)
Norman James89de9162015-08-27 21:41:36 -050094
Brad Bishopfc38a572016-07-22 08:56:09 -040095 @dbus.service.method(DBUS_NAME, in_signature='sy', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -040096 def getObjectFromByteId(self, category, key):
97 byte = int(key)
98 return self.doObjectLookup(category, byte)
Brad Bishopfa736492016-06-29 22:21:49 -040099
Brad Bishop416539d2016-07-22 07:22:42 -0400100 # Get the FRU area names defined in ID_LOOKUP table given a fru_id.
101 # If serval areas are defined for a fru_id, the areas are returned
102 # together as a string with each area name seperated with ','.
103 # If no fru area defined in ID_LOOKUP, an empty string will be returned.
104 @dbus.service.method(DBUS_NAME, in_signature='y', out_signature='s')
105 def getFRUArea(self, fru_id):
106 ret_str = ''
107 fru_id = '_' + str(fru_id)
108 area_list = [
109 area for area in System.ID_LOOKUP['FRU_STR'].keys()
110 if area.endswith(fru_id)]
111 for area in area_list:
112 ret_str = area + ',' + ret_str
113 # remove the last ','
114 return ret_str[:-1]
Brad Bishopfa736492016-06-29 22:21:49 -0400115
Brad Bishop416539d2016-07-22 07:22:42 -0400116 def NewObjectHandler(self, obj_path, iprops, bus_name=None):
117 current_state = self.Get(DBUS_NAME, "current_state")
Brad Bishop416539d2016-07-22 07:22:42 -0400118 if current_state not in System.EXIT_STATE_DEPEND:
119 return
120
121 if obj_path in System.EXIT_STATE_DEPEND[current_state]:
122 print "New object: "+obj_path+" ("+bus_name+")"
Brad Bishop416539d2016-07-22 07:22:42 -0400123
124 @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis')
125 def gpioInit(self, name):
126 gpio_path = ''
127 gpio_num = -1
128 r = ['', gpio_num, '']
129 if name not in System.GPIO_CONFIG:
130 # TODO: Error handling
131 print "ERROR: "+name+" not found in GPIO config table"
132 else:
133
134 gpio_num = -1
135 gpio = System.GPIO_CONFIG[name]
136 if 'gpio_num' in System.GPIO_CONFIG[name]:
137 gpio_num = gpio['gpio_num']
138 else:
139 if 'gpio_pin' in System.GPIO_CONFIG[name]:
Brad Bishopa7ac8052016-09-21 09:17:05 -0400140 gpio_num = obmc.system.convertGpio(gpio['gpio_pin'])
Brad Bishop416539d2016-07-22 07:22:42 -0400141 else:
142 print "ERROR: SystemManager - GPIO lookup failed for "+name
143
144 if (gpio_num != -1):
145 r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
146 return r
147
Norman James89de9162015-08-27 21:41:36 -0500148
149if __name__ == '__main__':
150 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400151 bus = get_dbus()
Brad Bishop416539d2016-07-22 07:22:42 -0400152 obj = SystemManager(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500153 mainloop = gobject.MainLoop()
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400154 obj.unmask_signals()
Brad Bishop416539d2016-07-22 07:22:42 -0400155 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James89de9162015-08-27 21:41:36 -0500156
Norman James89de9162015-08-27 21:41:36 -0500157 print "Running SystemManager"
158 mainloop.run()
Brad Bishop53066752016-09-21 08:48:04 -0400159
160# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4