Norman James | 42c1be8 | 2015-10-22 14:34:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/python -u |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 2 | |
| 3 | import sys |
| 4 | import gobject |
| 5 | import dbus |
| 6 | import dbus.service |
| 7 | import dbus.mainloop.glib |
Norman James | 0b73e7d | 2016-01-19 14:03:50 -0600 | [diff] [blame] | 8 | import subprocess |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 9 | from obmc.dbuslib.bindings import get_dbus |
Brad Bishop | 0b380f7 | 2016-06-10 00:29:50 -0400 | [diff] [blame] | 10 | import obmc_system_config as System |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 11 | |
| 12 | |
| 13 | DBUS_NAME = 'org.openbmc.managers.Download' |
| 14 | OBJ_NAME = '/org/openbmc/managers/Download' |
| 15 | TFTP_PORT = 69 |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 16 | |
| 17 | class DownloadManagerObject(dbus.service.Object): |
| 18 | def __init__(self,bus,name): |
| 19 | dbus.service.Object.__init__(self,bus,name) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 20 | bus.add_signal_receiver(self.DownloadHandler, |
Norman James | 8585b21 | 2016-01-29 08:08:30 -0600 | [diff] [blame] | 21 | dbus_interface = "org.openbmc.Flash", |
| 22 | signal_name = "Download", path_keyword = "path") |
| 23 | bus.add_signal_receiver(self.TftpDownloadHandler, |
| 24 | signal_name = "TftpDownload", path_keyword = "path") |
| 25 | |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 26 | |
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 27 | @dbus.service.signal(DBUS_NAME,signature='ss') |
| 28 | def DownloadComplete(self,outfile,filename): |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 29 | print "Download Complete: "+outfile |
| 30 | return outfile |
| 31 | |
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 32 | @dbus.service.signal(DBUS_NAME,signature='s') |
| 33 | def DownloadError(self,filename): |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 34 | pass |
| 35 | |
Norman James | 8585b21 | 2016-01-29 08:08:30 -0600 | [diff] [blame] | 36 | def TftpDownloadHandler(self,ip,filename,path = None): |
| 37 | try: |
| 38 | filename = str(filename) |
| 39 | print "Downloading: "+filename+" from "+ip |
| 40 | outfile = System.FLASH_DOWNLOAD_PATH+"/"+filename |
| 41 | rc = subprocess.call(["tftp", "-l",outfile,"-r",filename,"-g",ip]) |
| 42 | if (rc == 0): |
| 43 | self.DownloadComplete(outfile,filename) |
| 44 | else: |
| 45 | self.DownloadError(filename) |
| 46 | |
| 47 | |
| 48 | except Exception as e: |
| 49 | print "ERROR DownloadManager: "+str(e) |
| 50 | self.DownloadError(filename) |
| 51 | |
| 52 | |
| 53 | ## TODO: this needs to be deprecated. Shouldn't call flash interface from here |
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 54 | def DownloadHandler(self,url,filename,path = None): |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 55 | try: |
| 56 | filename = str(filename) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 57 | print "Downloading: "+filename+" from "+url |
Norman James | 408920d | 2015-10-08 07:02:45 -0500 | [diff] [blame] | 58 | outfile = System.FLASH_DOWNLOAD_PATH+"/"+filename |
Norman James | 8af7d4a | 2016-01-06 14:09:08 -0600 | [diff] [blame] | 59 | subprocess.call(["tftp", "-l",outfile,"-r",filename,"-g",url]) |
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 60 | obj = bus.get_object("org.openbmc.control.Flash",path) |
| 61 | intf = dbus.Interface(obj,"org.openbmc.Flash") |
| 62 | intf.update(outfile) |
Norman James | 8af7d4a | 2016-01-06 14:09:08 -0600 | [diff] [blame] | 63 | |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 64 | except Exception as e: |
| 65 | print "ERROR DownloadManager: "+str(e) |
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 66 | obj = bus.get_object("org.openbmc.control.Flash",path) |
| 67 | intf = dbus.Interface(obj,"org.openbmc.Flash") |
| 68 | intf.error("Download Error: "+filename) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 69 | |
| 70 | |
| 71 | if __name__ == '__main__': |
| 72 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 73 | bus = get_dbus() |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 74 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 75 | obj = DownloadManagerObject(bus, OBJ_NAME) |
| 76 | mainloop = gobject.MainLoop() |
| 77 | |
| 78 | print "Running Download Manager" |
| 79 | mainloop.run() |
| 80 | |