-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
224 lines (187 loc) · 6.08 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QPixmap>
#include "MfErrNo.h"
#include "Sw_Device.h"
#include "Sw_ISO14443A-3.h"
#include "Sw_Mf_Classic.h"
#include "Tools.h"
#include "TypeDefs.h"
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->readerName->setText("No reader connected");
QPixmap pixmap_logo(":/assets/assets/Odalid_Logo_120x60.jpg");
int logo_width = ui->logo->width();
int logo_height = ui->logo->height();
ui->logo->setPixmap(pixmap_logo.scaled(logo_width, logo_height, Qt::KeepAspectRatio));
QPixmap pixmap_mifare(":/assets/assets/Mifare_Logo.jpg");
int mifare_width = ui->mifare->width();
int mifare_height = ui->mifare->height();
ui->mifare->setPixmap(pixmap_mifare.scaled(mifare_width, mifare_height, Qt::KeepAspectRatio));
ui->readerName->update();
}
MainWindow::~MainWindow()
{
delete ui;
}
ReaderName MyReader;
void MainWindow::on_Connect_clicked(){
int16_t status = MI_OK;
MyReader.Type = ReaderCDC;
MyReader.device = 0;
status = OpenCOM(&MyReader);
qDebug() << "OpenCOM" << status ;
status = Version(&MyReader);
ui->readerName->setText(MyReader.version);
ui->readerName->update();
status = LEDBuzzer(&MyReader, LED_YELLOW_ON);
}
void MainWindow::on_Disconnect_clicked(){
ui->errorLabel->setText("");
int16_t status = MI_OK;
status = LEDBuzzer(&MyReader,LED_OFF);
status = CloseCOM(&MyReader);
ui->readerName->setText("No reader connected");
ui->readerName->update();
}
void MainWindow::on_Quit_clicked(){
ui->errorLabel->setText("");
int16_t status = MI_OK;
status = LEDBuzzer(&MyReader,LED_OFF);
status = CloseCOM(&MyReader);
close();
}
void MainWindow::on_ConnectCard_clicked(){
uint8_t atq[2];
uint8_t sak[1];
uint8_t uid[12];
uint16_t uid_len = 12;
int16_t status = MI_OK;
status = LEDBuzzer(&MyReader, BUZZER_ON);
DELAYS_MS(2);
status = LEDBuzzer(&MyReader, BUZZER_OFF);
status = ISO14443_3_A_PollCard(&MyReader, atq, sak, uid, &uid_len);
qDebug() << status << "aaa";
status = readCard();
if(status != MI_OK){
ui->errorLabel->setText(GetErrorMessage(status));
}
else {
status = LEDBuzzer(&MyReader, LED_RED_ON);
ui->errorLabel->setText("");
}
}
void MainWindow::on_Update_clicked(){
ui->errorLabel->setText("");
int16_t status = MI_OK;
status = LEDBuzzer(&MyReader, BUZZER_ON);
DELAYS_MS(2);
status = LEDBuzzer(&MyReader, BUZZER_OFF);
DELAYS_MS(100);
char DataIn[16];
//surnameEdit
strncpy(DataIn, ui->surnameEdit->text().toUtf8().data(), 16);
auto data = (uint8_t*)DataIn;
status = Mf_Classic_Write_Block(&MyReader, TRUE, 10, data, AuthKeyB, 2);
if(status != MI_OK){
ui->errorLabel->setText(GetErrorMessage(status));
}
//nameEdit
strncpy(DataIn, ui->nameEdit->text().toUtf8().data(), 16);
data = (uint8_t*)DataIn;
status = Mf_Classic_Write_Block(&MyReader, TRUE, 9, data, AuthKeyB, 2);
if(status != MI_OK){
ui->errorLabel->setText(GetErrorMessage(status));
}
status = LEDBuzzer(&MyReader, BUZZER_ON);
DELAYS_MS(2);
status = LEDBuzzer(&MyReader, BUZZER_OFF);
}
void MainWindow::on_Pay_clicked(){
ui->errorLabel->setText("");
int16_t status = MI_OK;
int32_t value =ui->decrementSpin->value();
qDebug() << ui->unitNumberBox->toPlainText().toInt();
if(value > ui->unitNumberBox->toPlainText().toInt()){
ui->errorLabel->setText("balance too low");
return;
}
status = LEDBuzzer(&MyReader, BUZZER_ON);
DELAYS_MS(2);
status = LEDBuzzer(&MyReader, BUZZER_OFF);
DELAYS_MS(100);
status = LEDBuzzer(&MyReader, BUZZER_ON);
DELAYS_MS(2);
status = LEDBuzzer(&MyReader, BUZZER_OFF);
status = Mf_Classic_Decrement_Value(&MyReader, TRUE, 13,value,13, AuthKeyB,3);
if(status!= MI_OK){
ui->errorLabel->setText(GetErrorMessage(status));
return;
}
ui->unitNumberBox->setText(QString::number(ui->unitNumberBox->toPlainText().toInt() - value));
ui->unitNumberBox->update();
}
void MainWindow::on_Load_clicked(){
ui->errorLabel->setText("");
int16_t status = MI_OK;
status = LEDBuzzer(&MyReader, BUZZER_ON);
DELAYS_MS(2);
status = LEDBuzzer(&MyReader, BUZZER_OFF);
DELAYS_MS(100);
status = LEDBuzzer(&MyReader, BUZZER_ON);
DELAYS_MS(2);
status = LEDBuzzer(&MyReader, BUZZER_OFF);
int32_t value =ui->incrementSpin->value();
status = Mf_Classic_Increment_Value(&MyReader, TRUE, 13,value,13, AuthKeyB,3);
if(status!= MI_OK){
ui->errorLabel->setText(GetErrorMessage(status));
}
ui->unitNumberBox->setText(QString::number(ui->unitNumberBox->toPlainText().toInt() + value));
ui->unitNumberBox->update();
}
int16_t MainWindow::readCard(){
int16_t status = MI_OK;
uint8_t data[240] = {0};
//reading identity
uint8_t sect = 2;
memset(data, 0x00, 240);
status = Mf_Classic_Read_Sector(&MyReader, TRUE, sect, data, AuthKeyA, 2);
if(status != MI_OK){
ui->errorLabel->setText(GetErrorMessage(status));
return status;
}
else{
uint8_t bloc_count = 3;
for (uint8_t bloc = 0;bloc < bloc_count ;bloc++ ) {
QString name = "";
for(uint8_t offset = 0;offset < 16; offset++){
if(data[16*bloc + offset] >= ' '){
char character = static_cast<char>(data[16*bloc + offset]);
name += character;
}
}
if(bloc==2)
ui->surnameEdit->setText(name);
if(bloc==1)
ui->nameEdit->setText(name);
}
}
//reading units
uint32_t value;
sect = 3;
uint8_t bloc = 13;
status = Mf_Classic_Read_Value(&MyReader, TRUE, bloc, &value,AuthKeyA, 3);
if(status != MI_OK){
ui->errorLabel->setText(GetErrorMessage(status));
return status;
}
else {
qDebug() << value ;
ui->unitNumberBox->setText(QString::number(value));
}
return status;
}