blob: d6822c5f1d7178377b788a888af82059f37ba192 [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>
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080026
Jeremy Kerr329a35f2016-03-10 15:36:01 +080027struct console;
Jeremy Kerrd47963e2016-03-16 17:29:55 +080028struct config;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080029
Jeremy Kerrf733c852017-02-07 18:40:10 +080030/* Handler API.
31 *
Jeremy Kerr1b575012017-02-08 11:22:23 +080032 * Console data handlers: these implement the functions that process
33 * data coming out of the main tty device.
34 *
35 * Handlers are registered at link time using the console_handler_register()
36 * macro. We call each handler's ->init() function at startup, and ->fini() at
37 * exit.
38 *
Jeremy Kerrf733c852017-02-07 18:40:10 +080039 * Handlers will almost always want to register a ringbuffer consumer, which
40 * provides data coming from the tty. Use cosole_register_ringbuffer_consumer()
41 * for this. To send data to the tty, use console_data_out().
Jeremy Kerr1b575012017-02-08 11:22:23 +080042 *
43 * If a handler needs to monitor a separate file descriptor for events, use the
44 * poller API, through console_poller_register().
45 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080046struct handler {
47 const char *name;
48 int (*init)(struct handler *handler,
Jeremy Kerrd47963e2016-03-16 17:29:55 +080049 struct console *console,
50 struct config *config);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080051 void (*fini)(struct handler *handler);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080052 int (*baudrate)(struct handler *handler,
53 speed_t baudrate);
Jeremy Kerr021b91f2016-04-28 11:51:52 +080054 bool active;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080055};
56
57#define __handler_name(n) __handler_ ## n
58#define _handler_name(n) __handler_name(n)
59
Jeremy Kerr55c97122017-02-07 17:06:46 +080060#define console_handler_register(h) \
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080061 static const \
62 __attribute__((section("handlers"))) \
63 __attribute__((used)) \
64 struct handler * _handler_name(__COUNTER__) = h;
65
66int console_data_out(struct console *console, const uint8_t *data, size_t len);
67
Jeremy Kerr329a35f2016-03-10 15:36:01 +080068/* poller API */
69struct poller;
70
71enum poller_ret {
72 POLLER_OK = 0,
73 POLLER_REMOVE,
74 POLLER_EXIT,
75};
76
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080077typedef enum poller_ret (*poller_event_fn_t)(struct handler *handler,
Jeremy Kerr329a35f2016-03-10 15:36:01 +080078 int revents, void *data);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080079typedef enum poller_ret (*poller_timeout_fn_t)(struct handler *handler,
80 void *data);
Jeremy Kerr329a35f2016-03-10 15:36:01 +080081
Jeremy Kerr55c97122017-02-07 17:06:46 +080082struct poller *console_poller_register(struct console *console,
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080083 struct handler *handler, poller_event_fn_t event_fn,
84 poller_timeout_fn_t timeout_fn, int fd, int events,
85 void *data);
Jeremy Kerr329a35f2016-03-10 15:36:01 +080086
Jeremy Kerr55c97122017-02-07 17:06:46 +080087void console_poller_unregister(struct console *console, struct poller *poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +080088
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080089void console_poller_set_events(struct console *console, struct poller *poller,
90 int events);
91
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080092void console_poller_set_timeout(struct console *console, struct poller *poller,
93 const struct timeval *interval);
94
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080095/* ringbuffer API */
96enum ringbuffer_poll_ret {
97 RINGBUFFER_POLL_OK = 0,
98 RINGBUFFER_POLL_REMOVE,
99};
100
101typedef enum ringbuffer_poll_ret (*ringbuffer_poll_fn_t)(void *data,
102 size_t force_len);
103
104struct ringbuffer;
105struct ringbuffer_consumer;
106
107struct ringbuffer *ringbuffer_init(size_t size);
108void ringbuffer_fini(struct ringbuffer *rb);
109
110struct ringbuffer_consumer *ringbuffer_consumer_register(struct ringbuffer *rb,
111 ringbuffer_poll_fn_t poll_fn, void *data);
112
113void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc);
114
115int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len);
116
117size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
118 uint8_t **data);
119
120int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len);
121
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800122size_t ringbuffer_len(struct ringbuffer_consumer *rbc);
123
Jeremy Kerrf733c852017-02-07 18:40:10 +0800124/* console wrapper around ringbuffer consumer registration */
125struct ringbuffer_consumer *console_ringbuffer_consumer_register(
126 struct console *console,
127 ringbuffer_poll_fn_t poll_fn, void *data);
128
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800129/* config API */
130struct config;
131const char *config_get_value(struct config *config, const char *name);
132struct config *config_init(const char *filename);
133void config_fini(struct config *config);
134
Benjamin Fairfcbdea92018-06-04 14:19:25 -0700135int config_parse_baud(speed_t *speed, const char *baud_string);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800136uint32_t parse_baud_to_int(speed_t speed);
137speed_t parse_int_to_baud(uint32_t baud);
Kun Yi6424cc32018-06-14 14:09:28 -0700138int config_parse_logsize(const char *size_str, size_t *size);
Benjamin Fairfcbdea92018-06-04 14:19:25 -0700139
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800140/* socket paths */
Jeremy Kerr0cff6522016-03-18 09:57:01 +0800141extern const char *console_socket_path;
142extern const size_t console_socket_path_len;
143extern const char *console_socket_path_readable;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800144
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800145/* utils */
146int write_buf_to_fd(int fd, const uint8_t *buf, size_t len);
147
148#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
149
150#define offsetof(type, member) \
151 ((unsigned long)&((type *)NULL)->member)
152
153#define container_of(ptr, type, member) \
154 ((type *)((void *)((ptr) - offsetof(type, member))))
155
Jeremy Kerr658cbda2016-03-09 18:10:00 +0800156#define BUILD_ASSERT(c) \
157 do { \
158 char __c[(c)?1:-1] __attribute__((unused)); \
159 } while (0)