/* Put a block of data into the ring buffer. If the capacity of ring buffer is insufficient, it will discard out-of-range data.  */
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length)
/* Put a block of data into the ring buffer. If the capacity of ring buffer is insufficient, it will discard out-of-range data.  */
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length)
{
    rt_uint16_t size;
    RT_ASSERT(rb != RT_NULL);
    size = rt_ringbuffer_space_len(rb);
    if (size == 0)
        return 0;
    if (size < length)
        length = size;
    if (rb->buffer_size - rb->write_index > length)
    {
        memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
        rb->write_index += length;
        return length;
    }
    memcpy(&rb->buffer_ptr[rb->write_index],
           &ptr[0],
           rb->buffer_size - rb->write_index);
    memcpy(&rb->buffer_ptr[0],
           &ptr[rb->buffer_size - rb->write_index],
           length - (rb->buffer_size - rb->write_index));
    rb->write_mirror = ~rb->write_mirror;
    rb->write_index = length - (rb->buffer_size - rb->write_index);
    return length;
}