keyboard: Fix ring buffer for printer packet forwarding

This commit is contained in:
Markus Koch 2020-01-03 16:46:14 +01:00
parent d588eeee42
commit b8a5ae2fcd

View File

@ -14,7 +14,7 @@
#define BAUDRATE 19200 #define BAUDRATE 19200
#define PRINTER_PACKET_SIZE 4 #define PRINTER_PACKET_SIZE 6
/* Keycode from include/linux/input.h */ /* Keycode from include/linux/input.h */
#define KEY_RESERVED 0 #define KEY_RESERVED 0
@ -214,7 +214,8 @@ void uart_putc(char c)
ISR(USART_RXC_vect) ISR(USART_RXC_vect)
{ {
if (UCSRA & (1 << RXC)) { if (UCSRA & (1 << RXC)) {
ring_buffer[ring_buffer_wr++] = UDR;// WARNING: No overflow protection ring_buffer[ring_buffer_wr] = UDR;
ring_buffer_wr++; // WARNING: No overflow protection
// Will auto wrap at 256; print_buffer_wr = print_buffer_wr % PRINT_BUFFER_SIZE; // Will auto wrap at 256; print_buffer_wr = print_buffer_wr % PRINT_BUFFER_SIZE;
} }
} }
@ -229,11 +230,20 @@ void uart_forward_packets()
else else
size = ring_buffer_rd - ring_buffer_wr; size = ring_buffer_rd - ring_buffer_wr;
if (size > PRINTER_PACKET_SIZE) { if (size >= PRINTER_PACKET_SIZE) {
uart_putc(0xFF); /* check sync */
uart_putc(0xAA); if (ring_buffer[ring_buffer_rd] != ((char)0xFF)) {
ring_buffer_rd++; /* Drop wrong sync header */
return;
}
if (ring_buffer[ring_buffer_rd + 1] != ((char)0xAA)) {
ring_buffer_rd += 1; /* Drop wrong app header */
return;
}
for (i = 0; i < PRINTER_PACKET_SIZE; ++i) { for (i = 0; i < PRINTER_PACKET_SIZE; ++i) {
uart_putc(ring_buffer[ring_buffer_rd++]); // Will auto wrap at 256 uart_putc(ring_buffer[ring_buffer_rd]); // Will auto wrap at 256
ring_buffer_rd++;
} }
} }
} }