1*40a360c2SBrad Bishop#!/usr/bin/python -u
2*40a360c2SBrad Bishop
3*40a360c2SBrad Bishopimport sys
4*40a360c2SBrad Bishopimport gobject
5*40a360c2SBrad Bishopimport dbus
6*40a360c2SBrad Bishopimport dbus.service
7*40a360c2SBrad Bishopimport dbus.mainloop.glib
8*40a360c2SBrad Bishopimport subprocess
9*40a360c2SBrad Bishopfrom obmc.dbuslib.bindings import get_dbus
10*40a360c2SBrad Bishop
11*40a360c2SBrad Bishopif (len(sys.argv) < 2):
12*40a360c2SBrad Bishop	print "Usage:  download_manager.py [system name]"
13*40a360c2SBrad Bishop	exit(1)
14*40a360c2SBrad BishopSystem = __import__(sys.argv[1])
15*40a360c2SBrad Bishop
16*40a360c2SBrad Bishop
17*40a360c2SBrad BishopDBUS_NAME = 'org.openbmc.managers.Download'
18*40a360c2SBrad BishopOBJ_NAME = '/org/openbmc/managers/Download'
19*40a360c2SBrad BishopTFTP_PORT = 69
20*40a360c2SBrad Bishop
21*40a360c2SBrad Bishopclass DownloadManagerObject(dbus.service.Object):
22*40a360c2SBrad Bishop	def __init__(self,bus,name):
23*40a360c2SBrad Bishop		dbus.service.Object.__init__(self,bus,name)
24*40a360c2SBrad Bishop		bus.add_signal_receiver(self.DownloadHandler,
25*40a360c2SBrad Bishop			dbus_interface = "org.openbmc.Flash",
26*40a360c2SBrad Bishop			signal_name = "Download", path_keyword = "path")
27*40a360c2SBrad Bishop		bus.add_signal_receiver(self.TftpDownloadHandler,
28*40a360c2SBrad Bishop			signal_name = "TftpDownload", path_keyword = "path")
29*40a360c2SBrad Bishop
30*40a360c2SBrad Bishop
31*40a360c2SBrad Bishop	@dbus.service.signal(DBUS_NAME,signature='ss')
32*40a360c2SBrad Bishop	def DownloadComplete(self,outfile,filename):
33*40a360c2SBrad Bishop		print "Download Complete: "+outfile
34*40a360c2SBrad Bishop		return outfile
35*40a360c2SBrad Bishop
36*40a360c2SBrad Bishop	@dbus.service.signal(DBUS_NAME,signature='s')
37*40a360c2SBrad Bishop	def DownloadError(self,filename):
38*40a360c2SBrad Bishop		pass
39*40a360c2SBrad Bishop
40*40a360c2SBrad Bishop	def TftpDownloadHandler(self,ip,filename,path = None):
41*40a360c2SBrad Bishop		try:
42*40a360c2SBrad Bishop			filename = str(filename)
43*40a360c2SBrad Bishop			print "Downloading: "+filename+" from "+ip
44*40a360c2SBrad Bishop			outfile = System.FLASH_DOWNLOAD_PATH+"/"+filename
45*40a360c2SBrad Bishop			rc = subprocess.call(["tftp", "-l",outfile,"-r",filename,"-g",ip])
46*40a360c2SBrad Bishop			if (rc == 0):
47*40a360c2SBrad Bishop				self.DownloadComplete(outfile,filename)
48*40a360c2SBrad Bishop			else:
49*40a360c2SBrad Bishop				self.DownloadError(filename)
50*40a360c2SBrad Bishop
51*40a360c2SBrad Bishop
52*40a360c2SBrad Bishop		except Exception as e:
53*40a360c2SBrad Bishop			print "ERROR DownloadManager: "+str(e)
54*40a360c2SBrad Bishop			self.DownloadError(filename)
55*40a360c2SBrad Bishop
56*40a360c2SBrad Bishop
57*40a360c2SBrad Bishop	## TODO: this needs to be deprecated.  Shouldn't call flash interface from here
58*40a360c2SBrad Bishop	def DownloadHandler(self,url,filename,path = None):
59*40a360c2SBrad Bishop		try:
60*40a360c2SBrad Bishop			filename = str(filename)
61*40a360c2SBrad Bishop			print "Downloading: "+filename+" from "+url
62*40a360c2SBrad Bishop			outfile = System.FLASH_DOWNLOAD_PATH+"/"+filename
63*40a360c2SBrad Bishop			subprocess.call(["tftp", "-l",outfile,"-r",filename,"-g",url])
64*40a360c2SBrad Bishop			obj = bus.get_object("org.openbmc.control.Flash",path)
65*40a360c2SBrad Bishop			intf = dbus.Interface(obj,"org.openbmc.Flash")
66*40a360c2SBrad Bishop			intf.update(outfile)
67*40a360c2SBrad Bishop
68*40a360c2SBrad Bishop		except Exception as e:
69*40a360c2SBrad Bishop			print "ERROR DownloadManager: "+str(e)
70*40a360c2SBrad Bishop			obj = bus.get_object("org.openbmc.control.Flash",path)
71*40a360c2SBrad Bishop			intf = dbus.Interface(obj,"org.openbmc.Flash")
72*40a360c2SBrad Bishop			intf.error("Download Error: "+filename)
73*40a360c2SBrad Bishop
74*40a360c2SBrad Bishop
75*40a360c2SBrad Bishopif __name__ == '__main__':
76*40a360c2SBrad Bishop    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
77*40a360c2SBrad Bishop    bus = get_dbus()
78*40a360c2SBrad Bishop    name = dbus.service.BusName(DBUS_NAME, bus)
79*40a360c2SBrad Bishop    obj = DownloadManagerObject(bus, OBJ_NAME)
80*40a360c2SBrad Bishop    mainloop = gobject.MainLoop()
81*40a360c2SBrad Bishop
82*40a360c2SBrad Bishop    print "Running Download Manager"
83*40a360c2SBrad Bishop    mainloop.run()
84*40a360c2SBrad Bishop
85