1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/boards/native/drivers/native-uart0.c
Ludwig Ortmann 7b9d199ec8 make system calls safer/clean up headers
wrap some libc functions that do system calls (terminal output)
wrap read/write with syscall guard
define real_read/write (next dynamic linker find for read/write)
guard system calls in remaining code
introduce native_internhal.h
throw out some debug statements that break things
clean up includes a bit
declare board_init in native_internhal.h
add -ldl to LINKFLAGS for cpu/syscalls
2013-11-13 00:01:42 +01:00

98 lines
2.0 KiB
C

/*
* native uart0 implementation
*/
#include <err.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/select.h>
#include "cpu.h"
#include "debug.h"
#include "board_uart0.h"
#include "native_internal.h"
static int _native_uart_in;
fd_set _native_uart_rfds;
int uart0_puts(char *astring, int length)
{
int nwritten, offset;
nwritten = 0;
offset = 0;
while (
(length - offset > 0) && (
(nwritten = write(
STDOUT_FILENO,
astring+offset,
length-offset)
) > 0)
) {
offset += nwritten;
}
if (nwritten == -1) {
err(EXIT_FAILURE, "uart0_puts: write");
}
else if ((length > 0) && (nwritten == 0)) {
/* XXX: handle properly */
errx(EXIT_FAILURE, "uart0_puts: Could not write to stdout. I don't know what to do now.");
}
return length;
}
void _native_handle_uart0_input()
{
char buf[42];
int nread;
if (!FD_ISSET(_native_uart_in, &_native_rfds)) {
DEBUG("_native_handle_uart0_input - nothing to do\n");
return;
}
DEBUG("_native_handle_uart0_input\n");
nread = read(_native_uart_in, buf, sizeof(buf));
if (nread == -1) {
err(1, "_native_handle_uart0_input(): read()");
}
else if (nread == 0) {
/* XXX:
* preliminary resolution for this situation, will be coped
* with properly in #161 */
close(_native_uart_in);
_native_uart_in = -1;
printf("stdin closed");
}
for(int pos = 0; pos < nread; pos++) {
uart0_handle_incoming(buf[pos]);
}
uart0_notify_thread();
thread_yield();
}
int _native_set_uart_fds(void)
{
DEBUG("_native_set_uart_fds");
if (_native_uart_in != -1) {
FD_SET(_native_uart_in, &_native_rfds);
}
return _native_uart_in;
}
void _native_init_uart0()
{
_native_uart_in = STDIN_FILENO;
puts("RIOT native uart0 initialized.");
}