Skip to content

Instantly share code, notes, and snippets.

@benpicco
Created November 6, 2013 21:32
Show Gist options
  • Save benpicco/7344469 to your computer and use it in GitHub Desktop.
Save benpicco/7344469 to your computer and use it in GitHub Desktop.
diff --cc native/drivers/native-uart0.c
index 7e8489b,38720fd..0000000
--- a/native/drivers/native-uart0.c
+++ b/native/drivers/native-uart0.c
@@@ -8,15 -8,6 +8,18 @@@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
++<<<<<<< HEAD
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <sys/un.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
++=======
++>>>>>>> native_fixes
#include <sys/select.h>
@@@ -24,17 -15,12 +27,26 @@@
#include "debug.h"
#include "board_uart0.h"
++<<<<<<< HEAD
+int _native_uart_in;
+int _native_uart_out;
+int _native_uart_sock;
+int _native_uart_conn;
+int _native_null_in_pipe[2];
+int _native_null_out_file;
+
+fd_set _native_uart_rfds;
+
+/* uart API */
+
++=======
+ #include "native_internal.h"
+
+ static int _native_uart_in;
+
+ fd_set _native_uart_rfds;
+
++>>>>>>> native_fixes
int uart0_puts(char *astring, int length)
{
int nwritten, offset;
@@@ -45,7 -31,7 +57,11 @@@
while (
(length - offset > 0) && (
(nwritten = write(
++<<<<<<< HEAD
+ _native_uart_out,
++=======
+ STDOUT_FILENO,
++>>>>>>> native_fixes
astring+offset,
length-offset)
) > 0)
@@@ -152,26 -54,23 +168,43 @@@ void handle_uart_in(
char buf[42];
int nread;
++<<<<<<< HEAD
+ DEBUG("handle_uart_in\n");
++=======
+ if (!FD_ISSET(_native_uart_in, &_native_rfds)) {
+ DEBUG("_native_handle_uart0_input - nothing to do\n");
+ return;
+ }
+ DEBUG("_native_handle_uart0_input\n");
++>>>>>>> native_fixes
nread = read(_native_uart_in, buf, sizeof(buf));
if (nread == -1) {
- err(1, "_native_handle_uart0_input(): read()");
+ err(1, "handle_uart_in(): read()");
}
else if (nread == 0) {
++<<<<<<< HEAD
+ /* end of file / socket closed */
+ if (_native_uart_conn != 0) {
+ if (dup2(_native_null_out_file, _native_uart_out) == -1) {
+ err(EXIT_FAILURE, "handle_uart_in: dup2(STDOUT_FILENO)");
+ }
+ if (dup2(_native_null_in_pipe[0], _native_uart_in) == -1) {
+ err(EXIT_FAILURE, "handle_uart_in: dup2(STDIN_FILENO)");
+ }
+ _native_uart_conn = 0;
+ }
+ else {
+ errx(EXIT_FAILURE, "handle_uart_in: unhandled situation!!");
+ }
++=======
+ /* XXX:
+ * preliminary resolution for this situation, will be coped
+ * with properly in #161 */
+ close(_native_uart_in);
+ _native_uart_in = -1;
+ printf("stdin closed");
++>>>>>>> native_fixes
}
for(int pos = 0; pos < nread; pos++) {
uart0_handle_incoming(buf[pos]);
@@@ -224,158 -83,15 +257,167 @@@ void _native_handle_uart0_input(
int _native_set_uart_fds(void)
{
DEBUG("_native_set_uart_fds");
++<<<<<<< HEAD
+ FD_SET(_native_uart_in, &_native_rfds);
+ if (_native_uart_sock == -1) {
+ return (_native_uart_in);
+ }
+ else {
+ FD_SET(_native_uart_sock, &_native_rfds);
+ return ((_native_uart_in > _native_uart_sock) ? _native_uart_in : _native_uart_sock);
+ }
+}
+
+void _native_null_io()
+{
+#ifdef NATIVE_LOGOUT
+ char logname[255];
+ snprintf(logname, sizeof(logname), "/tmp/riot.log.%d", getpid());
+ if ((_native_null_out_file = creat(logname, 0666)) == -1) {
+ err(EXIT_FAILURE, "daemonize: open");
+ }
+#else
+ if ((_native_null_out_file = open("/dev/null", O_WRONLY)) == -1) {
+ err(EXIT_FAILURE, "daemonize: open");
+ }
+#endif
+ if (pipe(_native_null_in_pipe) == -1) {
+ err(1, "daemonize(): pipe()");
+ }
+
+ if (dup2(_native_null_out_file, STDOUT_FILENO) == -1) {
+ err(EXIT_FAILURE, "daemonize: dup2(STDOUT_FILENO)");
+ }
+
+ if (dup2(_native_null_in_pipe[0], STDIN_FILENO) == -1) {
+ err(EXIT_FAILURE, "daemonize: dup2(STDIN_FILENO)");
+ }
+}
+
+void _native_log_stderr(char *stderrtype)
+{
+ char stderr_logname[255];
+ int stderr_outfile;
+
+ if (strcmp(stderrtype, "stdio") == 0) {
+ return;
+ }
+ else if (strcmp(stderrtype, "file") != 0) {
+ errx(EXIT_FAILURE, "_native_log_stderr: unknown log type");
+ }
+
+ snprintf(stderr_logname, sizeof(stderr_logname), "/tmp/riot.stderr.%d", getpid());
+ if ((stderr_outfile = creat(stderr_logname, 0666)) == -1) {
+ err(EXIT_FAILURE, "daemonize: open");
+ }
+ if (dup2(stderr_outfile, STDERR_FILENO) == -1) {
+ err(EXIT_FAILURE, "daemonize: dup2(STDERR_FILENO)");
+ }
+}
+
+void _native_init_uart0_stdio(char *stdiotype)
+{
+ if (strcmp(stdiotype, "tcp") == 0) {
+ _native_null_io();
+ _native_uart_sock = init_tcp_socket();
+ }
+ else if (strcmp(stdiotype, "unix") == 0) {
+ _native_null_io();
+ _native_uart_sock = init_unix_socket();
+ }
+ else if (strcmp(stdiotype, "stdio") == 0) {
+ _native_uart_sock = -1;
+ }
+ else {
+ errx(EXIT_FAILURE, "_native_init_uart0: unknown stdio type");
+ }
++=======
+ if (_native_uart_in != -1) {
+ FD_SET(_native_uart_in, &_native_rfds);
+ }
+ return _native_uart_in;
++>>>>>>> native_fixes
}
-void _native_init_uart0()
+void _native_init_uart0(char *stdiotype, char *stderrtype)
{
- _native_uart_out = STDOUT_FILENO;
_native_uart_in = STDIN_FILENO;
+ _native_init_uart0_stdio(stdiotype);
+ _native_log_stderr(stderrtype);
+
puts("RIOT native uart0 initialized.");
}
++<<<<<<< HEAD
+
+int putchar(int c) {
+ write(_native_uart_out, &c, 1);
+ return 0;
+}
+
+int puts(const char *s)
+{
+ int r;
+ r = uart0_puts((char*)s, strlen(s));
+ putchar('\n');
+ return r;
+}
+
+char *make_message(const char *format, va_list argp)
+{
+ int n;
+ int size = 100;
+ char *message, *temp;
+
+ if ((message = malloc(size)) == NULL)
+ return NULL;
+
+ while (1) {
+ n = vsnprintf(message, size, format, argp);
+ if (n < 0)
+ return NULL;
+ if (n < size)
+ return message;
+ size = n + 1;
+ if ((temp = realloc(message, size)) == NULL) {
+ free(message);
+ return NULL;
+ } else {
+ message = temp;
+ }
+ }
+}
+
+
+int printf(const char *format, ...)
+{
+ int r;
+ va_list argp;
+ char *m;
+
+ va_start(argp, format);
+ if ((m = make_message(format, argp)) == NULL) {
+ err(EXIT_FAILURE, "malloc");
+ }
+ r = uart0_puts(m, strlen(m));
+ va_end(argp);
+ free(m);
+
+ return r;
+}
+
+int vprintf(const char *format, va_list argp)
+{
+ int r;
+ char *m;
+
+ if ((m = make_message(format, argp)) == NULL) {
+ err(EXIT_FAILURE, "malloc");
+ }
+ r = uart0_puts(m, strlen(m));
+ free(m);
+
+ return r;
+}
++=======
++>>>>>>> native_fixes
diff --cc cpu/native/startup.c
index 012697c,2117cde..0000000
--- a/cpu/native/startup.c
+++ b/cpu/native/startup.c
@@@ -23,107 -23,27 +23,122 @@@
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
#include <err.h>
-
+#include <string.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
- #include <kernel_internal.h>
- #include <cpu.h>
+ #include "kernel_internal.h"
+ #include "cpu.h"
++<<<<<<< HEAD
+#include "board_internal.h"
+#include "tap.h"
+
+extern void board_init(void);
+extern void native_cpu_init(void);
+extern void native_interrupt_init(void);
+char *argv0;
+int _native_null_in_pipe[2];
+int _native_null_out_file;
+
+void daemonize()
+{
+ pid_t pid;
+
+ /* fork */
+ if ((pid = fork()) == -1) {
+ err(EXIT_FAILURE, "daemonize: fork");
+ }
+
+ if (pid > 0) {
+ printf("RIOT pid: %d\n", pid);
+ exit(EXIT_SUCCESS);
+ }
+
+}
+
+void usage_exit()
+{
+ printf("usage: %s", argv0);
+#ifdef MODULE_NATIVENET
+ printf(" <tap interface>");
+#endif
+ printf(" [-t|-u]");
+ printf(" [-d] [-e]\n");
+
+ printf("\nOptions:\n\
+-d daeomize\n");
+ printf("\
+-u redirect stdio to unix socket\n\
+-t redirect stdio to tcp socket\n\
+-e redirect stderr file\n");
+ printf("\n\
+The order of command line arguments matters.\n");
+ exit(EXIT_FAILURE);
+}
+
+__attribute__((constructor)) static void startup(int argc, char **argv)
+{
+ int argp = 1;
+#ifdef MODULE_UART0
+ extern int _native_uart_out;
+ char *stdiotype, *stderrtype;
+
+ stdiotype = "stdio";
+ stderrtype = "stdio";
+
+ _native_uart_out = STDOUT_FILENO;
+#endif
+ argv0 = argv[0];
+
+#ifdef MODULE_NATIVENET
+ if (argc < 2) {
+ usage_exit();
++=======
+ #include "native_internal.h"
+ #include "tap.h"
+
+ __attribute__((constructor)) static void startup(int argc, char **argv)
+ {
+ #ifdef MODULE_NATIVENET
+ if (argc < 2) {
+ printf("usage: %s <tap interface>\n", argv[0]);
+ exit(EXIT_FAILURE);
++>>>>>>> native_fixes
+ }
+ argp++;
+#endif
+#ifdef MODULE_UART0
+ for (; argp < argc; argp++) {
+ char *arg = argv[argp];
+ if (strcmp("-d", arg) == 0) {
+ daemonize();
+ }
+ else if (strcmp("-e", arg) == 0) {
+ stderrtype = "file";
+ }
+ else if (strcmp("-t", arg) == 0) {
+ stdiotype = "tcp";
+ }
+ else if (strcmp("-u", arg) == 0) {
+ stdiotype = "unix";
+ }
+ else {
+ usage_exit();
+ }
}
-#else /* args unused here */
- (void) argc;
- (void) argv;
#endif
+
++<<<<<<< HEAD
+extern ssize_t (*real_read)(int fd, void *buf, size_t count);
+extern ssize_t (*real_write)(int fd, const void *buf, size_t count);
+
++=======
++>>>>>>> native_fixes
/* get system read/write */
*(void **)(&real_read) = dlsym(RTLD_NEXT, "read");
*(void **)(&real_write) = dlsym(RTLD_NEXT, "write");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment