blob: 47407481d6ffa8ed8ad3f2e0db7e2db878e46644 [file] [log] [blame]
Norman James42c1be82015-10-22 14:34:26 -05001#!/usr/bin/python -u
Norman James19e45912015-10-04 20:19:41 -05002
3import os
4import sys
5import gobject
6import dbus
7import dbus.service
8import dbus.mainloop.glib
9import cPickle
10import json
Norman Jamesab777b12015-10-29 06:19:18 -050011import PropertyCacher
Norman James19e45912015-10-04 20:19:41 -050012
13if (len(sys.argv) < 2):
14 print "Usage: inventory_items.py [system name]"
15 exit(1)
16System = __import__(sys.argv[1])
17import Openbmc
18
19
20INTF_NAME = 'org.openbmc.InventoryItem'
21DBUS_NAME = 'org.openbmc.managers.Inventory'
Norman Jamesa3e47c42015-10-18 14:43:10 -050022ENUM_INTF = 'org.openbmc.Object.Enumerate'
Norman James19e45912015-10-04 20:19:41 -050023
24FRUS = System.FRU_INSTANCES
Norman James19e45912015-10-04 20:19:41 -050025
Norman Jamescfc2b442015-10-31 17:31:46 -050026class Inventory(Openbmc.DbusProperties):
Norman James19e45912015-10-04 20:19:41 -050027 def __init__(self,bus,name):
28 dbus.service.Object.__init__(self,bus,name)
29 self.objects = [ ]
Norman Jamescfc2b442015-10-31 17:31:46 -050030 self.ObjectAdded(name,ENUM_INTF)
Norman James19e45912015-10-04 20:19:41 -050031
32 def addItem(self,item):
33 self.objects.append(item)
34
Norman Jamesa3e47c42015-10-18 14:43:10 -050035 @dbus.service.method(ENUM_INTF,
Norman James19e45912015-10-04 20:19:41 -050036 in_signature='', out_signature='a{sa{sv}}')
Norman Jamesa3e47c42015-10-18 14:43:10 -050037 def enumerate(self):
Norman James19e45912015-10-04 20:19:41 -050038 tmp_obj = {}
39 for item in self.objects:
Norman Jamesa3e47c42015-10-18 14:43:10 -050040 tmp_obj[str(item.name)]=item.GetAll(INTF_NAME)
Norman James19e45912015-10-04 20:19:41 -050041 return tmp_obj
42
43
44
Norman Jamesa3e47c42015-10-18 14:43:10 -050045class InventoryItem(Openbmc.DbusProperties):
Norman Jamesb714eb22015-10-26 17:12:57 -050046 def __init__(self,bus,name,is_fru,fru_type):
Norman Jamesa3e47c42015-10-18 14:43:10 -050047 Openbmc.DbusProperties.__init__(self)
Norman James19e45912015-10-04 20:19:41 -050048 dbus.service.Object.__init__(self,bus,name)
Norman Jamesb714eb22015-10-26 17:12:57 -050049
Norman Jamesa3e47c42015-10-18 14:43:10 -050050 self.name = name
Norman Jamescfc2b442015-10-31 17:31:46 -050051
Norman Jamesab777b12015-10-29 06:19:18 -050052 ## this will load properties from cache
53 PropertyCacher.load(name,INTF_NAME,self.properties)
Norman Jamesc9c92dc2015-11-17 08:59:07 -060054 data = {'is_fru': is_fru, 'fru_type': fru_type, 'present': 'Inactive', 'fault': 'None', 'version': 'None' }
Norman Jamesb714eb22015-10-26 17:12:57 -050055 self.SetMultiple(INTF_NAME,data)
Norman Jamescfc2b442015-10-31 17:31:46 -050056 self.ObjectAdded(name,INTF_NAME)
Norman Jamesab777b12015-10-29 06:19:18 -050057
Norman Jamesa3e47c42015-10-18 14:43:10 -050058
59 @dbus.service.method(INTF_NAME,
Norman James19e45912015-10-04 20:19:41 -050060 in_signature='a{sv}', out_signature='')
61 def update(self,data):
Norman Jamesb714eb22015-10-26 17:12:57 -050062 self.SetMultiple(INTF_NAME,data)
Norman Jamesab777b12015-10-29 06:19:18 -050063 PropertyCacher.save(self.name,INTF_NAME,self.properties)
Norman James19e45912015-10-04 20:19:41 -050064
Norman Jamesa3e47c42015-10-18 14:43:10 -050065 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050066 in_signature='s', out_signature='')
67 def setPresent(self,present):
Norman Jamesb714eb22015-10-26 17:12:57 -050068 self.Set(INTF_NAME,'present',present)
Norman James96da5c22015-10-15 10:17:06 -050069
Norman Jamesa3e47c42015-10-18 14:43:10 -050070 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050071 in_signature='s', out_signature='')
72 def setFault(self,fault):
Norman Jamesb714eb22015-10-26 17:12:57 -050073 self.Set(INTF_NAME,'fault',fault)
Norman James19e45912015-10-04 20:19:41 -050074
75
Norman Jamesc9c92dc2015-11-17 08:59:07 -060076def getVersion():
77 version = "Error"
78 with open('/etc/os-release', 'r') as f:
79 for line in f:
80 p = line.rstrip('\n')
81 parts = line.rstrip('\n').split('=')
82 if (parts[0] == "BUILD_ID"):
83 version = parts[1]
84 return version
85
86
Norman James19e45912015-10-04 20:19:41 -050087if __name__ == '__main__':
88 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Norman James5e792e32015-10-07 17:36:17 -050089 bus = Openbmc.getDBus()
Norman James19e45912015-10-04 20:19:41 -050090 name = dbus.service.BusName(DBUS_NAME,bus)
91 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050092 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Norman James19e45912015-10-04 20:19:41 -050093
94 for f in FRUS.keys():
95 obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
Norman Jamesb714eb22015-10-26 17:12:57 -050096 obj = InventoryItem(bus,obj_path,FRUS[f]['is_fru'],FRUS[f]['fru_type'])
Norman James19e45912015-10-04 20:19:41 -050097 obj_parent.addItem(obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -060098
99 ## TODO: this is a hack to update bmc inventory item with version
100 ## should be done by flash object
101 if (FRUS[f]['fru_type'] == "BMC"):
102 version = getVersion()
103 obj.update({'version': version})
104
Norman James19e45912015-10-04 20:19:41 -0500105 print "Running Inventory Manager"
106 mainloop.run()
107