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
13import obmc_system_config as System
14
15INTF_NAME = 'org.openbmc.InventoryItem'
16DBUS_NAME = 'org.openbmc.Inventory'
17FRUS = System.FRU_INSTANCES
18
19class Inventory(DbusProperties,DbusObjectManager):
20	def __init__(self,bus,name):
21		DbusProperties.__init__(self)
22		DbusObjectManager.__init__(self)
23		dbus.service.Object.__init__(self,bus,name)
24
25
26class InventoryItem(DbusProperties):
27	def __init__(self,bus,name,data):
28		DbusProperties.__init__(self)
29		dbus.service.Object.__init__(self,bus,name)
30
31		self.name = name
32
33		## this will load properties from cache
34		if (data.has_key('present') == False):
35			data['present'] = 'False'
36		if (data.has_key('fault') == False):
37			data['fault'] = 'False'
38		if (data.has_key('version') == False):
39			data['version'] = ''
40
41		self.SetMultiple(INTF_NAME,data)
42
43
44	@dbus.service.method(INTF_NAME,
45		in_signature='a{sv}', out_signature='')
46	def update(self,data):
47		self.SetMultiple(INTF_NAME,data)
48		PropertyCacher.save(self.name,INTF_NAME,self.properties)
49
50	@dbus.service.method(INTF_NAME,
51		in_signature='s', out_signature='')
52	def setPresent(self,present):
53		self.Set(INTF_NAME,'present',present)
54
55	@dbus.service.method(INTF_NAME,
56		in_signature='s', out_signature='')
57	def setFault(self,fault):
58		self.Set(INTF_NAME,'fault',fault)
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	return version
70
71
72if __name__ == '__main__':
73    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
74    bus = get_dbus()
75    mainloop = gobject.MainLoop()
76    obj_parent = Inventory(bus, '/org/openbmc/inventory')
77
78    for f in FRUS.keys():
79	obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
80    	obj = InventoryItem(bus,obj_path,FRUS[f])
81	obj_parent.add(obj_path,obj)
82
83    	## TODO:  this is a hack to update bmc inventory item with version
84    	## should be done by flash object
85	if (FRUS[f]['fru_type'] == "BMC"):
86		version = getVersion()
87		obj.update({'version': version})
88
89    obj_parent.unmask_signals()
90    name = dbus.service.BusName(DBUS_NAME,bus)
91    print "Running Inventory Manager"
92    mainloop.run()
93
94