DLCamCardManager.cpp
7.13 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
#include "stdafx.h"
#include <exception>
#include <cstdint>
#include <sstream>
#include <iomanip> // hex conversion.
#include <intrin.h>
#include "etradeclient/hardware/DLCamCardManager.h"
#include "etradeclient/hardware/ex_reference/bank_card_reader/dcrf32.h"
#include "ETradeClient/hardware/dili_card_device.h"
// Endian conversion.
static const uint8_t CARD_CHIP_NUM_LEN = 12; // 卡面号
static const uint8_t CARD_TYPE_CODE_LEN = 16; // 卡片类型码(卡类型编码)
static const uint8_t CARD_ISSUER_CODE_LEN = 20; // 发卡机构编码
static const uint8_t CARD_VERIFY_CODE_LEN = 8; // 校验码(安全码编码(3位数))
static const uint8_t CARD_SN_LEN = 32; // 卡片内码, 卡序列号SN
static const uint8_t CARD_TYPE_CODE_LEN_USE = 6;
static const uint8_t CARD_ISSUER_CODE_LEN_USE = 4;
static const uint8_t CARD_VERIFY_CODE_LEN_USE = 3;
static const char PADDING_CHAR = 'f';
template <typename T>
std::string DecToHex(T d, bool show_base = false)
{
std::stringstream ss;
if (show_base)
ss << "0x";
ss << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex << static_cast<int>(d);
return ss.str();
}
DLCamCardManager::DLCamCardManager() :
m_icdev(NULL)
{}
DLCamCardManager::~DLCamCardManager()
{
DisConnect();
}
std::string DLCamCardManager::HexToChar(unsigned char* pHex, int iLen)
{
std::string sResult;
if (pHex && iLen)
{
for (int index = 0; index < iLen; ++index)
{
char cNum[3] = { 0 };
sprintf_s(cNum, "%2X", pHex[index]);
sResult.append(cNum, 2);
}
}
return sResult;
}
bool DLCamCardManager::CheckOrderResult(unsigned char* pResult, unsigned char cLen)
{
bool bResult = false;
if (pResult[(int)cLen - 2] == 0x90 &&
pResult[(int)cLen - 1] == 0x00)
{
bResult = true;
}
return bResult;
}
bool DLCamCardManager::Connect()
{
static const uint8_t RETRY_COUNT = 10;
const int kUSBPort = 100, kBaudRate = 0; // port 为 100时,表示使用USB接口通讯,则波特率无效,因此设置为0.
const int kBeepMs = 10;
uint8_t conn_count = 0;
do
{
m_icdev = dc_init(kUSBPort, kBaudRate);
if (0 < (int)m_icdev)
{
dc_beep(m_icdev, kBeepMs);
return true;
}
++conn_count;
} while (conn_count < RETRY_COUNT);
return false;
}
bool DLCamCardManager::DisConnect()
{
bool bResult = false;
if (m_icdev)
{
int iResult = dc_exit(m_icdev);
if (iResult)
{
bResult = false;
}
else
{
bResult = true;
}
}
return bResult;
}
bool DLCamCardManager::SelectMF()
{
bool bResult = true;
const int kOK = 0;
const unsigned int kTimeout = 7; // 延迟时间,单位为:10ms, 7 来自与官方示例,该值如果设过大会导致操作失败.
const unsigned int kMaxDataSize = 512;
const unsigned char kSendDataLen = 7; // 发送的信息长度
unsigned char recv_data_len = 0; // 读取信息的长度
unsigned char send_data[kMaxDataSize] = { 0 }, recv_data[kMaxDataSize] = { 0 };
send_data[0] = 0x00; // CLA
send_data[1] = 0xA4; // INS
send_data[2] = 0x00; // P1,按文件名选择DF
send_data[3] = 0x00; // P2
send_data[4] = 0x02; // Lc 数据长度
// Data:DF文件名
send_data[5] = 0x3F;
send_data[6] = 0x00;
__int16 iResult = 0;
if (m_icdev)
{
iResult = dc_pro_command(m_icdev, kSendDataLen, send_data, &recv_data_len, recv_data, kTimeout);
}
else
{
bResult = false;
throw std::exception("Card reader handle is empty!");
}
if (iResult != kOK || !CheckOrderResult(recv_data, recv_data_len))
{
bResult = false;
throw std::exception("Select MF failed!");
}
return bResult;
}
bool DLCamCardManager::SelectFile()
{
bool bResult = false;
const int kOK = 0;
const unsigned int kTimeout = 7; // 延迟时间,单位为:10ms, 7 来自与官方示例,该值如果设过大会导致操作失败.
const unsigned int kMaxDataSize = 512;
const unsigned char kSendDataLen = 7; // 发送的信息长度
unsigned char recv_data_len = 0; // 读取信息的长度
unsigned char send_data[kMaxDataSize] = { 0 }, recv_data[kMaxDataSize] = { 0 };
send_data[0] = 0x00; // CLA
send_data[1] = 0xA4; // INS
send_data[2] = 0x00; // P1,按文件名选择DF
send_data[3] = 0x00; // P2
send_data[4] = 0x02; // Lc 数据长度
// Data:DF文件名
send_data[5] = 0x00;
send_data[6] = 0x05;
__int16 iResult = 0;
if (m_icdev)
{
iResult = dc_pro_command(m_icdev, kSendDataLen, send_data, &recv_data_len, recv_data, kTimeout);
}
else
{
bResult = false;
throw std::exception("Card reader handle is empty!");
}
if (iResult != kOK || !CheckOrderResult(recv_data, recv_data_len))
{
bResult = false;
throw std::exception("Select 0x05 file failed!");
}
return bResult;
}
std::string DLCamCardManager::ReadCardBasicFile()
{
std::string sResult;
const int kOK = 0;
const unsigned int kTimeout = 7; // 延迟时间,单位为:10ms, 7 来自与官方示例,该值如果设过大会导致操作失败.
const unsigned int kMaxDataSize = 512;
const unsigned char kSendDataLen = 5; // 发送的信息长度
unsigned char recv_data_len = 0; // 读取信息的长度
unsigned char send_data[kMaxDataSize] = { 0 }, recv_data[kMaxDataSize] = { 0 };
send_data[0] = 0x00; // CLA
send_data[1] = 0xB0; // INS
send_data[2] = 0x85; // P1
send_data[3] = 0x00; // P2,按文件记录号访问
send_data[4] = 0x2C; // Le
bool bResult = true;
__int16 iResult = 0;
if (m_icdev)
{
iResult = dc_pro_command(m_icdev, kSendDataLen, send_data, &recv_data_len, recv_data, kTimeout);
}
else
{
bResult = false;
throw std::exception("Card reader handle is empty!");
}
if (iResult != kOK || !CheckOrderResult(recv_data, recv_data_len))
{
bResult = false;
throw std::exception("Read data at 0x05 file failed!");
}
if (bResult)
{
RecordDataT sTemp = RecordDataT(recv_data, recv_data_len);
for (int index = 0; index < (int)recv_data_len - 2; ++index)
sResult.append(DecToHex(sTemp.at(index)));
}
return sResult;
}
DILICard::BasicInfo DLCamCardManager::ReadCardBasicInfo()
{
std::string basic_info_str = ReadCardBasicFile();
int iTotalLength = basic_info_str.size();
if (basic_info_str.size() < iTotalLength)
{
throw std::exception("Card basic info error!");
}
DILICard::BasicInfo basic_info;
uint32_t offset = 0;
basic_info.chip_num = basic_info_str.substr(offset, CARD_CHIP_NUM_LEN);
offset += CARD_CHIP_NUM_LEN;
std::string field;
field = basic_info_str.substr(offset, CARD_TYPE_CODE_LEN);
basic_info.type_code = field.substr(0, CARD_TYPE_CODE_LEN_USE);
offset += CARD_TYPE_CODE_LEN;
field = basic_info_str.substr(offset, CARD_ISSUER_CODE_LEN);
basic_info.issuer_code = field.substr(0, CARD_ISSUER_CODE_LEN_USE);
offset += CARD_ISSUER_CODE_LEN;
field = basic_info_str.substr(offset, CARD_VERIFY_CODE_LEN);
basic_info.verify_code = field.substr(0, CARD_VERIFY_CODE_LEN_USE);
offset += CARD_VERIFY_CODE_LEN;
//sn号全为f或者全为0时,入库会有问题,暂时用卡面号替代sn号 add by liuye 2017.7.27
//basic_info.device_id = basic_info_str.substr(offset, CARD_SN_LEN);
basic_info.device_id = basic_info.chip_num;
//
return basic_info;
}
void DLCamCardManager::FindAndResetCard() // 寻卡并复位卡片
{
unsigned long snr = 0;
unsigned char lenth[128] = { 0 };
unsigned char data[128] = { 0 };
int st = 0;
if (m_icdev)
{
st = dc_card(m_icdev, 0, &snr); // 一定要先寻卡
if (0 != st)
throw std::exception("Find dili card failed!");
st = dc_pro_reset(m_icdev, lenth, data);
if (0 != st)
throw std::exception("Reset dili card failed!");
}
else
{
throw std::exception("Card reader handle is empty!");
}
}