|
| 1 | +#include "app.h" |
| 2 | +#include <iostream> |
| 3 | +#include <memory> |
| 4 | +#include <fstream> |
| 5 | +#include <filesystem> |
| 6 | +#include <algorithm> |
| 7 | +#include <stdlib.h> |
| 8 | +#include "app.h" |
| 9 | +#include "parser/dbcscanner.h" |
| 10 | +#include "codegen/c-main-generator.h" |
| 11 | +#include "codegen/c-util-generator.h" |
| 12 | +#include "codegen/fs-creator.h" |
| 13 | +#include "codegen/version.h" |
| 14 | +#include <codegen/version.h> |
| 15 | +#include <helpers/formatter.h> |
| 16 | + |
| 17 | +void CoderApp::Run() |
| 18 | +{ |
| 19 | + if (ParseParams()) |
| 20 | + { |
| 21 | + GenerateCode(); |
| 22 | + } |
| 23 | + else |
| 24 | + { |
| 25 | + PrintHelp(); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +bool CoderApp::ParseParams() |
| 30 | +{ |
| 31 | + for (size_t i = 0; i < Params.size(); i++) |
| 32 | + { |
| 33 | + if (Params[i].first.compare("-dbc") == 0) |
| 34 | + { |
| 35 | + dbc.value = Params[i].second; |
| 36 | + dbc.ok = true; |
| 37 | + } |
| 38 | + else if (Params[i].first.compare("-out") == 0) |
| 39 | + { |
| 40 | + outdir.value = Params[i].second; |
| 41 | + outdir.ok = true; |
| 42 | + } |
| 43 | + else if (Params[i].first.compare("-drvname") == 0) |
| 44 | + { |
| 45 | + drvname.value = make_c_name(Params[i].second); |
| 46 | + drvname.ok = true; |
| 47 | + |
| 48 | + if (drvname.value.length() == 0) |
| 49 | + { |
| 50 | + drvname.ok = false; |
| 51 | + } |
| 52 | + } |
| 53 | + else if (Params[i].first.compare("-rw") == 0) |
| 54 | + { |
| 55 | + rewrite_src = true; |
| 56 | + } |
| 57 | + else if (Params[i].first.compare("-nodeutils") == 0) |
| 58 | + { |
| 59 | + gen_nodeutils = true; |
| 60 | + } |
| 61 | + else if (Params[i].first.compare("-help") == 0) |
| 62 | + { |
| 63 | + return false; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return (dbc.ok && outdir.ok && drvname.ok); |
| 68 | +} |
| 69 | + |
| 70 | +void CoderApp::GenerateCode() |
| 71 | +{ |
| 72 | + auto scanner = std::make_unique<DbcScanner>(); |
| 73 | + auto cigen = std::make_unique<CiMainGenerator>(); |
| 74 | + auto ciugen = std::make_unique<CiUtilGenerator>(); |
| 75 | + auto fscreator = std::make_unique<FsCreator>(); |
| 76 | + |
| 77 | + std::ifstream reader; |
| 78 | + |
| 79 | + std::cout << "dbc file : " << dbc.value << std::endl; |
| 80 | + std::cout << "gen path : " << outdir.value << std::endl; |
| 81 | + std::cout << "drv name : " << drvname.value << std::endl; |
| 82 | + |
| 83 | + if (std::filesystem::exists(dbc.value) == false) |
| 84 | + { |
| 85 | + std::cout << "DBC file is not exists!" << std::endl; |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + reader.open(dbc.value); |
| 90 | + |
| 91 | + std::istream& s = reader; |
| 92 | + |
| 93 | + scanner->TrimDbcText(s); |
| 94 | + |
| 95 | + std::string info(""); |
| 96 | + |
| 97 | + // create main destination directory |
| 98 | + auto ret = fscreator->PrepareDirectory(drvname.value.c_str(), outdir.value.c_str(), rewrite_src, info); |
| 99 | + |
| 100 | + if (ret) |
| 101 | + { |
| 102 | + cigen->Generate(scanner->dblist, fscreator->FS); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + std::cout << "One or both are invalid\n"; |
| 107 | + } |
| 108 | + |
| 109 | + // check if option --node-utils is requested, when requested binutil generation |
| 110 | + // wiil be performed on each node from DBC file in accordance to its RX / TX subscription |
| 111 | + if (gen_nodeutils) |
| 112 | + { |
| 113 | + std::vector<std::string> nodes; |
| 114 | + |
| 115 | + for (size_t num = 0; num < scanner->dblist.msgs.size(); num++) |
| 116 | + { |
| 117 | + // iterate all messages and collect All nodes assign to at least one message |
| 118 | + auto m = scanner->dblist.msgs[num]; |
| 119 | + |
| 120 | + for (size_t txs = 0; txs < m->TranS.size(); txs++) |
| 121 | + { |
| 122 | + std::string tx_node_name = m->TranS[txs]; |
| 123 | + |
| 124 | + if (std::find(nodes.begin(), nodes.end(), tx_node_name) == nodes.end()) |
| 125 | + { |
| 126 | + // New node name. put it in the node collection |
| 127 | + nodes.push_back(tx_node_name); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + for (size_t recs = 0; recs < m->RecS.size(); recs++) |
| 132 | + { |
| 133 | + std::string rx_node_name = m->RecS[recs]; |
| 134 | + |
| 135 | + // test all recs |
| 136 | + if (std::find(nodes.begin(), nodes.end(), rx_node_name) == nodes.end()) |
| 137 | + { |
| 138 | + // New node name, put it in the node collection |
| 139 | + nodes.push_back(rx_node_name); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // for each node in collection perform specific bin-util generation |
| 145 | + for (size_t node = 0; node < nodes.size(); node++) |
| 146 | + { |
| 147 | + std::string util_name = nodes[node] + "_" + drvname.value; |
| 148 | + |
| 149 | + // set new driver name for current node |
| 150 | + fscreator->FS.gen.drvname = str_tolower(util_name); |
| 151 | + fscreator->FS.gen.DRVNAME = str_toupper(fscreator->FS.gen.drvname); |
| 152 | + fscreator->FS.file.util_c.dir = fscreator->FS.file.utildir; |
| 153 | + fscreator->FS.file.util_h.dir = fscreator->FS.file.utildir; |
| 154 | + |
| 155 | + fscreator->FS.file.util_h.fname = str_tolower(fscreator->FS.gen.drvname + "-binutil.h"); |
| 156 | + fscreator->FS.file.util_h.fpath = fscreator->FS.file.utildir + "/" + fscreator->FS.file.util_h.fname; |
| 157 | + |
| 158 | + fscreator->FS.file.util_c.fname = str_tolower(fscreator->FS.gen.drvname + "-binutil.c"); |
| 159 | + fscreator->FS.file.util_c.fpath = fscreator->FS.file.utildir + "/" + fscreator->FS.file.util_c.fname; |
| 160 | + |
| 161 | + MsgsClassification groups; |
| 162 | + |
| 163 | + for (size_t i = 0; i < scanner->dblist.msgs.size(); i++) |
| 164 | + { |
| 165 | + auto m = scanner->dblist.msgs[i]; |
| 166 | + |
| 167 | + bool found = (std::find(m->TranS.begin(), m->TranS.end(), nodes[node]) != m->TranS.end()); |
| 168 | + |
| 169 | + if (found) |
| 170 | + { |
| 171 | + // Message is in Tx array of current node |
| 172 | + groups.Tx.push_back(m->MsgID); |
| 173 | + } |
| 174 | + |
| 175 | + found = (std::find(m->RecS.begin(), m->RecS.end(), nodes[node]) != m->RecS.end()); |
| 176 | + |
| 177 | + if (found) |
| 178 | + { |
| 179 | + // Message is in Rx array of current node |
| 180 | + groups.Rx.push_back(m->MsgID); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if (ret) |
| 185 | + { |
| 186 | + ciugen->Generate(scanner->dblist, fscreator->FS, groups, drvname.value); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + MsgsClassification groups; |
| 193 | + |
| 194 | + for (size_t i = 0; i < scanner->dblist.msgs.size(); i++) |
| 195 | + { |
| 196 | + groups.Rx.push_back(scanner->dblist.msgs[i]->MsgID); |
| 197 | + } |
| 198 | + |
| 199 | + if (ret) |
| 200 | + { |
| 201 | + ciugen->Generate(scanner->dblist, fscreator->FS, groups, drvname.value); |
| 202 | + } |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | +void CoderApp::PrintHelp() |
| 208 | +{ |
| 209 | + std::cout << "coderdbc v" << CODEGEN_LIB_VERSION_MAJ << "." << CODEGEN_LIB_VERSION_MIN << std::endl << std::endl; |
| 210 | + std::cout << "project source code:\thttps://github.com/astand/c-coderdbc\t\t" << std::endl; |
| 211 | + std::cout << "free web application:\thttps://coderdbc.com" << std::endl; |
| 212 | + std::cout << std::endl; |
| 213 | + std::cout << "required parameters:" << std::endl; |
| 214 | + |
| 215 | + std::cout << " -dbc\t\t path to dbc file" << std::endl; |
| 216 | + std::cout << " -out\t\t directory for generated source files (must be pre-created)" << std::endl; |
| 217 | + std::cout << " -drvname\t driver name - will be used for naming driver parts" << std::endl; |
| 218 | + std::cout << std::endl; |
| 219 | + std::cout << "optional parameters:" << std::endl; |
| 220 | + std::cout << " -nodeutils\t will generate specific pairs of binutils drivers for each node" << std::endl; |
| 221 | + std::cout << " -rw\t\t by default each new generation with previously used params" << std::endl; |
| 222 | + std::cout << " \t\t will create new sud-directory with source files (000, 001, ... etc)" << std::endl; |
| 223 | + std::cout << " \t\t '-rw' option enables rewriting: all source files previously generated" << std::endl; |
| 224 | + std::cout << " \t\t will be replaced by new ones" << std::endl; |
| 225 | + std::cout << std::endl; |
| 226 | + |
| 227 | + std::cout << "examples:" << std::endl; |
| 228 | + std::cout << std::endl; |
| 229 | + |
| 230 | + std::cout << |
| 231 | + "./dbccoder -dbc /home/user/docs/driveshaft.dbc -out /home/user/docs/gen/ -drvname drivedb -nodeutils -rw" << std::endl; |
| 232 | + |
| 233 | + std::cout << |
| 234 | + "./dbccoder -dbc /home/user/docs/driveshaft.dbc -out /home/user/docs/gen/ -drvname drivedb -nodeutils" << std::endl; |
| 235 | + |
| 236 | + std::cout << "./dbccoder -dbc /home/user/docs/driveshaft.dbc -out /home/user/docs/gen/ -drvname drivedb" << std::endl; |
| 237 | + std::cout << std::endl; |
| 238 | +} |
0 commit comments