blob: 772fb3ecc04de975c0f36506de44f532bdad38bb [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 James323ed972015-12-09 09:06:37 -060024 self.InterfacesAdded(name,self.properties)
Norman James19e45912015-10-04 20:19:41 -050025
26
Brad Bishop84e73b52016-05-12 15:57:52 -040027class InventoryItem(DbusProperties):
Norman James0a5fbf82015-12-03 17:56:55 -060028 def __init__(self,bus,name,data):
Brad Bishop84e73b52016-05-12 15:57:52 -040029 DbusProperties.__init__(self)
Norman James19e45912015-10-04 20:19:41 -050030 dbus.service.Object.__init__(self,bus,name)
Norman Jamesb714eb22015-10-26 17:12:57 -050031
Norman Jamesa3e47c42015-10-18 14:43:10 -050032 self.name = name
Norman Jamescfc2b442015-10-31 17:31:46 -050033
Norman Jamesab777b12015-10-29 06:19:18 -050034 ## this will load properties from cache
Norman James0a5fbf82015-12-03 17:56:55 -060035 if (data.has_key('present') == False):
36 data['present'] = 'False'
37 if (data.has_key('fault') == False):
38 data['fault'] = 'False'
39 if (data.has_key('version') == False):
40 data['version'] = ''
41
Norman Jamesb714eb22015-10-26 17:12:57 -050042 self.SetMultiple(INTF_NAME,data)
Norman Jamesab777b12015-10-29 06:19:18 -050043
Norman Jamesa3e47c42015-10-18 14:43:10 -050044
45 @dbus.service.method(INTF_NAME,
Norman James19e45912015-10-04 20:19:41 -050046 in_signature='a{sv}', out_signature='')
47 def update(self,data):
Norman Jamesb714eb22015-10-26 17:12:57 -050048 self.SetMultiple(INTF_NAME,data)
Norman Jamesab777b12015-10-29 06:19:18 -050049 PropertyCacher.save(self.name,INTF_NAME,self.properties)
Norman James19e45912015-10-04 20:19:41 -050050
Norman Jamesa3e47c42015-10-18 14:43:10 -050051 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050052 in_signature='s', out_signature='')
53 def setPresent(self,present):
Norman Jamesb714eb22015-10-26 17:12:57 -050054 self.Set(INTF_NAME,'present',present)
Norman James96da5c22015-10-15 10:17:06 -050055
Norman Jamesa3e47c42015-10-18 14:43:10 -050056 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050057 in_signature='s', out_signature='')
58 def setFault(self,fault):
Norman Jamesb714eb22015-10-26 17:12:57 -050059 self.Set(INTF_NAME,'fault',fault)
Norman James19e45912015-10-04 20:19:41 -050060
61
Norman Jamesc9c92dc2015-11-17 08:59:07 -060062def getVersion():
63 version = "Error"
64 with open('/etc/os-release', 'r') as f:
65 for line in f:
66 p = line.rstrip('\n')
67 parts = line.rstrip('\n').split('=')
Adriana Kobylak4e13e9f2016-04-25 15:08:01 -050068 if (parts[0] == "VERSION_ID"):
Norman Jamesc9c92dc2015-11-17 08:59:07 -060069 version = parts[1]
70 return version
71
72
Norman James19e45912015-10-04 20:19:41 -050073if __name__ == '__main__':
74 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040075 bus = get_dbus()
Norman James19e45912015-10-04 20:19:41 -050076 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050077 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Norman James19e45912015-10-04 20:19:41 -050078
79 for f in FRUS.keys():
80 obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
Norman James0a5fbf82015-12-03 17:56:55 -060081 obj = InventoryItem(bus,obj_path,FRUS[f])
Norman James323ed972015-12-09 09:06:37 -060082 obj_parent.add(obj_path,obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -060083
84 ## TODO: this is a hack to update bmc inventory item with version
85 ## should be done by flash object
86 if (FRUS[f]['fru_type'] == "BMC"):
87 version = getVersion()
88 obj.update({'version': version})
89
Brad Bishopa81e98a2016-05-02 23:07:36 -040090 name = dbus.service.BusName(DBUS_NAME,bus)
Norman James19e45912015-10-04 20:19:41 -050091 print "Running Inventory Manager"
92 mainloop.run()
93