
    wi                        S r Sr/ SQrSSKJr  SSKrSSKrSSKJr  SSK	J
r
  SSKJr  SSKrSSKrSSKrSSKr\R$                  " S5      rSr\" 5       rS rS r  S S	 jrS!S
 jr " S S\5      rS r " S S5      r " S S\5      r  " S S\ 5      r! " S S\"5      r# " S S5      r$ " S S5      r% " S S5      r& " S S\
RN                  5      r(S"S jr)S#S jr*g)$av  usb.core - Core USB features.

This module exports:

Device - a class representing a USB device.
Configuration - a class representing a configuration descriptor.
Interface - a class representing an interface descriptor.
Endpoint - a class representing an endpoint descriptor.
find() - a function to find USB devices.
show_devices() - a function to show the devices present.
zWander Lairson Costa)	DeviceConfiguration	InterfaceEndpointUSBErrorUSBTimeoutErrorNoBackendErrorfindshow_devices    Nzusb.corei  c           	      @    U H  n[        X[        X5      5        M     g N)setattrgetattr)inputoutputfieldsfs       B/home/james-whalen/.local/lib/python3.13/site-packages/usb/core.py	_set_attrr   A   s    75,-     c                 :     [        X5      nU$ !   [        n U$ = fr   )r   	_sentinel)objectnameattrs      r   _try_getattrr   E   s)    v$ KKs    c                     US:X  a  UnU$  Uc  [         R                  " X5      nU$ [         R                  " XU5      n U$ !   Un U$ = f)z<try to get a string, but return a string no matter what
    r   )util
get_string)devindexlangiddefault_str_i0default_access_errorstrings         r   _try_get_stringr&   L   s`     z M	*~4
 M V< M	*)FMs   A A Ac                 6     X   nU$ ! [          a    Un U$ f = f)zJtry to get a string from the lookup table, return "" instead of key
error
)KeyError)tablevaluedefaultr%   s       r   _try_lookupr,   \   s0     M  Ms    c                       \ rS rSrSrS rSrg)_DescriptorInfof   z^this class is used so that when a descriptor is shown on the
terminal it is propely formatted c                     U $ r    selfs    r   __repr___DescriptorInfo.__repr__i   s    r   r1   N)__name__
__module____qualname____firstlineno____doc__r4   __static_attributes__r1   r   r   r.   r.   f   s    )r   r.   c                 F   ^  [         R                  " T 5      U 4S j5       nU$ )Nc                    >  U R                   R                  5         T" U /UQ70 UD6U R                   R                  5         $ ! U R                   R                  5         f = fr   )lockacquirerelease)r3   argskwargsr   s      r   wrappersynchronized.<locals>.wrapperm   sK    	 IIT+D+F+IIDIIs   %A A)	functoolswraps)r   rC   s   ` r   synchronizedrG   l   s%    __Q    Nr   c                       \ rS rSrS r\S 5       r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       r\S 5       r\S 5       r\SS j5       rSrg)_ResourceManagerv   c                     X l         S U l        Xl        S U l        [	        5       U l        0 U l        0 U l        [        R                  " 5       U l
        g r   )backend_active_cfg_indexr    handleset_claimed_intf_intf_setting_ep_info	threadingRLockr>   )r3   r    rL   s      r   __init___ResourceManager.__init__w   sB    !% UOO%	r   c                     U R                   c*  U R                  R                  U R                  5      U l         U R                   $ r   )rN   rL   open_devicer    r2   s    r   managed_open_ResourceManager.managed_open   s0    ;;,,22488<DK{{r   c                 x    U R                   b-  U R                  R                  U R                   5        S U l         g g r   )rN   rL   close_devicer2   s    r   managed_close_ResourceManager.managed_close   s.    ;;"LL%%dkk2DK #r   c                    Uc  US   nOD[        U[        5      (       a  UnO,US:X  a   " S S5      nU" 5       nO[        R                  " XS9nUc  [	        S[        U5      -   5      eU R                  5         U R                  R                  U R                  UR                  5        UR                  U l        U R                  R                  5         U R                  R                  5         g )Nr   c                       \ rS rSrS rSrg)E_ResourceManager.managed_set_configuration.<locals>.MockConfiguration   c                      S U l         SU l        g )Nr   )r!   bConfigurationValuer2   s    r   rU   N_ResourceManager.managed_set_configuration.<locals>.MockConfiguration.__init__   s    !%DJ/0D,r   )rd   r!   N)r6   r7   r8   r9   rU   r;   r1   r   r   MockConfigurationra      s    1r   rf   rd   zInvalid configuration )
