Jeremy Kerr | 9326d77 | 2016-03-17 17:15:02 +0800 | [diff] [blame] | 1 | /** |
| 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 Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 16 | |
Johnathan Mantey | a395a77 | 2019-03-19 14:28:53 -0700 | [diff] [blame] | 17 | #pragma once |
| 18 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 19 | #include <poll.h> |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 20 | #include <stdbool.h> |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <termios.h> /* for speed_t */ |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 23 | #include <time.h> |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 24 | #include <systemd/sd-bus.h> |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 25 | #include <sys/time.h> |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 26 | #include <sys/un.h> |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 27 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 28 | struct console; |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 29 | struct config; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 30 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 31 | /* Handler API. |
| 32 | * |
Jeremy Kerr | 1b57501 | 2017-02-08 11:22:23 +0800 | [diff] [blame] | 33 | * 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 Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 40 | * 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 Kerr | 1b57501 | 2017-02-08 11:22:23 +0800 | [diff] [blame] | 43 | * |
| 44 | * If a handler needs to monitor a separate file descriptor for events, use the |
| 45 | * poller API, through console_poller_register(). |
| 46 | */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 47 | struct handler { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 48 | 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 Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 54 | }; |
| 55 | |
Andrew Jeffery | 750fb0c | 2023-04-19 11:48:50 +0930 | [diff] [blame] | 56 | /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 57 | #define __handler_name(n) __handler_##n |
| 58 | #define _handler_name(n) __handler_name(n) |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 59 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 60 | #define console_handler_register(h) \ |
| 61 | static const __attribute__((section("handlers"))) \ |
| 62 | __attribute__((used)) struct handler * \ |
| 63 | _handler_name(__COUNTER__) = h |
Andrew Jeffery | 750fb0c | 2023-04-19 11:48:50 +0930 | [diff] [blame] | 64 | /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 65 | |
| 66 | int console_data_out(struct console *console, const uint8_t *data, size_t len); |
| 67 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 68 | enum poller_ret { |
| 69 | POLLER_OK = 0, |
| 70 | POLLER_REMOVE, |
| 71 | POLLER_EXIT, |
| 72 | }; |
| 73 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 74 | typedef char(socket_path_t)[sizeof(((struct sockaddr_un *)NULL)->sun_path)]; |
| 75 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 76 | typedef enum poller_ret (*poller_event_fn_t)(struct handler *handler, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 77 | int revents, void *data); |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 78 | typedef enum poller_ret (*poller_timeout_fn_t)(struct handler *handler, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 79 | void *data); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 80 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 81 | enum tty_device { |
| 82 | TTY_DEVICE_UNDEFINED = 0, |
| 83 | TTY_DEVICE_VUART, |
| 84 | TTY_DEVICE_UART, |
| 85 | TTY_DEVICE_PTY, |
| 86 | }; |
| 87 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 88 | /* Console server structure */ |
| 89 | struct console { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 90 | struct { |
| 91 | const char *kname; |
| 92 | char *dev; |
| 93 | int fd; |
| 94 | enum tty_device type; |
| 95 | union { |
| 96 | struct { |
| 97 | char *sysfs_devnode; |
| 98 | int sirq; |
| 99 | uint16_t lpc_addr; |
| 100 | } vuart; |
| 101 | struct { |
| 102 | speed_t baud; |
| 103 | } uart; |
| 104 | }; |
| 105 | } tty; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 106 | const char *console_id; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 107 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 108 | /* Socket name starts with null character hence we need length */ |
| 109 | socket_path_t socket_name; |
| 110 | ssize_t socket_name_len; |
| 111 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 112 | struct ringbuffer *rb; |
| 113 | |
| 114 | struct handler **handlers; |
| 115 | long n_handlers; |
| 116 | |
| 117 | struct poller **pollers; |
| 118 | long n_pollers; |
| 119 | |
| 120 | struct pollfd *pollfds; |
| 121 | struct sd_bus *bus; |
| 122 | }; |
| 123 | |
| 124 | /* poller API */ |
| 125 | struct poller { |
| 126 | struct handler *handler; |
| 127 | void *data; |
| 128 | poller_event_fn_t event_fn; |
| 129 | poller_timeout_fn_t timeout_fn; |
| 130 | struct timeval timeout; |
| 131 | bool remove; |
| 132 | }; |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 133 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 134 | /* we have two extra entry in the pollfds array for the VUART tty */ |
| 135 | enum internal_pollfds { |
| 136 | POLLFD_HOSTTTY = 0, |
| 137 | POLLFD_DBUS = 1, |
| 138 | MAX_INTERNAL_POLLFD = 2, |
| 139 | }; |
| 140 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 141 | struct poller *console_poller_register(struct console *console, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 142 | struct handler *handler, |
Andrew Jeffery | 6422215 | 2023-04-19 12:51:24 +0930 | [diff] [blame] | 143 | poller_event_fn_t poller_fn, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 144 | poller_timeout_fn_t timeout_fn, int fd, |
| 145 | int events, void *data); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 146 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 147 | void console_poller_unregister(struct console *console, struct poller *poller); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 148 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 149 | void console_poller_set_events(struct console *console, struct poller *poller, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 150 | int events); |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 151 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 152 | void console_poller_set_timeout(struct console *console, struct poller *poller, |
Andrew Jeffery | 6422215 | 2023-04-19 12:51:24 +0930 | [diff] [blame] | 153 | const struct timeval *tv); |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 154 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 155 | /* ringbuffer API */ |
Andrew Jeffery | 1b86844 | 2023-04-19 13:16:32 +0930 | [diff] [blame] | 156 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 157 | enum ringbuffer_poll_ret { |
| 158 | RINGBUFFER_POLL_OK = 0, |
| 159 | RINGBUFFER_POLL_REMOVE, |
| 160 | }; |
| 161 | |
| 162 | typedef enum ringbuffer_poll_ret (*ringbuffer_poll_fn_t)(void *data, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 163 | size_t force_len); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 164 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 165 | struct ringbuffer_consumer; |
| 166 | |
Andrew Jeffery | 1b86844 | 2023-04-19 13:16:32 +0930 | [diff] [blame] | 167 | struct ringbuffer { |
| 168 | uint8_t *buf; |
| 169 | size_t size; |
| 170 | size_t tail; |
| 171 | struct ringbuffer_consumer **consumers; |
| 172 | int n_consumers; |
| 173 | }; |
| 174 | |
| 175 | struct ringbuffer_consumer { |
| 176 | struct ringbuffer *rb; |
| 177 | ringbuffer_poll_fn_t poll_fn; |
| 178 | void *poll_data; |
| 179 | size_t pos; |
| 180 | }; |
| 181 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 182 | struct ringbuffer *ringbuffer_init(size_t size); |
| 183 | void ringbuffer_fini(struct ringbuffer *rb); |
| 184 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 185 | struct ringbuffer_consumer * |
| 186 | ringbuffer_consumer_register(struct ringbuffer *rb, |
| 187 | ringbuffer_poll_fn_t poll_fn, void *data); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 188 | |
| 189 | void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc); |
| 190 | |
| 191 | int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len); |
| 192 | |
| 193 | size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 194 | uint8_t **data); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 195 | |
| 196 | int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len); |
| 197 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 198 | size_t ringbuffer_len(struct ringbuffer_consumer *rbc); |
| 199 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 200 | /* console wrapper around ringbuffer consumer registration */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 201 | struct ringbuffer_consumer * |
| 202 | console_ringbuffer_consumer_register(struct console *console, |
| 203 | ringbuffer_poll_fn_t poll_fn, void *data); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 204 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 205 | /* Console server API */ |
| 206 | void tty_init_termios(struct console *console); |
| 207 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 208 | /* config API */ |
| 209 | struct config; |
| 210 | const char *config_get_value(struct config *config, const char *name); |
| 211 | struct config *config_init(const char *filename); |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 212 | const char *config_resolve_console_id(struct config *config, |
| 213 | const char *id_arg); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 214 | void config_fini(struct config *config); |
| 215 | |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 216 | int config_parse_baud(speed_t *speed, const char *baud_string); |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 217 | uint32_t parse_baud_to_int(speed_t speed); |
| 218 | speed_t parse_int_to_baud(uint32_t baud); |
Medicine Yeh | d6e8b64 | 2024-03-18 01:49:17 -0700 | [diff] [blame] | 219 | int config_parse_bytesize(const char *size_str, size_t *size); |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 220 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 221 | /* socket paths */ |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 222 | ssize_t console_socket_path(socket_path_t path, const char *id); |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 223 | ssize_t console_socket_path_readable(const struct sockaddr_un *addr, |
| 224 | size_t addrlen, socket_path_t path); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 225 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 226 | /* utils */ |
| 227 | int write_buf_to_fd(int fd, const uint8_t *buf, size_t len); |
| 228 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 229 | /* console-dbus API */ |
| 230 | void dbus_init(struct console *console, |
| 231 | struct config *config __attribute__((unused))); |
| 232 | |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 233 | /* socket-handler API */ |
| 234 | int dbus_create_socket_consumer(struct console *console); |
| 235 | |
Andrew Jeffery | f2232d5 | 2023-04-18 21:50:17 +0930 | [diff] [blame] | 236 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 237 | |
Patrick Williams | 397fd03 | 2023-02-10 05:50:21 -0600 | [diff] [blame] | 238 | #ifndef offsetof |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 239 | #define offsetof(type, member) ((unsigned long)&((type *)NULL)->member) |
Patrick Williams | 397fd03 | 2023-02-10 05:50:21 -0600 | [diff] [blame] | 240 | #endif |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 241 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 242 | #define container_of(ptr, type, member) \ |
| 243 | ((type *)((void *)((ptr)-offsetof(type, member)))) |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 244 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 245 | #define BUILD_ASSERT(c) \ |
| 246 | do { \ |
| 247 | char __c[(c) ? 1 : -1] __attribute__((unused)); \ |
Jeremy Kerr | 658cbda | 2016-03-09 18:10:00 +0800 | [diff] [blame] | 248 | } while (0) |