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