blob: 7fb600fc3898f62b64e705ec9915f122b2d705c6 [file] [log] [blame]
Brad Bishopf5110e12016-08-30 19:28:45 -04001#!/usr/bin/env python
Norman Jamesf066e872015-10-07 15:29:51 -05002
Gunnar Mills19029902017-10-06 14:47:10 -05003import os
CamVan Nguyend65b2d52018-02-27 15:14:41 -06004# TODO: openbmc/openbmc#2994 remove python 2 support
5try: # python 2
6 import gobject
7except ImportError: # python 3
8 from gi.repository import GObject as gobject
Norman Jamesf066e872015-10-07 15:29:51 -05009import dbus
10import dbus.service
11import dbus.mainloop.glib
Norman James0b73e7d2016-01-19 14:03:50 -060012import subprocess
Brad Bishop84e73b52016-05-12 15:57:52 -040013from obmc.dbuslib.bindings import get_dbus
Norman Jamesf066e872015-10-07 15:29:51 -050014
15
Brad Bishopfa066652016-07-06 12:50:17 -040016FLASH_DOWNLOAD_PATH = '/tmp'
Norman Jamesf066e872015-10-07 15:29:51 -050017DBUS_NAME = 'org.openbmc.managers.Download'
18OBJ_NAME = '/org/openbmc/managers/Download'
19TFTP_PORT = 69
Norman Jamesf066e872015-10-07 15:29:51 -050020
Brad Bishopf5110e12016-08-30 19:28:45 -040021
Norman Jamesf066e872015-10-07 15:29:51 -050022class DownloadManagerObject(dbus.service.Object):
Brad Bishopf5110e12016-08-30 19:28:45 -040023 def __init__(self, bus, name):
24 dbus.service.Object.__init__(self, bus, name)
25 bus.add_signal_receiver(
26 self.DownloadHandler,
27 dbus_interface="org.openbmc.Flash",
28 signal_name="Download",
29 path_keyword="path")
30 bus.add_signal_receiver(
31 self.TftpDownloadHandler,
32 signal_name="TftpDownload",
33 path_keyword="path")
Norman James8585b212016-01-29 08:08:30 -060034
Brad Bishopf5110e12016-08-30 19:28:45 -040035 @dbus.service.signal(DBUS_NAME, signature='ss')
36 def DownloadComplete(self, outfile, filename):
CamVan Nguyend65b2d52018-02-27 15:14:41 -060037 print("Download Complete: "+outfile)
Brad Bishopf5110e12016-08-30 19:28:45 -040038 return outfile
Norman Jamesf066e872015-10-07 15:29:51 -050039
Brad Bishopf5110e12016-08-30 19:28:45 -040040 @dbus.service.signal(DBUS_NAME, signature='s')
41 def DownloadError(self, filename):
42 pass
Norman Jamesf066e872015-10-07 15:29:51 -050043
Brad Bishopf5110e12016-08-30 19:28:45 -040044 def TftpDownloadHandler(self, ip, filename, path=None):
45 try:
46 filename = str(filename)
CamVan Nguyend65b2d52018-02-27 15:14:41 -060047 print("Downloading: "+filename+" from "+ip)
Gunnar Mills19029902017-10-06 14:47:10 -050048 outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename)
Brad Bishopf5110e12016-08-30 19:28:45 -040049 rc = subprocess.call(
50 ["tftp", "-l", outfile, "-r", filename, "-g", ip])
51 if (rc == 0):
52 self.DownloadComplete(outfile, filename)
53 else:
54 self.DownloadError(filename)
Norman Jamesf066e872015-10-07 15:29:51 -050055
Brad Bishopf5110e12016-08-30 19:28:45 -040056 except Exception as e:
CamVan Nguyend65b2d52018-02-27 15:14:41 -060057 print("ERROR DownloadManager: "+str(e))
Brad Bishopf5110e12016-08-30 19:28:45 -040058 self.DownloadError(filename)
Norman James8585b212016-01-29 08:08:30 -060059
Brad Bishopf5110e12016-08-30 19:28:45 -040060 # TODO: this needs to be deprecated.
61 # Shouldn't call flash interface from here
62 def DownloadHandler(self, url, filename, path=None):
63 try:
64 filename = str(filename)
CamVan Nguyend65b2d52018-02-27 15:14:41 -060065 print("Downloading: "+filename+" from "+url)
Gunnar Mills19029902017-10-06 14:47:10 -050066 outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename)
Brad Bishopf5110e12016-08-30 19:28:45 -040067 subprocess.call(
68 ["tftp", "-l", outfile, "-r", filename, "-g", url])
69 obj = bus.get_object("org.openbmc.control.Flash", path)
70 intf = dbus.Interface(obj, "org.openbmc.Flash")
71 intf.update(outfile)
Norman James8585b212016-01-29 08:08:30 -060072
Brad Bishopf5110e12016-08-30 19:28:45 -040073 except Exception as e:
CamVan Nguyend65b2d52018-02-27 15:14:41 -060074 print("ERROR DownloadManager: "+str(e))
Brad Bishopf5110e12016-08-30 19:28:45 -040075 obj = bus.get_object("org.openbmc.control.Flash", path)
76 intf = dbus.Interface(obj, "org.openbmc.Flash")
77 intf.error("Download Error: "+filename)
Norman Jamesf066e872015-10-07 15:29:51 -050078
79
80if __name__ == '__main__':
81 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040082 bus = get_dbus()
Norman Jamesf066e872015-10-07 15:29:51 -050083 obj = DownloadManagerObject(bus, OBJ_NAME)
84 mainloop = gobject.MainLoop()
Brad Bishop70852a32016-06-29 22:58:51 -040085 name = dbus.service.BusName(DBUS_NAME, bus)
Brad Bishopf5110e12016-08-30 19:28:45 -040086
CamVan Nguyend65b2d52018-02-27 15:14:41 -060087 print("Running Download Manager")
Norman Jamesf066e872015-10-07 15:29:51 -050088 mainloop.run()
Brad Bishop53066752016-09-21 08:48:04 -040089
90# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4