wip
authorMazet Laurent <laurent.mazet@thalesgroup.com>
Mon, 7 Apr 2025 17:26:40 +0000 (19:26 +0200)
committerMazet Laurent <laurent.mazet@thalesgroup.com>
Mon, 7 Apr 2025 17:26:40 +0000 (19:26 +0200)
morep_valid.c
parse.c
parse.h
payload.c [new file with mode: 0644]
payload.h [new file with mode: 0644]

index 8b24ee1c1609d8235e6a13906ff2961e1f0a5af2..cb7ef92dc3679ed1fe9094153316b71efddc2e91 100644 (file)
@@ -14,8 +14,8 @@
 
 /* depend: */
 /* cflags: */
-/* linker: morep.o parse.o raw_data.o */
-/* winlnk: morep.o parse.o raw_data.o */
+/* linker: morep.o parse.o payload.o raw_data.o */
+/* winlnk: morep.o parse.o payload.o raw_data.o */
 
 #include <errno.h>
 #include <signal.h>
 
 #include "morep.h"
 #include "parse.h"
-#include "raw_data.h"
 #include "verbose.h"
 
+#include "payload.h"
+#include "raw_data.h"
+
 char *progname = NULL;
 
 int stop = 0;
 
 DECLARE_VERBOSE_LEVEL (morep, INFO);
 
+#define MODERAW
+
 #define BUFMAX 4096
 
 void sig_handler (int sig)
@@ -287,6 +291,15 @@ int main (int argc, char **argv)
 
         /* get values */
         char *tmp = line + offset;
+        uint8_t msgtype;
+#ifdef MODERAW
+        PAYLOAD_t payload= {0};
+        if (parse_payload (tmp, &payload) != 0) {
+            VERBOSE (morep, WARNING, PRINTF ("can't parse line '%s'\n", line));
+            continue;
+        }
+        msgtype = payload.msgtype;
+#else /* ! MODERAW */
         TEST_CHARS (tmp, " \t", 0);
         if (strncmp (tmp, "MSG", 3) != 0) {
             VERBOSE (morep, WARNING, PRINTF ("can't parse line '%s' (%s)\n", line, tmp));
@@ -294,7 +307,7 @@ int main (int argc, char **argv)
         }
         tmp += 3;
         TEST_CHARS (tmp, " \t=", 0);
-        uint8_t msgtype = strtol (tmp, &tmp, 0);
+        msgtype = strtol (tmp, &tmp, 0);
         if ((*tmp != ' ') && (*tmp != '\t')) {
             VERBOSE (morep, WARNING, PRINTF ("can't parse line '%s'\n", line));
             continue;
@@ -305,6 +318,7 @@ int main (int argc, char **argv)
             VERBOSE (morep, WARNING, PRINTF ("can't parse line '%s'\n", line));
             continue;
         }
+#endif /* MODERAW */
         VERBOSE (morep, TRACE, PRINTF ("payload length: %d\n", payload.data_len));
 
         /* transmit */
@@ -335,7 +349,17 @@ int main (int argc, char **argv)
                 }
             }
             if (log) {
+#ifdef MODERAW
+                char buffer[64 + sizeof (payload.data)] = {0};
+                PAYLOAD_t _rxpayload;
+                _rxpayload.msgtype = rxmsgtype;
+                _rxpayload.data_len = rxlen;
+                memcpy (_rxpayload.data, rxpayload, rxlen);
+                format_payload (&_rxpayload, buffer, sizeof (buffer));
+                fprintf (log, "R%s %s\n", comm->etype, buffer);
+#else /* ! MODERAW */
                 print_message (log, comm->etype, 0, rxmsgtype, seqnum, rxpayload, rxlen);
+#endif /* MODERAW */
            }
         }
     }
