Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update build & test toolset * Update build & test toolset (NUnit,Fake,..) * Update System.Reactive (.netstandard) * Removed empty test project * Cleanup example projects * Added (native) example for #31 * Working on issue #31 - Start to implement a DeviceMonitor (to detect attached/detached readers) * Added proposed solution from @olegsavelos (issue #31) - DeviceMonitorFactory - DeviceMonitor - Rx-extensions to observe device events * Added feature #31
- Loading branch information
Showing
80 changed files
with
5,786 additions
and
1,754 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,96 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using PCSC; | ||
|
||
namespace CardStatus | ||
{ | ||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
using (var context = new SCardContext()) { | ||
public static void Main() { | ||
var contextFactory = ContextFactory.Instance; | ||
|
||
context.Establish(SCardScope.System); | ||
|
||
// retrieve all reader names | ||
using (var context = contextFactory.Establish(SCardScope.System)) { | ||
var readerNames = context.GetReaders(); | ||
|
||
if (readerNames == null || readerNames.Length < 1) { | ||
if (NoReaderFound(readerNames)) { | ||
Console.WriteLine("No readers found."); | ||
Console.ReadKey(); | ||
return; | ||
} else { | ||
DisplayReaderStatus(context, readerNames); | ||
} | ||
|
||
// get the card status of each reader that is currently connected | ||
foreach (var readerName in readerNames) { | ||
using (var reader = new SCardReader(context)) { | ||
Console.WriteLine("Trying to connect to reader {0}.", readerName); | ||
Console.ReadKey(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Displays the card status of each reader in <paramref name="readerNames"/> | ||
/// </summary> | ||
/// <param name="context">Smartcard context to connect</param> | ||
/// <param name="readerNames">Smartcard readers</param> | ||
private static void DisplayReaderStatus(ISCardContext context, IEnumerable<string> readerNames) { | ||
foreach (var readerName in readerNames) { | ||
using (var reader = new SCardReader(context)) { | ||
Console.WriteLine("Trying to connect to reader {0}.", readerName); | ||
|
||
var sc = reader.Connect(readerName, SCardShareMode.Shared, SCardProtocol.Any); | ||
if (sc == SCardError.Success) { | ||
DisplayReaderStatus(reader); | ||
reader.Disconnect(SCardReaderDisposition.Reset); | ||
} else { | ||
Console.WriteLine("No card inserted or reader is reserved exclusively by another application."); | ||
Console.WriteLine("Error message: {0}\n", SCardHelper.StringifyError(sc)); | ||
} | ||
var sc = reader.Connect(readerName, SCardShareMode.Shared, SCardProtocol.Any); | ||
if (sc != SCardError.Success) { | ||
Console.WriteLine("No card inserted or reader is reserved exclusively by another application."); | ||
Console.WriteLine("Error message: {0}\n", SCardHelper.StringifyError(sc)); | ||
continue; | ||
} | ||
} | ||
|
||
Console.ReadKey(); | ||
PrintReaderStatus(reader); | ||
Console.WriteLine(); | ||
reader.Disconnect(SCardReaderDisposition.Reset); | ||
} | ||
} | ||
} | ||
|
||
private static void DisplayReaderStatus(ISCardReader reader) { | ||
/// <summary> | ||
/// Queries the reader's status and prints it out | ||
/// </summary> | ||
/// <param name="reader">Connected reader</param> | ||
private static void PrintReaderStatus(ISCardReader reader) { | ||
string[] readerNames; | ||
SCardProtocol proto; | ||
SCardState state; | ||
byte[] atr; | ||
|
||
var sc = reader.Status( | ||
out readerNames, // contains the reader name(s) | ||
out state, // contains the current state (flags) | ||
out proto, // contains the currently used communication protocol | ||
out atr); // contains the ATR | ||
out readerNames, // contains the reader name(s) | ||
out state, // contains the current state (flags) | ||
out proto, // contains the currently used communication protocol | ||
out atr); // contains the ATR | ||
|
||
if (sc == SCardError.Success) { | ||
Console.WriteLine("Connected with protocol {0} in state {1}", proto, state); | ||
DisplayCardAtr(atr); | ||
Console.WriteLine(); | ||
} else { | ||
Console.WriteLine("Unable to retrieve card status.\nError message: {0}", | ||
if (sc != SCardError.Success) { | ||
Console.WriteLine("Unable to retrieve card status.\nError message: {0}", | ||
SCardHelper.StringifyError(sc)); | ||
return; | ||
} | ||
|
||
Console.WriteLine("Connected with protocol {0} in state {1}", proto, state); | ||
PrintCardAtr(atr); | ||
} | ||
|
||
private static void DisplayCardAtr(byte[] atr) { | ||
/// <summary> | ||
/// Prints the smart cards ATR as hex string | ||
/// </summary> | ||
/// <param name="atr">ATR bytes</param> | ||
private static void PrintCardAtr(byte[] atr) { | ||
if (atr == null || atr.Length <= 0) { | ||
return; | ||
} | ||
|
||
Console.WriteLine("Card ATR: {0}", BitConverter.ToString(atr)); | ||
} | ||
|
||
/// <summary> | ||
/// Returns <c>true</c> if the supplied collection <paramref name="readerNames"/> does not contain any reader name. | ||
/// </summary> | ||
/// <param name="readerNames">Collection of smartcard reader names</param> | ||
/// <returns><c>true</c> if no reader found</returns> | ||
private static bool NoReaderFound(ICollection<string> readerNames) { | ||
return readerNames == null || readerNames.Count < 1; | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.