- ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
- rxbuf[rx_bytes] = UDR0;
- if (rx_bytes + 1 < BUFSIZE) // ignore overruns
- rx_bytes++;
- last_rx = get_clock();
+ uint8_t rx_byte = UDR0;
+ clock_t now = get_clock();
+
+ if (transmitting) // how did we get here? discard it
+ goto out;
+
+ if (buf_len && buffer[0] != mb_unit_id) // not for us
+ goto out;
+
+ if (buf_len == BUFSIZE) { // overrun - discard the packet
+ buffer[0] = 0xFF;
+ buf_len = 1;
+ goto out;
+ }
+
+ if (now - last_rx >= REQ_TIMEOUT) { // new packet; start over
+ buf_len = 0;