Демонстрирует автоматическое чтение данных карты при поднесении карты к считывателю.
#include <stdio.h>
#include <chrono>
#include <iomanip>
#include <iostream>
#include "../ilr_cpp_helpers.h"
#define ILR_LOG_FILE
using namespace ilr;
const int64_t g_TemicPassword = -1;
const CMifareClassicKey g_McKey;
const CMifarePlusKey g_MpKey;
const bool g_fOpenText = true;
ilr::CAsyncCommand g_oReadCmd;
std::chrono::steady_clock::time_point g_StartTime;
uint32_t g_nCardMemSize;
#ifdef ILR_LOG
const char kLogLevelChars[] = {'-', 'E', 'W', 'I', 'D'};
const char kLogFileName[] = "ilreaders.log";
void LogCallback(
ilr_log_level level,
const char* pContext,
const char* pMessage,
void*) {
#ifdef ILR_LOG_FILE
std::ofstream file(kLogFileName, std::ios_base::out | std::ios_base::app);
auto& out = file;
#else
auto& out = std::cout;
#endif
auto t = std::time(nullptr);
auto tmb = std::localtime(&t);
out << std::put_time(tmb, "%d-%m-%Y %H:%M:%S") << " [" << kLogLevelChars[level] << ' '
<< pContext << "] " << pMessage << std::endl;
}
#endif
void MessageCallback(
ilr_reader_msg nMsg,
const void* pMsgData,
void* pUserData) {
try {
switch (nMsg) {
{
std::stringstream ss;
ss << " " << kMpTypeNames[pInfo->nMpType];
ss << " SL" << static_cast<int>(pInfo->nSL);
std::cout << "{!} Карта найдена " << kCardTypeNames[pInfo->nType] << ' '
<< CardUIDToStr(pInfo->nType, pInfo->rUID) << ss.str() << std::endl;
if (g_oReadCmd) {
g_oReadCmd.Cancel();
g_oReadCmd.Close();
std::cout << "Чтение данных отменено" << std::endl;
}
auto pReader = static_cast<CReader*>(pUserData);
pReader->GetReaderInfo(rRI);
auto nRwCards = pReader->GetSupportedRewrittenCardTypes(rRI.
nModel, rRI.
nFwVersion);
g_nCardMemSize = pInfo->nMemSize;
switch (pInfo->nType) {
g_StartTime = ilr::now();
g_oReadCmd = pReader->Begin_ReadTemic(0, 10);
std::cout << "Чтение данных..." << std::endl;
}
break;
g_StartTime = ilr::now();
g_oReadCmd = pReader->Begin_ReadMfUltralight(0, 16);
std::cout << "Чтение данных..." << std::endl;
}
break;
g_StartTime = ilr::now();
auto nBlockCount = GetNumberOfMfBlocks(pInfo->nMemSize);
g_oReadCmd = pReader->Begin_ReadMfClassic(0, nBlockCount);
std::cout << "Чтение данных..." << std::endl;
}
break;
g_StartTime = ilr::now();
auto nBlockCount = GetNumberOfMfBlocks(pInfo->nMemSize);
g_oReadCmd = pReader->Begin_ReadMfPlus(0, nBlockCount, g_fOpenText);
std::cout << "Чтение данных..." << std::endl;
}
}
g_StartTime = ilr::now();
auto nBlockCount = GetNumberOfMfBlocks(pInfo->nMemSize);
g_oReadCmd = pReader->Begin_ReadMfClassic(0, nBlockCount);
std::cout << "Чтение данных..." << std::endl;
}
}
break;
default:
std::cout << "Считыватель не поддерживает чтение данных из карты" << std::endl;
break;
}
if (!g_oReadCmd)
pReader->SetAutoScan(true, false);
}
break;
{
std::stringstream ss;
ss << " " << kMpTypeNames[pInfo->nMpType];
ss << " SL" << static_cast<int>(pInfo->nSL);
std::cout << "{!} Карта потеряна " << kCardTypeNames[pInfo->nType] << ' '
<< CardUIDToStr(pInfo->nType, pInfo->rUID) << ss.str() << std::endl;
if (g_oReadCmd) {
g_oReadCmd.Cancel();
g_oReadCmd.Close();
std::cout << "Чтение данных отменено" << std::endl;
auto pReader = (CReader*)pUserData;
pReader->SetAutoScan(true, false);
}
}
break;
if (g_oReadCmd) {
auto nSpan = since(g_StartTime).count();
auto pReader = static_cast<CReader*>(pUserData);
auto nStatus = g_oReadCmd.GetStatus();
pReader->SetAutoScan(true, false);
switch (g_nCardType) {
{
const uint* pBlocks = nullptr;
size_t nCount = 0;
pReader->End_ReadTemic(g_oReadCmd, pBlocks, nCount);
const uint8_t* p;
for (size_t i = 0; i < nCount; i++) {
p = (const uint8_t*)&pBlocks[i];
std::cout << std::dec << std::setw(2) << i << ". " << std::hex
<< std::setw(2) << (uint)p[0] << ' ' << std::setw(2)
<< (uint)p[1] << ' ' << std::setw(2) << (uint)p[2] << ' '
<< std::setw(2) << (uint)p[3] << std::endl;
}
std::cout << "Прочитано " << std::dec << nCount << " блоков за " << nSpan
<< " мс" << std::endl;
}
break;
{
const uint* pPages = nullptr;
size_t nCount = 0;
pReader->End_ReadMfUltralight(g_oReadCmd, pPages, nCount);
const uint8_t* p;
for (size_t i = 0; i < nCount; i++) {
p = (const uint8_t*)&pPages[i];
std::cout << std::dec << std::setw(2) << i << ". " << std::hex
<< std::setw(2) << (uint)p[0] << ' ' << std::setw(2)
<< (uint)p[1] << ' ' << std::setw(2) << (uint)p[2] << ' '
<< std::setw(2) << (uint)p[3] << std::endl;
}
std::cout << "Прочитано " << nCount << " страниц за " << nSpan << " мс"
<< std::endl;
}
break;
{
size_t nCount;
pReader->End_ReadMfClassic(g_oReadCmd, pBlocks, nCount);
for (size_t i = 0; i < nCount; i++) {
pBlock = &pBlocks[i];
std::cout
<< std::dec << std::setw(2) << i << ". " << std::hex << std::setw(2)
<< (uint)pBlock->
a[0] <<
' ' << std::setw(2) << (uint)pBlock->
a[1]
<<
' ' << std::setw(2) << (uint)pBlock->
a[2] <<
' ' << std::setw(2)
<< (uint)pBlock->
a[3] <<
' ' << std::setw(2) << (uint)pBlock->
a[4]
<<
' ' << std::setw(2) << (uint)pBlock->
a[5] <<
' ' << std::setw(2)
<< (uint)pBlock->
a[6] <<
' ' << std::setw(2) << (uint)pBlock->
a[7]
<<
' ' << std::setw(2) << (uint)pBlock->
a[8] <<
' ' << std::setw(2)
<< (uint)pBlock->
a[9] <<
' ' << std::setw(2) << (uint)pBlock->
a[10]
<<
' ' << std::setw(2) << (uint)pBlock->
a[11] <<
' ' << std::setw(2)
<< (uint)pBlock->
a[12] <<
' ' << std::setw(2) << (uint)pBlock->
a[13]
<<
' ' << std::setw(2) << (uint)pBlock->
a[14] <<
' ' << std::setw(2)
<< (uint)pBlock->
a[15] << std::endl;
}
std::cout << "Прочитано " << nCount << " блоков за " << nSpan << " мс"
<< std::endl;
}
break;
{
size_t nCount;
pReader->End_ReadMfPlus(g_oReadCmd, pBlocks, nCount);
for (size_t i = 0; i < nCount; i++) {
pBlock = &pBlocks[i];
std::cout
<< std::dec << std::setw(2) << i << ". " << std::hex << std::setw(2)
<< (uint)pBlock->
a[0] <<
' ' << std::setw(2) << (uint)pBlock->
a[1]
<<
' ' << std::setw(2) << (uint)pBlock->
a[2] <<
' ' << std::setw(2)
<< (uint)pBlock->
a[3] <<
' ' << std::setw(2) << (uint)pBlock->
a[4]
<<
' ' << std::setw(2) << (uint)pBlock->
a[5] <<
' ' << std::setw(2)
<< (uint)pBlock->
a[6] <<
' ' << std::setw(2) << (uint)pBlock->
a[7]
<<
' ' << std::setw(2) << (uint)pBlock->
a[8] <<
' ' << std::setw(2)
<< (uint)pBlock->
a[9] <<
' ' << std::setw(2) << (uint)pBlock->
a[10]
<<
' ' << std::setw(2) << (uint)pBlock->
a[11] <<
' ' << std::setw(2)
<< (uint)pBlock->
a[12] <<
' ' << std::setw(2) << (uint)pBlock->
a[13]
<<
' ' << std::setw(2) << (uint)pBlock->
a[14] <<
' ' << std::setw(2)
<< (uint)pBlock->
a[15] << std::endl;
}
std::cout << "Прочитано " << nCount << " блоков за " << nSpan << " мс"
<< std::endl;
}
break;
default:
std::cout << "Неожиданный тип карты!" << std::endl;
break;
}
std::cout << "Успех" << std::endl;
else
g_oReadCmd.Close();
}
break;
{
auto pReader = static_cast<CReader*>(pUserData);
auto nStatus = pReader->GetConnectionStatus();
switch (nStatus) {
std::cout << "{!} Считыватель подключён" << std::endl;
break;
case ILR_CONNECTION_DISCONNECTED:
std::cout << "{!} Считыватель отключён" << std::endl;
default:
break;
}
}
break;
}
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
int main() {
try {
#ifdef ILR_LOG
#ifdef ILR_LOG_FILE
std::ofstream file(kLogFileName, std::ios_base::out | std::ios_base::trunc);
file.close();
#endif
CILR::SetLogCallback(LogCallback);
#endif
CILR oILR;
CReader oReader;
{
std::cout << "Поиск считывателя..." << std::endl;
CReaderSearch oSearch(oILR.GetSearch());
#if 0
oSearch.GetOptions(rOptions);
oSearch.SetOptions(rOptions);
const uint16_t aListenPorts[] = {25000};
oSearch.SetListenPorts(aListenPorts, std::size(aListenPorts));
#endif
oSearch.Scan();
auto nCount = oSearch.GetReaderCount();
bool fFound = false;
for (size_t i = 0; i < nCount; i++) {
oSearch.GetReaderInfo(i, rInfo);
continue;
fFound = true;
break;
}
if (!fFound) {
std::cout << "Считыватель не найден" << std::endl;
return 0;
}
oReader.LoadTemicPassword(g_TemicPassword);
oReader.LoadMfCKey(g_McKey);
oReader.LoadMfPKey(g_MpKey);
oReader.SetScanTemic(true);
std::cout <<
"Подключение к считывателю [" << kPortTypeNames[rInfo.
nPortType] <<
": "
oReader.Connect();
}
oReader.GetReaderInfo(rInfo);
std::stringstream ss;
ss << kReaderModelNames[rInfo.
nModel];
ss <<
" с/н:" << rInfo.
nSn;
ss <<
" прошивка:" << ReaderVersionToStr(rInfo.
nFwVersion);
std::cout << "Считыватель успешно подключён [" << ss.str() << ']' << std::endl;
oReader.SetMessageCallback(MessageCallback, &oReader);
oReader.GetOptions(rOptions);
oReader.SetOptions(rOptions);
std::cout << "Ожидание поднесения карты..." << std::endl;
std::cout << "Нажмите <Enter> для выхода..." << std::endl;
std::cin.get();
if (g_oReadCmd) {
g_oReadCmd.Cancel();
g_oReadCmd.Close();
std::cout << "Чтение данных отменено" << std::endl;
}
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
#define ILR_SUCCEEDED(status)
Проверяет код возврата SDK на успех.
Definition: ilreaders.h:181
ILREADERS_EXPORT const char * ilr_get_error_text(ilr_status nErrorCode)
Возвращает описание ошибки по её коду.
ilr_rwcard_type_flags
Флаги типов перезаписываемых карт.
Definition: ilreaders.h:627
ilr_reader_msg
Сообщение считывателя.
Definition: ilreaders.h:489
@ ILR_RWCT_F_TEMIC
Temic.
Definition: ilreaders.h:628
@ ILR_RWCT_F_MF_CLASSIC
Mifare Classic или Plus SL1.
Definition: ilreaders.h:630
@ ILR_RWCT_F_MF_ULTRALIGHT
Mifare Ultralight.
Definition: ilreaders.h:629
@ ILR_RWCT_F_MF_PLUS_SL3
Mifare Plus SL3.
Definition: ilreaders.h:632
@ ILR_MF_PLUS_UNKNOWN
Не известно.
Definition: ilreaders.h:570
@ ILR_READER_MSG_CONNECTION_CHANGED
Definition: ilreaders.h:500
@ ILR_READER_MSG_CARD_FOUND
Definition: ilreaders.h:504
@ ILR_READER_MSG_CARD_LOST
Definition: ilreaders.h:508
@ ILR_READER_MSG_COMMAND_FINISH
Definition: ilreaders.h:495
@ ILR_MF_PLUS_SL_UNKNOWN
Не известно.
Definition: ilreaders.h:558
@ ILR_MF_PLUS_SL_SL3
Уровень 3.
Definition: ilreaders.h:562
@ ILR_MF_PLUS_SL_SL1
Уровень 1 (эмуляция Mifare Classic).
Definition: ilreaders.h:560
@ ILR_CONNECTION_CONNECTED
Подключён.
Definition: ilreaders.h:531
@ ILR_READER_CCID
Definition: ilreaders.h:454
Заголовочный файл API SDK Readers.
ilr_log_level
Уровень лога.
Definition: ilreaders.h:306
@ ILR_LOG_LEVEL_DEBUG
Отладочные сообщения.
Definition: ilreaders.h:336
@ ILR_CARD_MF_PLUS_4K
Mifare Plus 4K.
Definition: ilreaders.h:268
@ ILR_CARD_SMART_MX_MF1K
Smart MX with Mifare 1K.
Definition: ilreaders.h:269
@ ILR_CARD_SMART_MX_MF4K
Smart MX with Mifare 4K.
Definition: ilreaders.h:270
@ ILR_CARD_MF_CLASSIC_2K
Mifare Classic 2K.
Definition: ilreaders.h:263
@ ILR_CARD_MF_ULTRALIGHT
Mifare UltraLight.
Definition: ilreaders.h:259
@ ILR_CARD_MF_PLUS_1K
Mifare Plus 1K.
Definition: ilreaders.h:266
@ ILR_CARD_MF_CLASSIC_4K
Mifare Classic 4K.
Definition: ilreaders.h:264
@ ILR_CARD_TEMIC
Temic (T5557).
Definition: ilreaders.h:257
@ ILR_CARD_MF_PLUS_2K
Mifare Plus 2K.
Definition: ilreaders.h:267
@ ILR_CARD_MF_CLASSIC_1K
Mifare Classic 1K.
Definition: ilreaders.h:262
@ ILR_CARD_MF_MINI
Mifare Mini.
Definition: ilreaders.h:261
@ ILR_READER_MODEL_UNKNOWN
Definition: ilreaders.h:200
Информация о карте.
Definition: ilreaders.h:583
Данные блока Mifare Classic/Plus.
Definition: ilreaders.h:618
uint8_t a[16]
Байты блока.
Definition: ilreaders.h:619
Информация о считывателе.
Definition: ilreaders.h:282
ilr_reader_model nModel
Definition: ilreaders.h:291
uint32_t nFwVersion
Definition: ilreaders.h:297
const char * pszConnect
Definition: ilreaders.h:289
const char * pszPortName
Definition: ilreaders.h:286
int64_t nFwBuildDate
Definition: ilreaders.h:300
ilr_port_type nPortType
Definition: ilreaders.h:284
int nSn
Definition: ilreaders.h:293
Настройки считывателя.
Definition: ilreaders.h:656
uint32_t nHoldCardTypes
Definition: ilreaders.h:661
Настройки поиска считывателей.
Definition: ilreaders.h:461
uint32_t nReaderTypes
Definition: ilreaders.h:466