-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathc-util-generator.cpp
319 lines (258 loc) · 7.39 KB
/
c-util-generator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "c-util-generator.h"
#include "helpers/formatter.h"
#include <algorithm>
#include <regex>
static const std::string openguard = "#ifdef __cplusplus\n\
extern \"C\" {\n\
#endif";
static const std::string closeguard = "#ifdef __cplusplus\n\
}\n\
#endif";
CiUtilGenerator::CiUtilGenerator()
{
Clear();
}
void CiUtilGenerator::Clear()
{
// clear all groups of messages
tx.clear();
rx.clear();
both.clear();
}
void CiUtilGenerator::Generate(DbcMessageList_t& dlist, const AppSettings_t& fsd,
const MsgsClassification& groups, const std::string& drvname)
{
Clear();
p_dlist = &dlist;
code_drvname = drvname;
file_drvname = str_tolower(drvname);
// 1 step is to prepare vectors of message's groups
for (auto m : dlist.msgs)
{
if (!m->frameNotEmpty)
{
// skip frame when it is empty
continue;
}
auto v = std::find_if(groups.Both.begin(), groups.Both.end(), [&](const uint32_t& msgid)
{
return msgid == m->MsgID;
});
if (v != std::end(groups.Both))
{
// message is in Both group, so put it to rx and tx containers
tx.push_back(m);
rx.push_back(m);
continue;
}
v = std::find_if(groups.Rx.begin(), groups.Rx.end(), [&](const uint32_t& msgid)
{
return msgid == m->MsgID;
});
if (v != std::end(groups.Rx))
{
rx.push_back(m);
}
v = std::find_if(groups.Tx.begin(), groups.Tx.end(), [&](const uint32_t& msgid)
{
return msgid == m->MsgID;
});
if (v != std::end(groups.Tx))
{
tx.push_back(m);
}
}
std::sort(rx.begin(), rx.end(), [&](const MessageDescriptor_t* a, const MessageDescriptor_t* b)
{
return a->MsgID < b->MsgID;
});
std::sort(tx.begin(), tx.end(), [&](const MessageDescriptor_t* a, const MessageDescriptor_t* b)
{
return a->MsgID < b->MsgID;
});
fdesc = &fsd.file;
gdesc = &fsd.gen;
// print header for util code
PrintHeader();
// print main source for util code
PrintSource();
}
void CiUtilGenerator::PrintHeader()
{
tof.Flush();
if (gdesc->start_info.size() > 0)
{
tof.Append("// " + std::regex_replace(gdesc->start_info, std::regex("\n"), "\n// "));
}
tof.Append("#pragma once");
tof.Append();
// include common dbc code config header
tof.Append("#include <nv1_can_gen/src/dbccodeconf.hpp>");
tof.Append();
// include c-main driver header
tof.Append("#include \"%s.hpp\"", file_drvname.c_str());
tof.Append();
if (rx.size() == 0)
{
tof.Append("// There is no any RX mapped massage.");
tof.Append();
}
else
{
// print the typedef
tof.Append("typedef struct\n{");
for (auto m : rx)
{
tof.Append(" %s_t %s;", m->Name.c_str(), m->Name.c_str());
}
tof.Append("} %s_rx_t;", gdesc->drvname.c_str());
tof.Append();
}
if (tx.size() == 0)
{
tof.Append("// There is no any TX mapped massage.");
tof.Append();
}
else
{
// print the typedef
tof.Append("typedef struct\n{");
for (auto m : tx)
{
tof.Append(" %s_t %s;", m->Name.c_str(), m->Name.c_str());
}
tof.Append("} %s_tx_t;", gdesc->drvname.c_str());
tof.Append();
}
if (rx.size() > 0)
{
// receive function necessary only when more than 0 rx messages were mapped
tof.Append("uint32_t %s_Receive(%s_rx_t* m, const uint8_t* d, uint32_t msgid, uint8_t dlc);",
gdesc->drvname.c_str(), gdesc->drvname.c_str());
tof.Append();
}
// print extern for super structs
if (rx.size() > 0 || tx.size() > 0)
{
tof.Append("#ifdef __DEF_%s__", gdesc->DRVNAME.c_str());
tof.Append();
if (rx.size() > 0)
{
tof.Append("extern %s_rx_t %s_rx;", gdesc->drvname.c_str(), gdesc->drvname.c_str());
tof.Append();
}
if (tx.size() > 0)
{
tof.Append("extern %s_tx_t %s_tx;", gdesc->drvname.c_str(), gdesc->drvname.c_str());
tof.Append();
}
tof.Append("#endif // __DEF_%s__", gdesc->DRVNAME.c_str());
tof.Append();
}
tof.Flush(fdesc->util_h.fpath);
}
void CiUtilGenerator::PrintSource()
{
if (gdesc->start_info.size() > 0)
{
tof.Append("// " + std::regex_replace(gdesc->start_info, std::regex("\n"), "\n// "));
}
tof.Append("#include \"%s\"", fdesc->util_h.fname.c_str());
tof.Append();
tof.Append("// DBC file version");
tof.Append("#if (%s != (%uU)) || (%s != (%uU))",
gdesc->verhigh_def.c_str(), p_dlist->ver.hi, gdesc->verlow_def.c_str(), p_dlist->ver.low);
tof.Append("#error The %s binutil source file has inconsistency with core dbc lib!",
gdesc->DRVNAME.c_str());
tof.Append("#endif");
tof.Append();
// optional RX and TX struct allocations
if (rx.size() > 0 || tx.size() > 0)
{
tof.Append("#ifdef __DEF_%s__", gdesc->DRVNAME.c_str());
tof.Append();
if (rx.size() > 0)
{
tof.Append("%s_rx_t %s_rx;", gdesc->drvname.c_str(), gdesc->drvname.c_str());
tof.Append();
}
if (tx.size() > 0)
{
tof.Append("%s_tx_t %s_tx;", gdesc->drvname.c_str(), gdesc->drvname.c_str());
tof.Append();
}
tof.Append("#endif // __DEF_%s__", gdesc->DRVNAME.c_str());
tof.Append();
}
if (rx.size() > 0)
{
// tree will be created inside (in dynamic memory) so this
// scope is responsible for deletion these resources
// tree is the struct tree-view which is used to execute
// binary search on FrameID for selecting unpacking function
auto tree = FillTreeLevel(rx, 0, static_cast<int32_t>(rx.size()));
tof.Append("uint32_t %s_Receive(%s_rx_t* _m, const uint8_t* _d, uint32_t _id, uint8_t dlc_)",
gdesc->drvname.c_str(), gdesc->drvname.c_str());
tof.Append("{");
tof.Append(" uint32_t recid = 0;");
// put tree-view struct on code (in treestr variable)
std::string treestr;
condtree.Clear();
tof.Append(condtree.WriteCode(tree, treestr, 1));
tof.Append();
tof.Append(" return recid;");
tof.Append("}");
tof.Append();
// clear tree after using
condtree.DeleteTree(tree);
}
tof.Flush(fdesc->util_c.fpath);
}
ConditionalTree_t* CiUtilGenerator::FillTreeLevel(std::vector<MessageDescriptor_t*>& list,
int32_t l,
int32_t h,
bool started)
{
int32_t span = h - l;
int32_t lowhalf = span / 2;
treestarted = started;
if (h < 1)
{
return nullptr;
}
ConditionalTree_t* ret = new ConditionalTree_t;
if (!treestarted && h == 1)
{
ret->Type = ConditionalType::Cond;
auto msg = list[l];
ret->ConditionExpresion = StrPrint("_id == 0x%XU", msg->MsgID);
ret->High = new ConditionalTree_t;
ret->High->Type = ConditionalType::Single;
ret->High->UtilCodeBody = StrPrint("recid = Unpack_%s_%s(&(_m->%s), _d, dlc_);",
msg->Name.c_str(), code_drvname.c_str(), msg->Name.c_str());
return ret;
}
if (span > 1)
{
ret->Type = ConditionalType::Cond;
if (lowhalf > 1)
{
ret->ConditionExpresion = StrPrint("(_id >= 0x%XU) && (_id < 0x%XU)", list[l]->MsgID, list[(l + lowhalf)]->MsgID);
}
else
{
ret->ConditionExpresion = StrPrint("_id == 0x%XU", list[l]->MsgID);
}
ret->High = FillTreeLevel(list, l, l + lowhalf, true);
ret->Low = FillTreeLevel(list, l + lowhalf, h, true);
}
else
{
ret->Type = ConditionalType::Express;
auto msg = list[l];
ret->ConditionExpresion = StrPrint("_id == 0x%XU", msg->MsgID);
ret->UtilCodeBody = StrPrint("recid = Unpack_%s_%s(&(_m->%s), _d, dlc_);",
msg->Name.c_str(), code_drvname.c_str(), msg->Name.c_str());
}
return ret;
}