/* Read and unescape given number of frame bytes from the transport channel.  */
static ssize_t read_bytes(struct xbee_driver_t *self_p, uint8_t *buf_p, size_t size)
/* Read and unescape given number of frame bytes from the transport channel.  */
static ssize_t read_bytes(struct xbee_driver_t *self_p, uint8_t *buf_p, size_t size)
{
    ssize_t res;
    size_t i;
    uint8_t byte;
    for (i = 0; i < size; i++) {
        res = read_byte(self_p, &byte);
        if (res != sizeof(byte)) {
            return (res);
        }
        if (byte == FRAME_DELIMITER) {
            return (-EPROTO);
        } else if (byte == ESCAPE) {
            res = read_byte(self_p, &byte);
            if (res != sizeof(byte)) {
                return (res);
            }
            byte ^= 0x20;
            if ((byte != FRAME_DELIMITER)
                && (byte != ESCAPE)
                && (byte != XON)
                && (byte != XOFF)) {
                return (-EPROTO);
            }
        }
        buf_p[i] = byte;
    }
    return (size);
}