1 // Copyright 2014 Google, Inc. All rights reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
11 "github.com/google/gopacket"
14 type USBEventType uint8
17 USBEventTypeSubmit USBEventType = 'S'
18 USBEventTypeComplete USBEventType = 'C'
19 USBEventTypeError USBEventType = 'E'
22 func (a USBEventType) String() string {
24 case USBEventTypeSubmit:
26 case USBEventTypeComplete:
28 case USBEventTypeError:
31 return "Unknown event type"
35 type USBRequestBlockSetupRequest uint8
38 USBRequestBlockSetupRequestGetStatus USBRequestBlockSetupRequest = 0x00
39 USBRequestBlockSetupRequestClearFeature USBRequestBlockSetupRequest = 0x01
40 USBRequestBlockSetupRequestSetFeature USBRequestBlockSetupRequest = 0x03
41 USBRequestBlockSetupRequestSetAddress USBRequestBlockSetupRequest = 0x05
42 USBRequestBlockSetupRequestGetDescriptor USBRequestBlockSetupRequest = 0x06
43 USBRequestBlockSetupRequestSetDescriptor USBRequestBlockSetupRequest = 0x07
44 USBRequestBlockSetupRequestGetConfiguration USBRequestBlockSetupRequest = 0x08
45 USBRequestBlockSetupRequestSetConfiguration USBRequestBlockSetupRequest = 0x09
46 USBRequestBlockSetupRequestSetIdle USBRequestBlockSetupRequest = 0x0a
49 func (a USBRequestBlockSetupRequest) String() string {
51 case USBRequestBlockSetupRequestGetStatus:
53 case USBRequestBlockSetupRequestClearFeature:
54 return "CLEAR_FEATURE"
55 case USBRequestBlockSetupRequestSetFeature:
57 case USBRequestBlockSetupRequestSetAddress:
59 case USBRequestBlockSetupRequestGetDescriptor:
60 return "GET_DESCRIPTOR"
61 case USBRequestBlockSetupRequestSetDescriptor:
62 return "SET_DESCRIPTOR"
63 case USBRequestBlockSetupRequestGetConfiguration:
64 return "GET_CONFIGURATION"
65 case USBRequestBlockSetupRequestSetConfiguration:
66 return "SET_CONFIGURATION"
67 case USBRequestBlockSetupRequestSetIdle:
74 type USBTransportType uint8
77 USBTransportTypeTransferIn USBTransportType = 0x80 // Indicates send or receive
78 USBTransportTypeIsochronous USBTransportType = 0x00 // Isochronous transfers occur continuously and periodically. They typically contain time sensitive information, such as an audio or video stream.
79 USBTransportTypeInterrupt USBTransportType = 0x01 // Interrupt transfers are typically non-periodic, small device "initiated" communication requiring bounded latency, such as pointing devices or keyboards.
80 USBTransportTypeControl USBTransportType = 0x02 // Control transfers are typically used for command and status operations.
81 USBTransportTypeBulk USBTransportType = 0x03 // Bulk transfers can be used for large bursty data, using all remaining available bandwidth, no guarantees on bandwidth or latency, such as file transfers.
84 func (a USBTransportType) LayerType() gopacket.LayerType {
85 return USBTypeMetadata[a].LayerType
88 func (a USBTransportType) String() string {
90 case USBTransportTypeTransferIn:
92 case USBTransportTypeIsochronous:
94 case USBTransportTypeInterrupt:
96 case USBTransportTypeControl:
98 case USBTransportTypeBulk:
101 return "Unknown transport type"
105 type USBDirectionType uint8
108 USBDirectionTypeUnknown USBDirectionType = iota
113 func (a USBDirectionType) String() string {
115 case USBDirectionTypeIn:
117 case USBDirectionTypeOut:
120 return "Unknown direction type"
124 // The reference at http://www.beyondlogic.org/usbnutshell/usb1.shtml contains more information about the protocol.
128 EventType USBEventType
129 TransferType USBTransportType
130 Direction USBDirectionType
144 UrbCopyOfTransferFlags uint32
148 func (u *USB) LayerType() gopacket.LayerType { return LayerTypeUSB }
150 func (m *USB) NextLayerType() gopacket.LayerType {
152 return LayerTypeUSBRequestBlockSetup
156 return m.TransferType.LayerType()
159 func decodeUSB(data []byte, p gopacket.PacketBuilder) error {
162 return decodingLayerDecoder(d, data, p)
165 func (m *USB) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
166 m.ID = binary.LittleEndian.Uint64(data[0:8])
167 m.EventType = USBEventType(data[8])
168 m.TransferType = USBTransportType(data[9])
170 m.EndpointNumber = data[10] & 0x7f
171 if data[10]&uint8(USBTransportTypeTransferIn) > 0 {
172 m.Direction = USBDirectionTypeIn
174 m.Direction = USBDirectionTypeOut
177 m.DeviceAddress = data[11]
178 m.BusID = binary.LittleEndian.Uint16(data[12:14])
180 if uint(data[14]) == 0 {
184 if uint(data[15]) == 0 {
188 m.TimestampSec = int64(binary.LittleEndian.Uint64(data[16:24]))
189 m.TimestampUsec = int32(binary.LittleEndian.Uint32(data[24:28]))
190 m.Status = int32(binary.LittleEndian.Uint32(data[28:32]))
191 m.UrbLength = binary.LittleEndian.Uint32(data[32:36])
192 m.UrbDataLength = binary.LittleEndian.Uint32(data[36:40])
194 m.Contents = data[:40]
195 m.Payload = data[40:]
198 m.Payload = data[40:]
200 m.Payload = data[uint32(len(data))-m.UrbDataLength:]
203 // if 64 bit, dissect_linux_usb_pseudo_header_ext
205 m.UrbInterval = binary.LittleEndian.Uint32(data[40:44])
206 m.UrbStartFrame = binary.LittleEndian.Uint32(data[44:48])
207 m.UrbDataLength = binary.LittleEndian.Uint32(data[48:52])
208 m.IsoNumDesc = binary.LittleEndian.Uint32(data[52:56])
209 m.Contents = data[:56]
210 m.Payload = data[56:]
214 // eop (end of packet)
219 type USBRequestBlockSetup struct {
222 Request USBRequestBlockSetupRequest
228 func (u *USBRequestBlockSetup) LayerType() gopacket.LayerType { return LayerTypeUSBRequestBlockSetup }
230 func (m *USBRequestBlockSetup) NextLayerType() gopacket.LayerType {
231 return gopacket.LayerTypePayload
234 func (m *USBRequestBlockSetup) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
235 m.RequestType = data[0]
236 m.Request = USBRequestBlockSetupRequest(data[1])
237 m.Value = binary.LittleEndian.Uint16(data[2:4])
238 m.Index = binary.LittleEndian.Uint16(data[4:6])
239 m.Length = binary.LittleEndian.Uint16(data[6:8])
240 m.Contents = data[:8]
245 func decodeUSBRequestBlockSetup(data []byte, p gopacket.PacketBuilder) error {
246 d := &USBRequestBlockSetup{}
247 return decodingLayerDecoder(d, data, p)
250 type USBControl struct {
254 func (u *USBControl) LayerType() gopacket.LayerType { return LayerTypeUSBControl }
256 func (m *USBControl) NextLayerType() gopacket.LayerType {
257 return gopacket.LayerTypePayload
260 func (m *USBControl) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
265 func decodeUSBControl(data []byte, p gopacket.PacketBuilder) error {
267 return decodingLayerDecoder(d, data, p)
270 type USBInterrupt struct {
274 func (u *USBInterrupt) LayerType() gopacket.LayerType { return LayerTypeUSBInterrupt }
276 func (m *USBInterrupt) NextLayerType() gopacket.LayerType {
277 return gopacket.LayerTypePayload
280 func (m *USBInterrupt) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
285 func decodeUSBInterrupt(data []byte, p gopacket.PacketBuilder) error {
287 return decodingLayerDecoder(d, data, p)
290 type USBBulk struct {
294 func (u *USBBulk) LayerType() gopacket.LayerType { return LayerTypeUSBBulk }
296 func (m *USBBulk) NextLayerType() gopacket.LayerType {
297 return gopacket.LayerTypePayload
300 func (m *USBBulk) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
305 func decodeUSBBulk(data []byte, p gopacket.PacketBuilder) error {
307 return decodingLayerDecoder(d, data, p)