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

How to use SelectPassiveTarget with libfreefare? #1

Open
Lesterpig opened this issue Jan 18, 2016 · 4 comments
Open

How to use SelectPassiveTarget with libfreefare? #1

Lesterpig opened this issue Jan 18, 2016 · 4 comments

Comments

@Lesterpig
Copy link

Hi,

I'm having some issues dealing with libfreefare.
I would like to transform a nfc.ISO14443aTarget into a freefare.Tag to use the SelectPassiveTarget feature of libnfc.

Here is my code:

func Connect(device nfc.Device) (*freefare.DESFireTag, error) {

    // Scan for tags
    modulation := nfc.Modulation{
        Type: nfc.ISO14443a,
        BaudRate: nfc.Nbr424,
    }

    nfcTag, _ := device.InitiatorSelectPassiveTarget(modulation, nil)
    isoTarget, _ := nfcTag.(*nfc.ISO14443aTarget)
    rawTag, _ := freefare.NewTag(device, isoTarget)

    tag := rawTag.(freefare.DESFireTag)

    err = tag.Connect()
    if err != nil {
        return nil, err
    }

    return &tag, nil

}

Am I doing things right here?
The result is a segfault at:

runtime.cgocall(0x454930, 0xc82003fdc8, 0x0)
    /usr/lib/go/src/runtime/cgocall.go:120 +0x11b fp=0xc82003fd70 sp=0xc82003fd40
github.com/fuzxxl/freefare/0.3/freefare._C2func_mifare_desfire_connect(0x7f4e8c006f10, 0x0, 0x0, 0x0)
    ??:0 +0x47 fp=0xc82003fdc8 sp=0xc82003fd70
github.com/fuzxxl/freefare/0.3/freefare.DESFireTag.Connect(0xc8200103a0, 0xffff, 0x0, 0x0)

Thank you for your help!

@clausecker
Copy link
Owner

Why don't you do any error checking, especially with the type assertions? Please make sure that all type assertions are successful. In the meanwhile, let me check what went wrong.

The code hasn't been really tested (cf. README), let me see what I missed in there.

@clausecker
Copy link
Owner

It seems like I get a NULL pointer somewhere. I should definitely check for that in case someone forgets to check for errors.

@Lesterpig
Copy link
Author

Thank you for you answer.

Here is an updated version with its result:

func Connect(device nfc.Device) (*freefare.DESFireTag, error) {

    // Scan for tags
    modulation := nfc.Modulation{
        Type: nfc.ISO14443a,
        BaudRate: nfc.Nbr424,
    }

    nfcTag, err := device.InitiatorSelectPassiveTarget(modulation, nil)
    fmt.Println(err)
    isoTarget, ok := nfcTag.(*nfc.ISO14443aTarget)
    fmt.Println(ok)
    rawTag, err := freefare.NewTag(device, isoTarget)
    fmt.Println(err)

    tag := rawTag.(freefare.DESFireTag)
    fmt.Println(tag)

    err = tag.Connect()
    if err != nil {
        return nil, err
    }

    return &tag, nil

}
<nil>
true
<nil>
Mifare DESFire
fatal error: unexpected signal during runtime execution
[signal 0xb code=0x2 addr=0x7f53647f9c47 pc=0x7f537bb9e9b8]

@clausecker
Copy link
Owner

Well, okay, that's interesting. Let me debug further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants