Skip to content

Instantly share code, notes, and snippets.

@tannewt
Last active July 7, 2023 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tannewt/8e463586cea34e1b5a01e4826059cc4b to your computer and use it in GitHub Desktop.
Save tannewt/8e463586cea34e1b5a01e4826059cc4b to your computer and use it in GitHub Desktop.
HID report parser for kaitai struct
meta:
id: usb_hid
file-extension: usb_hid
bit-endian: le
endian: le
seq:
- id: items
type: item
repeat: eos
enums:
item_type:
0: main
1: global
2: local
types:
item:
seq:
- id: b_size
type: b2
- id: b_type
type: b2
enum: item_type
- id: b_tag
type: b4
- id: data
type:
switch-on: b_tag
cases:
0xff: long_item
_: short_item
long_item:
seq:
- id: b_data_size
type: u1
- id: b_long_item_tag
type: u1
- id: data
size: b_data_size
unknown_short_item:
seq:
- id: data
size: _parent._parent.b_size == 0 ? 0 : (1 << (_parent._parent.b_size - 1))
short_item:
seq:
- id: item
type:
switch-on: _parent.b_type
cases:
'item_type::main': main_item
'item_type::local': local_item
'item_type::global': global_item
_: unknown_short_item
main_item:
seq:
- id: item
type:
switch-on: _parent._parent.b_tag
cases:
0x8: field_item
0x9: field_item
0xB: field_item
0xA: collection_item
0xC: end_collection_item
collection_item:
seq:
- id: collection_type
# type: collection_type
type: u1
- id: subitems
type: item
# repeat: expr
# repeat-expr: 10
repeat: until
repeat-until: _.b_type == item_type::main and _.b_tag == 0xc
end_collection_item:
doc: no data
field_attributes:
seq:
- id: data
type: b1
- type: b7
field_item:
seq:
- id: field
type: field_attributes
#size: _parent._parent._parent.b_size == 0 ? 0 : (1 << (_parent._parent._parent.b_size - 1))
usage_item:
seq:
- id: usage
type: u1
usage_page_item:
seq:
- id: usage
type: u2
local_item:
seq:
- id: item
size: _parent._parent.b_size == 0 ? 0 : (1 << (_parent._parent.b_size - 1))
type:
switch-on: _parent._parent.b_tag
cases:
0x0: usage_item
global_item:
seq:
- id: item
size: _parent._parent.b_size == 0 ? 0 : (1 << (_parent._parent.b_size - 1))
type:
switch-on: _parent._parent.b_tag
cases:
0x0: usage_page_item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment