Modestus Moon OS  R4
CS 450 project
serial.c
Go to the documentation of this file.
1 /*
2  ----- serial.c -----
3 
4  Description..: Contains methods and variables used for
5  serial input and output.
6 */
7 
8 #include <stdint.h>
9 #include <string.h>
10 
11 #include <core/io.h>
12 #include <core/serial.h>
13 #include <core/commhand.h>
14 #include <input.h>
15 
16 #define NO_ERROR 0
17 
18 // Active devices used for serial I/O
21 
22 /*
23  Procedure..: init_serial
24  Description..: Initializes devices for user interaction, logging, ...
25 */
26 int init_serial(int device)
27 {
28  outb(device + 1, 0x00); //disable interrupts
29  outb(device + 3, 0x80); //set line control register
30  outb(device + 0, 115200/9600); //set bsd least sig bit
31  outb(device + 1, 0x00); //brd most significant bit
32  outb(device + 3, 0x03); //lock divisor; 8bits, no parity, one stop
33  outb(device + 2, 0xC7); //enable fifo, clear, 14byte threshold
34  outb(device + 4, 0x0B); //enable interrupts, rts/dsr set
35  (void)inb(device); //read bit to reset port
36  return NO_ERROR;
37 }
38 
39 /*
40  Procedure..: serial_println
41  Description..: Writes a message to the active serial output device.
42  Appends a newline character.
43 */
44 int serial_println(const char *msg)
45 {
46  int i;
47  for(i=0; *(i+msg)!='\0'; i++){
48  outb(serial_port_out,*(i+msg));
49  }
50  outb(serial_port_out,'\r');
51  outb(serial_port_out,'\n');
52  return NO_ERROR;
53 }
54 
55 /*
56  Procedure..: serial_print
57  Description..: Writes a message to the active serial output device.
58 */
59 int serial_print(const char *msg)
60 {
61  int i;
62  for(i=0; *(i+msg)!='\0'; i++){
63  outb(serial_port_out,*(i+msg));
64  }
65  if (*msg == '\r') outb(serial_port_out,'\n');
66  return NO_ERROR;
67 }
68 
69 /*
70  Procedure..: set_serial_out
71  Description..: Sets serial_port_out to the given device address.
72  All serial output, such as that from serial_println, will be
73  directed to this device.
74 */
75 int set_serial_out(int device)
76 {
77  serial_port_out = device;
78  return NO_ERROR;
79 }
80 
81 /*
82  Procedure..: set_serial_in
83  Description..: Sets serial_port_in to the given device address.
84  All serial input, such as console input via a virtual machine,
85  QEMU/Bochs/etc, will be directed to this device.
86 */
87 int set_serial_in(int device)
88 {
89  serial_port_in = device;
90  return NO_ERROR;
91 }
92 
93 
94 void clear_buff() {
95  serial_print("\033[2K");
96  serial_print("\033[G");
97  serial_print("> ");
98 }
99 
100 void return_cursor(int cursor_loc, int length) {
101  int move = length - cursor_loc;
102  int i = 0;
103  for (; i < move; i++) {
104  serial_print("\033[D");
105  }
106 }
107 
108 /*
109  * @brief serial_poll Displays the user input and when a return signal is recieved, sends it to the command handler
110  * @param in_string The array to hold the user input
111  * @return The input string entered by the user
112  */
113 
115  //in_char is the character read from the register
116  //out_char is that character with a null terminator appended on the end
117  //length is the current size of the input
118  //cursor_loc is the current location of the cursor in the input
119  char in_char;
120  char out_char[2];
121  out_char[1] = '\0';
122  int length = 0;
123  int cursor_loc = 0;
124 
125  //polling for user input
126  while (1) {
127 
128  if (inb(COM1+5)&1) {
129  //the character is read from the register
130  in_char = inb(COM1);
131  out_char[0] = in_char;
132 
133  if (in_char == BACKSPACE) {
134  if (length != 0) {
135  if (cursor_loc == length)
136  {
137  cursor_loc--;
138  in_string[cursor_loc] = '\0';
139  clear_buff();
141  length--;
142  return_cursor(cursor_loc, length);
143 
144  }
145  else
146  {
147  cursor_loc--;
148  strcpy(&in_string[cursor_loc], &in_string[cursor_loc + 1]);
149  in_string[length - 1] = '\0';
150  clear_buff();
152  length--;
153  return_cursor(cursor_loc, length);
154  }
155  }
156  } else if (in_char == DELETE) {
157  } else if (in_char == RETURN || in_char == NEW_LINE) {
158 
159  return in_string;
160  } else if (in_char == ESC) {
161  //buffer through ansi escape keys ([)
162  in_char = inb(COM1);
163  in_char = inb(COM1);
164 
165  if (in_char == 'A') { //UP
166  clear_buff();
167  in_string[0] = '\0';
168  cursor_loc = 0;
169  length = 0;
170  //go backwards in command history
171  } else if (in_char == 'B') { //DOWN
172  clear_buff();
173  in_string[0] = '\0';
174  cursor_loc = 0;
175  length = 0;
176  //go forward in command history
177  } else if (in_char == 'C') { //RIGHT
178  if (cursor_loc < length) {
179  serial_print("\033[C");
180  cursor_loc++;
181  }
182  } else if (in_char == 'D') { //LEFT
183  if (cursor_loc > 0) {
184  serial_print("\033[D");
185  cursor_loc--;
186  }
187  } else if (in_char == '3') { //DEL
188  strcpy(&in_string[cursor_loc], &in_string[cursor_loc + 1]);
189  in_string[length] = '\0';
190  clear_buff();
192  length--;
193  return_cursor(cursor_loc, length);
194  }
195 
196  } else {
197  if (length < MAX_LENGTH - 1) {
198  if (length == cursor_loc) {
199  in_string[length] = in_char;
200  in_string[length + 1] = '\0';
201  serial_print(out_char);
202  cursor_loc++;
203  length++;
204  }
205  else {
206  int i = length;
207  in_string[length+1] = '\0';
208  for (; i >= cursor_loc; i--) {
209  in_string[i + 1] = in_string[i];
210  }
211  in_string[cursor_loc] = in_char;
212  length++;
213  cursor_loc++;
214  clear_buff();
216  return_cursor(cursor_loc, length);
217  }
218  }
219  }
220  }
221  }
222  return in_string;
223 }
#define BACKSPACE
Definition: input.h:9
void return_cursor(int cursor_loc, int length)
Definition: serial.c:100
#define MAX_LENGTH
Definition: input.h:6
int serial_print(const char *msg)
Definition: serial.c:59
int init_serial(int device)
Definition: serial.c:26
void clear_buff()
Definition: serial.c:94
char * serial_poll(char in_string[MAX_LENGTH])
Definition: serial.c:114
char * strcpy(char *s1, const char *s2)
strcpy copies one string to another string
Definition: string.c:26
int set_serial_out(int device)
Definition: serial.c:75
#define RETURN
Definition: input.h:16
char in_string[MAX_LENGTH]
Definition: commhand.c:10
int set_serial_in(int device)
Definition: serial.c:87
#define NEW_LINE
Definition: input.h:17
#define ESC
Definition: input.h:11
int serial_port_in
Definition: serial.c:20
#define COM1
Definition: serial.h:6
int serial_println(const char *msg)
Definition: serial.c:44
int serial_port_out
Definition: serial.c:19
#define DELETE
Definition: input.h:10
#define inb(port)
Definition: io.h:15
#define NO_ERROR
Definition: serial.c:16
#define outb(port, data)
Definition: io.h:8