mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-12 23:13:40 +00:00
machine/usb/descriptor: Fix encoding of values
The limit for positive values is incorrect and leads to an overflow (e.g. 0xFF and 0xFFFF become -1)
This commit is contained in:
@@ -103,7 +103,7 @@ var classHID = [ClassHIDTypeLen]byte{
|
|||||||
0x00, // CountryCode
|
0x00, // CountryCode
|
||||||
0x01, // NumDescriptors
|
0x01, // NumDescriptors
|
||||||
0x22, // ClassType
|
0x22, // ClassType
|
||||||
0x90, // ClassLength L
|
0x91, // ClassLength L
|
||||||
0x00, // ClassLength H
|
0x00, // ClassLength H
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ var CDCHID = Descriptor{
|
|||||||
EndpointEP5OUT.Bytes(),
|
EndpointEP5OUT.Bytes(),
|
||||||
}),
|
}),
|
||||||
HID: map[uint16][]byte{
|
HID: map[uint16][]byte{
|
||||||
2: Append([][]byte{
|
2: Append([][]byte{ // Update ClassLength in classHID whenever the array length is modified!
|
||||||
HIDUsagePageGenericDesktop,
|
HIDUsagePageGenericDesktop,
|
||||||
HIDUsageDesktopKeyboard,
|
HIDUsageDesktopKeyboard,
|
||||||
HIDCollectionApplication,
|
HIDCollectionApplication,
|
||||||
@@ -210,6 +210,7 @@ var CDCHID = Descriptor{
|
|||||||
HIDReportSize(16),
|
HIDReportSize(16),
|
||||||
HIDReportCount(1),
|
HIDReportCount(1),
|
||||||
HIDInputDataAryAbs,
|
HIDInputDataAryAbs,
|
||||||
HIDCollectionEnd}),
|
HIDCollectionEnd,
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package descriptor
|
package descriptor
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
hidUsagePage = 0x05
|
hidUsagePage = 0x05
|
||||||
hidUsage = 0x09
|
hidUsage = 0x09
|
||||||
@@ -130,6 +132,28 @@ var (
|
|||||||
HIDOutputConstVarAbs = []byte{hidOutput, 0x03}
|
HIDOutputConstVarAbs = []byte{hidOutput, 0x03}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func hidShortItem(tag byte, value uint32) []byte {
|
||||||
|
switch {
|
||||||
|
case value <= math.MaxUint8:
|
||||||
|
return []byte{tag | hidSizeValue1, byte(value)}
|
||||||
|
case value <= math.MaxUint16:
|
||||||
|
return []byte{tag | hidSizeValue2, byte(value), byte(value >> 8)}
|
||||||
|
default:
|
||||||
|
return []byte{tag | hidSizeValue4, byte(value), byte(value >> 8), byte(value >> 16), byte(value >> 24)}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func hidShortItemSigned(tag byte, value int32) []byte {
|
||||||
|
switch {
|
||||||
|
case math.MinInt8 <= value && value <= math.MaxInt8:
|
||||||
|
return []byte{tag | hidSizeValue1, byte(value)}
|
||||||
|
case math.MinInt16 <= value && value <= math.MaxInt16:
|
||||||
|
return []byte{tag | hidSizeValue2, byte(value), byte(value >> 8)}
|
||||||
|
default:
|
||||||
|
return []byte{tag | hidSizeValue4, byte(value), byte(value >> 8), byte(value >> 16), byte(value >> 24)}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func HIDReportSize(size int) []byte {
|
func HIDReportSize(size int) []byte {
|
||||||
return []byte{hidReportSize, byte(size)}
|
return []byte{hidReportSize, byte(size)}
|
||||||
}
|
}
|
||||||
@@ -143,69 +167,27 @@ func HIDReportID(id int) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func HIDLogicalMinimum(min int) []byte {
|
func HIDLogicalMinimum(min int) []byte {
|
||||||
switch {
|
return hidShortItemSigned(hidLogicalMinimum, int32(min))
|
||||||
case min < -32767 || 65535 < min:
|
|
||||||
return []byte{hidLogicalMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)}
|
|
||||||
case min < -127 || 255 < min:
|
|
||||||
return []byte{hidLogicalMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)}
|
|
||||||
default:
|
|
||||||
return []byte{hidLogicalMinimum + hidSizeValue1, byte(min)}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HIDLogicalMaximum(max int) []byte {
|
func HIDLogicalMaximum(max int) []byte {
|
||||||
switch {
|
return hidShortItemSigned(hidLogicalMaximum, int32(max))
|
||||||
case max < -32767 || 65535 < max:
|
|
||||||
return []byte{hidLogicalMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)}
|
|
||||||
case max < -127 || 255 < max:
|
|
||||||
return []byte{hidLogicalMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)}
|
|
||||||
default:
|
|
||||||
return []byte{hidLogicalMaximum + hidSizeValue1, byte(max)}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HIDUsageMinimum(min int) []byte {
|
func HIDUsageMinimum(min int) []byte {
|
||||||
switch {
|
return hidShortItem(hidUsageMinimum, uint32(min))
|
||||||
case min < -32767 || 65535 < min:
|
|
||||||
return []byte{hidUsageMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)}
|
|
||||||
case min < -127 || 255 < min:
|
|
||||||
return []byte{hidUsageMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)}
|
|
||||||
default:
|
|
||||||
return []byte{hidUsageMinimum + hidSizeValue1, byte(min)}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HIDUsageMaximum(max int) []byte {
|
func HIDUsageMaximum(max int) []byte {
|
||||||
switch {
|
return hidShortItem(hidUsageMaximum, uint32(max))
|
||||||
case max < -32767 || 65535 < max:
|
|
||||||
return []byte{hidUsageMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)}
|
|
||||||
case max < -127 || 255 < max:
|
|
||||||
return []byte{hidUsageMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)}
|
|
||||||
default:
|
|
||||||
return []byte{hidUsageMaximum + hidSizeValue1, byte(max)}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HIDPhysicalMinimum(min int) []byte {
|
func HIDPhysicalMinimum(min int) []byte {
|
||||||
switch {
|
return hidShortItemSigned(hidPhysicalMinimum, int32(min))
|
||||||
case min < -32767 || 65535 < min:
|
|
||||||
return []byte{hidPhysicalMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)}
|
|
||||||
case min < -127 || 255 < min:
|
|
||||||
return []byte{hidPhysicalMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)}
|
|
||||||
default:
|
|
||||||
return []byte{hidPhysicalMinimum + hidSizeValue1, byte(min)}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HIDPhysicalMaximum(max int) []byte {
|
func HIDPhysicalMaximum(max int) []byte {
|
||||||
switch {
|
return hidShortItemSigned(hidPhysicalMaximum, int32(max))
|
||||||
case max < -32767 || 65535 < max:
|
|
||||||
return []byte{hidPhysicalMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)}
|
|
||||||
case max < -127 || 255 < max:
|
|
||||||
return []byte{hidPhysicalMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)}
|
|
||||||
default:
|
|
||||||
return []byte{hidPhysicalMaximum + hidSizeValue1, byte(max)}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HIDUnitExponent(exp int) []byte {
|
func HIDUnitExponent(exp int) []byte {
|
||||||
|
|||||||
Reference in New Issue
Block a user