diff --git a/parse.c b/parse.c
index 47c8e4d4e1b8fed2abd6d42ebaeae72d1cd09117..72bea91843049cc1f9b91c42932f2964d47d05a3 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -24,7 +24,7 @@ int parse_int (char *str)
 {
     char *pt = NULL;
     int val = strtol (str, &pt, 0);
-    if (*pt != '\0') {
+    if ((*pt != '\0') && (*pt != ' ') && (*pt != '\t')) {
         VERBOSE (morep, WARNING, PRINTF ("can't parse '%s' as integer\n", str));
     }
     return val;
@@ -34,7 +34,7 @@ double parse_double (char *str)
 {
     char *pt = NULL;
     double val = strtod (str, &pt);
-    if (*pt != '\0') {
+    if ((*pt != '\0') && (*pt != ' ') && (*pt != '\t')) {
         VERBOSE (morep, WARNING, PRINTF ("can't parse '%s' as double\n", str));
     }
     return val;
diff --git a/parse.h b/parse.h
index f19037834e81a287e591dd321de6a87c62514c35..e01570cdfc518f8af9d633df4f3c664923c28ab7 100644 (file)
--- a/parse.h
+++ b/parse.h
@@ -53,33 +53,33 @@ __BEGIN_DECLS
 
    @param line string to be parsed
 */
-#define BEGIN_PARSE(line)                              \
-{                                                      \
-    int rc = 0;                                        \
-    char *pt = line;                                   \
-    while (1) {                                        \
-        /* find variable */                            \
-        TEST_CHARS (pt, " \t", 0);                     \
-        char *var = pt;                                \
-        TEST_CHARS (pt, " \t=", 1);                    \
-        if (*var == '\0') {                            \
-            break;                                     \
-        }                                              \
-        *pt++ = '\0';                                  \
+#define BEGIN_PARSE(line)                                  \
+{                                                          \
+    int rc = 0;                                            \
+    char *pt = line;                                       \
+    while (1) {                                            \
+        /* find variable */                                \
+        TEST_CHARS (pt, " \t", 0);                         \
+        char *var = pt;                                    \
+        TEST_CHARS (pt, " \t=", 1);                        \
+        if (*var == '\0') {                                \
+            break;                                         \
+        }                                                  \
+        *pt++ = '\0';                                      \
         VERBOSE (morep, TRACE, PRINTF ("var: %s\n", var)); \
-        TEST_CHARS (pt, " \t=", 0);                    \
-        /* find value */                               \
-        char *val = pt;                                \
-        while (*pt != '\0') {                          \
-            if ((*pt == ' ') && (*(pt - 1) != '\\')) { \
-                break;                                 \
-            }                                          \
-            pt++;                                      \
-        }                                              \
+        TEST_CHARS (pt, " \t=", 0);                        \
+        /* find value */                                   \
+        char *val = pt;                                    \
+        while (*pt != '\0') {                              \
+            if ((*pt == ' ') && (*(pt - 1) != '\\')) {     \
+                break;                                     \
+            }                                              \
+            pt++;                                          \
+        }                                                  \
         VERBOSE (morep, TRACE, PRINTF ("val: %s\n", val)); \
-        if (*val == '\0') {                            \
-            rc = 1;                                    \
-            break;                                     \
+        if (*val == '\0') {                                \
+            rc = 1;                                        \
+            break;                                         \
         }
 
 /**
@@ -90,20 +90,21 @@ __BEGIN_DECLS
    @param name field name
    @param buf preallocated storage
 */
-#define PARSE(name, buf)                                                     \
-        else if (strcmp (var, name) == 0) {                                  \
-            _Generic ((buf),                                          \
-                uint8_t: buf = parse_int (val), \
-                int8_t: buf = parse_int (val), \
-                uint16_t: buf = parse_int (val), \
-                int16_t: buf = parse_int (val), \
-                uint32_t: buf = parse_int (val), \
-                float: buf = parse_double (val), \
-                double: buf = parse_double (val))i; \
+#define PARSE(name, buf)                           \
+        else if (strcmp (var, name) == 0) {        \
+            _Generic ((buf),                       \
+                uint8_t: buf = parse_int (val),    \
+                int8_t: buf = parse_int (val),     \
+                uint16_t: buf = parse_int (val),   \
+                int16_t: buf = parse_int (val),    \
+                uint32_t: buf = parse_int (val),   \
+                int32_t: buf = parse_int (val),    \
+                float: buf = parse_double (val),   \
+                double: buf = parse_double (val)); \
         }
 
-#define PARSEA(name, buf) \
-        else if (strcmp (var, name) == 0) {                      \
+#define PARSEA(name, buf)                                     \
+        else if (strcmp (var, name) == 0) {                   \
             buf##_len = parse_array (val, buf, sizeof (buf)); \
         }
 
@@ -143,15 +144,18 @@ __BEGIN_DECLS
    @param name field name
    @param field data from structure
 */
-#define FORMAT(name, field)                                                           \
-    _Generic (typeof (field),                                                         \
-        uint8_t: len += snprintf (_buffer, _maxlen - len, "%d", field),               \
-        int8_t: len += snprintf (_buffer, _maxlen - len, "%d", field),                \
-        uint16_t: len += snprintf (_buffer, _maxlen - len, "%d", field),              \
-        int16_t: len += snprintf (_buffer, _maxlen - len, "%d", field),               \
-        uint8_t: len += snprintf (_buffer, _maxlen - len, "%d", field));              \
-    if (len == _maxlen) {                                                             \
-        VERBOSE (morep, WARNING, PRINTF ("field '%s' too large\n", #field));          \
+#define FORMAT(name, field)                                                  \
+    _Generic (field,                                                         \
+        uint8_t: len += snprintf (_buffer, _maxlen - len, "%d", field),      \
+        int8_t: len += snprintf (_buffer, _maxlen - len, "%d", field),       \
+        uint16_t: len += snprintf (_buffer, _maxlen - len, "%d", field),     \
+        int16_t: len += snprintf (_buffer, _maxlen - len, "%d", field),      \
+        uint32_t: len += snprintf (_buffer, _maxlen - len, "%d", field),     \
+        int32_t: len += snprintf (_buffer, _maxlen - len, "%d", field),      \
+        float: len += snprintf (_buffer, _maxlen - len, "%g", field),        \
+        double: len += snprintf (_buffer, _maxlen - len, "%g", field));      \
+    if (len == _maxlen) {                                                    \
+        VERBOSE (morep, WARNING, PRINTF ("field '%s' too large\n", #field)); \
     }
 
 #define FORMATA(name, field)                                                 \
diff --git a/payload.c b/payload.c
new file mode 100644 (file)
index 0000000..4a6d690
--- /dev/null
+++ b/payload.c
@@ -0,0 +1,37 @@
+/*
+  File name        : payload.c
+  Projet           : MERLIN
+  Date of creation : 2025/04/07
+  Version          : 1.0
+  Copyright        : Thales SIX
+  Author           : Laurent Mazet <laurent.mazet@thalesgroup.com>
+
+  Description      : This file contains definition of a raw payloaddata type
+
+  History          :
+  - initial version
+*/
+
+#include <stdint.h>
+
+#include "parse.h"
+
+#include "payload.h"
+
+int parse_payload (char *line, PAYLOAD_t *out)
+{
+    BEGIN_PARSE (line)
+    PARSE ("MSG", out->msgtype)
+    PARSEA ("PAYLOAD", out->data)
+    END_PARSE ()
+}
+
+int format_payload (PAYLOAD_t *in, char *buffer, int maxlen)
+{
+    BEGIN_FORMAT (buffer, maxlen)
+    FORMAT ("MSG", in->msgtype)
+    FORMATA ("PAYLOAD", in->data)
+    END_FORMAT ()
+}
+
+/* vim: set ts=4 sw=4 si et: */
diff --git a/payload.h b/payload.h
new file mode 100644 (file)
index 0000000..b53a329
--- /dev/null
+++ b/payload.h
@@ -0,0 +1,59 @@
+/*
+  File name        : payload.h
+  Projet           : MERLIN
+  Date of creation : 2025/04/07
+  Version          : 1.0
+  Copyright        : Thales SIX
+  Author           : Laurent Mazet <laurent.mazet@thalesgroup.com>
+
+  Description      : This file contains definition of a raw paylad
+
+  History          :
+  - initial version
+*/
+
+#ifndef __PAYLOAD_H__
+#define __PAYLOAD_H__
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+/**
+   @ingroup MESSAGES
+
+   Raw data type
+*/
+typedef struct {
+    uint8_t msgtype; /**< message type */
+    int data_len; /**< data length*/
+    uint8_t data[1496]; /**< data message */
+} PAYLOAD_t;
+
+/**
+   @ingroup MESSAGES
+
+   Parse a string containing a raw payload
+
+   @param line string to analyse
+   @param out output structure
+*/
+int parse_payload (char *line, PAYLOAD_t *out);
+
+/**
+   @ingroup MESSAGES
+
+   Format payload into a string
+
+   @param in input structure
+   @param buffer output string
+   @param maxlen buffer limit
+*/
+int format_payload (PAYLOAD_t *in, char *buffer, int maxlen);
+
+__END_DECLS
+
+#endif /* __PAYLOAD_H__ */
+
+/* vim: set ts=4 sw=4 si et: */