* @brief Get data from the ring buffer. * * @param rb A pointer to the ring buffer. * @param ptr A pointer to the data buffer. * @param length The size of the data we want to read from the ring buffer. * * @return Return the data size we read from the ring buffer. */
| 175 | * @return Return the data size we read from the ring buffer. |
| 176 | */ |
| 177 | rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, |
| 178 | rt_uint8_t *ptr, |
| 179 | rt_uint32_t length) |
| 180 | { |
| 181 | rt_size_t size; |
| 182 | |
| 183 | RT_ASSERT(rb != RT_NULL); |
| 184 | |
| 185 | /* whether has enough data */ |
| 186 | size = rt_ringbuffer_data_len(rb); |
| 187 | |
| 188 | /* no data */ |
| 189 | if (size == 0) |
| 190 | return 0; |
| 191 | |
| 192 | /* less data */ |
| 193 | if (size < length) |
| 194 | length = size; |
| 195 | |
| 196 | if (rb->buffer_size - rb->read_index > length) |
| 197 | { |
| 198 | /* copy all of data */ |
| 199 | rt_memcpy(ptr, &rb->buffer_ptr[rb->read_index], length); |
| 200 | /* this should not cause overflow because there is enough space for |
| 201 | * length of data in current mirror */ |
| 202 | rb->read_index += length; |
| 203 | return length; |
| 204 | } |
| 205 | |
| 206 | rt_memcpy(&ptr[0], |
| 207 | &rb->buffer_ptr[rb->read_index], |
| 208 | rb->buffer_size - rb->read_index); |
| 209 | rt_memcpy(&ptr[rb->buffer_size - rb->read_index], |
| 210 | &rb->buffer_ptr[0], |
| 211 | length - (rb->buffer_size - rb->read_index)); |
| 212 | |
| 213 | /* we are going into the other side of the mirror */ |
| 214 | rb->read_mirror = ~rb->read_mirror; |
| 215 | rb->read_index = length - (rb->buffer_size - rb->read_index); |
| 216 | |
| 217 | return length; |
| 218 | } |
| 219 | RTM_EXPORT(rt_ringbuffer_get); |
| 220 | |
| 221 | /** |