blob: 5871019968a9785bd9a477644618a4d791c29303 [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;
Jeremy Kerrd47963e2016-03-16 17:29:55 +08006struct config;
Jeremy Kerr329a35f2016-03-10 15:36:01 +08007
8/* handler API */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08009enum {
10 HANDLER_OK = 0,
11 HANDLER_EXIT,
12};
13
14struct handler {
15 const char *name;
16 int (*init)(struct handler *handler,
Jeremy Kerrd47963e2016-03-16 17:29:55 +080017 struct console *console,
18 struct config *config);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080019 int (*data_in)(struct handler *handler,
20 uint8_t *buf, size_t len);
21 void (*fini)(struct handler *handler);
22};
23
24#define __handler_name(n) __handler_ ## n
25#define _handler_name(n) __handler_name(n)
26
27#define console_register_handler(h) \
28 static const \
29 __attribute__((section("handlers"))) \
30 __attribute__((used)) \
31 struct handler * _handler_name(__COUNTER__) = h;
32
33int console_data_out(struct console *console, const uint8_t *data, size_t len);
34
Jeremy Kerr329a35f2016-03-10 15:36:01 +080035/* poller API */
36struct poller;
37
38enum poller_ret {
39 POLLER_OK = 0,
40 POLLER_REMOVE,
41 POLLER_EXIT,
42};
43
44typedef enum poller_ret (*poller_fn_t)(struct handler *handler,
45 int revents, void *data);
46
47struct poller *console_register_poller(struct console *console,
48 struct handler *handler, poller_fn_t poller_fn,
49 int fd, int events, void *data);
50
51void console_unregister_poller(struct console *console, struct poller *poller);
52
Jeremy Kerrd66195c2016-03-16 17:24:51 +080053/* config API */
54struct config;
55const char *config_get_value(struct config *config, const char *name);
56struct config *config_init(const char *filename);
57void config_fini(struct config *config);
58
Jeremy Kerr2bd05182016-03-10 16:59:43 +080059/* socket paths */
60const char *console_socket_path;
61const size_t console_socket_path_len;
62const char *console_socket_path_readable;
63
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080064/* utils */
65int write_buf_to_fd(int fd, const uint8_t *buf, size_t len);
66
67#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
68
69#define offsetof(type, member) \
70 ((unsigned long)&((type *)NULL)->member)
71
72#define container_of(ptr, type, member) \
73 ((type *)((void *)((ptr) - offsetof(type, member))))
74
Jeremy Kerr658cbda2016-03-09 18:10:00 +080075#define BUILD_ASSERT(c) \
76 do { \
77 char __c[(c)?1:-1] __attribute__((unused)); \
78 } while (0)