Skip to content

Commit

Permalink
fixup! gdb_packet: add no ack mode
Browse files Browse the repository at this point in the history
  • Loading branch information
perigoso committed Jul 21, 2023
1 parent c9c1389 commit e58436e
Showing 1 changed file with 15 additions and 41 deletions.
56 changes: 15 additions & 41 deletions src/gdb_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef enum packet_state {
PACKET_GDB_ESCAPE,
PACKET_GDB_CHECKSUM_UPPER,
PACKET_GDB_CHECKSUM_LOWER,
PACKET_GDB_COMPLETE,
} packet_state_e;

static bool noackmode = false;
Expand Down Expand Up @@ -106,7 +107,7 @@ size_t gdb_getpacket(char *const packet, const size_t size)
uint8_t rx_checksum = 0, checksum = 0;

while (true) {
const char rx_char = gdb_if_getchar();
const char rx_char = state != PACKET_GDB_COMPLETE ? gdb_if_getchar() : '\0';
if (rx_char == '\x04') {
/* EOT (end of transmission) - connection was closed */
packet[0] = '\x04';
Expand All @@ -130,7 +131,7 @@ size_t gdb_getpacket(char *const packet, const size_t size)
* returns PACKET_IDLE or PACKET_GDB_CAPTURE if it detects the start of a GDB packet
*/
state = consume_remote_packet(packet, size);
/* No need to handle csum or offset, everything happened in PACKET_IDLE state so nothing was modified */
/* No need to handle checksum or offset, everything happened in PACKET_IDLE state so nothing was modified */
}
#endif
break;
Expand All @@ -139,35 +140,14 @@ size_t gdb_getpacket(char *const packet, const size_t size)
if (rx_char == GDB_PACKET_START) {
/* Restart GDB packet capture */
offset = 0;
csum = 0;
checksum = 0;
break;
}
if (rx_char == GDB_PACKET_END) {
/* End of GDB packet */
if (noackmode) {
/* We knowlingly ignore the checksum that follows the packet in no ack mode */

/* Null terminate packet */
packet[offset] = '\0';

/* Log packet for debugging */
DEBUG_GDB("%s: ", __func__);
for (size_t j = 0; j < offset; j++) {
const char value = packet[j];
if (value >= ' ' && value < '\x7f')
DEBUG_GDB("%c", value);
else
DEBUG_GDB("\\x%02X", value);
}
DEBUG_GDB("\n");

/* Return packet captured size */
return offset;
} else {
/* Move to checksum capture */
state = PACKET_GDB_CHECKSUM_UPPER;
break;
}
/* Move to checksum capture or knowlingly ignore the checksum that follows in no ack mode (as per spec) */
state = noackmode ? PACKET_GDB_COMPLETE : PACKET_GDB_CHECKSUM_UPPER;
break;
}

/* Not start or end of packet, add to checksum */
Expand Down Expand Up @@ -205,13 +185,12 @@ size_t gdb_getpacket(char *const packet, const size_t size)
state = PACKET_GDB_COMPLETE;
break;

/* Check checksum */
if (rx_csum == csum) {
/* Checksum matches our calculated checksum, captured valid packet */

/* Acknowledge packet */
gdb_if_putchar(GDB_PACKET_ACK, 1U);
case PACKET_GDB_COMPLETE:
if (!noackmode)
/* (N)Acknowledge packet */
gdb_if_putchar(rx_checksum == checksum ? GDB_PACKET_ACK : GDB_PACKET_NACK, 1U);

if (noackmode || rx_checksum == checksum) {
/* Null terminate packet */
packet[offset] = '\0';

Expand All @@ -228,15 +207,10 @@ size_t gdb_getpacket(char *const packet, const size_t size)

/* Return packet captured size */
return offset;
} else {
/* Checksum does not match our calculated checksum */

/* N-Acknowledge packet */
gdb_if_putchar(GDB_PACKET_NACK, 1U);

/* Restart packet capture */
state = PACKET_IDLE;
}

/* Restart packet capture */
state = PACKET_IDLE;
break;

default:
Expand Down

0 comments on commit e58436e

Please sign in to comment.