blob: 8261589582e24027e62c0aade5e6be568567dd82 [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
Norman Jamescfc2b442015-10-31 17:31:46 -050032
Norman Jamesab777b12015-10-29 06:19:18 -050033 ## this will load properties from cache
Norman James0a5fbf82015-12-03 17:56:55 -060034 if (data.has_key('present') == False):
35 data['present'] = 'False'
36 if (data.has_key('fault') == False):
37 data['fault'] = 'False'
38 if (data.has_key('version') == False):
39 data['version'] = ''
40
Norman Jamesb714eb22015-10-26 17:12:57 -050041 self.SetMultiple(INTF_NAME,data)
Norman Jamesab777b12015-10-29 06:19:18 -050042
Norman Jamesa3e47c42015-10-18 14:43:10 -050043
44 @dbus.service.method(INTF_NAME,
Norman James19e45912015-10-04 20:19:41 -050045 in_signature='a{sv}', out_signature='')
46 def update(self,data):
Norman Jamesb714eb22015-10-26 17:12:57 -050047 self.SetMultiple(INTF_NAME,data)
Norman Jamesab777b12015-10-29 06:19:18 -050048 PropertyCacher.save(self.name,INTF_NAME,self.properties)
Norman James19e45912015-10-04 20:19:41 -050049
Norman Jamesa3e47c42015-10-18 14:43:10 -050050 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050051 in_signature='s', out_signature='')
52 def setPresent(self,present):
Norman Jamesb714eb22015-10-26 17:12:57 -050053 self.Set(INTF_NAME,'present',present)
Norman James96da5c22015-10-15 10:17:06 -050054
Norman Jamesa3e47c42015-10-18 14:43:10 -050055 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050056 in_signature='s', out_signature='')
57 def setFault(self,fault):
Norman Jamesb714eb22015-10-26 17:12:57 -050058 self.Set(INTF_NAME,'fault',fault)
Norman James19e45912015-10-04 20:19:41 -050059
60
Norman Jamesc9c92dc2015-11-17 08:59:07 -060061def getVersion():
62 version = "Error"
63 with open('/etc/os-release', 'r') as f:
64 for line in f:
65 p = line.rstrip('\n')
66 parts = line.rstrip('\n').split('=')
Adriana Kobylak4e13e9f2016-04-25 15:08:01 -050067 if (parts[0] == "VERSION_ID"):
Norman Jamesc9c92dc2015-11-17 08:59:07 -060068 version = parts[1]
69 return version
70
71
Norman James19e45912015-10-04 20:19:41 -050072if __name__ == '__main__':
73 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040074 bus = get_dbus()
Norman James19e45912015-10-04 20:19:41 -050075 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050076 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Norman James19e45912015-10-04 20:19:41 -050077
78 for f in FRUS.keys():
79 obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
Norman James0a5fbf82015-12-03 17:56:55 -060080 obj = InventoryItem(bus,obj_path,FRUS[f])
Norman James323ed972015-12-09 09:06:37 -060081 obj_parent.add(obj_path,obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -060082
83 ## TODO: this is a hack to update bmc inventory item with version
84 ## should be done by flash object
85 if (FRUS[f]['fru_type'] == "BMC"):
86 version = getVersion()
87 obj.update({'version': version})
88
Brad Bishopf0f3efe2016-06-29 23:20:24 -040089 obj_parent.unmask_signals()
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