Skip to content

Commit 65acb6c

Browse files
David BetzDavid Betz
David Betz
authored and
David Betz
committed
More work on error reporting during downloads.
1 parent 00be190 commit 65acb6c

File tree

6 files changed

+86
-26
lines changed

6 files changed

+86
-26
lines changed

src/fastloader.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ int Loader::fastLoadImageHelper(const uint8_t *image, int imageSize, LoadType lo
243243
if ((sts = transmitPacket(packetID, verifyRAM, sizeof(verifyRAM), &result)) != 0)
244244
return sts;
245245
if (result != -checksum) {
246-
nmessage(ERROR_RAM_CHECKSUM_FAILED, result, -checksum);
246+
nmessage(ERROR_RAM_CHECKSUM_FAILED);
247247
return -2;
248248
}
249249
packetID = -checksum;
@@ -253,7 +253,7 @@ int Loader::fastLoadImageHelper(const uint8_t *image, int imageSize, LoadType lo
253253
if ((sts = transmitPacket(packetID, programVerifyEEPROM, sizeof(programVerifyEEPROM), &result, 8000)) != 0)
254254
return sts;
255255
if (result != -checksum*2) {
256-
nmessage(ERROR_EEPROM_CHECKSUM_FAILED, result, -checksum*2);
256+
nmessage(ERROR_EEPROM_CHECKSUM_FAILED);
257257
return -2;
258258
}
259259
packetID = -checksum*2;

src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ int main(int argc, char *argv[])
536536
else if (file) {
537537
loader.setConnection(connection);
538538
if ((sts = loader.fastLoadImage(image, imageSize, (LoadType)loadType)) != 0) {
539-
nmessage(ERROR_DOWNLOAD_FAILED, sts);
539+
nmessage(ERROR_DOWNLOAD_FAILED);
540540
return 1;
541541
}
542542
nmessage(INFO_DOWNLOAD_SUCCESSFUL);

src/messages.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static const char *infoText[] = {
4949
static const char *errorText[] = {
5050
"Option -n can only be used to name wifi modules",
5151
"Invalid address: %s",
52-
"Download failed: %d",
52+
"Download failed",
5353
"Can't open file '%s'",
5454
"Propeller not found on %s",
5555
"Failed to enter terminal mode",
@@ -74,8 +74,11 @@ static const char *errorText[] = {
7474
"No reset method '%s'",
7575
"Reset failed",
7676
"Wrong Propeller version: got %d, expected 1",
77-
"RAM checksum failed: got %d, expected %d",
78-
"EEPROM checksum failed: got %d, expected %d"
77+
"RAM checksum failed",
78+
"EEPROM checksum failed",
79+
"EEPROM verify failed",
80+
"Communication lost",
81+
"Load image failed"
7982
};
8083

8184
static void vmessage(const char *fmt, va_list ap, int eol);

src/messages.h

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ enum {
7979
/* 124 */ ERROR_WRONG_PROPELLER_VERSION,
8080
/* 125 */ ERROR_RAM_CHECKSUM_FAILED,
8181
/* 126 */ ERROR_EEPROM_CHECKSUM_FAILED,
82+
/* 127 */ ERROR_EEPROM_VERIFY_FAILED,
83+
/* 128 */ ERROR_COMMUNICATION_LOST,
84+
/* 129 */ ERROR_LOAD_IMAGE_FAILED,
8285
MAX_ERROR
8386
};
8487

src/serialloader.cpp

+15-13
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,16 @@ int SerialPropConnection::loadImage(const uint8_t *image, int imageSize, LoadTyp
339339
uint8_t *packet;
340340

341341
/* use the loader baud rate */
342-
if (setBaudRate(loaderBaudRate()) != 0)
342+
if (setBaudRate(loaderBaudRate()) != 0) {
343+
nerror(ERROR_FAILED_TO_SET_BAUD_RATE);
343344
return -1;
345+
}
344346

345347
/* generate a loader packet */
346-
if (!(packet = GenerateLoaderPacket(image, imageSize, &packetSize, loadType)))
348+
if (!(packet = GenerateLoaderPacket(image, imageSize, &packetSize, loadType))) {
349+
nerror(ERROR_INTERNAL_CODE_ERROR);
347350
return -1;
351+
}
348352

349353
/* reset the Propeller */
350354
generateResetSignal();
@@ -392,15 +396,14 @@ int SerialPropConnection::loadImage(const uint8_t *image, int imageSize, LoadTyp
392396

393397
/* check for timeout */
394398
if (cnt <= 0) {
395-
message("Timeout waiting for checksum");
396-
nmessage(ERROR_DOWNLOAD_FAILED);
399+
nmessage(ERROR_COMMUNICATION_LOST);
397400
return -1;
398401
}
399402

400403
/* verify the checksum response */
401404
if (packet2[0] != 0xFE) {
402-
message("Loader checksum failed: expected 0xFE, got %02x", packet2[0]);
403-
nmessage(ERROR_DOWNLOAD_FAILED);
405+
//message("RAM checksum failed: expected 0xFE, got %02x", packet2[0]);
406+
nmessage(ERROR_RAM_CHECKSUM_FAILED);
404407
return -1;
405408
}
406409

@@ -420,15 +423,14 @@ int SerialPropConnection::loadImage(const uint8_t *image, int imageSize, LoadTyp
420423

421424
/* check for timeout */
422425
if (cnt <= 0) {
423-
message("Timeout waiting for checksum");
424-
nmessage(ERROR_DOWNLOAD_FAILED);
426+
nmessage(ERROR_COMMUNICATION_LOST);
425427
return -1;
426428
}
427429

428430
/* verify the checksum response */
429431
if (packet2[0] != 0xFE) {
430-
message("Loader checksum failed: expected 0xFE, got %02x", packet2[0]);
431-
nmessage(ERROR_DOWNLOAD_FAILED);
432+
//message("EEPROM checksum failed: expected 0xFE, got %02x", packet2[0]);
433+
nmessage(ERROR_EEPROM_CHECKSUM_FAILED);
432434
return -1;
433435
}
434436

@@ -446,14 +448,14 @@ int SerialPropConnection::loadImage(const uint8_t *image, int imageSize, LoadTyp
446448
/* check for timeout */
447449
if (cnt <= 0) {
448450
message("Timeout waiting for checksum");
449-
nmessage(ERROR_DOWNLOAD_FAILED);
451+
nmessage(ERROR_COMMUNICATION_LOST);
450452
return -1;
451453
}
452454

453455
/* verify the checksum response */
454456
if (packet2[0] != 0xFE) {
455-
message("Loader checksum failed: expected 0xFE, got %02x", packet2[0]);
456-
nmessage(ERROR_DOWNLOAD_FAILED);
457+
//message("EEPROM verify failed: expected 0xFE, got %02x", packet2[0]);
458+
nmessage(ERROR_EEPROM_VERIFY_FAILED);
457459
return -1;
458460
}
459461
}

src/wifipropconnection.cpp

+59-7
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ int WiFiPropConnection::identify(int *pVersion)
9696
return 0;
9797
}
9898

99+
static int beginsWith(const char *body, const char *str)
100+
{
101+
int length = strlen(str);
102+
return strncasecmp(body, str, length) == 0;
103+
}
104+
99105
int WiFiPropConnection::loadImage(const uint8_t *image, int imageSize, uint8_t *response, int responseSize)
100106
{
101107
uint8_t buffer[1024], *packet, *body;
@@ -122,19 +128,38 @@ Content-Length: %d\r\n\
122128
}
123129
else if (result != 200) {
124130
char *body = (char *)getBody(buffer, cnt, &cnt);
131+
int sts = -1;
125132
if (body) {
126133
body[cnt] = '\0';
127-
if (strncasecmp(body, "RX handshake timeout", strlen("RX handshake timeout")) == 0
128-
|| strncasecmp(body, "RX handshake failed", strlen("RX handshake failed")) == 0) {
134+
if (beginsWith(body, "RX handshake timeout")) {
135+
nerror(ERROR_COMMUNICATION_LOST);
136+
}
137+
else if (beginsWith(body, "RX handshake failed")) {
129138
nerror(ERROR_PROPELLER_NOT_FOUND, portName());
130139
}
131-
else if (strncasecmp(body, "Wrong Propeller version: got ", strlen("Wrong Propeller version: got ")) == 0) {
140+
else if (beginsWith(body, "Wrong Propeller version: got ")) {
132141
int version = atoi(&body[strlen("Wrong Propeller version: got ")]);
133142
nerror(ERROR_WRONG_PROPELLER_VERSION, version);
134143
}
144+
else if (beginsWith(body, "Checksum timeout")) {
145+
nerror(ERROR_COMMUNICATION_LOST);
146+
}
147+
else if (beginsWith(body, "Checksum error")) {
148+
nerror(ERROR_RAM_CHECKSUM_FAILED);
149+
}
150+
else if (beginsWith(body, "Load image failed")) {
151+
nerror(ERROR_LOAD_IMAGE_FAILED);
152+
}
153+
else if (beginsWith(body, "StartAck timeout")) {
154+
nerror(ERROR_COMMUNICATION_LOST);
155+
sts = -2;
156+
}
157+
else {
158+
nerror(ERROR_INTERNAL_CODE_ERROR);
159+
}
135160
}
136161
message("Load returned %d", result);
137-
return -1;
162+
return sts;
138163
}
139164

140165
/* find the response body */
@@ -152,7 +177,7 @@ Content-Length: %d\r\n\
152177
int WiFiPropConnection::loadImage(const uint8_t *image, int imageSize, LoadType loadType, int info)
153178
{
154179
uint8_t buffer[1024], *packet;
155-
int hdrCnt, result;
180+
int hdrCnt, result, cnt;
156181

157182
/* WX image buffer is limited to 2K */
158183
if (imageSize > 2048)
@@ -173,13 +198,40 @@ Content-Length: %d\r\n\
173198
memcpy(packet, buffer, hdrCnt);
174199
memcpy(&packet[hdrCnt], image, imageSize);
175200

176-
if (sendRequest(packet, hdrCnt + imageSize, buffer, sizeof(buffer), &result) == -1) {
201+
if ((cnt = sendRequest(packet, hdrCnt + imageSize, buffer, sizeof(buffer), &result)) == -1) {
177202
message("Load request failed");
178203
return -1;
179204
}
180205
else if (result != 200) {
206+
char *body = (char *)getBody(buffer, cnt, &cnt);
207+
int sts = -1;
208+
if (body) {
209+
body[cnt] = '\0';
210+
if (beginsWith(body, "RX handshake timeout")) {
211+
nerror(ERROR_COMMUNICATION_LOST);
212+
}
213+
else if (beginsWith(body, "RX handshake failed")) {
214+
nerror(ERROR_PROPELLER_NOT_FOUND, portName());
215+
}
216+
else if (beginsWith(body, "Wrong Propeller version: got ")) {
217+
int version = atoi(&body[strlen("Wrong Propeller version: got ")]);
218+
nerror(ERROR_WRONG_PROPELLER_VERSION, version);
219+
}
220+
else if (beginsWith(body, "Checksum timeout")) {
221+
nerror(ERROR_COMMUNICATION_LOST);
222+
}
223+
else if (beginsWith(body, "Checksum error")) {
224+
nerror(ERROR_RAM_CHECKSUM_FAILED);
225+
}
226+
else if (beginsWith(body, "Load image failed")) {
227+
nerror(ERROR_LOAD_IMAGE_FAILED);
228+
}
229+
else {
230+
nerror(ERROR_INTERNAL_CODE_ERROR);
231+
}
232+
}
181233
message("Load returned %d", result);
182-
return -1;
234+
return sts;
183235
}
184236

185237
return 0;

0 commit comments

Comments
 (0)