Skip to content

Issue#9 and other things #10

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

Merged
merged 17 commits into from
May 7, 2022
Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
**/build
**/build*
loc-test*
17 changes: 17 additions & 0 deletions docs/RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Changelog

## v2.2 2022-05-07

### Fixed
- "C valid name" check for main driver name and value table descriptions
- Fixed some minor printing artefacts (extra whitespaces, extra lines)
- Very strange issue with wrong naming through all bunch of __***-binutil__ drivers (WTF?)


### Added
- Values from value tables and it's descpriptions are printed as macroses
([issue#9](https://github.com/astand/c-coderdbc/issues/9) from [@delipl](https://github.com/delipl))
- Added more information about __\_\_ext_sig\_\___ function
- Added strong check of matching of the versions of secondary dbc source
files to main dbc source file (drvname.h)
- Sources which are presumed to be located in __include__ directory are in square brakets

---

## v2.1 2022-02-19

Expand Down
2 changes: 1 addition & 1 deletion run-test-gen.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
start "coder" "build/Release/coderdbc.exe" -dbc test\testdb.dbc -out -out test\gencode -drvname testdb -nodeutils -rw
start "coder" "build-win/Release/coderdbc.exe" -dbc test\testdb.dbc -out -out test\gencode -drvname testdb -nodeutils -rw
pause
83 changes: 66 additions & 17 deletions src/codegen/c-main-generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
const char* ext_sig_func_name = "__ext_sig__";

const char* extend_func_body =
"// To compile this function you need to typedef 'bitext_t' and 'ubitext_t'\n"
"// globally in @dbccodeconf.h or locally in 'dbcdrvname'-config.h\n"
"// Type selection may affect common performance. Most useful types are:\n"
"// bitext_t : int64_t and ubitext_t : uint64_t\n"
"static bitext_t %s( ubitext_t val, uint8_t bits )\n"
"// This function performs extension of sign for the signals\n"
"// which have non-aligned to power of 2 bit's width.\n"
"// The types 'bitext_t' and 'ubitext_t' define maximal bit width which\n"
"// can be correctly handled. You need to select type which can contain\n"
"// 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"
"static bitext_t %s(ubitext_t val, uint8_t bits)\n"
"{\n"
" ubitext_t const m = 1u << (bits - 1);\n"
" return (val ^ m) - m;\n"
Expand Down Expand Up @@ -86,7 +90,7 @@ void CiMainGenerator::Gen_MainHeader()
fwriter->AppendLine(StrPrint("#define %s (%uU)", fdesc->verlow_def.c_str(), p_dlist->ver.low), 2);

fwriter->AppendLine("// include current dbc-driver compilation config");
fwriter->AppendLine(StrPrint("#include \"%s-config.h\"", fdesc->drvname.c_str()), 2);
fwriter->AppendLine(StrPrint("#include <%s-config.h>", fdesc->drvname.c_str()), 2);

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

Expand All @@ -95,7 +99,7 @@ void CiMainGenerator::Gen_MainHeader()
"// base monitor struct\n"
"// function signature for HASH calculation: (@GetFrameHash)\n"
"// function signature for getting system tick value: (@GetSystemTick)\n"
"#include \"canmonitorutil.h\"\n"
"#include <canmonitorutil.h>\n"
"\n"
);

Expand Down Expand Up @@ -137,6 +141,30 @@ void CiMainGenerator::Gen_MainHeader()
{
max_sig_name_len = s.Name.size();
}

// For each signal in current message print value tables definitions
if (s.ValDefs.vpairs.size() > 0)
{
fwriter->AppendLine(StrPrint("\n// Value tables for @%s signal", s.Name.c_str()), 2);

for (auto i = 0; i < s.ValDefs.vpairs.size(); i++)
{
// The value table definition consists of 'signal name + message name + value definition'
// This provides reliable way of avoiding issues with same macros names
std::string defname = StrPrint("%s_%s_%s", s.Name.c_str(), m.Name.c_str(), s.ValDefs.vpairs[i].first.c_str());

// @ifndef guard for the case when different values of table have
// the same name (it is valid for DBC file format)
// For this case only one of same named values will be available as macro
fwriter->AppendLine(StrPrint("#ifndef %s", defname.c_str()));

fwriter->AppendLine(StrPrint("#define %s_%s_%s (%d)",
s.Name.c_str(), m.Name.c_str(), s.ValDefs.vpairs[i].first.c_str(),
s.ValDefs.vpairs[i].second));

fwriter->AppendLine(StrPrint("#endif"), 2);
}
}
}

