blob: 1c0a5f767162e2057cfa2ac6205322bc38f7d5d4 [file] [log] [blame]
Brad Bishopc91b0bf2016-08-30 20:03:48 -04001#!/usr/bin/env python
Norman James19e45912015-10-04 20:19:41 -05002
Brad Bishop0f608de2016-09-08 22:48:22 -04003import os
4import sys
Patrick Williams75fe8cc2022-07-22 16:12:12 -05005
CamVan Nguyend65b2d52018-02-27 15:14:41 -06006# TODO: openbmc/openbmc#2994 remove python 2 support
7try: # python 2
8 import gobject
9except ImportError: # python 3
10 from gi.repository import GObject as gobject
Norman James19e45912015-10-04 20:19:41 -050011import dbus
12import dbus.service
13import dbus.mainloop.glib
Brad Bishopee1b1542016-05-12 16:55:00 -040014import obmc.dbuslib.propertycacher as PropertyCacher
Brad Bishop84e73b52016-05-12 15:57:52 -040015from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
Brad Bishop985b2a52016-08-30 20:06:23 -040016
Norman James19e45912015-10-04 20:19:41 -050017
Patrick Williams75fe8cc2022-07-22 16:12:12 -050018INTF_NAME = "org.openbmc.InventoryItem"
19DBUS_NAME = "org.openbmc.Inventory"
Brad Bishop985b2a52016-08-30 20:06:23 -040020
Brad Bishop3d13c282016-09-21 08:07:47 -040021FRUS = {}
Norman James19e45912015-10-04 20:19:41 -050022
Brad Bishopc91b0bf2016-08-30 20:03:48 -040023
24class Inventory(DbusProperties, DbusObjectManager):
25 def __init__(self, bus, name):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050026 super(Inventory, self).__init__(conn=bus, object_path=name)
Norman James19e45912015-10-04 20:19:41 -050027
28
Brad Bishop84e73b52016-05-12 15:57:52 -040029class InventoryItem(DbusProperties):
Brad Bishopc91b0bf2016-08-30 20:03:48 -040030 def __init__(self, bus, name, data):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050031 super(InventoryItem, self).__init__(conn=bus, object_path=name)
Norman Jamesb714eb22015-10-26 17:12:57 -050032
Brad Bishopc91b0bf2016-08-30 20:03:48 -040033 self.name = name
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050034
Patrick Williams75fe8cc2022-07-22 16:12:12 -050035 if "present" not in data:
36 data["present"] = "False"
37 if "fault" not in data:
38 data["fault"] = "False"
39 if "version" not in data:
40 data["version"] = ""
Norman James0a5fbf82015-12-03 17:56:55 -060041
Brad Bishopc91b0bf2016-08-30 20:03:48 -040042 self.SetMultiple(INTF_NAME, data)
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050043
Adriana Kobylak24341f92018-01-26 15:07:23 -060044 # this will load properties from cache
Brad Bishopc91b0bf2016-08-30 20:03:48 -040045 PropertyCacher.load(name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050046
Patrick Williams75fe8cc2022-07-22 16:12:12 -050047 @dbus.service.method(INTF_NAME, in_signature="a{sv}", out_signature="")
Brad Bishopc91b0bf2016-08-30 20:03:48 -040048 def update(self, data):
49 self.SetMultiple(INTF_NAME, data)
50 PropertyCacher.save(self.name, INTF_NAME, self.properties)
Norman James96da5c22015-10-15 10:17:06 -050051
Patrick Williams75fe8cc2022-07-22 16:12:12 -050052 @dbus.service.method(INTF_NAME, in_signature="s", out_signature="")
Brad Bishopc91b0bf2016-08-30 20:03:48 -040053 def setPresent(self, present):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050054 self.Set(INTF_NAME, "present", present)
Brad Bishopc91b0bf2016-08-30 20:03:48 -040055 PropertyCacher.save(self.name, INTF_NAME, self.properties)
56
Patrick Williams75fe8cc2022-07-22 16:12:12 -050057 @dbus.service.method(INTF_NAME, in_signature="s", out_signature="")
Brad Bishopc91b0bf2016-08-30 20:03:48 -040058 def setFault(self, fault):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050059 self.Set(INTF_NAME, "fault", fault)
Brad Bishopc91b0bf2016-08-30 20:03:48 -040060 PropertyCacher.save(self.name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050061
62
Norman Jamesc9c92dc2015-11-17 08:59:07 -060063def getVersion():
Brad Bishopc91b0bf2016-08-30 20:03:48 -040064 version = "Error"
Patrick Williams75fe8cc2022-07-22 16:12:12 -050065 with open("/etc/os-release", "r") as f:
Brad Bishopc91b0bf2016-08-30 20:03:48 -040066 for line in f:
Patrick Williams75fe8cc2022-07-22 16:12:12 -050067 p = line.rstrip("\n")
68 parts = line.rstrip("\n").split("=")
69 if parts[0] == "VERSION_ID":
Brad Bishopc91b0bf2016-08-30 20:03:48 -040070 version = parts[1]
71 version = version.strip('"')
72 return version
Norman Jamesc9c92dc2015-11-17 08:59:07 -060073
74
Patrick Williams75fe8cc2022-07-22 16:12:12 -050075if __name__ == "__main__":
Norman James19e45912015-10-04 20:19:41 -050076 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040077 bus = get_dbus()
Norman James19e45912015-10-04 20:19:41 -050078 mainloop = gobject.MainLoop()
Patrick Williams75fe8cc2022-07-22 16:12:12 -050079 obj_parent = Inventory(bus, "/org/openbmc/inventory")
80 INVENTORY_FILE = os.path.join(
81 sys.prefix, "share", "inventory", "inventory.json"
82 )
Matt Spinler14c82862016-08-15 13:31:10 -050083
84 if os.path.exists(INVENTORY_FILE):
Brad Bishop3d13c282016-09-21 08:07:47 -040085 import json
Patrick Williams75fe8cc2022-07-22 16:12:12 -050086
87 with open(INVENTORY_FILE, "r") as f:
Matt Spinler14c82862016-08-15 13:31:10 -050088 try:
89 inv = json.load(f)
90 except ValueError:
CamVan Nguyend65b2d52018-02-27 15:14:41 -060091 print("Invalid JSON detected in " + INVENTORY_FILE)
Matt Spinler14c82862016-08-15 13:31:10 -050092 else:
93 FRUS = inv
Brad Bishop3d13c282016-09-21 08:07:47 -040094 else:
95 try:
96 import obmc_system_config as System
Patrick Williams75fe8cc2022-07-22 16:12:12 -050097
Brad Bishop3d13c282016-09-21 08:07:47 -040098 FRUS = System.FRU_INSTANCES
99 except ImportError:
100 pass
Norman James19e45912015-10-04 20:19:41 -0500101
CamVan Nguyend65b2d52018-02-27 15:14:41 -0600102 for f in list(FRUS.keys()):
Brad Bishop7e5ec462016-09-21 09:09:48 -0400103 import obmc.inventory
Patrick Williams75fe8cc2022-07-22 16:12:12 -0500104
Brad Bishop7e5ec462016-09-21 09:09:48 -0400105 obj_path = f.replace("<inventory_root>", obmc.inventory.INVENTORY_ROOT)
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400106 obj = InventoryItem(bus, obj_path, FRUS[f])
107 obj_parent.add(obj_path, obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -0600108
Adriana Kobylak24341f92018-01-26 15:07:23 -0600109 # TODO: this is a hack to update bmc inventory item with version
110 # should be done by flash object
Patrick Williams75fe8cc2022-07-22 16:12:12 -0500111 if FRUS[f]["fru_type"] == "BMC":
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400112 version = getVersion()
Patrick Williams75fe8cc2022-07-22 16:12:12 -0500113 obj.update({"version": version})
Norman Jamesc9c92dc2015-11-17 08:59:07 -0600114
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400115 obj_parent.unmask_signals()
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400116 name = dbus.service.BusName(DBUS_NAME, bus)
CamVan Nguyend65b2d52018-02-27 15:14:41 -0600117 print("Running Inventory Manager")
Norman James19e45912015-10-04 20:19:41 -0500118 mainloop.run()
Brad Bishop53066752016-09-21 08:48:04 -0400119
120# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4