140a360c2SBrad Bishop#!/usr/bin/python -u
240a360c2SBrad Bishop
340a360c2SBrad Bishopimport sys
440a360c2SBrad Bishopimport gobject
540a360c2SBrad Bishopimport dbus
640a360c2SBrad Bishopimport dbus.service
740a360c2SBrad Bishopimport dbus.mainloop.glib
840a360c2SBrad Bishopimport subprocess
940a360c2SBrad Bishopfrom obmc.dbuslib.bindings import get_dbus
1040a360c2SBrad Bishop
1140a360c2SBrad Bishop
12*fa06665fSBrad BishopFLASH_DOWNLOAD_PATH = '/tmp'
1340a360c2SBrad BishopDBUS_NAME = 'org.openbmc.managers.Download'
1440a360c2SBrad BishopOBJ_NAME = '/org/openbmc/managers/Download'
1540a360c2SBrad BishopTFTP_PORT = 69
1640a360c2SBrad Bishop
1740a360c2SBrad Bishopclass DownloadManagerObject(dbus.service.Object):
1840a360c2SBrad Bishop	def __init__(self,bus,name):
1940a360c2SBrad Bishop		dbus.service.Object.__init__(self,bus,name)
2040a360c2SBrad Bishop		bus.add_signal_receiver(self.DownloadHandler,
2140a360c2SBrad Bishop			dbus_interface = "org.openbmc.Flash",
2240a360c2SBrad Bishop			signal_name = "Download", path_keyword = "path")
2340a360c2SBrad Bishop		bus.add_signal_receiver(self.TftpDownloadHandler,
2440a360c2SBrad Bishop			signal_name = "TftpDownload", path_keyword = "path")
2540a360c2SBrad Bishop
2640a360c2SBrad Bishop
2740a360c2SBrad Bishop	@dbus.service.signal(DBUS_NAME,signature='ss')
2840a360c2SBrad Bishop	def DownloadComplete(self,outfile,filename):
2940a360c2SBrad Bishop		print "Download Complete: "+outfile
3040a360c2SBrad Bishop		return outfile
3140a360c2SBrad Bishop
3240a360c2SBrad Bishop	@dbus.service.signal(DBUS_NAME,signature='s')
3340a360c2SBrad Bishop	def DownloadError(self,filename):
3440a360c2SBrad Bishop		pass
3540a360c2SBrad Bishop
3640a360c2SBrad Bishop	def TftpDownloadHandler(self,ip,filename,path = None):
3740a360c2SBrad Bishop		try:
3840a360c2SBrad Bishop			filename = str(filename)
3940a360c2SBrad Bishop			print "Downloading: "+filename+" from "+ip
40*fa06665fSBrad Bishop			outfile = FLASH_DOWNLOAD_PATH+"/"+filename
4140a360c2SBrad Bishop			rc = subprocess.call(["tftp", "-l",outfile,"-r",filename,"-g",ip])
4240a360c2SBrad Bishop			if (rc == 0):
4340a360c2SBrad Bishop				self.DownloadComplete(outfile,filename)
4440a360c2SBrad Bishop			else:
4540a360c2SBrad Bishop				self.DownloadError(filename)
4640a360c2SBrad Bishop
4740a360c2SBrad Bishop
4840a360c2SBrad Bishop		except Exception as e:
4940a360c2SBrad Bishop			print "ERROR DownloadManager: "+str(e)
5040a360c2SBrad Bishop			self.DownloadError(filename)
5140a360c2SBrad Bishop
5240a360c2SBrad Bishop
5340a360c2SBrad Bishop	## TODO: this needs to be deprecated.  Shouldn't call flash interface from here
5440a360c2SBrad Bishop	def DownloadHandler(self,url,filename,path = None):
5540a360c2SBrad Bishop		try:
5640a360c2SBrad Bishop			filename = str(filename)
5740a360c2SBrad Bishop			print "Downloading: "+filename+" from "+url
58*fa06665fSBrad Bishop			outfile = FLASH_DOWNLOAD_PATH+"/"+filename
5940a360c2SBrad Bishop			subprocess.call(["tftp", "-l",outfile,"-r",filename,"-g",url])
6040a360c2SBrad Bishop			obj = bus.get_object("org.openbmc.control.Flash",path)
6140a360c2SBrad Bishop			intf = dbus.Interface(obj,"org.openbmc.Flash")
6240a360c2SBrad Bishop			intf.update(outfile)
6340a360c2SBrad Bishop
6440a360c2SBrad Bishop		except Exception as e:
6540a360c2SBrad Bishop			print "ERROR DownloadManager: "+str(e)
6640a360c2SBrad Bishop			obj = bus.get_object("org.openbmc.control.Flash",path)
6740a360c2SBrad Bishop			intf = dbus.Interface(obj,"org.openbmc.Flash")
6840a360c2SBrad Bishop			intf.error("Download Error: "+filename)
6940a360c2SBrad Bishop
7040a360c2SBrad Bishop
7140a360c2SBrad Bishopif __name__ == '__main__':
7240a360c2SBrad Bishop    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
7340a360c2SBrad Bishop    bus = get_dbus()
7440a360c2SBrad Bishop    obj = DownloadManagerObject(bus, OBJ_NAME)
7540a360c2SBrad Bishop    mainloop = gobject.MainLoop()
7670852a38SBrad Bishop    name = dbus.service.BusName(DBUS_NAME, bus)
7740a360c2SBrad Bishop
7840a360c2SBrad Bishop    print "Running Download Manager"
7940a360c2SBrad Bishop    mainloop.run()
8040a360c2SBrad Bishop
81