Compare commits

...

5 commits

Author SHA1 Message Date
Stephan I. Böttcher
1e247d1c89 send_file: fill buffer tail with 0 and send all 2023-11-14 08:49:02 +01:00
Stephan I. Böttcher
14e5942df7 verbosity tuning 2023-11-14 01:35:49 +01:00
Stephan I. Böttcher
218e90587c fix send_file, do not read beyond the input, whole words 2023-11-14 01:00:37 +01:00
Stephan I. Böttcher
a9abe76f5e irena: fix verbosity 2023-11-14 00:33:24 +01:00
Stephan I. Böttcher
c94ecf149b irena: implement verbosity 2023-11-14 00:31:59 +01:00

67
irena.c
View file

@ -30,11 +30,29 @@ const char version[] = "$Id$";
FILE *mout;
int verbosity = 1;
int next_verbosity = 1;
static inline void silent(int v) {
if (verbosity < v)
next_verbosity = 0;
}
static inline void loud(int v) {
if (v > verbosity)
next_verbosity = v;
}
static inline int verbose(int v)
{
v = next_verbosity >= v;
next_verbosity = verbosity;
return v;
}
// replacement for perror()
void merror(const char *s)
{
int e = errno;
fprintf(mout, "%s: %s\n", s, strerror(e));
if (verbose(-1))
fprintf(mout, "%s: %s\n", s, strerror(e));
}
#ifdef DEBUG
@ -1936,7 +1954,7 @@ int send_irena_command(const char *c)
{
static int no_uart;
if (i_buf.fd <= 0) {
if (!no_uart)
if (!no_uart && verbose(-1))
fprintf(mout, "irena: no uart: %s\n", c);
no_uart++;
return -1;
@ -1948,6 +1966,8 @@ int send_irena_command(const char *c)
size_t sl = strlen(c);
int n = write(i_buf.fd, c, sl);
last_irena_command_sent = t;
if (verbose(2))
fprintf(mout, "IRENA CMD sent: «%s»\n", c);
if (n==sl)
n += write(i_buf.fd, "\n", 1);
if (n != sl+1)
@ -1956,7 +1976,8 @@ int send_irena_command(const char *c)
return -3;
}
else {
fprintf(mout, "incomplete write: %d/%lu %s\n", n, strlen(c), c);
if (verbose(-1))
fprintf(mout, "incomplete write: %d/%lu %s\n", n, strlen(c), c);
return -4;
}
return 0;
@ -1973,9 +1994,11 @@ int fct_min_data = 1;
time_t fct_last;
char fct_cmd[16];
static inline
void send_fct(time_t t) {
void send_fct(time_t t)
{
if (fct_status != fct_size) {
if (!fct_size) {
silent(4);
if (!send_irena_command("u/f=-2")) {
fct_ack = -1;
fct_status = fct_size;
@ -1990,12 +2013,14 @@ void send_fct(time_t t) {
fct_status = -1;
return;
}
if (fct_got_data >= fct_min_data || fct_last + fct_max_cadence < t)
if (fct_got_data >= fct_min_data || fct_last + fct_max_cadence < t) {
silent(4);
if (!send_irena_command(fct_cmd)) {
fct_last = t;
fct_got_data = 0;
fct_sent++;
}
}
}
void process_line(const char *l)
@ -2006,14 +2031,19 @@ void process_line(const char *l)
if (!strncmp(l, "=B64 ", 5)) {
fct_got_data++;
fct_packets++;
if (verbose(3))
fprintf(mout, "DATA received: «%s»\n", l);
process_base64(l+5);
return;
}
if (*l && !strncmp(l+1, "FCT ", 4)) {
fct_ack++;
if (verbose(4))
fprintf(mout, "FCT ACK received %d, «%s»\n", fct_ack, l);
return;
}
fprintf(mout, "IRENA: %s\n", l);
if (verbose(1))
fprintf(mout, "IRENA: %s\n", l);
eprintf(EP_M, "I %s\n", l);
}
@ -2063,13 +2093,21 @@ int send_file(const char *fn)
unsigned char buf[512];
int n;
while ((n = fread(buf, 1, sizeof(buf), f))) {
if (n<sizeof(buf))
memset(buf+n, 0, sizeof(buf)-n);
n = sizeof(buf);
n = (n+3) & ~3; // NOP
for (int i=0; i < n; ) {
char cmd[64];
int retry = 0;
int c = snprintf(cmd, 64, "v f[%d]=:", i);
int c = snprintf(cmd, 64, "v f[%d]=:", i>>2);
while (c<58) {
int nn = -i & 3;
if (c < 54 || nn > n-i)
nn = n-i;
if (!nn)
break;
unsigned char a1, a2=0, a3=0;
int nn = n-i;
a1 = buf[i++];
if (nn>1) a2 = buf[i++];
if (nn>2) a3 = buf[i++];
@ -2080,6 +2118,7 @@ int send_file(const char *fn)
}
cmd[c++] = 0;
while (retry<3) {
loud(2);
while (-2==send_irena_command(cmd))
poll_uart(1);
char *res = poll_response(2);
@ -2425,6 +2464,7 @@ struct numbers {
{.name="time_resolution", .doc="seconds", .i=&time_resolution, .flags=NU_INT},
{.name="highgain", .doc="ch#", .i=high_gain_ch, .flags=NU_INT, .size=18},
{.name="lowgain", .doc="ch#", .i=low_gain_ch, .flags=NU_INT, .size=18},
{.name="verbosity", .doc="int", .i=&verbosity, .flags=NU_INT},
{.name=0}
};
@ -2511,11 +2551,12 @@ int process_cmd(char *l)
int na = split(l, 10, av);
if (!na)
return 0;
fprintf(mout, "CMD:");
for (int i=0; i<na; i++)
fprintf(mout, " «%s»", av[i]);
fprintf(mout, "\n");
if (verbose(2) || !strcmp(av[0], "echo")) {
fprintf(mout, "CMD:");
for (int i=0; i<na; i++)
fprintf(mout, " «%s»", av[i]);
fprintf(mout, "\n");
}
if (!strcmp(av[0], "echo")) {
return 0;
}