1f5110e11SBrad Bishop#!/usr/bin/env python
240a360c2SBrad Bishop
31902990dSGunnar Millsimport os
4*75fe8cc4SPatrick Williams
5d65b2d50SCamVan Nguyen# TODO: openbmc/openbmc#2994 remove python 2 support
6d65b2d50SCamVan Nguyentry:  # python 2
740a360c2SBrad Bishop    import gobject
8d65b2d50SCamVan Nguyenexcept ImportError:  # python 3
9d65b2d50SCamVan Nguyen    from gi.repository import GObject as gobject
1040a360c2SBrad Bishopimport dbus
1140a360c2SBrad Bishopimport dbus.service
1240a360c2SBrad Bishopimport dbus.mainloop.glib
1340a360c2SBrad Bishopimport subprocess
1440a360c2SBrad Bishopfrom obmc.dbuslib.bindings import get_dbus
1540a360c2SBrad Bishop
1640a360c2SBrad Bishop
17*75fe8cc4SPatrick WilliamsFLASH_DOWNLOAD_PATH = "/tmp"
18*75fe8cc4SPatrick WilliamsDBUS_NAME = "org.openbmc.managers.Download"
19*75fe8cc4SPatrick WilliamsOBJ_NAME = "/org/openbmc/managers/Download"
2040a360c2SBrad BishopTFTP_PORT = 69
2140a360c2SBrad Bishop
22f5110e11SBrad Bishop
2340a360c2SBrad Bishopclass DownloadManagerObject(dbus.service.Object):
2440a360c2SBrad Bishop    def __init__(self, bus, name):
2540a360c2SBrad Bishop        dbus.service.Object.__init__(self, bus, name)
26f5110e11SBrad Bishop        bus.add_signal_receiver(
27f5110e11SBrad Bishop            self.DownloadHandler,
2840a360c2SBrad Bishop            dbus_interface="org.openbmc.Flash",
29f5110e11SBrad Bishop            signal_name="Download",
30*75fe8cc4SPatrick Williams            path_keyword="path",
31*75fe8cc4SPatrick Williams        )
32f5110e11SBrad Bishop        bus.add_signal_receiver(
33f5110e11SBrad Bishop            self.TftpDownloadHandler,
34f5110e11SBrad Bishop            signal_name="TftpDownload",
35*75fe8cc4SPatrick Williams            path_keyword="path",
36*75fe8cc4SPatrick Williams        )
3740a360c2SBrad Bishop
38*75fe8cc4SPatrick Williams    @dbus.service.signal(DBUS_NAME, signature="ss")
3940a360c2SBrad Bishop    def DownloadComplete(self, outfile, filename):
40d65b2d50SCamVan Nguyen        print("Download Complete: " + outfile)
4140a360c2SBrad Bishop        return outfile
4240a360c2SBrad Bishop
43*75fe8cc4SPatrick Williams    @dbus.service.signal(DBUS_NAME, signature="s")
4440a360c2SBrad Bishop    def DownloadError(self, filename):
4540a360c2SBrad Bishop        pass
4640a360c2SBrad Bishop
4740a360c2SBrad Bishop    def TftpDownloadHandler(self, ip, filename, path=None):
4840a360c2SBrad Bishop        try:
4940a360c2SBrad Bishop            filename = str(filename)
50d65b2d50SCamVan Nguyen            print("Downloading: " + filename + " from " + ip)
511902990dSGunnar Mills            outfile = FLASH_DOWNLOAD_PATH + "/" + os.path.basename(filename)
52f5110e11SBrad Bishop            rc = subprocess.call(
53*75fe8cc4SPatrick Williams                ["tftp", "-l", outfile, "-r", filename, "-g", ip]
54*75fe8cc4SPatrick Williams            )
55*75fe8cc4SPatrick Williams            if rc == 0:
5640a360c2SBrad Bishop                self.DownloadComplete(outfile, filename)
5740a360c2SBrad Bishop            else:
5840a360c2SBrad Bishop                self.DownloadError(filename)
5940a360c2SBrad Bishop
6040a360c2SBrad Bishop        except Exception as e:
61d65b2d50SCamVan Nguyen            print("ERROR DownloadManager: " + str(e))
6240a360c2SBrad Bishop            self.DownloadError(filename)
6340a360c2SBrad Bishop
64f5110e11SBrad Bishop    # TODO: this needs to be deprecated.
65f5110e11SBrad Bishop    # Shouldn't call flash interface from here
6640a360c2SBrad Bishop    def DownloadHandler(self, url, filename, path=None):
6740a360c2SBrad Bishop        try:
6840a360c2SBrad Bishop            filename = str(filename)
69d65b2d50SCamVan Nguyen            print("Downloading: " + filename + " from " + url)
701902990dSGunnar Mills            outfile = FLASH_DOWNLOAD_PATH + "/" + os.path.basename(filename)
71*75fe8cc4SPatrick Williams            subprocess.call(["tftp", "-l", outfile, "-r", filename, "-g", url])
7240a360c2SBrad Bishop            obj = bus.get_object("org.openbmc.control.Flash", path)
7340a360c2SBrad Bishop            intf = dbus.Interface(obj, "org.openbmc.Flash")
7440a360c2SBrad Bishop            intf.update(outfile)
7540a360c2SBrad Bishop
7640a360c2SBrad Bishop        except Exception as e:
77d65b2d50SCamVan Nguyen            print("ERROR DownloadManager: " + str(e))
7840a360c2SBrad Bishop            obj = bus.get_object("org.openbmc.control.Flash", path)
7940a360c2SBrad Bishop            intf = dbus.Interface(obj, "org.openbmc.Flash")
8040a360c2SBrad Bishop            intf.error("Download Error: " + filename)
8140a360c2SBrad Bishop
8240a360c2SBrad Bishop
83*75fe8cc4SPatrick Williamsif __name__ == "__main__":
8440a360c2SBrad Bishop    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
8540a360c2SBrad Bishop    bus = get_dbus()
8640a360c2SBrad Bishop    obj = DownloadManagerObject(bus, OBJ_NAME)
8740a360c2SBrad Bishop    mainloop = gobject.MainLoop()
8870852a38SBrad Bishop    name = dbus.service.BusName(DBUS_NAME, bus)
8940a360c2SBrad Bishop
90d65b2d50SCamVan Nguyen    print("Running Download Manager")
9140a360c2SBrad Bishop    mainloop.run()
9253066750SBrad Bishop
9353066750SBrad Bishop# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
94