blob: d6d3d1df8a25d528ce956da23325c63b4b1ed68e [file] [log] [blame]
Brad Bishopc91b0bf2016-08-30 20:03:48 -04001#!/usr/bin/env python
Norman James19e45912015-10-04 20:19:41 -05002
Norman James19e45912015-10-04 20:19:41 -05003import gobject
4import dbus
5import dbus.service
6import dbus.mainloop.glib
Brad Bishopee1b1542016-05-12 16:55:00 -04007import obmc.dbuslib.propertycacher as PropertyCacher
Brad Bishop84e73b52016-05-12 15:57:52 -04008from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
Brad Bishop985b2a52016-08-30 20:06:23 -04009
10try:
11 import obmc_system_config as System
12 have_system = True
13except ImportError:
14 have_system = False
Norman James19e45912015-10-04 20:19:41 -050015
16INTF_NAME = 'org.openbmc.InventoryItem'
Norman James323ed972015-12-09 09:06:37 -060017DBUS_NAME = 'org.openbmc.Inventory'
Brad Bishop985b2a52016-08-30 20:06:23 -040018
19if have_system:
20 FRUS = System.FRU_INSTANCES
21else:
22 FRUS = {}
Norman James19e45912015-10-04 20:19:41 -050023
Brad Bishopc91b0bf2016-08-30 20:03:48 -040024
25class Inventory(DbusProperties, DbusObjectManager):
26 def __init__(self, bus, name):
27 DbusProperties.__init__(self)
28 DbusObjectManager.__init__(self)
29 dbus.service.Object.__init__(self, bus, name)
Norman James19e45912015-10-04 20:19:41 -050030
31
Brad Bishop84e73b52016-05-12 15:57:52 -040032class InventoryItem(DbusProperties):
Brad Bishopc91b0bf2016-08-30 20:03:48 -040033 def __init__(self, bus, name, data):
34 DbusProperties.__init__(self)
35 dbus.service.Object.__init__(self, bus, name)
Norman Jamesb714eb22015-10-26 17:12:57 -050036
Brad Bishopc91b0bf2016-08-30 20:03:48 -040037 self.name = name
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050038
Brad Bishopc91b0bf2016-08-30 20:03:48 -040039 if 'present' not in data:
40 data['present'] = 'False'
41 if 'fault' not in data:
42 data['fault'] = 'False'
43 if 'version' not in data:
44 data['version'] = ''
Norman James0a5fbf82015-12-03 17:56:55 -060045
Brad Bishopc91b0bf2016-08-30 20:03:48 -040046 self.SetMultiple(INTF_NAME, data)
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050047
Brad Bishopc91b0bf2016-08-30 20:03:48 -040048 ## this will load properties from cache
49 PropertyCacher.load(name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050050
Brad Bishopc91b0bf2016-08-30 20:03:48 -040051 @dbus.service.method(
52 INTF_NAME, in_signature='a{sv}', out_signature='')
53 def update(self, data):
54 self.SetMultiple(INTF_NAME, data)
55 PropertyCacher.save(self.name, INTF_NAME, self.properties)
Norman James96da5c22015-10-15 10:17:06 -050056
Brad Bishopc91b0bf2016-08-30 20:03:48 -040057 @dbus.service.method(
58 INTF_NAME, in_signature='s', out_signature='')
59 def setPresent(self, present):
60 self.Set(INTF_NAME, 'present', present)
61 PropertyCacher.save(self.name, INTF_NAME, self.properties)
62
63 @dbus.service.method(
64 INTF_NAME, in_signature='s', out_signature='')
65 def setFault(self, fault):
66 self.Set(INTF_NAME, 'fault', fault)
67 PropertyCacher.save(self.name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050068
69
Norman Jamesc9c92dc2015-11-17 08:59:07 -060070def getVersion():
Brad Bishopc91b0bf2016-08-30 20:03:48 -040071 version = "Error"
72 with open('/etc/os-release', 'r') as f:
73 for line in f:
74 p = line.rstrip('\n')
75 parts = line.rstrip('\n').split('=')
76 if (parts[0] == "VERSION_ID"):
77 version = parts[1]
78 version = version.strip('"')
79 return version
Norman Jamesc9c92dc2015-11-17 08:59:07 -060080
81
Norman James19e45912015-10-04 20:19:41 -050082if __name__ == '__main__':
83 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040084 bus = get_dbus()
Norman James19e45912015-10-04 20:19:41 -050085 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050086 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Norman James19e45912015-10-04 20:19:41 -050087
88 for f in FRUS.keys():
Brad Bishopc91b0bf2016-08-30 20:03:48 -040089 obj_path = f.replace("<inventory_root>", System.INVENTORY_ROOT)
90 obj = InventoryItem(bus, obj_path, FRUS[f])
91 obj_parent.add(obj_path, obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -060092
Brad Bishopc91b0bf2016-08-30 20:03:48 -040093 ## TODO: this is a hack to update bmc inventory item with version
94 ## should be done by flash object
95 if (FRUS[f]['fru_type'] == "BMC"):
96 version = getVersion()
97 obj.update({'version': version})
Norman Jamesc9c92dc2015-11-17 08:59:07 -060098
Brad Bishopf0f3efe2016-06-29 23:20:24 -040099 obj_parent.unmask_signals()
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400100 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James19e45912015-10-04 20:19:41 -0500101 print "Running Inventory Manager"
102 mainloop.run()