Created
February 27, 2015 01:20
-
-
Save messinm/7086bfce591bf6f1d554 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// #include <stdlib.h> | |
import "C" | |
import ("fmt" | |
"github.com/fuzxxl/nfc/2.0/nfc" | |
"reflect" | |
"unsafe") | |
func main() { | |
fmt.Printf("libnfc version %v\n",nfc.Version()) | |
//fmt.Printf("\n") | |
devices,err := nfc.ListDevices() | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
for i, v := range devices { | |
fmt.Printf("device [%d]=%v\n", i, v) | |
} | |
} | |
fmt.Println("opening first available device...\n") | |
dev,err := nfc.Open("") | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
defer dev.Close() | |
fmt.Printf("connection =%v\n", dev.Connection()) | |
} | |
fmt.Println("initiator init...\n") | |
err = dev.InitiatorInit() | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
mods,err := dev.SupportedModulations(nfc.InitiatorMode) | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
for i,v := range mods { | |
fmt.Printf("modulation [%d]=%v\n", i, v) | |
} | |
var mod nfc.Modulation | |
mod.Type = nfc.ISO14443a | |
mod.BaudRate = nfc.Nbr106 | |
fmt.Println("looking for targets..\n") | |
targets, err := dev.InitiatorListPassiveTargets(mod) | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
for i,v := range targets { | |
fmt.Printf("target [%d]=%v type=%v\n", i, v, reflect.TypeOf(v)) | |
} | |
/* | |
type ISO14443aTarget struct { | |
Atqa [2]byte | |
Sak byte | |
UIDLen int // length of the Uid field | |
UID [10]byte | |
AtsLen int // length of the ATS field | |
// Maximal theoretical ATS is FSD-2, FSD=256 for FSDI=8 in RATS | |
Ats [254]byte // up to 254 bytes | |
Baud int // Baud rate | |
} | |
from libnfc nfc-types.h | |
00185 typedef struct { | |
00186 uint8_t abtAtqa[2]; | |
00187 uint8_t btSak; | |
00188 size_t szUidLen; | |
00189 uint8_t abtUid[10]; | |
00190 size_t szAtsLen; | |
00191 uint8_t abtAts[254]; // Maximal theoretical ATS is FSD-2, FSD=256 for FSDI=8 in RATS | |
00192 } nfc_iso14443a_info; | |
*/ | |
unsafe_p := (unsafe.Pointer(targets[0].Marshall())) | |
defer C.free(unsafe_p) | |
mtarget := (*nfc.ISO14443aTarget)(unsafe_p) | |
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) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment