/* scan for the sequence $<data>#<checksum>. # will be replaced with \0. * Returns NULL on disconnection. */ char *getpacket(void) { char *buffer = &remcomInBuffer[0]; unsigned char checksum; unsigned char xmitcsum; int count; char ch; while (1) { /* wait around for the start character, ignore all other characters */ do { ch = get_debug_char(); if (ch == -1) // disconnected return NULL; } while (ch != '$'); retry: checksum = 0; xmitcsum = -1; count = 0; /* now, read until a # or end of buffer is found */ while (count < BUFMAX - 1) { ch = get_debug_char(); if (ch == '$') goto retry; buffer[count] = ch; if (ch == '#') break; count = count + 1; checksum = checksum + ch; } if (ch == '#') { buffer[count] = 0; ch = get_debug_char(); xmitcsum = hex(ch) << 4; ch = get_debug_char(); xmitcsum += hex(ch); if (checksum != xmitcsum) { put_debug_char('-'); /* failed checksum */ flush_out_buffer(); } else { put_debug_char('+'); /* successful transfer */ /* if a sequence char is present, reply the sequence ID */ if(buffer[2] == ':') { put_debug_char(buffer[0]); put_debug_char(buffer[1]); flush_out_buffer(); return &buffer[3]; } flush_out_buffer(); return &buffer[0]; } } } }
inline void DoSerial(void) { if (tx_tail!=tx_head) { if (put_debug_char(tx_buffer[tx_tail])) tx_tail = (tx_tail+1)&(DBG_BUFFER_SIZE-1); } }
void DebugTask(void) { if (debug_char_rdy()) debug_rcv(get_debug_char()); else debug_idle(); if (tx_tail!=tx_head) { if (put_debug_char(tx_buffer[tx_tail])) tx_tail = (tx_tail+1)&(BUFFER_SIZE-1); } }
/* send the packet in buffer. */ static void putpacket(char *buffer) { unsigned char checksum; int count; char ch; /* $<packet info>#<checksum> */ do { put_debug_char('$'); checksum = 0; count = 0; while ((ch = buffer[count])) { put_debug_char(ch); checksum += ch; count += 1; } put_debug_char('#'); put_debug_char(hexchars[checksum >> 4]); put_debug_char(hexchars[checksum & 0xf]); flush_out_buffer(); ch = get_debug_char(); } while (ch != '+' && ch != -1); }