-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.cpp
executable file
·265 lines (223 loc) · 6.97 KB
/
main.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
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
/*
YACardEmu
----------------
Copyright (C) 2020-2023 wutno (https://github.com/GXTX)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <iostream>
#include <chrono>
#include <thread>
#include <atomic>
#include <csignal>
#include <string>
#include "C1231LR.h"
#include "C1231BR.h"
#include "SerIo.h"
#include "WebIo.h"
#include "httplib.h"
#include "mini/ini.h"
#include "spdlog/spdlog.h"
#include "spdlog/async.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "ghc/filesystem.hpp"
// Globals
struct Settings {
CardIo::Settings card = {};
SerIo::Settings serial = {};
std::string webListenHost;
int webPort = 8080;
};
Settings globalSettings{};
std::atomic<bool> running{true};
constexpr static auto delay = std::chrono::microseconds(250);
std::shared_ptr<spdlog::async_logger> g_logger;
const char *helptext =
"YACardEmu - A simulator for magnetic card readers\n"
"Commandline arguments:\n"
"-d : debug log level\n"
"-t : trace log level\n"
"-f : log to yacardemu.log\n"
"-h : show this help text\n"
"\n";
//
void WaitForInput()
{
#ifdef _WIN32
g_logger->flush();
std::cout << "Press any key to close the application...";
std::cin.get();
#endif
}
void SigHandler(int sig)
{
if (sig == SIGINT || sig == SIGTERM) {
g_logger->flush();
running = false;
}
}
// TODO: Handle this elsewhere, feels dirty
bool ReadConfig()
{
// Read in config values
mINI::INIFile config("config.ini");
mINI::INIStructure ini;
std::string lhost, lport, lbaud, lparity;
// TODO: Generate INI
if (!config.read(ini)) {
spdlog::critical("Unable to open config.ini!");
return false;
}
if (ini.has("config")) {
lhost = ini["config"]["apihost"];
lport = ini["config"]["apiport"];
lbaud = ini["config"]["serialbaud"];
lparity = ini["config"]["serialparity"];
globalSettings.card.mech = ini["config"]["targetdevice"];
globalSettings.card.cardPath = ini["config"]["basepath"];
globalSettings.card.cardName = ini["config"]["autoselectedcard"];
globalSettings.serial.devicePath = ini["config"]["serialpath"];
}
if (globalSettings.card.cardPath.empty()) {
globalSettings.card.cardPath = ghc::filesystem::current_path().string();
}
#ifdef _WIN32
if (globalSettings.card.cardPath.back() != '\\')
globalSettings.card.cardPath.append("\\");
#else
if (globalSettings.card.cardPath.back() != '/')
globalSettings.card.cardPath.append("/");
#endif
if (lhost.empty()) {
globalSettings.webListenHost = "0.0.0.0";
} else {
globalSettings.webListenHost = lhost;
}
if (lport.empty()) {
globalSettings.webPort = 8080;
} else {
globalSettings.webPort = std::stoi(lport);
}
if (globalSettings.serial.devicePath.empty()) {
globalSettings.serial.devicePath = "/dev/ttyUSB1";
}
if (lbaud.empty()) {
globalSettings.serial.baudRate = 9600;
} else {
globalSettings.serial.baudRate = std::stoi(lbaud);
}
if (lparity.empty()) {
globalSettings.serial.parity = SP_PARITY_NONE;
} else {
if (lparity.find("even") != std::string::npos) {
globalSettings.serial.parity = SP_PARITY_EVEN;
} else {
globalSettings.serial.parity = SP_PARITY_NONE;
}
}
return true;
}
int main(int argc, char *argv[])
{
// Handle quitting gracefully via signals
std::signal(SIGINT, SigHandler);
std::signal(SIGTERM, SigHandler);
// Parse args
#ifdef NDEBUG
spdlog::level::level_enum log_level = spdlog::level::info;
#else
spdlog::level::level_enum log_level = spdlog::level::debug;
#endif
bool file_log = false;
if (argc > 1) {
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
while (arg[0] == '-') arg.erase(0,1);
switch (arg[0]) {
case 'd':
log_level = spdlog::level::debug;
break;
case 't':
log_level = spdlog::level::trace;
break;
case 'f':
file_log = true;
break;
case 'h':
default:
std::cout << helptext;
return 0;
}
}
}
// Set up logger
spdlog::init_thread_pool(8192, 1);
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt >();
std::vector<spdlog::sink_ptr> sinks;
if (file_log) {
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("yacardemu.log", true);
sinks.push_back(file_sink);
}
sinks.push_back(stdout_sink);
g_logger = std::make_shared<spdlog::async_logger>("main", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
g_logger->set_level(log_level);
g_logger->flush_on(spdlog::level::info);
g_logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v");
if (!ReadConfig()) {
WaitForInput();
return 1;
}
std::unique_ptr<CardIo> cardHandler{};
if (globalSettings.card.mech.compare("C1231LR") == 0 || globalSettings.card.mech.compare("S31R") == 0) {
cardHandler = std::make_unique<C1231LR>(&globalSettings.card);
} else if (globalSettings.card.mech.compare("C1231BR") == 0) {
cardHandler = std::make_unique<C1231BR>(&globalSettings.card);
} else {
g_logger->critical("Invalid target device: {}", globalSettings.card.mech);
WaitForInput();
return 1;
}
std::unique_ptr<SerIo> serialHandler = std::make_unique<SerIo>(&globalSettings.serial);
if (!serialHandler->Open()) {
WaitForInput();
return 1;
}
std::unique_ptr<WebIo> webHandler = std::make_unique<WebIo>(&globalSettings.card, globalSettings.webListenHost, globalSettings.webPort, &running);
if (!webHandler->Spawn()) {
WaitForInput();
return 1;
}
// TODO: These don't need to be here, put them in their respective classes
SerIo::Status serialStatus = SerIo::Status::Okay;
CardIo::StatusCode cardStatus = CardIo::StatusCode::Okay;
g_logger->info("Running...");
while (running) {
serialStatus = serialHandler->Read();
// TODO: device read/write should probably be a separate thread
if (serialStatus != SerIo::Status::Okay && serialHandler->m_readBuffer.empty()) {
std::this_thread::sleep_for(delay);
continue;
}
cardStatus = cardHandler->ReceivePacket(serialHandler->m_readBuffer);
if (cardStatus == CardIo::Okay || cardStatus == CardIo::ChecksumError) {
// We need to send our ACK as quick as possible
serialHandler->SendAck(cardStatus == CardIo::Okay ? true : false);
} else if (cardStatus == CardIo::ServerWaitingReply) {
// Do not reply until we get this command
cardHandler->BuildPacket(serialHandler->m_writeBuffer);
serialHandler->Write();
}
std::this_thread::sleep_for(delay);
}
return 0;
}