isinstancer   r   find_descriptor
ValueErrorstrrY   rL   set_configurationrN   rd   r!   rM   rQ   clearrR   )r3   deviceconfigcfgrf   s        r   managed_set_configuration*_ResourceManager.managed_set_configuration   s    >)C..Cq[1 1 $%C&&vJC;5FCDD&&t{{C4K4KL
 "%  "r   c                    U R                  5         [        U[        5      (       a  UR                  nOUnX0R                  ;  aB  U R
                  R                  U R                  U5        U R                  R                  U5        g g r   )	rY   rh   r   bInterfaceNumberrP   rL   claim_interfacerN   add)r3   rn   intfis       r   managed_claim_interface(_ResourceManager.managed_claim_interface   sh    dI&&%%AA&&&LL((a8""1% 'r   c                 x   Uc!  U R                  U5      nUS   R                  nO$[        U[        5      (       a  UR                  nOUnX@R                  ;   aC   U R
                  R                  U R                  U5        U R                  R                  U5        g g ! U R                  R                  U5        f = f)Nr   r   )	get_active_configurationrt   rh   r   rP   rL   release_interfacerN   remove)r3   rn   rw   rp   rx   s        r   managed_release_interface*_ResourceManager.managed_release_interface   s    <//7CE
++Ai((%%AA"""-..t{{A>""))!,	 # ""))!,s   &B B9c                 2   [        U[        5      (       a  UnO|U R                  U5      nUc  US   R                  nUb  [        R
                  " XRUS9nO[        R
                  " XRS9nUc)  [        S[        U5      -   S-   [        U5      -   S-   5      eU R                  X5        Uc  UR                  nU R                  R                  U R                  UR                  U5        X0R                  UR                  '   U R                  R                  5         g )Nr|   )rt   bAlternateSetting)rt   zNo matching interface (,))rh   r   r}   rt   r   ri   rj   rk   ry   r   rL   set_interface_altsettingrN   rQ   rR   rm   )r3   rn   rw   altrx   rp   s         r   managed_set_interface&_ResourceManager.managed_set_interface   s    dI&&A//7C|5z22((WZ[((Dy !:SY!F!LsSVx!WZ]!]^^$$V/;%%C--dkk1;M;MsS141--.r   c                     [        U[        5      (       a  UR                  nOUnU R                  X5      u  pEU R	                  X5        XE4$ r   )rh   r   bEndpointAddressget_interface_and_endpointry   )r3   rn   endpointendpoint_addressrw   eps         r   setup_request_ResourceManager.setup_request   sK     h))'88'226L$$V2zr   c                 d    U R                   U   $ ! [         a    U R                  U5       Hg  nUR                  U R                  R                  UR                  S5      :w  a  M9  [        R                  " X2S9nUc  MR  X44U R                   U'   X44s  s $    [        S[        U5      -   5      ef = f)Nr   )r   zInvalid endpoint address )rR   r(   r}   r   rQ   getrt   r   ri   rj   hex)r3   rn   r   rw   r   s        r   r   +_ResourceManager.get_interface_and_endpoint   s    	R==!122 		R55f=))T-?-?-C-CDDYDY[\-]]))$R>7;jDMM"238O > 83?O;PPQQ		Rs    A)B/>B/B/c                 
   U R                   ci  U R                  5         [        R                  " UU R                  R                  U R                  5      S9nUc  [        S5      eUR                  U l         U$ XR                      $ )Nrg   zConfiguration not set)	rM   rY   r   ri   rL   get_configurationrN   r   r!   )r3   rn   rp   s      r   r}   )_ResourceManager.get_active_configuration   sv    !!)&&(,(F(Ft{{(SC {677%(YYD"J,,--r   c                     [         R                   " U R                  5      nU H  n U R                  X5        M     g ! [         a     M'  f = fr   )copyrP   r   r   )r3   rn   claimedrx   s       r   release_all_interfaces'_ResourceManager.release_all_interfaces  sI    ))D../A..v9    s   =
A
Ac                     U R                  U5        U(       a  U R                  5         U R                  R                  5         U R                  R                  5         S U l        g r   )r   r]   rQ   rm   rR   rM   )r3   rn   close_handles      r   dispose_ResourceManager.dispose  sI    ##F+   "!%r   )rM   rP   rR   rQ   rL   r    rN   r>   N)T)r6   r7   r8   r9   rU   rG   rY   r]   rq   ry   r   r   r   r   r}   r   r   r;   r1   r   r   rI   rI   v   s    &  
  
  8 
& 
& - -  0 
 
 R R . .   & &r   rI   c                   "    \ rS rSrSrSS jrSrg)r   i   zException class for USB errors.

Backends must raise this exception when USB related errors occur.  The
backend specific error code is available through the 'backend_error_code'
member variable.
Nc                 <    [         R                  XU5        X l        g)zInitialize the object.

This initializes the USBError object. The strerror and errno are passed
to the parent object. The error_code parameter is attributed to the
backend_error_code member variable.
N)IOErrorrU   backend_error_code)r3   strerror
error_codeerrnos       r   rU   USBError.__init__(  s     	h/",r   )r   NN)r6   r7   r8   r9   r:   rU   r;   r1   r   r   r   r      s    	-r   r   c                       \ rS rSrSrSrg)r   i3  zException class for connection timeout errors.

Backends must raise this exception when a call on a USB connection returns
a timeout error code.
r1   Nr6   r7   r8   r9   r:   r;   r1   r   r   r   r   3  s    
 	r   r   c                       \ rS rSrSrSrg)r   i;  z2Exception class when a valid backend is not found.r1   Nr   r1   r   r   r   r   ;  s    9r   r   c                   R    \ rS rSrSr  SS jrS rS rSS jrSS jr	S	 r
S
 rSrg)r   i?  a  Represent an endpoint object.

This class contains all fields of the Endpoint Descriptor according to the
USB Specification. You can access them as class properties. For example, to
access the field bEndpointAddress of the endpoint descriptor, you can do so:

>>> import usb.core
>>> dev = usb.core.find()
>>> for cfg in dev:
>>>     for i in cfg:
>>>         for e in i:
>>>             print e.bEndpointAddress
c                     Xl         X l        UR                  R                  nUR	                  UR                  R
                  UUUU5      n[        UU S5        g)a  Initialize the Endpoint object.

The device parameter is the device object returned by the find()
function. endpoint is the endpoint logical index (not the endpoint
address). The configuration parameter is the logical index of the
configuration (not the bConfigurationValue field). The interface
parameter is the interface logical index (not the bInterfaceNumber
field) and alternate_setting is the alternate setting logical index
(not the bAlternateSetting value). An interface may have only one
alternate setting. In this case, the alternate_setting parameter
should be zero. By "logical index" we mean the relative order of the
configurations returned by the peripheral as a result of GET_DESCRIPTOR
request.
)	bLengthbDescriptorTyper   bmAttributeswMaxPacketSize	bIntervalbRefreshbSynchAddressextra_descriptorsN)rn   r!   _ctxrL   get_endpoint_descriptorr    r   )r3   rn   r   	interfacealternate_settingconfigurationrL   descs           r   rU   Endpoint.__init__N  sY      
++%%..KKOO%! 	
	r   c                 .    SU R                  5       -   S-   $ N<>_strr2   s    r   r4   Endpoint.__repr__{      TYY[ 3&&r   c                 @   SU R                  5       -   S-   n[        R                  " U R                  5      [        R                  :X  a  SnOSnU< SS[        U5      -
  -  < S3SS	U R                  4-  -   S
SU R                  [        [        R                  U R                  5      4-  -   S
SU R                  U4-  -   S
SU R                  [        R                  U R                  S-     4-  -   SSU R                  U R                  4-  -   SSU R                  4-  -   $ )Nz       INOUT=<   
z       %-17s:%#7x (7 bytes)
r   z       %-17s:%#7x %s
r   r   r      z       %-17s:%#7x (%d bytes)
r   z       %-17s:%#7xr   )r   r   endpoint_directionr   ENDPOINT_INlenr   r   r,   _ludescriptorsr   ep_attributesr   r   )r3   headstr	directions      r   __str__Endpoint.__str__~  sJ   TYY[(3.""4#8#89T=M=MMII"C2G+<$=>'4<<+) 	)) 	!!4#7#7COOT-A-AB$D 	DD 	!"D$9$99$F 	FF 	! 1 1!!4#4#4s#:<$> 	>
> 	) $"5"5t7J7J,L 	LL 	{DNN;;< 	<r   Nc                 :    U R                   R                  XU5      $ )a.  Write data to the endpoint.

The parameter data contains the data to be sent to the endpoint and
timeout is the time limit of the operation. The transfer type and
endpoint address are automatically inferred.

The method returns the number of bytes written.

For details, see the Device.write() method.
)rn   write)r3   datatimeouts      r   r   Endpoint.write  s     {{  W55r   c                 :    U R                   R                  XU5      $ )a  Read data from the endpoint.

The parameter size_or_buffer is either the number of bytes to
read or an array object where the data will be put in and timeout is the
time limit of the operation. The transfer type and endpoint address
are automatically inferred.

The method returns either an array object or the number of bytes
actually read.

For details, see the Device.read() method.
)rn   read)r3   size_or_bufferr   s      r   r   Endpoint.read  s     {{g>>r   c                 N    U R                   R                  U R                  5        g)z0Clear the halt/status condition of the endpoint.N)rn   
clear_haltr   r2   s    r   r   Endpoint.clear_halt  s    t445r   c                     [         R                  " U R                  5      [         R                  :X  a  SnOSnSU R                  [        R
                  U R                  S-     U4-  $ )Nr   r   zENDPOINT 0x%X: %s %sr   )r   r   r   r   r   r   r   )r3   r   s     r   r   Endpoint._str  sc    ""4#8#89T=M=MMII #d&;&;t00368& 	r   rn   r!   r   r   r   r   )r6   r7   r8   r9   r:   rU   r4   r   r   r   r   r   r;   r1   r   r   r   r   ?  s3     67;<+Z'<.6?6	r   r   c                   V    \ rS rSrSr  SS jrS rS rS rS r	S r
S	 rS
 rS rSrg)r   i  a  Represent an interface object.

This class contains all fields of the Interface Descriptor
according to the USB Specification. You may access them as class
properties. For example, to access the field bInterfaceNumber
of the interface descriptor, you can do so:

>>> import usb.core
>>> dev = usb.core.find()
>>> for cfg in dev:
>>>     for i in cfg:
>>>         print i.bInterfaceNumber
c                     Xl         X0l        X l        X@l        UR                  R
                  nUR                  U R                   R                  R                  UUU5      n[        UU S5        g)a  Initialize the interface object.

The device parameter is the device object returned by the find()
function. The configuration parameter is the logical index of the
configuration (not the bConfigurationValue field). The interface
parameter is the interface logical index (not the bInterfaceNumber
field) and alternate_setting is the alternate setting logical index
(not the bAlternateSetting value). An interface may have only one
alternate setting. In this case, the alternate_setting parameter
should be zero.  By "logical index" we mean the relative order of
the configurations returned by the peripheral as a result of
GET_DESCRIPTOR request.
)
r   r   rt   r   bNumEndpointsbInterfaceClassbInterfaceSubClassbInterfaceProtocol
iInterfacer   N)	rn   alternate_indexr!   r   r   rL   get_interface_descriptorr    r   )r3   rn   r   r   r   rL   r   s          r   rU   Interface.__init__  sj     0
*++%%//KK$$((%!	 		r   c                 .    SU R                  5       -   S-   $ r   r   r2   s    r   r4   Interface.__repr__  r   r   c                 Z    U R                  5       nU  H  nUS[        U5      -   -  nM     U$ )z'Show all information for the interface.r   _get_full_descriptor_strrk   )r3   r%   r   s      r   r   Interface.__str__   s4     ..0HdS]**F r   c                     [        U 5      $ )z*Return a tuple of the interface endpoints.tupler2   s    r   	endpointsInterface.endpoints      T{r   c                 d    U R                   R                  U R                  U R                  5        g)z$Set the interface alternate setting.N)rn   r   rt   r   r2   s    r   set_altsettingInterface.set_altsetting  s&    ,,!!""	$r   c              #      #    [        U R                  5       H<  n[        U R                  UU R                  U R
                  U R                  5      v   M>     g7f)z,Iterate over all endpoints of the interface.N)ranger   r   rn   r!   r   r   r3   rx   s     r   __iter__Interface.__iter__  sJ     t))*AKKJJ((&&( ( +s   AAc                 p    [        U R                  UU R                  U R                  U R                  5      $ )z1Return the Endpoint object in the given position.)r   rn   r!   r   r   r3   r!   s     r   __getitem__Interface.__getitem__  s2    

$$""$ 	$r   c           	          U R                   (       a  SU R                   -  nOSnSU R                  U[        [        R                  U R
                  SS94-  $ )Nz, %d zINTERFACE %d%s: %szUnknown Class)r+   )r   rt   r,   r   interface_classesr   )r3   alt_settings     r   r   Interface._str%  sW    !! 4#9#99KK#t'<'<k--t/C/C)+', , 	,r   c                 l   SU R                  5       -   S-   nU< SS[        U5      -
  -  < S3SSU R                  4-  -   SS	U R                  [	        [
        R                  U R                  5      4-  -   S
SU R                  4-  -   S
SU R                  4-  -   S
SU R                  4-  -   SSU R                  [	        [
        R                  U R                  5      4-  -   S
SU R                  4-  -   S
SU R                  4-  -   SSU R                  [        U R                   U R                  5      4-  -   $ )Nz    r   r   r   r   z     %-19s:%#7x (9 bytes)
r   z     %-19s:%#7x %s
r   z     %-19s:%#7x
rt   r   r   r   r   r   z     %-19s:%#7x %sr   )r   r   r   r   r,   r   r   rt   r   r   r   r  r   r   r   r&   rn   )r3   r   s     r   r   "Interface._get_full_descriptor_str/  s   499;&,"C2G+<$=>%t||)% 	%% 	t33)=)=>"@ 	@@ 	 5 57 	77 	!7!79 	9	9 	T//1 	11 	t33--t/C/CD"F 	FF 	 $"9"9; 	;;" 	 $"9"9; 	;#;& 	$//DKK9 ; 	;'; 	;r   )r   r   rn   r!   Nr   )r6   r7   r8   r9   r:   rU   r4   r   r   r   r  r	  r   r   r;   r1   r   r   r   r     s=     ,-34,\'$($,;r   r   c                   X    \ rS rSrSrSS jrS rS rS rS r	S r
S	 rS
 rS rS rSrg)r   iI  a~  Represent a configuration object.

This class contains all fields of the Configuration Descriptor according to
the USB Specification. You may access them as class properties.  For
example, to access the field bConfigurationValue of the configuration
descriptor, you can do so:

>>> import usb.core
>>> dev = usb.core.find()
>>> for cfg in dev:
>>>     print cfg.bConfigurationValue
c                     Xl         X l        UR                  R                  nUR	                  U R                   R                  R
                  U5      n[        UU S5        g)ab  Initialize the configuration object.

The device parameter is the device object returned by the find()
function. The configuration parameter is the logical index of the
configuration (not the bConfigurationValue field). By "logical index"
we mean the relative order of the configurations returned by the
peripheral as a result of GET_DESCRIPTOR request.
)	r   r   wTotalLengthbNumInterfacesrd   iConfigurationr   	bMaxPowerr   N)rn   r!   r   rL   get_configuration_descriptorr    r   )r3   rn   r   rL   r   s        r   rU   Configuration.__init__W  sX     "
++%%33  $$
 	
	r   c                 .    SU R                  5       -   S-   $ r   r   r2   s    r   r4   Configuration.__repr__z  r   r   c                 Z    U R                  5       nU  H  nUS[        U5      -  -  nM     U$ )N
%sr   )r3   r%   r   s      r   r   Configuration.__str__}  s2    ..0Ifs9~--F r   c                     [        U 5      $ )z/Return a tuple of the configuration interfaces.r   r2   s    r   
interfacesConfiguration.interfaces  r   r   c                 N    U R                   R                  U R                  5        g)z)Set this configuration as the active one.N)rn   rl   rd   r2   s    r   rO   Configuration.set  s    %%d&>&>?r   c              #      #    [        U R                  5       H/  nSn  [        U R                  XU R                  5      v   US-  nM*     g! [
        [        4 a     MG  f = f7f)z1Iterate over all interfaces of the configuration.r      N)r  r  r   rn   r!   r   
IndexError)r3   rx   r   s      r   r  Configuration.__iter__  sd     t**+AC#DKKDD1HC  , j) s(   A#+A	A#A A#A  A#c                 R    [        U R                  US   US   U R                  5      $ )zReturn the Interface object in the given position.

index is a tuple of two values with interface index and
alternate setting index, respectivally. Example:

>>> interface = config[(0, 0)]
r   r%  )r   rn   r!   r  s     r   r	  Configuration.__getitem__  s%     eAha$**EEr   c                 $   U R                   R                  b>  U R                   R                  S:  a  [        R                  nU$ [        R                  nU$ U R                   R
                  S:  a  [        R                  O[        R                  nU$ )N   i   )rn   speedr   MAX_POWER_UNITS_USB_SUPERSPEEDMAX_POWER_UNITS_USB2p0bcdUSB)r3   power_multipliers     r   _get_power_multiplier#Configuration._get_power_multiplier  s    ;;(EI[[EVEVZ[E[sAA  	 bea{a{   FJ[[EWEW[aEasAAgj  hB  hBr   c                 Z    SU R                   U R                  5       U R                  -  4-  $ )NzCONFIGURATION %d: %d mA)rd   r1  r  r2   s    r   r   Configuration._str  s2    ($$&&(4>>9,; ; 	;r   c                    SU R                  5       -   S-   nU R                  S-  (       a  SnOSnU R                  S-  (       a  SnOSnU< S	S
[        U5      -
  -  < S3SSU R                  4-  -   SSU R                  [        [        R                  U R                  5      4-  -   SSU R                  U R                  4-  -   SSU R                  4-  -   SSU R                  4-  -   SSU R                  [        U R                  U R                  5      4-  -   SSU R                  X#4-  -   SSU R                  U R                  5       U R                  -  4-  -   $ )Nz  r   @   SelfBus    z, Remote Wakeupr  r   r   r   z   %-21s:%#7x (9 bytes)
r   z   %-21s:%#7x %s
r   z   %-21s:%#7x (%d bytes)
r  z   %-21s:%#7x
r  rd   r  z   %-21s:%#7x %s Powered%s
r   z   %-21s:%#7x (%d mA)r  )r   r   r   r   r   r,   r   r   r  r  rd   r  r&   rn   r  r1  )r3   r   poweredremote_wakeups       r   r   &Configuration._get_full_descriptor_str  s   $s*%GG%-MM"C2G+<$=>#t||'% 	%% 	t33)=)=> @ 	@@ 	%D--t/@/@(B 	BB 	d113 	3	3 	!4#;#;= 	== 	d11DKK)<)<= ? 	?? 	'D--w* 	& 	 &&(4>>9#; 	;'; 	;r   r   N)r   )r6   r7   r8   r9   r:   rU   r4   r   r   rO   r  r	  r1  r   r   r;   r1   r   r   r   r   I  s<    !F'@	F ;
!;r   r   c                   6   \ rS rSrSrS rS rS rS rS r	S r
\S	 5       r\S
 5       r\S 5       r\S 5       r\S 5       r\S 5       rS&S jrS rS'S jrS rS rS&S jrS&S jr  S(S jrS rS rS rS rS rS rS r S r!S  r"S! r#S" r$\" \"\!S#S$9r%S%r&g))r   i  a  Device object.

This class contains all fields of the Device Descriptor according to the
USB Specification. You may access them as class properties.  For example,
to access the field bDescriptorType of the device descriptor, you can
do so:

>>> import usb.core
>>> dev = usb.core.find()
>>> dev.bDescriptorType

Additionally, the class provides methods to communicate with the hardware.
Typically, an application will first call the set_configuration() method to
put the device in a known configured state, optionally call the
set_interface_altsetting() to select the alternate setting (if there is
more than one) of the interface used, and call the write() and read()
methods to send and receive data, respectively.

When working in a new hardware, the first try could be like this:

>>> import usb.core
>>> dev = usb.core.find(idVendor=myVendorId, idProduct=myProductId)
>>> dev.set_configuration()
>>> dev.write(1, 'test')

This sample finds the device of interest (myVendorId and myProductId should
be replaced by the corresponding values of your device), then configures
the device (by default, the configuration value is 1, which is a typical
value for most devices) and then writes some data to the endpoint 0x01.

Timeout values for the write, read and ctrl_transfer methods are specified
in milliseconds. If the parameter is omitted, Device.default_timeout value
will be used instead. This property can be set by the user at anytime.
c                     [        XR                  5      (       aG  U R                  U R                  U R                  4UR                  UR                  UR                  4:H  $ [
        $ r   )rh   	__class__rL   busaddressNotImplemented)r3   others     r   __eq__Device.__eq__  sO    e^^,,LL$((DLL9MM599emm<= = "!r   c                 Z    [        U R                  U R                  U R                  45      $ r   )hashrL   r@  rA  r2   s    r   __hash__Device.__hash__  s     T\\488T\\:;;r   c                 .    SU R                  5       -   S-   $ r   r   r2   s    r   r4   Device.__repr__  r   r   c                     U R                  5       n U  H  nUS[        U5      -  -  nM     U$ ! [         a>     U R                  5       nUSUR                  -  -  n U$ ! [         a
    US-  n  U$ f = ff = f)Nr  z" USBError Accessing Configurations)r   rk   r   r}   info)r3   r%   r   s      r   r   Device.__str__  s    ..0	?!%&3}#555 "&   	?? $ = = ?&M$6$677   ?>>?		?s&   . 
A6"AA2-A61A22A6c                     [        U 5      $ )z,Return a tuple of the device configurations.r   r2   s    r   configurationsDevice.configurations  r   r   c                 B   [        X5      U l        [        U l        Su  U l        U l        U l        SU l        UR                  U5      n[        UU S5        UR                  b  [        UR                  5      U l
        OSU l
        UR                  b  [        UR                  5      U l        OSU l        UR                  b  [        UR                  5      U l        OSU l        UR                  b  [        UR                  5      U l        OSU l        SU l        SU l        g)a  Initialize the Device object.

Library users should normally get a Device instance through
the find function. The dev parameter is the identification
of a device to the backend and its meaning is opaque outside
of it. The backend parameter is a instance of a backend
object.
)NNNN)r   r   r/  bDeviceClassbDeviceSubClassbDeviceProtocolbMaxPacketSize0idVendor	idProduct	bcdDeviceiManufactureriProductiSerialNumberbNumConfigurationsrA  r@  port_numberport_numbersr,  )rI   r   _DEFAULT_TIMEOUT_Device__default_timeout_serial_number_product_manufacturer_langidsget_device_descriptorr   r@  intrA  r^  r,  _has_parent_parent)r3   r    rL   r   s       r   rU   Device.__init__  s     %S2	!1AQ>T]D,>,,S1	4 88488}DHDH<<#t||,DLDL'"4#3#34D#D::!TZZDJDJr   c                     U R                   c(   [        R                  " U 5      U l         U R                   $ U R                   $ ! [         a    SU l          U R                   $ f = f)a  Return the USB device's supported language ID codes.

These are 16-bit codes familiar to Windows developers, where for
example instead of en-US you say 0x0409. USB_LANGIDS.pdf on the usb.org
developer site for more info. String requests using a LANGID not
in this array should not be sent to the device.

This property will cause some USB traffic the first time it is accessed
and cache the resulting value for future use.
r1   )re  r   get_langidsr   r2   s    r   langidsDevice.langidsS  s\     == # $ 0 0 6 }}t}}  # "}}#s   A A! A!c                 ~    U R                   c%  [        R                  " X R                  5      U l         U R                   $ )zReturn the USB device's serial number string descriptor.

This property will cause some USB traffic the first time it is accessed
and cache the resulting value for future use.
)rb  r   r   r\  r2   s    r   serial_numberDevice.serial_numberf  s3     &"&//$8J8J"KD"""r   c                 ~    U R                   c%  [        R                  " X R                  5      U l         U R                   $ )zReturn the USB device's product string descriptor.

This property will cause some USB traffic the first time it is accessed
and cache the resulting value for future use.
)rc  r   r   r[  r2   s    r   productDevice.productq  s,     ==  OOD--@DM}}r   c                 H   U R                   c  U R                  R                  R                  U R                  R                  5      nUSLU l         U R                   (       a0  [        XR                  R                  5      U l        U R                  $ SU l        U R                  $ )zReturn the parent device. N)rh  r   rL   
get_parentr    r   ri  )r3   ri  s     r   parentDevice.parent|  s}     #ii''22499==AG&d2D%gyy/@/@A ||  $||r   c                 ~    U R                   c%  [        R                  " X R                  5      U l         U R                   $ )zReturn the USB device's manufacturer string descriptor.

This property will cause some USB traffic the first time it is accessed
and cache the resulting value for future use.
)rd  r   r   rZ  r2   s    r   manufacturerDevice.manufacturer  s3     %!%7I7I!JD!!!r   c                 .    U R                   R                  $ )z,Return the backend being used by the device.)r   rL   r2   s    r   rL   Device.backend  s     yy   r   Nc                 :    U R                   R                  X5        g)ad  Set the active configuration.

