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 { |
| 48 | const char *name; |
| 49 | int (*init)(struct handler *handler, |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 50 | struct console *console, |
| 51 | struct config *config); |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 52 | void (*fini)(struct handler *handler); |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 53 | int (*baudrate)(struct handler *handler, |
| 54 | speed_t baudrate); |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 55 | bool active; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | #define __handler_name(n) __handler_ ## n |
| 59 | #define _handler_name(n) __handler_name(n) |
| 60 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 61 | #define console_handler_register(h) \ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 62 | static const \ |
| 63 | __attribute__((section("handlers"))) \ |
| 64 | __attribute__((used)) \ |
| 65 | struct handler * _handler_name(__COUNTER__) = h; |
| 66 | |
| 67 | int console_data_out(struct console *console, const uint8_t *data, size_t len); |
| 68 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 69 | /* poller API */ |
| 70 | struct poller; |
| 71 | |
| 72 | enum poller_ret { |
| 73 | POLLER_OK = 0, |
| 74 | POLLER_REMOVE, |
| 75 | POLLER_EXIT, |
| 76 | }; |
| 77 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 78 | typedef enum poller_ret (*poller_event_fn_t)(struct handler *handler, |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 79 | int revents, void *data); |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 80 | typedef enum poller_ret (*poller_timeout_fn_t)(struct handler *handler, |
| 81 | void *data); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 82 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 83 | struct poller *console_poller_register(struct console *console, |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 84 | struct handler *handler, poller_event_fn_t event_fn, |
| 85 | poller_timeout_fn_t timeout_fn, int fd, int events, |
| 86 | void *data); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 87 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 88 | void console_poller_unregister(struct console *console, struct poller *poller); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 89 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 90 | void console_poller_set_events(struct console *console, struct poller *poller, |
| 91 | int events); |
| 92 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 93 | void console_poller_set_timeout(struct console *console, struct poller *poller, |
| 94 | const struct timeval *interval); |
| 95 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 96 | /* ringbuffer API */ |
| 97 | enum ringbuffer_poll_ret { |
| 98 | RINGBUFFER_POLL_OK = 0, |
| 99 | RINGBUFFER_POLL_REMOVE, |
| 100 | }; |
| 101 | |
| 102 | typedef enum ringbuffer_poll_ret (*ringbuffer_poll_fn_t)(void *data, |
| 103 | size_t force_len); |
| 104 | |
| 105 | struct ringbuffer; |
| 106 | struct ringbuffer_consumer; |
| 107 | |
| 108 | struct ringbuffer *ringbuffer_init(size_t size); |
| 109 | void ringbuffer_fini(struct ringbuffer *rb); |
| 110 | |
| 111 | struct ringbuffer_consumer *ringbuffer_consumer_register(struct ringbuffer *rb, |
| 112 | ringbuffer_poll_fn_t poll_fn, void *data); |
| 113 | |
| 114 | void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc); |
| 115 | |
| 116 | int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len); |
| 117 | |
| 118 | size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset, |
| 119 | uint8_t **data); |
| 120 | |
| 121 | int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len); |
| 122 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 123 | size_t ringbuffer_len(struct ringbuffer_consumer *rbc); |
| 124 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 125 | /* console wrapper around ringbuffer consumer registration */ |
| 126 | struct ringbuffer_consumer *console_ringbuffer_consumer_register( |
| 127 | struct console *console, |
| 128 | ringbuffer_poll_fn_t poll_fn, void *data); |
| 129 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 130 | /* config API */ |
| 131 | struct config; |
| 132 | const char *config_get_value(struct config *config, const char *name); |
| 133 | struct config *config_init(const char *filename); |
| 134 | void config_fini(struct config *config); |
| 135 | |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 136 | int config_parse_baud(speed_t *speed, const char *baud_string); |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 137 | uint32_t parse_baud_to_int(speed_t speed); |
| 138 | speed_t parse_int_to_baud(uint32_t baud); |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 139 | int config_parse_logsize(const char *size_str, size_t *size); |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 140 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 141 | /* socket paths */ |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame^] | 142 | ssize_t console_socket_path(struct sockaddr_un *addr, const char *id); |
| 143 | |
| 144 | typedef char (socket_path_t)[sizeof(((struct sockaddr_un *)NULL)->sun_path)]; |
| 145 | ssize_t console_socket_path_readable(const struct sockaddr_un *addr, |
| 146 | size_t addrlen, socket_path_t path); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 147 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 148 | /* utils */ |
| 149 | int write_buf_to_fd(int fd, const uint8_t *buf, size_t len); |
| 150 | |
| 151 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 152 | |
| 153 | #define offsetof(type, member) \ |
| 154 | ((unsigned long)&((type *)NULL)->member) |
| 155 | |
| 156 | #define container_of(ptr, type, member) \ |
| 157 | ((type *)((void *)((ptr) - offsetof(type, member)))) |
| 158 | |
Jeremy Kerr | 658cbda | 2016-03-09 18:10:00 +0800 | [diff] [blame] | 159 | #define BUILD_ASSERT(c) \ |
| 160 | do { \ |
| 161 | char __c[(c)?1:-1] __attribute__((unused)); \ |
| 162 | } while (0) |