11
11
from .device import USBInterface , get_usbdevice
12
12
from .utils import (
13
13
Buffer ,
14
- endpoint_descriptor ,
15
14
split_bmRequestType ,
16
15
STAGE_SETUP ,
17
16
STAGE_DATA ,
23
22
24
23
_DEV_CLASS_MISC = const (0xEF )
25
24
_CS_DESC_TYPE = const (0x24 ) # CS Interface type communication descriptor
26
- _ITF_ASSOCIATION_DESC_TYPE = const (0xB ) # Interface Association descriptor
27
25
28
26
# CDC control interface definitions
29
27
_INTERFACE_CLASS_CDC = const (2 )
62
60
# Other definitions
63
61
_CDC_VERSION = const (0x0120 ) # release number in binary-coded decimal
64
62
63
+ # Number of endpoints in each interface
65
64
_CDC_CONTROL_EP_NUM = const (1 )
66
65
_CDC_DATA_EP_NUM = const (2 )
67
66
@@ -93,7 +92,8 @@ class CDC(io.IOBase):
93
92
# with some additional methods.
94
93
#
95
94
# This is a standalone class, instead of a USBInterface subclass, because
96
- # CDC consists of multiple interfaces (CDC control and CDC data)
95
+ # CDC consists of multiple interfaces (CDC control and CDC data) and also
96
+ # so it can derive from io.IOBase.
97
97
def __init__ (self , ** kwargs ):
98
98
# For CDC to work, the device class must be set to Interface Association
99
99
usb_device = get_usbdevice ()
@@ -218,9 +218,7 @@ class CDCControlInterface(USBInterface):
218
218
# Implements the CDC Control Interface
219
219
220
220
def __init__ (self ):
221
- super ().__init__ (
222
- _INTERFACE_CLASS_CDC , _INTERFACE_SUBCLASS_CDC , _PROTOCOL_NONE , _CDC_CONTROL_EP_NUM
223
- )
221
+ super ().__init__ ()
224
222
225
223
# Callbacks for particular changes initiated by the host
226
224
self .break_cb = None # Host sent a "break" condition
@@ -233,23 +231,12 @@ def __init__(self):
233
231
234
232
self .ep_in = None # Set when enumeration happens
235
233
236
- def descriptor_config_cb (self , desc , itf_num , str_num , ep_num ):
237
- # CDC needs a Interface Association Descriptor (IAD)
238
- # two interfaces in total
239
- desc .pack (
240
- "<BBBBBBBB" ,
241
- 8 ,
242
- _ITF_ASSOCIATION_DESC_TYPE ,
243
- itf_num ,
244
- 2 ,
245
- _INTERFACE_CLASS_CDC ,
246
- _INTERFACE_SUBCLASS_CDC ,
247
- _PROTOCOL_NONE ,
248
- 0 ,
249
- )
234
+ def descriptor_config_cb (self , desc , itf_num , ep_num ):
235
+ # CDC needs a Interface Association Descriptor (IAD) connecting the Control & Data interfaces
236
+ desc .interface_assoc (itf_num , 2 , _INTERFACE_CLASS_CDC , _INTERFACE_SUBCLASS_CDC )
250
237
251
- # Now add the standard interface descriptor
252
- strs = super (). descriptor_config_cb ( desc , itf_num , str_num , ep_num )
238
+ # Now add the Control interface descriptor
239
+ desc . interface ( itf_num , _CDC_CONTROL_EP_NUM , _INTERFACE_CLASS_CDC , _INTERFACE_SUBCLASS_CDC )
253
240
254
241
# Append the CDC class-specific interface descriptor
255
242
# see CDC120-track, p20
@@ -258,8 +245,8 @@ def descriptor_config_cb(self, desc, itf_num, str_num, ep_num):
258
245
5 , # bFunctionLength
259
246
_CS_DESC_TYPE , # bDescriptorType
260
247
_CDC_FUNC_DESC_HEADER , # bDescriptorSubtype
261
- _CDC_VERSION ,
262
- ) # cdc version
248
+ _CDC_VERSION , # cdc version
249
+ )
263
250
264
251
# CDC-PSTN table3 "Call Management"
265
252
# set to No
@@ -269,8 +256,8 @@ def descriptor_config_cb(self, desc, itf_num, str_num, ep_num):
269
256
_CS_DESC_TYPE , # bDescriptorType
270
257
_CDC_FUNC_DESC_CALL_MANAGEMENT , # bDescriptorSubtype
271
258
0 , # bmCapabilities - XXX no call managment so far
272
- 1 ,
273
- ) # bDataInterface - interface 1
259
+ itf_num + 1 , # bDataInterface - interface 1
260
+ )
274
261
275
262
# CDC-PSTN table4 "Abstract Control"
276
263
# set to support line_coding and send_break
@@ -279,8 +266,8 @@ def descriptor_config_cb(self, desc, itf_num, str_num, ep_num):
279
266
4 , # bFunctionLength
280
267
_CS_DESC_TYPE , # bDescriptorType
281
268
_CDC_FUNC_DESC_ABSTRACT_CONTROL , # bDescriptorSubtype
282
- 0x6 ,
283
- ) # bmCapabilities D1, D2
269
+ 0x6 , # bmCapabilities D1, D2
270
+ )
284
271
285
272
# CDC-PSTN "Union"
286
273
# set control interface / data interface number
@@ -290,15 +277,13 @@ def descriptor_config_cb(self, desc, itf_num, str_num, ep_num):
290
277
_CS_DESC_TYPE , # bDescriptorType
291
278
_CDC_FUNC_DESC_UNION , # bDescriptorSubtype
292
279
itf_num , # bControlInterface
293
- itf_num + 1 , # data interface, assume this one will be assigned next
294
- ) # bSubordinateInterface0 (data class itf number)
280
+ itf_num + 1 , # bSubordinateInterface0 ( data class itf number)
281
+ )
295
282
296
283
# Descriptor for a single endpoint
297
284
self .ep_in = ep_num | EP_IN_FLAG
298
285
desc .endpoint (self .ep_in , "interrupt" , 8 , 16 )
299
286
300
- return strs
301
-
302
287
def handle_interface_control_xfer (self , stage , request ):
303
288
# Handle class-specific interface control transfers
304
289
bmRequestType , bRequest , wValue , _ , wLength = struct .unpack ("BBHHH" , request )
@@ -337,26 +322,29 @@ class CDCDataInterface(USBInterface):
337
322
# Implements the CDC Data Interface
338
323
339
324
def __init__ (self ):
340
- super ().__init__ (
341
- _CDC_ITF_DATA_CLASS , _CDC_ITF_DATA_SUBCLASS , _CDC_ITF_DATA_PROT , _CDC_DATA_EP_NUM
342
- )
325
+ super ().__init__ ()
326
+
343
327
self ._wb = () # Optional write Buffer (IN endpoint), set by CDC.init()
344
328
self ._rb = () # Optional read Buffer (OUT endpoint), set by CDC.init()
345
329
self ._timeout = 1000 # set from CDC.init() as well
346
330
347
331
self .ep_in = self .ep_out = None # Set when enumeration happens
348
332
349
- def descriptor_config_cb (self , pack_fn , itf_num , str_num , ep_num ):
333
+ def descriptor_config_cb (self , desc , itf_num , ep_num ):
350
334
# Add the standard interface descriptor
351
- strs = super ().descriptor_config_cb (pack_fn , itf_num , str_num , ep_num )
335
+ desc .interface (
336
+ itf_num ,
337
+ _CDC_DATA_EP_NUM ,
338
+ _CDC_ITF_DATA_CLASS ,
339
+ _CDC_ITF_DATA_SUBCLASS ,
340
+ _CDC_ITF_DATA_PROT ,
341
+ )
352
342
353
- # Two endpoints, bulk IN and OUT
354
- self .ep_in = ep_num | EP_IN_FLAG
343
+ # Two endpoints, bulk OUT and IN
355
344
self .ep_out = ep_num
356
- endpoint_descriptor (pack_fn , self .ep_out , "bulk" , _BULK_EP_LEN , 0 )
357
- endpoint_descriptor (pack_fn , self .ep_in , "bulk" , _BULK_EP_LEN , 0 )
358
-
359
- return strs
345
+ self .ep_in = ep_num | EP_IN_FLAG
346
+ desc .endpoint (self .ep_out , "bulk" , _BULK_EP_LEN , 0 )
347
+ desc .endpoint (self .ep_in , "bulk" , _BULK_EP_LEN , 0 )
360
348
361
349
def write (self , buf ):
362
350
# use a memoryview to track how much of 'buf' we've written so far
0 commit comments