|
|
|
@ -72,6 +72,18 @@ static char usart_getc(struct usart_console *console, int block) |
|
|
|
|
return c; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int console_peek(char *c, struct usart_console *console, int block) |
|
|
|
|
{ |
|
|
|
|
while (block && console->cur == console->next); |
|
|
|
|
|
|
|
|
|
if (console->cur != console->next) { |
|
|
|
|
*c = console->recv_buf[console->cur]; |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return -EAGAIN; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int console_getc(char *c, struct usart_console *console) |
|
|
|
|
{ |
|
|
|
|
if (console->cur == console->next) |
|
|
|
@ -120,9 +132,65 @@ int console_getline(struct usart_console *console, char *buf, size_t n) |
|
|
|
|
*p = '\0'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
*p = '\0'; |
|
|
|
|
|
|
|
|
|
return -EAGAIN; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ssize_t console_read(struct usart_console *console, char *buf, size_t n) |
|
|
|
|
{ |
|
|
|
|
char *p = buf; |
|
|
|
|
char c; |
|
|
|
|
|
|
|
|
|
fprintf(console->fp, "console_read\n"); |
|
|
|
|
|
|
|
|
|
*buf = '\0'; |
|
|
|
|
|
|
|
|
|
while (console_peek(&c, console, 1) == 0) { |
|
|
|
|
switch (c) { |
|
|
|
|
case '\004': |
|
|
|
|
if (buf < p) |
|
|
|
|
goto out; |
|
|
|
|
|
|
|
|
|
console_getc(&c, console); |
|
|
|
|
return 0; |
|
|
|
|
case 10: |
|
|
|
|
case 127: |
|
|
|
|
if (buf < p) { |
|
|
|
|
usart_send_blocking(console->dev, '\010'); |
|
|
|
|
usart_send_blocking(console->dev, ' '); |
|
|
|
|
usart_send_blocking(console->dev, '\010'); |
|
|
|
|
--p; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
console_getc(&c, console); |
|
|
|
|
|
|
|
|
|
break; |
|
|
|
|
case '\r': |
|
|
|
|
if (((size_t)p - (size_t)buf) >= n) |
|
|
|
|
goto out; |
|
|
|
|
|
|
|
|
|
*p++ = '\n'; |
|
|
|
|
usart_send_blocking(console->dev, '\r'); |
|
|
|
|
console_getc(&c, console); |
|
|
|
|
goto out; |
|
|
|
|
default: |
|
|
|
|
if (((size_t)p - (size_t)buf) >= n) |
|
|
|
|
goto out; |
|
|
|
|
|
|
|
|
|
*p++ = c; |
|
|
|
|
usart_send_blocking(console->dev, c); |
|
|
|
|
console_getc(&c, console); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
out: |
|
|
|
|
*p = '\0'; |
|
|
|
|
|
|
|
|
|
return p - buf; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static ssize_t usart_read(void *cookie, char *buf, size_t n) |
|
|
|
|
{ |
|
|
|
|
struct usart_console *console = cookie; |
|
|
|
@ -217,7 +285,7 @@ static int usart_init(struct usart_console *console) |
|
|
|
|
struct usart_console *console_init(unsigned dev_id) |
|
|
|
|
{ |
|
|
|
|
cookie_io_functions_t fops = { |
|
|
|
|
.read = usart_read, |
|
|
|
|
.read = console_read, |
|
|
|
|
.write = usart_write, |
|
|
|
|
.seek = NULL, |
|
|
|
|
.close = NULL, |
|
|
|
@ -239,8 +307,7 @@ struct usart_console *console_init(unsigned dev_id) |
|
|
|
|
usart_init(console); |
|
|
|
|
|
|
|
|
|
console->fp = fopencookie(console, "r+w", fops); |
|
|
|
|
|
|
|
|
|
setbuf(console->fp, NULL); |
|
|
|
|
setvbuf(console->fp, NULL, _IONBF, 0); |
|
|
|
|
|
|
|
|
|
return console; |
|
|
|
|
} |
|
|
|
|