This repository was archived by the owner on Feb 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
122 lines (105 loc) · 2.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
nxprd "github.com/hohlerde/go-nxprd"
)
func getTag(t nxprd.TagType) string {
switch t {
case nxprd.TagType1:
return "1"
case nxprd.TagType2:
return "2"
case nxprd.TagType3:
return "3"
case nxprd.TagType4A:
return "4A"
case nxprd.TagTypeP2P:
return "P2P"
case nxprd.TagTypeNFCDEP4A:
return "NFC_DEP & 4A"
default:
return "Undefined"
}
}
func getTech(t nxprd.TechType) string {
switch t {
case nxprd.TechA:
return "A"
case nxprd.TechB:
return "B"
case nxprd.TechF:
return "F"
case nxprd.TechV15693T5T:
return "V / ISO 15693 / T5T"
case nxprd.Tech18000p3m3EPCGen2:
return "ISO 18000p3m3 / EPC Gen2"
default:
return "Undefined"
}
}
func slice2Str(arr []byte) string {
var buffer bytes.Buffer
for i := 0; i < len(arr); i++ {
buffer.WriteString(fmt.Sprintf("0x%02X ", arr[i]))
}
return strings.TrimSpace(buffer.String())
}
func printInfo(dev *nxprd.Device) {
fmt.Printf("Card : %s\n", dev.Params.DevType)
fmt.Printf("Tag type : %s\n", getTag(dev.Params.TagType))
fmt.Printf("Technology type : %s\n", getTech(dev.Params.TechType))
fmt.Printf("UID : %s\n", slice2Str(dev.Params.UID))
fmt.Printf("ATQ(A) : %s\n", slice2Str(dev.Params.ATQ))
fmt.Printf("SAK : 0x%02X\n", dev.Params.SAK)
}
func main() {
termReader := bufio.NewReader(os.Stdin)
fmt.Println("Mifare Ultralight Test")
fmt.Println("\nPlace a card near the reader and press <Enter>")
termReader.ReadString('\n')
// Initialize the library. We need to do that once.
if err := nxprd.Init(); err != nil {
fmt.Println("\nError: Initializing NXP library failed")
fmt.Println(err)
return
}
// Try to detect/discover a card/tag for 1000ms. Discover will block.
// 1000ms is the default timeout.
dev, err := nxprd.Discover()
if err != nil {
fmt.Println("\nCouldn't detect card")
fmt.Println(err)
return
}
if dev.Params.DevType == nxprd.Unknown {
// A card/tag could be detected, but the wrapper doesn't support it yet.
// So we can't read or write blocks, but we can access some parameters.
fmt.Println("\nFound an unknown card")
fmt.Println("")
printInfo(dev)
return
}
fmt.Println("")
printInfo(dev)
// The standard Mifare Ultralight card has a minimum of 15 blocks (pages),
// each block consisting of 4 bytes.
fmt.Println("\nTrying to read memory pages 0-15:")
fmt.Println("")
for i := 0; i < 16; i++ {
fmt.Printf("Page %02d: ", i)
// Read each block
buffer, err := dev.Reader.ReadBlock(i)
if err != nil {
fmt.Println(err)
} else {
// and print the bytes in hex format.
fmt.Println(slice2Str(buffer))
}
}
// In order to cleanup the C part of the wrapper DeInit need to be called.
nxprd.DeInit()
}