HACK: add console_init_fd

wip/virtual-tbm
Merlijn Wajer 5 years ago
parent ff82126348
commit fe3b178d3c
  1. 2
      include/console.h
  2. 28
      source/platform/usart.c

@ -9,10 +9,12 @@
#endif
struct console {
FILE *read_fp;
FILE *fp;
};
struct console *console_init(unsigned dev_id);
struct console *console_init_fd(unsigned dev_id, FILE* rfd, FILE* wfd);
int console_peek(char *c, struct console *console, int block);
int console_getc(char *c, struct console *console);
int console_getline(struct console *console, char *buf, size_t n);

@ -94,6 +94,14 @@ int console_peek(char *c, struct console *console_, int block)
int console_getc(char *c, struct console *console_)
{
*c = fgetc(console_->read_fp);
if (*c == EOF) {
return -1;
}
fprintf(stderr, "fgetc on %d gives %x\n", fileno(console_->read_fp), *c);
return 0;
#if 0
struct usart_console *console = container_of(console_,
struct usart_console, console);
@ -106,6 +114,7 @@ int console_getc(char *c, struct console *console_)
}
return 0;
#endif
}
int console_getline(struct console *console_, char *buf, size_t n)
@ -159,6 +168,8 @@ int console_getline(struct console *console_, char *buf, size_t n)
ssize_t console_read(struct console *console, char *buf, size_t n)
{
return fread(buf, n, 1, console->read_fp);
#if 0
char *p = buf;
char c;
@ -178,6 +189,7 @@ ssize_t console_read(struct console *console, char *buf, size_t n)
}
return p - buf;
#endif
}
static ssize_t usart_write(void *cookie, const char *buf, size_t n)
@ -236,14 +248,28 @@ static int usart_init(struct usart_console *console)
return 0;
}
struct console *console_init_fd(unsigned dev_id, FILE* rfd, FILE* wfd) {
struct usart_console *console;
console = usart + dev_id;
console->console.read_fp = rfd;
console->console.fp = wfd;
setvbuf(console->console.fp, NULL, _IONBF, 0);
return &console->console;
}
struct console *console_init(unsigned dev_id)
{
#if 0
cookie_io_functions_t fops = {
.read = NULL,
.write = usart_write,
.seek = NULL,
.close = NULL,
};
#endif
struct usart_console *console;
console = usart + dev_id;
@ -261,8 +287,10 @@ struct console *console_init(unsigned dev_id)
usart_init(console);
#if 0
console->console.fp = fopencookie(console, "w", fops);
setvbuf(console->console.fp, NULL, _IONBF, 0);
#endif
return &console->console;
}

Loading…
Cancel
Save