From 3199529ab3ed39737454f9f0b4e91915955c38b2 Mon Sep 17 00:00:00 2001 From: Thomas Albers Date: Thu, 10 Aug 2023 11:18:49 +0200 Subject: Add attempts number as a command line option --- serial.c | 12 ++++++------ zup.c | 13 ++++++++++++- zup.h | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/serial.c b/serial.c index f6270ba..5f7548c 100644 --- a/serial.c +++ b/serial.c @@ -152,8 +152,8 @@ write_header(int fd, const struct header *header) msg(1, "write_header\n"); - for (int i = 0; i < MAX_TRANS_ATTEMPTS; ++i) { - msg(1, " attempt [%d/%d]\n", i + 1, MAX_TRANS_ATTEMPTS); + for (int i = 0; i < param.max_attempts; ++i) { + msg(1, " attempt [%d/%d]\n", i + 1, param.max_attempts); if ((err = e_write(fd, buf, sizeof(buf)))) goto error; @@ -189,8 +189,8 @@ read_buf(int fd, size_t len, void *_buf) msg(1, "read_buf\n"); - for (int i = 0; i < MAX_TRANS_ATTEMPTS; ++i) { - msg(1, " attempt [%d/%d]\n", i + 1, MAX_TRANS_ATTEMPTS); + for (int i = 0; i < param.max_attempts; ++i) { + msg(1, " attempt [%d/%d]\n", i + 1, param.max_attempts); if ((err = e_read(fd, echecksum, sizeof(echecksum))) || (err = e_read(fd, ebuf, 2 * len))) @@ -231,8 +231,8 @@ write_buf(int fd, size_t len, const void *_buf) msg(1, "write_buf\n"); int ack; - for (int i = 0; i < MAX_TRANS_ATTEMPTS; ++i) { - msg(1, " attempt [%d/%d]\n", i + 1, MAX_TRANS_ATTEMPTS); + for (int i = 0; i < param.max_attempts; ++i) { + msg(1, " attempt [%d/%d]\n", i + 1, param.max_attempts); if ((err = e_write(fd, echecksum, LEN(echecksum))) || (err = e_write(fd, ebuf, 2 * len))) diff --git a/zup.c b/zup.c index d3acc54..5fe1e61 100644 --- a/zup.c +++ b/zup.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -97,7 +98,7 @@ parse_baud(const char *str) static int parse_options(int argc, char *const argv[]) { - const char *sopts = "b:p:hHVPRrv"; + const char *sopts = "a:b:p:hHVPRrv"; const struct option lopts[] = { {"baud", required_argument, 0, 'b'}, {"verbose", no_argument, 0, 'v'}, @@ -106,6 +107,7 @@ parse_options(int argc, char *const argv[]) {"repl", no_argument, 0, 'R'}, {"port", required_argument, 0, 'p'}, {"progress", no_argument, 0, 'P'}, + {"attempts", required_argument, 0, 'a'}, {"human-readable", no_argument, 0, 'H'}, {"help", no_argument, 0, 'h'}, { 0, 0, 0, 0 } @@ -123,6 +125,15 @@ parse_options(int argc, char *const argv[]) } break; + case 'a': + errno = 0; + param.max_attempts = strtoul(optarg, NULL, 10); + if (errno) { + perror("Invalid attempts number\n"); + exit(EXIT_FAILURE); + } + break; + case 'v': param.verbose = 1; break; diff --git a/zup.h b/zup.h index 8fd5f7d..9f01d15 100644 --- a/zup.h +++ b/zup.h @@ -26,6 +26,7 @@ struct param { unsigned int human_readable : 1; unsigned int verbose : 2; unsigned int show_progress : 1; + unsigned int max_attempts; }; #ifdef __GNUC__ @@ -123,7 +124,6 @@ hexdump(size_t start_address, size_t len, const uint8_t *buf); #define MAX_ARGS 5 #define MAX_PACKET_SIZE 256 #define MAX_ATTEMPTS 3 -#define MAX_TRANS_ATTEMPTS 5 #define VERSION "1.0.0" -- cgit v1.2.3