Skip to content

Fix memory corruption (marker was getting overflowed because #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void CoderApp::GenerateCode()
CiMainGenerator cigen;
CiUtilGenerator ciugen;
FsCreator fscreator;
std::string path;

std::ifstream reader;

Expand All @@ -57,9 +58,18 @@ void CoderApp::GenerateCode()
std::string info("");

// create main destination directory
fscreator.Configure(Params.drvname.first, Params.outdir.first, info, scanner.dblist.ver.hi, scanner.dblist.ver.low);
if (!Params.is_rewrite)
{
path = fscreator.FindPath(Params.outdir.first);
}
else
{
path = Params.outdir.first;
}

fscreator.Configure(Params.drvname.first, path, info, scanner.dblist.ver.hi, scanner.dblist.ver.low);

auto ret = fscreator.PrepareDirectory(Params.is_rewrite);
auto ret = fscreator.PrepareDirectory();

fscreator.FS.gen.no_config = Params.is_noconfig;
fscreator.FS.gen.no_inc = Params.is_nocanmon;
Expand Down Expand Up @@ -120,10 +130,10 @@ void CoderApp::GenerateCode()
fscreator.FS.file.util_c.dir = fscreator.FS.file.utildir;
fscreator.FS.file.util_h.dir = fscreator.FS.file.utildir;

fscreator.FS.file.util_h.fname = str_tolower(fscreator.FS.gen.drvname + "-binutil.h");
fscreator.FS.file.util_h.fname = str_tolower(fscreator.FS.gen.drvname + "-binutil.hpp");
fscreator.FS.file.util_h.fpath = fscreator.FS.file.utildir + "/" + fscreator.FS.file.util_h.fname;

fscreator.FS.file.util_c.fname = str_tolower(fscreator.FS.gen.drvname + "-binutil.c");
fscreator.FS.file.util_c.fname = str_tolower(fscreator.FS.gen.drvname + "-binutil.cpp");
fscreator.FS.file.util_c.fpath = fscreator.FS.file.utildir + "/" + fscreator.FS.file.util_c.fname;

MsgsClassification groups;
Expand Down Expand Up @@ -197,8 +207,8 @@ void CoderApp::PrintHelp()
std::cout << " \t\t '-rw' option enables rewriting: all source files previously generated" << std::endl;
std::cout << " \t\t will be replaced by new ones" << std::endl;
std::cout << " -noconfig:\t no {drivername}-config and dbccodeconfig generation" << std::endl;
std::cout << " -noinc:\t no canmonitorutil.h generation" << std::endl;
std::cout << " -nofmon:\t no ***-fmon.c generation" << std::endl;
std::cout << " -noinc:\t no canmonitorutil.hpp generation" << std::endl;
std::cout << " -nofmon:\t no ***-fmon.cpp generation" << std::endl;
std::cout << std::endl;

std::cout << "examples:" << std::endl;
Expand Down
38 changes: 11 additions & 27 deletions src/codegen/c-main-generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const char* extend_func_body =
"// n+1 bits where n is the largest signed signal width. For example if\n"
"// the most wide signed signal has a width of 31 bits you need to set\n"
"// bitext_t as int32_t and ubitext_t as uint32_t\n"
"// Defined these typedefs in @dbccodeconf.h or locally in 'dbcdrvname'-config.h\n"
"// Defined these typedefs in @dbccodeconf.hpp or locally in 'dbcdrvname'-config.hpp\n"
"static bitext_t %s(ubitext_t val, uint8_t bits)\n"
"{\n"
" ubitext_t const m = 1u << (bits - 1);\n"
Expand Down Expand Up @@ -66,10 +66,10 @@ void CiMainGenerator::Generate(DbcMessageList_t& dlist, const AppSettings_t& fsd

if (!fsd.gen.no_config)
{
// 6 step is to print template for drv-config.h
// 6 step is to print template for drv-config.hpp
Gen_ConfigHeader();

// 8 step is to print dbccodeconf.h template
// 8 step is to print dbccodeconf.hpp template
Gen_DbcCodeConf();
}

Expand All @@ -93,8 +93,6 @@ void CiMainGenerator::Gen_MainHeader()

fwriter.Append("#pragma once");
fwriter.Append();
fwriter.Append("#ifdef __cplusplus\nextern \"C\" {\n#endif");
fwriter.Append();
fwriter.Append("#include <stdint.h>");
fwriter.Append();

Expand All @@ -104,15 +102,15 @@ void CiMainGenerator::Gen_MainHeader()
fwriter.Append();

fwriter.Append("// include current dbc-driver compilation config");
fwriter.Append("#include \"%s-config.h\"", fdesc->gen.drvname.c_str());
fwriter.Append("#include \"%s-config.hpp\"", fdesc->gen.drvname.c_str());
fwriter.Append();

fwriter.Append("#ifdef %s", fdesc->gen.usemon_def.c_str());

fwriter.Append(
"// This file must define:\n"
"// base monitor struct\n"
"#include \"canmonitorutil.h\"\n"
"#include <nv1_can_gen/src/canmonitorutil.hpp>\n"
"\n"
);

Expand Down Expand Up @@ -169,10 +167,7 @@ void CiMainGenerator::Gen_MainHeader()
fwriter.Append("#define %s_DLC (%uU)", m.Name.c_str(), m.DLC);
fwriter.Append("#define %s_CANID (%#xU)", m.Name.c_str(), m.MsgID);

if (m.Cycle > 0)
{
fwriter.Append("#define %s_CYC (%dU)", m.Name.c_str(), m.Cycle);
}
fwriter.Append("#define %s_CYC (%dU)", m.Name.c_str(), m.Cycle);

size_t max_sig_name_len = 27;

Expand Down Expand Up @@ -328,8 +323,6 @@ void CiMainGenerator::Gen_MainHeader()
fwriter.Append();
}

fwriter.Append("#ifdef __cplusplus\n}\n#endif");

// save fwrite cached text to file
fwriter.Flush(fdesc->file.core_h.fpath);
}
Expand Down Expand Up @@ -362,15 +355,15 @@ void CiMainGenerator::Gen_MainSource()
"// Function prototypes to be called each time CAN frame is unpacked\n"
"// FMon function may detect RC, CRC or DLC violation\n");

