blob: 7c2e711121a0d0eb5d54ed7781cde4ba2fb085e3 [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
Johnathan Manteya395a772019-03-19 14:28:53 -070017#pragma once
18
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080019#include <poll.h>
Jeremy Kerr021b91f2016-04-28 11:51:52 +080020#include <stdbool.h>
Benjamin Fairfcbdea92018-06-04 14:19:25 -070021#include <stdint.h>
22#include <termios.h> /* for speed_t */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080023#include <time.h>
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080024#include <systemd/sd-bus.h>
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080025#include <sys/time.h>
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103026#include <sys/un.h>
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080027
Jeremy Kerr329a35f2016-03-10 15:36:01 +080028struct console;
Jeremy Kerrd47963e2016-03-16 17:29:55 +080029struct config;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080030
Jeremy Kerrf733c852017-02-07 18:40:10 +080031/* Handler API.
32 *
Jeremy Kerr1b575012017-02-08 11:22:23 +080033 * Console data handlers: these implement the functions that process
34 * data coming out of the main tty device.
35 *
36 * Handlers are registered at link time using the console_handler_register()
37 * macro. We call each handler's ->init() function at startup, and ->fini() at
38 * exit.
39 *
Jeremy Kerrf733c852017-02-07 18:40:10 +080040 * Handlers will almost always want to register a ringbuffer consumer, which
41 * provides data coming from the tty. Use cosole_register_ringbuffer_consumer()
42 * for this. To send data to the tty, use console_data_out().
Jeremy Kerr1b575012017-02-08 11:22:23 +080043 *
44 * If a handler needs to monitor a separate file descriptor for events, use the
45 * poller API, through console_poller_register().
46 */
Jeremy Kerre2826c72024-07-05 10:54:21 +080047struct handler;
48
49struct handler_type {
Andrew Jefferya72711a2023-04-18 18:19:41 +093050 const char *name;
Jeremy Kerre2826c72024-07-05 10:54:21 +080051 struct handler *(*init)(const struct handler_type *type,
52 struct console *console, struct config *config);
Andrew Jefferya72711a2023-04-18 18:19:41 +093053 void (*fini)(struct handler *handler);
54 int (*baudrate)(struct handler *handler, speed_t baudrate);
Jeremy Kerre2826c72024-07-05 10:54:21 +080055};
56
57struct handler {
58 const struct handler_type *type;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080059};
60
Andrew Jeffery750fb0c2023-04-19 11:48:50 +093061/* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Andrew Jefferya72711a2023-04-18 18:19:41 +093062#define __handler_name(n) __handler_##n
63#define _handler_name(n) __handler_name(n)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080064
Jeremy Kerre2826c72024-07-05 10:54:21 +080065#ifndef __clang__
66#define handler_type_check(h) BUILD_ASSERT_OR_ZERO((h)->init && (h)->fini)
67#else
68/* clang doesn't seem to be able to constify the type ops */
69#define handler_type_check(h) 0
70#endif
71
Andrew Jefferya72711a2023-04-18 18:19:41 +093072#define console_handler_register(h) \
73 static const __attribute__((section("handlers"))) \
Jeremy Kerre2826c72024-07-05 10:54:21 +080074 __attribute__((used)) struct handler_type * \
75 _handler_name(__COUNTER__) = (h) + handler_type_check(h)
Andrew Jeffery750fb0c2023-04-19 11:48:50 +093076/* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080077
78int console_data_out(struct console *console, const uint8_t *data, size_t len);
79
Jeremy Kerr329a35f2016-03-10 15:36:01 +080080enum poller_ret {
81 POLLER_OK = 0,
82 POLLER_REMOVE,
83 POLLER_EXIT,
84};
85
Ninad Palsuleb14ca192023-03-31 09:49:00 -050086typedef char(socket_path_t)[sizeof(((struct sockaddr_un *)NULL)->sun_path)];
87
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080088typedef enum poller_ret (*poller_event_fn_t)(struct handler *handler,
Andrew Jefferya72711a2023-04-18 18:19:41 +093089 int revents, void *data);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080090typedef enum poller_ret (*poller_timeout_fn_t)(struct handler *handler,
Andrew Jefferya72711a2023-04-18 18:19:41 +093091 void *data);
Jeremy Kerr329a35f2016-03-10 15:36:01 +080092
Andrew Jeffery30ea6382023-05-03 16:56:10 +093093enum tty_device {
94 TTY_DEVICE_UNDEFINED = 0,
95 TTY_DEVICE_VUART,
96 TTY_DEVICE_UART,
97 TTY_DEVICE_PTY,
98};
99
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200100struct console_server {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930101 struct {
102 const char *kname;
103 char *dev;
104 int fd;
105 enum tty_device type;
106 union {
107 struct {
108 char *sysfs_devnode;
109 int sirq;
110 uint16_t lpc_addr;
111 } vuart;
112 struct {
113 speed_t baud;
114 } uart;
115 };
116 } tty;
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200117
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200118 // All the pollfds are stored here,
119 // so 'poll' can operate on them.
120 // The other 'pollfd*' are just pointers to this array.
121 struct pollfd *pollfds;
122 size_t capacity_pollfds;
123
124 // index into pollfds
125 size_t tty_pollfd_index;
126
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200127 struct config *config;
128
129 // the currently active console
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200130 struct console *active;
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200131
132 struct console **consoles;
133 size_t n_consoles;
134
135 // index into (struct console_server)->pollfds
136 size_t dbus_pollfd_index;
137
138 struct sd_bus *bus;
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200139};
140
141struct console {
142 // point back to the console server
143 // which we are a member of
144 struct console_server *server;
145
Ninad Palsulee258e512023-04-26 22:17:57 -0500146 const char *console_id;
Ninad Palsulee258e512023-04-26 22:17:57 -0500147
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500148 /* Socket name starts with null character hence we need length */
149 socket_path_t socket_name;
150 ssize_t socket_name_len;
151
Ninad Palsulee258e512023-04-26 22:17:57 -0500152 struct ringbuffer *rb;
153
154 struct handler **handlers;
155 long n_handlers;
156
157 struct poller **pollers;
158 long n_pollers;
Ninad Palsulee258e512023-04-26 22:17:57 -0500159};
160
161/* poller API */
162struct poller {
163 struct handler *handler;
164 void *data;
165 poller_event_fn_t event_fn;
166 poller_timeout_fn_t timeout_fn;
167 struct timeval timeout;
168 bool remove;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500169
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200170 // index into (struct console_server)->pollfds
171 size_t pollfd_index;
Ninad Palsulee258e512023-04-26 22:17:57 -0500172};
173
Jeremy Kerr55c97122017-02-07 17:06:46 +0800174struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930175 struct handler *handler,
Andrew Jeffery64222152023-04-19 12:51:24 +0930176 poller_event_fn_t poller_fn,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930177 poller_timeout_fn_t timeout_fn, int fd,
178 int events, void *data);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800179
Jeremy Kerr55c97122017-02-07 17:06:46 +0800180void console_poller_unregister(struct console *console, struct poller *poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800181
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800182void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930183 int events);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800184
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800185void console_poller_set_timeout(struct console *console, struct poller *poller,
Andrew Jeffery64222152023-04-19 12:51:24 +0930186 const struct timeval *tv);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800187
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800188/* ringbuffer API */
Andrew Jeffery1b868442023-04-19 13:16:32 +0930189
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800190enum ringbuffer_poll_ret {
191 RINGBUFFER_POLL_OK = 0,
192 RINGBUFFER_POLL_REMOVE,
193};
194
195typedef enum ringbuffer_poll_ret (*ringbuffer_poll_fn_t)(void *data,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930196 size_t force_len);
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800197
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800198struct ringbuffer_consumer;
199
Andrew Jeffery1b868442023-04-19 13:16:32 +0930200struct ringbuffer {
201 uint8_t *buf;
202 size_t size;
203 size_t tail;
204 struct ringbuffer_consumer **consumers;
205 int n_consumers;
206};
207
208struct ringbuffer_consumer {
209 struct ringbuffer *rb;
210 ringbuffer_poll_fn_t poll_fn;
211 void *poll_data;
212 size_t pos;
213};
214
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800215struct ringbuffer *ringbuffer_init(size_t size);
216void ringbuffer_fini(struct ringbuffer *rb);
217
Andrew Jefferya72711a2023-04-18 18:19:41 +0930218struct ringbuffer_consumer *
219ringbuffer_consumer_register(struct ringbuffer *rb,
220 ringbuffer_poll_fn_t poll_fn, void *data);
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800221
222void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc);
223
224int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len);
225
226size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930227 uint8_t **data);
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800228
229int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len);
230
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800231size_t ringbuffer_len(struct ringbuffer_consumer *rbc);
232
Jeremy Kerrf733c852017-02-07 18:40:10 +0800233/* console wrapper around ringbuffer consumer registration */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930234struct ringbuffer_consumer *
235console_ringbuffer_consumer_register(struct console *console,
236 ringbuffer_poll_fn_t poll_fn, void *data);
Jeremy Kerrf733c852017-02-07 18:40:10 +0800237
Ninad Palsulee258e512023-04-26 22:17:57 -0500238/* Console server API */
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200239void tty_init_termios(struct console_server *server);
Ninad Palsulee258e512023-04-26 22:17:57 -0500240
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800241/* socket paths */
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500242ssize_t console_socket_path(socket_path_t path, const char *id);
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030243ssize_t console_socket_path_readable(const struct sockaddr_un *addr,
244 size_t addrlen, socket_path_t path);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800245
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800246/* utils */
247int write_buf_to_fd(int fd, const uint8_t *buf, size_t len);
248
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200249/* console_server dbus */
250int dbus_server_init(struct console_server *server);
251void dbus_server_fini(struct console_server *server);
252
Ninad Palsulee258e512023-04-26 22:17:57 -0500253/* console-dbus API */
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930254int dbus_init(struct console *console,
255 struct config *config __attribute__((unused)));
Ninad Palsulee258e512023-04-26 22:17:57 -0500256
Ninad Palsulebd992c92023-05-09 18:30:01 -0500257/* socket-handler API */
258int dbus_create_socket_consumer(struct console *console);
259
Patrick Williams397fd032023-02-10 05:50:21 -0600260#ifndef offsetof
Andrew Jefferya72711a2023-04-18 18:19:41 +0930261#define offsetof(type, member) ((unsigned long)&((type *)NULL)->member)
Patrick Williams397fd032023-02-10 05:50:21 -0600262#endif
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800263
Andrew Jefferya72711a2023-04-18 18:19:41 +0930264#define container_of(ptr, type, member) \
265 ((type *)((void *)((ptr)-offsetof(type, member))))
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800266
Andrew Jefferya72711a2023-04-18 18:19:41 +0930267#define BUILD_ASSERT(c) \
268 do { \
269 char __c[(c) ? 1 : -1] __attribute__((unused)); \
Jeremy Kerr658cbda2016-03-09 18:10:00 +0800270 } while (0)
Jeremy Kerra71b3952024-07-05 11:52:49 +0800271
272#define BUILD_ASSERT_OR_ZERO(c) (sizeof(char[(c) ? 1 : -1]) - 1)
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200273
274// returns the index of that pollfd in server->pollfds
275// we cannot return a pointer because 'realloc' may move server->pollfds
276ssize_t console_server_request_pollfd(struct console_server *server, int fd,
277 short int events);
278
279int console_server_release_pollfd(struct console_server *server,
280 size_t pollfd_index);