1 | /** |
---|
2 | * @file rtems/print.h |
---|
3 | * |
---|
4 | * @brief User print interface to the bspIO print plug in. |
---|
5 | * |
---|
6 | * This include file defines the user interface to kernel print methods. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2016 Chris Johns <chrisj@rtems.org> |
---|
11 | * All rights reserved. |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.org/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #ifndef _RTEMS_PRINTER_H |
---|
19 | #define _RTEMS_PRINTER_H |
---|
20 | |
---|
21 | #include <rtems/print.h> |
---|
22 | #include <rtems/chain.h> |
---|
23 | #include <rtems/rtems/intr.h> |
---|
24 | #include <rtems/rtems/tasks.h> |
---|
25 | |
---|
26 | #include <stdio.h> |
---|
27 | #include <string.h> |
---|
28 | |
---|
29 | #ifdef __cplusplus |
---|
30 | extern "C" { |
---|
31 | #endif |
---|
32 | |
---|
33 | /** |
---|
34 | * @addtogroup XXX |
---|
35 | * |
---|
36 | * @{ |
---|
37 | */ |
---|
38 | |
---|
39 | /** |
---|
40 | * @defgroup RTEMSPrintSupport |
---|
41 | */ |
---|
42 | |
---|
43 | /** |
---|
44 | * Type definition for function which can be plugged in to certain reporting |
---|
45 | * routines to redirect the output. |
---|
46 | * |
---|
47 | * Use the RTEMS Print interface to call these functions. Do not directly use |
---|
48 | * them. |
---|
49 | * |
---|
50 | * If the user provides their own printer, then they may redirect those reports |
---|
51 | * as they see fit. |
---|
52 | */ |
---|
53 | typedef int (*rtems_print_printer)(void *, const char *format, va_list ap); |
---|
54 | |
---|
55 | /** |
---|
56 | * Type definition for the printer structure used to access the kernel print |
---|
57 | * support. |
---|
58 | */ |
---|
59 | struct rtems_printer { |
---|
60 | void *context; |
---|
61 | rtems_print_printer printer; |
---|
62 | }; |
---|
63 | |
---|
64 | /** |
---|
65 | * @brief check if the printer is valid. |
---|
66 | * |
---|
67 | * @param[in] printer Pointer to the printer structure. |
---|
68 | * |
---|
69 | * @return true The printer is valid else false is returned. |
---|
70 | */ |
---|
71 | static inline bool rtems_print_printer_valid(const rtems_printer *printer) |
---|
72 | { |
---|
73 | return printer != NULL && printer->printer != NULL; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * @brief Initializes the printer to print nothing. |
---|
78 | * |
---|
79 | * An empty printer prints nothing. You can use this to implement an enable and |
---|
80 | * disable type print implementation. |
---|
81 | * |
---|
82 | * @param[in] printer Pointer to the printer structure. |
---|
83 | */ |
---|
84 | static inline void rtems_print_printer_empty(rtems_printer *printer) |
---|
85 | { |
---|
86 | printer->context = NULL; |
---|
87 | printer->printer = NULL; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * @brief Initializes the printer to print via printk(). |
---|
92 | * |
---|
93 | * @param[in] printer Pointer to the printer structure. |
---|
94 | */ |
---|
95 | void rtems_print_printer_printk(rtems_printer *printer); |
---|
96 | |
---|
97 | /** |
---|
98 | * @brief Initializes the printer to print via printf(). |
---|
99 | * |
---|
100 | * @param[in] printer Pointer to the printer structure. |
---|
101 | */ |
---|
102 | void rtems_print_printer_printf(rtems_printer *printer); |
---|
103 | |
---|
104 | /** |
---|
105 | * @brief Initializes the printer to print via fprintf() using the specified |
---|
106 | * file stream. |
---|
107 | * |
---|
108 | * @param[in] printer Pointer to the printer structure. |
---|
109 | */ |
---|
110 | void rtems_print_printer_fprintf(rtems_printer *printer, FILE *file); |
---|
111 | |
---|
112 | /** |
---|
113 | * @brief Initializes the printer to print via fprintf() using an unbuffered |
---|
114 | * FILE stream with output through rtems_putc(). |
---|
115 | * |
---|
116 | * @param[in] printer Pointer to the printer structure. |
---|
117 | */ |
---|
118 | void rtems_print_printer_fprintf_putc(rtems_printer *printer); |
---|
119 | |
---|
120 | typedef struct { |
---|
121 | rtems_id task; |
---|
122 | RTEMS_INTERRUPT_LOCK_MEMBER( lock ) |
---|
123 | rtems_chain_control free_buffers; |
---|
124 | rtems_chain_control todo_buffers; |
---|
125 | size_t task_stack_size; |
---|
126 | rtems_task_priority task_priority; |
---|
127 | int fd; |
---|
128 | void *buffer_table; |
---|
129 | size_t buffer_count; |
---|
130 | size_t buffer_size; |
---|
131 | } rtems_printer_task_context; |
---|
132 | |
---|
133 | static inline void rtems_printer_task_initialize( |
---|
134 | rtems_printer_task_context *context |
---|
135 | ) |
---|
136 | { |
---|
137 | memset( context, 0, sizeof( *context ) ); |
---|
138 | } |
---|
139 | |
---|
140 | static inline void rtems_printer_task_set_stack_size( |
---|
141 | rtems_printer_task_context *context, |
---|
142 | size_t stack_size |
---|
143 | ) |
---|
144 | { |
---|
145 | context->task_stack_size = stack_size; |
---|
146 | } |
---|
147 | |
---|
148 | static inline void rtems_printer_task_set_priority( |
---|
149 | rtems_printer_task_context *context, |
---|
150 | rtems_task_priority priority |
---|
151 | ) |
---|
152 | { |
---|
153 | context->task_priority = priority; |
---|
154 | } |
---|
155 | |
---|
156 | static inline void rtems_printer_task_set_file_descriptor( |
---|
157 | rtems_printer_task_context *context, |
---|
158 | int fd |
---|
159 | ) |
---|
160 | { |
---|
161 | context->fd = fd; |
---|
162 | } |
---|
163 | |
---|
164 | static inline void rtems_printer_task_set_buffer_table( |
---|
165 | rtems_printer_task_context *context, |
---|
166 | void *buffer_table |
---|
167 | ) |
---|
168 | { |
---|
169 | context->buffer_table = buffer_table; |
---|
170 | } |
---|
171 | |
---|
172 | static inline void rtems_printer_task_set_buffer_count( |
---|
173 | rtems_printer_task_context *context, |
---|
174 | size_t buffer_count |
---|
175 | ) |
---|
176 | { |
---|
177 | context->buffer_count = buffer_count; |
---|
178 | } |
---|
179 | |
---|
180 | static inline void rtems_printer_task_set_buffer_size( |
---|
181 | rtems_printer_task_context *context, |
---|
182 | size_t buffer_size |
---|
183 | ) |
---|
184 | { |
---|
185 | context->buffer_size = buffer_size; |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * @brief Creates a printer task. |
---|
190 | * |
---|
191 | * Print requests via rtems_printf() or rtems_vprintf() using a printer task |
---|
192 | * printer are output to a buffer and then placed on a work queue in FIFO |
---|
193 | * order. The work queue is emptied by the printer task. The printer task |
---|
194 | * writes the buffer content to the file descriptor specified by the context. |
---|
195 | * Buffers are allocated from a pool of buffers as specified by the context. |
---|
196 | * |
---|
197 | * @param[in] printer Pointer to the printer structure. |
---|
198 | * @param[in] context The initialized printer task context. |
---|
199 | * |
---|
200 | * @retval 0 Successful operation. |
---|
201 | * @retval EINVAL Invalid context parameters. |
---|
202 | * @retval ENOMEM Not enough resources. |
---|
203 | */ |
---|
204 | int rtems_print_printer_task( |
---|
205 | rtems_printer *printer, |
---|
206 | rtems_printer_task_context *context |
---|
207 | ); |
---|
208 | |
---|
209 | /** |
---|
210 | * @brief Drains the work queue of the printer task. |
---|
211 | * |
---|
212 | * Waits until all output buffers in the work queue at the time of this |
---|
213 | * function call are written to the file descriptor and an fsync() completed. |
---|
214 | * |
---|
215 | * The printer task must be successfully started via rtems_print_printer_task() |
---|
216 | * before this function can be used. Otherwise, the behaviour is undefined. |
---|
217 | * |
---|
218 | * @param[in] context The printer task context of a successfully started |
---|
219 | * printer task. |
---|
220 | */ |
---|
221 | void rtems_printer_task_drain(rtems_printer_task_context *context); |
---|
222 | |
---|
223 | /** @} */ |
---|
224 | |
---|
225 | #ifdef __cplusplus |
---|
226 | } |
---|
227 | #endif |
---|
228 | |
---|
229 | #endif /* _RTEMS_PRINTER_H */ |
---|