From 4a1c86d80dd4ec0d7159466a10f53131481555ed Mon Sep 17 00:00:00 2001 From: Matthias Fulz Date: Sun, 8 May 2022 02:18:47 +0200 Subject: [PATCH] New cmds --- qmk-cmd.go | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ qmk-dev.go | 16 ++++++++-- 2 files changed, 105 insertions(+), 2 deletions(-) diff --git a/qmk-cmd.go b/qmk-cmd.go index 9bea114..e2178fd 100644 --- a/qmk-cmd.go +++ b/qmk-cmd.go @@ -22,6 +22,8 @@ const ( QENC_CMD_GET_KEYS QEncCmd = 0x09 QENC_CMD_GET_BUFFSIZE QEncCmd = 0x0A QENC_CMD_SET_KEYS QEncCmd = 0x0B + QENC_CMD_GET_CFG QEncCmd = 0x0C + QENC_CMD_INITIALIZE QEncCmd = 0x0D ) type QEncMode byte @@ -51,6 +53,43 @@ const ( 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) { buf, err := initBuffer(dev, QENC_CMD_RESET) if err != nil { @@ -61,6 +100,36 @@ func QCmdReset(dev *QEncDevice) (err error) { 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) { 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) } +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) { buf, err := initBuffer(dev, QENC_CMD_SET_CFG) if err != nil { diff --git a/qmk-dev.go b/qmk-dev.go index 5f8b6ac..04cda75 100644 --- a/qmk-dev.go +++ b/qmk-dev.go @@ -81,6 +81,10 @@ func (d *QEncDevice) SendBuffer(data []byte) (ret []byte, err error) { } func (d *QEncDevice) setBufSize() (err error) { + if d.bufSize > 0 { + return nil + } + buf_sizes := []int{8, 16, 32, 64, 128} for _, bs := range buf_sizes { @@ -93,14 +97,12 @@ func (d *QEncDevice) setBufSize() (err error) { } sbuf[2] = byte(QENC_CMD_GET_BUFFSIZE) - fmt.Printf("Trying: %d\n", buf_max) if _, err = d.dev.Write(sbuf); err != nil { return fmt.Errorf("Failed to write to device: '%w'", err) } _, err = d.dev.ReadWithTimeout(buf, time.Duration(time.Second*1)) if err != nil { - fmt.Println("next round") continue } if buf, err = QEncCheckResponse(buf); err != nil { @@ -118,6 +120,16 @@ func (d *QEncDevice) setBufSize() (err error) { 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) { vid := uint16(hid.VendorIDAny) pid := uint16(hid.ProductIDAny)