summaryrefslogtreecommitdiff
path: root/src/kernel/console.c
blob: bf83fec33fe79e1081f524cf31debd3837417716 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "console.h"

#include "string.h"
#include <stddef.h>

void console_init( console_t *console )
{
	memset( console, 0, sizeof( console_t ) );
	
	console->vga_text = NULL;
	console->serial = NULL;
	console->network = NULL;
}

void console_add_vga_text_output( console_t *console, vga_text_t *vga_text )
{
	console->vga_text = vga_text;
}

void console_add_serial_output( console_t *console, serial_t *serial )
{
	console->serial = serial;
}

void console_add_network_output( console_t *console, network_t *network )
{
	console->network = network;
}

static void send_via_udp( network_t *network, const char *s, size_t n )
{
	//~ network_ethernet_packet_t *packet = (network_ethernet_packet_t *)event->buf.data;

	//~ network_mac_address_t dst_addr;
	//~ network_mac_address_t src_addr;
	//~ network_ether_type_t type;
//~ } __attribute__( ( packed ) ) network_ethernet_packet_header_t;
//~ typedef struct {
	//~ network_ethernet_packet_header_t header;
	//~ uint8_t *data;
//~ } __attribute__( ( packed ) ) network_ethernet_packet_t;

	//~ packet->header.dst_addr = arp->source_hardware_address;
	//~ packet->header.src_addr = global_context.network->mac_address;
	//~ arp->operation = ARP_OPERATION_ANSWER;
	//~ arp->destination_hardware_address = arp->source_hardware_address;
	//~ arp->source_hardware_address = global_context.network->mac_address;
	//~ network_ipv4_address_t tmp = arp->destination_protocol_address;
	//~ arp->destination_protocol_address = arp->source_protocol_address;
	//~ arp->source_protocol_address = tmp;
	//~ event->buf.length = sizeof( network_ethernet_packet_header_t ) + sizeof( network_arp_packet_t );
			
	//~ ((network_vtable_t *)( global_context.network->base.vtable ))->send( global_context.network, event->buf );
}

void console_put_char( console_t *console, const char c )
{
	if( console->vga_text != NULL ) {
		vga_text_put_char( console->vga_text, c );
	}
	
	if( console->serial != NULL ) {
		serial_put_char( console->serial, c );
	}
	
	if( console->network != NULL ) {
		send_via_udp( console->network, &c, 1 );
	}
}

void console_put_string( console_t *console, const char *s )
{
	if( console->vga_text != NULL ) {
		vga_text_put_string( console->vga_text, s );
	}
	
	if( console->serial != NULL ) {
		serial_put_string( console->serial, s );
	}

	if( console->network != NULL ) {
		send_via_udp( console->network, s, strlen( s ) );
	}
}

void console_put_newline( console_t *console )
{
	if( console->vga_text != NULL ) {
		// wipe contents till the end of the line on the
		// VGA text console, old data could still be there
		// from the BIOS(es)
		int x = vga_text_get_cursor_x( console->vga_text );
		for( int i = x; i < console->vga_text->res_x; i++ ) {
			vga_text_put_char( console->vga_text, ' ' );
		}
	}
	
	if( console->serial != NULL ) {
		serial_put_newline( console->serial );
	}

	if( console->network != NULL ) {
		char c = '\n';
		send_via_udp( console->network, &c, 1 );
	}
}