The configuration parameter is the bConfigurationValue field of the
configuration you want to set as active. If you call this method
without parameter, it will use the first configuration found.  As a
device hardly ever has more than one configuration, calling the method
without arguments is enough to get the device ready.
N)r   rq   )r3   r   s     r   rl   Device.set_configuration  s     			++D@r   c                 8    U R                   R                  U 5      $ )zJReturn a Configuration object representing the current
configuration set.
)r   r}   r2   s    r   r}   Device.get_active_configuration  s     yy11$77r   c                 <    U R                   R                  XU5        g)a  Set the alternate setting for an interface.

When you want to use an interface and it has more than one alternate
setting, you should call this method to select the appropriate
alternate setting. If you call the method without one or the two
parameters, it will be selected the first one found in the Device in
the same way of the set_configuration method.

Commonly, an interface has only one alternate setting and this call is
not necessary. For most devices, either it has more than one
alternate setting or not, it is not harmful to make a call to this
method with no arguments, as devices will silently ignore the request
when there is only one alternate setting, though the USB Spec allows
devices with no additional alternate setting return an error to the
Host in response to a SET_INTERFACE request.

If you are in doubt, you may want to call it with no arguments wrapped
by a try/except clause:

>>> try:
>>>     dev.set_interface_altsetting()
>>> except usb.core.USBError:
>>>     pass
N)r   r   )r3   r   r   s      r   r   Device.set_interface_altsetting  s    2 			''9JKr   c                     [        U[        5      (       a  UR                  nU R                  R	                  5         U R                  R
                  R                  U R                  R                  U5        g)z3Clear the halt/stall condition for the endpoint ep.N)rh   r   r   r   rY   rL   r   rN   )r3   r   s     r   r   Device.clear_halt  sN    b(##$$B		 		$$TYY%5%5r:r   c                    U R                   R                  5         U R                   R                  U S5        U R                   R                  R	                  U R                   R
                  5        U R                   R                  U S5        g)zReset the device.FTN)r   rY   r   rL   reset_devicerN   r2   s    r   resetDevice.reset  s\    		 		$&		&&tyy'7'78		$%r   c           	         U R                   R                  n[        R                  UR                  [        R
                  UR                  [        R                  UR                  0nU R                   R                  X5      u  pgU[        R                  " UR                  5         nU" U R                   R                  UR                  UR                  [        R                   " U5      U R#                  U5      5      $ )a}  Write data to the endpoint.

This method is used to send data to the device. The endpoint parameter
corresponds to the bEndpointAddress member whose endpoint you want to
communicate with.

The data parameter should be a sequence like type convertible to
the array type (see array module).

The timeout is specified in milliseconds.

The method returns the number of bytes written.
)r   rL   r   ENDPOINT_TYPE_BULK
bulk_writeENDPOINT_TYPE_INTR
intr_writeENDPOINT_TYPE_ISO	iso_writer   endpoint_typer   rN   r   rt   _interopas_array_Device__get_timeout)	r3   r   r   r   rL   fn_maprw   r   fns	            r   r   Device.write  s     ))## ++G,>,>++G,>,>**7+<+< 99**4:D&&r78		  ##%%!!$'""7+ 	r   c           	         U R                   R                  n[        R                  UR                  [        R
                  UR                  [        R                  UR                  0nU R                   R                  X5      u  pgU[        R                  " UR                  5         n[        U[        R                  5      (       a  Un	O[        R                  " U5      n	U" U R                   R                  UR                   UR"                  U	U R%                  U5      5      n
[        U[        R                  5      (       a  U
$ U
['        U	5      U	R(                  -  :w  a  U	SU
 $ U	$ )ah  Read data from the endpoint.

This method is used to receive data from the device. The endpoint
parameter corresponds to the bEndpointAddress member whose endpoint
you want to communicate with. The size_or_buffer parameter either
tells how many bytes you want to read or supplies the buffer to
receive the data (it *must* be an object of the type array).

The timeout is specified in milliseconds.

If the size_or_buffer parameter is the number of bytes to read, the
method returns an array object with the data read. If the
size_or_buffer parameter is an array object, it returns the number
of bytes actually read.
N)r   rL   r   r  	bulk_readr  	intr_readr  iso_readr   r  r   rh   arraycreate_bufferrN   r   rt   r  r   itemsize)r3   r   r   r   rL   r  rw   r   r  buffrets              r   r   Device.read  s     ))## ++G,=,=++G,=,=**7+;+; 99**4:D&&r78nekk22!D%%n5D		  ##%%""7+- nekk22JCI--:Kr   c                 z   [         R                  " U5      nU R                  R                  5         US-  nUS-  n	U[        R
                  :X  a4  U	[        R                  :w  a   US-  n
U R                  R                  X
5        U R                  R                  R                  U R                  R                  UUUUUU R                  U5      5      n[        U[        R                  5      (       d(  [        R                  " U5      [        R                  :X  a  U$ U[!        U5      UR"                  -  :w  a  USU $ U$ )a  Do a control transfer on the endpoint 0.

This method is used to issue a control transfer over the endpoint 0
(endpoint 0 is required to always be a control endpoint).

The parameters bmRequestType, bRequest, wValue and wIndex are the same
of the USB Standard Control Request format.

Control requests may or may not have a data payload to write/read.
In cases which it has, the direction bit of the bmRequestType
field is used to infer the desired request direction. For
host to device requests (OUT), data_or_wLength parameter is
the data payload to send, and it must be a sequence type convertible
to an array object. In this case, the return value is the number
of bytes written in the data payload. For device to host requests
(IN), data_or_wLength is either the wLength parameter of the control
request specifying the number of bytes to read in data payload, and
the return value is an array object with data read, or an array
object which the data will be read to, and the return value is the
number of bytes read.
r   `      N)r  r  r   rY   r   CTRL_RECIPIENT_INTERFACECTRL_TYPE_VENDORry   rL   ctrl_transferrN   r  rh   r  ctrl_directionCTRL_OUTr   r  )r3   bmRequestTypebRequestwValuewIndexdata_or_wLengthr   r  	recipientrqtypeinterface_numberr  s               r   r  Device.ctrl_transfer!  s   .   1		  "A%	&)555d333%}II--dEii--$(II$4$4$1$,$*$*$($($6$6w$?A ou{{33&&}5FJCI--:Kr   c                     U R                   R                  5         U R                   R                  R                  U R                   R                  U5      $ )zDetermine if there is kernel driver associated with the interface.

If a kernel driver is active, the object will be unable to perform
I/O.

The interface parameter is the device interface number to check.
)r   rY   rL   is_kernel_driver_activerN   r3   r   s     r   r  Device.is_kernel_driver_activeV  sA     			 yy  88		   	r   c                     U R                   R                  5         U R                   R                  R                  U R                   R                  U5        g)zDetach a kernel driver.

If successful, you will then be able to perform I/O.

The interface parameter is the device interface number to detach the
driver from.
N)r   rY   rL   detach_kernel_driverrN   r  s     r   r  Device.detach_kernel_driverc  s<     			 		..II	r   c                     U R                   R                  5         U R                   R                  R                  U R                   R                  U5        g)zRe-attach an interface's kernel driver, which was previously
detached using detach_kernel_driver().

The interface parameter is the device interface number to attach the
driver to.
N)r   rY   rL   attach_kernel_driverrN   r  s     r   r  Device.attach_kernel_driverp  s<     			 		..II	r   c              #   ^   #    [        U R                  5       H  n[        X5      v   M     g7f)z.Iterate over all configurations of the device.N)r  r]  r   r  s     r   r  Device.__iter__|  s%     t../A(( 0s   +-c                     [        X5      $ )z6Return the Configuration object in the given position.)r   r  s     r   r	  Device.__getitem__  s    T))r   c                 ^    [        U S5      (       a  U R                  R                  U 5        g g )Nr   )hasattrr   r   r2   s    r   _finalize_objectDevice._finalize_object  s&    4  IId# !r   c                 $    Ub  U$ U R                   $ r   ra  )r3   r   s     r   __get_timeoutDevice.__get_timeout  s    N%%%r   c                 2    US:  a  [        S5      eXl        g )Nr   z"Timeout cannot be a negative value)rj   ra  )r3   tmos     r   __set_def_tmoDevice.__set_def_tmo  s    7ABB!$r   c                     U R                   $ r   r  r2   s    r   __get_def_tmoDevice.__get_def_tmo  s    %%%r   c                 d    SU R                   U R                  U R                  U R                  4-  $ )Nz,DEVICE ID %04x:%04x on Bus %03d Address %03d)rW  rX  r@  rA  r2   s    r   r   Device._str  s1    =MM4>>488T\\AC C 	Cr   c                    U R                  5       S-   nU R                  S-  (       a  [        U R                  S-  5      nOSnU R                  S-  (       a  [        U R                  S-  5      nOSnU< SS[	        U5      -
  -  < S3SSU R
                  4-  -   S	S
U R                  [        [        R                  U R                  5      4-  -   SSU R                  U R                  S-  S-	  U R                  S-  S-	  U4-  -   S	SU R                  [        [        R                  U R                  5      4-  -   SSU R                  4-  -   SSU R                  4-  -   SSU R                  U R                  4-  -   SSU R                  4-  -   SSU R                   4-  -   SSU R                  U R                  S-  S-	  U R                  S-  S-	  U4-  -   S	SU R"                  [%        X R"                  5      4-  -   S	SU R&                  [%        X R&                  5      4-  -   S	SU R(                  [%        X R(                  5      4-  -   SS U R*                  4-  -   $ )!Nr      r  r   r   r   z %-23s:%#7x (18 bytes)
r   z %-23s:%#7x %s
r   z %-23s:%#7x USB %d.%d%s
r/  i         r+  rS  z %-23s:%#7x
rT  rU  z %-23s:%#7x (%d bytes)
rV  z %-23s: %#06x
rW  rX  z %-23s:%#7x Device %d.%d%s
rY  rZ  r[  r\  z %-23s:%#7xr]  )r   r/  rk   rY  r   r   r   r,   r   r   rS  device_classesrT  rU  rV  rW  rX  rZ  r&   r[  r\  r]  )r3   r   low_bcd_usblow_bcd_devices       r   r   Device._get_full_descriptor_str  s   ))+#;;dkkC/0KK>>C #!56NN#SBW,=%>?"t||&% 	%% 	t33)=)=>@ 	@@ 	$dkkDKK&$81#<[[4A%{'4 	44 	D--**D,=,=>@ 	@@ 	t335 	55 	t335 	55  	#t33T5I5I&K 	K!K$ 	' 	'%'( 	) 	))), 	'$..6*AA)E^^d"q(.*: 	:-:2 	T//D"4"457 	7378 	D--02 	292> 	T//D"4"457 	7?!7D 	 $"9"9; 	;E#; #	;r   z*Default timeout for transfer I/O functions)doc)__default_timeoutr   rh  re  rd  ri  rc  rb  rA  r@  r^  r,  r   r   )r   r   NN)'r6   r7   r8   r9   r:   rD  rH  r4   r   rP  rU   propertyrm  rp  rs  rw  rz  rL   rl   r}   r   r   r  r   r   r  r  r  r  r  r	  r  r  _Device__set_def_tmo_Device__get_def_tmor   r   default_timeoutr;   r1   r   r   r   r     s$   !F"<'?B  $ # #   	 	 " " ! !	A8L6;&B,\ GH.23j
)
*$&
%
&C0;d %%JOr   r   c                 H  ^^ UU4S jnTcf  SSK Js  Jn  SSKJs  Jn  SSKJs  Jn  XWU4 H8  nUR                  5       mTc  M  [        R                  SUR                  5          O   [        S5      eU (       a  U" S0 UD6$  [        U" S0 UD65      $ ! [         a     gf = f)aR	  Find an USB device and return it.

find() is the function used to discover USB devices.  You can pass as
arguments any combination of the USB Device Descriptor fields to match a
device. For example:

find(idVendor=0x3f4, idProduct=0x2009)

will return the Device object for the device with idVendor field equals
to 0x3f4 and idProduct equals to 0x2009.

If there is more than one device which matchs the criteria, the first one
found will be returned. If a matching device cannot be found the function
returns None. If you want to get all devices, you can set the parameter
find_all to True, then find will return an iterator with all matched devices.
If no matching device is found, it will return an empty iterator. Example:

for printer in find(find_all=True, bDeviceClass=7):
    print (printer)

This call will get all the USB printers connected to the system.  (actually
may be not, because some devices put their class information in the
Interface Descriptor).

You can also use a customized match criteria:

dev = find(custom_match = lambda d: d.idProduct==0x3f4 and d.idVendor==0x2009)

A more accurate printer finder using a customized match would be like
so:

def is_printer(dev):
    import usb.util
    if dev.bDeviceClass == 7:
        return True
    for cfg in dev:
        if usb.util.find_descriptor(cfg, bInterfaceClass=7) is not None:
            return True

for printer in find(find_all=True, custom_match = is_printer):
    print (printer)

Now even if the device class code is in the interface descriptor the
printer will be found.

You can combine a customized match with device descriptor fields. In this
case, the fields must match and the custom_match must return True. In the
our previous example, if we would like to get all printers belonging to the
manufacturer 0x3f4, the code would be like so:

printers = list(find(find_all=True, idVendor=0x3f4, custom_match=is_printer))

If you want to use find as a 'list all devices' function, just call
it with find_all = True:

devices = list(find(find_all=True))

Finally, you can pass a custom backend to the find function:

find(backend = MyBackend())

PyUSB has builtin backends for libusb 0.1, libusb 1.0 and OpenUSB.  If you
do not supply a backend explicitly, find() function will select one of the
predefineds backends according to system availability.

Backends are explained in the usb.backend module.
c               ;      >^#    TR                  5        HQ  n[        UT5      mU4S jU R                  5        5       n[        U5      (       d  M;  Tb  T" T5      (       d  MM  Tv   MS     g 7f)Nc              3   F   >#    U  H  u  pU[        TU5      :H  v   M     g 7fr   )r   ).0keyvalds      r   	<genexpr>,find.<locals>.device_iter.<locals>.<genexpr>  s     PXSSLC00s   !)enumerate_devicesr   itemsall)rB   r    testsr  rL   custom_matchs      @r   device_iterfind.<locals>.device_iter  sP     ,,.CsG$APPE5zz|3|A	 /s   AA+A+"	A+Nr   zfind(): using backend "%s"zNo backend availabler1   )usb.backend.libusb1rL   libusb1usb.backend.libusb0libusb0usb.backend.openusbopenusbget_backend_loggerrM  r6   r   nextStopIteration)	find_allrL   r  rA   r  r  r  r  ms	    ``      r   r	   r	     s    H ------G,AmmoG"91::F	 - !!788"T""	+d+,, 		s   B 
B! B!c                     SUS'   [        S0 UD6nSnU HY  nU (       d>  X4R                  5       < S[        [        R                  UR
                  5      < S3-  nMH  US[        U5      -  -  nM[     [        U5      $ )zShow information about connected devices.

The verbose flag sets to verbose or not.
**kwargs are passed directly to the find() function.
Tr  r  z, r   z%s

r1   )r	   r   r,   r   r  rS  rk   r.   )verboserB   devicesstringsrn   s        r   r
   r
   3  sz     F:nVnGGkkm[""F$7$769 : :G x#f+--G  7##r   )Nr  zError Accessing String)r  )FNN)F)+r:   
__author____all__usb.utilr   r   operatorusb._interopr  usb._objfinalizer_objfinalizerusb._lookup_lookupr   loggingr  rS   rE   	getLoggerr  r`  r   r   r   r   r&   r,   rk   r.   rG   rI   r   r   r   rj   r   r   r   r   AutoFinalizedObjectr   r	   r
   r1   r   r   <module>r     s   >
 $
J     )     


J
' H	. AC7 c g& g&T-w -&	h 		Z 	 BF; F;RE; E;N@].. @F^@$r   