blob: 67cc99e86854f483ef04c98ca955f3bc46e92250 [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
24/* handler API */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080025enum {
26 HANDLER_OK = 0,
27 HANDLER_EXIT,
28};
29
Jeremy Kerr1b575012017-02-08 11:22:23 +080030/*
31 * Console data handlers: these implement the functions that process
32 * data coming out of the main tty device.
33 *
34 * Handlers are registered at link time using the console_handler_register()
35 * macro. We call each handler's ->init() function at startup, and ->fini() at
36 * exit.
37 *
38 * Incoming data from the tty will be passed to the handler through the
39 * ->data_in() function. To send data to the tty, use console_data_out().
40 *
41 * If a handler needs to monitor a separate file descriptor for events, use the
42 * poller API, through console_poller_register().
43 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080044struct handler {
45 const char *name;
46 int (*init)(struct handler *handler,
Jeremy Kerrd47963e2016-03-16 17:29:55 +080047 struct console *console,
48 struct config *config);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080049 int (*data_in)(struct handler *handler,
50 uint8_t *buf, size_t len);
51 void (*fini)(struct handler *handler);
Jeremy Kerr021b91f2016-04-28 11:51:52 +080052 bool active;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080053};
54
55#define __handler_name(n) __handler_ ## n
56#define _handler_name(n) __handler_name(n)
57
Jeremy Kerr55c97122017-02-07 17:06:46 +080058#define console_handler_register(h) \
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080059 static const \
60 __attribute__((section("handlers"))) \
61 __attribute__((used)) \
62 struct handler * _handler_name(__COUNTER__) = h;
63
64int console_data_out(struct console *console, const uint8_t *data, size_t len);
65
Jeremy Kerr329a35f2016-03-10 15:36:01 +080066/* poller API */
67struct poller;
68
69enum poller_ret {
70 POLLER_OK = 0,
71 POLLER_REMOVE,
72 POLLER_EXIT,
73};
74
75typedef enum poller_ret (*poller_fn_t)(struct handler *handler,
76 int revents, void *data);
77
Jeremy Kerr55c97122017-02-07 17:06:46 +080078struct poller *console_poller_register(struct console *console,
Jeremy Kerr329a35f2016-03-10 15:36:01 +080079 struct handler *handler, poller_fn_t poller_fn,
80 int fd, int events, void *data);
81
Jeremy Kerr55c97122017-02-07 17:06:46 +080082void console_poller_unregister(struct console *console, struct poller *poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +080083
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080084/* ringbuffer API */
85enum ringbuffer_poll_ret {
86 RINGBUFFER_POLL_OK = 0,
87 RINGBUFFER_POLL_REMOVE,
88};
89
90typedef enum ringbuffer_poll_ret (*ringbuffer_poll_fn_t)(void *data,
91 size_t force_len);
92
93struct ringbuffer;
94struct ringbuffer_consumer;
95
96struct ringbuffer *ringbuffer_init(size_t size);
97void ringbuffer_fini(struct ringbuffer *rb);
98
99struct ringbuffer_consumer *ringbuffer_consumer_register(struct ringbuffer *rb,
100 ringbuffer_poll_fn_t poll_fn, void *data);
101
102void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc);
103
104int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len);
105
106size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
107 uint8_t **data);
108
109int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len);
110
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800111/* config API */
112struct config;
113const char *config_get_value(struct config *config, const char *name);
114struct config *config_init(const char *filename);
115void config_fini(struct config *config);
116
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800117/* socket paths */
Jeremy Kerr0cff6522016-03-18 09:57:01 +0800118extern const char *console_socket_path;
119extern const size_t console_socket_path_len;
120extern const char *console_socket_path_readable;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800121
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800122/* utils */
123int write_buf_to_fd(int fd, const uint8_t *buf, size_t len);
124
125#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
126
127#define offsetof(type, member) \
128 ((unsigned long)&((type *)NULL)->member)
129
130#define container_of(ptr, type, member) \
131 ((type *)((void *)((ptr) - offsetof(type, member))))
132
Jeremy Kerr658cbda2016-03-09 18:10:00 +0800133#define BUILD_ASSERT(c) \
134 do { \
135 char __c[(c)?1:-1] __attribute__((unused)); \
136 } while (0)