blob: d27abd378456461541dbfdad90a4df9f9604f40d [file] [log] [blame]
Norman James19e45912015-10-04 20:19:41 -05001#!/usr/bin/env python
2
3import os
4import sys
5import gobject
6import dbus
7import dbus.service
8import dbus.mainloop.glib
9import cPickle
10import json
11
12if (len(sys.argv) < 2):
13 print "Usage: inventory_items.py [system name]"
14 exit(1)
15System = __import__(sys.argv[1])
16import Openbmc
17
18
19INTF_NAME = 'org.openbmc.InventoryItem'
20DBUS_NAME = 'org.openbmc.managers.Inventory'
Norman Jamesa3e47c42015-10-18 14:43:10 -050021ENUM_INTF = 'org.openbmc.Object.Enumerate'
Norman James19e45912015-10-04 20:19:41 -050022
23FRUS = System.FRU_INSTANCES
24FRU_PATH = System.FRU_PATH
25
26class Inventory(dbus.service.Object):
27 def __init__(self,bus,name):
Norman James5e792e32015-10-07 17:36:17 -050028 global FRU_PATH
Norman James19e45912015-10-04 20:19:41 -050029 dbus.service.Object.__init__(self,bus,name)
Norman James5e792e32015-10-07 17:36:17 -050030 if not os.path.exists(FRU_PATH):
31 os.makedirs(FRU_PATH)
32
33
Norman James19e45912015-10-04 20:19:41 -050034 self.objects = [ ]
35
36 def addItem(self,item):
37 self.objects.append(item)
38
Norman Jamesa3e47c42015-10-18 14:43:10 -050039 @dbus.service.method(ENUM_INTF,
Norman James19e45912015-10-04 20:19:41 -050040 in_signature='', out_signature='a{sa{sv}}')
Norman Jamesa3e47c42015-10-18 14:43:10 -050041 def enumerate(self):
Norman James19e45912015-10-04 20:19:41 -050042 tmp_obj = {}
43 for item in self.objects:
Norman Jamesa3e47c42015-10-18 14:43:10 -050044 tmp_obj[str(item.name)]=item.GetAll(INTF_NAME)
Norman James19e45912015-10-04 20:19:41 -050045 return tmp_obj
46
47
48
Norman Jamesa3e47c42015-10-18 14:43:10 -050049class InventoryItem(Openbmc.DbusProperties):
50 def __init__(self,bus,name):
51 Openbmc.DbusProperties.__init__(self)
Norman James19e45912015-10-04 20:19:41 -050052 dbus.service.Object.__init__(self,bus,name)
Norman Jamesa3e47c42015-10-18 14:43:10 -050053 self.name = name
Norman James19e45912015-10-04 20:19:41 -050054 self.cache = True
Norman Jamesa3e47c42015-10-18 14:43:10 -050055 self.Set(INTF_NAME,'is_fru',False)
56 self.Set(INTF_NAME,'fru_type',0)
57 self.Set(INTF_NAME,'present',"INACTIVE")
58 self.Set(INTF_NAME,'fault',"NONE")
59
60
61
62 @dbus.service.method(INTF_NAME,
Norman James19e45912015-10-04 20:19:41 -050063 in_signature='a{sv}', out_signature='')
64 def update(self,data):
65 ## translate dbus data into basic data types
66 for k in data.keys():
Norman Jamesa3e47c42015-10-18 14:43:10 -050067 self.setField(k,data[k])
68
Norman James19e45912015-10-04 20:19:41 -050069 self.saveToCache()
70
Norman Jamesa3e47c42015-10-18 14:43:10 -050071 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050072 in_signature='s', out_signature='')
73 def setPresent(self,present):
Norman Jamesa3e47c42015-10-18 14:43:10 -050074 self.setField('present',present)
Norman James96da5c22015-10-15 10:17:06 -050075
Norman Jamesa3e47c42015-10-18 14:43:10 -050076 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050077 in_signature='s', out_signature='')
78 def setFault(self,fault):
Norman Jamesa3e47c42015-10-18 14:43:10 -050079 self.setField('fault',fault)
Norman James19e45912015-10-04 20:19:41 -050080
Norman James8c6d8382015-10-06 07:47:16 -050081 def setField(self,field,value):
Norman Jamesa3e47c42015-10-18 14:43:10 -050082 f = str(field)
83 d = Openbmc.DbusVariable(f,value)
84 self.Set(INTF_NAME,f,d.getBaseValue())
Norman James8c6d8382015-10-06 07:47:16 -050085
Norman James19e45912015-10-04 20:19:41 -050086 def isCached(self):
87 return self.cache
88
89 def getCacheFilename(self):
90 global FRU_PATH
Norman Jamesa3e47c42015-10-18 14:43:10 -050091 name = self.name.replace('/','.')
Norman James19e45912015-10-04 20:19:41 -050092 filename = FRU_PATH+name[1:]+".fru"
93 return filename
94
95 def saveToCache(self):
96 if (self.isCached() == False):
97 return
Norman Jamesa3e47c42015-10-18 14:43:10 -050098 print "Caching: "+self.name
Norman James8c6d8382015-10-06 07:47:16 -050099 try:
100 output = open(self.getCacheFilename(), 'wb')
Norman Jamesa3e47c42015-10-18 14:43:10 -0500101 ## save properties
102 cPickle.dump(self.properties[INTF_NAME],output)
Norman James8c6d8382015-10-06 07:47:16 -0500103 except Exception as e:
104 print "ERROR: "+str(e)
105 finally:
106 output.close()
Norman James19e45912015-10-04 20:19:41 -0500107
108 def loadFromCache(self):
109 if (self.isCached() == False):
110 return;
111 ## overlay with pickled data
112 filename=self.getCacheFilename()
113 if (os.path.isfile(filename)):
114 print "Loading from cache: "+filename
Norman James8c6d8382015-10-06 07:47:16 -0500115 try:
116 p = open(filename, 'rb')
117 data2 = cPickle.load(p)
118 for k in data2.keys():
Norman Jamesa3e47c42015-10-18 14:43:10 -0500119 self.setField(k,data2[k])
Norman James8c6d8382015-10-06 07:47:16 -0500120 except Exception as e:
Norman James5e792e32015-10-07 17:36:17 -0500121 print "No cache file found: " +str(e)
Norman James8c6d8382015-10-06 07:47:16 -0500122 finally:
123 p.close()
Norman James19e45912015-10-04 20:19:41 -0500124
125
126if __name__ == '__main__':
127 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Norman James5e792e32015-10-07 17:36:17 -0500128 bus = Openbmc.getDBus()
Norman James19e45912015-10-04 20:19:41 -0500129 name = dbus.service.BusName(DBUS_NAME,bus)
130 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -0500131 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Norman James19e45912015-10-04 20:19:41 -0500132
133 for f in FRUS.keys():
134 obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
135 obj = InventoryItem(bus,obj_path)
Norman James8c6d8382015-10-06 07:47:16 -0500136 obj.setField('is_fru',FRUS[f]['is_fru'])
137 obj.setField('fru_type',FRUS[f]['fru_type'])
Norman James19e45912015-10-04 20:19:41 -0500138 obj.loadFromCache();
139 obj_parent.addItem(obj)
140
141 print "Running Inventory Manager"
142 mainloop.run()
143