/* Finalize and output current packet to the output channel.  */
static ssize_t packet_output(struct soam_t *self_p)
/* Finalize and output current packet to the output channel.  */
static ssize_t packet_output(struct soam_t *self_p)
{
    ssize_t size;
    size_t payload_crc_size;
    uint16_t crc;
    size = (self_p->tx.pos + 2);
    payload_crc_size = (size - 5);
    self_p->tx.buf_p[2] = self_p->transaction_id;
    self_p->tx.buf_p[3] = (payload_crc_size >> 8);
    self_p->tx.buf_p[4] = payload_crc_size;
    crc = crc_ccitt(0xffff, &self_p->tx.buf_p[0], size - 2);
    self_p->tx.buf_p[size - 2] = (crc >> 8);
    self_p->tx.buf_p[size - 1] = crc;
    if (chan_write(self_p->tx.chout_p,
                   &self_p->tx.buf_p[0],
                   size) != size) {
        return (-1);
    }
    return (size - 7);
}