Converted more code to be easier to generalize to larger CAN payloads

for CAN-FD. Improved some code to be faster. Fixed up some compile
time warnings.
This commit is contained in:
Collin Kidder
2019-12-15 15:02:06 -05:00
parent ef24036d1c
commit 8beb0e31a0
28 changed files with 589 additions and 435 deletions
+32 -27
View File
@@ -236,7 +236,7 @@ void CANScriptHelper::sendFrame(QJSValue bus, QJSValue id, QJSValue length, QJSV
if (!data.isArray()) qDebug() << "data isn't an array";
for (unsigned int i = 0; i < bytes.length(); i++)
for (int i = 0; i < bytes.length(); i++)
{
bytes[i] = (uint8_t)data.property(i).toInt();
}
@@ -254,15 +254,19 @@ 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";
unsigned char *data = reinterpret_cast<unsigned char *>(frame.payload().data());
int dataLen = frame.payload().length();
for (int i = 0; i < filters.length(); i++)
{
if (filters[i].checkFilter(frame.frameId(), frame.bus))
{
QJSValueList args;
args << frame.bus << frame.frameId() << frame.payload().length();
QJSValue dataBytes = scriptEngine->newArray(frame.payload().length());
QJSValue dataBytes = scriptEngine->newArray(dataLen);
for (unsigned int j = 0; j < frame.payload().length(); j++) dataBytes.setProperty(j, QJSValue((unsigned char)frame.payload()[j]));
for (int j = 0; j < dataLen; j++) dataBytes.setProperty(j, QJSValue(data[j]));
args.append(dataBytes);
gotFrameFunction.call(args);
return; //as soon as one filter matches we jump out
@@ -299,26 +303,29 @@ void ISOTPScriptHelper::setFilter(QJSValue id, QJSValue mask, QJSValue bus)
handler->addFilter(busVal, idVal, maskVal);
}
void ISOTPScriptHelper::sendISOTP(QJSValue bus, QJSValue id, QJSValue length, QJSValue data)
void ISOTPScriptHelper::sendISOTP(QJSValue bus, QJSValue id, QJSValue length, QJSValue dataBytes)
{
ISOTP_MESSAGE msg;
msg.extended = false;
msg.ID = id.toInt();
msg.len = length.toUInt();
msg.setExtendedFrameFormat(false);
msg.setFrameId(id.toUInt());
msg.payload().resize(length.toInt());
if (!data.isArray()) qDebug() << "data isn't an array";
unsigned char *data = reinterpret_cast<unsigned char *>(msg.payload().data());
int dataLen = msg.payload().length();
for (int i = 0; i < msg.len; i++)
if (!dataBytes.isArray()) qDebug() << "data isn't an array";
for (int i = 0; i < dataLen; i++)
{
msg.data[i] = static_cast<uint8_t>(data.property(i).toInt());
data[i] = static_cast<uint8_t>(dataBytes.property(i).toInt());
}
msg.bus = bus.toInt();
if (msg.ID > 0x7FF) msg.extended = true;
if (msg.frameId() > 0x7FF) msg.setExtendedFrameFormat(true);
qDebug() << "sending isotp message from script";
handler->sendISOTPFrame(msg.bus, msg.ID, msg.data);
handler->sendISOTPFrame(msg.bus, msg.frameId(), msg.payload());
}
void ISOTPScriptHelper::setRxCallback(QJSValue cb)
@@ -333,10 +340,10 @@ void ISOTPScriptHelper::newISOMessage(ISOTP_MESSAGE msg)
//qDebug() << "Got frame in script interface";
QJSValueList args;
args << msg.bus << msg.ID << msg.len;
QJSValue dataBytes = scriptEngine->newArray(static_cast<uint>(msg.len));
args << msg.bus << msg.frameId() << msg.payload().length();
QJSValue dataBytes = scriptEngine->newArray(static_cast<uint>(msg.payload().length()));
for (int j = 0; j < msg.len; j++) dataBytes.setProperty(static_cast<quint32>(j), QJSValue(msg.data[j]));
for (int j = 0; j < msg.payload().length(); j++) dataBytes.setProperty(static_cast<quint32>(j), QJSValue((unsigned char)msg.payload()[j]));
args.append(dataBytes);
gotFrameFunction.call(args);
}
@@ -373,25 +380,23 @@ void UDSScriptHelper::setFilter(QJSValue id, QJSValue mask, QJSValue bus)
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.setExtendedFrameFormat(false);
msg.setFrameId( id.toUInt() );
msg.payload().resize(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 (int i = 0; i < msg.len; i++)
for (int i = 0; i < msg.payload().length(); i++)
{
msg.data.append(static_cast<uint8_t>(data.property(static_cast<quint32>(i)).toInt()));
msg.payload().append(static_cast<uint8_t>(data.property(static_cast<quint32>(i)).toInt()));
}
msg.bus = bus.toInt();
if (msg.ID > 0x7FF) msg.extended = true;
if (msg.frameId() > 0x7FF) msg.setExtendedFrameFormat( true );
qDebug() << "sending UDS message from script";
@@ -406,15 +411,15 @@ void UDSScriptHelper::setRxCallback(QJSValue cb)
void UDSScriptHelper::newUDSMessage(UDS_MESSAGE msg)
{
//qDebug() << "udsScriptHelper got a UDS message";
qDebug() << "UDS script helper. Msg data len: " << msg.len;
qDebug() << "UDS script helper. Msg data len: " << msg.payload().length();
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(static_cast<unsigned int>(msg.len));
args << msg.bus << msg.frameId() << msg.service << msg.subFunc << msg.payload().length();
QJSValue dataBytes = scriptEngine->newArray(static_cast<unsigned int>(msg.payload().length()));
for (int j = 0; j < msg.data.length(); j++) dataBytes.setProperty(static_cast<quint32>(j), QJSValue(msg.data[j]));
for (int j = 0; j < msg.payload().length(); j++) dataBytes.setProperty(static_cast<quint32>(j), QJSValue((unsigned char)msg.payload()[j]));
args.append(dataBytes);
gotFrameFunction.call(args);
}