#include <stdio.h>
#include <iomanip>
#include <iostream>
#define ILR_LOG_FILE
using namespace ilr;
#ifdef ILR_LOG
const char kLogLevelChars[] = {'-', 'E', 'W', 'I', 'D'};
const char kLogFileName[] = "ilreaders.log";
#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 DoReadUltralight(
CReader& oReader) {
size_t nRead = 0;
try {
uint aPages[16];
std::cout << "Чтение данных карты..." << std::endl;
std::cout <<
"Прочитано за " <<
since(tStartTime).count() <<
" мс" << std::endl;
const char* kLocked[2] = {"", "Заблокировано"};
uint nPageData, n;
uint8_t* pBytes;
std::cout << "Страница. Байты 0..3" << std::endl;
for (size_t i = 0; i < 16; i++) {
nPageData = aPages[i];
pBytes = (uint8_t*)&nPageData;
std::cout << std::dec << std::setw(4) << i << ". " << std::hex << std::setw(3)
<< (uint)pBytes[0] << std::setw(3) << (uint)pBytes[1] << std::setw(3)
<< (uint)pBytes[2] << std::setw(3) << (uint)pBytes[3];
switch (i) {
case 0:
case 1:
std::cout << " Серийный номер" << std::endl;
break;
case 2:
{
std::cout << " Внутреннее / Блокировка" << std::endl;
n = (nPageData >> 16);
std::cout << "Lock0[" << std::hex << std::setw(2) << (n & 0xff)
<< "] BOTP:" << std::dec << (n & 1) << ", BL9-4:" << ((n >> 1) & 1)
<< ", BL15-10:" << ((n >> 2) & 1) << ", OTP:" << ((n >> 3) & 1)
<< ", L4:" << ((n >> 4) & 1) << ", L5:" << ((n >> 5) & 1)
<< ", L6:" << ((n >> 6) & 1) << ", L7:" << ((n >> 7) & 1)
<< std::endl;
printf(
"Lock1[%.2X] L8:%d, L9:%d, L10:%d, L11:%d, L12:%d, L13:%d, L14:%d, "
"L15:%d\n",
n >> 8, (n >> 8) & 1, (n >> 9) & 1, (n >> 10) & 1, (n >> 11) & 1,
(n >> 12) & 1, (n >> 13) & 1, (n >> 14) & 1, (n >> 15) & 1);
break;
}
case 3:
{
std::string s("00000000 00000000 00000000 00000000");
n = nPageData;
for (size_t j = 0; j < s.length(); ++j) {
if (' ' == s[j])
continue;
if (n & 1)
s[j] = '1';
n >>= 1;
}
std::cout << " OTP (" << std::dec << nPageData << ") " << s << ' '
<< kLocked[
GET_BIT(aPages[2] >> 16, 3)] << std::endl;
break;
}
default:
std::cout << " Данные (" << std::dec << nPageData << ") "
<< kLocked[
GET_BIT(aPages[2] >> 16, i)] << std::endl;
break;
}
}
std::cout << "-----" << std::endl;
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
std::cout << "Прочитано " << std::dec << nRead << " из 16 страниц" << std::endl;
}
}
void DoWriteUltralight(
CReader& oReader) {
try {
std::cout << "Введите номер страницы (10-тичное), байты 0 1 2 3 (16-ричные):" << std::endl;
size_t nPageN;
uint a[4];
std::cin >> std::dec >> nPageN >> std::hex >> a[0] >> a[1] >> a[2] >> a[3];
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (std::cin.fail()) {
std::cin.clear();
std::cout << "Неправильный ввод" << std::endl;
return;
}
uint nPageData;
for (size_t i = 0; i < std::size(a); ++i)
((uint8_t*)&nPageData)[i] = (uint8_t)a[i];
std::cout << "Запись..." << std::endl;
std::cout <<
"Записано за " <<
since(tStartTime).count() <<
" мс" << std::endl;
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
{
oSearch.GetOptions(rOptions);
oSearch.SetOptions(rOptions);
}
std::cout << "Поиск считывателей..." << std::endl;
oSearch.Scan();
auto nCount = oSearch.GetReaderCount();
if (0 == nCount) {
std::cout << "Считыватель не найден" << std::endl;
return false;
}
std::cout << "Найдено " << nCount << ':' << std::endl;
bool fFound = false;
for (size_t i = 0; i < nCount; i++) {
oSearch.GetReaderInfo(i, rInfo);
continue;
fFound = true;
break;
}
}
if (!fFound) {
std::cout << "Считыватель не поддерживает чтение/запись карт Mifare Ultralight"
<< std::endl;
return false;
}
std::cout <<
"Подключение к считывателю [" << kPortTypeNames[rInfo.
nPortType] <<
": "
}
std::stringstream ss;
ss << kReaderModelNames[rInfo.
nModel];
ss <<
" с/н:" << rInfo.
nSn;
ss <<
" прошивка:" <<
ReaderVersionToStr(rInfo.
nFwVersion);
std::cout << "Считыватель успешно подключён [" << ss.str() << ']' << std::endl;
return true;
}
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
if (!DoConnectTo(oILR, oReader))
return 0;
while (true) {
std::cout << "Поиск карты Mifare Ultralight..." << std::endl;
if (fCardFound)
std::cout << kCardTypeNames[rCI.
nType] <<
' ' << CardUIDToStr(rCI.
nType, rCI.
rUID)
<< std::endl;
else
std::cout << "Карта не найдена" << std::endl;
std::cout << "-----" << std::endl;
std::cout << "Введите номер команды:" << std::endl;
std::cout << "1 - Искать снова" << std::endl;
if (fCardFound) {
std::cout << "2 - Прочитать данные из карты" << std::endl;
std::cout << "3 - Записать данные на карту..." << std::endl;
}
std::cout << "0 - Выйти из программы" << std::endl;
int nCommand;
std::cin >> nCommand;
if (std::cin.fail()) {
std::cin.clear();
nCommand = -1;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch (nCommand) {
case 1:
break;
case 2:
if (fCardFound)
DoReadUltralight(oReader);
break;
case 3:
if (fCardFound)
DoWriteUltralight(oReader);
break;
case 0:
return 0;
default:
std::cout << "Неправильный ввод" << std::endl;
break;
}
}
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
Класс для инициализации/финализации библиотеки SDK.
Definition: ilr_cpp_helpers.h:2607
CReaderSearch GetSearch()
Создаёт дескриптор поиска считывателей.
Definition: ilr_cpp_helpers.h:2761
CReader GetReader(ilr_port_type nPortType, const char *pszPortName)
Создаёт дескриптор считывателя.
Definition: ilr_cpp_helpers.h:2767
Класс поиска считывателей.
Definition: ilr_cpp_helpers.h:583
Класс считывателя.
Definition: ilr_cpp_helpers.h:996
void ReadMfUltralight(size_t nPageIdx, uint32_t *pBuf, size_t nPageCount, size_t *pRead=nullptr)
Читает данные карты Mifare Ultralight.
Definition: ilr_cpp_helpers.h:2258
void SetAutoScan(bool fEnable=true, bool fWait=true)
Включает/выключает автоматическое сканирование карт.
Definition: ilr_cpp_helpers.h:2242
void Connect(bool fReconnect=false)
Подключается к считывателю.
Definition: ilr_cpp_helpers.h:2197
void SetOptions(const ilr_reader_options &rOptions)
Устанавливает параметры считывателя.
Definition: ilr_cpp_helpers.h:2189
void WriteMfUltralight(size_t nPageIdx, const uint32_t *pData, size_t nPageCount, size_t *pWritten=nullptr)
Пишет данные карты Mifare Ultralight.
Definition: ilr_cpp_helpers.h:2274
void Scan(bool fReset=false, bool fPowerOff=true)
Ищет карты.
Definition: ilr_cpp_helpers.h:2227
void GetReaderInfo(ilr_reader_info &rInfo) const
Возвращает информацию о считывателе.
Definition: ilr_cpp_helpers.h:2223
void GetOptions(ilr_reader_options &rOptions) const
Возвращает параметры считывателя.
Definition: ilr_cpp_helpers.h:2193
bool GetCardInfo(ilr_card_info &rInfo) const
Возвращает информацию о карте в поле считывателя.
Definition: ilr_cpp_helpers.h:2237
@ ILR_RWCT_F_MF_ULTRALIGHT
Mifare Ultralight.
Definition: ilreaders.h:642
@ ILR_READER_CCID
Definition: ilreaders.h:467
Заголовочный файл SDK Readers с классами-помощниками C++.
#define GET_BIT(val, bitN)
Возвращает true если бит bitN установлен в числе val.
Definition: ilr_cpp_helpers.h:2779
auto since(std::chrono::time_point< clock_t, duration_t > const &start)
Возвращает интервал времени в миллисекундах от времени start до текущего времени.
Definition: ilr_cpp_helpers.h:2802
std::chrono::steady_clock::time_point now()
Definition: ilr_cpp_helpers.h:2791
Заголовочный файл API SDK Readers.
#define ILR_CALL
Макрос, определяющий соглашение о вызове функций.
Definition: ilreaders.h:56
ilr_log_level
Уровень лога.
Definition: ilreaders.h:319
@ ILR_LOG_LEVEL_DEBUG
Отладочные сообщения.
Definition: ilreaders.h:349
@ ILR_CARD_MF_ULTRALIGHT
Mifare UltraLight.
Definition: ilreaders.h:272
@ ILR_READER_MODEL_UNKNOWN
Definition: ilreaders.h:213
Информация о карте.
Definition: ilreaders.h:596
ilr_card_type nType
Тип карты.
Definition: ilreaders.h:597
ilr_card_uid rUID
Номер карты.
Definition: ilreaders.h:598
Информация о считывателе.
Definition: ilreaders.h:295
ilr_reader_model nModel
Definition: ilreaders.h:304
uint32_t nFwVersion
Definition: ilreaders.h:310
const char * pszConnect
Definition: ilreaders.h:302
const char * pszPortName
Definition: ilreaders.h:299
int64_t nFwBuildDate
Definition: ilreaders.h:313
ilr_port_type nPortType
Definition: ilreaders.h:297
int nSn
Definition: ilreaders.h:306
Настройки считывателя.
Definition: ilreaders.h:663
ilr_reader_model nConnectModel
Definition: ilreaders.h:665
Настройки поиска считывателей.
Definition: ilreaders.h:474
uint32_t nReaderTypes
Definition: ilreaders.h:479