Skip to content

Commit

Permalink
use config file
Browse files Browse the repository at this point in the history
Signed-off-by: Sola <[email protected]>
  • Loading branch information
unlimitedsola committed Jun 16, 2018
1 parent b9a95e7 commit 37e0290
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 23 deletions.
4 changes: 4 additions & 0 deletions card.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"card_uid": "f28a0d00",
"card_id": "1UZV VIXZ OJFL 2U12 FEDY"
}
80 changes: 57 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,60 @@ import (
"github.com/tarm/serial"
"log"
"strings"
"io/ioutil"
"encoding/json"
"encoding/hex"
)

var cardUID = "\xf2\x8a\x0d\x00"
var cardID = strings.Replace("1UZV VIXZ OJFL 2U12 FEDY", " ", "", -1)
var cardIDPart1 = cardID[:16]
var cardIDPart2 = cardID[16:20] + strings.Repeat("\x00", 12)
type Config struct {
CardUID string `json:"card_uid"`
CardID string `json:"card_id"`
}

func check(e error) {
if e != nil {
panic(e)
}
}

func readConfigFromFile() Config {
config := Config{}
data, err := ioutil.ReadFile("card.json")
check(err)
err = json.Unmarshal(data, &config)
check(err)
return config
}

func hexToString(hexStr string) string {
bytes, err := hex.DecodeString(hexStr)
check(err)
return string(bytes)
}

func makeResponseTable(config Config) map[string]string {

cardUID := hexToString(config.CardUID)
cardID := strings.Replace(config.CardID, " ", "", -1)
cardIDPart1 := cardID[:16]
cardIDPart2 := cardID[16:20] + strings.Repeat("\x00", 12)

return map[string]string{
// is there any card? -> NOPE!
"\x02\x00\x02\x31\x30\x03\x02": "\x02\x00\x03\x31\x30\x4e\x03",
// eject card -> OK!
"\x02\x00\x02\x32\x30\x03\x01": "\x02\x00\x03\x32\x30\x59\x03",
// find card -> We got one!
"\x02\x00\x02\x35\x30\x03\x06": "\x02\x00\x03\x35\x30\x59\x03",
// get UID -> return our UID!
"\x02\x00\x02\x35\x31\x03\x07": "\x02\x00\x07\x35\x31\x59" + cardUID + "\x03",
// auth key A at sector 0 (key = 37 21 53 6a 72 40) -> You got it!
"\x02\x00\x09\x35\x32\x00\x37\x21\x53\x6a\x72\x40\x03\x12": "\x02\x00\x04\x35\x32\x00\x59\x03",
// read sector 0 block 1 -> Our card ID
"\x02\x00\x04\x35\x33\x00\x01\x03\x02": "\x02\x00\x15\x35\x33\x00\x01\x59" + cardIDPart1 + "\x03",
"\x02\x00\x04\x35\x33\x00\x02\x03\x01": "\x02\x00\x15\x35\x33\x00\x02\x59" + cardIDPart2 + "\x03",
}

var table = map[string]string{
// is there any card? -> NOPE!
"\x02\x00\x02\x31\x30\x03\x02": "\x02\x00\x03\x31\x30\x4e\x03",
// eject card -> OK!
"\x02\x00\x02\x32\x30\x03\x01": "\x02\x00\x03\x32\x30\x59\x03",
// find card -> We got one!
"\x02\x00\x02\x35\x30\x03\x06": "\x02\x00\x03\x35\x30\x59\x03",
// get UID -> return our UID!
"\x02\x00\x02\x35\x31\x03\x07": "\x02\x00\x07\x35\x31\x59" + cardUID + "\x03",
// auth key A at sector 0 (key = 37 21 53 6a 72 40) -> You got it!
"\x02\x00\x09\x35\x32\x00\x37\x21\x53\x6a\x72\x40\x03\x12": "\x02\x00\x04\x35\x32\x00\x59\x03",
// read sector 0 block 1 -> Our card ID
"\x02\x00\x04\x35\x33\x00\x01\x03\x02": "\x02\x00\x15\x35\x33\x00\x01\x59" + cardIDPart1 + "\x03",
"\x02\x00\x04\x35\x33\x00\x02\x03\x01": "\x02\x00\x15\x35\x33\x00\x02\x59" + cardIDPart2 + "\x03",
}

func calcBCC(data string) string {
Expand All @@ -37,11 +70,13 @@ func calcBCC(data string) string {
}

func main() {
config := readConfigFromFile()
table := makeResponseTable(config)
fmt.Printf("dmt-io-emulator is ready!\n")
fmt.Printf("card uid: %x\n", cardUID)
fmt.Printf("card id: %s\n", cardID)
config := &serial.Config{Name: "COM9", Baud: 9600}
port, err := serial.OpenPort(config)
fmt.Printf("Card UID: %s\n", config.CardUID)
fmt.Printf("Card ID: %s\n", config.CardID)
portConfig := &serial.Config{Name: "COM9", Baud: 9600}
port, err := serial.OpenPort(portConfig)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -72,7 +107,6 @@ func main() {
}
cmd = ""
}

}
}

Expand Down

0 comments on commit 37e0290

Please sign in to comment.