1de6ed120SBrad Bishop#!/usr/bin/env python
240a360c2SBrad Bishop
3*d65b2d50SCamVan Nguyen# TODO: openbmc/openbmc#2994 remove python 2 support
4*d65b2d50SCamVan Nguyentry:  # python 2
540a360c2SBrad Bishop    import gobject
6*d65b2d50SCamVan Nguyenexcept ImportError:  # python 3
7*d65b2d50SCamVan Nguyen    from gi.repository import GObject as gobject
840a360c2SBrad Bishopimport dbus
940a360c2SBrad Bishopimport dbus.service
1040a360c2SBrad Bishopimport dbus.mainloop.glib
1140a360c2SBrad Bishopimport os
1240a360c2SBrad Bishopfrom obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus
1340a360c2SBrad Bishopimport obmc.enums
140b380f7bSBrad Bishopimport obmc_system_config as System
15a7ac805bSBrad Bishopimport obmc.system
1640a360c2SBrad Bishop
1740a360c2SBrad BishopDBUS_NAME = 'org.openbmc.managers.System'
1840a360c2SBrad BishopOBJ_NAME = '/org/openbmc/managers/System'
19520f8b05SRatan Gupta
2040a360c2SBrad Bishop
2140a360c2SBrad Bishopclass SystemManager(DbusProperties, DbusObjectManager):
2240a360c2SBrad Bishop    def __init__(self, bus, obj_name):
23f47f5faaSBrad Bishop        super(SystemManager, self).__init__(
24f47f5faaSBrad Bishop            conn=bus,
25f47f5faaSBrad Bishop            object_path=obj_name)
26fa736499SBrad Bishop        self.bus = bus
2740a360c2SBrad Bishop
28*d65b2d50SCamVan Nguyen        print("SystemManager Init Done")
2940a360c2SBrad Bishop
30416539dfSBrad Bishop    @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis')
3140a360c2SBrad Bishop    def gpioInit(self, name):
3240a360c2SBrad Bishop        gpio_path = ''
3340a360c2SBrad Bishop        gpio_num = -1
3440a360c2SBrad Bishop        r = ['', gpio_num, '']
35416539dfSBrad Bishop        if name not in System.GPIO_CONFIG:
36605620d2SXo Wang            # TODO: Better error handling
37605620d2SXo Wang            msg = "ERROR: "+name+" not found in GPIO config table"
38*d65b2d50SCamVan Nguyen            print(msg)
39605620d2SXo Wang            raise Exception(msg)
4040a360c2SBrad Bishop        else:
4140a360c2SBrad Bishop
4240a360c2SBrad Bishop            gpio_num = -1
4340a360c2SBrad Bishop            gpio = System.GPIO_CONFIG[name]
44416539dfSBrad Bishop            if 'gpio_num' in System.GPIO_CONFIG[name]:
4540a360c2SBrad Bishop                gpio_num = gpio['gpio_num']
4640a360c2SBrad Bishop            else:
47416539dfSBrad Bishop                if 'gpio_pin' in System.GPIO_CONFIG[name]:
48a7ac805bSBrad Bishop                    gpio_num = obmc.system.convertGpio(gpio['gpio_pin'])
4940a360c2SBrad Bishop                else:
50605620d2SXo Wang                    msg = "ERROR: SystemManager - GPIO lookup failed for "+name
51*d65b2d50SCamVan Nguyen                    print(msg)
52605620d2SXo Wang                    raise Exception(msg)
5340a360c2SBrad Bishop
5440a360c2SBrad Bishop            if (gpio_num != -1):
5540a360c2SBrad Bishop                r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
5640a360c2SBrad Bishop        return r
5740a360c2SBrad Bishop
583f87de8bSXo Wang    @dbus.service.method(DBUS_NAME, in_signature='',
5975a18a23SLei YU                         out_signature='ssa(sb)a(sb)a(sbb)ssssa(sb)')
6075a18a23SLei YU    def getGpioConfiguration(self):
6175a18a23SLei YU        power_config = System.GPIO_CONFIGS.get('power_config', {})
6275a18a23SLei YU        power_good_in = power_config.get('power_good_in', '')
6375a18a23SLei YU        latch_out = power_config.get('latch_out', '')
6475a18a23SLei YU        power_up_outs = power_config.get('power_up_outs', [])
6575a18a23SLei YU        reset_outs = power_config.get('reset_outs', [])
66a42400f2SLei YU        pci_reset_outs = power_config.get('pci_reset_outs', [])
6775a18a23SLei YU        hostctl_config = System.GPIO_CONFIGS.get('hostctl_config', {})
6875a18a23SLei YU        fsi_data = hostctl_config.get('fsi_data', '')
6975a18a23SLei YU        fsi_clk = hostctl_config.get('fsi_clk', '')
7075a18a23SLei YU        fsi_enable = hostctl_config.get('fsi_enable', '')
7175a18a23SLei YU        cronus_sel = hostctl_config.get('cronus_sel', '')
7275a18a23SLei YU        optionals = hostctl_config.get('optionals', [])
7324341f9dSAdriana Kobylak        r = [power_good_in, latch_out, power_up_outs, reset_outs,
7424341f9dSAdriana Kobylak             pci_reset_outs, fsi_data, fsi_clk, fsi_enable, cronus_sel,
7524341f9dSAdriana Kobylak             optionals]
76*d65b2d50SCamVan Nguyen        print("Power GPIO config: " + str(r))
773f87de8bSXo Wang        return r
783f87de8bSXo Wang
7940a360c2SBrad Bishop
8040a360c2SBrad Bishopif __name__ == '__main__':
8140a360c2SBrad Bishop    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
8240a360c2SBrad Bishop    bus = get_dbus()
8340a360c2SBrad Bishop    obj = SystemManager(bus, OBJ_NAME)
8440a360c2SBrad Bishop    mainloop = gobject.MainLoop()
85f0f3efe1SBrad Bishop    obj.unmask_signals()
8670852a38SBrad Bishop    name = dbus.service.BusName(DBUS_NAME, bus)
8740a360c2SBrad Bishop
88*d65b2d50SCamVan Nguyen    print("Running SystemManager")
8940a360c2SBrad Bishop    mainloop.run()
9053066750SBrad Bishop
9153066750SBrad Bishop# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
92