AfRApay.FTM: Terminal states and hotkeys

This commit is contained in:
embr 2023-02-08 14:32:20 +01:00 committed by zotan's git
parent f6d5b90e23
commit e73725cdbf
Signed by: zotan's git
GPG key ID: C40BFEA2B3BA06A8

View file

@ -10,7 +10,7 @@ rootCommand.Add(listReadersOption);
rootCommand.SetHandler((listReaders) => {
using (var context = ContextFactory.Instance.Establish(SCardScope.System)) {
// We need a card reader or this won't work!
// We need at least one card reader or this won't work!
var readerNames = context.GetReaders();
if (readerNames.Length == 0) {
Console.Error.WriteLine("Error: no card reader detected");
@ -19,16 +19,28 @@ rootCommand.SetHandler((listReaders) => {
// If --list-readers is passed, list readers and exit.
if (listReaders) {
Console.Error.WriteLine("---------- Connected Readers ----------");
Console.Error.WriteLine("----------- Connected Readers ----------");
foreach (var name in readerNames) {
Console.WriteLine(name);
}
Console.Error.WriteLine("---------------------------------------");
Console.Error.WriteLine("----------------------------------------");
Environment.Exit(0);
}
Console.Error.WriteLine("----------------------------------------");
Console.Error.WriteLine("--- AfRApay FTM - Fancy Test Machine ---");
Console.Error.WriteLine("----------------------------------------");
Console.Error.WriteLine();
Console.Error.WriteLine("Hotkeys (case insensitive):");
Console.Error.WriteLine(" [L] Link Card, instead of debiting it");
Console.Error.WriteLine(" [Esc] Cancel, return to default state");
Console.Error.WriteLine();
Console.Error.WriteLine("----------------------------------------");
// Listen for events on all connected readers.
using (var monitor = MonitorFactory.Instance.Create(SCardScope.System)) {
var state = TerminalState.Default;
monitor.Initialized += (_, args) => Console.WriteLine("[ Reader Initialized: {0} ]", args.ReaderName);
monitor.MonitorException += (_, args) => {
Console.Error.WriteLine("! ERROR: {0}", args);
@ -38,7 +50,7 @@ rootCommand.SetHandler((listReaders) => {
monitor.CardInserted += (_, args) => {
Console.WriteLine("> TAP: {0}", Convert.ToHexString(args.Atr));
var reader = new IsoReader(context, args.ReaderName, SCardShareMode.Shared, SCardProtocol.Any);
HandleTap(reader);
HandleTap(reader, state);
};
monitor.CardRemoved += (_, args) => {
Console.WriteLine("< OFF");
@ -48,7 +60,20 @@ rootCommand.SetHandler((listReaders) => {
Console.WriteLine("[ Starting... ]");
monitor.Start(readerNames);
while (true) {
Console.Read();
var key = Console.ReadKey();
switch (key.Key) {
case ConsoleKey.L:
state = TerminalState.Link;
Console.Error.WriteLine("=> Mode: Link");
break;
case ConsoleKey.Escape:
state = TerminalState.Default;
Console.Error.WriteLine("\\ => Mode: Default"); // Hack: the \\ eats the escape character.
break;
default:
Console.Error.WriteLine(" => UNRECOGNISED KEY");
break;
};
}
}
}
@ -57,7 +82,7 @@ rootCommand.SetHandler((listReaders) => {
return await rootCommand.InvokeAsync(args);
// Queries a card for data when one is tapped.
static void HandleTap(IsoReader reader) {
static void HandleTap(IsoReader reader, TerminalState state) {
// Send a PCSC pseudo-APDU to query the ISO 14443 UID.
var rsp = reader.Transmit(new CommandApdu(IsoCase.Case2Short, SCardProtocol.Any) {
CLA = 0xFF,
@ -77,3 +102,9 @@ static void HandleTap(IsoReader reader) {
static bool IsSucc(Response rsp) {
return rsp.SW1 == (byte)SW1Code.Normal && rsp.SW2 == 0x00;
}
// Terminal State.
enum TerminalState {
Default,
Link,
};