forked from google/periph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (129 loc) · 3.75 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2018 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
// mfrc522 reads RFID tags.
package main
import (
"errors"
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"
"periph.io/x/periph/conn/gpio/gpioreg"
"periph.io/x/periph/conn/spi/spireg"
"periph.io/x/periph/experimental/devices/mfrc522"
"periph.io/x/periph/experimental/devices/mfrc522/commands"
"periph.io/x/periph/host"
)
func mainImpl() error {
sector := flag.Int("sector", 1, "Sector to access")
block := flag.Int("block", 0, "Block to access")
rsPin := flag.String("rs-pin", "", "Reset pin")
irqPin := flag.String("irq-pin", "", "IRQ pin")
keyCommand := flag.Bool("wa", false, "Overwrite keys")
blockCommand := flag.String("wb", "", "Overwrite block by provided data (comma-separated list of 16 bytes)")
spiID := flag.String("spi", "", "SPI device")
key := flag.String("key", "", "Comma-separated key bytes")
flag.Parse()
if *irqPin == "" || *rsPin == "" {
return errors.New("please provide -rs-pin and -irq-pin arguments, or -h for help")
}
if _, err := host.Init(); err != nil {
return err
}
var currentAccessKey [6]byte
if *key != "" {
keyBytes := strings.SplitN(*key, ",", 6)
if len(keyBytes) != 6 {
return errors.New("key should consist of 6 decimal numbers")
}
for i, v := range keyBytes {
intV, err := strconv.ParseUint(v, 10, 8)
if err != nil {
return err
}
currentAccessKey[i] = byte(intV)
}
} else {
copy(currentAccessKey[:], mfrc522.DefaultKey[:])
}
currentAccessMethod := byte(commands.PICC_AUTHENT1B)
spiDev, err := spireg.Open(*spiID)
if err != nil {
return err
}
rsPinReg := gpioreg.ByName(*rsPin)
if rsPinReg == nil {
return fmt.Errorf("reset pin %s can not be found", *rsPin)
}
irqPinReg := gpioreg.ByName(*irqPin)
if rsPinReg == nil {
return fmt.Errorf("IRQ pin %s can not be found", *irqPin)
}
rfid, err := mfrc522.NewSPI(spiDev, rsPinReg, irqPinReg)
if err != nil {
return err
}
timeout := 10 * time.Second
data, err := rfid.ReadCard(timeout, currentAccessMethod, *sector, *block, currentAccessKey)
if err != nil {
return err
}
auth, err := rfid.ReadAuth(timeout, currentAccessMethod, *sector, currentAccessKey)
if err != nil {
return err
}
var access mfrc522.BlocksAccess
access.Init(auth[6:10])
fmt.Printf("RFID sector %d, block %d : %v, auth: %v\n", *sector, *block, data, auth)
fmt.Printf("Permissions: B0: %s, B1: %s, B2: %s, B3/A: %s\n",
strconv.FormatUint(uint64(access.B0), 2),
strconv.FormatUint(uint64(access.B1), 2),
strconv.FormatUint(uint64(access.B2), 2),
strconv.FormatUint(uint64(access.B3), 2),
)
if *keyCommand {
err = rfid.WriteSectorTrail(timeout, commands.PICC_AUTHENT1A,
*sector,
[6]byte{1, 2, 3, 4, 5, 6},
[6]byte{6, 5, 4, 3, 2, 1},
&mfrc522.BlocksAccess{
B0: mfrc522.RAB_WB_IB_DAB,
B1: mfrc522.RB_WB_IN_DN,
B2: mfrc522.AnyKeyRWID,
B3: mfrc522.KeyA_RN_WN_BITS_RAB_WN_KeyB_RN_WN,
},
currentAccessKey,
)
if err != nil {
return err
}
fmt.Println("Write successful")
} else if *blockCommand != "" {
var defaultDataBytes [16]byte
bytesBuffer := strings.Split(*blockCommand, ",")
if len(bytesBuffer) != 16 {
return errors.New("data bytes must contain exactly 16 elements")
}
for i := range defaultDataBytes {
intVal, err := strconv.ParseUint(bytesBuffer[i], 10, 8)
if err != nil {
return err
}
defaultDataBytes[i] = byte(intVal)
}
err = rfid.WriteCard(timeout, currentAccessMethod, *sector, *block, defaultDataBytes, currentAccessKey)
if err != nil {
return err
}
}
return rfid.Halt()
}
func main() {
if err := mainImpl(); err != nil {
fmt.Fprintf(os.Stderr, "mfrc522: %s.\n", err)
os.Exit(1)
}
}