blob: 6320f908e92b9eefe8d4185c835e0d3735d5e5cb [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'
Norman James323ed972015-12-09 09:06:37 -060021DBUS_NAME = 'org.openbmc.Inventory'
Norman James19e45912015-10-04 20:19:41 -050022FRUS = System.FRU_INSTANCES
Norman James19e45912015-10-04 20:19:41 -050023
Norman James323ed972015-12-09 09:06:37 -060024class Inventory(Openbmc.DbusProperties,Openbmc.DbusObjectManager):
Norman James19e45912015-10-04 20:19:41 -050025 def __init__(self,bus,name):
Norman James323ed972015-12-09 09:06:37 -060026 Openbmc.DbusProperties.__init__(self)
27 Openbmc.DbusObjectManager.__init__(self)
Norman James19e45912015-10-04 20:19:41 -050028 dbus.service.Object.__init__(self,bus,name)
Norman James323ed972015-12-09 09:06:37 -060029 self.InterfacesAdded(name,self.properties)
Norman James19e45912015-10-04 20:19:41 -050030
31
Norman Jamesa3e47c42015-10-18 14:43:10 -050032class InventoryItem(Openbmc.DbusProperties):
Norman James0a5fbf82015-12-03 17:56:55 -060033 def __init__(self,bus,name,data):
Norman Jamesa3e47c42015-10-18 14:43:10 -050034 Openbmc.DbusProperties.__init__(self)
Norman James19e45912015-10-04 20:19:41 -050035 dbus.service.Object.__init__(self,bus,name)
Norman Jamesb714eb22015-10-26 17:12:57 -050036
Norman Jamesa3e47c42015-10-18 14:43:10 -050037 self.name = name
Norman Jamescfc2b442015-10-31 17:31:46 -050038
Norman Jamesab777b12015-10-29 06:19:18 -050039 ## this will load properties from cache
Norman James0a5fbf82015-12-03 17:56:55 -060040 # PropertyCacher.load(name,INTF_NAME,self.properties)
41 if (data.has_key('present') == False):
42 data['present'] = 'False'
43 if (data.has_key('fault') == False):
44 data['fault'] = 'False'
45 if (data.has_key('version') == False):
46 data['version'] = ''
47
Norman Jamesb714eb22015-10-26 17:12:57 -050048 self.SetMultiple(INTF_NAME,data)
Norman Jamesab777b12015-10-29 06:19:18 -050049
Norman Jamesa3e47c42015-10-18 14:43:10 -050050
51 @dbus.service.method(INTF_NAME,
Norman James19e45912015-10-04 20:19:41 -050052 in_signature='a{sv}', out_signature='')
53 def update(self,data):
Norman Jamesb714eb22015-10-26 17:12:57 -050054 self.SetMultiple(INTF_NAME,data)
Norman Jamesab777b12015-10-29 06:19:18 -050055 PropertyCacher.save(self.name,INTF_NAME,self.properties)
Norman James19e45912015-10-04 20:19:41 -050056
Norman Jamesa3e47c42015-10-18 14:43:10 -050057 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050058 in_signature='s', out_signature='')
59 def setPresent(self,present):
Norman Jamesb714eb22015-10-26 17:12:57 -050060 self.Set(INTF_NAME,'present',present)
Norman James96da5c22015-10-15 10:17:06 -050061
Norman Jamesa3e47c42015-10-18 14:43:10 -050062 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050063 in_signature='s', out_signature='')
64 def setFault(self,fault):
Norman Jamesb714eb22015-10-26 17:12:57 -050065 self.Set(INTF_NAME,'fault',fault)
Norman James19e45912015-10-04 20:19:41 -050066
67
Norman Jamesc9c92dc2015-11-17 08:59:07 -060068def getVersion():
69 version = "Error"
70 with open('/etc/os-release', 'r') as f:
71 for line in f:
72 p = line.rstrip('\n')
73 parts = line.rstrip('\n').split('=')
74 if (parts[0] == "BUILD_ID"):
75 version = parts[1]
76 return version
77
78
Norman James19e45912015-10-04 20:19:41 -050079if __name__ == '__main__':
80 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Norman James5e792e32015-10-07 17:36:17 -050081 bus = Openbmc.getDBus()
Norman James19e45912015-10-04 20:19:41 -050082 name = dbus.service.BusName(DBUS_NAME,bus)
83 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050084 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Norman James19e45912015-10-04 20:19:41 -050085
86 for f in FRUS.keys():
87 obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
Norman James0a5fbf82015-12-03 17:56:55 -060088 obj = InventoryItem(bus,obj_path,FRUS[f])
Norman James323ed972015-12-09 09:06:37 -060089 obj_parent.add(obj_path,obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -060090
91 ## TODO: this is a hack to update bmc inventory item with version
92 ## should be done by flash object
93 if (FRUS[f]['fru_type'] == "BMC"):
94 version = getVersion()
95 obj.update({'version': version})
96
Norman James19e45912015-10-04 20:19:41 -050097 print "Running Inventory Manager"
98 mainloop.run()
99