From 6ac689fd0c5f486bca61b672fbf668e9db2ccc84 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sat, 25 Nov 2023 01:03:52 -0800 Subject: [PATCH] new parser can write SD log (#307) * stub sd logs * framing for SD log output * make old and new match * only grab one struct, prefix * sd log seems to just work? * wiring * wiring * wiring * array-of-struct handling * handle array of struct * array of structs doesn't really work right * sd log end file --- .../com/rusefi/ldmp/LiveDataProcessor.java | 7 +++ .../rusefi/newparse/layout/ArrayLayout.java | 5 ++ .../com/rusefi/newparse/layout/Layout.java | 23 ++++++++- .../rusefi/newparse/layout/ScalarLayout.java | 47 +++++++++++++++++++ .../rusefi/newparse/layout/StructLayout.java | 29 ++++++++++++ .../newparse/layout/StructNamePrefixer.java | 7 ++- .../rusefi/newparse/outputs/SdLogWriter.java | 36 ++++++++++++++ .../rusefi/output/SdCardFieldsConsumer.java | 2 +- 8 files changed, 152 insertions(+), 4 deletions(-) create mode 100644 java_tools/configuration_definition/src/main/java/com/rusefi/newparse/outputs/SdLogWriter.java diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/ldmp/LiveDataProcessor.java b/java_tools/configuration_definition/src/main/java/com/rusefi/ldmp/LiveDataProcessor.java index bbb10d72f7..d73fbcf4fc 100644 --- a/java_tools/configuration_definition/src/main/java/com/rusefi/ldmp/LiveDataProcessor.java +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/ldmp/LiveDataProcessor.java @@ -97,6 +97,8 @@ private int handleYaml(Map data) throws IOException { // tsOutputsDestination + File.separator + "/data_logs.ini" // ); + // SdLogWriter sdLogWriter = new SdLogWriter("console/binary_log/log_fields_generated.h"); + EntryHandler handler = new EntryHandler() { @Override public void onEntry(String name, String javaName, String folder, String prepend, boolean withCDefines, String[] outputNames, String constexpr, String conditional, Boolean isPtr) throws IOException { @@ -163,6 +165,10 @@ public void onEntry(String name, String javaName, String folder, String prepend, // outputChannelWriter.writeOutputChannels(parseState, outputNames[i]); // } // } + + // if (constexpr != null) { + // sdLogWriter.writeSdLogs(parseState, constexpr + (isPtr ? "->" : ".")); + // } } state.doJob(); @@ -241,6 +247,7 @@ public void onEntry(String name, String javaName, String folder, String prepend, lazyFile.close(); outputValueConsumer.endFile(); + // sdLogWriter.endFile(); // return outputChannelWriter.getSize(); return outputsSections.sensorTsPosition; diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/ArrayLayout.java b/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/ArrayLayout.java index 55eda8322d..ce10eea005 100644 --- a/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/ArrayLayout.java +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/ArrayLayout.java @@ -83,4 +83,9 @@ public void writeCOffsetCheck(PrintStream ps, String parentTypeName) { protected void writeOutputChannelLayout(PrintStream ps, PrintStream psDatalog, StructNamePrefixer prefixer, int offsetAdd) { this.prototypeLayout.writeOutputChannelLayout(ps, psDatalog, prefixer, offsetAdd, this.length); } + + @Override + protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName) { + this.prototypeLayout.writeSdLogLayout(ps, prefixer, sourceName, this.length); + } } diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/Layout.java b/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/Layout.java index ccfed6c217..eb38adb8e9 100644 --- a/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/Layout.java +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/Layout.java @@ -28,7 +28,7 @@ public String toString() { } public final void writeTunerstudioLayout(PrintStream ps, TsMetadata meta) { - writeTunerstudioLayout(ps, meta, new StructNamePrefixer(), 0); + writeTunerstudioLayout(ps, meta, new StructNamePrefixer('_'), 0); } protected void writeTunerstudioLayout(PrintStream ps, TsMetadata meta, StructNamePrefixer prefixer, int offsetAdd) {} @@ -68,7 +68,7 @@ public void writeCLayout(PrintStream ps, int[] arrayLength) { public void writeCOffsetCheck(PrintStream ps, String parentTypeName) { } public void writeOutputChannelLayout(PrintStream ps, PrintStream psDatalog, String prefix, int offsetAdd) { - StructNamePrefixer prefixer = new StructNamePrefixer(); + StructNamePrefixer prefixer = new StructNamePrefixer('_'); if (prefix != null) { prefixer.push(prefix); @@ -91,4 +91,23 @@ protected static void writeDatalogName(PrintStream ps, String name, String comme ps.print(text); } + + public void writeSdLogLayout(PrintStream ps, String sourceName) { + // TODO + final String prefix = null; + + StructNamePrefixer prefixer = new StructNamePrefixer('.'); + + if (prefix != null) { + prefixer.push(prefix); + } + + writeSdLogLayout(ps, prefixer, sourceName); + } + + protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName) { } + + protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName, int[] arrayLength) { + throw new IllegalStateException("This type can't be in an array!"); + } } diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/ScalarLayout.java b/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/ScalarLayout.java index a1dfa4affb..3eed7a4376 100644 --- a/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/ScalarLayout.java +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/ScalarLayout.java @@ -217,4 +217,51 @@ protected void writeOutputChannelLayout(PrintStream ps, PrintStream psDatalog, S elementOffset += type.size; } } + + @Override + protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName) { + writeSdLogLayout(ps, prefixer, "", "", sourceName); + } + + @Override + protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName, int[] arrayLength) { + if (arrayLength.length != 1) { + throw new IllegalStateException("Output channels don't support multi dimension arrays"); + } + + for (int i = 0; i < arrayLength[0]; i++) { + writeSdLogLayout(ps, prefixer, "[" + i + "]", " " + (i + 1), sourceName); + } + } + + private void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String arraySub, String commentSuffix, String sourceName) { + if (this.name.startsWith("unused")) { + return; + } + + // {engine->outputChannels.mafMeasured, "MAF", "kg/h", 1}, + + final String prefixedName = prefixer.get(this.name); + + ps.print("\t{"); + ps.print(sourceName); + ps.print(prefixedName); + ps.print(arraySub); + ps.print(", \""); + + String comment = this.options.comment; + + // default to name in case of no comment + if (comment == null || comment.length() == 0) { + comment = prefixedName; + } + + ps.print(comment); + ps.print(commentSuffix); + ps.print("\", "); + ps.print(this.options.units); + ps.print(", "); + ps.print(this.options.digits); + ps.println("},"); + } } diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/StructLayout.java b/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/StructLayout.java index ca546ae3cd..140dcc6891 100644 --- a/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/StructLayout.java +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/StructLayout.java @@ -226,4 +226,33 @@ protected void writeOutputChannelLayout(PrintStream ps, PrintStream psDatalog, S elementOffset += this.size; } } + + private void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName, String name) { + if (!this.noPrefix) { + prefixer.push(name); + } + + this.children.forEach(c -> c.writeSdLogLayout(ps, prefixer, sourceName)); + + if (!this.noPrefix) { + prefixer.pop(); + } + } + + @Override + protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName) { + writeSdLogLayout(ps, prefixer, sourceName, this.name); + } + + @Override + protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName, int[] arrayLength) { + if (arrayLength.length != 1) { + throw new IllegalStateException("Output channels don't support multi dimension arrays"); + } + + // TODO: This doesn't quite work, as it's unclear how to make automatic naming work properly + // for (int i = 0; i < arrayLength[0]; i++) { + // writeSdLogLayout(ps, prefixer, sourceName, this.name + "[" + i + "]"); + // } + } } diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/StructNamePrefixer.java b/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/StructNamePrefixer.java index 68f6caba2f..06e35b518c 100644 --- a/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/StructNamePrefixer.java +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/layout/StructNamePrefixer.java @@ -5,6 +5,11 @@ public class StructNamePrefixer { private final Stack stack = new Stack<>(); private int idx = -1; + private final char prefixChar; + + public StructNamePrefixer(char prefixChar) { + this.prefixChar = prefixChar; + } public void pop() { stack.pop(); @@ -18,7 +23,7 @@ public void push(String name) { this.idx = -1; } - stack.push(name + "_"); + stack.push(name + this.prefixChar); } void setIndex(int idx) { diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/outputs/SdLogWriter.java b/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/outputs/SdLogWriter.java new file mode 100644 index 0000000000..b5ca6e3516 --- /dev/null +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/newparse/outputs/SdLogWriter.java @@ -0,0 +1,36 @@ +package com.rusefi.newparse.outputs; + +import com.rusefi.newparse.ParseState; +import com.rusefi.newparse.layout.StructLayout; +import com.rusefi.newparse.parsing.Struct; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.PrintStream; + +public class SdLogWriter { + private final PrintStream ps; + + public SdLogWriter(String outputFile) throws FileNotFoundException { + this(new PrintStreamAlwaysUnix(new FileOutputStream(outputFile))); + } + + public SdLogWriter(PrintStream ps) { + this.ps = ps; + + ps.println("static constexpr LogField fields[] = {"); + ps.println("\t{packedTime, GAUGE_NAME_TIME, \"sec\", 0},"); + } + + public void endFile() { + ps.println("};"); + } + + public void writeSdLogs(ParseState parser, String sourceName) { + // Assume the last struct is the one we want... + Struct s = parser.getStructs().get(parser.getStructs().size() - 1); + + StructLayout sl = new StructLayout(0, "root", s); + sl.writeSdLogLayout(ps, sourceName); + } +} diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/output/SdCardFieldsConsumer.java b/java_tools/configuration_definition/src/main/java/com/rusefi/output/SdCardFieldsConsumer.java index da869f6a01..b15438aacf 100644 --- a/java_tools/configuration_definition/src/main/java/com/rusefi/output/SdCardFieldsConsumer.java +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/output/SdCardFieldsConsumer.java @@ -25,7 +25,7 @@ public void endFile() throws IOException { public static void wrapContent(LazyFile output, String content) { output.write("static constexpr LogField fields[] = {\n" + - "{packedTime, GAUGE_NAME_TIME, \"sec\", 0},\n"); + "\t{packedTime, GAUGE_NAME_TIME, \"sec\", 0},\n"); output.write(content); output.write("};\n"); }