Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PN532 binding does not work correctly when configured "AsTarget" #2029

Open
raffaeler opened this issue Feb 6, 2023 · 1 comment
Open

PN532 binding does not work correctly when configured "AsTarget" #2029

raffaeler opened this issue Feb 6, 2023 · 1 comment
Assignees
Labels
area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio bug Something isn't working Priority:2 Work that is important, but not critical for the release

Comments

@raffaeler
Copy link
Contributor

The physical device is a PN532 board from Elechouse similar to this one

The code used to test the device (with a Raspberry PI) is the one published at the binding page

The nuget packages in the test project are the following:

    <PackageReference Include="Iot.Device.Bindings" Version="2.2.0" />
    <PackageReference Include="System.Device.Gpio" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />

I made the tests using both the I2C and the RS232 connections. In both cases the communication breaks:

  • the I2C communication breaks and the device needs to be reset before re-testing it
  • the RS232 communication breaks after a while but still shows a timeout communication error

Log using the I2C

Mifare
08-00-00-00-00-60
feliCa
01-FE-A2-A3-A4-A5-A6-A7-C0-C1-C2-C3-C4-C5-C6-C7-FF-FF
picc
AA-99-88-77-66-55-44-33-22-11-00-00
Before WriteCommand(TgInitAsTarget)WriteCommand: CommandSet TgInitAsTarget
Bytes to send: 04-08-00-00-00-00-60-01-FE-A2-A3-A4-A5-A6-A7-C0-C1-C2-C3-C4-C5-C6-C7-FF-FF-AA-99-88-77-66-55-44-33-22-11-00-00
W> 00-FF-27-D9-D4-8C-04-08-00-00-00-00-60-01-FE-A2-A3-A4-A5-A6-A7-C0-C1-C2-C3-C4-C5-C6-C7-FF-FF-AA-99-88-77-66-55-44-33-22-11-00-00-99
Before ReadResponse(TgInitAsTarget)WriteCommand: CommandSet SetParameters 
Bytes to send: 20
W> 00-FF-03-FD-D4-12-20-FA
Mifare
08-00-00-00-00-60
feliCa
01-FE-A2-A3-A4-A5-A6-A7-C0-C1-C2-C3-C4-C5-C6-C7-FF-FF
picc
AA-99-88-77-66-55-44-33-22-11-00-00
Before WriteCommand(TgInitAsTarget)WriteCommand: CommandSet TgInitAsTarget 
Bytes to send: 04-08-00-00-00-00-60-01-FE-A2-A3-A4-A5-A6-A7-C0-C1-C2-C3-C4-C5-C6-C7-FF-FF-AA-99-88-77-66-55-44-33-22-11-00-00
W> 00-FF-27-D9-D4-8C-04-08-00-00-00-00-60-01-FE-A2-A3-A4-A5-A6-A7-C0-C1-C2-C3-C4-C5-C6-C7-FF-FF-AA-99-88-77-66-55-44-33-22-11-00-00-99
Before ReadResponse(TgInitAsTarget)WriteCommand: CommandSet SetParameters 
Bytes to send: 20
W> 00-FF-03-FD-D4-12-20-FA
Mifare
08-00-00-00-00-60
feliCa
01-FE-A2-A3-A4-A5-A6-A7-C0-C1-C2-C3-C4-C5-C6-C7-FF-FF
picc
AA-99-88-77-66-55-44-33-22-11-00-00
Before WriteCommand(TgInitAsTarget)WriteCommand: CommandSet TgInitAsTarget 
Bytes to send: 04-08-00-00-00-00-60-01-FE-A2-A3-A4-A5-A6-A7-C0-C1-C2-C3-C4-C5-C6-C7-FF-FF-AA-99-88-77-66-55-44-33-22-11-00-00
W> 00-FF-27-D9-D4-8C-04-08-00-00-00-00-60-01-FE-A2-A3-A4-A5-A6-A7-C0-C1-C2-C3-C4-C5-C6-C7-FF-FF-AA-99-88-77-66-55-44-33-22-11-00-00-99
Before ReadResponse(TgInitAsTarget)Unhandled exception. System.IO.IOException: Error 5 performing I2C data transfer.   
at System.Device.I2c.UnixI2cBus.WriteReadCore(UInt16 deviceAddress, Byte* writeBuffer, Byte* readBuffer, UInt16 writeBufferLength, UInt16 readBufferLength)   at System.Device.I2c.UnixI2cBus.Read(Int32 deviceAddress, Span`1 buffer)   
at TestNfc.Pn532.ReadResponseI2C(CommandSet commandSet, Span`1 readData) in H:\dev.git\libraries\SipLibrary\src\SipLibrarySolution\TestNfc\Pn532.cs:line 2000   
at TestNfc.Pn532.InitAsTarget(TargetModeInitialization mode, TargetMifareParameters mifare, TargetFeliCaParameters feliCa, TargetPiccParameters picc) in H:\dev.git\libraries\SipLibrary\src\SipLibrarySolution\TestNfc\Pn532.cs:line 923   
at TestNfc.Program.AsTarget(Pn532 pn532) in H:\dev.git\libraries\SipLibrary\src\SipLibrarySolution\TestNfc\Program.cs:line 68   
at TestNfc.Program.Start() in H:\dev.git\libraries\SipLibrary\src\SipLibrarySolution\TestNfc\Program.cs:line 59   
at TestNfc.Program.Main(String[] args) in H:\dev.git\libraries\SipLibrary\src\SipLibrarySolution\TestNfc\Program.cs:line 25
Aborted

