1f5110e11SBrad Bishop#!/usr/bin/env python
240a360c2SBrad Bishop
31902990dSGunnar Millsimport os
475fe8cc4SPatrick 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 Bishop
11*d8c6f5a3SPatrick Williamsimport subprocess
12*d8c6f5a3SPatrick Williams
13*d8c6f5a3SPatrick Williamsimport dbus
14*d8c6f5a3SPatrick Williamsimport dbus.mainloop.glib
15*d8c6f5a3SPatrick Williamsimport dbus.service
16*d8c6f5a3SPatrick Williamsfrom obmc.dbuslib.bindings import get_dbus
1740a360c2SBrad Bishop
1875fe8cc4SPatrick WilliamsFLASH_DOWNLOAD_PATH = "/tmp"
1975fe8cc4SPatrick WilliamsDBUS_NAME = "org.openbmc.managers.Download"
2075fe8cc4SPatrick WilliamsOBJ_NAME = "/org/openbmc/managers/Download"
2140a360c2SBrad BishopTFTP_PORT = 69
2240a360c2SBrad Bishop
23f5110e11SBrad Bishop
2440a360c2SBrad Bishopclass DownloadManagerObject(dbus.service.Object):
2540a360c2SBrad Bishop    def __init__(self, bus, name):
2640a360c2SBrad Bishop        dbus.service.Object.__init__(self, bus, name)
27f5110e11SBrad Bishop        bus.add_signal_receiver(
28f5110e11SBrad Bishop            self.DownloadHandler,
2940a360c2SBrad Bishop            dbus_interface="org.openbmc.Flash",
30f5110e11SBrad Bishop            signal_name="Download",
3175fe8cc4SPatrick Williams            path_keyword="path",
3275fe8cc4SPatrick Williams        )
33f5110e11SBrad Bishop        bus.add_signal_receiver(
34f5110e11SBrad Bishop            self.TftpDownloadHandler,
35f5110e11SBrad Bishop            signal_name="TftpDownload",
3675fe8cc4SPatrick Williams            path_keyword="path",
3775fe8cc4SPatrick Williams        )
3840a360c2SBrad Bishop
3975fe8cc4SPatrick Williams    @dbus.service.signal(DBUS_NAME, signature="ss")
4040a360c2SBrad Bishop    def DownloadComplete(self, outfile, filename):
41d65b2d50SCamVan Nguyen        print("Download Complete: " + outfile)
4240a360c2SBrad Bishop        return outfile
4340a360c2SBrad Bishop
4475fe8cc4SPatrick Williams    @dbus.service.signal(DBUS_NAME, signature="s")
4540a360c2SBrad Bishop    def DownloadError(self, filename):
4640a360c2SBrad Bishop        pass
4740a360c2SBrad Bishop
4840a360c2SBrad Bishop    def TftpDownloadHandler(self, ip, filename, path=None):
4940a360c2SBrad Bishop        try:
5040a360c2SBrad Bishop            filename = str(filename)
51d65b2d50SCamVan Nguyen            print("Downloading: " + filename + " from " + ip)
521902990dSGunnar Mills            outfile = FLASH_DOWNLOAD_PATH + "/" + os.path.basename(filename)
53f5110e11SBrad Bishop            rc = subprocess.call(
5475fe8cc4SPatrick Williams                ["tftp", "-l", outfile, "-r", filename, "-g", ip]
5575fe8cc4SPatrick Williams            )
5675fe8cc4SPatrick Williams            if rc == 0:
5740a360c2SBrad Bishop                self.DownloadComplete(outfile, filename)
5840a360c2SBrad Bishop            else:
5940a360c2SBrad Bishop                self.DownloadError(filename)
6040a360c2SBrad Bishop
6140a360c2SBrad Bishop        except Exception as e:
62d65b2d50SCamVan Nguyen            print("ERROR DownloadManager: " + str(e))
6340a360c2SBrad Bishop            self.DownloadError(filename)
6440a360c2SBrad Bishop
65f5110e11SBrad Bishop    # TODO: this needs to be deprecated.
66f5110e11SBrad Bishop    # Shouldn't call flash interface from here
6740a360c2SBrad Bishop    def DownloadHandler(self, url, filename, path=None):
6840a360c2SBrad Bishop        try:
6940a360c2SBrad Bishop            filename = str(filename)
70d65b2d50SCamVan Nguyen            print("Downloading: " + filename + " from " + url)
711902990dSGunnar Mills            outfile = FLASH_DOWNLOAD_PATH + "/" + os.path.basename(filename)
7275fe8cc4SPatrick Williams            subprocess.call(["tftp", "-l", outfile, "-r", filename, "-g", url])
7340a360c2SBrad Bishop            obj = bus.get_object("org.openbmc.control.Flash", path)
7440a360c2SBrad Bishop            intf = dbus.Interface(obj, "org.openbmc.Flash")
7540a360c2SBrad Bishop            intf.update(outfile)
7640a360c2SBrad Bishop
7740a360c2SBrad Bishop        except Exception as e:
78d65b2d50SCamVan Nguyen            print("ERROR DownloadManager: " + str(e))
7940a360c2SBrad Bishop            obj = bus.get_object("org.openbmc.control.Flash", path)
8040a360c2SBrad Bishop            intf = dbus.Interface(obj, "org.openbmc.Flash")
8140a360c2SBrad Bishop            intf.error("Download Error: " + filename)
8240a360c2SBrad Bishop
8340a360c2SBrad Bishop
8475fe8cc4SPatrick Williamsif __name__ == "__main__":
8540a360c2SBrad Bishop    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
8640a360c2SBrad Bishop    bus = get_dbus()
8740a360c2SBrad Bishop    obj = DownloadManagerObject(bus, OBJ_NAME)
8840a360c2SBrad Bishop    mainloop = gobject.MainLoop()
8970852a38SBrad Bishop    name = dbus.service.BusName(DBUS_NAME, bus)
9040a360c2SBrad Bishop
91d65b2d50SCamVan Nguyen    print("Running Download Manager")
9240a360c2SBrad Bishop    mainloop.run()
9353066750SBrad Bishop
9453066750SBrad Bishop# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
95