1
6
7
8
9
10
11
12
13
14
15
16
17
22
23
24
35
36
37
44
45
46
47
56
57
58
64
65
66
74
75
76
77
78
79
80
81
82
83
84
85
86
89
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
134
135
136
141
142
148
154
155
158
159
160
161
162
163
164
173
/* ... */
#include <assert.h>
#include "esp_gdbstub_common.h"
static unsigned char s_cmd[GDBSTUB_CMD_BUFLEN];
static char s_chsum;
void esp_gdbstub_send_start(void)
{
s_chsum = 0;
esp_gdbstub_putchar('$');
}{ ... }
void esp_gdbstub_send_char(char c)
{
if (c == '#' || c == '$' || c == '}' || c == '*') {
esp_gdbstub_putchar('}');
esp_gdbstub_putchar(c ^ 0x20);
s_chsum += (c ^ 0x20) + '}';
}{...} else {
esp_gdbstub_putchar(c);
s_chsum += c;
}{...}
}{ ... }
void esp_gdbstub_send_str(const char *c)
{
while (*c != 0) {
esp_gdbstub_send_char(*c);
c++;
}{...}
}{ ... }
void esp_gdbstub_send_hex(int val, int bits)
{
assert(bits >= 4);
const char *hex_chars = "0123456789abcdef";
for (int i = bits; i > 0; i -= 4) {
esp_gdbstub_send_char(hex_chars[(val >> (i - 4)) & 0xf]);
}{...}
}{ ... }
void esp_gdbstub_send_end(void)
{
esp_gdbstub_putchar('#');
esp_gdbstub_send_hex(s_chsum, 8);
esp_gdbstub_flush();
}{ ... }
void esp_gdbstub_send_str_packet(const char *str)
{
esp_gdbstub_send_start();
if (str != NULL) {
esp_gdbstub_send_str(str);
}{...}
esp_gdbstub_send_end();
}{ ... }
uint32_t esp_gdbstub_gethex(const unsigned char **ptr, int bits)
{
int i;
int no;
uint32_t v = 0;
char c;
no = bits / 4;
if (bits == -1) {
no = 64;
}{...}
for (i = 0; i < no; i++) {
c = **ptr;
(*ptr)++;
if (c >= '0' && c <= '9') {
v <<= 4;
v |= (c - '0');
}{...} else if (c >= 'A' && c <= 'F') {
v <<= 4;
v |= (c - 'A') + 10;
}{...} else if (c >= 'a' && c <= 'f') {
v <<= 4;
v |= (c - 'a') + 10;
}{...} else if (c == '#') {
if (bits == -1) {
(*ptr)--;
return v;
}{...}
return GDBSTUB_ST_ENDPACKET;
}{...} else {
if (bits == -1) {
(*ptr)--;
return v;
}{...}
return GDBSTUB_ST_ERR;
}{...}
}{...}
return v;
}{ ... }
int esp_gdbstub_read_command(unsigned char **out_cmd, size_t *out_size)
{
unsigned char c;
unsigned char chsum = 0;
unsigned char sentchs[2];
int p = 0;
c = esp_gdbstub_getchar();
if (c != '$') {
return c;
}{...}
while (1) {
c = esp_gdbstub_getchar();
if (c == '#') {
s_cmd[p] = 0;
break;
}{...}
chsum += c;
if (c == '$') {
chsum = 0;
p = 0;
continue;
}{...}
if (c == '}') {
c = esp_gdbstub_getchar();
chsum += c;
c ^= 0x20;
}{...}
s_cmd[p++] = c;
if (p >= GDBSTUB_CMD_BUFLEN) {
return GDBSTUB_ST_ERR;
}{...}
}{...}
sentchs[0] = esp_gdbstub_getchar();
sentchs[1] = esp_gdbstub_getchar();
const unsigned char *c_ptr = &sentchs[0];
unsigned char rchsum = esp_gdbstub_gethex(&c_ptr, 8);
if (rchsum != chsum) {
esp_gdbstub_putchar('-');
return GDBSTUB_ST_ERR;
}{...} else {
esp_gdbstub_putchar('+');
*out_cmd = s_cmd;
*out_size = p;
return GDBSTUB_ST_OK;
}{...}
}{ ... }