142 lines
5.2 KiB
C#
142 lines
5.2 KiB
C#
using PCSC;
|
|
using PCSC.Iso7816;
|
|
using PCSC.Monitoring;
|
|
using System;
|
|
using System.Windows.Forms;
|
|
using Microsoft.Toolkit.Uwp.Notifications;
|
|
using System.Linq;
|
|
|
|
namespace NFCtoKeyboard
|
|
{
|
|
class NfcHandler
|
|
{
|
|
private static IMonitorFactory monitorFactory;
|
|
private static ISCardMonitor monitor;
|
|
public static bool running = false;
|
|
private static bool setupRequired = true;
|
|
private static string lastScan = "";
|
|
|
|
public static void Setup()
|
|
{
|
|
Console.WriteLine("Running scanner setup");
|
|
monitorFactory = MonitorFactory.Instance;
|
|
monitor = monitorFactory.Create(SCardScope.System);
|
|
monitor.StatusChanged += (sender, args) =>
|
|
{
|
|
if (args.NewState != SCRState.Present)
|
|
{
|
|
Console.WriteLine("Ignoring Scanner action: " + args.NewState.ToString());
|
|
return;
|
|
}
|
|
var context = ContextFactory.Instance.Establish(SCardScope.System);
|
|
string uid;
|
|
try
|
|
{
|
|
using (var rfidReader = context.ConnectReader(Settings.GetString("scanner-name", ""), SCardShareMode.Shared, SCardProtocol.Any))
|
|
{
|
|
var apdu = new CommandApdu(IsoCase.Case2Short, rfidReader.Protocol)
|
|
{
|
|
CLA = 0xFF,
|
|
Instruction = InstructionCode.GetData,
|
|
P1 = 0x00,
|
|
P2 = 0x00,
|
|
Le = 0 // We don't know the ID tag size
|
|
};
|
|
|
|
using (rfidReader.Transaction(SCardReaderDisposition.Leave))
|
|
{
|
|
Console.WriteLine("Retrieving the UID .... ");
|
|
|
|
var sendPci = SCardPCI.GetPci(rfidReader.Protocol);
|
|
var receivePci = new SCardPCI(); // IO returned protocol control information.
|
|
|
|
var receiveBuffer = new byte[256];
|
|
var command = apdu.ToArray();
|
|
|
|
var bytesReceived = rfidReader.Transmit(
|
|
sendPci, // Protocol Control Information (T0, T1 or Raw)
|
|
command, // command APDU
|
|
command.Length,
|
|
receivePci, // returning Protocol Control Information
|
|
receiveBuffer,
|
|
receiveBuffer.Length); // data buffer
|
|
|
|
var responseApdu =
|
|
new ResponseApdu(receiveBuffer, bytesReceived, IsoCase.Case2Short, rfidReader.Protocol);
|
|
if (responseApdu.GetData() == null)
|
|
{
|
|
Console.WriteLine("Tag UID is null");
|
|
return;
|
|
}
|
|
|
|
byte[] uidBytes = responseApdu.GetData();
|
|
|
|
if (Settings.GetBool("reverse-text-output", true))
|
|
{
|
|
Array.Reverse(uidBytes);
|
|
}
|
|
|
|
uid = BitConverter.ToString(uidBytes);
|
|
uid = uid.Replace("-", "");
|
|
}
|
|
}
|
|
Console.WriteLine("Scanned tag with uid: " + uid);
|
|
if (lastScan == uid)
|
|
{
|
|
Console.WriteLine("Ignoring scan, tag was already scanned in the last 1.5s");
|
|
return;
|
|
}
|
|
lastScan = uid;
|
|
System.Timers.Timer runonce = new(1500);
|
|
runonce.Elapsed += (s, e) => { lastScan = ""; };
|
|
runonce.AutoReset = false;
|
|
runonce.Start();
|
|
|
|
|
|
Console.WriteLine("pressing keys on keyboard");
|
|
if (Settings.GetBool("return-after-input", true))
|
|
{
|
|
SendKeys.SendWait(uid + "{ENTER}");
|
|
}
|
|
else
|
|
{
|
|
SendKeys.SendWait(uid);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
new ToastContentBuilder()
|
|
.AddText("NFCtoKeyboard")
|
|
.AddText(ex.Message)
|
|
.Show();
|
|
}
|
|
};
|
|
}
|
|
|
|
public static void Start()
|
|
{
|
|
|
|
Console.WriteLine("Starting scanner");
|
|
if (running)
|
|
{
|
|
Console.WriteLine("Scanner is already runngin, stopping old one first");
|
|
Stop();
|
|
}
|
|
running = true;
|
|
if (setupRequired)
|
|
{
|
|
setupRequired = false;
|
|
Setup();
|
|
}
|
|
monitor.Start(Settings.GetString("scanner-name", ""));
|
|
}
|
|
|
|
public static void Stop()
|
|
{
|
|
Console.WriteLine("Stopping scanner");
|
|
running = false;
|
|
monitor.Cancel();
|
|
setupRequired = true;
|
|
}
|
|
}
|
|
}
|