- if (dma_done & AO_DMA_DONE) {
- if (!(rx_packet.status & PKT_APPEND_STATUS_1_CRC_OK)) {
- printf ("bad crc\n"); flush();
- return AO_DMA_ABORTED;
- }
- if (rx_packet.packet.len == AO_PACKET_SYN) {
- rx_seq = rx_packet.packet.seq;
- tx_packet.seq = rx_packet.packet.ack;
- tx_packet.ack = rx_seq;
- } else if (rx_packet.packet.len) {
- if (rx_packet.packet.seq == rx_seq + 1 && rx_used == rx_len)
- {
- printf ("rx len %3d seq %3d ack %3d\n",
- rx_packet.packet.len,
- rx_packet.packet.seq,
- rx_packet.packet.ack);
- flush();
- memcpy(rx_data, rx_packet.packet.d, rx_packet.packet.len);
- rx_used = 0;
- rx_len = rx_packet.packet.len;
- rx_seq = rx_packet.packet.seq;
- tx_packet.ack = rx_seq;
- ao_wakeup(&rx_data);
- }
- }
- if (rx_packet.packet.ack == tx_packet.seq) {
- tx_packet.len = 0;
- ao_wakeup(&tx_packet);
+ /* Check to see if we got a valid packet */
+ if (!dma_done)
+ return 0;
+ if (!(ao_rx_packet.status & PKT_APPEND_STATUS_1_CRC_OK))
+ return 0;
+
+ /* SYN packets carry no data */
+ if (ao_rx_packet.packet.len == AO_PACKET_SYN) {
+ rx_seq = ao_rx_packet.packet.seq;
+ ao_tx_packet.seq = ao_rx_packet.packet.ack;
+ ao_tx_packet.ack = rx_seq;
+ } else if (ao_rx_packet.packet.len) {
+
+ /* Check for incoming data at the next sequence and
+ * for an empty data buffer
+ */
+ if (ao_rx_packet.packet.seq == (uint8_t) (rx_seq + (uint8_t) 1) &&
+ ao_packet_rx_used == ao_packet_rx_len) {
+
+ /* Copy data to the receive data buffer and set up the
+ * offsets
+ */
+ memcpy(rx_data, ao_rx_packet.packet.d, ao_rx_packet.packet.len);
+ ao_packet_rx_used = 0;
+ ao_packet_rx_len = ao_rx_packet.packet.len;
+
+ /* Mark the sequence that we've received to
+ * let the sender know when we return a packet
+ */
+ rx_seq = ao_rx_packet.packet.seq;
+ ao_tx_packet.ack = rx_seq;
+
+ /* Poke anyone looking for received data */
+ ao_wakeup(&ao_stdin_ready);