+void *hostsim_uart_stdin(void *arg)
+{
+ pthread_t thread = (pthread_t)arg;
+ int n;
+ char c;
+
+ /* read on old stdin and put it in pipe */
+ while (1) {
+ n = read(old_stdin, &c, 1);
+ if (n <= 0)
+ break;
+
+ pthread_mutex_lock(&mut);
+ g_msg.type = UART_RCV;
+ g_msg.c = c;
+ pthread_kill(thread, SIGUSR1);
+
+ write(stdin_pipe[1], &c, 1);
+ }
+ pthread_exit(NULL);
+ return NULL;
+}
+
+void *hostsim_uart_stdout(void *arg)
+{
+ pthread_t thread = (pthread_t)arg;
+ int n;
+ char c;
+
+ /* read on our pipe, and forward it to the old stdout */
+ while (1) {
+ n = read(stdout_pipe[0], &c, 1);
+ if (n <= 0)
+ break;
+
+ pthread_mutex_lock(&mut);
+ g_msg.type = UART_SND;
+ g_msg.c = c;
+ pthread_kill(thread, SIGUSR1);
+
+ write(old_stdout, &c, 1);
+ }
+ pthread_exit(NULL);
+ return NULL;
+}
+
+int hostsim_uart_init(void)
+{
+ struct termios term;
+
+ tcgetattr(0, &oldterm);
+ memcpy(&term, &oldterm, sizeof(term));
+ term.c_lflag &= ~(ICANON | ECHO | ISIG);
+ tcsetattr(0, TCSANOW, &term);
+
+ /* duplicate stdin */
+ old_stdin = dup(0);
+ if (old_stdin < 0)
+ return 1;
+
+ /* duplicate stdout */
+ old_stdout = dup(1);
+ if (old_stdout < 0)
+ return -1;
+
+ /* create 2 pipes */
+ if (pipe(stdin_pipe) < 0)
+ return -1;
+ if (pipe(stdout_pipe) < 0)
+ return -1;
+
+ /* replace file desc 0 (stdin) by our pipe */
+ if (dup2(stdin_pipe[0], 0) < 0)
+ return -1;
+ close(stdin_pipe[0]);
+
+ /* replace file desc 1 (stdout) by our pipe */
+ if (dup2(stdout_pipe[1], 1) < 0)
+ return -1;
+ close(stdout_pipe[1]);
+ setbuf(stdin, NULL);
+ setbuf(stdout, NULL);
+
+ return 0;
+}
+