librfn
An ad-hoc utility library
console_posix.c
Go to the documentation of this file.
1 /*
2  * console_libopencm3.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * This file was derived from libopencm3's usart_irq_printf.c example.
7  *
8  * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>,
9  * Copyright (C) 2011 Piotr Esden-Tempski <piotr@esden.net>
10  * Copyright (C) 2014 Daniel Thompson <daniel@redfelineninja.org.uk>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License as published
14  * by the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  */
17 
18 #include <librfn/console.h>
19 
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 static pthread_t injector_thread;
25 
26 static void *injector(void *p)
27 {
28  console_t *c = p;
29  int d;
30 
31  while ((d = getchar()) != -1)
32  console_putchar(c, d);
33 
34  exit(0);
35  return NULL;
36 }
37 
38 static pt_state_t console_exit(console_t *c)
39 {
40  exit(0);
41 }
42 static const console_cmd_t cmd_exit =
43  CONSOLE_CMD_VAR_INIT("exit", console_exit);
44 
46 {
47  int res = pthread_create(&injector_thread, NULL, injector, c);
48  if (0 != res) {
49  perror("Cannot start injector");
50  }
51 
52  console_register(&cmd_exit);
53 }
struct charlie c
Console descriptor.
Definition: console.h:66
void console_hwinit(console_t *c)
Platform dependant function that will be called during console_init().
Definition: console_posix.c:45
void console_putchar(console_t *c, char d)
Asynchronously send a character to the command processor.
Definition: console.c:177
#define CONSOLE_CMD_VAR_INIT(n, f)
Definition: console.h:52
Console command descriptor.
Definition: console.h:47
int console_register(const console_cmd_t *cmd)
Register a new command.
Definition: console.c:151
pt_state_t
Definition: protothreads.h:80