blob: 4e574ef67980aaefaef0fd0531e1e0b12acb0a67 [file] [log] [blame]
Brad Bishopf5110e12016-08-30 19:28:45 -04001#!/usr/bin/env python
Norman Jamesf066e872015-10-07 15:29:51 -05002
Norman Jamesf066e872015-10-07 15:29:51 -05003import gobject
4import dbus
5import dbus.service
6import dbus.mainloop.glib
Norman James0b73e7d2016-01-19 14:03:50 -06007import subprocess
Brad Bishop84e73b52016-05-12 15:57:52 -04008from obmc.dbuslib.bindings import get_dbus
Norman Jamesf066e872015-10-07 15:29:51 -05009
10
Brad Bishopfa066652016-07-06 12:50:17 -040011FLASH_DOWNLOAD_PATH = '/tmp'
Norman Jamesf066e872015-10-07 15:29:51 -050012DBUS_NAME = 'org.openbmc.managers.Download'
13OBJ_NAME = '/org/openbmc/managers/Download'
14TFTP_PORT = 69
Norman Jamesf066e872015-10-07 15:29:51 -050015
Brad Bishopf5110e12016-08-30 19:28:45 -040016
Norman Jamesf066e872015-10-07 15:29:51 -050017class DownloadManagerObject(dbus.service.Object):
Brad Bishopf5110e12016-08-30 19:28:45 -040018 def __init__(self, bus, name):
19 dbus.service.Object.__init__(self, bus, name)
20 bus.add_signal_receiver(
21 self.DownloadHandler,
22 dbus_interface="org.openbmc.Flash",
23 signal_name="Download",
24 path_keyword="path")
25 bus.add_signal_receiver(
26 self.TftpDownloadHandler,
27 signal_name="TftpDownload",
28 path_keyword="path")
Norman James8585b212016-01-29 08:08:30 -060029
Brad Bishopf5110e12016-08-30 19:28:45 -040030 @dbus.service.signal(DBUS_NAME, signature='ss')
31 def DownloadComplete(self, outfile, filename):
32 print "Download Complete: "+outfile
33 return outfile
Norman Jamesf066e872015-10-07 15:29:51 -050034
Brad Bishopf5110e12016-08-30 19:28:45 -040035 @dbus.service.signal(DBUS_NAME, signature='s')
36 def DownloadError(self, filename):
37 pass
Norman Jamesf066e872015-10-07 15:29:51 -050038
Brad Bishopf5110e12016-08-30 19:28:45 -040039 def TftpDownloadHandler(self, ip, filename, path=None):
40 try:
41 filename = str(filename)
42 print "Downloading: "+filename+" from "+ip
43 outfile = FLASH_DOWNLOAD_PATH+"/"+filename
44 rc = subprocess.call(
45 ["tftp", "-l", outfile, "-r", filename, "-g", ip])
46 if (rc == 0):
47 self.DownloadComplete(outfile, filename)
48 else:
49 self.DownloadError(filename)
Norman Jamesf066e872015-10-07 15:29:51 -050050
Brad Bishopf5110e12016-08-30 19:28:45 -040051 except Exception as e:
52 print "ERROR DownloadManager: "+str(e)
53 self.DownloadError(filename)
Norman James8585b212016-01-29 08:08:30 -060054
Brad Bishopf5110e12016-08-30 19:28:45 -040055 # TODO: this needs to be deprecated.
56 # Shouldn't call flash interface from here
57 def DownloadHandler(self, url, filename, path=None):
58 try:
59 filename = str(filename)
60 print "Downloading: "+filename+" from "+url
61 outfile = FLASH_DOWNLOAD_PATH+"/"+filename
62 subprocess.call(
63 ["tftp", "-l", outfile, "-r", filename, "-g", url])
64 obj = bus.get_object("org.openbmc.control.Flash", path)
65 intf = dbus.Interface(obj, "org.openbmc.Flash")
66 intf.update(outfile)
Norman James8585b212016-01-29 08:08:30 -060067
Brad Bishopf5110e12016-08-30 19:28:45 -040068 except Exception as e:
69 print "ERROR DownloadManager: "+str(e)
70 obj = bus.get_object("org.openbmc.control.Flash", path)
71 intf = dbus.Interface(obj, "org.openbmc.Flash")
72 intf.error("Download Error: "+filename)
Norman Jamesf066e872015-10-07 15:29:51 -050073
74
75if __name__ == '__main__':
76 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040077 bus = get_dbus()
Norman Jamesf066e872015-10-07 15:29:51 -050078 obj = DownloadManagerObject(bus, OBJ_NAME)
79 mainloop = gobject.MainLoop()
Brad Bishop70852a32016-06-29 22:58:51 -040080 name = dbus.service.BusName(DBUS_NAME, bus)
Brad Bishopf5110e12016-08-30 19:28:45 -040081
Norman Jamesf066e872015-10-07 15:29:51 -050082 print "Running Download Manager"
83 mainloop.run()