xref: /openbmc/skeleton/pyinventorymgr/inventory_items.py (revision c91b0bfd5bc748c2b22dd226ab1d064235d08e4f)
1#!/usr/bin/env python
2
3import gobject
4import dbus
5import dbus.service
6import dbus.mainloop.glib
7import obmc.dbuslib.propertycacher as PropertyCacher
8from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
9import obmc_system_config as System
10
11INTF_NAME = 'org.openbmc.InventoryItem'
12DBUS_NAME = 'org.openbmc.Inventory'
13FRUS = System.FRU_INSTANCES
14
15
16class Inventory(DbusProperties, DbusObjectManager):
17    def __init__(self, bus, name):
18        DbusProperties.__init__(self)
19        DbusObjectManager.__init__(self)
20        dbus.service.Object.__init__(self, bus, name)
21
22
23class InventoryItem(DbusProperties):
24    def __init__(self, bus, name, data):
25        DbusProperties.__init__(self)
26        dbus.service.Object.__init__(self, bus, name)
27
28        self.name = name
29
30        if 'present' not in data:
31            data['present'] = 'False'
32        if 'fault' not in data:
33            data['fault'] = 'False'
34        if 'version' not in data:
35            data['version'] = ''
36
37        self.SetMultiple(INTF_NAME, data)
38
39        ## this will load properties from cache
40        PropertyCacher.load(name, INTF_NAME, self.properties)
41
42    @dbus.service.method(
43        INTF_NAME, in_signature='a{sv}', out_signature='')
44    def update(self, data):
45        self.SetMultiple(INTF_NAME, data)
46        PropertyCacher.save(self.name, INTF_NAME, self.properties)
47
48    @dbus.service.method(
49        INTF_NAME, in_signature='s', out_signature='')
50    def setPresent(self, present):
51        self.Set(INTF_NAME, 'present', present)
52        PropertyCacher.save(self.name, INTF_NAME, self.properties)
53
54    @dbus.service.method(
55        INTF_NAME, in_signature='s', out_signature='')
56    def setFault(self, fault):
57        self.Set(INTF_NAME, 'fault', fault)
58        PropertyCacher.save(self.name, INTF_NAME, self.properties)
59
60
61def getVersion():
62    version = "Error"
63    with open('/etc/os-release', 'r') as f:
64        for line in f:
65            p = line.rstrip('\n')
66            parts = line.rstrip('\n').split('=')
67            if (parts[0] == "VERSION_ID"):
68                version = parts[1]
69                version = version.strip('"')
70    return version
71
72
73if __name__ == '__main__':
74    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
75    bus = get_dbus()
76    mainloop = gobject.MainLoop()
77    obj_parent = Inventory(bus, '/org/openbmc/inventory')
78
79    for f in FRUS.keys():
80        obj_path = f.replace("<inventory_root>", System.INVENTORY_ROOT)
81        obj = InventoryItem(bus, obj_path, FRUS[f])
82        obj_parent.add(obj_path, obj)
83
84        ## TODO:  this is a hack to update bmc inventory item with version
85        ## should be done by flash object
86        if (FRUS[f]['fru_type'] == "BMC"):
87            version = getVersion()
88            obj.update({'version': version})
89
90    obj_parent.unmask_signals()
91    name = dbus.service.BusName(DBUS_NAME, bus)
92    print "Running Inventory Manager"
93    mainloop.run()
94