blob: ddaec98e9a9215f8c145377e77503794454a15b1 [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)
Adriana Kobylak1e8a74a2016-08-15 12:12:56 -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)
Adriana Kobylak1e8a74a2016-08-15 12:12:56 -050056 PropertyCacher.save(self.name, INTF_NAME, self.properties)
Norman James96da5c22015-10-15 10:17:06 -050057
Norman Jamesa3e47c42015-10-18 14:43:10 -050058 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050059 in_signature='s', out_signature='')
60 def setFault(self,fault):
Norman Jamesb714eb22015-10-26 17:12:57 -050061 self.Set(INTF_NAME,'fault',fault)
Adriana Kobylak1e8a74a2016-08-15 12:12:56 -050062 PropertyCacher.save(self.name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050063
64
Norman Jamesc9c92dc2015-11-17 08:59:07 -060065def getVersion():
66 version = "Error"
67 with open('/etc/os-release', 'r') as f:
68 for line in f:
69 p = line.rstrip('\n')
70 parts = line.rstrip('\n').split('=')
Adriana Kobylak4e13e9f2016-04-25 15:08:01 -050071 if (parts[0] == "VERSION_ID"):
Norman Jamesc9c92dc2015-11-17 08:59:07 -060072 version = parts[1]
Ratan Gupta07bd9c02016-08-10 05:08:55 -050073 version = version.strip('"')
Norman Jamesc9c92dc2015-11-17 08:59:07 -060074 return version
75
76
Norman James19e45912015-10-04 20:19:41 -050077if __name__ == '__main__':
78 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040079 bus = get_dbus()
Norman James19e45912015-10-04 20:19:41 -050080 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050081 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Norman James19e45912015-10-04 20:19:41 -050082
83 for f in FRUS.keys():
84 obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
Norman James0a5fbf82015-12-03 17:56:55 -060085 obj = InventoryItem(bus,obj_path,FRUS[f])
Norman James323ed972015-12-09 09:06:37 -060086 obj_parent.add(obj_path,obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -060087
88 ## TODO: this is a hack to update bmc inventory item with version
89 ## should be done by flash object
90 if (FRUS[f]['fru_type'] == "BMC"):
91 version = getVersion()
92 obj.update({'version': version})
93
Brad Bishopf0f3efe2016-06-29 23:20:24 -040094 obj_parent.unmask_signals()
Brad Bishopa81e98a2016-05-02 23:07:36 -040095 name = dbus.service.BusName(DBUS_NAME,bus)
Norman James19e45912015-10-04 20:19:41 -050096 print "Running Inventory Manager"
97 mainloop.run()
98