1f5110e11SBrad Bishop#!/usr/bin/env python 240a360c2SBrad Bishop 31902990dSGunnar Millsimport os 4*d65b2d50SCamVan Nguyen# TODO: openbmc/openbmc#2994 remove python 2 support 5*d65b2d50SCamVan Nguyentry: # python 2 640a360c2SBrad Bishop import gobject 7*d65b2d50SCamVan Nguyenexcept ImportError: # python 3 8*d65b2d50SCamVan Nguyen from gi.repository import GObject as gobject 940a360c2SBrad Bishopimport dbus 1040a360c2SBrad Bishopimport dbus.service 1140a360c2SBrad Bishopimport dbus.mainloop.glib 1240a360c2SBrad Bishopimport subprocess 1340a360c2SBrad Bishopfrom obmc.dbuslib.bindings import get_dbus 1440a360c2SBrad Bishop 1540a360c2SBrad Bishop 16fa06665fSBrad BishopFLASH_DOWNLOAD_PATH = '/tmp' 1740a360c2SBrad BishopDBUS_NAME = 'org.openbmc.managers.Download' 1840a360c2SBrad BishopOBJ_NAME = '/org/openbmc/managers/Download' 1940a360c2SBrad BishopTFTP_PORT = 69 2040a360c2SBrad Bishop 21f5110e11SBrad Bishop 2240a360c2SBrad Bishopclass DownloadManagerObject(dbus.service.Object): 2340a360c2SBrad Bishop def __init__(self, bus, name): 2440a360c2SBrad Bishop dbus.service.Object.__init__(self, bus, name) 25f5110e11SBrad Bishop bus.add_signal_receiver( 26f5110e11SBrad Bishop self.DownloadHandler, 2740a360c2SBrad Bishop dbus_interface="org.openbmc.Flash", 28f5110e11SBrad Bishop signal_name="Download", 29f5110e11SBrad Bishop path_keyword="path") 30f5110e11SBrad Bishop bus.add_signal_receiver( 31f5110e11SBrad Bishop self.TftpDownloadHandler, 32f5110e11SBrad Bishop signal_name="TftpDownload", 33f5110e11SBrad Bishop path_keyword="path") 3440a360c2SBrad Bishop 3540a360c2SBrad Bishop @dbus.service.signal(DBUS_NAME, signature='ss') 3640a360c2SBrad Bishop def DownloadComplete(self, outfile, filename): 37*d65b2d50SCamVan Nguyen print("Download Complete: "+outfile) 3840a360c2SBrad Bishop return outfile 3940a360c2SBrad Bishop 4040a360c2SBrad Bishop @dbus.service.signal(DBUS_NAME, signature='s') 4140a360c2SBrad Bishop def DownloadError(self, filename): 4240a360c2SBrad Bishop pass 4340a360c2SBrad Bishop 4440a360c2SBrad Bishop def TftpDownloadHandler(self, ip, filename, path=None): 4540a360c2SBrad Bishop try: 4640a360c2SBrad Bishop filename = str(filename) 47*d65b2d50SCamVan Nguyen print("Downloading: "+filename+" from "+ip) 481902990dSGunnar Mills outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename) 49f5110e11SBrad Bishop rc = subprocess.call( 50f5110e11SBrad Bishop ["tftp", "-l", outfile, "-r", filename, "-g", ip]) 5140a360c2SBrad Bishop if (rc == 0): 5240a360c2SBrad Bishop self.DownloadComplete(outfile, filename) 5340a360c2SBrad Bishop else: 5440a360c2SBrad Bishop self.DownloadError(filename) 5540a360c2SBrad Bishop 5640a360c2SBrad Bishop except Exception as e: 57*d65b2d50SCamVan Nguyen print("ERROR DownloadManager: "+str(e)) 5840a360c2SBrad Bishop self.DownloadError(filename) 5940a360c2SBrad Bishop 60f5110e11SBrad Bishop # TODO: this needs to be deprecated. 61f5110e11SBrad Bishop # Shouldn't call flash interface from here 6240a360c2SBrad Bishop def DownloadHandler(self, url, filename, path=None): 6340a360c2SBrad Bishop try: 6440a360c2SBrad Bishop filename = str(filename) 65*d65b2d50SCamVan Nguyen print("Downloading: "+filename+" from "+url) 661902990dSGunnar Mills outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename) 67f5110e11SBrad Bishop subprocess.call( 68f5110e11SBrad Bishop ["tftp", "-l", outfile, "-r", filename, "-g", url]) 6940a360c2SBrad Bishop obj = bus.get_object("org.openbmc.control.Flash", path) 7040a360c2SBrad Bishop intf = dbus.Interface(obj, "org.openbmc.Flash") 7140a360c2SBrad Bishop intf.update(outfile) 7240a360c2SBrad Bishop 7340a360c2SBrad Bishop except Exception as e: 74*d65b2d50SCamVan Nguyen print("ERROR DownloadManager: "+str(e)) 7540a360c2SBrad Bishop obj = bus.get_object("org.openbmc.control.Flash", path) 7640a360c2SBrad Bishop intf = dbus.Interface(obj, "org.openbmc.Flash") 7740a360c2SBrad Bishop intf.error("Download Error: "+filename) 7840a360c2SBrad Bishop 7940a360c2SBrad Bishop 8040a360c2SBrad Bishopif __name__ == '__main__': 8140a360c2SBrad Bishop dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 8240a360c2SBrad Bishop bus = get_dbus() 8340a360c2SBrad Bishop obj = DownloadManagerObject(bus, OBJ_NAME) 8440a360c2SBrad Bishop mainloop = gobject.MainLoop() 8570852a38SBrad Bishop name = dbus.service.BusName(DBUS_NAME, bus) 8640a360c2SBrad Bishop 87*d65b2d50SCamVan Nguyen print("Running Download Manager") 8840a360c2SBrad Bishop mainloop.run() 8953066750SBrad Bishop 9053066750SBrad Bishop# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 91