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 PRINTER_PACKET_SIZE 4
#define PRINTER_PACKET_SIZE 6
/* Keycode from include/linux/input.h */
#define KEY_RESERVED 0
@ -214,7 +214,8 @@ void uart_putc(char c)
ISR(USART_RXC_vect)
{
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;
}
}
@ -229,11 +230,20 @@ void uart_forward_packets()
else
size = ring_buffer_rd - ring_buffer_wr;
if (size > PRINTER_PACKET_SIZE) {
uart_putc(0xFF);
uart_putc(0xAA);
if (size >= PRINTER_PACKET_SIZE) {
/* check sync */
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) {
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++;
}
}
}