xref: /openbmc/skeleton/pyinventorymgr/inventory_items.py (revision af3c91369ab4d9372924786302f1e1c725ad25d0)
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		self.InterfacesAdded(name,self.properties)
25
26
27class InventoryItem(DbusProperties):
28	def __init__(self,bus,name,data):
29		DbusProperties.__init__(self)
30		dbus.service.Object.__init__(self,bus,name)
31
32		self.name = name
33
34		## this will load properties from cache
35		if (data.has_key('present') == False):
36			data['present'] = 'False'
37		if (data.has_key('fault') == False):
38			data['fault'] = 'False'
39		if (data.has_key('version') == False):
40			data['version'] = ''
41
42		self.SetMultiple(INTF_NAME,data)
43
44
45	@dbus.service.method(INTF_NAME,
46		in_signature='a{sv}', out_signature='')
47	def update(self,data):
48		self.SetMultiple(INTF_NAME,data)
49		PropertyCacher.save(self.name,INTF_NAME,self.properties)
50
51	@dbus.service.method(INTF_NAME,
52		in_signature='s', out_signature='')
53	def setPresent(self,present):
54		self.Set(INTF_NAME,'present',present)
55
56	@dbus.service.method(INTF_NAME,
57		in_signature='s', out_signature='')
58	def setFault(self,fault):
59		self.Set(INTF_NAME,'fault',fault)
60
61
62def getVersion():
63	version = "Error"
64	with open('/etc/os-release', 'r') as f:
65		for line in f:
66			p = line.rstrip('\n')
67			parts = line.rstrip('\n').split('=')
68			if (parts[0] == "VERSION_ID"):
69				version = parts[1]
70	return version
71
72
73if __name__ == '__main__':
74    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
75    bus = get_dbus()
76    mainloop = gobject.MainLoop()
77    obj_parent = Inventory(bus, '/org/openbmc/inventory')
78
79    for f in FRUS.keys():
80	obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
81    	obj = InventoryItem(bus,obj_path,FRUS[f])
82	obj_parent.add(obj_path,obj)
83
84    	## 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})
89
90    name = dbus.service.BusName(DBUS_NAME,bus)
91    print "Running Inventory Manager"
92    mainloop.run()
93
94