3
3
void uart_terminal_console_output_handle_rx_data_cb (uint8_t * buf , size_t len , void * context ) {
4
4
furi_assert (context );
5
5
UART_TerminalApp * app = context ;
6
+ FuriString * new_str = furi_string_alloc ();
7
+
8
+ if (app -> hex_mode ) {
9
+ while (len -- ) {
10
+ uint8_t byte = * (buf ++ );
11
+ if (byte == '\0' ) break ;
12
+ furi_string_cat_printf (new_str , "%02X " , byte );
13
+ }
14
+ }
15
+ else {
16
+ buf [len ] = '\0' ;
17
+ furi_string_cat_printf (new_str , "%s" , buf );
18
+ }
6
19
7
20
// If text box store gets too big, then truncate it
8
- app -> text_box_store_strlen += len ;
9
- if (app -> text_box_store_strlen >= UART_TERMINAL_TEXT_BOX_STORE_SIZE - 1 ) {
21
+ app -> text_box_store_strlen += furi_string_size ( new_str ); ;
22
+ while (app -> text_box_store_strlen >= UART_TERMINAL_TEXT_BOX_STORE_SIZE - 1 ) {
10
23
furi_string_right (app -> text_box_store , app -> text_box_store_strlen / 2 );
11
24
app -> text_box_store_strlen = furi_string_size (app -> text_box_store ) + len ;
12
25
}
13
26
14
- // Null-terminate buf and append to text box store
15
- buf [len ] = '\0' ;
16
- furi_string_cat_printf (app -> text_box_store , "%s" , buf );
27
+ furi_string_cat (app -> text_box_store , new_str );
28
+ furi_string_free (new_str );
17
29
18
30
view_dispatcher_send_custom_event (
19
31
app -> view_dispatcher , UART_TerminalEventRefreshConsoleOutput );
20
32
}
21
33
34
+ static uint8_t hex_char_to_byte (const char c ) {
35
+ if (c >= '0' && c <= '9' ) {
36
+ return c - '0' ;
37
+ }
38
+ if (c >= 'A' && c <= 'F' ) {
39
+ return c - 'A' + 10 ;
40
+ }
41
+ if (c >= 'a' && c <= 'f' ) {
42
+ return c - 'a' + 10 ;
43
+ }
44
+ return 0 ;
45
+ }
46
+
22
47
void uart_terminal_scene_console_output_on_enter (void * context ) {
23
48
UART_TerminalApp * app = context ;
24
49
25
50
TextBox * text_box = app -> text_box ;
26
51
text_box_reset (app -> text_box );
27
52
text_box_set_font (text_box , TextBoxFontText );
28
- if (app -> focus_console_start ) {
29
- text_box_set_focus (text_box , TextBoxFocusStart );
30
- } else {
31
- text_box_set_focus (text_box , TextBoxFocusEnd );
32
- }
53
+ text_box_set_focus (text_box , TextBoxFocusEnd );
33
54
34
- //Change baudrate ///////////////////////////////////////////////////////////////////////////
35
- if (0 == strncmp ("75" , app -> selected_tx_string , strlen ("75" )) && app -> BAUDRATE != 75 ) {
36
- uart_terminal_uart_free (app -> uart );
37
- app -> BAUDRATE = 75 ;
38
- app -> uart = uart_terminal_uart_init (app );
39
- }
40
- if (0 == strncmp ("110" , app -> selected_tx_string , strlen ("110" )) && app -> BAUDRATE != 110 ) {
41
- uart_terminal_uart_free (app -> uart );
42
- app -> BAUDRATE = 110 ;
43
- app -> uart = uart_terminal_uart_init (app );
44
- }
45
- if (0 == strncmp ("150" , app -> selected_tx_string , strlen ("150" )) && app -> BAUDRATE != 150 ) {
46
- uart_terminal_uart_free (app -> uart );
47
- app -> BAUDRATE = 150 ;
48
- app -> uart = uart_terminal_uart_init (app );
49
- }
50
- if (0 == strncmp ("300" , app -> selected_tx_string , strlen ("300" )) && app -> BAUDRATE != 300 ) {
51
- uart_terminal_uart_free (app -> uart );
52
- app -> BAUDRATE = 300 ;
53
- app -> uart = uart_terminal_uart_init (app );
54
- }
55
- if (0 == strncmp ("600" , app -> selected_tx_string , strlen ("600" )) && app -> BAUDRATE != 600 ) {
56
- uart_terminal_uart_free (app -> uart );
57
- app -> BAUDRATE = 600 ;
58
- app -> uart = uart_terminal_uart_init (app );
59
- }
60
- if (0 == strncmp ("1200" , app -> selected_tx_string , strlen ("1200" )) && app -> BAUDRATE != 1200 ) {
61
- uart_terminal_uart_free (app -> uart );
62
- app -> BAUDRATE = 1200 ;
63
- app -> uart = uart_terminal_uart_init (app );
64
- }
65
- if (0 == strncmp ("1800" , app -> selected_tx_string , strlen ("1800" )) && app -> BAUDRATE != 1800 ) {
66
- uart_terminal_uart_free (app -> uart );
67
- app -> BAUDRATE = 1800 ;
68
- app -> uart = uart_terminal_uart_init (app );
69
- }
70
- if (0 == strncmp ("2400" , app -> selected_tx_string , strlen ("2400" )) && app -> BAUDRATE != 2400 ) {
71
- uart_terminal_uart_free (app -> uart );
72
- app -> BAUDRATE = 2400 ;
73
- app -> uart = uart_terminal_uart_init (app );
74
- }
75
- if (0 == strncmp ("4800" , app -> selected_tx_string , strlen ("4800" )) && app -> BAUDRATE != 4800 ) {
76
- uart_terminal_uart_free (app -> uart );
77
- app -> BAUDRATE = 4800 ;
78
- app -> uart = uart_terminal_uart_init (app );
79
- }
80
- if (0 == strncmp ("7200" , app -> selected_tx_string , strlen ("7200" )) && app -> BAUDRATE != 7200 ) {
81
- uart_terminal_uart_free (app -> uart );
82
- app -> BAUDRATE = 7200 ;
83
- app -> uart = uart_terminal_uart_init (app );
84
- }
85
- if (0 == strncmp ("9600" , app -> selected_tx_string , strlen ("9600" )) && app -> BAUDRATE != 9600 ) {
86
- uart_terminal_uart_free (app -> uart );
87
- app -> BAUDRATE = 9600 ;
88
- app -> uart = uart_terminal_uart_init (app );
89
- }
90
- if (0 == strncmp ("14400" , app -> selected_tx_string , strlen ("14400" )) && app -> BAUDRATE != 14400 ) {
91
- uart_terminal_uart_free (app -> uart );
92
- app -> BAUDRATE = 14400 ;
93
- app -> uart = uart_terminal_uart_init (app );
94
- }
95
- if (0 == strncmp ("19200" , app -> selected_tx_string , strlen ("19200" )) && app -> BAUDRATE != 19200 ) {
96
- uart_terminal_uart_free (app -> uart );
97
- app -> BAUDRATE = 19200 ;
98
- app -> uart = uart_terminal_uart_init (app );
99
- }
100
- if (0 == strncmp ("38400" , app -> selected_tx_string , strlen ("38400" )) && app -> BAUDRATE != 38400 ) {
101
- uart_terminal_uart_free (app -> uart );
102
- app -> BAUDRATE = 38400 ;
103
- app -> uart = uart_terminal_uart_init (app );
104
- }
105
- if (0 == strncmp ("56000" , app -> selected_tx_string , strlen ("56000" )) && app -> BAUDRATE != 56000 ) {
106
- uart_terminal_uart_free (app -> uart );
107
- app -> BAUDRATE = 56000 ;
108
- app -> uart = uart_terminal_uart_init (app );
109
- }
110
- if (0 == strncmp ("57600" , app -> selected_tx_string , strlen ("57600" )) && app -> BAUDRATE != 57600 ) {
111
- uart_terminal_uart_free (app -> uart );
112
- app -> BAUDRATE = 57600 ;
113
- app -> uart = uart_terminal_uart_init (app );
114
- }
115
- if (0 == strncmp ("76800" , app -> selected_tx_string , strlen ("76800" )) && app -> BAUDRATE != 76800 ) {
116
- uart_terminal_uart_free (app -> uart );
117
- app -> BAUDRATE = 76800 ;
118
- app -> uart = uart_terminal_uart_init (app );
119
- }
120
- if (0 == strncmp ("115200" , app -> selected_tx_string , strlen ("115200" )) &&
121
- app -> BAUDRATE != 115200 ) {
122
- uart_terminal_uart_free (app -> uart );
123
- app -> BAUDRATE = 115200 ;
124
- app -> uart = uart_terminal_uart_init (app );
125
- }
126
- if (0 == strncmp ("128000" , app -> selected_tx_string , strlen ("128000" )) && app -> BAUDRATE != 128000 ) {
127
- uart_terminal_uart_free (app -> uart );
128
- app -> BAUDRATE = 128000 ;
129
- app -> uart = uart_terminal_uart_init (app );
130
- }
131
- if (0 == strncmp ("230400" , app -> selected_tx_string , strlen ("230400" )) &&
132
- app -> BAUDRATE != 230400 ) {
133
- uart_terminal_uart_free (app -> uart );
134
- app -> BAUDRATE = 230400 ;
135
- app -> uart = uart_terminal_uart_init (app );
136
- }
137
- if (0 == strncmp ("250000" , app -> selected_tx_string , strlen ("250000" )) && app -> BAUDRATE != 250000 ) {
138
- uart_terminal_uart_free (app -> uart );
139
- app -> BAUDRATE = 250000 ;
140
- app -> uart = uart_terminal_uart_init (app );
141
- }
142
- if (0 == strncmp ("256000" , app -> selected_tx_string , strlen ("256000" )) && app -> BAUDRATE != 256000 ) {
143
- uart_terminal_uart_free (app -> uart );
144
- app -> BAUDRATE = 256000 ;
145
- app -> uart = uart_terminal_uart_init (app );
55
+ bool need_reinit = false;
56
+
57
+ //Change baudrate
58
+ if (app -> BAUDRATE != app -> NEW_BAUDRATE && app -> NEW_BAUDRATE ) {
59
+ need_reinit = true;
146
60
}
147
- if (0 == strncmp ("460800" , app -> selected_tx_string , strlen ("460800" )) &&
148
- app -> BAUDRATE != 460800 ) {
149
- uart_terminal_uart_free (app -> uart );
150
- app -> BAUDRATE = 460800 ;
151
- app -> uart = uart_terminal_uart_init (app );
61
+
62
+ //Change UART port
63
+ if (app -> uart_ch != app -> new_uart_ch ) {
64
+ need_reinit = true;
152
65
}
153
- if ( 0 == strncmp ( "921600" , app -> selected_tx_string , strlen ( "921600" )) &&
154
- app -> BAUDRATE != 921600 ) {
66
+
67
+ if ( need_reinit ) {
155
68
uart_terminal_uart_free (app -> uart );
156
- app -> BAUDRATE = 921600 ;
69
+ app -> BAUDRATE = app -> NEW_BAUDRATE ;
70
+ app -> uart_ch = app -> new_uart_ch ;
157
71
app -> uart = uart_terminal_uart_init (app );
158
72
}
159
- /////////////////////////////////////////////////////////////////////////////////////////////////////
160
73
161
74
if (app -> is_command ) {
162
75
furi_string_reset (app -> text_box_store );
163
76
app -> text_box_store_strlen = 0 ;
164
-
165
- if (0 == strncmp ("help" , app -> selected_tx_string , strlen ("help" ))) {
166
- const char * help_msg =
167
- "UART terminal for Flipper\n\nI'm in github: cool4uma\n\nThis app is a modified\nWiFi Marauder companion,\nThanks 0xchocolate(github)\nfor great code and app.\n\n" ;
168
- furi_string_cat_str (app -> text_box_store , help_msg );
169
- app -> text_box_store_strlen += strlen (help_msg );
170
- }
171
-
172
- if (app -> show_stopscan_tip ) {
173
- const char * help_msg = "Press BACK to return\n" ;
174
- furi_string_cat_str (app -> text_box_store , help_msg );
175
- app -> text_box_store_strlen += strlen (help_msg );
176
- }
177
77
}
178
78
179
79
// Set starting text - for "View Log", this will just be what was already in the text box store
@@ -186,16 +86,41 @@ void uart_terminal_scene_console_output_on_enter(void* context) {
186
86
uart_terminal_uart_set_handle_rx_data_cb (
187
87
app -> uart , uart_terminal_console_output_handle_rx_data_cb ); // setup callback for rx thread
188
88
189
- // Send command with CR+LF or newline '\n'
190
- if (app -> is_command && app -> selected_tx_string ) {
191
- if (app -> TERMINAL_MODE == 1 ){
192
- uart_terminal_uart_tx (
193
- (uint8_t * )(app -> selected_tx_string ), strlen (app -> selected_tx_string ));
194
- uart_terminal_uart_tx ((uint8_t * )("\r\n" ), 2 );
195
- } else {
196
- uart_terminal_uart_tx (
197
- (uint8_t * )(app -> selected_tx_string ), strlen (app -> selected_tx_string ));
198
- uart_terminal_uart_tx ((uint8_t * )("\n" ), 1 );
89
+ uint8_t uart_ch = app -> uart_ch ;
90
+
91
+ if (app -> hex_mode ) {
92
+ // Send binary packet
93
+ if (app -> selected_tx_string ) {
94
+ const char * str = app -> selected_tx_string ;
95
+ uint8_t digit_num = 0 ;
96
+ uint8_t byte = 0 ;
97
+ while (* str ) {
98
+ byte |= (hex_char_to_byte (* str ) << ((1 - digit_num ) * 4 ));
99
+
100
+ if (++ digit_num == 2 ) {
101
+ uart_terminal_uart_tx (uart_ch , & byte , 1 );
102
+ digit_num = 0 ;
103
+ byte = 0 ;
104
+ }
105
+ str ++ ;
106
+ }
107
+
108
+ if (digit_num ) {
109
+ uart_terminal_uart_tx (uart_ch , & byte , 1 );
110
+ }
111
+ }
112
+ } else {
113
+ // Send command with CR+LF or newline '\n'
114
+ if (app -> is_command && app -> selected_tx_string ) {
115
+ if (app -> TERMINAL_MODE == 1 ){
116
+ uart_terminal_uart_tx (uart_ch ,
117
+ (uint8_t * )(app -> selected_tx_string ), strlen (app -> selected_tx_string ));
118
+ uart_terminal_uart_tx (uart_ch , (uint8_t * )("\r\n" ), 2 );
119
+ } else {
120
+ uart_terminal_uart_tx (uart_ch ,
121
+ (uint8_t * )(app -> selected_tx_string ), strlen (app -> selected_tx_string ));
122
+ uart_terminal_uart_tx (uart_ch , (uint8_t * )("\n" ), 1 );
123
+ }
199
124
}
200
125
}
201
126
}
0 commit comments