2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file filter_common.c Common helper functions for filter input/output. */
10 #include <sys/types.h>
19 #include "buffer_tree.h"
24 /** The array of supported filters. */
25 struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
28 * Call the init function of each supported filter.
31 void filter_init(void)
35 FOR_EACH_SUPPORTED_FILTER(i)
36 filters[i].init(filters + i);
40 * Close and destroy a filter callback.
42 * \param fcb The filter callback to close.
44 * This removes \a fcb from the list of filter callbacks and calls
45 * the close callback associated with \a fcb.
47 static void close_filter_callback(struct filter_callback *fcb)
49 PARA_NOTICE_LOG("closing filter_callback %p\n", fcb);
55 * Close all callbacks of a filter node.
57 * \param fn The filter node which contains the filter callbacks to be closed.
59 * Call close_filter_callback() for each entry in the filter callback list
62 static void close_callbacks(struct filter_node *fn)
64 struct filter_callback *fcb, *tmp;
66 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
67 PARA_INFO_LOG("closing %s filter callback\n",
68 filters[fn->filter_num].name);
69 close_filter_callback(fcb);
73 static void call_callbacks(struct filter_node *fn, char *inbuf, size_t inlen,
74 char *outbuf, size_t outlen)
76 struct filter_callback *fcb, *tmp;
77 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
79 if (inlen && fcb->input_cb) {
80 ret = fcb->input_cb(inbuf, inlen, fcb);
82 close_filter_callback(fcb);
86 if (!outlen || !fcb->output_cb)
88 ret = fcb->output_cb(outbuf, outlen, fcb);
90 close_filter_callback(fcb);
95 * Call the convert function of each filter.
98 * \param t The task identifying the filter chain.
100 * This is the core function of the filter subsystem. It loops over the list of
101 * filter nodes determined by \a t and calls the filter's convert function if
102 * there is input available for the filter node in question. If the convert
103 * function consumed some or all of its input data, all registered input
104 * callbacks are called. Similarly, if a convert function produced output, all
105 * registered output callbacks get called.
107 * On errors a (negative) error code is stored in t->error.
109 * \sa filter_node, filter#convert, filter_callback.
111 void filter_post_select(__a_unused struct sched *s, struct task *t)
113 struct filter_chain *fc = container_of(t, struct filter_chain, task);
114 struct filter_node *fn;
117 int i, conv, conv_total = 0;
119 if (fc->output_error && *fc->output_error < 0) {
120 t->error = *fc->output_error;
125 loaded = fc->in_loaded;
127 FOR_EACH_FILTER_NODE(fn, fc, i) {
128 struct filter *f = filters + fn->filter_num;
129 if (fn->loaded < fn->bufsize) {
130 size_t size, old_fn_loaded = fn->loaded;
131 t->error = f->convert(ib, *loaded, fn);
135 call_callbacks(fn, ib, size, fn->buf + old_fn_loaded,
136 fn->loaded - old_fn_loaded);
138 conv += size + fn->loaded - old_fn_loaded;
140 memmove(ib, ib + size, *loaded);
143 loaded = &fn->loaded;
148 if (*fc->input_error >= 0)
152 if (*fc->in_loaded && conv_total)
154 t->error = -E_FC_EOF;
158 * Close all filter nodes and their callbacks.
160 * \param fc The filter chain to close.
162 * For each filter node determined by \a fc, call the close function of each
163 * registered filter callback as well as the close function of the
164 * corresponding filter. Free all resources and destroy all callback lists and
165 * the filter node list.
167 * \sa filter::close, filter_callback::close
169 void close_filters(struct filter_chain *fc)
171 struct filter_node *fn;
176 PARA_NOTICE_LOG("closing filter chain %p\n", fc);
177 FOR_EACH_FILTER_NODE(fn, fc, i) {
178 struct filter *f = filters + fn->filter_num;
180 PARA_INFO_LOG("closing %s filter\n", f->name);
184 free(fc->filter_nodes);
188 * If the filter has a command line parser and options is not NULL, run it.
189 * Returns filter_num on success, negative on errors
191 static int parse_filter_args(int filter_num, char *options, void **conf)
193 struct filter *f = &filters[filter_num];
194 int ret, i, argc = 2;
197 // PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
198 // options? options : "(none)", f->parse_config);
199 if (!f->parse_config)
200 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
201 // PARA_DEBUG_LOG("options: %s\n", options);
202 argc = create_argv(options, " \t", &argv);
204 return -E_BAD_FILTER_OPTIONS;
205 PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
206 for (i = argc - 1; i >= 0; i--)
207 argv[i + 1] = argv[i];
208 argv[0] = para_strdup(f->name);
210 ret = f->parse_config(argc, argv, conf);
211 free(argv[argc - 1]);
212 argv[argc - 1] = NULL;
214 return ret < 0? ret : filter_num;
218 * Check the filter command line options.
220 * \param fa The command line options.
221 * \param conf Points to the filter configuration upon successful return.
223 * Check if \a fa starts with a the name of a supported filter, followed by
224 * a colon. If yes, call the command line parser of that filter.
226 * \return On success, the number of the filter is returned and \a conf
227 * is initialized to point to the filter configuration determined by \a fa.
228 * On errors, a negative value is returned.
230 * Note: If \a fa specifies a filter that has no command line parser success is
231 * returned, and \a conf is initialized to \p NULL.
233 * \sa filter::parse_config
235 int check_filter_arg(char *fa, void **conf)
240 // PARA_DEBUG_LOG("arg: %s\n", fa);
241 FOR_EACH_SUPPORTED_FILTER(j) {
242 const char *name = filters[j].name;
243 size_t len = strlen(name);
245 if (strlen(fa) < len)
247 if (strncmp(name, fa, len))
252 if (c && !filters[j].parse_config)
253 return -E_BAD_FILTER_OPTIONS;
254 return parse_filter_args(j, c? fa + len + 1 :
255 fa + strlen(fa), conf);
257 return -E_UNSUPPORTED_FILTER;
260 void print_filter_helps(int detailed)
264 printf_or_die("\nAvailable filters: \n\t");
265 FOR_EACH_SUPPORTED_FILTER(i)
266 printf_or_die("%s%s", i? " " : "", filters[i].name);
267 printf_or_die("\n\n");
269 FOR_EACH_SUPPORTED_FILTER(i) {
270 struct filter *f = filters + i;
272 if (!f->help.short_help)
274 printf_or_die("Options for %s:\n", f->name);
275 ggo_print_help(&f->help, detailed);
280 void generic_filter_pre_select(struct sched *s, struct task *t)
282 struct filter_node *fn = container_of(t, struct filter_node, task);
285 if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0) {
286 s->timeout.tv_sec = 0;
287 s->timeout.tv_usec = 1;