fwriter.Append("#include \"%s-fmon.h\"", fdesc->gen.drvname.c_str());
fwriter.Append("#include \"%s-fmon.hpp\"", fdesc->gen.drvname.c_str());
fwriter.Append();

fwriter.Append("#endif // %s", fdesc->gen.usemon_def.c_str());
fwriter.Append("");
fwriter.Append("// This macro guard for the case when you need to enable");
fwriter.Append("// using diag monitors but there is no necessity in proper");
fwriter.Append("// SysTick provider. For providing one you need define macro");
fwriter.Append("// before this line - in dbccodeconf.h");
fwriter.Append("// before this line - in dbccodeconf.hpp");
fwriter.Append("");
fwriter.Append("#ifndef GetSystemTick");
fwriter.Append("#define GetSystemTick() (0u)");
Expand Down Expand Up @@ -452,7 +445,7 @@ void CiMainGenerator::Gen_ConfigHeader()
ConfigGenerator confgen;
confgen.FillHeader(fwriter, fdesc->gen);

fwriter.Flush(fdesc->file.confdir + '/' + fdesc->gen.drvname + "-config.h");
fwriter.Flush(fdesc->file.confdir + '/' + fdesc->gen.drvname + "-config.hpp");
}

void CiMainGenerator::Gen_FMonHeader()
Expand All @@ -475,10 +468,6 @@ void CiMainGenerator::Gen_CanMonUtil()
fwriter.Append("");
fwriter.Append("#include <stdint.h>");
fwriter.Append("");
fwriter.Append("#ifdef __cplusplus");
fwriter.Append("extern \"C\" {");
fwriter.Append("#endif");
fwriter.Append("");
fwriter.Append("// declare here all availible checksum algorithms");
fwriter.Append("typedef enum");
fwriter.Append("{");
Expand Down Expand Up @@ -520,12 +509,8 @@ void CiMainGenerator::Gen_CanMonUtil()
fwriter.Append("");
fwriter.Append("} FrameMonitor_t;");
fwriter.Append("");
fwriter.Append("#ifdef __cplusplus");
fwriter.Append("}");
fwriter.Append("#endif");
fwriter.Append("");

fwriter.Flush(fdesc->file.incdir + '/' + "canmonitorutil.h");
fwriter.Flush(fdesc->file.incdir + '/' + "canmonitorutil.hpp");
}

void CiMainGenerator::Gen_DbcCodeConf()
Expand Down Expand Up @@ -562,7 +547,7 @@ void CiMainGenerator::Gen_DbcCodeConf()
fwriter.Append("// #define GetFrameHash(a,b,c,d,e) __get_hash__(a,b,c,d,e)");
fwriter.Append("");

fwriter.Flush(fdesc->file.confdir + '/' + "dbccodeconf.h");
fwriter.Flush(fdesc->file.confdir + '/' + "dbccodeconf.hpp");
}

void CiMainGenerator::WriteSigStructField(const SignalDescriptor_t& sig, bool bits, size_t padwidth)
Expand Down Expand Up @@ -879,4 +864,3 @@ void CiMainGenerator::PrintPackCommonText(const std::string& arrtxt, const CiExp
fwriter.Append();
}
}

9 changes: 2 additions & 7 deletions src/codegen/c-util-generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,12 @@ void CiUtilGenerator::PrintHeader()
tof.Append("#pragma once");
tof.Append();

tof.Append(openguard);
tof.Append();

// include common dbc code config header
tof.Append("#include \"dbccodeconf.h\"");
tof.Append("#include <nv1_can_gen/src/dbccodeconf.hpp>");
tof.Append();

