blob: c8aa7fdd6b069124bc5b0108c6b26a287d1053ad [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'
21
22FRUS = System.FRU_INSTANCES
23FRU_PATH = System.FRU_PATH
24
25class Inventory(dbus.service.Object):
26 def __init__(self,bus,name):
Norman James5e792e32015-10-07 17:36:17 -050027 global FRU_PATH
Norman James19e45912015-10-04 20:19:41 -050028 dbus.service.Object.__init__(self,bus,name)
Norman James5e792e32015-10-07 17:36:17 -050029 if not os.path.exists(FRU_PATH):
30 os.makedirs(FRU_PATH)
31
32
Norman James19e45912015-10-04 20:19:41 -050033 self.objects = [ ]
34
35 def addItem(self,item):
36 self.objects.append(item)
37
38 @dbus.service.method("org.openbmc.managers.Inventory",
39 in_signature='', out_signature='a{sa{sv}}')
40 def getItems(self):
41 tmp_obj = {}
42 for item in self.objects:
43 tmp_obj[str(item.item['name'])]=item.getItemDict()
44 return tmp_obj
45
46
47
48class InventoryItem(dbus.service.Object):
49 def __init__(self,bus,name):
50 dbus.service.Object.__init__(self,bus,name)
51 ## store all properties in a dict so can easily
52 ## send on dbus
53 self.item = {
54 'name' : name,
55 'is_fru' : False,
56 'fru_type' : 0,
57 'state' : 0,
Norman James2891c532015-10-06 08:01:56 -050058 'manufacturer' : "",
Norman James19e45912015-10-04 20:19:41 -050059 }
Norman James19e45912015-10-04 20:19:41 -050060 self.cache = True
Norman James19e45912015-10-04 20:19:41 -050061
62 def getItemDict(self):
63 return self.item
64
Norman James19e45912015-10-04 20:19:41 -050065 @dbus.service.method('org.openbmc.InventoryItem',
66 in_signature='a{sv}', out_signature='')
67 def update(self,data):
68 ## translate dbus data into basic data types
69 for k in data.keys():
70 d = Openbmc.DbusProperty(k,data[k])
Norman James2891c532015-10-06 08:01:56 -050071 self.item[str(k)] = d.getBaseValue()
Norman James19e45912015-10-04 20:19:41 -050072 self.saveToCache()
73
74 @dbus.service.method("org.openbmc.SensorValue",
75 in_signature='y', out_signature='')
76 def setValue(self,data):
77 self.item['state'] = data
Norman James19e45912015-10-04 20:19:41 -050078 print "Update Fru State: "+str(self.item['state'])
79
Norman James8c6d8382015-10-06 07:47:16 -050080 def setField(self,field,value):
Norman James2891c532015-10-06 08:01:56 -050081 self.item[field] = value
Norman James8c6d8382015-10-06 07:47:16 -050082
Norman James19e45912015-10-04 20:19:41 -050083 def isCached(self):
84 return self.cache
85
86 def getCacheFilename(self):
87 global FRU_PATH
88 name = self.item['name'].replace('/','.')
89 filename = FRU_PATH+name[1:]+".fru"
90 return filename
91
92 def saveToCache(self):
93 if (self.isCached() == False):
94 return
95 print "Caching: "+self.item['name']
Norman James8c6d8382015-10-06 07:47:16 -050096 try:
97 output = open(self.getCacheFilename(), 'wb')
98 ## just pickle dict not whole object
Norman James2891c532015-10-06 08:01:56 -050099 cPickle.dump(self.item,output)
Norman James8c6d8382015-10-06 07:47:16 -0500100 except Exception as e:
101 print "ERROR: "+str(e)
102 finally:
103 output.close()
Norman James19e45912015-10-04 20:19:41 -0500104
105 def loadFromCache(self):
106 if (self.isCached() == False):
107 return;
108 ## overlay with pickled data
109 filename=self.getCacheFilename()
110 if (os.path.isfile(filename)):
111 print "Loading from cache: "+filename
Norman James8c6d8382015-10-06 07:47:16 -0500112 try:
113 p = open(filename, 'rb')
114 data2 = cPickle.load(p)
115 for k in data2.keys():
Norman James2891c532015-10-06 08:01:56 -0500116 self.item[k] = data2[k]
Norman James8c6d8382015-10-06 07:47:16 -0500117 except Exception as e:
Norman James5e792e32015-10-07 17:36:17 -0500118 print "No cache file found: " +str(e)
Norman James8c6d8382015-10-06 07:47:16 -0500119 finally:
120 p.close()
Norman James19e45912015-10-04 20:19:41 -0500121
122
123if __name__ == '__main__':
124 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Norman James5e792e32015-10-07 17:36:17 -0500125 bus = Openbmc.getDBus()
Norman James19e45912015-10-04 20:19:41 -0500126 name = dbus.service.BusName(DBUS_NAME,bus)
127 mainloop = gobject.MainLoop()
128 obj_parent = Inventory(bus, '/org/openbmc/managers/Inventory')
129
130 for f in FRUS.keys():
131 obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
132 obj = InventoryItem(bus,obj_path)
Norman James8c6d8382015-10-06 07:47:16 -0500133 obj.setField('is_fru',FRUS[f]['is_fru'])
134 obj.setField('fru_type',FRUS[f]['fru_type'])
Norman James19e45912015-10-04 20:19:41 -0500135 obj.loadFromCache();
136 obj_parent.addItem(obj)
137
138 print "Running Inventory Manager"
139 mainloop.run()
140