fwriter->AppendText("\n");
Expand Down Expand Up @@ -238,6 +266,13 @@ void CiMainGenerator::Gen_MainSource()
// include main header file
fwriter->AppendLine(StrPrint("#include \"%s\"", fdesc->core_h.fname.c_str()), 3);

fwriter->AppendLine("// DBC file version");
fwriter->AppendLine(StrPrint("#if (%s != (%uU)) || (%s != (%uU))",
fdesc->verhigh_def.c_str(), p_dlist->ver.hi, fdesc->verlow_def.c_str(), p_dlist->ver.low));

fwriter->AppendLine(StrPrint("#error The %s dbc source files have different versions", fdesc->DRVNAME.c_str()));
fwriter->AppendLine("#endif", 2);

// put diagmonitor ifdef selection for including @drv-fmon header
// with FMon_* signatures to call from unpack function
fwriter->AppendLine(StrPrint("#ifdef %s", fdesc->usemon_def.c_str()));
Expand All @@ -246,7 +281,7 @@ 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->AppendLine(StrPrint("#include \"%s-fmon.h\"", fdesc->drvname.c_str()), 2);
fwriter->AppendLine(StrPrint("#include <%s-fmon.h>", fdesc->drvname.c_str()), 2);

fwriter->AppendLine(StrPrint("#endif // %s", fdesc->usemon_def.c_str()), 3);

Expand Down Expand Up @@ -307,7 +342,7 @@ void CiMainGenerator::Gen_ConfigHeader()
fwriter->AppendLine("#pragma once");
fwriter->AppendLine("");
fwriter->AppendLine("/* include common dbccode configurations */");
fwriter->AppendLine("#include \"dbccodeconf.h\"");
fwriter->AppendLine("#include <dbccodeconf.h>");
fwriter->AppendLine("");
fwriter->AppendLine("");
fwriter->AppendLine("/* ------------------------------------------------------------------------- *");
Expand Down Expand Up @@ -427,12 +462,12 @@ void CiMainGenerator::Gen_FMonHeader()
fwriter->AppendLine(StrPrint("#define %s_FMON (%uU)", fdesc->verhigh_def.c_str(), p_dlist->ver.hi));
fwriter->AppendLine(StrPrint("#define %s_FMON (%uU)", fdesc->verlow_def.c_str(), p_dlist->ver.low), 2);

fwriter->AppendLine(StrPrint("#include \"%s-config.h\"", fdesc->drvname.c_str()), 2);
fwriter->AppendLine(StrPrint("#include <%s-config.h>", fdesc->drvname.c_str()), 2);

// put diagmonitor ifdef selection for including @drv-fmon header
// with FMon_* signatures to call from unpack function
fwriter->AppendLine(StrPrint("#ifdef %s", fdesc->usemon_def.c_str()), 2);
fwriter->AppendLine("#include \"canmonitorutil.h\"");
fwriter->AppendLine("#include <canmonitorutil.h>");
fwriter->AppendLine("/*\n\
This file contains the prototypes of all the functions that will be called\n\
from each Unpack_*name* function to detect DBC related errors\n\
Expand Down Expand Up @@ -461,7 +496,7 @@ void CiMainGenerator::Gen_FMonSource()
fwriter->AppendLine("// " + std::regex_replace(fdesc->start_info, std::regex("\n"), "\n// "));
}