Log using the RS232

01-FE-A2-A3-A4-A5-A6-A7-C0-C1-C2-C3-C4-C5-C6-C7-FF-FFpiccAA-99-88-77-66-55-44-33-22-11-00-00
Before WriteCommand(TgInitAsTarget)WriteCommand: CommandSet TgInitAsTarget Bytes to send: 04-08-00-00-00-00-60-01-FE-A2-A3-A4-A5-A6-A7-C0-C1-C2-C3-C4-C5-C6-C7-FF-FF-AA-99-88-77-66-55-44-33-22-11-00-00
Before ReadResponse(TgInitAsTarget)
Unhandled exception. System.TimeoutException: The operation has timed out.   at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count, Int32 timeout)   
at System.IO.Ports.SerialPort.ReadByte()   
at TestNfc.Pn532.ReadResponseSerial(CommandSet commandSet, Span`1 readData) in H:\dev.git\libraries\SipLibrary\src\SipLibrarySolution\TestNfc\Pn532.cs:line 1786   
at TestNfc.Pn532.ReadResponse(CommandSet commandSet, Span`1 readData) in H:\dev.git\libraries\SipLibrary\src\SipLibrarySolution\TestNfc\Pn532.cs:line 1747   
at TestNfc.Pn532.InitAsTarget(TargetModeInitialization mode, TargetMifareParameters mifare, TargetFeliCaParameters feliCa, TargetPiccParameters picc) in H:\dev.git\libraries\SipLibrary\src\SipLibrarySolution\TestNfc\Pn532.cs:line 932   
at TestNfc.Program.AsTarget(Pn532 pn532) in H:\dev.git\libraries\SipLibrary\src\SipLibrarySolution\TestNfc\Program.cs:line 70   
at TestNfc.Program.Start() in H:\dev.git\libraries\SipLibrary\src\SipLibrarySolution\TestNfc\Program.cs:line 61   
at TestNfc.Program.Main(String[] args) in H:\dev.git\libraries\SipLibrary\src\SipLibrarySolution\TestNfc\Program.cs:line 26
Aborted

The logs have been instrumented with additional Console.WriteLine. This caused the slow to exhibit some delays and caused some improvements (the exception was visible after a while) but did not resolve the issue.

I already discussed the issue with @Ellerbach (the original author) and made multiple tests with him to verify what was going on.

@raffaeler raffaeler added bug Something isn't working area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio labels Feb 6, 2023
@krwq krwq added the Priority:2 Work that is important, but not critical for the release label Feb 9, 2023
@Ellerbach
Copy link
Member

I've finally found a bit of time and adjusted a bit the code, played with it. If you adjust it like this:

(modeInitialized, retData) = pn532.InitAsTarget(
    TargetModeInitialization.PiccOnly | TargetModeInitialization.PassiveOnly,
    new TargetMifareParameters()
    {
        NfcId3 = new byte[] { 0x01, 0x02, 0x03 },
        Atqa = new byte[] { 0x04, 0x00 },
        Sak = 0x20
    },
    new TargetFeliCaParameters()
    {
        NfcId2 = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        Pad = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        SystemCode = new byte[] { 0x00, 0x00 }
    },
    new TargetPiccParameters());

You'll get it to work and you'll get the following answer:

PN532 as a target: ISDep: False, IsPicc True, B106kbps, Mifare
Initiator: E0-80
Status: None, Data: 00-A4-04-00-07-D2-76-00-00-85-01-01-00

In this case:
00 = no error
A4 = ISO7816 to select a file (it's not Mifare native as it know it's an emulator and not a real card)
04 = first parameter = in this case select by name
00 = second parameter (there is none in this case)
07 = data length
the data themselves is a magic packet asking for the first file.

So, it's about implementing properly the ISO7816 protocole or at least a minimum but all up, it does work as expected

I may continue to look at this a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio bug Something isn't working Priority:2 Work that is important, but not critical for the release
Projects
None yet
Development

No branches or pull requests

3 participants