freefare icon indicating copy to clipboard operation
freefare copied to clipboard

How to use SelectPassiveTarget with libfreefare?

Open Lesterpig opened this issue 9 years ago • 4 comments

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!

Lesterpig avatar Jan 18 '16 00:01 Lesterpig

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 avatar Jan 18 '16 01:01 clausecker

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

clausecker avatar Jan 18 '16 01:01 clausecker

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]

Lesterpig avatar Jan 18 '16 06:01 Lesterpig

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

clausecker avatar Jan 18 '16 07:01 clausecker