blob: 7ba80cac4beae49d7a0ae4c3fd5e4cad41118268 [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
Norman Jamesf066e872015-10-07 15:29:51 -05004import gobject
5import dbus
6import dbus.service
7import dbus.mainloop.glib
Norman James0b73e7d2016-01-19 14:03:50 -06008import subprocess
Brad Bishop84e73b52016-05-12 15:57:52 -04009from obmc.dbuslib.bindings import get_dbus
Norman Jamesf066e872015-10-07 15:29:51 -050010
11
Brad Bishopfa066652016-07-06 12:50:17 -040012FLASH_DOWNLOAD_PATH = '/tmp'
Norman Jamesf066e872015-10-07 15:29:51 -050013DBUS_NAME = 'org.openbmc.managers.Download'
14OBJ_NAME = '/org/openbmc/managers/Download'
15TFTP_PORT = 69
Norman Jamesf066e872015-10-07 15:29:51 -050016
Brad Bishopf5110e12016-08-30 19:28:45 -040017
Norman Jamesf066e872015-10-07 15:29:51 -050018class DownloadManagerObject(dbus.service.Object):
Brad Bishopf5110e12016-08-30 19:28:45 -040019 def __init__(self, bus, name):
20 dbus.service.Object.__init__(self, bus, name)
21 bus.add_signal_receiver(
22 self.DownloadHandler,
23 dbus_interface="org.openbmc.Flash",
24 signal_name="Download",
25 path_keyword="path")
26 bus.add_signal_receiver(
27 self.TftpDownloadHandler,
28 signal_name="TftpDownload",
29 path_keyword="path")
Norman James8585b212016-01-29 08:08:30 -060030
Brad Bishopf5110e12016-08-30 19:28:45 -040031 @dbus.service.signal(DBUS_NAME, signature='ss')
32 def DownloadComplete(self, outfile, filename):
33 print "Download Complete: "+outfile
34 return outfile
Norman Jamesf066e872015-10-07 15:29:51 -050035
Brad Bishopf5110e12016-08-30 19:28:45 -040036 @dbus.service.signal(DBUS_NAME, signature='s')
37 def DownloadError(self, filename):
38 pass
Norman Jamesf066e872015-10-07 15:29:51 -050039
Brad Bishopf5110e12016-08-30 19:28:45 -040040 def TftpDownloadHandler(self, ip, filename, path=None):
41 try:
42 filename = str(filename)
43 print "Downloading: "+filename+" from "+ip
Gunnar Mills19029902017-10-06 14:47:10 -050044 outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename)
Brad Bishopf5110e12016-08-30 19:28:45 -040045 rc = subprocess.call(
46 ["tftp", "-l", outfile, "-r", filename, "-g", ip])
47 if (rc == 0):
48 self.DownloadComplete(outfile, filename)
49 else:
50 self.DownloadError(filename)
Norman Jamesf066e872015-10-07 15:29:51 -050051
Brad Bishopf5110e12016-08-30 19:28:45 -040052 except Exception as e:
53 print "ERROR DownloadManager: "+str(e)
54 self.DownloadError(filename)
Norman James8585b212016-01-29 08:08:30 -060055
Brad Bishopf5110e12016-08-30 19:28:45 -040056 # TODO: this needs to be deprecated.
57 # Shouldn't call flash interface from here
58 def DownloadHandler(self, url, filename, path=None):
59 try:
60 filename = str(filename)
61 print "Downloading: "+filename+" from "+url
Gunnar Mills19029902017-10-06 14:47:10 -050062 outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename)
Brad Bishopf5110e12016-08-30 19:28:45 -040063 subprocess.call(
64 ["tftp", "-l", outfile, "-r", filename, "-g", url])
65 obj = bus.get_object("org.openbmc.control.Flash", path)
66 intf = dbus.Interface(obj, "org.openbmc.Flash")
67 intf.update(outfile)
Norman James8585b212016-01-29 08:08:30 -060068
Brad Bishopf5110e12016-08-30 19:28:45 -040069 except Exception as e:
70 print "ERROR DownloadManager: "+str(e)
71 obj = bus.get_object("org.openbmc.control.Flash", path)
72 intf = dbus.Interface(obj, "org.openbmc.Flash")
73 intf.error("Download Error: "+filename)
Norman Jamesf066e872015-10-07 15:29:51 -050074
75
76if __name__ == '__main__':
77 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040078 bus = get_dbus()
Norman Jamesf066e872015-10-07 15:29:51 -050079 obj = DownloadManagerObject(bus, OBJ_NAME)
80 mainloop = gobject.MainLoop()
Brad Bishop70852a32016-06-29 22:58:51 -040081 name = dbus.service.BusName(DBUS_NAME, bus)
Brad Bishopf5110e12016-08-30 19:28:45 -040082
Norman Jamesf066e872015-10-07 15:29:51 -050083 print "Running Download Manager"
84 mainloop.run()
Brad Bishop53066752016-09-21 08:48:04 -040085
86# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4