RTEMS CPU Kit with SuperCore  4.11.3
stdio-redirect.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Chris Johns (chrisj@rtems.org)
3  *
4  * The license and distribution terms for this file may be
5  * found in the file LICENSE in this distribution.
6  *
7  * This software with is provided ``as is'' and with NO WARRANTY.
8  */
9 
10 /*
11  * @brief Redirect an stdio file decriptor.
12  *
13  * The is a helper module of code design to redirect an stdio file
14  * descriptor. You can optionally have the data buffered and/or you can provide
15  * a handler function that is called when data arrives.
16  *
17  * The module uses standard POSIX calls to implement the redirection and if the
18  * threading was POSIX based the code would be portable. Currently the code
19  * uses RTEMS threads.
20  *
21  * Redirecting stderr and stdout is useful in embedded system because you can
22  * transport the output off your device or create a web interface that can
23  * display the output. This can be a very powerful diagnostic and support tool.
24  *
25  * The implementation does:
26  *
27  * 1. Duplicate the file descriptor (fd) to redirect using the dup call. The
28  * duplicated desciptor is used to echo the output out the existing path.
29  *
30  * 2. Create a pipe using the pipe call.
31  *
32  * 3. Duplicate the pipe's writer file descriptor to user's file
33  * descriptor. This results in any writes to the user's fd being written to
34  * the pipe.
35  *
36  * 4. Create a reader task that blocks on the pipe. It optionally calls a
37  * handler and if configured buffers the data.
38  */
39 
40 #if !defined(RTEMS_STDIO_REDIRECT_H)
41 #define RTEMS_STDIO_REDIRECT_H
42 
43 #include <stdbool.h>
44 #include <rtems.h>
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif /* __cplusplus */
49 
50 /*
51  * Handler called whenever redirected data arrives.
52  *
53  * @param buffer The data.
54  * @param length The amount of data in the buffer.
55  */
56 typedef void (*rtems_stdio_redirect_handler)(const char* buffer,
57  ssize_t length);
58 
59 /*
60  * Redirector data.
61  */
62 typedef struct
63 {
64  volatile uint32_t state;
65  rtems_id reader;
66  rtems_id lock;
67  int fd;
68  int fd_dup;
69  int pipe[2];
70  char* input;
71  ssize_t input_size;
72  char* buffer;
73  ssize_t buffer_size;
74  ssize_t in;
75  bool full;
76  bool echo;
77  rtems_stdio_redirect_handler handler;
79 
80 /*
81  * Open a redirector returning the handle to it.
82  *
83  * @param fd The file descriptor to redirect.
84  * @param priority The priority of the reader thread.
85  */
86 rtems_stdio_redirect* rtems_stdio_redirect_open(int fd,
87  rtems_task_priority priority,
88  size_t stack_size,
89  ssize_t input_size,
90  ssize_t buffer_size,
91  bool echo,
92  rtems_stdio_redirect_handler handler);
93 
94 /*
95  * Close the redirector.
96  */
97 void rtems_stdio_redirect_close(rtems_stdio_redirect* sr);
98 
99 /*
100  * Get data from the capture buffer. Data read is removed from the buffer.
101  *
102  * @param sr The stdio redirection handle.
103  * @param buffer The buffer data is written into.
104  * @param length The size of the buffer.
105  * @return ssize_t The amount of data written and -1 or an error.
106  */
107 ssize_t rtems_stdio_redirect_read(rtems_stdio_redirect* sr,
108  char* buffer,
109  ssize_t length);
110 
111 #ifdef __cplusplus
112 }
113 #endif /* __cplusplus */
114 
115 #endif
Objects_Id rtems_id
Used to manage and manipulate RTEMS object identifiers.
Definition: types.h:80
Priority_Control rtems_task_priority
Define the type for an RTEMS API task priority.
Definition: tasks.h:79
int pipe(int filsdes[2])
POSIX 1003.1b 6.1.1 Create an Inter-Process Channel.
Definition: pipe.c:31
Definition: stdio-redirect.h:63