* @brief This function will read the specified length of data from the pipe. * * @param device is a pointer to the pipe device descriptor. * * @param pos is a parameter compatible with POSIX standard interface (currently meaningless, just pass in 0). * * @param buffer is a pointer to the buffer to store the read data. * * @param count is the length of data to be read. * *
| 493 | * When the return value is 0, it means the pipe device handle is empty or the count is 0. |
| 494 | */ |
| 495 | rt_ssize_t rt_pipe_read(rt_device_t device, rt_off_t pos, void *buffer, rt_size_t count) |
| 496 | { |
| 497 | uint8_t *pbuf; |
| 498 | rt_size_t read_bytes = 0; |
| 499 | rt_pipe_t *pipe = (rt_pipe_t *)device; |
| 500 | |
| 501 | if (device == RT_NULL) |
| 502 | { |
| 503 | rt_set_errno(-EINVAL); |
| 504 | return 0; |
| 505 | } |
| 506 | if (count == 0) |
| 507 | { |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | pbuf = (uint8_t*)buffer; |
| 512 | rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER); |
| 513 | |
| 514 | while (read_bytes < count) |
| 515 | { |
| 516 | int len = rt_ringbuffer_get(pipe->fifo, &pbuf[read_bytes], count - read_bytes); |
| 517 | if (len <= 0) |
| 518 | { |
| 519 | break; |
| 520 | } |
| 521 | |
| 522 | read_bytes += len; |
| 523 | } |
| 524 | rt_mutex_release(&pipe->lock); |
| 525 | |
| 526 | return read_bytes; |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * @brief This function will write the specified length of data to the pipe. |
nothing calls this directly
no test coverage detected