xref: /btstack/platform/embedded/btstack_stdin_embedded.c (revision a06bcae0f7f63d9d69b2f846d04300ea7058ea66)
1 /*
2  * Copyright (C) 2017 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 MATTHIAS
24  * RINGWALD 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_embedded.c"
39 
40 /*
41  *  btstack_stdin_embedded.c
42  *
43  *  Common code to provide console input using an IRQ-driven console UART in hal_stdin.h
44  *
45  */
46 
47 #include "hal_stdin.h"
48 
49 #include "btstack_stdin.h"
50 #include "btstack_run_loop.h"
51 #include "btstack_run_loop_embedded.h"
52 
53 volatile int stdin_character_received;
54 volatile char stdin_character;
55 static void (*stdin_handler)(char c);
56 static btstack_data_source_t stdin_data_source;
57 
58 static void btstack_stdin_handler(char c){
59 	stdin_character = c;
60 	stdin_character_received = 1;
61 	btstack_run_loop_embedded_trigger();
62 }
63 
64 static void btstack_stdin_process(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type){
65 	if (!stdin_character_received) return;
66 	if (stdin_handler){
67 		(*stdin_handler)(stdin_character);
68 	}
69 	stdin_character_received = 0;
70 }
71 
72 void btstack_stdin_setup(void (*handler)(char c)){
73 	// set handler
74 	stdin_handler = handler;
75 
76 	// set up polling data_source
77 	btstack_run_loop_set_data_source_handler(&stdin_data_source, &btstack_stdin_process);
78 	btstack_run_loop_enable_data_source_callbacks(&stdin_data_source, DATA_SOURCE_CALLBACK_POLL);
79 	btstack_run_loop_add_data_source(&stdin_data_source);
80 
81 	// start receiving
82 	hal_stdin_setup(&btstack_stdin_handler);
83 }
84