summaryrefslogtreecommitdiffstats
path: root/cpukit/include/rtems/printer.h
blob: dbd887221e7941f87baacc1b676a74e7619472b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
 * @file
 *
 * @brief User print interface to the bspIO print plug in.
 *
 * This include file defines the user interface to kernel print methods.
 */

/*
 *  Copyright (c) 2016 Chris Johns <chrisj@rtems.org>
 *  All rights reserved.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifndef _RTEMS_PRINTER_H
#define _RTEMS_PRINTER_H

#include <rtems/print.h>
#include <rtems/chain.h>
#include <rtems/rtems/intr.h>
#include <rtems/rtems/tasks.h>

#include <stdio.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @addtogroup RTEMSPrintSupport
 *
 * @{
 */

/**
 * Type definition for function which can be plugged in to certain reporting
 * routines to redirect the output.
 *
 * Use the RTEMS Print interface to call these functions. Do not directly use
 * them.
 *
 * If the user provides their own printer, then they may redirect those reports
 * as they see fit.
 */
typedef int (*rtems_print_printer)(void *, const char *format, va_list ap);

/**
 * Type definition for the printer structure used to access the kernel print
 * support.
 */
struct rtems_printer {
  void                *context;
  rtems_print_printer  printer;
};

/**
 * @brief check if the printer is valid.
 *
 * @param[in] printer Pointer to the printer structure.
 *
 * @return true The printer is valid else false is returned.
 */
static inline bool rtems_print_printer_valid(const rtems_printer *printer)
{
  return printer != NULL && printer->printer != NULL;
}

/**
 * @brief Initializes the printer to print nothing.
 *
 * An empty printer prints nothing. You can use this to implement an enable and
 * disable type print implementation.
 *
 * @param[in] printer Pointer to the printer structure.
 */
static inline void rtems_print_printer_empty(rtems_printer *printer)
{
  printer->context = NULL;
  printer->printer = NULL;
}

/**
 * @brief Initializes the printer to print via printk().
 *
 * @param[in] printer Pointer to the printer structure.
 */
void rtems_print_printer_printk(rtems_printer *printer);

/**
 * @brief Initializes the printer to print via printf().
 *
 * @param[in] printer Pointer to the printer structure.
 */
void rtems_print_printer_printf(rtems_printer *printer);

/**
 * @brief Initializes the printer to print via fprintf() using the specified
 * file stream.
 *
 * @param[in] printer Pointer to the printer structure.
 */
void rtems_print_printer_fprintf(rtems_printer *printer, FILE *file);

/**
 * @brief Initializes the printer to print via fprintf() using an unbuffered
 * FILE stream with output through rtems_putc().
 *
 * @param[in] printer Pointer to the printer structure.
 */
void rtems_print_printer_fprintf_putc(rtems_printer *printer);

typedef struct {
  rtems_id                     task;
  RTEMS_INTERRUPT_LOCK_MEMBER( lock )
  rtems_chain_control          free_buffers;
  rtems_chain_control          todo_buffers;
  size_t                       task_stack_size;
  rtems_task_priority          task_priority;
  int                          fd;
  void                        *buffer_table;
  size_t                       buffer_count;
  size_t                       buffer_size;
} rtems_printer_task_context;

static inline void rtems_printer_task_initialize(
  rtems_printer_task_context *context
)
{
  memset( context, 0, sizeof( *context ) );
}

static inline void rtems_printer_task_set_stack_size(
  rtems_printer_task_context *context,
  size_t                    stack_size
)
{
  context->task_stack_size = stack_size;
}

static inline void rtems_printer_task_set_priority(
  rtems_printer_task_context *context,
  rtems_task_priority       priority
)
{
  context->task_priority = priority;
}

static inline void rtems_printer_task_set_file_descriptor(
  rtems_printer_task_context *context,
  int                       fd
)
{
  context->fd = fd;
}

static inline void rtems_printer_task_set_buffer_table(
  rtems_printer_task_context *context,
  void                     *buffer_table
)
{
  context->buffer_table = buffer_table;
}

static inline void rtems_printer_task_set_buffer_count(
  rtems_printer_task_context *context,
  size_t                    buffer_count
)
{
  context->buffer_count = buffer_count;
}

static inline void rtems_printer_task_set_buffer_size(
  rtems_printer_task_context *context,
  size_t                    buffer_size
)
{
  context->buffer_size = buffer_size;
}

/**
 * @brief Creates a printer task.
 *
 * Print requests via rtems_printf() or rtems_vprintf() using a printer task
 * printer are output to a buffer and then placed on a work queue in FIFO
 * order.  The work queue is emptied by the printer task.  The printer task
 * writes the buffer content to the file descriptor specified by the context.
 * Buffers are allocated from a pool of buffers as specified by the context.
 *
 * @param[in] printer Pointer to the printer structure.
 * @param[in] context The initialized printer task context.
 *
 * @retval 0 Successful operation.
 * @retval EINVAL Invalid context parameters.
 * @retval ENOMEM Not enough resources.
 */
int rtems_print_printer_task(
  rtems_printer              *printer,
  rtems_printer_task_context *context
);

/**
 * @brief Drains the work queue of the printer task.
 *
 * Waits until all output buffers in the work queue at the time of this
 * function call are written to the file descriptor and an fsync() completed.
 *
 * The printer task must be successfully started via rtems_print_printer_task()
 * before this function can be used.  Otherwise, the behaviour is undefined.
 *
 * @param[in] context The printer task context of a successfully started
 *   printer task.
 */
void rtems_printer_task_drain(rtems_printer_task_context *context);

/** @} */

#ifdef __cplusplus
}
#endif

#endif /* _RTEMS_PRINTER_H */