Skip to content

Commit 211db6d

Browse files
author
David Betz
committed
Convert calls to message that contain a message code to calls to nmessage.
1 parent c23173c commit 211db6d

File tree

5 files changed

+76
-77
lines changed

5 files changed

+76
-77
lines changed

src/fastloader.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ int Loader::fastLoadImage(const uint8_t *image, int imageSize, LoadType loadType
171171
}
172172

173173
/* transmit the image */
174-
message("002-Downloading file to port %s", m_connection->portName());
174+
nmessage(INFO_DOWNLOADING, m_connection->portName());
175175
remaining = imageSize;
176176
while (remaining > 0) {
177177
int size;
178-
progress("008-%ld bytes remaining ", (long)remaining);
178+
nprogress(INFO_BYTES_REMAINING, (long)remaining);
179179
if ((size = remaining) > m_connection->maxDataSize())
180180
size = m_connection->maxDataSize();
181181
if (transmitPacket(packetID, image, size, &result) != 0)
@@ -188,7 +188,7 @@ int Loader::fastLoadImage(const uint8_t *image, int imageSize, LoadType loadType
188188
image += size;
189189
--packetID;
190190
}
191-
message("009-%ld bytes sent ", (long)imageSize);
191+
nmessage(INFO_BYTES_SENT, (long)imageSize);
192192

193193
/*
194194
When we're doing a download that does not include an EEPROM write, the Packet IDs end up as:
@@ -206,7 +206,7 @@ int Loader::fastLoadImage(const uint8_t *image, int imageSize, LoadType loadType
206206
*/
207207

208208
/* transmit the RAM verify packet and verify the checksum */
209-
message("003-Verifying RAM");
209+
nmessage(INFO_VERIFYING_RAM);
210210
if (transmitPacket(packetID, verifyRAM, sizeof(verifyRAM), &result) != 0)
211211
return -1;
212212
if (result != -checksum) {
@@ -216,7 +216,7 @@ int Loader::fastLoadImage(const uint8_t *image, int imageSize, LoadType loadType
216216
packetID = -checksum;
217217

218218
if (loadType & ltDownloadAndProgram) {
219-
message("004-Programming EEPROM");
219+
nmessage(INFO_PROGRAMMING_EEPROM);
220220
if (transmitPacket(packetID, programVerifyEEPROM, sizeof(programVerifyEEPROM), &result, 8000) != 0)
221221
return -1;
222222
if (result != -checksum*2) {

src/main.cpp

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -279,21 +279,21 @@ int main(int argc, char *argv[])
279279
if (file) {
280280
nmessage(INFO_OPENING_FILE, file);
281281
if (!(image = Loader::readFile(file, &imageSize))) {
282-
message("103-Can't open file '%s'", file);
282+
nmessage(ERROR_CANT_OPEN_FILE, file);
283283
return 1;
284284
}
285285
switch (PropImage::validate(image, imageSize)) {
286286
case PropImage::SUCCESS:
287287
// success
288288
break;
289289
case PropImage::IMAGE_TRUNCATED:
290-
message("110-File is truncated or not a Propeller application image");
290+
nmessage(ERROR_FILE_TRUNCATED);
291291
return 1;
292292
case PropImage::IMAGE_CORRUPTED:
293-
message("111-File is corrupt or not a Propeller application");
293+
nmessage(ERROR_FILE_CORRUPTED);
294294
return 1;
295295
default:
296-
message("998-Internal error");
296+
nmessage(ERROR_INTERNAL_CODE_ERROR);
297297
return 1;
298298
}
299299
}
@@ -374,24 +374,24 @@ int main(int argc, char *argv[])
374374
if (useSerial) {
375375
SerialInfo info; // needs to stay in scope as long as we're using port
376376
if (!(serialConnection = new SerialPropConnection)) {
377-
message("999-Insufficient memory");
377+
nmessage(ERROR_INSUFFICIENT_MEMORY);
378378
return 1;
379379
}
380380
if (!port) {
381381
SerialInfoList ports;
382382
if (SerialPropConnection::findPorts(PORT_PREFIX, true, ports) != 0) {
383-
message("115-Serial port discovery failed");
383+
nmessage(ERROR_SERIAL_PORT_DISCOVERY_FAILED);
384384
return 1;
385385
}
386386
if (ports.size() == 0) {
387-
message("116-No serial ports found");
387+
nmessage(ERROR_NO_SERIAL_PORTS_FOUND);
388388
return 1;
389389
}
390390
info = ports.front();
391391
port = info.port();
392392
}
393393
if ((sts = serialConnection->open(port)) != 0) {
394-
message("117-Unable to connect to port %s", port);
394+
nmessage(ERROR_UNABLE_TO_CONNECT_TO_PORT, port);
395395
return 1;
396396
}
397397
connection = serialConnection;
@@ -400,41 +400,38 @@ int main(int argc, char *argv[])
400400
/* do a wifi download */
401401
else {
402402
if (!(wifiConnection = new WiFiPropConnection)) {
403-
message("999-Insufficient memory");
403+
nmessage(ERROR_INSUFFICIENT_MEMORY);
404404
return 1;
405405
}
406406
if (!ipaddr) {
407407
WiFiInfoList addrs;
408408
if (WiFiPropConnection::findModules(false, addrs, 1) != 0) {
409-
message("113-Wifi module discovery failed");
409+
nmessage(ERROR_WIFI_MODULE_DISCOVERY_FAILED);
410410
return 1;
411411
}
412412
if (addrs.size() == 0) {
413-
message("114-No wifi modules found");
413+
nmessage(ERROR_NO_WIFI_MODULES_FOUND);
414414
return 1;
415415
}
416416
const char *ipaddr2 = addrs.front().address();
417417
char *p;
418418
if (!(p = (char *)malloc(strlen(ipaddr2) + 1))) {
419-
message("999-Insufficient memory");
419+
nmessage(ERROR_INSUFFICIENT_MEMORY);
420420
return 1;
421421
}
422422
strcpy(p, ipaddr2);
423423
ipaddr = p;
424424
}
425425
if ((sts = wifiConnection->setAddress(ipaddr)) != 0) {
426-
message("101-Invalid address: %s", ipaddr);
426+
nmessage(ERROR_INVALID_MODULE_ADDRESS, ipaddr);
427427
return 1;
428428
}
429429
if (wifiConnection->getVersion() != 0) {
430-
message("118-Unable to connect to module at %s", ipaddr);
430+
nmessage(ERROR_UNABLE_TO_CONNECT_TO_MODULE, ipaddr);
431431
return 1;
432432
}
433433
if ((sts = wifiConnection->checkVersion()) != 0) {
434-
message("\
435-
106-Unrecognized wi-fi module firmware\n\
436-
Version is %s but expected %s.\n\
437-
Recommended action: update firmware and/or PropLoader to latest version(s).", wifiConnection->version(), WIFI_REQUIRED_MAJOR_VERSION);
434+
nmessage(ERROR_WRONG_WIFI_MODULE_FIRMWARE, wifiConnection->version(), WIFI_REQUIRED_MAJOR_VERSION);
438435
return 1;
439436
}
440437
connection = wifiConnection;
@@ -497,25 +494,25 @@ int main(int argc, char *argv[])
497494

498495
/* if we deleted every character then this is an invalid name */
499496
if (!cleanName[0]) {
500-
message("108-Invalid module name");
497+
nmessage(ERROR_INVALID_MODULE_NAME);
501498
return 1;
502499
}
503500

504501
/* show the clean name if it is different from what the user requested */
505502
if (strcmp(name, cleanName) != 0)
506-
message("010-Setting module name to '%s'", cleanName);
503+
nmessage(INFO_SETTING_MODULE_NAME, cleanName);
507504

508505
if (wifiConnection->setName(cleanName) != 0) {
509-
message("109-Failed to set module name");
506+
nmessage(ERROR_FAILED_TO_SET_MODULE_NAME);
510507
return 1;
511508
}
512509
}
513510

514511
/* write a file to the SD card */
515512
if (writeFile) {
516-
message("007-Writing '%s' to the SD card", file);
513+
nmessage(INFO_WRITING_TO_SD_CARD, file);
517514
if (WriteFileToSDCard(config, connection, file, file) != 0) {
518-
message("107-Failed to write SD card file '%s'", file);
515+
nmessage(ERROR_FAILED_TO_WRITE_TO_SD_CARD, file);
519516
return 1;
520517
}
521518
}
@@ -524,32 +521,32 @@ int main(int argc, char *argv[])
524521
else if (file) {
525522
loader.setConnection(connection);
526523
if (file && (sts = loader.fastLoadImage(image, imageSize, (LoadType)loadType)) != 0) {
527-
message("102-Download failed: %d", sts);
524+
nmessage(ERROR_DOWNLOAD_FAILED, sts);
528525
return 1;
529526
}
530-
message("005-Download successful!");
527+
nmessage(INFO_DOWNLOAD_SUCCESSFUL);
531528
}
532529

533530
/* set the baud rate used by the program */
534531
if (connection->setBaudRate(connection->programBaudRate()) != 0) {
535-
message("119-Failed to set baud rate");
532+
nmessage(ERROR_FAILED_TO_SET_BAUD_RATE);
536533
return 1;
537534
}
538535

539536
/* enter terminal mode */
540537
if (terminalMode) {
541-
message("006-[ Entering terminal mode. Type ESC or Control-C to exit. ]");
538+
nmessage(INFO_TERMINAL_MODE);
542539

543540
/* open a connection to the target */
544541
if (!connection->isOpen() && connection->connect() != 0) {
545542
message("Can't open connection to target");
546-
message("105-Failed to enter terminal mode");
543+
nmessage(ERROR_FAILED_TO_ENTER_TERMINAL_MODE);
547544
return 1;
548545
}
549546

550547
/* enter terminal mode */
551548
if (connection->terminal(false, pstTerminalMode) != 0) {
552-
message("105-Failed to enter terminal mode");
549+
nmessage(ERROR_FAILED_TO_ENTER_TERMINAL_MODE);
553550
return 1;
554551
}
555552
}
@@ -592,9 +589,9 @@ static int WriteFileToSDCard(BoardConfig *config, PropConnection *connection, co
592589
FILE *fp;
593590

594591
/* open the file */
595-
message("001-Opening file '%s'", path);
592+
nmessage(INFO_OPENING_FILE, path);
596593
if ((fp = fopen(path, "rb")) == NULL)
597-
return error("103-Can't open file '%s'", path);
594+
return nerror(ERROR_CANT_OPEN_FILE, path);
598595

599596
if (!target) {
600597
if (!(target = strrchr(path, '/')))
@@ -623,14 +620,14 @@ static int WriteFileToSDCard(BoardConfig *config, PropConnection *connection, co
623620
}
624621

625622
while ((cnt = fread(buf, 1, PKTMAXLEN, fp)) > 0) {
626-
progress("008-%ld bytes remaining ", (long)remaining);
623+
nprogress(INFO_BYTES_REMAINING, (long)remaining);
627624
if (!packetDriver.sendPacket(TYPE_DATA, buf, cnt)) {
628625
fclose(fp);
629626
return error("SendPacket DATA failed");
630627
}
631628
remaining -= cnt;
632629
}
633-
message("009-%ld bytes sent ", (long)size);
630+
nmessage(INFO_BYTES_SENT, (long)size);
634631

635632
fclose(fp);
636633

src/messages.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ void nmessage(int code, ...)
121121
va_end(ap);
122122
}
123123

124-
void progress(const char *fmt, ...)
124+
void nprogress(int code, ...)
125125
{
126126
va_list ap;
127-
va_start(ap, fmt);
128-
vmessage(fmt, ap, '\r');
127+
va_start(ap, code);
128+
vnmessage(code, messageText(code), ap, '\n');
129129
va_end(ap);
130130
}
131131

src/messages.h

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,44 @@ is created, it simply takes on the next available code number even if it's logic
3232
*/
3333

3434
enum {
35-
MIN_INFO = 1,
36-
INFO_OPENING_FILE = MIN_INFO,
37-
INFO_DOWNLOADING,
38-
INFO_VERIFYING_RAM,
39-
INFO_PROGRAMMING_EEPROM,
40-
INFO_DOWNLOAD_SUCCESSFUL,
41-
INFO_TERMINAL_MODE,
42-
INFO_WRITING_TO_SD_CARD,
43-
INFO_BYTES_REMAINING,
44-
INFO_BYTES_SENT,
45-
INFO_SETTING_MODULE_NAME,
35+
/* 000 */ DEBUG_MESSAGE,
36+
37+
MIN_INFO = 1,
38+
/* 001 */ INFO_OPENING_FILE = MIN_INFO,
39+
/* 002 */ INFO_DOWNLOADING,
40+
/* 003 */ INFO_VERIFYING_RAM,
41+
/* 004 */ INFO_PROGRAMMING_EEPROM,
42+
/* 005 */ INFO_DOWNLOAD_SUCCESSFUL,
43+
/* 006 */ INFO_TERMINAL_MODE,
44+
/* 007 */ INFO_WRITING_TO_SD_CARD,
45+
/* 008 */ INFO_BYTES_REMAINING,
46+
/* 009 */ INFO_BYTES_SENT,
47+
/* 010 */ INFO_SETTING_MODULE_NAME,
4648
MAX_INFO,
4749

48-
MIN_ERROR = 100,
49-
ERROR_CAN_ONLY_NAME_WIFI_MODULES = MIN_ERROR,
50-
ERROR_INVALID_MODULE_ADDRESS,
51-
ERROR_DOWNLOAD_FAILED,
52-
ERROR_CANT_OPEN_FILE,
53-
ERROR_PROPELLER_NOT_FOUND,
54-
ERROR_FAILED_TO_ENTER_TERMINAL_MODE,
55-
ERROR_WRONG_WIFI_MODULE_FIRMWARE,
56-
ERROR_FAILED_TO_WRITE_TO_SD_CARD,
57-
ERROR_INVALID_MODULE_NAME,
58-
ERROR_FAILED_TO_SET_MODULE_NAME,
59-
ERROR_FILE_TRUNCATED,
60-
ERROR_FILE_CORRUPTED,
61-
ERROR_CANT_READ_PROPELLER_APP_FILE,
62-
ERROR_WIFI_MODULE_DISCOVERY_FAILED,
63-
ERROR_NO_WIFI_MODULES_FOUND,
64-
ERROR_SERIAL_PORT_DISCOVERY_FAILED,
65-
ERROR_NO_SERIAL_PORTS_FOUND,
66-
ERROR_UNABLE_TO_CONNECT_TO_PORT,
67-
ERROR_UNABLE_TO_CONNECT_TO_MODULE,
68-
ERROR_FAILED_TO_SET_BAUD_RATE,
69-
ERROR_INTERNAL_CODE_ERROR,
70-
ERROR_INSUFFICIENT_MEMORY,
50+
MIN_ERROR = 100,
51+
/* 100 */ ERROR_CAN_ONLY_NAME_WIFI_MODULES = MIN_ERROR,
52+
/* 101 */ ERROR_INVALID_MODULE_ADDRESS,
53+
/* 102 */ ERROR_DOWNLOAD_FAILED,
54+
/* 103 */ ERROR_CANT_OPEN_FILE,
55+
/* 104 */ ERROR_PROPELLER_NOT_FOUND,
56+
/* 105 */ ERROR_FAILED_TO_ENTER_TERMINAL_MODE,
57+
/* 106 */ ERROR_WRONG_WIFI_MODULE_FIRMWARE,
58+
/* 107 */ ERROR_FAILED_TO_WRITE_TO_SD_CARD,
59+
/* 108 */ ERROR_INVALID_MODULE_NAME,
60+
/* 109 */ ERROR_FAILED_TO_SET_MODULE_NAME,
61+
/* 110 */ ERROR_FILE_TRUNCATED,
62+
/* 111 */ ERROR_FILE_CORRUPTED,
63+
/* 112 */ ERROR_CANT_READ_PROPELLER_APP_FILE,
64+
/* 113 */ ERROR_WIFI_MODULE_DISCOVERY_FAILED,
65+
/* 114 */ ERROR_NO_WIFI_MODULES_FOUND,
66+
/* 115 */ ERROR_SERIAL_PORT_DISCOVERY_FAILED,
67+
/* 116 */ ERROR_NO_SERIAL_PORTS_FOUND,
68+
/* 117 */ ERROR_UNABLE_TO_CONNECT_TO_PORT,
69+
/* 118 */ ERROR_UNABLE_TO_CONNECT_TO_MODULE,
70+
/* 119 */ ERROR_FAILED_TO_SET_BAUD_RATE,
71+
/* 120 */ ERROR_INTERNAL_CODE_ERROR,
72+
/* 121 */ ERROR_INSUFFICIENT_MEMORY,
7173
MAX_ERROR
7274
};
7375

@@ -78,7 +80,7 @@ int error(const char *fmt, ...);
7880
int nerror(int code, ...);
7981
void message(const char *fmt, ...);
8082
void nmessage(int code, ...);
81-
void progress(const char *fmt, ...);
83+
void nprogress(int code, ...);
8284

8385
#ifdef __cplusplus
8486
}

src/wifipropconnection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ GET /wx/setting?name=version HTTP/1.1\r\n\
382382
}
383383

384384
if (!(dst = (char *)malloc(srcLen - 4 + 1))) {
385-
message("999-Insufficient memory");
385+
nmessage(ERROR_INSUFFICIENT_MEMORY);
386386
return -1;
387387
}
388388

0 commit comments

Comments
 (0)