QDNix
Quick’n’dirty *NIX
console.c
1 #include "dev/device.h"
2 #include <stddef.h>
3 #include <stdint.h>
4 
5 #include <sys/console.h>
6 
7 #ifdef CONFIG_BUFFERED_CONSOLE
8 static char console_buffer[CONFIG_CONSOLE_BUFFER_SIZE] = { '\0' };
9 static size_t console_buffer_idx = 0;
10 #endif /* CONFIG_BUFFERED_CONSOLE */
11 
12 static Console *console = NULL;
13 
14 void
15 console_flush(void)
16 {
17 #ifdef CONFIG_BUFFERED_CONSOLE
18  if (console != NULL && console->write != NULL && console_buffer_idx > 0)
19  {
20  console->write(console->device, console_buffer, console_buffer_idx);
21  }
22 #endif /* CONFIG_BUFFERED_CONSOLE */
23 }
24 
25 void
26 console_putchar(char c)
27 {
28 #ifdef CONFIG_BUFFERED_CONSOLE
29  if (console_buffer_idx > CONFIG_CONSOLE_BUFFER_SIZE)
30  {
31  console_flush();
32  }
33  console_buffer[console_buffer_idx++] = c;
34 #else
35  if (console != NULL && console->write != NULL)
36  {
37  console->write(console->device, &c, 1);
38  }
39 #endif /* CONFIG_BUFFERED_CONSOLE */
40 }
41 
42 void
43 console_setup(Device *dev)
44 {
45  /* XXX: rework drivers */
46  static Console cons;
47 
48  if (dev->class == DEVICE_TTY)
49  {
50  cons.write = dev->drivers.serial.write;
51  cons.read = dev->drivers.serial.read;
52  }
53  cons.device = &(dev->drivers.serial);
54 
55  console = &cons;
56 #ifdef CONFIG_BUFFERED_CONSOLE
57  if (console_buffer_idx > 0)
58  {
59  console_flush();
60  }
61 #endif /* CONFIG_BUFFERED_CONSOLE */
62 }
63 
Definition: console.h:7
Definition: device.h:20