Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 2 | |
Gunnar Mills | 1902990 | 2017-10-06 14:47:10 -0500 | [diff] [blame] | 3 | import os |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame^] | 4 | # TODO: openbmc/openbmc#2994 remove python 2 support |
| 5 | try: # python 2 |
| 6 | import gobject |
| 7 | except ImportError: # python 3 |
| 8 | from gi.repository import GObject as gobject |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 9 | import dbus |
| 10 | import dbus.service |
| 11 | import dbus.mainloop.glib |
Norman James | 0b73e7d | 2016-01-19 14:03:50 -0600 | [diff] [blame] | 12 | import subprocess |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 13 | from obmc.dbuslib.bindings import get_dbus |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 14 | |
| 15 | |
Brad Bishop | fa06665 | 2016-07-06 12:50:17 -0400 | [diff] [blame] | 16 | FLASH_DOWNLOAD_PATH = '/tmp' |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 17 | DBUS_NAME = 'org.openbmc.managers.Download' |
| 18 | OBJ_NAME = '/org/openbmc/managers/Download' |
| 19 | TFTP_PORT = 69 |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 20 | |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 21 | |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 22 | class DownloadManagerObject(dbus.service.Object): |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 23 | 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 James | 8585b21 | 2016-01-29 08:08:30 -0600 | [diff] [blame] | 34 | |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 35 | @dbus.service.signal(DBUS_NAME, signature='ss') |
| 36 | def DownloadComplete(self, outfile, filename): |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame^] | 37 | print("Download Complete: "+outfile) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 38 | return outfile |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 39 | |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 40 | @dbus.service.signal(DBUS_NAME, signature='s') |
| 41 | def DownloadError(self, filename): |
| 42 | pass |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 43 | |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 44 | def TftpDownloadHandler(self, ip, filename, path=None): |
| 45 | try: |
| 46 | filename = str(filename) |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame^] | 47 | print("Downloading: "+filename+" from "+ip) |
Gunnar Mills | 1902990 | 2017-10-06 14:47:10 -0500 | [diff] [blame] | 48 | outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 49 | 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 James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 55 | |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 56 | except Exception as e: |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame^] | 57 | print("ERROR DownloadManager: "+str(e)) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 58 | self.DownloadError(filename) |
Norman James | 8585b21 | 2016-01-29 08:08:30 -0600 | [diff] [blame] | 59 | |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 60 | # 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 Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame^] | 65 | print("Downloading: "+filename+" from "+url) |
Gunnar Mills | 1902990 | 2017-10-06 14:47:10 -0500 | [diff] [blame] | 66 | outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 67 | 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 James | 8585b21 | 2016-01-29 08:08:30 -0600 | [diff] [blame] | 72 | |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 73 | except Exception as e: |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame^] | 74 | print("ERROR DownloadManager: "+str(e)) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 75 | obj = bus.get_object("org.openbmc.control.Flash", path) |
| 76 | intf = dbus.Interface(obj, "org.openbmc.Flash") |
| 77 | intf.error("Download Error: "+filename) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 78 | |
| 79 | |
| 80 | if __name__ == '__main__': |
| 81 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 82 | bus = get_dbus() |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 83 | obj = DownloadManagerObject(bus, OBJ_NAME) |
| 84 | mainloop = gobject.MainLoop() |
Brad Bishop | 70852a3 | 2016-06-29 22:58:51 -0400 | [diff] [blame] | 85 | name = dbus.service.BusName(DBUS_NAME, bus) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 86 | |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame^] | 87 | print("Running Download Manager") |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 88 | mainloop.run() |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 89 | |
| 90 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |