-
Notifications
You must be signed in to change notification settings - Fork 1
/
beanpacket.cpp
287 lines (230 loc) · 7.48 KB
/
beanpacket.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
#include "beanpacket.h"
#include "mainwindow.h"
#include "beanpacketwidget.h"
#include "beanpacketssender.h"
#include <QApplication>
#include <QRegularExpressionMatch>
#include <QDateTime>
#include <QDebug>
#include <QTimer>
BeanPacket::BeanPacket() = default;
BeanPacket::~BeanPacket() = default;
BeanPacket::BeanPacket(BeanPacket *srcPacket) {
dstId = srcPacket->dstId;
msgId = srcPacket->msgId;
dlc = srcPacket->dlc;
memcpy(&data, &srcPacket->data, 11);
prio = srcPacket->prio;
parsedDlc = srcPacket->parsedDlc;
crc = srcPacket->crc;
bad = srcPacket->bad;
debug = srcPacket->debug;
timeEpoch = srcPacket->timeEpoch;
counter = srcPacket->counter;
repeat = srcPacket->repeat;
ro = srcPacket->ro;
}
BeanPacket *BeanPacket::fromSerialMsg(const QString &serialMsg) {
auto beanPacket = new BeanPacket();
beanPacket->timeEpoch = QDateTime::currentMSecsSinceEpoch();
QRegularExpression re(
".*Bytes:\\s(?<prio>[a-f0-9]?)(?<len>[a-f0-9])\\s(?<dst>[a-f0-9]+)\\s(?<msg>[a-f0-9]+)\\s(?<data>.*)\\s"
"(?<tail>(?:[0-9a-f]+\\s){3})CRC:\\s"
"(?<crc2>[a-f0-9]+)\\s+\\(?(?<count>[0-9]+\\/[0-9]+)\\)?.*");
if (!re.isValid()) {
qWarning("Wrong regexp!");
}
QRegularExpressionMatch match = re.match(serialMsg);
if (!match.hasMatch()) {
beanPacket->bad = false;
return beanPacket;
}
//QString capCount = match.captured("count");
beanPacket->parsedDlc = match.captured("len");
beanPacket->debug = serialMsg;
bool finalOk = true;
bool ok;
beanPacket->prio = match.captured("prio").toUInt(&ok, 16);
if (!ok) finalOk = false;
beanPacket->dstId = match.captured("dst").toUInt(&ok, 16);
if (!ok) finalOk = false;
beanPacket->msgId = match.captured("msg").toUInt(&ok, 16);
if (!ok) finalOk = false;
// QString captData = ;
// qDebug() << "Captured data: " << captData;
// QString parsedData = parseData(captData);
// qDebug() << "Parsed data: " << parsedData.data();
beanPacket->dataFrom(match.captured("data"));
beanPacket->crc = match.captured("crc2").toUInt(&ok, 16);
if (!ok) finalOk = false;
if (finalOk) {
beanPacket->bad = false;
}
return beanPacket;
}
BeanPacket *BeanPacket::fromCSVLine(const QString &csvLine) {
QStringList localSplit = csvLine.split(";");
auto beanPacket = new BeanPacket();
qDebug() << "split size: " << localSplit.size();
if (localSplit.size() == 9) {
beanPacket->bad = true;
} else {
return beanPacket;
}
bool ok;
beanPacket->dstId = trimQuotes(localSplit[3]).toUInt(&ok, 16);
if (!ok) beanPacket->bad = false;
beanPacket->msgId = trimQuotes(localSplit[4]).toUInt(&ok, 16);
if (!ok) beanPacket->bad = false;
beanPacket->dlc = trimQuotes(localSplit[5]).toUInt(&ok, 16);
if (!ok) beanPacket->bad = false;
beanPacket->dataFrom(parseData(trimQuotes(localSplit[6])));
beanPacket->crc = trimQuotes(localSplit[7]).toUInt(&ok, 16);
if (!ok) beanPacket->bad = false;
QString prioStr = "0x0" + trimQuotes(localSplit[8]);
qDebug() << "Prio: " << prioStr;
beanPacket->prio = prioStr.toUInt(&ok, 16);
if (!ok) beanPacket->bad = false;
beanPacket->timeEpoch = trimQuotes(localSplit[2]).toUInt(&ok, 16);
if (!ok) beanPacket->bad = false;
return beanPacket;
}
void BeanPacket::sendToSerialBin(QSerialPort *seriall) {
qDebug() << "Sending to serial " << this;
uint8_t payload[14];
payload[0] = prio;
payload[1] = msgId;
payload[2] = dlc;
uint8_t len = dlc + 3;
memcpy(&payload[3], &data[0], dlc);
seriall->write("Test\n");
seriall->write((const char *) payload, sizeof(len));
bool sent = seriall->waitForBytesWritten();
qDebug() << "sent packet: " << sent;
}
void BeanPacket::sendPacket() {
auto seriall = MainWindow::getSerialPort();
if (seriall) {
sendToSerialBin(seriall);
counter++;
notifyModel();
} else {
qWarning() << "Serial is not connected!";
}
}
void BeanPacket::notifyModel() {
foreach(QWidget *widget, qApp->topLevelWidgets()) {
if (auto *mainWindow = dynamic_cast<MainWindow *>(widget)) {
auto senderWidget = mainWindow->findChild<BeanPacketsSender *>("beanPacketsSender");
if (senderWidget) {
senderWidget->dataChanged();
}
break;
}
}
}
//QSerialPort *BeanPacket::getSerialPort(void) {
// MainWindow window;
// foreach(QWidget *widget, qApp->topLevelWidgets()) {
// if (MainWindow *mainWindow = qobject_cast<MainWindow*>(widget)) {
// return mainWindow->getSerialPort();
// break;
// }
// }
// return NULL;
//}
void BeanPacket::dataFrom(const QString &str) {
dataFrom(parseData(str));
}
void BeanPacket::dataFrom(const QByteArray &arr) {
dlc = (uint8_t) arr.size();
qDebug() << "Array size: " << dlc;
for (int i = 0; i < arr.size(); i++) {
data[i] = (uint8_t) arr.at(i);
}
}
QByteArray BeanPacket::parseData(const QString &dataStr) {
QByteArray octets;
QStringList localSplit = dataStr.split(" ");
int len = 0;
for (const auto &srcOctet : localSplit) {
len++;
QString tempStr = "0x";
if (srcOctet.length() == 1) {
tempStr.append("0");
}
tempStr.append(srcOctet);
octets.resize(len);
qDebug() << " Tempstr: " << tempStr << " > " << tempStr.toUInt();
bool ok;
octets[len - 1] = tempStr.toUInt(&ok, 16);
}
return octets;
}
QString BeanPacket::parseDataStr(const QString &dataStr) {
QStringList octets;
QStringList localSplit = dataStr.split(" ");
for (const auto &srcOctet : localSplit) {
if (srcOctet.length() == 1) {
QString tempStr = "0";
tempStr.append(srcOctet);
octets.append(tempStr);
} else {
octets.append(srcOctet);
}
}
return octets.join(" ");
}
QString BeanPacket::getParsedDlc() const {
return QString::number(parsedDlc.toInt() - 2);
}
QString BeanPacket::getPrioStr() const {
return QString::number(prio);
}
QString BeanPacket::trimQuotes(QString &arg) {
QString stripped = arg;
if (arg.startsWith("\"") && arg.endsWith("\"")) {
stripped = arg.mid(1, arg.size() - 2);
}
qDebug() << "Final line: " << stripped;
return stripped;
}
void BeanPacket::sendOnce() {
QTimer::singleShot(0, this, SLOT(sendPacket()));
}
void BeanPacket::runPacketSending(const int millis) {
}
void BeanPacket::stopPacketSending() {
}
QString BeanPacket::getDataStr() const {
QString tempStr;
for (int i = 0; i < dlc; i++) {
tempStr.append(QString("%1")
.arg(data[i], 2, 16, QChar('0'))
.toUpper());
if (i != dlc - 1) {
tempStr.append(" ");
}
}
return tempStr;
}
QString BeanPacket::getDlcStr() const {
return QString("%1").arg(dlc).toUpper();
}
QString BeanPacket::getMsgIdStr() const {
return QString("%1").arg(msgId, 0, 16).toUpper();
}
QString BeanPacket::getDstIdStr() const {
return QString("%1").arg(dstId, 0, 16).toUpper();
}
QString BeanPacket::getCrcStr() const {
return QString("%1")
.arg(crc, 2, 16, QChar('0'))
.toUpper();
}
QString BeanPacket::getRepeatStr() const {
return QString("%1").arg(repeat).toUpper();
}
QString BeanPacket::getCounterStr() const {
return QString::number(counter);
}