1#!/usr/bin/env python
2
3import os
4import gobject
5import dbus
6import dbus.service
7import dbus.mainloop.glib
8import subprocess
9from obmc.dbuslib.bindings import get_dbus
10
11
12FLASH_DOWNLOAD_PATH = '/tmp'
13DBUS_NAME = 'org.openbmc.managers.Download'
14OBJ_NAME = '/org/openbmc/managers/Download'
15TFTP_PORT = 69
16
17
18class DownloadManagerObject(dbus.service.Object):
19    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")
30
31    @dbus.service.signal(DBUS_NAME, signature='ss')
32    def DownloadComplete(self, outfile, filename):
33        print "Download Complete: "+outfile
34        return outfile
35
36    @dbus.service.signal(DBUS_NAME, signature='s')
37    def DownloadError(self, filename):
38        pass
39
40    def TftpDownloadHandler(self, ip, filename, path=None):
41        try:
42            filename = str(filename)
43            print "Downloading: "+filename+" from "+ip
44            outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename)
45            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)
51
52        except Exception as e:
53            print "ERROR DownloadManager: "+str(e)
54            self.DownloadError(filename)
55
56    # 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
62            outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename)
63            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)
68
69        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)
74
75
76if __name__ == '__main__':
77    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
78    bus = get_dbus()
79    obj = DownloadManagerObject(bus, OBJ_NAME)
80    mainloop = gobject.MainLoop()
81    name = dbus.service.BusName(DBUS_NAME, bus)
82
83    print "Running Download Manager"
84    mainloop.run()
85
86# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
87