Checkpoint 2
This commit is contained in:
90
wiegand.go
90
wiegand.go
@@ -12,7 +12,12 @@ import (
|
||||
"github.com/warthog618/gpiod"
|
||||
)
|
||||
|
||||
type Wiegand struct {
|
||||
type Wiegand interface {
|
||||
GetCardUid() (uint64, error)
|
||||
OpenDoor()
|
||||
}
|
||||
|
||||
type WiegandHW struct {
|
||||
aLine *gpiod.Line
|
||||
bLine *gpiod.Line
|
||||
bits [64]bool
|
||||
@@ -23,7 +28,7 @@ type Wiegand struct {
|
||||
solenoidLine *gpiod.Line
|
||||
}
|
||||
|
||||
func (w *Wiegand) OpenDoor() {
|
||||
func (w *WiegandHW) OpenDoor() {
|
||||
fmt.Println("Open")
|
||||
w.solenoidLine.SetValue(1)
|
||||
d, _ := time.ParseDuration("500ms")
|
||||
@@ -39,65 +44,58 @@ func printCardId(card uint64) {
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
func (w *Wiegand) cardRunner(validUids ValidUids) {
|
||||
for {
|
||||
// Wait for bit timeout
|
||||
fmt.Printf("Waiting for bit timeout\n")
|
||||
<-w.bitTimeoutTimer.C
|
||||
fmt.Printf("\n")
|
||||
|
||||
if w.bitNr != 64 {
|
||||
fmt.Printf("We got less than 64 bits: %d\n", w.bitNr)
|
||||
}
|
||||
|
||||
var card uint64 = 0
|
||||
for i := 63; i != 0; i-- {
|
||||
if w.bits[i] == true {
|
||||
card |= 1 << (63 - i)
|
||||
}
|
||||
}
|
||||
|
||||
printCardId(card)
|
||||
|
||||
b := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(b, card)
|
||||
hashed, err := scrypt.Key(b, []byte("hashsah"), 16384, 8, 1, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
hashedHex := hex.EncodeToString(hashed)
|
||||
fmt.Println(hashedHex)
|
||||
|
||||
_, ok := validUids[hashedHex]
|
||||
if ok {
|
||||
fmt.Println("Opening door")
|
||||
w.OpenDoor()
|
||||
} else {
|
||||
fmt.Println("Unknown card")
|
||||
}
|
||||
|
||||
w.bitNr = 0
|
||||
func hashCardUid(card uint64) string {
|
||||
b := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(b, card)
|
||||
hashed, err := scrypt.Key(b, []byte(config.uid_salt), 16384, 8, 1, 64)
|
||||
if err != nil {
|
||||
panic(err) // can only happen when scrypt params are garbage
|
||||
}
|
||||
|
||||
hashedHex := hex.EncodeToString(hashed)
|
||||
return hashedHex
|
||||
}
|
||||
|
||||
func (w *Wiegand) wiegandAEvent(evt gpiod.LineEvent) {
|
||||
func (w *WiegandHW) GetCardUid() (uint64, error) {
|
||||
// Wait for bit timeout
|
||||
fmt.Printf("Waiting for bit timeout\n")
|
||||
|
||||
<-w.bitTimeoutTimer.C
|
||||
fmt.Printf("\n")
|
||||
|
||||
defer func() { w.bitNr = 0 }()
|
||||
|
||||
if w.bitNr != 64 {
|
||||
return 0, fmt.Errorf("We got less than 64 bits: %d\n", w.bitNr)
|
||||
}
|
||||
|
||||
var card uint64 = 0
|
||||
for i := 63; i != 0; i-- {
|
||||
if w.bits[i] == true {
|
||||
card |= 1 << (63 - i)
|
||||
}
|
||||
}
|
||||
|
||||
return card, nil
|
||||
}
|
||||
|
||||
func (w *WiegandHW) wiegandAEvent(evt gpiod.LineEvent) {
|
||||
w.bitTimeoutTimer.Reset(w.bitTimeout)
|
||||
w.bits[w.bitNr] = false
|
||||
fmt.Printf("0")
|
||||
w.bitNr += 1
|
||||
}
|
||||
|
||||
func (w *Wiegand) wiegandBEvent(evt gpiod.LineEvent) {
|
||||
func (w *WiegandHW) wiegandBEvent(evt gpiod.LineEvent) {
|
||||
w.bitTimeoutTimer.Reset(w.bitTimeout)
|
||||
w.bits[w.bitNr] = true
|
||||
fmt.Printf("1")
|
||||
w.bitNr += 1
|
||||
}
|
||||
|
||||
func WiegandSetup(a int, b int, bitTimeout time.Duration, solenoid int) *Wiegand {
|
||||
func WiegandSetup(a int, b int, bitTimeout time.Duration, solenoid int) *WiegandHW {
|
||||
|
||||
var wiegand Wiegand
|
||||
var wiegand WiegandHW
|
||||
wiegand.bitTimeout = bitTimeout
|
||||
|
||||
wiegand.bitTimeoutTimer = time.NewTimer(wiegand.bitTimeout)
|
||||
@@ -128,7 +126,7 @@ func WiegandSetup(a int, b int, bitTimeout time.Duration, solenoid int) *Wiegand
|
||||
return &wiegand
|
||||
}
|
||||
|
||||
func (w *Wiegand) WiegandClose() {
|
||||
func (w *WiegandHW) WiegandClose() {
|
||||
w.aLine.Close()
|
||||
w.bLine.Close()
|
||||
w.solenoidLine.Close()
|
||||
|
||||
Reference in New Issue
Block a user