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