ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
circular_buffer_io_fd_poll.h
Go to the documentation of this file.
1#ifndef CIRCULAR_BUFFER_IO_FD_POLL_H
2#define CIRCULAR_BUFFER_IO_FD_POLL_H
3
4/** \file
5 * \brief Circular buffer IO with file descriptor polling
6 *
7 * Usage:
8 *
9 * Initialize circular buffers (or ASCII Serial Com object), and input and
10 * output files.
11 *
12 * In the event loop:
13 *
14 * Run circular_buffer_io_fd_poll_do_poll
15 *
16 * Run circular_buffer_io_fd_poll_do_input
17 *
18 * Run circular_buffer_io_fd_poll_do_output
19 *
20 * Process data and/or push/pop from circular buffers either after running
21 * do_input or after running do_output
22 */
23
24#include "circular_buffer.h"
25#include <poll.h>
26#include <stdio.h>
27
28/** \brief Circular buffer IO with file descriptor polling struct
29 *
30 * Keeps track of the state of the IO
31 *
32 */
35 circular_buffer_uint8 *out_buf;
36 int fd_in;
37 int fd_out;
38 struct pollfd fds[2]; // fds[0] is in, fds[1] is out
40
41/** \brief Initialize circular buffer IO with file descriptor polling object
42 *
43 * Initialize object
44 *
45 * \param uninitialized circular_buffer_io_fd_poll object
46 *
47 * All other arguments should already be initialized
48 *
49 */
52 circular_buffer_uint8 *out_buf, int fd_in,
53 int fd_out);
54
55/** \brief poll circular buffer IO with file descriptor polling object
56 *
57 * Poll file descriptors. Always polls on input file descriptor, but only polls
58 * on output file descriptor when output circular buffer is not empty.
59 *
60 * \param initialized circular_buffer_io_fd_poll object
61 *
62 * \param timeout: timout after waiting for input or output fds for this long,
63 * in ms. If 0, return immediately. If -1, wait indefinetly for fds to be
64 * ready. See poll documentation (man 2 poll)
65 *
66 * \return status: if 0 success, otherwise failure
67 */
69 int timeout);
70
71/** \brief circular buffer IO with file descriptor polling object: write to
72 * output fd
73 *
74 * Write to output fd from output circular buffer. Only does anything if output
75 * circular buffer not empty and output fd ready for write, the latter of which
76 * is taken care of by circular_buffer_io_fd_poll_do_poll.
77 *
78 * Elements are popped from the front of the output circular buffer.
79 *
80 * \param initialized circular_buffer_io_fd_poll object
81 *
82 * \return the number of elements written to the fd and popped from output
83 * circular buffer
84 */
86
87/** \brief circular buffer IO with file descriptor polling object: read from
88 * input fd
89 *
90 * Read from input fd to input circular buffer. Only does anything if input fd
91 * has something to read, which is taken care of by
92 * circular_buffer_io_fd_poll_do_poll.
93 *
94 * Elements are pushed to the back of the input circular buffer.
95 *
96 * \param initialized circular_buffer_io_fd_poll object
97 *
98 * \return the number of elements read from the fd and pushed to the input
99 * circular buffer
100 */
102
103void circular_buffer_io_fd_poll_print(circular_buffer_io_fd_poll *cb_io,
104 FILE *stream);
105
106#endif
uint8_t circular_buffer_io_fd_poll_do_poll(circular_buffer_io_fd_poll *cb_io, int timeout)
poll circular buffer IO with file descriptor polling object
void circular_buffer_io_fd_poll_init(circular_buffer_io_fd_poll *cb_io, circular_buffer_uint8 *in_buf, circular_buffer_uint8 *out_buf, int fd_in, int fd_out)
Initialize circular buffer IO with file descriptor polling object.
struct circular_buffer_io_fd_poll_struct circular_buffer_io_fd_poll
Circular buffer IO with file descriptor polling struct.
size_t circular_buffer_io_fd_poll_do_input(circular_buffer_io_fd_poll *cb_io)
circular buffer IO with file descriptor polling object: read from input fd
size_t circular_buffer_io_fd_poll_do_output(circular_buffer_io_fd_poll *cb_io)
circular buffer IO with file descriptor polling object: write to output fd
Circular buffer IO with file descriptor polling struct.
circular buffer struct