blob: 38cddef4a739b7ecfacaee1c1dc1c417987b5521 [file] [log] [blame]
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08002#include <poll.h>
3#include <stdint.h>
4
Jeremy Kerr329a35f2016-03-10 15:36:01 +08005struct console;
6
7/* handler API */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08008enum {
9 HANDLER_OK = 0,
10 HANDLER_EXIT,
11};
12
13struct handler {
14 const char *name;
15 int (*init)(struct handler *handler,
16 struct console *console);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080017 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
31int console_data_out(struct console *console, const uint8_t *data, size_t len);
32
Jeremy Kerr329a35f2016-03-10 15:36:01 +080033/* poller API */
34struct poller;
35
36enum poller_ret {
37 POLLER_OK = 0,
38 POLLER_REMOVE,
39 POLLER_EXIT,
40};
41
42typedef enum poller_ret (*poller_fn_t)(struct handler *handler,
43 int revents, void *data);
44
45struct poller *console_register_poller(struct console *console,
46 struct handler *handler, poller_fn_t poller_fn,
47 int fd, int events, void *data);
48
49void console_unregister_poller(struct console *console, struct poller *poller);
50
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080051/* utils */
52int 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 Kerr658cbda2016-03-09 18:10:00 +080062#define BUILD_ASSERT(c) \
63 do { \
64 char __c[(c)?1:-1] __attribute__((unused)); \
65 } while (0)