/* Escape and write given number of frame bytes to the transport channel.  */
static ssize_t write_bytes(struct xbee_driver_t *self_p, const uint8_t *buf_p, size_t size)
/* Escape and write given number of frame bytes to the transport channel.  */
static ssize_t write_bytes(struct xbee_driver_t *self_p, const uint8_t *buf_p, size_t size)
{
    ssize_t res;
    size_t i;
    uint8_t chunk[2];
    size_t chunk_size;
    for (i = 0; i < size; i++) {
        switch (buf_p[i]) {
        case FRAME_DELIMITER:
        case ESCAPE:
        case XON:
        case XOFF:
            chunk[0] = ESCAPE;
            chunk[1] = buf_p[i];
            chunk[1] ^= 0x20;
            chunk_size = 2;
            break;
        default:
            chunk[0] = buf_p[i];
            chunk_size = 1;
            break;
        }
        res = chan_write(self_p->transport.chout_p,
                         &chunk[0],
                         chunk_size);
        if (res != chunk_size) {
            return (-EIO);
        }
    }
    return (size);
}