// include c-main driver header
tof.Append("#include \"%s.h\"", file_drvname.c_str());
tof.Append("#include \"%s.hpp\"", file_drvname.c_str());
tof.Append();


Expand Down Expand Up @@ -190,8 +187,6 @@ void CiUtilGenerator::PrintHeader()
tof.Append();
}

tof.Append(closeguard);

tof.Flush(fdesc->util_h.fpath);
}

Expand Down
2 changes: 1 addition & 1 deletion src/codegen/c-util-generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CiUtilGenerator {
// the output of this function source files which contain:
// - global TX and RX typedefs message struct
// - function to Unpack incoming frame to dedicated RX message struct field
// - optional (through define in global "dbccodeconf.h") variable allocation in source files
// - optional (through define in global "dbccodeconf.hpp") variable allocation in source files
//
void Generate(DbcMessageList_t& dlist, const AppSettings_t& fsd,
const MsgsClassification& groups, const std::string& drvname);
Expand Down
14 changes: 7 additions & 7 deletions src/codegen/config-generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void ConfigGenerator::FillHeader(FileWriter& wr, const GenDescriptor_t& gsett)
wr.Append("#pragma once");
wr.Append("");
wr.Append("/* include common dbccode configurations */");
wr.Append("#include \"dbccodeconf.h\"");
wr.Append("#include <nv1_can_gen/src/dbccodeconf.hpp>");
wr.Append("");
wr.Append("");
wr.Append("/* ------------------------------------------------------------------------- *");
Expand All @@ -28,7 +28,7 @@ void ConfigGenerator::FillHeader(FileWriter& wr, const GenDescriptor_t& gsett)
wr.Append(" u8 Data[8] (CAN Frame payload data)");
wr.Append(" u8 IDE (CAN Frame Extended (1) / Standard (0) ID type)");
wr.Append("");
wr.Append(" This struct definition have to be placed (or be included) in dbccodeconf.h */");
wr.Append(" This struct definition have to be placed (or be included) in dbccodeconf.hpp */");
wr.Append("");
wr.Append("/* #define %s */", gsett.usesruct_def.c_str());
wr.Append(2);
Expand All @@ -43,7 +43,7 @@ void ConfigGenerator::FillHeader(FileWriter& wr, const GenDescriptor_t& gsett)
wr.Append(" 1. All the '_ro' fields will have a pair field with '_phys' postfix.");
wr.Append(" If only offset != 0 is true then the type of '_phys' signal is the same");
wr.Append(" as '_ro' signal. In other case the type will be @sigfloat_t which");
wr.Append(" have to be defined in user dbccodeconf.h");
wr.Append(" have to be defined in user dbccodeconf.hpp");
wr.Append("");
wr.Append(" 2. In pack function '_ro' signal will be rewritten by '_phys' signal, which");
wr.Append(" requires from user to use ONLY '_phys' signal for packing frame");
Expand All @@ -55,15 +55,15 @@ void ConfigGenerator::FillHeader(FileWriter& wr, const GenDescriptor_t& gsett)
wr.Append(2);

wr.Append("/* ------------------------------------------------------------------------- *");
wr.Append(" Note(!) that the \"canmonitorutil.h\" must be accessed in include path:");
wr.Append(" Note(!) that the \"canmonitorutil.hpp\" must be accessed in include path:");
wr.Append("");
wr.Append(" This macro adds:");
wr.Append("");
wr.Append(" - monitor field @mon1 to message struct");
wr.Append("");
wr.Append(" - capture system tick in unpack function and save value to mon1 field");
wr.Append(" to provide to user better missing frame detection code. For this case");
wr.Append(" user must provide function declared in canmonitorutil.h - GetSysTick()");
wr.Append(" user must provide function declared in canmonitorutil.hpp - GetSysTick()");
wr.Append(" which may return 1ms uptime.");
wr.Append("");
wr.Append(" - calling function FMon_*** (from 'fmon' driver) inside unpack function");
Expand Down Expand Up @@ -95,8 +95,8 @@ void ConfigGenerator::FillHeader(FileWriter& wr, const GenDescriptor_t& gsett)
wr.Append(" - \"Checksum\": constant marker word");
wr.Append("");
wr.Append(" - \"XOR8\": type of method, this text will be passed to GetFrameHash");
wr.Append(" (canmonitorutil.h) function as is, the best use case is to define 'enum");
wr.Append(" DbcCanCrcMethods' in canmonitorutil.h file with all possible");
wr.Append(" (canmonitorutil.hpp) function as is, the best use case is to define 'enum");
wr.Append(" DbcCanCrcMethods' in canmonitorutil.hpp file with all possible");
wr.Append(" checksum algorithms (e.g. XOR8, XOR4 etc)");
wr.Append("");
wr.Append(" - \"3\": optional value that will be passed to GetFrameHash as integer value");
Expand Down
Loading