blob: 67a6838049fd590c85b86ad7948b84b0135f835d [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 Bishop0b380f72016-06-10 00:29:50 -04009import obmc_system_config as System
Norman James19e45912015-10-04 20:19:41 -050010
11INTF_NAME = 'org.openbmc.InventoryItem'
Norman James323ed972015-12-09 09:06:37 -060012DBUS_NAME = 'org.openbmc.Inventory'
Norman James19e45912015-10-04 20:19:41 -050013FRUS = System.FRU_INSTANCES
Norman James19e45912015-10-04 20:19:41 -050014
Brad Bishopc91b0bf2016-08-30 20:03:48 -040015
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)
Norman James19e45912015-10-04 20:19:41 -050021
22
Brad Bishop84e73b52016-05-12 15:57:52 -040023class InventoryItem(DbusProperties):
Brad Bishopc91b0bf2016-08-30 20:03:48 -040024 def __init__(self, bus, name, data):
25 DbusProperties.__init__(self)
26 dbus.service.Object.__init__(self, bus, name)
Norman Jamesb714eb22015-10-26 17:12:57 -050027
Brad Bishopc91b0bf2016-08-30 20:03:48 -040028 self.name = name
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050029
Brad Bishopc91b0bf2016-08-30 20:03:48 -040030 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'] = ''
Norman James0a5fbf82015-12-03 17:56:55 -060036
Brad Bishopc91b0bf2016-08-30 20:03:48 -040037 self.SetMultiple(INTF_NAME, data)
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050038
Brad Bishopc91b0bf2016-08-30 20:03:48 -040039 ## this will load properties from cache
40 PropertyCacher.load(name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050041
Brad Bishopc91b0bf2016-08-30 20:03:48 -040042 @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)
Norman James96da5c22015-10-15 10:17:06 -050047
Brad Bishopc91b0bf2016-08-30 20:03:48 -040048 @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)
Norman James19e45912015-10-04 20:19:41 -050059
60
Norman Jamesc9c92dc2015-11-17 08:59:07 -060061def getVersion():
Brad Bishopc91b0bf2016-08-30 20:03:48 -040062 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
Norman Jamesc9c92dc2015-11-17 08:59:07 -060071
72
Norman James19e45912015-10-04 20:19:41 -050073if __name__ == '__main__':
74 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040075 bus = get_dbus()
Norman James19e45912015-10-04 20:19:41 -050076 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050077 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Norman James19e45912015-10-04 20:19:41 -050078
79 for f in FRUS.keys():
Brad Bishopc91b0bf2016-08-30 20:03:48 -040080 obj_path = f.replace("<inventory_root>", System.INVENTORY_ROOT)
81 obj = InventoryItem(bus, obj_path, FRUS[f])
82 obj_parent.add(obj_path, obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -060083
Brad Bishopc91b0bf2016-08-30 20:03:48 -040084 ## 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})
Norman Jamesc9c92dc2015-11-17 08:59:07 -060089
Brad Bishopf0f3efe2016-06-29 23:20:24 -040090 obj_parent.unmask_signals()
Brad Bishopc91b0bf2016-08-30 20:03:48 -040091 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James19e45912015-10-04 20:19:41 -050092 print "Running Inventory Manager"
93 mainloop.run()