xref: /btstack/platform/posix/btstack_stdin_posix.c (revision c28332930a36e786ece9fe096e3cd616d9dd92c9)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "btstack_stdin_posix.c"
39 
40 #include <errno.h>
41 #include <stdio.h>
42 #include <unistd.h>
43 
44 #ifdef ENABLE_BTSTACK_STDIN_LOGGING
45 #include "btstack_debug.h"
46 #endif
47 #include "btstack_defines.h"
48 #include "btstack_run_loop.h"
49 #include <stdlib.h>
50 
51 #include "btstack_stdin.h"
52 #include <termios.h>
53 
54 static btstack_data_source_t stdin_source;
55 static int activated = 0;
56 static void (*stdin_handler)(char c);
57 
58 // read single byte after data source callback was triggered
59 static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){
60     UNUSED(ds);
61     UNUSED(callback_type);
62 
63     char data;
64     int result = read(stdin_source.source.fd, &data, 1);
65     if (result < 1) return;
66     if (stdin_handler == NULL) return;
67 
68 #ifdef ENABLE_BTSTACK_STDIN_LOGGING
69     log_info("stdin: %c", data);
70 #endif
71 
72     (*stdin_handler)(data);
73 }
74 
75 void btstack_stdin_setup(void (*handler)(char c)){
76 
77     if (activated) return;
78 
79     // disable line buffering
80     struct termios term = {0};
81     if (tcgetattr(0, &term) < 0)
82             perror("tcsetattr()");
83     term.c_lflag &= ~ICANON;
84     term.c_lflag &= ~ECHO;
85     if (tcsetattr(0, TCSANOW, &term) < 0) {
86         perror("tcsetattr ICANON");
87     }
88 
89     stdin_handler = handler;
90 
91     btstack_run_loop_set_data_source_fd(&stdin_source, 0); // stdin
92 
93     btstack_run_loop_enable_data_source_callbacks(&stdin_source, DATA_SOURCE_CALLBACK_READ);
94     btstack_run_loop_set_data_source_handler(&stdin_source, stdin_process);
95     btstack_run_loop_add_data_source(&stdin_source);
96 
97     activated = 1;
98 }
99 
100 void btstack_stdin_reset(void){
101     if (!activated) return;
102     activated = 0;
103     stdin_handler = NULL;
104 
105     btstack_run_loop_remove_data_source(&stdin_source);
106 
107     struct termios term = {0};
108     if (tcgetattr(0, &term) < 0){
109         perror("tcsetattr()");
110     }
111     term.c_lflag |= ICANON;
112     term.c_lflag |= ECHO;
113     if (tcsetattr(0, TCSANOW, &term) < 0){
114         perror("tcsetattr ICANON");
115     }
116 }
117 
118