-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsagemmorpho.cpp
621 lines (489 loc) · 15.8 KB
/
sagemmorpho.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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#include "sagemmorpho.h"
#include "console.h"
#include "Morpho/morpho_protocol.h"
#include "Morpho/morpho_get_descriptor.h"
#include "Morpho/morpho_configure_uart.h"
#include "Morpho/morpho_add_base_record.h"
#include "Morpho/morpho_get_data.h"
#include "Morpho/morpho_get_base_config.h"
#include "Morpho/morpho_erase_base.h"
#include "Morpho/morpho_destroy_base.h"
#include "Morpho/morpho_create_base.h"
#include "Morpho/morpho_identify.h"
#include "Morpho/morpho_cancel.h"
#include "Morpho/morpho_asynchronous_message.h"
#include "Morpho/Ilv_errors.h"
#include "ui_sagemmorpho.h"
SagemMorpho::SagemMorpho(QWidget *parent) :
QWidget(parent),
ui(new Ui::SagemMorpho),
m_request(MorphoRequest_None),
m_userId(1),
m_repeat(1),
m_receiveState(ReceiveState_ReceiveSOP),
m_ackDisable(false)
{
ui->setupUi(this);
connect(ui->console, &Console::getData, this, &SagemMorpho::getData);
}
SagemMorpho::~SagemMorpho()
{
delete ui;
}
void SagemMorpho::receiveSOP()
{
size_t responseSize = m_response.length();
uint8_t packet[128];
size_t packetSize = (responseSize < sizeof(packet))? responseSize : sizeof(packet);
memset(packet, 0, sizeof(packet));
memcpy(packet, m_response.data(), packetSize);
//check ack-nack
size_t sopSize = 0;
uint8_t rc = 0;
int err = MORPHO_ReceiveSOP(packet, packetSize, &rc, &sopSize);
if(err != MORPHO_OK)
{
if(err == MORPHO_ERR_RESPONSE_NACK)
{
ui->console->putDataHex(m_response);
m_request = MorphoRequest_None;
return;
}
}
else if(err == MORPHO_OK)
{
ui->console->putDataHex(m_response.mid(0, sopSize));
m_response.remove(0, sopSize);
m_receiveState = ReceiveState_ReceiveData;
}
}
void SagemMorpho::receiveData()
{
size_t responseSize = m_response.length();
uint8_t packet[10*1024];
size_t packetSize = (responseSize < sizeof(packet))? responseSize : sizeof(packet);
memset(packet, 0, sizeof(packet));
memcpy(packet, m_response.data(), packetSize);
//check data
uint8_t* value = NULL;
size_t valueSize = 0;
uint8_t identifier = 0;
int err = MORPHO_ReceiveData(packet, packetSize, &identifier, &value, &valueSize);
if(err != MORPHO_OK)
{
return;
}
uint8_t ilvErr = ILV_OK;
uint8_t ilvStatus = ILVSTS_OK;
switch(identifier)
{
case MorphoRequest_GetDescriptor:
{
const char* product = NULL;
const char* sensor = NULL;
const char* software = NULL;
err = MORPHO_GetDescriptor_Response(value, valueSize, &ilvErr, &product, &sensor, &software);
if(product)
ui->console->putData(product, false);
if(sensor)
ui->console->putData(sensor, false);
if(software)
ui->console->putData(software, false);
ui->console->putDataRaw("\r\n");
}
break;
case MorphoRequest_GetBaseConfig:
{
SMorpho_GetBaseConfig params = {};
err = MORPHO_GetBaseConfig_Response(value, valueSize, &ilvErr, ¶ms);
QString sparams = QString("GetBaseConfig fingerNb:%1; recNb:%2 %3/%4; fieldNb:%5\r\n").arg(params.fingerNb).arg(params.recNb).arg(params.recNbFree).arg(params.recNbMax).arg(params.fieldNb);
ui->console->putData(sparams.toUtf8(), false);
for(int i = 0; i < params.fieldNb; i++)
{
QString sfield = QString("PublicField[%1] size:%2; name:'%3'\r\n").arg(i).arg(params.fieldDescription[i].size).arg(params.fieldDescription[i].name);
ui->console->putData(sfield.toUtf8(), false);
}
//ui->console->putDataHex(m_response);
}
break;
case MorphoRequest_AddBaseRecord:
{
uint8_t baseStatus = 0;
uint32_t userIndex = 0;
err = MORPHO_AddBaseRecord_Response(value, valueSize, &ilvErr, &ilvStatus, &userIndex);
ui->console->putDataHex(m_response);
}
break;
case MorphoRequest_GetData:
{
const char* userData = NULL;
err = MORPHO_GetData_Response(value, valueSize, &ilvErr, &userData);
ui->console->putDataHex(m_response);
}
break;
case MorphoRequest_ConfigureUart:
{
err = MORPHO_ConfigureUart_Response(value, valueSize, &ilvErr);
ui->console->putDataHex(m_response);
}
break;
case MorphoRequest_EraseBase:
{
err = MORPHO_EraseBase_Response(value, valueSize, &ilvErr);
ui->console->putDataHex(m_response);
}
break;
case MorphoRequest_DestroyBase:
{
err = MORPHO_DestroyBase_Response(value, valueSize, &ilvErr);
ui->console->putDataHex(m_response);
}
break;
case MorphoRequest_CreateBase:
{
err = MORPHO_CreateBase_Response(value, valueSize, &ilvErr);
ui->console->putDataHex(m_response);
}
break;
case MorphoRequest_Identify:
{
uint32_t userIndex = 0;
const char* userId = NULL;
err = MORPHO_Identify_Response(value, valueSize, &ilvErr, &ilvStatus, &userIndex, &userId);
ui->console->putDataHex(m_response);
}
break;
case MorphoRequest_AsynMessage:
{
const char* msg = NULL;
ui->console->putDataHex(m_response);
err = MORPHO_AsynMeassage_Response(value, valueSize, &ilvErr, &msg);
if(msg)
{
ui->console->putData(msg, false);
ui->console->putDataRaw("\r\n");
}
}
break;
default:
{
ui->console->putDataHex(m_response);
QString stat = QString("Unexpected data [id:%1]\r\n").arg(identifier);
ui->console->putDataRaw(stat.toUtf8());
}
break;
}
if(err != MORPHO_OK)
{
switch(err)
{
case MORPHO_WARN_VAL_ILV_ERROR:
{
QString ilv = QStringLiteral("0x%1").arg(ilvErr, 2, 16, QLatin1Char('0'));
QString stat = QString("ILV err [%1] - %2\r\n").arg(ilv).arg(QString((const char*)IlvConvertError(ilvErr)));
ui->console->putDataRaw(stat.toUtf8());
}
break;
case MORPHO_WARN_VAL_ILV_STATUS:
{
QString ilv = QStringLiteral("0x%1").arg(ilvStatus, 2, 16, QLatin1Char('0'));
QString stat = QString("ILV status [%1] - %2\r\n").arg(ilv).arg(QString((const char*)IlvConvertStatus(ilvStatus)));
ui->console->putDataRaw(stat.toUtf8());
}
break;
default:
break;
}
}
if(!m_ackDisable)
ack();
//repeat
switch(m_request)
{
case MorphoRequest_AddBaseRecord:
m_repeat--;
if(m_repeat > 0)
{
addRecord(++m_userId);
}
else
{
m_repeat = 1;
m_request = MorphoRequest_None;
}
break;
default:
m_repeat = 1;
m_request = MorphoRequest_None;
break;
}
}
void SagemMorpho::ack()
{
//ACK
uint8_t ack[4];
size_t ackSize = sizeof(ack);
MORPHO_ResponseAck(ack, &ackSize);
QByteArray request;
request.append((char*)ack, ackSize);
ui->console->setDataHex(request);
}
void SagemMorpho::putData(const QByteArray &data)
{
//process response
if( m_request != MorphoRequest_None)
{
//data
m_response.append(data);
switch(m_receiveState)
{
case ReceiveState_ReceiveSOP:
receiveSOP();
//check if data
if(m_receiveState == ReceiveState_ReceiveData)
{
receiveData();
}
break;
case ReceiveState_ReceiveData:
//ui->console->putDataHex(data, false);
//ui->console->putData(data, false);
receiveData();
break;
}
}
else
{
if(data.at(0) == 0x02)
{
//unexpected data
if(data.size() > 3 &&
(uint8_t)data.at(0) == STX &&
(uint8_t)data.at(1) == 0xE1 && //data packet
(uint8_t)data.at(data.size() - 1) == ETX)
{
//data
m_response.clear();
m_response.append(data);
receiveData();
}
else
{
ui->console->putDataHex(data);
}
}
else
ui->console->putData(data);
}
}
void SagemMorpho::setLocalEchoEnabled(bool set)
{
ui->console->setLocalEchoEnabled(set);
}
void SagemMorpho::clear()
{
ui->console->clear();
}
void SagemMorpho::sendBreak()
{
m_request = MorphoRequest_None;
ui->console->setData("$BRK\r\n");
MORPHO_ResetCounter();
}
void SagemMorpho::sendSag0()
{
m_request = MorphoRequest_None;
ui->console->setData("$SAG00\r\n");
}
void SagemMorpho::sendSag1()
{
m_request = MorphoRequest_None;
ui->console->setData("$SAG01\r\n");
}
void SagemMorpho::sendBdr0()
{
m_request = MorphoRequest_None;
ui->console->setData("$BDR00\r\n");
}
void SagemMorpho::sendBdr1()
{
m_request = MorphoRequest_None;
ui->console->setData("$BDR01\r\n");
}
void SagemMorpho::sendTrace()
{
m_request = MorphoRequest_None;
ui->console->setData("$TRACE\r\n");
}
void SagemMorpho::sendIdentify0()
{
m_request = MorphoRequest_None;
uint8_t rc = MORPHO_GetCounter();
uint8_t next = (uint8_t)(rc + 1);
MORPHO_SetCounter(next);
QString cmd = QString("$IDF00%1%2\r\n").arg(rc, 3, 10, QLatin1Char('0')).arg("05");
ui->console->setData(QByteArray::fromStdString(cmd.toStdString()));
}
void SagemMorpho::sendCancel0()
{
m_request = MorphoRequest_None;
uint8_t rc = MORPHO_GetCounter();
uint8_t next = (uint8_t)(rc + 1);
MORPHO_SetCounter(next);
QString cmd = QString("$IDC00%1\r\n").arg(rc, 3, 10, QLatin1Char('0'));
ui->console->setData(QByteArray::fromStdString(cmd.toStdString()));
}
void SagemMorpho::sendIdentify1()
{
m_request = MorphoRequest_None;
uint8_t rc = MORPHO_GetCounter();
uint8_t next = (uint8_t)(rc + 1);
MORPHO_SetCounter(next);
QString cmd = QString("$IDF01%1%2\r\n").arg(rc, 3, 10, QLatin1Char('0')).arg("05");
ui->console->setData(QByteArray::fromStdString(cmd.toStdString()));
}
void SagemMorpho::sendCancel1()
{
m_request = MorphoRequest_None;
uint8_t rc = MORPHO_GetCounter();
uint8_t next = (uint8_t)(rc + 1);
MORPHO_SetCounter(next);
QString cmd = QString("$IDC01%1\r\n").arg(rc, 3, 10, QLatin1Char('0'));
ui->console->setData(QByteArray::fromStdString(cmd.toStdString()));
}
void SagemMorpho::on_desciptorButton_clicked()
{
m_request = MorphoRequest_GetDescriptor;
m_receiveState = ReceiveState_ReceiveSOP;
m_response.clear();
uint8_t data[1024];
size_t dataSize = sizeof(data);
MORPHO_GetDescriptor_Request(data, &dataSize);
QByteArray request;
request.append((char*)data, dataSize);
ui->console->setDataHex(request);
}
void SagemMorpho::on_configUartButton_clicked()
{
m_request = MorphoRequest_ConfigureUart;
m_receiveState = ReceiveState_ReceiveSOP;
m_response.clear();
uint8_t data[1024];
size_t dataSize = sizeof(data);
MORPHO_ConfigureUart_Request(data, &dataSize, 115200);
QByteArray request;
request.append((char*)data, dataSize);
ui->console->setDataHex(request);
}
static const uint8_t tmplate[] =
{
0x82, 0x39, 0x00, 0x52, 0x5D, 0xFF, 0x80, 0x80, 0x91, 0x69, 0x9E, 0x66, 0x5C, 0xAE, 0x9C, 0x7E,
0xA7, 0x7D, 0x59, 0x9F, 0x58, 0x95, 0x3F, 0x95, 0x4D, 0x7C, 0x4E, 0x8D, 0x67, 0x8D, 0x35, 0x88,
0x43, 0x68, 0x71, 0x88, 0x6C, 0x76, 0x80, 0x88, 0xB6, 0x93, 0x60, 0x64, 0x7E, 0x81, 0xB0, 0x9F,
0x55, 0x58, 0x69, 0x66, 0x9F, 0xA2, 0x86, 0x6B, 0x82, 0x65, 0x85, 0x5A, 0x8D, 0x7E, 0x85, 0xA8,
0x94, 0x62, 0xA2, 0x5B, 0x6A, 0xB2, 0x9B, 0x4C, 0xA1, 0x75, 0xBC, 0x5E, 0xA1, 0x83, 0x74, 0x9B,
0xAC, 0x88, 0x9D, 0x8E, 0x36, 0x79, 0x46, 0x8F, 0x4D, 0x88, 0x29, 0x81, 0x29, 0x97, 0x3C, 0x64,
0x52, 0x74, 0x59, 0x8A, 0x73, 0x8A, 0x66, 0x7D, 0xAE, 0x95, 0x4C, 0x6C, 0x4F, 0x60, 0x53, 0x58,
0x95, 0x9B, 0x7B, 0x7F, 0x87, 0x95, 0x82, 0x71, 0x8C, 0xB0, 0xBB, 0x3F, 0x6B, 0x87, 0x18, 0x91,
0xB5, 0xC9, 0xB2, 0xDD, 0xDD, 0x52, 0x20, 0xB1, 0x59, 0xFF
};
void SagemMorpho::on_addRecordButton_clicked()
{
m_userId = ui->userStartBox->value();
m_repeat = ui->userCountBox->value();
addRecord(m_userId);
}
void SagemMorpho::addRecord(int userId)
{
m_request = MorphoRequest_AddBaseRecord;
m_receiveState = ReceiveState_ReceiveSOP;
m_response.clear();
size_t tmplateSize = sizeof(tmplate);
uint8_t tmplateId = ID_PK_COMP;
const char* userData[] = {"name", "surname"};
size_t userDataSize = 2;
uint8_t no_check = 1;
QString user = QStringLiteral("%1 ").arg(userId, 8, 10, QLatin1Char('0'));
uint8_t data[1024];
size_t dataSize = sizeof(data);
MORPHO_AddBaseRecord_Request(data, &dataSize, tmplate, tmplateSize, tmplateId, user.toStdString().c_str(), userData, userDataSize, no_check);
QByteArray request;
request.append((char*)data, dataSize);
ui->console->setDataHex(request);
}
void SagemMorpho::on_eraseBaseButton_clicked()
{
m_request = MorphoRequest_EraseBase;
m_receiveState = ReceiveState_ReceiveSOP;
m_response.clear();
uint8_t data[1024];
size_t dataSize = sizeof(data);
MORPHO_EraseBase_Request(data, &dataSize);
QByteArray request;
request.append((char*)data, dataSize);
ui->console->setDataHex(request);
}
void SagemMorpho::on_destroyBaseButton_clicked()
{
m_request = MorphoRequest_DestroyBase;
m_receiveState = ReceiveState_ReceiveSOP;
m_response.clear();
uint8_t data[1024];
size_t dataSize = sizeof(data);
MORPHO_DestroyBase_Request(data, &dataSize);
QByteArray request;
request.append((char*)data, dataSize);
ui->console->setDataHex(request);
}
void SagemMorpho::on_createBaseButton_clicked()
{
m_request = MorphoRequest_CreateBase;
m_receiveState = ReceiveState_ReceiveSOP;
m_response.clear();
uint8_t data[1024];
size_t dataSize = sizeof(data);
uint16_t nbRec = ui->dbsRecNbBox->value();
MORPHO_CreateBase_Request(data, &dataSize, nbRec);
QByteArray request;
request.append((char*)data, dataSize);
ui->console->setDataHex(request);
}
void SagemMorpho::on_getBaseConfigButton_clicked()
{
m_request = MorphoRequest_GetBaseConfig;
m_receiveState = ReceiveState_ReceiveSOP;
m_response.clear();
uint8_t data[1024];
size_t dataSize = sizeof(data);
MORPHO_GetBaseConfig_Request(data, &dataSize);
QByteArray request;
request.append((char*)data, dataSize);
ui->console->setDataHex(request);
}
void SagemMorpho::on_identifyButton_clicked()
{
m_request = MorphoRequest_Identify;
m_receiveState = ReceiveState_ReceiveSOP;
m_response.clear();
uint8_t data[1024];
size_t dataSize = sizeof(data);
uint16_t keepAlive = 0;
if(ui->keepAliveBox->isChecked())
keepAlive = 10;
MORPHO_Identify_Request(data, &dataSize, 0, 5, keepAlive);
QByteArray request;
request.append((char*)data, dataSize);
ui->console->setDataHex(request);
}
void SagemMorpho::on_cancelButton_clicked()
{
m_request = MorphoRequest_Cancel;
m_receiveState = ReceiveState_ReceiveSOP;
m_response.clear();
uint8_t data[1024];
size_t dataSize = sizeof(data);
MORPHO_Cancel_Request(data, &dataSize);
QByteArray request;
request.append((char*)data, dataSize);
ui->console->setDataHex(request);
}