From 692f85fc3a5ff18e8dcb6fcce9da2d6894c58a79 Mon Sep 17 00:00:00 2001 From: Mazet Laurent Date: Mon, 7 Apr 2025 18:14:05 +0200 Subject: [PATCH] fix generic parsing --- clear_data.h | 4 ++-- morep_valid.c | 8 ++++---- parse.h | 29 ++++++++++++++++------------- raw_data.c | 1 + raw_data.h | 4 ++-- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/clear_data.h b/clear_data.h index 50ccfda..795e9ec 100644 --- a/clear_data.h +++ b/clear_data.h @@ -27,9 +27,9 @@ __BEGIN_DECLS */ typedef struct { uint8_t channelid; /**< channel index */ - uint8_t bypass_len; /**< attached bypass message length */ + int bypass_len; /**< attached bypass message length */ uint8_t bypass[255]; /**< attached bypass message */ - uint8_t data_len; /**< data length (must be aligned to 16 bytes) */ + int data_len; /**< data length (must be aligned to 16 bytes) */ uint8_t data[1472]; /**< data message */ } CLEAR_DATA_t; diff --git a/morep_valid.c b/morep_valid.c index 674e314..8b24ee1 100644 --- a/morep_valid.c +++ b/morep_valid.c @@ -66,7 +66,6 @@ char *read_stream (FILE *sd, int *plen) memset (buffer + size - BUFMAX - 1, 0, BUFMAX + 1); blocklen = fread (buffer + size - BUFMAX - 1, 1, BUFMAX, sd); length += blocklen; - VERBOSE (morep, DEBUG, PRINTF ("buffer: 0x%p 0x%p %d %d %d\n", buffer, buffer + size - BUFMAX - 1, (int)size, length, blocklen)); } while (blocklen > 0); /* check size */ @@ -287,13 +286,13 @@ int main (int argc, char **argv) VERBOSE (morep, DEBUG, PRINTF ("work with %c[%s]\n", comm->mode ? 'T' : 'R', comm->etype)); /* get values */ - char *tmp = line; + char *tmp = line + offset; TEST_CHARS (tmp, " \t", 0); - TEST_CHARS (tmp, " \t=", 1); if (strncmp (tmp, "MSG", 3) != 0) { - VERBOSE (morep, WARNING, PRINTF ("can't parse line '%s'\n", line)); + VERBOSE (morep, WARNING, PRINTF ("can't parse line '%s' (%s)\n", line, tmp)); continue; } + tmp += 3; TEST_CHARS (tmp, " \t=", 0); uint8_t msgtype = strtol (tmp, &tmp, 0); if ((*tmp != ' ') && (*tmp != '\t')) { @@ -306,6 +305,7 @@ int main (int argc, char **argv) VERBOSE (morep, WARNING, PRINTF ("can't parse line '%s'\n", line)); continue; } + VERBOSE (morep, TRACE, PRINTF ("payload length: %d\n", payload.data_len)); /* transmit */ if (mode == 1) { diff --git a/parse.h b/parse.h index 4608c7f..f190378 100644 --- a/parse.h +++ b/parse.h @@ -30,9 +30,9 @@ __BEGIN_DECLS @param str string to be parsed @param delim list of characters to test - @param test 0 for "contain only", 1 for "exclude" + @param stop 0 for "contain only", 1 for "exclude" */ -#define TEST_CHARS(str, delim, test) \ +#define TEST_CHARS(str, delim, stop) \ while (*str != '\0') { \ int i, stat = 0; \ for (i = 0; (delim[i] != '\0') && (!stat); i++) { \ @@ -40,7 +40,7 @@ __BEGIN_DECLS stat = 1; \ } \ } \ - if (stat == test) { \ + if (stat == stop) { \ break; \ } \ str++; \ @@ -56,7 +56,7 @@ __BEGIN_DECLS #define BEGIN_PARSE(line) \ { \ int rc = 0; \ - char *pt = (line); \ + char *pt = line; \ while (1) { \ /* find variable */ \ TEST_CHARS (pt, " \t", 0); \ @@ -65,6 +65,8 @@ __BEGIN_DECLS if (*var == '\0') { \ break; \ } \ + *pt++ = '\0'; \ + VERBOSE (morep, TRACE, PRINTF ("var: %s\n", var)); \ TEST_CHARS (pt, " \t=", 0); \ /* find value */ \ char *val = pt; \ @@ -74,6 +76,7 @@ __BEGIN_DECLS } \ pt++; \ } \ + VERBOSE (morep, TRACE, PRINTF ("val: %s\n", val)); \ if (*val == '\0') { \ rc = 1; \ break; \ @@ -101,7 +104,7 @@ __BEGIN_DECLS #define PARSEA(name, buf) \ else if (strcmp (var, name) == 0) { \ - buf##_len = parse_array (val, buf, sizeof (buf)); \ + buf##_len = parse_array (val, buf, sizeof (buf)); \ } /** @@ -109,14 +112,14 @@ __BEGIN_DECLS End of parsing macro */ -#define END_PARSE() \ - else { \ - VERBOSE (morep, WARNING, PRINTF ("can understand '%s'\n", var)); \ - rc = 1; \ - break; \ - } \ - } \ - return rc; \ +#define END_PARSE() \ + else { \ + VERBOSE (morep, WARNING, PRINTF ("can't understand '%s'\n", var)); \ + rc = 1; \ + break; \ + } \ + } \ + return rc; \ } /** diff --git a/raw_data.c b/raw_data.c index ee27ec3..00cc787 100644 --- a/raw_data.c +++ b/raw_data.c @@ -22,6 +22,7 @@ int parse_raw_data (char *line, RAW_DATA_t *out) { BEGIN_PARSE (line) PARSEA ("DATA", out->data) + PARSEA ("PAYLOAD", out->data) END_PARSE () } diff --git a/raw_data.h b/raw_data.h index c261cad..e4dc476 100644 --- a/raw_data.h +++ b/raw_data.h @@ -26,8 +26,8 @@ __BEGIN_DECLS Raw data type */ typedef struct { - uint8_t data_len; /**< data length*/ - uint8_t data[1496 * 16 - 1]; /**< data message */ + int data_len; /**< data length*/ + uint8_t data[1496]; /**< data message */ } RAW_DATA_t; /** -- 2.30.2