1#!/usr/bin/env python
2
3# TODO: openbmc/openbmc#2994 remove python 2 support
4try:  # python 2
5    import gobject
6except ImportError:  # python 3
7    from gi.repository import GObject as gobject
8import dbus
9import dbus.service
10import dbus.mainloop.glib
11import os
12from obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus
13import obmc.enums
14import obmc_system_config as System
15import obmc.inventory
16import obmc.system
17
18DBUS_NAME = "org.openbmc.managers.System"
19OBJ_NAME = "/org/openbmc/managers/System"
20INTF_SENSOR = "org.openbmc.SensorValue"
21
22
23class SystemManager(DbusProperties, DbusObjectManager):
24    def __init__(self, bus, obj_name):
25        super(SystemManager, self).__init__(conn=bus, object_path=obj_name)
26        self.bus = bus
27
28        # replace symbolic path in ID_LOOKUP
29        for category in System.ID_LOOKUP:
30            for key in System.ID_LOOKUP[category]:
31                val = System.ID_LOOKUP[category][key]
32                new_val = val.replace(
33                    "<inventory_root>", obmc.inventory.INVENTORY_ROOT
34                )
35                System.ID_LOOKUP[category][key] = new_val
36
37        print("SystemManager Init Done")
38
39    def doObjectLookup(self, category, key):
40        obj_path = ""
41        intf_name = INTF_SENSOR
42        try:
43            obj_path = System.ID_LOOKUP[category][key]
44            parts = obj_path.split("/")
45            if parts[3] != "sensors":
46                print("ERROR SystemManager: SENSOR only supported type")
47                intf_name = ""
48        except Exception as e:
49            print("ERROR SystemManager: " + str(e) + " not found in lookup")
50
51        return [obj_path, intf_name]
52
53    @dbus.service.method(DBUS_NAME, in_signature="ss", out_signature="(ss)")
54    def getObjectFromId(self, category, key):
55        return self.doObjectLookup(category, key)
56
57    @dbus.service.method(DBUS_NAME, in_signature="sy", out_signature="(ss)")
58    def getObjectFromByteId(self, category, key):
59        byte = int(key)
60        return self.doObjectLookup(category, byte)
61
62    @dbus.service.method(DBUS_NAME, in_signature="s", out_signature="sis")
63    def gpioInit(self, name):
64        gpio_path = ""
65        gpio_num = -1
66        r = ["", gpio_num, ""]
67        if name not in System.GPIO_CONFIG:
68            # TODO: Better error handling
69            msg = "ERROR: " + name + " not found in GPIO config table"
70            print(msg)
71            raise Exception(msg)
72        else:
73
74            gpio_num = -1
75            gpio = System.GPIO_CONFIG[name]
76            if "gpio_num" in System.GPIO_CONFIG[name]:
77                gpio_num = gpio["gpio_num"]
78            else:
79                if "gpio_pin" in System.GPIO_CONFIG[name]:
80                    gpio_num = obmc.system.convertGpio(gpio["gpio_pin"])
81                else:
82                    msg = (
83                        "ERROR: SystemManager - GPIO lookup failed for " + name
84                    )
85                    print(msg)
86                    raise Exception(msg)
87
88            if gpio_num != -1:
89                r = [obmc.enums.GPIO_DEV, gpio_num, gpio["direction"]]
90        return r
91
92    @dbus.service.method(
93        DBUS_NAME, in_signature="", out_signature="ssa(sb)a(sb)a(sbb)ssssa(sb)"
94    )
95    def getGpioConfiguration(self):
96        power_config = System.GPIO_CONFIGS.get("power_config", {})
97        power_good_in = power_config.get("power_good_in", "")
98        latch_out = power_config.get("latch_out", "")
99        power_up_outs = power_config.get("power_up_outs", [])
100        reset_outs = power_config.get("reset_outs", [])
101        pci_reset_outs = power_config.get("pci_reset_outs", [])
102        hostctl_config = System.GPIO_CONFIGS.get("hostctl_config", {})
103        fsi_data = hostctl_config.get("fsi_data", "")
104        fsi_clk = hostctl_config.get("fsi_clk", "")
105        fsi_enable = hostctl_config.get("fsi_enable", "")
106        cronus_sel = hostctl_config.get("cronus_sel", "")
107        optionals = hostctl_config.get("optionals", [])
108        r = [
109            power_good_in,
110            latch_out,
111            power_up_outs,
112            reset_outs,
113            pci_reset_outs,
114            fsi_data,
115            fsi_clk,
116            fsi_enable,
117            cronus_sel,
118            optionals,
119        ]
120        print("Power GPIO config: " + str(r))
121        return r
122
123
124if __name__ == "__main__":
125    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
126    bus = get_dbus()
127    obj = SystemManager(bus, OBJ_NAME)
128    mainloop = gobject.MainLoop()
129    obj.unmask_signals()
130    name = dbus.service.BusName(DBUS_NAME, bus)
131
132    print("Running SystemManager")
133    mainloop.run()
134
135# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
136