MOS Source Code
Loading...
Searching...
No Matches
console.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mos/tasks/signal.h"
4#include "mos/tasks/thread.h"
5
6#include <limits.h>
8#include <mos/io/io.h>
11#include <mos/syslog/printk.h>
12#include <mos/tasks/schedule.h>
13#include <mos/tasks/wait.h>
14#include <mos_string.h>
15
17
18static size_t console_io_read(io_t *io, void *data, size_t size)
19{
20 console_t *con = container_of(io, console_t, io);
21
22retry_read:;
23 size_t read = 0;
24
27 {
29 bool ok = reschedule_for_waitlist(&con->waitlist);
30 if (!ok)
31 {
32 pr_emerg("console: '%s' closed", con->name);
33 return -EIO;
34 }
36
38 {
40 return -ERESTARTSYS;
41 }
42 }
43 else
44 {
45 read = ring_buffer_pos_pop_front(con->read.buf, &con->read.pos, data, size);
46 }
48
49 if (read == 0)
50 goto retry_read;
51
52 return read;
53}
54
55static size_t console_io_write(io_t *io, const void *data, size_t size)
56{
57 console_t *con = container_of(io, console_t, io);
59 if ((con->caps & CONSOLE_CAP_COLOR))
60 con->ops->set_color(con, con->default_fg, con->default_bg);
61 size_t ret = con->ops->write(con, data, size);
63 return ret;
64}
65
66static const io_op_t console_io_ops = {
67 .read = console_io_read,
68 .write = console_io_write,
69};
70
72{
74 {
75 bool result = con->ops->extra_setup(con);
76 if (!result)
77 {
78 pr_emerg("console: failed to setup '%s'", con->name);
79 return;
80 }
81 }
82
83 MOS_ASSERT_X(con->name != NULL, "console: %p's name is NULL", con);
84
86 io_flags_t flags = IO_WRITABLE;
87
88 if (con->caps & CONSOLE_CAP_READ)
89 {
90 MOS_ASSERT_X(con->read.buf, "console: '%s' has no read buffer", con->name);
93 flags |= IO_READABLE;
94 }
95
96 io_init(&con->io, IO_CONSOLE, flags, &console_io_ops);
99}
100
102{
104 return NULL;
105
107 {
108 if (strcmp(con->name, name) == 0)
109 return con;
110 }
111 return NULL;
112}
113
115{
117 {
118 if (strncmp(con->name, prefix, strlen(prefix)) == 0)
119 return con;
120 }
121 return NULL;
122}
123
124size_t console_write(console_t *con, const char *data, size_t size)
125{
127 size_t ret = con->ops->write(con, data, size);
129 return ret;
130}
131
132size_t console_write_color(console_t *con, const char *data, size_t size, standard_color_t fg, standard_color_t bg)
133{
134 standard_color_t prev_fg, prev_bg;
136 if (con->caps & CONSOLE_CAP_COLOR)
137 {
138 con->ops->get_color(con, &prev_fg, &prev_bg);
139 if (prev_fg != fg || prev_bg != bg)
140 con->ops->set_color(con, fg, bg);
141 }
142
143 size_t ret = con->ops->write(con, data, size);
145 return ret;
146}
147
149{
150 if (c == 0x3)
151 {
154 {
155 thread_t *thread = thread_get(entry->waiter);
156 if (thread)
157 signal_send_to_thread(thread, SIGINT);
158 }
160 }
161
163 waitlist_wake(&con->waitlist, INT_MAX);
164}
#define MOS_ASSERT_X(cond, msg,...)
Definition assert.h:15
size_t console_write_color(console_t *con, const char *data, size_t size, standard_color_t fg, standard_color_t bg)
Definition console.c:132
static size_t console_io_read(io_t *io, void *data, size_t size)
Definition console.c:18
console_t * console_get_by_prefix(const char *prefix)
Definition console.c:114
void console_register(console_t *con)
Definition console.c:71
size_t console_write(console_t *con, const char *data, size_t size)
Definition console.c:124
static const io_op_t console_io_ops
Definition console.c:66
static size_t console_io_write(io_t *io, const void *data, size_t size)
Definition console.c:55
console_t * console_get(const char *name)
Definition console.c:101
void console_putc(console_t *con, u8 c)
Definition console.c:148
list_head consoles
Definition console.c:16
@ CONSOLE_CAP_COLOR
Definition console.h:15
@ CONSOLE_CAP_READ
console supports read
Definition console.h:21
@ CONSOLE_CAP_EXTRA_SETUP
extra setup required
Definition console.h:20
long signal_send_to_thread(thread_t *target, signal_t signal)
Send a signal to a thread.
Definition signal.c:92
bool signal_has_pending(void)
Return true if there's a pending signal.
Definition signal.c:301
standard_color_t
Definition ansi_colors.h:18
MOSAPI s32 strncmp(const char *str1, const char *str2, size_t n)
Definition mos_string.c:32
MOSAPI s32 strcmp(const char *str1, const char *str2)
Definition mos_string.c:24
#define LIST_HEAD_INIT(container)
Definition list.h:38
MOSAPI void list_node_append(list_node_t *head, list_node_t *item)
Definition list.c:68
#define list_foreach(t, v, h)
Iterate over a list.
Definition list.h:83
#define list_node(element)
Get the ‘list_node’ of a list element. This is exactly the reverse of ‘list_entry’ above.
Definition list.h:68
list_node_t list_head
A linked list head.
Definition list.h:23
MOSAPI bool list_is_empty(const list_node_t *head)
Definition list.c:21
MOSAPI size_t ring_buffer_pos_pop_front(u8 *buffer, ring_buffer_pos_t *pos, u8 *buf, size_t size)
should_inline bool ring_buffer_pos_is_empty(ring_buffer_pos_t *pos)
Definition ring_buffer.h:60
MOSAPI void ring_buffer_pos_init(ring_buffer_pos_t *pos, size_t capacity)
Definition ring_buffer.c:44
should_inline size_t ring_buffer_pos_push_back_byte(u8 *buffer, ring_buffer_pos_t *pos, u8 data)
Definition ring_buffer.h:62
void io_init(io_t *io, io_type_t type, io_flags_t flags, const io_op_t *ops)
Definition io.c:44
io_flags_t
Definition io.h:25
@ IO_READABLE
Definition io.h:27
@ IO_WRITABLE
Definition io.h:28
@ IO_CONSOLE
Definition io.h:21
#define container_of(ptr, type, member)
Definition mos_global.h:50
#define NULL
Definition pb_syshdr.h:46
static size_t strlen(const char *s)
Definition pb_syshdr.h:80
#define pr_emerg(fmt,...)
Definition printk.h:39
__nodiscard bool reschedule_for_waitlist(waitlist_t *waitlist)
Definition schedule.c:174
#define ERESTARTSYS
Definition signal.h:10
size_t size
Definition slab.c:30
const char * name
Definition slab.c:31
#define spinlock_acquire(lock)
Definition spinlock.h:61
#define SPINLOCK_INIT
Definition spinlock.h:28
#define spinlock_release(lock)
Definition spinlock.h:62
console_caps caps
Definition console.h:32
struct console_ops * ops
Definition console.h:30
standard_color_t default_fg
Definition console.h:48
ring_buffer_pos_t pos
Definition console.h:38
io_t io
Definition console.h:29
spinlock_t lock
Definition console.h:37
size_t size
Definition console.h:40
const char * name
Definition console.h:31
struct console_t::@7 write
standard_color_t default_bg
Definition console.h:48
struct console_t::@6 read
waitlist_t waitlist
Definition console.h:33
u8 * buf
Definition console.h:39
Definition io.h:35
Definition io.h:46
The entry in the waiters list of a process, or a thread.
Definition wait.h:15
spinlock_t lock
Definition wait.h:23
list_head list
Definition wait.h:24
thread_t * thread_get(tid_t id)
Definition thread.c:166
unsigned char u8
Definition types.h:19
size_t waitlist_wake(waitlist_t *list, size_t max_wakeups)
Definition wait.c:45
__BEGIN_DECLS void waitlist_init(waitlist_t *list)
Definition wait.c:23