-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_main.cpp
More file actions
101 lines (81 loc) · 4.27 KB
/
reader_main.cpp
File metadata and controls
101 lines (81 loc) · 4.27 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
#define READER
#include <iostream>
#include "functions.h"
#include "data.h"
#include "web.h"
#include "timing.h"
#define OUT_PATH "./read_output"
#define TIME_PATH "./time_output.csv"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
int main() {
lla_ptr(ReaderConfiguration) readerConfig(new logicalaccess::ReaderConfiguration());
lla_ptr(ReaderProvider) provider = logicalaccess::LibraryManager::getInstance()->getReaderProvider("PCSC");
if (!provider) {
std::cerr << "Could not get reader provider PCSC\nIs the config in the working directory?";
return EXIT_FAILURE;
}
readerConfig->setReaderProvider(provider);
readerConfig->setReaderUnit(readerConfig->getReaderProvider()->createReaderUnit());
if (readerConfig->getReaderProvider()->getRPType() == "PCSC" &&
readerConfig->getReaderProvider()->getReaderList().empty()) {
std::cerr << "No readers on this system." << std::endl;
return EXIT_FAILURE;
}
readerConfig->getReaderUnit()->connectToReader();
for (;;) {
std::cout << "Please apply card..." << std::endl;
if (readerConfig->getReaderUnit()->waitInsertion(0)) {
if (readerConfig->getReaderUnit()->connect()) {
try {
lla_ptr(Chip) chip = readerConfig->getReaderUnit()->getSingleChip();
lla_ptr(MifareChip) mifareChip = lla_ptr_dynamic_cast(MifareChip, chip);
if (!mifareChip) {
std::cerr << "Card must be Mifare 1K. Please remove card" << std::endl;
readerConfig->getReaderUnit()->waitRemoval(0);
continue;
}
// DO CHIP COMMANDS
auto storageService = lla_ptr_dynamic_cast(StorageCardService,
chip->getService(logicalaccess::CST_STORAGE));
auto accessInfo = lla_ptr(MifareAccessInfo)(new logicalaccess::MifareAccessInfo());
accessInfo->keyA->fromString("ff ff ff ff ff ff");
CardData cd;
cd.storageService = storageService;
cd.accessInfo = accessInfo;
std::tuple<std::string, std::string> name;
std::string id;
uint8_t grade;
boost::uuids::uuid uuid {};
long readingTime = Measure<>::execution([&name, &id, &grade, &uuid, &cd]() {
name = readStudentName(cd);
id = readStudentId(cd);
grade = readStudentGrade(cd);
uuid = readUuid(cd);
});
std::cout << "Reading time: " << readingTime << "ms" << std::endl;
std::cout << appendToFile(OUT_PATH, "Student Name: " + std::get<0>(name) + " " + std::get<1>(name) + "\n");
std::cout << appendToFile(OUT_PATH, "Student Id: " + id + "\n");
std::cout << appendToFile(OUT_PATH, "Student Grade: " + std::to_string(grade) + "\n");
std::cout << appendToFile(OUT_PATH, "UUID: " + boost::uuids::to_string(uuid) + "\n");
std::cout << appendToFile(OUT_PATH, "\n");
long networkTime = Measure<>::execution(
sendData, std::get<0>(name), std::get<1>(name), id, grade, uuid
);
std::cout << "Network out time: " << networkTime << "ms" << std::endl;
appendToFile(TIME_PATH, std::to_string(readingTime) + "," + std::to_string(networkTime) + "\n");
readerConfig->getReaderUnit()->waitRemoval(0);
} catch (logicalaccess::CardException& e) {
std::cerr << "Caught an exception: " << e.error_code() << " -- " << e.what() << std::endl <<
"Did you remove the card too soon?" << std::endl;
readerConfig->getReaderUnit()->waitRemoval(15);
}
} else {
std::cerr << "Card connection failure" << std::endl;
}
} else {
std::cout << "Timeout" << std::endl;
}
}
}
#pragma clang diagnostic pop