blob: 1a9e813172a984001beedb0a5504d8dfaa61c977 [file] [log] [blame]
Jeremy Kerr9326d772016-03-17 17:15:02 +08001/**
2 * Copyright © 2016 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080016
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080017#include <poll.h>
18#include <stdint.h>
19
Jeremy Kerr329a35f2016-03-10 15:36:01 +080020struct console;
Jeremy Kerrd47963e2016-03-16 17:29:55 +080021struct config;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080022
23/* handler API */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080024enum {
25 HANDLER_OK = 0,
26 HANDLER_EXIT,
27};
28
29struct handler {
30 const char *name;
31 int (*init)(struct handler *handler,
Jeremy Kerrd47963e2016-03-16 17:29:55 +080032 struct console *console,
33 struct config *config);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080034 int (*data_in)(struct handler *handler,
35 uint8_t *buf, size_t len);
36 void (*fini)(struct handler *handler);
37};
38
39#define __handler_name(n) __handler_ ## n
40#define _handler_name(n) __handler_name(n)
41
42#define console_register_handler(h) \
43 static const \
44 __attribute__((section("handlers"))) \
45 __attribute__((used)) \
46 struct handler * _handler_name(__COUNTER__) = h;
47
48int console_data_out(struct console *console, const uint8_t *data, size_t len);
49
Jeremy Kerr329a35f2016-03-10 15:36:01 +080050/* poller API */
51struct poller;
52
53enum poller_ret {
54 POLLER_OK = 0,
55 POLLER_REMOVE,
56 POLLER_EXIT,
57};
58
59typedef enum poller_ret (*poller_fn_t)(struct handler *handler,
60 int revents, void *data);
61
62struct poller *console_register_poller(struct console *console,
63 struct handler *handler, poller_fn_t poller_fn,
64 int fd, int events, void *data);
65
66void console_unregister_poller(struct console *console, struct poller *poller);
67
Jeremy Kerrd66195c2016-03-16 17:24:51 +080068/* config API */
69struct config;
70const char *config_get_value(struct config *config, const char *name);
71struct config *config_init(const char *filename);
72void config_fini(struct config *config);
73
Jeremy Kerr2bd05182016-03-10 16:59:43 +080074/* socket paths */
Jeremy Kerr0cff6522016-03-18 09:57:01 +080075extern const char *console_socket_path;
76extern const size_t console_socket_path_len;
77extern const char *console_socket_path_readable;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080078
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080079/* utils */
80int write_buf_to_fd(int fd, const uint8_t *buf, size_t len);
81
82#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
83
84#define offsetof(type, member) \
85 ((unsigned long)&((type *)NULL)->member)
86
87#define container_of(ptr, type, member) \
88 ((type *)((void *)((ptr) - offsetof(type, member))))
89
Jeremy Kerr658cbda2016-03-09 18:10:00 +080090#define BUILD_ASSERT(c) \
91 do { \
92 char __c[(c)?1:-1] __attribute__((unused)); \
93 } while (0)