def flash_binary(fdesc, binary, base_address, device_class, destinations,
                 page_size=2048):
    """
    Writes a full binary to the flash using the given file descriptor.

    It also takes the binary image, the base address and the device class as
    parameters.
    """

    print("Erasing pages...")
    pbar = progressbar.ProgressBar(maxval=len(binary)).start()

    # First erase all pages
    for offset in range(0, len(binary), page_size):
        erase_command = commands.encode_erase_flash_page(base_address + offset,
                                                         device_class)
        res = utils.write_command_retry(fdesc, erase_command, destinations)

        failed_boards = [str(id) for id, success in res.items()
                         if not msgpack.unpackb(success)]

        if failed_boards:
            msg = ", ".join(failed_boards)
            msg = "Boards {} failed during page erase, aborting...".format(msg)
            logging.critical(msg)
            sys.exit(2)

        pbar.update(offset)

    pbar.finish()

    print("Writing pages...")
    pbar = progressbar.ProgressBar(maxval=len(binary)).start()

    # Then write all pages in chunks
    for offset, chunk in enumerate(page.slice_into_pages(binary, page_size)):
        offset *= page_size
        command = commands.encode_write_flash(chunk,
                                              base_address + offset,
                                              device_class)

        res = utils.write_command_retry(fdesc, command, destinations)
        failed_boards = [str(id) for id, success in res.items()
                         if not msgpack.unpackb(success)]

        if failed_boards:
            msg = ", ".join(failed_boards)
            msg = "Boards {} failed during page write, aborting...".format(msg)
            logging.critical(msg)
            sys.exit(2)

        pbar.update(offset)
    pbar.finish()

    # Finally update application CRC and size in config
    config = dict()
    config['application_size'] = len(binary)
    config['application_crc'] = crc32(binary)
    utils.config_update_and_save(fdesc, config, destinations)
Esempio n. 2
0
def main():
    args = parse_commandline_args()
    connection = utils.open_connection(args)

    config = {"ID": args.new}
    utils.write_command_retry(
        connection, commands.encode_update_config(config), [args.old]
    )
    utils.write_command_retry(connection, commands.encode_save_config(), [args.new])
Esempio n. 3
0
def main():
    args = parse_commandline_args()
    connection = utils.open_connection(args)

    config = {"ID": args.new}
    utils.write_command_retry(connection,
                              commands.encode_update_config(config),
                              [args.old])
    utils.write_command_retry(connection,
                              commands.encode_save_config(),
                              [args.new])
Esempio n. 4
0
def main():
    args = parse_commandline_args()
    connection = utils.open_connection(args)

    if args.all:
        scan_queue = list()

        # Broadcast ping
        utils.write_command(connection, commands.encode_ping(),
                            list(range(1, 128)))
        reader = utils.read_can_datagrams(connection)
        while True:
            dt = next(reader)

            if dt is None:  # Timeout
                break

            _, _, src = dt
            scan_queue.append(src)

    else:
        scan_queue = args.ids

    # Broadcast ask for config
    configs = utils.write_command_retry(connection,
                                        commands.encode_read_config(),
                                        scan_queue)

    for id, raw_config in configs.items():
        configs[id] = msgpack.unpackb(raw_config, encoding="ascii")

    print(json.dumps(configs, indent=4, sort_keys=True))
Esempio n. 5
0
def main():
    args = parse_commandline_args()
    connection = utils.open_connection(args)

    if args.all:
        scan_queue = list()

        # Broadcast ping
        utils.write_command(connection, commands.encode_ping(), list(range(1, 128)))
        reader = utils.read_can_datagrams(connection)
        while True:
            dt = next(reader)

            if dt is None: # Timeout
                break

            _, _, src = dt
            scan_queue.append(src)

    else:
        scan_queue = args.ids

    # Broadcast ask for config
    configs = utils.write_command_retry(connection,
                                        commands.encode_read_config(),
                                        scan_queue)

    for id, raw_config in configs.items():
        configs[id] = msgpack.unpackb(raw_config, encoding='ascii')

    print(json.dumps(configs, indent=4, sort_keys=True))