1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mos/device/serial_console.hpp"
4
5#include "ansi_colors.h"
6#include "mos/device/console.hpp"
7
8#include <mos/lib/structures/list.hpp>
9#include <mos_stdio.hpp>
10#include <mos_string.hpp>
11
12bool serial_console_irq_handler(u32 irq, void *data)
13{
14 MOS_UNUSED(irq);
15
16 Console *const console = (class Console *) data;
17 SerialConsole *const serial_con = static_cast<SerialConsole *>(console);
18 serial_con->handle_irq();
19
20 return true;
21}
22bool SerialConsole::extra_setup()
23{
24 linked_list_init(head_node: &this->list_node);
25 this->caps |= CONSOLE_CAP_COLOR;
26 this->caps |= CONSOLE_CAP_CLEAR;
27 return device->setup();
28}
29
30size_t SerialConsole::do_write(const char *data, size_t size)
31{
32 return device->write_data(data, length: size);
33}
34
35bool SerialConsole::set_color(standard_color_t fg, standard_color_t bg)
36{
37 this->fg = fg;
38 this->bg = bg;
39 char buf[64] = { 0 };
40 get_ansi_color(buf, fg, bg);
41 device->write_data(ANSI_COLOR_RESET, length: sizeof(ANSI_COLOR_RESET) - 1);
42 device->write_data(data: buf, length: strlen(str: buf));
43 return true;
44}
45
46bool SerialConsole::clear()
47{
48 device->write_data(data: "\033[2J", length: 4);
49 return true;
50}
51
52bool SerialConsole::get_size(u32 *width, u32 *height)
53{
54 *width = 80;
55 *height = 25;
56 return true;
57}
58
59void SerialConsole::handle_irq()
60{
61 while (device->get_data_ready())
62 {
63 char c = device->read_byte();
64 if (c == '\r')
65 c = '\n';
66 device->write_byte(byte: c);
67 this->putc(c);
68 }
69};
70