blob: 9571ff996d7a4b5c008fb21cb111b357c6c25a3d [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>
Jeremy Kerr021b91f2016-04-28 11:51:52 +080019#include <stdbool.h>
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080020
Jeremy Kerr329a35f2016-03-10 15:36:01 +080021struct console;
Jeremy Kerrd47963e2016-03-16 17:29:55 +080022struct config;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080023
Jeremy Kerrf733c852017-02-07 18:40:10 +080024/* Handler API.
25 *
Jeremy Kerr1b575012017-02-08 11:22:23 +080026 * Console data handlers: these implement the functions that process
27 * data coming out of the main tty device.
28 *
29 * Handlers are registered at link time using the console_handler_register()
30 * macro. We call each handler's ->init() function at startup, and ->fini() at
31 * exit.
32 *
Jeremy Kerrf733c852017-02-07 18:40:10 +080033 * Handlers will almost always want to register a ringbuffer consumer, which
34 * provides data coming from the tty. Use cosole_register_ringbuffer_consumer()
35 * for this. To send data to the tty, use console_data_out().
Jeremy Kerr1b575012017-02-08 11:22:23 +080036 *
37 * If a handler needs to monitor a separate file descriptor for events, use the
38 * poller API, through console_poller_register().
39 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080040struct handler {
41 const char *name;
42 int (*init)(struct handler *handler,
Jeremy Kerrd47963e2016-03-16 17:29:55 +080043 struct console *console,
44 struct config *config);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080045 void (*fini)(struct handler *handler);
Jeremy Kerr021b91f2016-04-28 11:51:52 +080046 bool active;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080047};
48
49#define __handler_name(n) __handler_ ## n
50#define _handler_name(n) __handler_name(n)
51
Jeremy Kerr55c97122017-02-07 17:06:46 +080052#define console_handler_register(h) \
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080053 static const \
54 __attribute__((section("handlers"))) \
55 __attribute__((used)) \
56 struct handler * _handler_name(__COUNTER__) = h;
57
58int console_data_out(struct console *console, const uint8_t *data, size_t len);
59
Jeremy Kerr329a35f2016-03-10 15:36:01 +080060/* poller API */
61struct poller;
62
63enum poller_ret {
64 POLLER_OK = 0,
65 POLLER_REMOVE,
66 POLLER_EXIT,
67};
68
69typedef enum poller_ret (*poller_fn_t)(struct handler *handler,
70 int revents, void *data);
71
Jeremy Kerr55c97122017-02-07 17:06:46 +080072struct poller *console_poller_register(struct console *console,
Jeremy Kerr329a35f2016-03-10 15:36:01 +080073 struct handler *handler, poller_fn_t poller_fn,
74 int fd, int events, void *data);
75
Jeremy Kerr55c97122017-02-07 17:06:46 +080076void console_poller_unregister(struct console *console, struct poller *poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +080077
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080078void console_poller_set_events(struct console *console, struct poller *poller,
79 int events);
80
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080081/* ringbuffer API */
82enum ringbuffer_poll_ret {
83 RINGBUFFER_POLL_OK = 0,
84 RINGBUFFER_POLL_REMOVE,
85};
86
87typedef enum ringbuffer_poll_ret (*ringbuffer_poll_fn_t)(void *data,
88 size_t force_len);
89
90struct ringbuffer;
91struct ringbuffer_consumer;
92
93struct ringbuffer *ringbuffer_init(size_t size);
94void ringbuffer_fini(struct ringbuffer *rb);
95
96struct ringbuffer_consumer *ringbuffer_consumer_register(struct ringbuffer *rb,
97 ringbuffer_poll_fn_t poll_fn, void *data);
98
99void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc);
100
101int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len);
102
103size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
104 uint8_t **data);
105
106int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len);
107
Jeremy Kerrf733c852017-02-07 18:40:10 +0800108/* console wrapper around ringbuffer consumer registration */
109struct ringbuffer_consumer *console_ringbuffer_consumer_register(
110 struct console *console,
111 ringbuffer_poll_fn_t poll_fn, void *data);
112
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800113/* config API */
114struct config;
115const char *config_get_value(struct config *config, const char *name);
116struct config *config_init(const char *filename);
117void config_fini(struct config *config);
118
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800119/* socket paths */
Jeremy Kerr0cff6522016-03-18 09:57:01 +0800120extern const char *console_socket_path;
121extern const size_t console_socket_path_len;
122extern const char *console_socket_path_readable;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800123
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800124/* utils */
125int write_buf_to_fd(int fd, const uint8_t *buf, size_t len);
126
127#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
128
129#define offsetof(type, member) \
130 ((unsigned long)&((type *)NULL)->member)
131
132#define container_of(ptr, type, member) \
133 ((type *)((void *)((ptr) - offsetof(type, member))))
134
Jeremy Kerr658cbda2016-03-09 18:10:00 +0800135#define BUILD_ASSERT(c) \
136 do { \
137 char __c[(c)?1:-1] __attribute__((unused)); \
138 } while (0)