Split IO handling code into separate handler modules
Currently, we have a direct, explicit link between the VUART IO and the
"outbound" data, currently to stdio.
We'll want to do multiple things with the VUART data in future (eg.,
logging).
This change introduces "handlers"; a struct of callbacks that are
invoked when UART data is available, or when a (handler-provided) poll
descriptor returns new events. We convert the stdio code into a handler
as part of this.
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
diff --git a/console-server.h b/console-server.h
new file mode 100644
index 0000000..c084321
--- /dev/null
+++ b/console-server.h
@@ -0,0 +1,46 @@
+
+struct console;
+
+#include <poll.h>
+#include <stdint.h>
+
+enum {
+ HANDLER_OK = 0,
+ HANDLER_EXIT,
+};
+
+struct handler {
+ const char *name;
+ int (*init)(struct handler *handler,
+ struct console *console);
+ int (*init_poll)(struct handler *hander,
+ struct pollfd *pollfd);
+ int (*poll_event)(struct handler *handler,
+ int events);
+ int (*data_in)(struct handler *handler,
+ uint8_t *buf, size_t len);
+ void (*fini)(struct handler *handler);
+};
+
+#define __handler_name(n) __handler_ ## n
+#define _handler_name(n) __handler_name(n)
+
+#define console_register_handler(h) \
+ static const \
+ __attribute__((section("handlers"))) \
+ __attribute__((used)) \
+ struct handler * _handler_name(__COUNTER__) = h;
+
+int console_data_out(struct console *console, const uint8_t *data, size_t len);
+
+/* utils */
+int write_buf_to_fd(int fd, const uint8_t *buf, size_t len);
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+
+#define offsetof(type, member) \
+ ((unsigned long)&((type *)NULL)->member)
+
+#define container_of(ptr, type, member) \
+ ((type *)((void *)((ptr) - offsetof(type, member))))
+