-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvinmanager.cpp
More file actions
468 lines (411 loc) · 14.4 KB
/
Copy pathvinmanager.cpp
File metadata and controls
468 lines (411 loc) · 14.4 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "vinmanager.h"
#include <QDebug>
#include <QRandomGenerator>
#include <QDateTime>
#include <QNetworkInterface>
VinManager::VinManager(QObject *parent)
: QObject(parent)
, m_settings("/etc/carputer/vin.conf", QSettings::IniFormat)
, m_bodyBound(false)
, m_sensorBound(false)
, m_meshBound(false)
, m_remoteBound(false)
, m_busy(false)
{
loadConfig();
// Status timer — clear status after 5 seconds
m_statusTimer.setSingleShot(true);
m_statusTimer.setInterval(5000);
connect(&m_statusTimer, &QTimer::timeout, this, [this]() {
m_statusText.clear();
emit statusTextChanged();
});
// Sensor module replies via UDP (port 5001 — same as sensor data)
connect(&m_sensorSocket, &QUdpSocket::readyRead, this, &VinManager::onSensorBindReply);
// Body controller replies via TCP
connect(&m_bodySocket, &QTcpSocket::readyRead, this, &VinManager::onBodyBindReply);
connect(&m_bodySocket, &QTcpSocket::connected, this, [this]() {
qDebug() << "[VinManager] Body controller TCP connected";
});
connect(&m_bodySocket, &QTcpSocket::errorOccurred, this, [this](QAbstractSocket::SocketError) {
qDebug() << "[VinManager] Body controller TCP error:" << m_bodySocket.errorString();
if (m_pendingBindType == "body") {
setBusy(false);
emit bindFailed("body", m_bodySocket.errorString());
setStatus("Body bind failed: " + m_bodySocket.errorString());
}
});
}
void VinManager::loadConfig()
{
m_vin = m_settings.value("vin").toString();
m_secret = m_settings.value("secret").toString();
// Body Controller
m_bodyBound = m_settings.value("body/bound", false).toBool();
m_bodyModuleId = m_settings.value("body/moduleId").toString();
m_bodyMac = m_settings.value("body/mac").toString();
// Sensor Module
m_sensorBound = m_settings.value("sensor/bound", false).toBool();
m_sensorModuleId = m_settings.value("sensor/moduleId").toString();
m_sensorMac = m_settings.value("sensor/mac").toString();
// Mesh Module
m_meshBound = m_settings.value("mesh/bound", false).toBool();
m_meshModuleId = m_settings.value("mesh/moduleId").toString();
m_meshMac = m_settings.value("mesh/mac").toString();
// Remote Fob
m_remoteBound = m_settings.value("remote/bound", false).toBool();
m_remoteModuleId = m_settings.value("remote/moduleId").toString();
m_remoteMac = m_settings.value("remote/mac").toString();
if (!m_vin.isEmpty()) emit vinChanged();
if (!m_secret.isEmpty()) emit secretChanged();
if (m_bodyBound) { emit bodyBoundChanged(); emit bodyModuleIdChanged(); emit bodyMacChanged(); }
if (m_sensorBound) { emit sensorBoundChanged(); emit sensorModuleIdChanged(); emit sensorMacChanged(); }
if (m_meshBound) { emit meshBoundChanged(); emit meshModuleIdChanged(); emit meshMacChanged(); }
if (m_remoteBound) { emit remoteBoundChanged(); emit remoteModuleIdChanged(); emit remoteMacChanged(); }
}
void VinManager::saveConfig()
{
m_settings.setValue("vin", m_vin);
m_settings.setValue("secret", m_secret);
m_settings.setValue("body/bound", m_bodyBound);
m_settings.setValue("body/moduleId", m_bodyModuleId);
m_settings.setValue("body/mac", m_bodyMac);
m_settings.setValue("sensor/bound", m_sensorBound);
m_settings.setValue("sensor/moduleId", m_sensorModuleId);
m_settings.setValue("sensor/mac", m_sensorMac);
m_settings.setValue("mesh/bound", m_meshBound);
m_settings.setValue("mesh/moduleId", m_meshModuleId);
m_settings.setValue("mesh/mac", m_meshMac);
m_settings.setValue("remote/bound", m_remoteBound);
m_settings.setValue("remote/moduleId", m_remoteModuleId);
m_settings.setValue("remote/mac", m_remoteMac);
m_settings.sync();
}
void VinManager::generateSecret()
{
QByteArray randomBytes;
randomBytes.resize(32);
for (int i = 0; i < 32; i++)
randomBytes[i] = (char)QRandomGenerator::global()->bounded(256);
m_secret = randomBytes.toHex();
emit secretChanged();
}
QString VinManager::generateModuleId(const QString &prefix, const QString &mac, const QString &vinStr)
{
QByteArray data = (mac + vinStr + QDateTime::currentDateTimeUtc().toString(Qt::ISODate)).toUtf8();
QString hash = QCryptographicHash::hash(data, QCryptographicHash::Sha256).toHex().left(8);
return prefix + "-" + hash.toUpper();
}
// --- Setters ---
void VinManager::setVin(const QString &vin)
{
QString normalized = vin.trimmed().toUpper();
if (normalized == m_vin)
return;
// Validate: exactly 17 alphanumeric characters
if (!normalized.isEmpty() && !QRegularExpression("^[A-HJ-NPR-Z0-9]{17}$").match(normalized).hasMatch()) {
setStatus("Invalid VIN — must be 17 alphanumeric chars (no I, O, Q)");
return;
}
m_vin = normalized;
m_settings.setValue("vin", m_vin);
m_settings.sync();
emit vinChanged();
setStatus("VIN set: " + m_vin);
}
// --- Bind actions ---
void VinManager::bindBody()
{
if (m_vin.isEmpty()) {
setStatus("Set VIN first");
return;
}
setStatus("Binding body controller...");
setBusy(true);
m_pendingBindType = "body";
m_pendingBindVin = m_vin;
sendBindToBody(m_vin);
}
void VinManager::bindSensor()
{
if (m_vin.isEmpty()) {
setStatus("Set VIN first");
return;
}
setStatus("Binding sensor module...");
setBusy(true);
m_pendingBindType = "sensor";
m_pendingBindVin = m_vin;
sendBindToSensor(m_vin);
}
void VinManager::bindMesh()
{
// Mesh module not yet created — store locally for now
if (m_vin.isEmpty()) {
setStatus("Set VIN first");
return;
}
QString moduleId = generateModuleId("mesh", "00:00:00:00:00:00", m_vin);
m_meshBound = true;
m_meshModuleId = moduleId;
m_meshMac = "00:00:00:00:00:00";
saveConfig();
emit meshBoundChanged();
emit meshModuleIdChanged();
emit meshMacChanged();
setStatus("Mesh module bound: " + moduleId);
emit bindSucceeded("mesh", moduleId);
}
void VinManager::bindRemote()
{
// Remote fob not yet created — store locally for now
if (m_vin.isEmpty()) {
setStatus("Set VIN first");
return;
}
QString moduleId = generateModuleId("RK", "00:00:00:00:00:00", m_vin);
m_remoteBound = true;
m_remoteModuleId = moduleId;
m_remoteMac = "00:00:00:00:00:00";
saveConfig();
emit remoteBoundChanged();
emit remoteModuleIdChanged();
emit remoteMacChanged();
setStatus("Remote fob bound: " + moduleId);
emit bindSucceeded("remote", moduleId);
}
// --- Unbind actions ---
void VinManager::unbindBody()
{
sendUnbindToBody();
m_bodyBound = false;
m_bodyModuleId.clear();
m_bodyMac.clear();
saveConfig();
emit bodyBoundChanged();
emit bodyModuleIdChanged();
emit bodyMacChanged();
setStatus("Body controller unbound");
emit unbindSucceeded("body");
}
void VinManager::unbindSensor()
{
sendUnbindToSensor();
m_sensorBound = false;
m_sensorModuleId.clear();
m_sensorMac.clear();
saveConfig();
emit sensorBoundChanged();
emit sensorModuleIdChanged();
emit sensorMacChanged();
setStatus("Sensor module unbound");
emit unbindSucceeded("sensor");
}
void VinManager::unbindMesh()
{
m_meshBound = false;
m_meshModuleId.clear();
m_meshMac.clear();
saveConfig();
emit meshBoundChanged();
emit meshModuleIdChanged();
emit meshMacChanged();
setStatus("Mesh module unbound");
emit unbindSucceeded("mesh");
}
void VinManager::unbindRemote()
{
m_remoteBound = false;
m_remoteModuleId.clear();
m_remoteMac.clear();
saveConfig();
emit remoteBoundChanged();
emit remoteModuleIdChanged();
emit remoteMacChanged();
setStatus("Remote fob unbound");
emit unbindSucceeded("remote");
}
void VinManager::unbindAll()
{
unbindBody();
unbindSensor();
unbindMesh();
unbindRemote();
m_secret.clear();
emit secretChanged();
setStatus("All modules unbound");
emit allUnbound();
}
// --- Network send ---
void VinManager::sendBindToSensor(const QString &vinStr)
{
QJsonObject cmd;
cmd["cmd"] = "bind";
cmd["vin"] = vinStr;
QJsonDocument doc(cmd);
QByteArray data = doc.toJson(QJsonDocument::Compact);
// Send to sensor module (UDP port 5002 — sensor command port)
m_sensorSocket.writeDatagram(data, QHostAddress("192.168.4.20"), 5002);
qDebug() << "[VinManager] Sent bind to sensor module:" << QString::fromUtf8(data);
}
void VinManager::sendBindToBody(const QString &vinStr)
{
QJsonObject cmd;
cmd["cmd"] = "bind";
cmd["vin"] = vinStr;
QJsonDocument doc(cmd);
QByteArray data = doc.toJson(QJsonDocument::Compact);
if (m_bodySocket.state() == QAbstractSocket::ConnectedState) {
m_bodySocket.write(data);
m_bodySocket.write("\n");
m_bodySocket.flush();
} else {
// Connect first, then send
m_bodySocket.connectToHost(QHostAddress("192.168.4.1"), 5000);
connect(&m_bodySocket, &QTcpSocket::connected, this, [this, data]() {
m_bodySocket.write(data);
m_bodySocket.write("\n");
m_bodySocket.flush();
disconnect(&m_bodySocket, &QTcpSocket::connected, nullptr, nullptr);
});
}
qDebug() << "[VinManager] Sent bind to body controller:" << QString::fromUtf8(data);
}
void VinManager::sendUnbindToSensor()
{
QJsonObject cmd;
cmd["cmd"] = "unbind";
QJsonDocument doc(cmd);
QByteArray data = doc.toJson(QJsonDocument::Compact);
m_sensorSocket.writeDatagram(data, QHostAddress("192.168.4.20"), 5002);
}
void VinManager::sendUnbindToBody()
{
QJsonObject cmd;
cmd["cmd"] = "unbind";
QJsonDocument doc(cmd);
QByteArray data = doc.toJson(QJsonDocument::Compact);
if (m_bodySocket.state() == QAbstractSocket::ConnectedState) {
m_bodySocket.write(data);
m_bodySocket.write("\n");
m_bodySocket.flush();
}
}
// --- Network receive ---
void VinManager::onSensorBindReply()
{
while (m_sensorSocket.hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(m_sensorSocket.pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
m_sensorSocket.readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(datagram, &parseError);
if (parseError.error != QJsonParseError::NoError || !doc.isObject()) {
qDebug() << "[VinManager] Sensor reply parse error:" << parseError.errorString();
continue;
}
QJsonObject obj = doc.object();
QString event = obj["event"].toString();
if (event == "bound") {
m_sensorModuleId = obj["moduleId"].toString();
m_sensorMac = obj["mac"].toString();
m_sensorBound = true;
m_settings.setValue("sensor/bound", true);
m_settings.setValue("sensor/moduleId", m_sensorModuleId);
m_settings.setValue("sensor/mac", m_sensorMac);
m_settings.sync();
emit sensorBoundChanged();
emit sensorModuleIdChanged();
emit sensorMacChanged();
setBusy(false);
setStatus("Sensor bound: " + m_sensorModuleId);
emit bindSucceeded("sensor", m_sensorModuleId);
qDebug() << "[VinManager] Sensor bound:" << m_sensorModuleId << "MAC:" << m_sensorMac;
} else if (event == "unbound") {
m_sensorBound = false;
m_sensorModuleId.clear();
m_sensorMac.clear();
saveConfig();
emit sensorBoundChanged();
emit sensorModuleIdChanged();
emit sensorMacChanged();
} else if (event == "bind_error") {
setBusy(false);
QString reason = obj["error"].toString();
emit bindFailed("sensor", reason);
setStatus("Sensor bind failed: " + reason);
}
}
}
void VinManager::onBodyBindReply()
{
QByteArray data = m_bodySocket.readAll();
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(data, &parseError);
if (parseError.error != QJsonParseError::NoError || !doc.isObject()) {
// Body controller might still be in text-only mode — look for text response
QString text = QString::fromUtf8(data).trimmed();
if (text.startsWith("BIND:")) {
// Text protocol bind response: "BIND:BC-XXXXXXXX\n"
QString moduleId = text.mid(5);
m_bodyModuleId = moduleId;
m_bodyBound = true;
m_bodyMac = "00:00:00:00:00:00"; // Text protocol doesn't return MAC
saveConfig();
emit bodyBoundChanged();
emit bodyModuleIdChanged();
emit bodyMacChanged();
setBusy(false);
setStatus("Body bound: " + m_bodyModuleId);
emit bindSucceeded("body", m_bodyModuleId);
}
return;
}
QJsonObject obj = doc.object();
QString event = obj["event"].toString();
if (event == "bound") {
m_bodyModuleId = obj["moduleId"].toString();
m_bodyMac = obj["mac"].toString();
m_bodyBound = true;
m_settings.setValue("body/bound", true);
m_settings.setValue("body/moduleId", m_bodyModuleId);
m_settings.setValue("body/mac", m_bodyMac);
m_settings.sync();
emit bodyBoundChanged();
emit bodyModuleIdChanged();
emit bodyMacChanged();
setBusy(false);
setStatus("Body bound: " + m_bodyModuleId);
emit bindSucceeded("body", m_bodyModuleId);
qDebug() << "[VinManager] Body bound:" << m_bodyModuleId << "MAC:" << m_bodyMac;
} else if (event == "unbound") {
m_bodyBound = false;
m_bodyModuleId.clear();
m_bodyMac.clear();
saveConfig();
emit bodyBoundChanged();
emit bodyModuleIdChanged();
emit bodyMacChanged();
} else if (event == "bind_error") {
setBusy(false);
QString reason = obj["error"].toString();
emit bindFailed("body", reason);
setStatus("Body bind failed: " + reason);
}
}
// --- Helpers ---
void VinManager::setStatus(const QString &msg)
{
m_statusText = msg;
emit statusTextChanged();
m_statusTimer.start();
}
void VinManager::setBusy(bool busy)
{
if (m_busy == busy)
return;
m_busy = busy;
emit busyChanged();
}