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

53
irena.c
View file

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