Added: PCSC Implementation

This commit is contained in:
TheJoKlLa
2020-09-15 15:27:01 +02:00
parent 2034c08d33
commit 187dfc4f4a
16 changed files with 757 additions and 8 deletions

63
NFC/Readers/PCSC/Card.cs Normal file
View File

@ -0,0 +1,63 @@
using PCSC;
using PCSC.Iso7816;
namespace NFC.Readers.PCSC
{
public class Card : ICard
{
private IsoReader _ISOReader;
private string _ReaderID;
public Card(IsoReader isoreader, string readerID)
{
_ISOReader = isoreader;
_ReaderID = readerID;
}
public void Connect()
{
_ISOReader.Connect(_ReaderID, SCardShareMode.Shared, SCardProtocol.Any);
}
public void Disconnect()
{
_ISOReader.Disconnect(SCardReaderDisposition.Eject);
}
public APDUResponse Transmit(APDUCommand apdu_cmd)
{
Response response = _ISOReader.Transmit(Convert(apdu_cmd));
return Convert(response);
}
public CommandApdu Convert(APDUCommand apdu_cmd)
{
CommandApdu apdu = new CommandApdu(IsoCase.Case2Short, _ISOReader.ActiveProtocol)
{
CLA = apdu_cmd.Class,
INS = apdu_cmd.Instruction,
P1 = apdu_cmd.Parameter1,
P2 = apdu_cmd.Parameter2,
Data = apdu_cmd.Data,
Le = apdu_cmd.LengthExpected
};
return apdu;
}
public APDUResponse Convert(Response response)
{
ResponseApdu responseApdu = response.Get(0);
APDUResponse apduResponse = new APDUResponse()
{
SW1 = responseApdu.SW1,
SW2 = responseApdu.SW2,
Body = responseApdu.GetData()
};
return apduResponse;
}
}
}

View File

@ -0,0 +1,33 @@
using PCSC;
namespace NFC.Readers.PCSC
{
public class Hardware : IHardware
{
public string[] GetReaders()
{
var contextFactory = ContextFactory.Instance;
using (var context = contextFactory.Establish(SCardScope.System))
{
return context.GetReaders();
}
}
public bool IsAvailable()
{
if(GetReaders().Length == 0)
{
return false;
}
else
{
return true;
}
}
public IReader OpenReader(string readerID)
{
return new Reader(readerID);
}
}
}

View File

@ -0,0 +1,51 @@
using PCSC;
using PCSC.Iso7816;
using System;
using System.Collections.Generic;
using System.Text;
namespace NFC.Readers.PCSC
{
public class Reader : IReader, IDisposable
{
private string _ReaderID;
private IContextFactory _ContextFactory;
private ISCardContext _SCardContext;
private IsoReader _ISOReader;
private ICard _Card;
public Reader(string readerID)
{
_ReaderID = readerID;
}
public event ReaderEventHandler CardDiscovered;
public event ReaderEventHandler CardLost;
public void Dispose()
{
stop();
}
public void start()
{
_ContextFactory = ContextFactory.Instance;
_SCardContext = _ContextFactory.Establish(SCardScope.System);
_ISOReader = new IsoReader(_SCardContext);
_Card = new Card(_ISOReader, _ReaderID);
CardDiscovered?.Invoke(this, _Card);
}
public void stop()
{
CardLost?.Invoke(this, _Card);
_ISOReader.Dispose();
_SCardContext.Dispose();
}
}
}