This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartcarddevice.cpp
94 lines (84 loc) · 2.08 KB
/
smartcarddevice.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
//
// SmartServer
// Copyright (C) 2021 АО "Нефтеавтоматика"
//
// Разработчик
// Ананьев А.А. <Ananev-AA@nefteavtomatika.ru>
//
#include <QDebug>
#include <thread>
#include "smartcarddevice.h"
#include "smartcarderror.h"
#include "scc_getid.h"
#include "scc_getfirmware.h"
///
/// \brief SmartCardDevice::SmartCardDevice
/// \param name
/// \param parent
///
SmartCardDevice::SmartCardDevice(SCARDCONTEXT hContext, const QString& name, QObject *parent)
: QObject(parent)
,_hCard(0)
,_hContext(hContext)
,_actProtocol(0)
,_name(name)
,_destroy(false)
{
std::thread([&](std::wstring name)
{
SmartCardInfo lastCard;
do
{
const auto retCode = SCardConnect(_hContext, name.c_str(), SCARD_SHARE_DIRECT, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &_hCard, &_actProtocol);
if(retCode == SCARD_S_SUCCESS)
{
const auto smi = readSmartCardInfo();
SCardDisconnect(_hCard, SCARD_LEAVE_CARD);
if(smi.isValid() && smi != lastCard)
{
lastCard = smi;
emit smartCardDetected(smi);
}
}
else
{
lastCard = SmartCardInfo();
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
while(!_destroy);
}, _name.toStdWString()).detach();
}
///
/// \brief SmartCardDevice::~SmartCardDevice
///
SmartCardDevice::~SmartCardDevice()
{
_destroy = true;
}
///
/// \brief SmartCardDevice::readSmartCardInfo
/// \return
///
SmartCardInfo SmartCardDevice::readSmartCardInfo()
{
SmartCardInfo smi;
try
{
{
SCC_GetId sc;
const auto cardId = sc.send(_hCard, _actProtocol);
smi.setId(cardId);
}
{
SCC_GetFirmware sc;
const auto firmware = sc.send(_hCard);
smi.setFirmware(firmware);
}
}
catch(std::exception& ex)
{
qWarning() << ex.what();
}
return smi;
}