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		if (data.has_key('present') == False):
34			data['present'] = 'False'
35		if (data.has_key('fault') == False):
36			data['fault'] = 'False'
37		if (data.has_key('version') == False):
38			data['version'] = ''
39
40		self.SetMultiple(INTF_NAME,data)
41
42		## this will load properties from cache
43		PropertyCacher.load(name, INTF_NAME, self.properties)
44
45
46	@dbus.service.method(INTF_NAME,
47		in_signature='a{sv}', out_signature='')
48	def update(self,data):
49		self.SetMultiple(INTF_NAME,data)
50		PropertyCacher.save(self.name, INTF_NAME, self.properties)
51
52	@dbus.service.method(INTF_NAME,
53		in_signature='s', out_signature='')
54	def setPresent(self,present):
55		self.Set(INTF_NAME,'present',present)
56		PropertyCacher.save(self.name, INTF_NAME, self.properties)
57
58	@dbus.service.method(INTF_NAME,
59		in_signature='s', out_signature='')
60	def setFault(self,fault):
61		self.Set(INTF_NAME,'fault',fault)
62		PropertyCacher.save(self.name, INTF_NAME, self.properties)
63
64
65def getVersion():
66	version = "Error"
67	with open('/etc/os-release', 'r') as f:
68		for line in f:
69			p = line.rstrip('\n')
70			parts = line.rstrip('\n').split('=')
71			if (parts[0] == "VERSION_ID"):
72				version = parts[1]
73				version = version.strip('"')
74	return version
75
76
77if __name__ == '__main__':
78    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
79    bus = get_dbus()
80    mainloop = gobject.MainLoop()
81    obj_parent = Inventory(bus, '/org/openbmc/inventory')
82
83    for f in FRUS.keys():
84	obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
85    	obj = InventoryItem(bus,obj_path,FRUS[f])
86	obj_parent.add(obj_path,obj)
87
88    	## TODO:  this is a hack to update bmc inventory item with version
89    	## should be done by flash object
90	if (FRUS[f]['fru_type'] == "BMC"):
91		version = getVersion()
92		obj.update({'version': version})
93
94    obj_parent.unmask_signals()
95    name = dbus.service.BusName(DBUS_NAME,bus)
96    print "Running Inventory Manager"
97    mainloop.run()
98
99