Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 1 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 2 | #include <poll.h> |
| 3 | #include <stdint.h> |
| 4 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame^] | 5 | struct console; |
| 6 | |
| 7 | /* handler API */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 8 | enum { |
| 9 | HANDLER_OK = 0, |
| 10 | HANDLER_EXIT, |
| 11 | }; |
| 12 | |
| 13 | struct handler { |
| 14 | const char *name; |
| 15 | int (*init)(struct handler *handler, |
| 16 | struct console *console); |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 17 | int (*data_in)(struct handler *handler, |
| 18 | uint8_t *buf, size_t len); |
| 19 | void (*fini)(struct handler *handler); |
| 20 | }; |
| 21 | |
| 22 | #define __handler_name(n) __handler_ ## n |
| 23 | #define _handler_name(n) __handler_name(n) |
| 24 | |
| 25 | #define console_register_handler(h) \ |
| 26 | static const \ |
| 27 | __attribute__((section("handlers"))) \ |
| 28 | __attribute__((used)) \ |
| 29 | struct handler * _handler_name(__COUNTER__) = h; |
| 30 | |
| 31 | int console_data_out(struct console *console, const uint8_t *data, size_t len); |
| 32 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame^] | 33 | /* poller API */ |
| 34 | struct poller; |
| 35 | |
| 36 | enum poller_ret { |
| 37 | POLLER_OK = 0, |
| 38 | POLLER_REMOVE, |
| 39 | POLLER_EXIT, |
| 40 | }; |
| 41 | |
| 42 | typedef enum poller_ret (*poller_fn_t)(struct handler *handler, |
| 43 | int revents, void *data); |
| 44 | |
| 45 | struct poller *console_register_poller(struct console *console, |
| 46 | struct handler *handler, poller_fn_t poller_fn, |
| 47 | int fd, int events, void *data); |
| 48 | |
| 49 | void console_unregister_poller(struct console *console, struct poller *poller); |
| 50 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 51 | /* utils */ |
| 52 | int write_buf_to_fd(int fd, const uint8_t *buf, size_t len); |
| 53 | |
| 54 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 55 | |
| 56 | #define offsetof(type, member) \ |
| 57 | ((unsigned long)&((type *)NULL)->member) |
| 58 | |
| 59 | #define container_of(ptr, type, member) \ |
| 60 | ((type *)((void *)((ptr) - offsetof(type, member)))) |
| 61 | |
Jeremy Kerr | 658cbda | 2016-03-09 18:10:00 +0800 | [diff] [blame] | 62 | #define BUILD_ASSERT(c) \ |
| 63 | do { \ |
| 64 | char __c[(c)?1:-1] __attribute__((unused)); \ |
| 65 | } while (0) |