blob: 78691d64061788e061a415d887bc3fcc70be984b [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
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
Norman James2656f332015-10-26 06:42:41 -050033
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
Norman James2656f332015-10-26 06:42:41 -050066 self.SetAll(INTF_NAME,data)
67 #self.saveToCache()
Norman James19e45912015-10-04 20:19:41 -050068
Norman Jamesa3e47c42015-10-18 14:43:10 -050069 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050070 in_signature='s', out_signature='')
71 def setPresent(self,present):
Norman Jamesa3e47c42015-10-18 14:43:10 -050072 self.setField('present',present)
Norman James96da5c22015-10-15 10:17:06 -050073
Norman Jamesa3e47c42015-10-18 14:43:10 -050074 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050075 in_signature='s', out_signature='')
76 def setFault(self,fault):
Norman Jamesa3e47c42015-10-18 14:43:10 -050077 self.setField('fault',fault)
Norman James19e45912015-10-04 20:19:41 -050078
Norman James8c6d8382015-10-06 07:47:16 -050079 def setField(self,field,value):
Norman Jamesa3e47c42015-10-18 14:43:10 -050080 f = str(field)
81 d = Openbmc.DbusVariable(f,value)
82 self.Set(INTF_NAME,f,d.getBaseValue())
Norman James8c6d8382015-10-06 07:47:16 -050083
Norman James19e45912015-10-04 20:19:41 -050084 def isCached(self):
85 return self.cache
86
87 def getCacheFilename(self):
88 global FRU_PATH
Norman Jamesa3e47c42015-10-18 14:43:10 -050089 name = self.name.replace('/','.')
Norman James19e45912015-10-04 20:19:41 -050090 filename = FRU_PATH+name[1:]+".fru"
91 return filename
92
93 def saveToCache(self):
94 if (self.isCached() == False):
95 return
Norman Jamesa3e47c42015-10-18 14:43:10 -050096 print "Caching: "+self.name
Norman James8c6d8382015-10-06 07:47:16 -050097 try:
98 output = open(self.getCacheFilename(), 'wb')
Norman Jamesa3e47c42015-10-18 14:43:10 -050099 ## save properties
100 cPickle.dump(self.properties[INTF_NAME],output)
Norman James8c6d8382015-10-06 07:47:16 -0500101 except Exception as e:
102 print "ERROR: "+str(e)
103 finally:
104 output.close()
Norman James19e45912015-10-04 20:19:41 -0500105
106 def loadFromCache(self):
107 if (self.isCached() == False):
108 return;
109 ## overlay with pickled data
110 filename=self.getCacheFilename()
111 if (os.path.isfile(filename)):
112 print "Loading from cache: "+filename
Norman James8c6d8382015-10-06 07:47:16 -0500113 try:
114 p = open(filename, 'rb')
115 data2 = cPickle.load(p)
116 for k in data2.keys():
Norman Jamesa3e47c42015-10-18 14:43:10 -0500117 self.setField(k,data2[k])
Norman James8c6d8382015-10-06 07:47:16 -0500118 except Exception as e:
Norman James5e792e32015-10-07 17:36:17 -0500119 print "No cache file found: " +str(e)
Norman James8c6d8382015-10-06 07:47:16 -0500120 finally:
121 p.close()
Norman James19e45912015-10-04 20:19:41 -0500122
123
124if __name__ == '__main__':
125 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Norman James5e792e32015-10-07 17:36:17 -0500126 bus = Openbmc.getDBus()
Norman James19e45912015-10-04 20:19:41 -0500127 name = dbus.service.BusName(DBUS_NAME,bus)
128 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -0500129 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Norman James19e45912015-10-04 20:19:41 -0500130
131 for f in FRUS.keys():
132 obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
133 obj = InventoryItem(bus,obj_path)
Norman James8c6d8382015-10-06 07:47:16 -0500134 obj.setField('is_fru',FRUS[f]['is_fru'])
135 obj.setField('fru_type',FRUS[f]['fru_type'])
Norman James2656f332015-10-26 06:42:41 -0500136 #obj.loadFromCache();
Norman James19e45912015-10-04 20:19:41 -0500137 obj_parent.addItem(obj)
138
139 print "Running Inventory Manager"
140 mainloop.run()
141