blob: dc692b2de1a7cf7a301942bfb82d6f9dd7451bc5 [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
Brad Bishopee1b1542016-05-12 16:55:00 -040011import obmc.dbuslib.propertycacher as PropertyCacher
Brad Bishop84e73b52016-05-12 15:57:52 -040012from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
Brad Bishop0b380f72016-06-10 00:29:50 -040013import obmc_system_config as System
Norman James19e45912015-10-04 20:19:41 -050014
15INTF_NAME = 'org.openbmc.InventoryItem'
Norman James323ed972015-12-09 09:06:37 -060016DBUS_NAME = 'org.openbmc.Inventory'
Norman James19e45912015-10-04 20:19:41 -050017FRUS = System.FRU_INSTANCES
Norman James19e45912015-10-04 20:19:41 -050018
Brad Bishop84e73b52016-05-12 15:57:52 -040019class Inventory(DbusProperties,DbusObjectManager):
Norman James19e45912015-10-04 20:19:41 -050020 def __init__(self,bus,name):
Brad Bishop84e73b52016-05-12 15:57:52 -040021 DbusProperties.__init__(self)
22 DbusObjectManager.__init__(self)
Norman James19e45912015-10-04 20:19:41 -050023 dbus.service.Object.__init__(self,bus,name)
Norman James19e45912015-10-04 20:19:41 -050024
25
Brad Bishop84e73b52016-05-12 15:57:52 -040026class InventoryItem(DbusProperties):
Norman James0a5fbf82015-12-03 17:56:55 -060027 def __init__(self,bus,name,data):
Brad Bishop84e73b52016-05-12 15:57:52 -040028 DbusProperties.__init__(self)
Norman James19e45912015-10-04 20:19:41 -050029 dbus.service.Object.__init__(self,bus,name)
Norman Jamesb714eb22015-10-26 17:12:57 -050030
Norman Jamesa3e47c42015-10-18 14:43:10 -050031 self.name = name
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050032
Norman James0a5fbf82015-12-03 17:56:55 -060033 if (data.has_key('present') == False):
34 data['present'] = 'False'
35 if (data.has_key('fault') == False):
36 data['fault'] = 'False'
37 if (data.has_key('version') == False):
38 data['version'] = ''
39
Norman Jamesb714eb22015-10-26 17:12:57 -050040 self.SetMultiple(INTF_NAME,data)
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050041
42 ## this will load properties from cache
43 PropertyCacher.load(name, INTF_NAME, self.properties)
Norman Jamesab777b12015-10-29 06:19:18 -050044
Norman Jamesa3e47c42015-10-18 14:43:10 -050045
46 @dbus.service.method(INTF_NAME,
Norman James19e45912015-10-04 20:19:41 -050047 in_signature='a{sv}', out_signature='')
48 def update(self,data):
Norman Jamesb714eb22015-10-26 17:12:57 -050049 self.SetMultiple(INTF_NAME,data)
Norman Jamesab777b12015-10-29 06:19:18 -050050 PropertyCacher.save(self.name,INTF_NAME,self.properties)
Norman James19e45912015-10-04 20:19:41 -050051
Norman Jamesa3e47c42015-10-18 14:43:10 -050052 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050053 in_signature='s', out_signature='')
54 def setPresent(self,present):
Norman Jamesb714eb22015-10-26 17:12:57 -050055 self.Set(INTF_NAME,'present',present)
Norman James96da5c22015-10-15 10:17:06 -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 setFault(self,fault):
Norman Jamesb714eb22015-10-26 17:12:57 -050060 self.Set(INTF_NAME,'fault',fault)
Norman James19e45912015-10-04 20:19:41 -050061
62
Norman Jamesc9c92dc2015-11-17 08:59:07 -060063def getVersion():
64 version = "Error"
65 with open('/etc/os-release', 'r') as f:
66 for line in f:
67 p = line.rstrip('\n')
68 parts = line.rstrip('\n').split('=')
Adriana Kobylak4e13e9f2016-04-25 15:08:01 -050069 if (parts[0] == "VERSION_ID"):
Norman Jamesc9c92dc2015-11-17 08:59:07 -060070 version = parts[1]
71 return version
72
73
Norman James19e45912015-10-04 20:19:41 -050074if __name__ == '__main__':
75 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040076 bus = get_dbus()
Norman James19e45912015-10-04 20:19:41 -050077 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050078 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Norman James19e45912015-10-04 20:19:41 -050079
80 for f in FRUS.keys():
81 obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
Norman James0a5fbf82015-12-03 17:56:55 -060082 obj = InventoryItem(bus,obj_path,FRUS[f])
Norman James323ed972015-12-09 09:06:37 -060083 obj_parent.add(obj_path,obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -060084
85 ## TODO: this is a hack to update bmc inventory item with version
86 ## should be done by flash object
87 if (FRUS[f]['fru_type'] == "BMC"):
88 version = getVersion()
89 obj.update({'version': version})
90
Brad Bishopf0f3efe2016-06-29 23:20:24 -040091 obj_parent.unmask_signals()
Brad Bishopa81e98a2016-05-02 23:07:36 -040092 name = dbus.service.BusName(DBUS_NAME,bus)
Norman James19e45912015-10-04 20:19:41 -050093 print "Running Inventory Manager"
94 mainloop.run()
95