Fix unit tests
[govpp.git] / vendor / github.com / google / gopacket / layers / usb.go
1 // Copyright 2014 Google, Inc. All rights reserved.
2 //
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
5 // tree.
6
7 package layers
8
9 import (
10         "encoding/binary"
11         "github.com/google/gopacket"
12 )
13
14 type USBEventType uint8
15
16 const (
17         USBEventTypeSubmit   USBEventType = 'S'
18         USBEventTypeComplete USBEventType = 'C'
19         USBEventTypeError    USBEventType = 'E'
20 )
21
22 func (a USBEventType) String() string {
23         switch a {
24         case USBEventTypeSubmit:
25                 return "SUBMIT"
26         case USBEventTypeComplete:
27                 return "COMPLETE"
28         case USBEventTypeError:
29                 return "ERROR"
30         default:
31                 return "Unknown event type"
32         }
33 }
34
35 type USBRequestBlockSetupRequest uint8
36
37 const (
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
47 )
48
49 func (a USBRequestBlockSetupRequest) String() string {
50         switch a {
51         case USBRequestBlockSetupRequestGetStatus:
52                 return "GET_STATUS"
53         case USBRequestBlockSetupRequestClearFeature:
54                 return "CLEAR_FEATURE"
55         case USBRequestBlockSetupRequestSetFeature:
56                 return "SET_FEATURE"
57         case USBRequestBlockSetupRequestSetAddress:
58                 return "SET_ADDRESS"
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:
68                 return "SET_IDLE"
69         default:
70                 return "UNKNOWN"
71         }
72 }
73
74 type USBTransportType uint8
75
76 const (
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.
82 )
83
84 func (a USBTransportType) LayerType() gopacket.LayerType {
85         return USBTypeMetadata[a].LayerType
86 }
87
88 func (a USBTransportType) String() string {
89         switch a {
90         case USBTransportTypeTransferIn:
91                 return "Transfer In"
92         case USBTransportTypeIsochronous:
93                 return "Isochronous"
94         case USBTransportTypeInterrupt:
95                 return "Interrupt"
96         case USBTransportTypeControl:
97                 return "Control"
98         case USBTransportTypeBulk:
99                 return "Bulk"
100         default:
101                 return "Unknown transport type"
102         }
103 }
104
105 type USBDirectionType uint8
106
107 const (
108         USBDirectionTypeUnknown USBDirectionType = iota
109         USBDirectionTypeIn
110         USBDirectionTypeOut
111 )
112
113 func (a USBDirectionType) String() string {
114         switch a {
115         case USBDirectionTypeIn:
116                 return "In"
117         case USBDirectionTypeOut:
118                 return "Out"
119         default:
120                 return "Unknown direction type"
121         }
122 }
123
124 // The reference at http://www.beyondlogic.org/usbnutshell/usb1.shtml contains more information about the protocol.
125 type USB struct {
126         BaseLayer
127         ID             uint64
128         EventType      USBEventType
129         TransferType   USBTransportType
130         Direction      USBDirectionType
131         EndpointNumber uint8
132         DeviceAddress  uint8
133         BusID          uint16
134         TimestampSec   int64
135         TimestampUsec  int32
136         Setup          bool
137         Data           bool
138         Status         int32
139         UrbLength      uint32
140         UrbDataLength  uint32
141
142         UrbInterval            uint32
143         UrbStartFrame          uint32
144         UrbCopyOfTransferFlags uint32
145         IsoNumDesc             uint32
146 }
147
148 func (u *USB) LayerType() gopacket.LayerType { return LayerTypeUSB }
149
150 func (m *USB) NextLayerType() gopacket.LayerType {
151         if m.Setup {
152                 return LayerTypeUSBRequestBlockSetup
153         } else if m.Data {
154         }
155
156         return m.TransferType.LayerType()
157 }
158
159 func decodeUSB(data []byte, p gopacket.PacketBuilder) error {
160         d := &USB{}
161
162         return decodingLayerDecoder(d, data, p)
163 }
164
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])
169
170         m.EndpointNumber = data[10] & 0x7f
171         if data[10]&uint8(USBTransportTypeTransferIn) > 0 {
172                 m.Direction = USBDirectionTypeIn
173         } else {
174                 m.Direction = USBDirectionTypeOut
175         }
176
177         m.DeviceAddress = data[11]
178         m.BusID = binary.LittleEndian.Uint16(data[12:14])
179
180         if uint(data[14]) == 0 {
181                 m.Setup = true
182         }
183
184         if uint(data[15]) == 0 {
185                 m.Data = true
186         }
187
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])
193
194         m.Contents = data[:40]
195         m.Payload = data[40:]
196
197         if m.Setup {
198                 m.Payload = data[40:]
199         } else if m.Data {
200                 m.Payload = data[uint32(len(data))-m.UrbDataLength:]
201         }
202
203         // if 64 bit, dissect_linux_usb_pseudo_header_ext
204         if false {
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:]
211         }
212
213         // crc5 or crc16
214         // eop (end of packet)
215
216         return nil
217 }
218
219 type USBRequestBlockSetup struct {
220         BaseLayer
221         RequestType uint8
222         Request     USBRequestBlockSetupRequest
223         Value       uint16
224         Index       uint16
225         Length      uint16
226 }
227
228 func (u *USBRequestBlockSetup) LayerType() gopacket.LayerType { return LayerTypeUSBRequestBlockSetup }
229
230 func (m *USBRequestBlockSetup) NextLayerType() gopacket.LayerType {
231         return gopacket.LayerTypePayload
232 }
233
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]
241         m.Payload = data[8:]
242         return nil
243 }
244
245 func decodeUSBRequestBlockSetup(data []byte, p gopacket.PacketBuilder) error {
246         d := &USBRequestBlockSetup{}
247         return decodingLayerDecoder(d, data, p)
248 }
249
250 type USBControl struct {
251         BaseLayer
252 }
253
254 func (u *USBControl) LayerType() gopacket.LayerType { return LayerTypeUSBControl }
255
256 func (m *USBControl) NextLayerType() gopacket.LayerType {
257         return gopacket.LayerTypePayload
258 }
259
260 func (m *USBControl) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
261         m.Contents = data
262         return nil
263 }
264
265 func decodeUSBControl(data []byte, p gopacket.PacketBuilder) error {
266         d := &USBControl{}
267         return decodingLayerDecoder(d, data, p)
268 }
269
270 type USBInterrupt struct {
271         BaseLayer
272 }
273
274 func (u *USBInterrupt) LayerType() gopacket.LayerType { return LayerTypeUSBInterrupt }
275
276 func (m *USBInterrupt) NextLayerType() gopacket.LayerType {
277         return gopacket.LayerTypePayload
278 }
279
280 func (m *USBInterrupt) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
281         m.Contents = data
282         return nil
283 }
284
285 func decodeUSBInterrupt(data []byte, p gopacket.PacketBuilder) error {
286         d := &USBInterrupt{}
287         return decodingLayerDecoder(d, data, p)
288 }
289
290 type USBBulk struct {
291         BaseLayer
292 }
293
294 func (u *USBBulk) LayerType() gopacket.LayerType { return LayerTypeUSBBulk }
295
296 func (m *USBBulk) NextLayerType() gopacket.LayerType {
297         return gopacket.LayerTypePayload
298 }
299
300 func (m *USBBulk) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
301         m.Contents = data
302         return nil
303 }
304
305 func decodeUSBBulk(data []byte, p gopacket.PacketBuilder) error {
306         d := &USBBulk{}
307         return decodingLayerDecoder(d, data, p)
308 }