2022-02-07 02:20:56 +01:00
|
|
|
package qmkenc
|
|
|
|
|
|
|
|
type QEncErr byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
QENC_ERR_OK QEncErr = 0x00
|
|
|
|
QENC_ERR_EMPTY_BUF QEncErr = 0x01
|
|
|
|
QENC_ERR_NO_CTX QEncErr = 0x02
|
|
|
|
QENC_ERR_NOT_ALLOWED QEncErr = 0x03
|
|
|
|
QENC_ERR_INVALID QEncErr = 0x04
|
|
|
|
QENC_ERR_RETRY QEncErr = 0x05
|
2022-04-10 02:15:25 +02:00
|
|
|
QENC_ERR_HW_SUPPORT QEncErr = 0x06
|
2022-06-26 23:58:14 +02:00
|
|
|
QENC_ERR_MORE_DATA QEncErr = 0x07
|
2022-02-07 02:20:56 +01:00
|
|
|
// device retry timeout
|
|
|
|
QENC_ERR_TIMEOUT QEncErr = 0xFF
|
|
|
|
)
|
|
|
|
|
|
|
|
type QEncError struct {
|
|
|
|
errCode QEncErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *QEncError) Error() string {
|
|
|
|
switch e.errCode {
|
|
|
|
case QENC_ERR_EMPTY_BUF:
|
|
|
|
return "Buffer is empty"
|
|
|
|
case QENC_ERR_NO_CTX:
|
|
|
|
return "No context"
|
|
|
|
case QENC_ERR_NOT_ALLOWED:
|
|
|
|
return "Action not allowed"
|
|
|
|
case QENC_ERR_INVALID:
|
|
|
|
return "Invalid data"
|
|
|
|
case QENC_ERR_RETRY:
|
|
|
|
return "Retry request"
|
2022-04-10 02:15:25 +02:00
|
|
|
case QENC_ERR_HW_SUPPORT:
|
|
|
|
return "Not supported by hardware"
|
2022-06-26 23:58:14 +02:00
|
|
|
case QENC_ERR_MORE_DATA:
|
|
|
|
return "More data available"
|
2022-02-07 02:20:56 +01:00
|
|
|
case QENC_ERR_TIMEOUT:
|
|
|
|
return "Timeout reached"
|
|
|
|
default:
|
|
|
|
return "Unknown error"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func QEncCheckResponse(data []byte) (ret []byte, err error) {
|
|
|
|
if data[0] == byte(QENC_ERR_OK) {
|
|
|
|
return data[1:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rerr := new(QEncError)
|
|
|
|
rerr.errCode = QEncErr(data[0])
|
|
|
|
|
2022-06-26 23:58:14 +02:00
|
|
|
if data[0] == byte(QENC_ERR_RETRY) {
|
|
|
|
ret = data[1:]
|
|
|
|
} else {
|
|
|
|
ret = data
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:20:56 +01:00
|
|
|
return ret, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToQEncError(err error) *QEncError {
|
|
|
|
if _, ok := err.(*QEncError); ok {
|
|
|
|
return err.(*QEncError)
|
|
|
|
} else {
|
|
|
|
qerr := new(QEncError)
|
|
|
|
qerr.errCode = QENC_ERR_INVALID
|
|
|
|
return qerr
|
|
|
|
}
|
|
|
|
}
|