fwriter->AppendLine(StrPrint("#include \"%s\"", fdesc->fmon_h.fname.c_str()), 2);
fwriter->AppendLine(StrPrint("#include <%s>", fdesc->fmon_h.fname.c_str()), 2);
// put diagmonitor ifdef selection for including @drv-fmon header
// with FMon_* signatures to call from unpack function
fwriter->AppendLine(StrPrint("#ifdef %s", fdesc->usemon_def.c_str()), 2);
Expand Down Expand Up @@ -618,37 +653,51 @@ void CiMainGenerator::WriteSigStructField(const SignalDescriptor_t& sig, bool bi

fwriter->AppendText(StrPrint(" Bits=%2d", sig.LengthBit));

size_t offset = 0;
std::string infocmnt{};

if (sig.IsDoubleSig)
{
if (sig.Offset != 0)
{
fwriter->AppendText(StrPrint(" Offset= %-18f", sig.Offset));
infocmnt = IndentedString(offset, infocmnt);
offset += 27;
infocmnt += StrPrint(" Offset= %f", sig.Offset);
}

if (sig.Factor != 1)
{
fwriter->AppendText(StrPrint(" Factor= %-15f", sig.Factor));
infocmnt = IndentedString(offset, infocmnt);
offset += 24;
infocmnt += StrPrint(" Factor= %f", sig.Factor);
}
}
else if (sig.IsSimpleSig == false)
{
// 2 type of signal
if (sig.Offset != 0)
{
fwriter->AppendText(StrPrint(" Offset= %-18d", (int)sig.Offset));
infocmnt = IndentedString(offset, infocmnt);
offset += 27;
infocmnt += StrPrint(" Offset= %d", (int)sig.Offset);
}

if (sig.Factor != 1)
{
fwriter->AppendText(StrPrint(" Factor= %-15d", (int)sig.Factor));
infocmnt = IndentedString(offset, infocmnt);
offset += 24;
infocmnt += StrPrint(" Factor= %d", (int)sig.Factor);
}
}

if (sig.Unit.size() > 0)
{
fwriter->AppendText(StrPrint(" Unit:'%s'", sig.Unit.c_str()));
infocmnt = IndentedString(offset, infocmnt);
infocmnt += StrPrint(" Unit:'%s'", sig.Unit.c_str());
}

fwriter->AppendText(infocmnt);

fwriter->AppendLine("", 2);

if (!sig.IsSimpleSig)
Expand Down
17 changes: 10 additions & 7 deletions src/codegen/c-util-generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,11 @@ void CiUtilGenerator::PrintHeader()
tof->AppendLine(openguard.c_str(), 2);

// include common dbc code config header
tof->AppendLine("#include \"dbccodeconf.h\"", 2);
tof->AppendLine("#include <dbccodeconf.h>", 2);

// include c-main driver header
tof->AppendLine(StrPrint("#include \"%s.h\"", file_drvname.c_str()), 2);
tof->AppendLine(StrPrint("#include <%s.h>", file_drvname.c_str()), 2);

// put version info macros
tof->AppendLine("// This version definition comes from main driver version and");
tof->AppendLine("// can be compared in user code space for strict compatibility test");
tof->AppendLine(StrPrint("#define %s (%uU)", fdesc->verhigh_def.c_str(), p_dlist->ver.hi));
tof->AppendLine(StrPrint("#define %s (%uU)", fdesc->verlow_def.c_str(), p_dlist->ver.low), 2);

if (rx.size() == 0)
{
Expand Down Expand Up @@ -191,6 +186,14 @@ void CiUtilGenerator::PrintSource()

tof->AppendLine(StrPrint("#include \"%s\"", fdesc->util_h.fname.c_str()), 2);

tof->AppendLine("// DBC file version");
tof->AppendLine(StrPrint("#if (%s != (%uU)) || (%s != (%uU))",
fdesc->verhigh_def.c_str(), p_dlist->ver.hi, fdesc->verlow_def.c_str(), p_dlist->ver.low));

tof->AppendLine(StrPrint("#error The %s binutil source file has inconsistency with core dbc lib!",
fdesc->DRVNAME.c_str()));
tof->AppendLine("#endif", 2);

// optional RX and TX struct allocations
if (rx.size() > 0 || tx.size() > 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
#include <stdint.h>

#define CODEGEN_LIB_VERSION_MAJ (2)
#define CODEGEN_LIB_VERSION_MIN (1)
#define CODEGEN_LIB_VERSION_MIN (2)
56 changes: 56 additions & 0 deletions src/helpers/formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ static const std::string __typeprint[8] =
"uint64_t"
};

std::string IndentedString(size_t n, const std::string& source, const char c)
{
if (source.length() >= n)
{
return source;
}
else
{
std::string indent(n - source.length(), c);
return source + indent;
}
}

const char* StrPrint(const char* format, ...)
{
va_list args;
Expand Down Expand Up @@ -87,3 +100,46 @@ std::string str_trim(std::string s)

return s;
}

template<char L, char U>
static inline bool in_range(const char c)
{
return ((c >= L) && (c <= U));
}

static bool is_first_valid(const char c)
{
return in_range<'a', 'z'>(c) || in_range<'A', 'Z'>(c) || c == '_';
}

static bool is_nonfirst_valid(const char c)
{
return is_first_valid(c) || in_range<'0', '9'>(c);
}

std::string make_c_name(const std::string& s)
{
std::string ret{};

if (s.length() == 0)
{
return ret;
}

for (auto i = 0; i < s.length(); i++)
{
if ((ret.length() == 0 && is_first_valid(s[i])) ||
(ret.length() > 0 && is_nonfirst_valid(s[i])))
{
// basic C-identifier rule
ret += s[i];
}
else if (s[i] == ' ')
{
// special case for whitespaces
ret += '_';
}
}

return ret;
}
10 changes: 10 additions & 0 deletions src/helpers/formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <stdarg.h>
#include <string>

std::string IndentedString(size_t n, const std::string& source, const char c = ' ');

const char* StrPrint(const char* format, ...);

std::string PrintType(uint8_t tid);
Expand All @@ -14,3 +16,11 @@ std::string str_toupper(std::string s);
std::string str_tolower(std::string s);

std::string str_trim(std::string s);

/**
* @brief Makes input string valid C-identifier
*
* @param s source string
* @return std::string
*/
std::string make_c_name(const std::string& s);
22 changes: 19 additions & 3 deletions src/maincli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <filesystem>
#include <algorithm>
#include <stdlib.h>
#include <helpers/formatter.h>
#include "parser/dbcscanner.h"
#include "codegen/c-main-generator.h"
#include "codegen/c-util-generator.h"
Expand Down Expand Up @@ -117,6 +118,16 @@ int main(int argc, char* argv[])
return 0;
}

if (drvname_ok)
{
dbc_driver_name = make_c_name(dbc_driver_name);

if (dbc_driver_name.length() == 0)
{
drvname_ok = false;
}
}

if (drvname_ok && path_ok && dbc_ok)
{

Expand Down Expand Up @@ -199,13 +210,16 @@ int main(int argc, char* argv[])
{
std::string util_name = nodes[node] + "_" + dbc_driver_name;

// set new driver name for current node
fscreator->FS.drvname = str_tolower(util_name);
fscreator->FS.DRVNAME = str_toupper(fscreator->FS.drvname);
fscreator->FS.util_c.dir = fscreator->FS.utildir;
fscreator->FS.util_h.dir = fscreator->FS.utildir;

fscreator->FS.util_h.fname = util_name + ".h";
fscreator->FS.util_h.fname = str_tolower(fscreator->FS.drvname + "-binutil.h");
fscreator->FS.util_h.fpath = fscreator->FS.utildir + "/" + fscreator->FS.util_h.fname;

fscreator->FS.util_c.fname = util_name + ".c";
fscreator->FS.util_c.fname = str_tolower(fscreator->FS.drvname + "-binutil.c");
fscreator->FS.util_c.fpath = fscreator->FS.utildir + "/" + fscreator->FS.util_c.fname;

MsgsClassification groups;
Expand Down Expand Up @@ -287,7 +301,9 @@ void PrintUsage()
std::cout <<
"./dbccoder -dbc /home/user/docs/driveshaft.dbc -out /home/user/docs/gen/ -drvname drivedb -nodeutils -rw" << std::endl;

std::cout << "./dbccoder -dbc /home/user/docs/driveshaft.dbc -out /home/user/docs/gen/ -drvname drivedb -nodeutils" << std::endl;
std::cout <<
"./dbccoder -dbc /home/user/docs/driveshaft.dbc -out /home/user/docs/gen/ -drvname drivedb -nodeutils" << std::endl;

std::cout << "./dbccoder -dbc /home/user/docs/driveshaft.dbc -out /home/user/docs/gen/ -drvname drivedb" << std::endl;
std::cout << std::endl;
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy