Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame^] | 1 | |
| 2 | struct console; |
| 3 | |
| 4 | #include <poll.h> |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | enum { |
| 8 | HANDLER_OK = 0, |
| 9 | HANDLER_EXIT, |
| 10 | }; |
| 11 | |
| 12 | struct handler { |
| 13 | const char *name; |
| 14 | int (*init)(struct handler *handler, |
| 15 | struct console *console); |
| 16 | int (*init_poll)(struct handler *hander, |
| 17 | struct pollfd *pollfd); |
| 18 | int (*poll_event)(struct handler *handler, |
| 19 | int events); |
| 20 | int (*data_in)(struct handler *handler, |
| 21 | uint8_t *buf, size_t len); |
| 22 | void (*fini)(struct handler *handler); |
| 23 | }; |
| 24 | |
| 25 | #define __handler_name(n) __handler_ ## n |
| 26 | #define _handler_name(n) __handler_name(n) |
| 27 | |
| 28 | #define console_register_handler(h) \ |
| 29 | static const \ |
| 30 | __attribute__((section("handlers"))) \ |
| 31 | __attribute__((used)) \ |
| 32 | struct handler * _handler_name(__COUNTER__) = h; |
| 33 | |
| 34 | int console_data_out(struct console *console, const uint8_t *data, size_t len); |
| 35 | |
| 36 | /* utils */ |
| 37 | int write_buf_to_fd(int fd, const uint8_t *buf, size_t len); |
| 38 | |
| 39 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 40 | |
| 41 | #define offsetof(type, member) \ |
| 42 | ((unsigned long)&((type *)NULL)->member) |
| 43 | |
| 44 | #define container_of(ptr, type, member) \ |
| 45 | ((type *)((void *)((ptr) - offsetof(type, member)))) |
| 46 | |