blob: d275ad8ccace9f097f6cda8977b235d6895c6805 [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 Kerr1a0e03b2016-03-08 17:57:11 +080047struct handler {
Andrew Jefferya72711a2023-04-18 18:19:41 +093048 const char *name;
49 int (*init)(struct handler *handler, struct console *console,
50 struct config *config);
51 void (*fini)(struct handler *handler);
52 int (*baudrate)(struct handler *handler, speed_t baudrate);
53 bool active;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080054};
55
Andrew Jeffery750fb0c2023-04-19 11:48:50 +093056/* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Andrew Jefferya72711a2023-04-18 18:19:41 +093057#define __handler_name(n) __handler_##n
58#define _handler_name(n) __handler_name(n)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080059
Andrew Jefferya72711a2023-04-18 18:19:41 +093060#define console_handler_register(h) \
61 static const __attribute__((section("handlers"))) \
62 __attribute__((used)) struct handler * \
63 _handler_name(__COUNTER__) = h
Andrew Jeffery750fb0c2023-04-19 11:48:50 +093064/* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080065
66int console_data_out(struct console *console, const uint8_t *data, size_t len);
67
Jeremy Kerr329a35f2016-03-10 15:36:01 +080068enum poller_ret {
69 POLLER_OK = 0,
70 POLLER_REMOVE,
71 POLLER_EXIT,
72};
73
Ninad Palsuleb14ca192023-03-31 09:49:00 -050074typedef char(socket_path_t)[sizeof(((struct sockaddr_un *)NULL)->sun_path)];
75
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080076typedef enum poller_ret (*poller_event_fn_t)(struct handler *handler,
Andrew Jefferya72711a2023-04-18 18:19:41 +093077 int revents, void *data);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080078typedef enum poller_ret (*poller_timeout_fn_t)(struct handler *handler,
Andrew Jefferya72711a2023-04-18 18:19:41 +093079 void *data);
Jeremy Kerr329a35f2016-03-10 15:36:01 +080080
Ninad Palsulee258e512023-04-26 22:17:57 -050081/* Console server structure */
82struct console {
83 const char *tty_kname;
84 char *tty_sysfs_devnode;
85 char *tty_dev;
86 const char *console_id;
87 int tty_sirq;
88 uint16_t tty_lpc_addr;
89 speed_t tty_baud;
90 int tty_fd;
91
Ninad Palsuleb14ca192023-03-31 09:49:00 -050092 /* Socket name starts with null character hence we need length */
93 socket_path_t socket_name;
94 ssize_t socket_name_len;
95
Ninad Palsulee258e512023-04-26 22:17:57 -050096 struct ringbuffer *rb;
97
98 struct handler **handlers;
99 long n_handlers;
100
101 struct poller **pollers;
102 long n_pollers;
103
104 struct pollfd *pollfds;
105 struct sd_bus *bus;
106};
107
108/* poller API */
109struct poller {
110 struct handler *handler;
111 void *data;
112 poller_event_fn_t event_fn;
113 poller_timeout_fn_t timeout_fn;
114 struct timeval timeout;
115 bool remove;
116};
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500117
Ninad Palsulee258e512023-04-26 22:17:57 -0500118/* we have two extra entry in the pollfds array for the VUART tty */
119enum internal_pollfds {
120 POLLFD_HOSTTTY = 0,
121 POLLFD_DBUS = 1,
122 MAX_INTERNAL_POLLFD = 2,
123};
124
Jeremy Kerr55c97122017-02-07 17:06:46 +0800125struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930126 struct handler *handler,
Andrew Jeffery64222152023-04-19 12:51:24 +0930127 poller_event_fn_t poller_fn,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930128 poller_timeout_fn_t timeout_fn, int fd,
129 int events, void *data);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800130
Jeremy Kerr55c97122017-02-07 17:06:46 +0800131void console_poller_unregister(struct console *console, struct poller *poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800132
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800133void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930134 int events);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800135
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800136void console_poller_set_timeout(struct console *console, struct poller *poller,
Andrew Jeffery64222152023-04-19 12:51:24 +0930137 const struct timeval *tv);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800138
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800139/* ringbuffer API */
Andrew Jeffery1b868442023-04-19 13:16:32 +0930140
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800141enum ringbuffer_poll_ret {
142 RINGBUFFER_POLL_OK = 0,
143 RINGBUFFER_POLL_REMOVE,
144};
145
146typedef enum ringbuffer_poll_ret (*ringbuffer_poll_fn_t)(void *data,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930147 size_t force_len);
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800148
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800149struct ringbuffer_consumer;
150
Andrew Jeffery1b868442023-04-19 13:16:32 +0930151struct ringbuffer {
152 uint8_t *buf;
153 size_t size;
154 size_t tail;
155 struct ringbuffer_consumer **consumers;
156 int n_consumers;
157};
158
159struct ringbuffer_consumer {
160 struct ringbuffer *rb;
161 ringbuffer_poll_fn_t poll_fn;
162 void *poll_data;
163 size_t pos;
164};
165
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800166struct ringbuffer *ringbuffer_init(size_t size);
167void ringbuffer_fini(struct ringbuffer *rb);
168
Andrew Jefferya72711a2023-04-18 18:19:41 +0930169struct ringbuffer_consumer *
170ringbuffer_consumer_register(struct ringbuffer *rb,
171 ringbuffer_poll_fn_t poll_fn, void *data);
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800172
173void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc);
174
175int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len);
176
177size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930178 uint8_t **data);
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800179
180int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len);
181
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800182size_t ringbuffer_len(struct ringbuffer_consumer *rbc);
183
Jeremy Kerrf733c852017-02-07 18:40:10 +0800184/* console wrapper around ringbuffer consumer registration */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930185struct ringbuffer_consumer *
186console_ringbuffer_consumer_register(struct console *console,
187 ringbuffer_poll_fn_t poll_fn, void *data);
Jeremy Kerrf733c852017-02-07 18:40:10 +0800188
Ninad Palsulee258e512023-04-26 22:17:57 -0500189/* Console server API */
190void tty_init_termios(struct console *console);
191
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800192/* config API */
193struct config;
194const char *config_get_value(struct config *config, const char *name);
195struct config *config_init(const char *filename);
196void config_fini(struct config *config);
197
Benjamin Fairfcbdea92018-06-04 14:19:25 -0700198int config_parse_baud(speed_t *speed, const char *baud_string);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800199uint32_t parse_baud_to_int(speed_t speed);
200speed_t parse_int_to_baud(uint32_t baud);
Kun Yi6424cc32018-06-14 14:09:28 -0700201int config_parse_logsize(const char *size_str, size_t *size);
Benjamin Fairfcbdea92018-06-04 14:19:25 -0700202
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800203/* socket paths */
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500204ssize_t console_socket_path(socket_path_t path, const char *id);
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030205ssize_t console_socket_path_readable(const struct sockaddr_un *addr,
206 size_t addrlen, socket_path_t path);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800207
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800208/* utils */
209int write_buf_to_fd(int fd, const uint8_t *buf, size_t len);
210
Ninad Palsulee258e512023-04-26 22:17:57 -0500211/* console-dbus API */
212void dbus_init(struct console *console,
213 struct config *config __attribute__((unused)));
214
Andrew Jefferyf2232d52023-04-18 21:50:17 +0930215#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800216
Patrick Williams397fd032023-02-10 05:50:21 -0600217#ifndef offsetof
Andrew Jefferya72711a2023-04-18 18:19:41 +0930218#define offsetof(type, member) ((unsigned long)&((type *)NULL)->member)
Patrick Williams397fd032023-02-10 05:50:21 -0600219#endif
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800220
Andrew Jefferya72711a2023-04-18 18:19:41 +0930221#define container_of(ptr, type, member) \
222 ((type *)((void *)((ptr)-offsetof(type, member))))
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800223
Andrew Jefferya72711a2023-04-18 18:19:41 +0930224#define BUILD_ASSERT(c) \
225 do { \
226 char __c[(c) ? 1 : -1] __attribute__((unused)); \
Jeremy Kerr658cbda2016-03-09 18:10:00 +0800227 } while (0)