Skip to content

Commit

Permalink
Added reader info
Browse files Browse the repository at this point in the history
  • Loading branch information
EmptySamurai committed Apr 3, 2015
1 parent 0d3c3bd commit c4e6f80
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 11 deletions.
19 changes: 19 additions & 0 deletions APDUTester/APDUReadersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,24 @@ public APDUResponse SendAPDU(string reader, APDURequest request)
};
}
}

public string GetReaderPort(string reader)
{
using (var context = new SCardContext())
{
context.Establish(SCardScope.System);
var scReader = new SCardReader(context);
SCardError error = scReader.Connect(reader, SCardShareMode.Shared, SCardProtocol.Any);
byte[] port;
error = scReader.GetAttrib(SCardAttribute.ChannelId, out port);
context.Release();
StringBuilder builder = new StringBuilder();
foreach (var b in port)
{
builder.Append(Convert.ToString(b, 16));
}
return builder.ToString();
}
}
}
}
5 changes: 4 additions & 1 deletion APDUTester/APDUTester.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="pcsc-sharp">
<HintPath>..\..\..\..\Projects\mobyusernfcmanagement\MobyUserNFCManagement\lib\pcsc-sharp.dll</HintPath>
<HintPath>lib\pcsc-sharp.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
Expand Down Expand Up @@ -102,6 +102,9 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Content Include="pcsc-sharp.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
1 change: 1 addition & 0 deletions APDUTester/IReadersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public interface IReadersController
{
string[] GetReaders();
APDUResponse SendAPDU(string reader, APDURequest request);
string GetReaderPort(string reader);
}
}
13 changes: 11 additions & 2 deletions APDUTester/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:conv="clr-namespace:APDUTester.Converters"
Title="MainWindow" Height="200" Width="525">
Title="APDUTester" Height="230" Width="525">
<Window.Resources>
<conv:HexByteConverter x:Key="HexByteConverter"/>
<conv:HexByteArrayConverter x:Key="HexByteArrayConverter" />
<Style x:Key="textBlockBorder" TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="Black" />
</Style>
</Window.Resources>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
Expand Down Expand Up @@ -37,6 +41,11 @@
<TextBox x:Name="tbData" Grid.Column="4" Grid.Row="1" Text="{Binding Path=Data, ValidatesOnExceptions=True, Converter={StaticResource HexByteArrayConverter}, Mode=OneWayToSource }" />
</Grid>
<Button x:Name="btSend" Width="50" HorizontalAlignment="Left" Click="btSend_Click" Margin="0 0 0 5">Send</Button>
<TextBlock x:Name="tblResponse" Height="40"/>
<Border Style="{StaticResource textBlockBorder}">
<ScrollViewer Height="80">
<TextBlock x:Name="tblResponse" ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True" />
</ScrollViewer>
</Border>
</StackPanel>
</Window>
37 changes: 29 additions & 8 deletions APDUTester/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,18 @@ private string ToHex(byte b)
return Convert.ToString(b, 16);
}

private void btSend_Click(object sender, RoutedEventArgs e)
private void btSend_Click(object sender, RoutedEventArgs e)
{
btSend.IsEnabled = false;
this.tblResponse.Text = "";

this.tblResponse.Text = "";

if (this.cbReaders.SelectedItem == null)
{
tblResponse.Text = "Error\n" + "Reader is not selected";
btSend.IsEnabled = true;
return;
}

if (!IsValid(this.grRequestForm))
{
tblResponse.Text = "Error\n" + "Fill the request form";
Expand All @@ -72,14 +79,18 @@ private void btSend_Click(object sender, RoutedEventArgs e)
var reader = this.cbReaders.SelectedItem.ToString();
new Thread(() =>
{
string responseText;
string responseText;
try
{

var response = controller.SendAPDU(reader, Request);
var responseMessage = String.Format("Data: {0} \n" +
"SW1: {1}; SW2: {2}", ToHexString(response.Data), ToHex(response.SW1), ToHex(response.SW2));
responseText = responseMessage;
StringBuilder builder = new StringBuilder();
builder.AppendLine(String.Format("Data: {0}", ToHexString(response.Data)));
builder.AppendLine(String.Format("SW1: {0}; SW2: {1}", ToHex(response.SW1), ToHex(response.SW2)));
builder.AppendLine();
builder.AppendLine("Reader Info:");
builder.AppendLine(ReaderInfo(reader));
responseText = builder.ToString();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -111,5 +122,15 @@ private bool IsValid(DependencyObject obj)
.OfType<DependencyObject>()
.All(IsValid);
}

private string ReaderInfo(string reader)
{
string message;
StringBuilder builder = new StringBuilder();
builder.AppendLine(String.Format("Name: {0}", reader));
builder.AppendLine(String.Format("Port: {0}", controller.GetReaderPort(reader)));
message = builder.ToString();
return message;
}
}
}
Binary file added APDUTester/pcsc-sharp.dll
Binary file not shown.

0 comments on commit c4e6f80

Please sign in to comment.