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 = (Console *) data;
17 SerialConsole *const serial_con = static_cast<SerialConsole *>(console);
18 serial_con->handle_irq();
19
20 return true;
21}
22
23size_t SerialConsole::do_write(const char *data, size_t size)
24{
25 return device->write_data(data, length: size);
26}
27
28bool SerialConsole::set_color(StandardColor fg, StandardColor bg)
29{
30 if (this->fg == fg && this->bg == bg)
31 return true; // no change
32
33 this->fg = fg;
34 this->bg = bg;
35 char buf[64] = { 0 };
36 get_ansi_color(buf, fg, bg);
37 device->write_data(ANSI_COLOR_RESET, length: sizeof(ANSI_COLOR_RESET) - 1);
38 device->write_data(data: buf, length: strlen(str: buf));
39 return true;
40}
41
42bool SerialConsole::clear()
43{
44 device->write_data(data: "\033[2J", length: 4);
45 return true;
46}
47
48void SerialConsole::handle_irq()
49{
50 while (device->get_data_ready())
51 {
52 char c = device->ReadByte();
53 if (c == '\r')
54 c = '\n';
55 if (c == '\0')
56 break;
57 device->WriteByte(byte: c);
58 this->putc(c);
59 }
60};
61