clausecker / freefare

Go bindings for the libfreefare
GNU Lesser General Public License v3.0
17 stars 2 forks source link

How to use SelectPassiveTarget with libfreefare? #1

Open Lesterpig opened 8 years ago

Lesterpig commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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

Lesterpig commented 8 years ago

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 commented 8 years ago

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