This commit is contained in:
Matthias Fulz 2022-05-08 02:18:47 +02:00
parent a293e6aa8a
commit 4a1c86d80d
2 changed files with 105 additions and 2 deletions

View File

@ -22,6 +22,8 @@ const (
QENC_CMD_GET_KEYS QEncCmd = 0x09 QENC_CMD_GET_KEYS QEncCmd = 0x09
QENC_CMD_GET_BUFFSIZE QEncCmd = 0x0A QENC_CMD_GET_BUFFSIZE QEncCmd = 0x0A
QENC_CMD_SET_KEYS QEncCmd = 0x0B QENC_CMD_SET_KEYS QEncCmd = 0x0B
QENC_CMD_GET_CFG QEncCmd = 0x0C
QENC_CMD_INITIALIZE QEncCmd = 0x0D
) )
type QEncMode byte type QEncMode byte
@ -51,6 +53,43 @@ const (
QENC_CFG_TIMEOUT QEncCfg = 0x03 QENC_CFG_TIMEOUT QEncCfg = 0x03
) )
type QCfg struct {
val []byte
}
func (q *QCfg) MaxError() int {
return int(q.val[0])
}
func (q *QCfg) ErrorCount() int {
return int(q.val[1])
}
func (q *QCfg) ParanoiaMode() bool {
if q.val[2] == 0 {
return false
}
return true
}
func (q *QCfg) SecureMode() bool {
if q.val[3] == 0 {
return false
}
return true
}
func (q *QCfg) Timeout() int {
return int(q.val[4])
}
func (q *QCfg) Initialized() bool {
if q.val[5] == 0 {
return false
}
return true
}
func QCmdReset(dev *QEncDevice) (err error) { func QCmdReset(dev *QEncDevice) (err error) {
buf, err := initBuffer(dev, QENC_CMD_RESET) buf, err := initBuffer(dev, QENC_CMD_RESET)
if err != nil { if err != nil {
@ -61,6 +100,36 @@ func QCmdReset(dev *QEncDevice) (err error) {
return err return err
} }
func QCmdUnlock(dev *QEncDevice) (err error) {
buf, err := initBuffer(dev, QENC_CMD_UNLOCK)
if err != nil {
return err
}
_, err = dev.SendBuffer(buf)
return err
}
func QCmdLock(dev *QEncDevice) (err error) {
buf, err := initBuffer(dev, QENC_CMD_LOCK)
if err != nil {
return err
}
_, err = dev.SendBuffer(buf)
return err
}
func QCmdInitialize(dev *QEncDevice) (err error) {
buf, err := initBuffer(dev, QENC_CMD_INITIALIZE)
if err != nil {
return err
}
_, err = dev.SendBuffer(buf)
return err
}
func QCmdEncrypt(dev *QEncDevice, data []byte) (err error) { func QCmdEncrypt(dev *QEncDevice, data []byte) (err error) {
return sendData(dev, data, QENC_CMD_ENCRYPT) return sendData(dev, data, QENC_CMD_ENCRYPT)
} }
@ -127,6 +196,28 @@ func QCmdSetKeys(dev *QEncDevice, data []byte) (err error) {
return sendData(dev, data, QENC_CMD_SET_KEYS) return sendData(dev, data, QENC_CMD_SET_KEYS)
} }
func QCmdGetCfg(dev *QEncDevice) (ret *QCfg, err error) {
buf, err := initBuffer(dev, QENC_CMD_GET_CFG)
if err != nil {
return ret, err
}
rbuf, err := dev.SendBuffer(buf)
if err != nil {
return ret, err
}
dataLen := binary.LittleEndian.Uint16(rbuf)
if int(dataLen) != 6 {
return ret, fmt.Errorf("Invalid data")
}
ret = &QCfg{}
ret.val = make([]byte, 6)
copy(ret.val, rbuf[3:3+dataLen])
return ret, nil
}
func setCfg(dev *QEncDevice, cfg QEncCfg, val uint8) (err error) { func setCfg(dev *QEncDevice, cfg QEncCfg, val uint8) (err error) {
buf, err := initBuffer(dev, QENC_CMD_SET_CFG) buf, err := initBuffer(dev, QENC_CMD_SET_CFG)
if err != nil { if err != nil {

View File

@ -81,6 +81,10 @@ func (d *QEncDevice) SendBuffer(data []byte) (ret []byte, err error) {
} }
func (d *QEncDevice) setBufSize() (err error) { func (d *QEncDevice) setBufSize() (err error) {
if d.bufSize > 0 {
return nil
}
buf_sizes := []int{8, 16, 32, 64, 128} buf_sizes := []int{8, 16, 32, 64, 128}
for _, bs := range buf_sizes { for _, bs := range buf_sizes {
@ -93,14 +97,12 @@ func (d *QEncDevice) setBufSize() (err error) {
} }
sbuf[2] = byte(QENC_CMD_GET_BUFFSIZE) sbuf[2] = byte(QENC_CMD_GET_BUFFSIZE)
fmt.Printf("Trying: %d\n", buf_max)
if _, err = d.dev.Write(sbuf); err != nil { if _, err = d.dev.Write(sbuf); err != nil {
return fmt.Errorf("Failed to write to device: '%w'", err) return fmt.Errorf("Failed to write to device: '%w'", err)
} }
_, err = d.dev.ReadWithTimeout(buf, time.Duration(time.Second*1)) _, err = d.dev.ReadWithTimeout(buf, time.Duration(time.Second*1))
if err != nil { if err != nil {
fmt.Println("next round")
continue continue
} }
if buf, err = QEncCheckResponse(buf); err != nil { if buf, err = QEncCheckResponse(buf); err != nil {
@ -118,6 +120,16 @@ func (d *QEncDevice) setBufSize() (err error) {
return fmt.Errorf("Unsupported device: '%w'", err) return fmt.Errorf("Unsupported device: '%w'", err)
} }
func QEncNewDevice(path string, bufSize uint8, timeout, retryTimeout, retryWait int) *QEncDevice {
return &QEncDevice{
path: path,
timeout: timeout,
retryTimeout: retryTimeout,
retryWait: retryWait,
bufSize: bufSize,
}
}
func QEncScanDevices(ids ...uint16) (ret []*QEncDevice, err error) { func QEncScanDevices(ids ...uint16) (ret []*QEncDevice, err error) {
vid := uint16(hid.VendorIDAny) vid := uint16(hid.VendorIDAny)
pid := uint16(hid.ProductIDAny) pid := uint16(hid.ProductIDAny)