Coder Social home page Coder Social logo

Comments (6)

clausecker avatar clausecker commented on August 28, 2024

Sounds like a bug in my code to me. Let me check please.

from nfc.

clausecker avatar clausecker commented on August 28, 2024
unsafe_p := (unsafe.Pointer(targets[0].Marshall()))
defer C.free(unsafe_p)
mtarget := (*nfc.ISO14443aTarget)(unsafe_p)

Casting an unsafe.Pointer you get from Marshall() to one of the target types can't work. The pointer you get from Marshall() points to a C type, whereas the target types are entirely different types. As per documentation for Target.Marshall():

Marshall() returns a pointer to an nfc_target allocated with C.malloc() that contains the same data as the Target. Don't forget to C.free() the result of Marshall() afterwards. A runtime panic may occur if any slice referenced by a Target has been made larger than the maximum length mentioned in the respective comments.

Where nfc_target refers to the C type nfc_target, not the Go type. If you want to convert a Target into an object of concrete type, use a type assertion like this (replacing the code above):

mtarget, ok := targets[0].(nfc.ISO14443aTarget)
if !ok {
    // targets[0] is apparently not an ISO 14443a target. Do something here...
}

Please tell me if this solved your problem and then close the issue.

from nfc.

messinm avatar messinm commented on August 28, 2024

Thanks for the quick response.

I tried your suggestion, but it won't compile. See snippet and error message below. Seems that nfc.ISO14443aTarget should implement nfc.Target, but doesn't.

                            //unsafe_p := (unsafe.Pointer(targets[0].Marshall()))
                            //defer C.free(unsafe_p)
                            //mtarget := (*nfc.ISO14443aTarget)(unsafe_p) 

                            mtarget, ok := targets[0].(nfc.ISO14443aTarget)
                            if !ok {
                               fmt.Printf("Not ISO14443aTarget!")
                            } else {
            fmt.Printf("Atqa =%v\n",mtarget.Atqa)
            fmt.Printf("Sak =%v\n",mtarget.Sak) 
                    fmt.Printf("UIDLen =%v\n",mtarget.UIDLen)  
                    fmt.Printf("UID =%v\n",mtarget.UID) 
                            }

//does not compile:
//../src/messinm2/testnfc/testnfc.go:81: impossible type assertion:
// nfc.ISO14443aTarget does not implement nfc.Target (Marshall method has pointer receiver)

from nfc.

clausecker avatar clausecker commented on August 28, 2024

Sorry. Cast to *nfc.ISO14443aTarget, not nfc.ISO14443aTarget. I don't know why I forgot that star.

from nfc.

messinm avatar messinm commented on August 28, 2024

That works! Thanks, and thanks very much for the lib.

from nfc.

clausecker avatar clausecker commented on August 28, 2024

It was a pleasure to me.

from nfc.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.