forked from collin80/SavvyCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptcontainer.cpp
More file actions
388 lines (314 loc) · 11.1 KB
/
scriptcontainer.cpp
File metadata and controls
388 lines (314 loc) · 11.1 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <QJSValueIterator>
#include <QDebug>
#include "scriptcontainer.h"
#include "connections/canconmanager.h"
ScriptContainer::ScriptContainer()
{
canHelper = new CANScriptHelper(&scriptEngine);
isoHelper = new ISOTPScriptHelper(&scriptEngine);
udsHelper = new UDSScriptHelper(&scriptEngine);
connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));
}
void ScriptContainer::compileScript()
{
QJSValue result = scriptEngine.evaluate(scriptText, fileName);
emit sendLog("Compiling script...");
canHelper->clearFilters();
isoHelper->clearFilters();
udsHelper->clearFilters();
if (result.isError())
{
emit sendLog("SCRIPT EXCEPTION!");
emit sendLog("Line: " + result.property("lineNumber").toString());
emit sendLog(result.property("message").toString());
emit sendLog("Stack:");
emit sendLog(result.property("stack").toString());
}
else
{
compiledScript = result;
//Add a bunch of helper objects into javascript that the scripts
//can use to interact with the CAN buses
QJSValue hostObj = scriptEngine.newQObject(this);
scriptEngine.globalObject().setProperty("host", hostObj);
QJSValue canObj = scriptEngine.newQObject(canHelper);
scriptEngine.globalObject().setProperty("can", canObj);
QJSValue isoObj = scriptEngine.newQObject(isoHelper);
scriptEngine.globalObject().setProperty("isotp", isoObj);
QJSValue udsObj = scriptEngine.newQObject(udsHelper);
scriptEngine.globalObject().setProperty("uds", udsObj);
//Find out which callbacks the script has created.
setupFunction = scriptEngine.globalObject().property("setup");
canHelper->setRxCallback(scriptEngine.globalObject().property("gotCANFrame"));
isoHelper->setRxCallback(scriptEngine.globalObject().property("gotISOTPMessage"));
udsHelper->setRxCallback(scriptEngine.globalObject().property("gotUDSMessage"));
tickFunction = scriptEngine.globalObject().property("tick");
if (setupFunction.isCallable())
{
qDebug() << "setup exists";
QJSValue res = setupFunction.call();
if (res.isError())
{
emit sendLog("Error in setup function on line " + res.property("lineNumber").toString());
emit sendLog(res.property("message").toString());
}
}
if (tickFunction.isCallable()) qDebug() << "tick exists";
}
}
void ScriptContainer::setScriptWindow(ScriptingWindow *win)
{
window = win;
connect(this, &ScriptContainer::sendLog, window, &ScriptingWindow::log);
}
void ScriptContainer::log(QJSValue logString)
{
QString val = logString.toString();
emit sendLog(val);
}
void ScriptContainer::setTickInterval(QJSValue interval)
{
int intervalValue = interval.toInt();
qDebug() << "called set tick interval with value " << intervalValue;
if (intervalValue > 0)
{
timer.setInterval(intervalValue);
timer.start();
}
else timer.stop();
}
void ScriptContainer::tick()
{
if (tickFunction.isCallable())
{
//qDebug() << "Calling tick function";
QJSValue res = tickFunction.call();
if (res.isError())
{
emit sendLog("Error in tick function on line " + res.property("lineNumber").toString());
emit sendLog(res.property("message").toString());
}
}
}
void ScriptContainer::addParameter(QJSValue name)
{
scriptParams.append(name.toString());
}
void ScriptContainer::updateValuesTable(QTableWidget *widget)
{
QString valu;
foreach (QString paramName, scriptParams)
{
valu = scriptEngine.globalObject().property(paramName).toString();
qDebug() << paramName << " - " << valu;
bool found = false;
for (int i = 0; i < widget->rowCount(); i++)
{
if (widget->item(i, 0) && widget->item(i, 0)->text().compare(paramName) == 0)
{
found = true;
if (!widget->item(i, 1)->isSelected())
{
widget->item(i,1)->setText(valu);
}
break;
}
}
if (!found)
{
int row = widget->rowCount();
widget->insertRow(widget->rowCount());
QTableWidgetItem *item;
item = new QTableWidgetItem();
item->setText(paramName);
item->setFlags(Qt::ItemIsEnabled);
widget->setItem(row, 0, item);
item = new QTableWidgetItem();
item->setText(valu);
widget->setItem(row, 1, item);
}
}
}
void ScriptContainer::updateParameter(QString name, QString value)
{
qDebug() << name << " * " << value;
QJSValue val(value);
scriptEngine.globalObject().setProperty(name, val);
}
/* CANScriptHandler Methods */
CANScriptHelper::CANScriptHelper(QJSEngine *engine)
{
scriptEngine = engine;
}
void CANScriptHelper::setRxCallback(QJSValue cb)
{
gotFrameFunction = cb;
}
void CANScriptHelper::setFilter(QJSValue id, QJSValue mask, QJSValue bus)
{
uint32_t idVal = id.toUInt();
uint32_t maskVal = mask.toUInt();
int busVal = bus.toInt();
qDebug() << "Called set filter";
qDebug() << idVal << "*" << maskVal << "*" << busVal;
CANFilter filter;
filter.setFilter(idVal, maskVal, busVal);
filters.append(filter);
CANConManager::getInstance()->addTargettedFrame(busVal, idVal, maskVal, this);
}
void CANScriptHelper::clearFilters()
{
qDebug() << "Called clear filters";
foreach (CANFilter filter, filters)
{
CANConManager::getInstance()->removeTargettedFrame(filter.bus, filter.ID, filter.mask, this);
}
filters.clear();
}
void CANScriptHelper::sendFrame(QJSValue bus, QJSValue id, QJSValue length, QJSValue data)
{
CANFrame frame;
frame.extended = false;
frame.ID = id.toInt();
frame.len = length.toUInt();
if (frame.len > 8) frame.len = 8;
if (!data.isArray()) qDebug() << "data isn't an array";
for (unsigned int i = 0; i < frame.len; i++)
{
frame.data[i] = (uint8_t)data.property(i).toInt();
}
frame.bus = (uint32_t)bus.toInt();
//if (frame.bus > 1) frame.bus = 1;
if (frame.ID > 0x7FF) frame.extended = true;
qDebug() << "sending frame from script";
CANConManager::getInstance()->sendFrame(frame);
}
void CANScriptHelper::gotTargettedFrame(const CANFrame &frame)
{
if (!gotFrameFunction.isCallable()) return; //nothing to do if we can't even call the function
//qDebug() << "Got frame in script interface";
for (int i = 0; i < filters.length(); i++)
{
if (filters[i].checkFilter(frame.ID, frame.bus))
{
QJSValueList args;
args << frame.bus << frame.ID << frame.len;
QJSValue dataBytes = scriptEngine->newArray(frame.len);
for (unsigned int j = 0; j < frame.len; j++) dataBytes.setProperty(j, QJSValue(frame.data[j]));
args.append(dataBytes);
gotFrameFunction.call(args);
return; //as soon as one filter matches we jump out
}
}
}
/* ISOTPScriptHelper methods */
ISOTPScriptHelper::ISOTPScriptHelper(QJSEngine *engine)
{
scriptEngine = engine;
handler = new ISOTP_HANDLER;
connect(handler, SIGNAL(newISOMessage(ISOTP_MESSAGE)), this, SLOT(newISOMessage(ISOTP_MESSAGE)));
handler->setReception(true);
}
void ISOTPScriptHelper::clearFilters()
{
handler->clearAllFilters();
}
void ISOTPScriptHelper::setFilter(QJSValue id, QJSValue mask, QJSValue bus)
{
uint32_t idVal = id.toUInt();
uint32_t maskVal = mask.toUInt();
int busVal = bus.toInt();
qDebug() << "Called isotp set filter";
qDebug() << idVal << "*" << maskVal << "*" << busVal;
handler->addFilter(busVal, idVal, maskVal);
}
void ISOTPScriptHelper::sendISOTP(QJSValue bus, QJSValue id, QJSValue length, QJSValue data)
{
ISOTP_MESSAGE msg;
msg.extended = false;
msg.ID = id.toInt();
msg.len = length.toUInt();
if (!data.isArray()) qDebug() << "data isn't an array";
for (int i = 0; i < msg.len; i++)
{
msg.data[i] = static_cast<uint8_t>(data.property(i).toInt());
}
msg.bus = bus.toInt();
if (msg.ID > 0x7FF) msg.extended = true;
qDebug() << "sending isotp message from script";
handler->sendISOTPFrame(msg.bus, msg.ID, msg.data);
}
void ISOTPScriptHelper::setRxCallback(QJSValue cb)
{
gotFrameFunction = cb;
}
void ISOTPScriptHelper::newISOMessage(ISOTP_MESSAGE msg)
{
qDebug() << "isotpScriptHelper got a ISOTP message";
if (!gotFrameFunction.isCallable()) return; //nothing to do if we can't even call the function
//qDebug() << "Got frame in script interface";
QJSValueList args;
args << msg.bus << msg.ID << msg.len;
QJSValue dataBytes = scriptEngine->newArray(static_cast<uint>(msg.len));
for (unsigned int j = 0; j < msg.len; j++) dataBytes.setProperty(j, QJSValue(msg.data[j]));
args.append(dataBytes);
gotFrameFunction.call(args);
}
/* UDSScriptHelper methods */
UDSScriptHelper::UDSScriptHelper(QJSEngine *engine)
{
scriptEngine = engine;
handler = new UDS_HANDLER;
connect(handler, SIGNAL(newUDSMessage(UDS_MESSAGE)), this, SLOT(newUDSMessage(UDS_MESSAGE)));
handler->setReception(true);
}
void UDSScriptHelper::clearFilters()
{
handler->clearAllFilters();
}
void UDSScriptHelper::setFilter(QJSValue id, QJSValue mask, QJSValue bus)
{
uint32_t idVal = id.toUInt();
uint32_t maskVal = mask.toUInt();
int busVal = bus.toInt();
qDebug() << "Called uds set filter";
qDebug() << idVal << "*" << maskVal << "*" << busVal;
handler->addFilter(busVal, idVal, maskVal);
}
void UDSScriptHelper::sendUDS(QJSValue bus, QJSValue id, QJSValue service, QJSValue sublen, QJSValue subFunc, QJSValue length, QJSValue data)
{
UDS_MESSAGE msg;
msg.extended = false;
msg.ID = id.toUInt();
msg.len = length.toUInt();
msg.service = service.toUInt();
msg.subFuncLen = sublen.toUInt();
msg.subFunc = subFunc.toUInt();
if (!data.isArray()) qDebug() << "data isn't an array";
msg.data.reserve(msg.len);
for (unsigned int i = 0; i < msg.len; i++)
{
msg.data.append(static_cast<uint8_t>(data.property(i).toInt()));
}
msg.bus = (uint32_t)bus.toInt();
if (msg.ID > 0x7FF) msg.extended = true;
qDebug() << "sending UDS message from script";
handler->sendUDSFrame(msg);
}
void UDSScriptHelper::setRxCallback(QJSValue cb)
{
gotFrameFunction = cb;
}
void UDSScriptHelper::newUDSMessage(UDS_MESSAGE msg)
{
//qDebug() << "udsScriptHelper got a UDS message";
qDebug() << "UDS script helper. Meg data len: " << msg.len;
if (!gotFrameFunction.isCallable()) return; //nothing to do if we can't even call the function
//qDebug() << "Got frame in script interface";
QJSValueList args;
args << msg.bus << msg.ID << msg.service << msg.subFunc << msg.len;
QJSValue dataBytes = scriptEngine->newArray(msg.len);
for (unsigned int j = 0; j < msg.data.length(); j++) dataBytes.setProperty(j, QJSValue(msg.data[j]));
args.append(dataBytes);
gotFrameFunction.call(args);
}