xref: /btstack/platform/embedded/btstack_stdin_embedded.c (revision 80e33422a96c028b3a9c308fc4b9b874712dafb4)
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 "btstack_config.h"
48 
49 #include "btstack_stdin.h"
50 #include "btstack_run_loop.h"
51 #include "btstack_run_loop_embedded.h"
52 
53 #ifdef ENABLE_SEGGER_RTT
54 #include "SEGGER_RTT.h"
55 #else
56 #include "hal_stdin.h"
57 #endif
58 
59 static void (*stdin_handler)(char c);
60 static btstack_data_source_t stdin_data_source;
61 
62 #ifndef ENABLE_SEGGER_RTT
63 
64 volatile int stdin_character_received;
65 volatile char stdin_character;
66 
67 static void btstack_stdin_handler(char c){
68 	stdin_character = c;
69 	stdin_character_received = 1;
70 	btstack_run_loop_embedded_trigger();
71 }
72 
73 static void btstack_stdin_process(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type){
74 	if (!stdin_character_received) return;
75 	(*stdin_handler)(stdin_character);
76 	stdin_character_received = 0;
77 }
78 
79 #else
80 
81 static void btstack_stdin_process(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type){
82 	if (SEGGER_RTT_HasKey()){
83 		int stdin_character = SEGGER_RTT_GetKey();
84 		(*stdin_handler)((uint8_t)stdin_character);
85 	}
86 }
87 
88 #endif
89 
90 void btstack_stdin_setup(void (*handler)(char c)){
91 	// set handler
92 	stdin_handler = handler;
93 
94 	// set up polling data_source
95 	btstack_run_loop_set_data_source_handler(&stdin_data_source, &btstack_stdin_process);
96 	btstack_run_loop_enable_data_source_callbacks(&stdin_data_source, DATA_SOURCE_CALLBACK_POLL);
97 	btstack_run_loop_add_data_source(&stdin_data_source);
98 
99 #ifndef ENABLE_SEGGER_RTT
100 	// start receiving via hal_stdin.h
101 	hal_stdin_setup(&btstack_stdin_handler);
102 #endif
103 }
104