/* Read given number of bytes from the input channel and write them to the flash memory. */
static int write_application(struct upgrade_uds_t *self_p, size_t size)
/* Read given number of bytes from the input channel and write them to the flash memory. */
static int write_application(struct upgrade_uds_t *self_p, size_t size)
{
    size_t left;
    uint8_t buffer[TRANSFER_DATA_SIZE_MAX];
    size_t n;
    left = size;
    while (left > 0) {
        if (left > sizeof(buffer)) {
            n = sizeof(buffer);
        } else {
            n = left;
        }
        if (chan_read(self_p->chin_p, buffer, n) != n) {
            return (-EIO);
        }
        if (upgrade_binary_upload(buffer, n) != 0) {
            return (-1);
        }
        left -= n;
    }
    return (0);
}