
    .                         S 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r	Sr
SrS	rS
rSrSrSrSrS rS r " S S\5      rS rS r " S S\R0                  5      rg)zCImplements raw HID interface on Linux using SysFS and device files.    )divisionN)errors)base                  t         c                     [        U 5      n X   nU[        :X  a1  US-   [        U 5      :  a	  SXS-      4$ [        R                  " S5      eUS-  nUS::  a  SU4$ US:X  a  g[        R                  " S5      e)a  Get value length for a key in rd.

For a key at position pos in the Report Descriptor rd, return the length
of the associated value.  This supports both short and long format
values.

Args:
  rd: Report Descriptor
  pos: The position of the key in rd.

Returns:
  (key_size, data_len) where key_size is the number of bytes occupied by
  the key and data_len is the length of the value associated by the key.
      zMalformed report descriptor   )r   r   zCannot happen)	bytearrayLONG_ITEM_ENCODINGlenr   HidError)rdposkeycodes       *platform/bq/third_party/pyu2f/hid/linux.pyGetValueLengthr   $   s     }"
# QwR!GOO9:: :Dt|Y	((    c                     SnUS:X  a  SnO(US:X  a  SnOUS:X  a  SnO[         R                  " S5      e[        R                  " X0XU-    5      u  nU$ )	zFReads value_size bytes from rd at offset, least signifcant byte first.Nr   z<Br   z<Hr   z<LzInvalid value size specified)r   r   structunpack)r   offset
value_sizeencodingrets        r   ReadLsbBytesr%   L   s\     (1_HQHQH
//8
99	xFJ+>!?	@$#	*r   c                       \ rS rSrSrg)NoReportCountFound]    N)__name__
__module____qualname____firstlineno____static_attributes__r)   r   r   r'   r'   ]   s    r   r'   c                    [        U 5      n SnSnSnSnSnU[        U 5      :  Ga  X   n[        X5      u  pU[        -  [        :X  a6  U(       a-  U(       a&  X4-  S-  n
[        UR                  U
5      Ul        SnSnGOJU[        -  [        :X  a6  U(       a-  U(       a&  X4-  S-  n
[        UR                  U
5      Ul        SnSnGOU[        -  [        :X  a  U(       a  XQl
        U(       a  Xal        OU[        -  [        :X  a%  [        U 5      US-   U	-   :  a  [        XS-   U	5      nOU[        -  [        :X  a%  [        U 5      US-   U	-   :  a  [        XS-   U	5      nOkU[        -  [        :X  a%  [        U 5      US-   U	-   :  a  [        XS-   U	5      nO5U[        -  [         :X  a$  [        U 5      US-   U	-   :  a  [        XS-   U	5      nX)U-   -  nU[        U 5      :  a  GM  U$ )a  Parse the binary report descriptor.

Parse the binary report descriptor into a DeviceDescriptor object.

Args:
  rd: The binary report descriptor
  desc: The DeviceDescriptor object to update with the results
      from parsing the descriptor.

Returns:
  None
r   Nr   r   )r   r   r   REPORT_DESCRIPTOR_KEY_MASK
INPUT_ITEMmaxinternal_max_in_report_lenOUTPUT_ITEMinternal_max_out_report_lenCOLLECTION_ITEM
usage_pageusageREPORT_COUNTr%   REPORT_SIZE
USAGE_PAGEUSAGE)r   descr   report_countreport_sizer7   r8   r   key_sizevalue_lengthbyte_lengths              r   ParseReportDescriptorrC   a   s    }"	#,+*
%c"g
'C ,B4H
'':5	+#1a7*-++[+:'	)	)[	8	+#1a7+.,,k,;(	)	)_	<	$	
	)	)\	9	RC!Gl*	*#Ba>	)	)[	8	RC!Gl*	*"2Qw=	)	)Z	7	RC!Gl*	*!"Ag|<
	)	)U	2	RC!Gl*	*Rq,7(""CM 	c"gN 
+r   c                 H   U R                  S5      nU H  nUR                  5       nU(       d  M  UR                  S5      u  pEUS:X  a  UR                  S5      Ul        MM  US:X  d  MU  UR                  S5      u  pgn[	        US5      Ul        [	        US5      Ul        M     g )N   
   =s   HID_NAMEutf8s   HID_ID   :   )splitstripdecodeproduct_stringint	vendor_id
product_id)	ueventr=   lineslinekv_vidpids	            r   ParseUeventrY      s    
,,u
%d::<D::dDAKHHV,d	
iGGDMkac3|dnCdo r   c                   F    \ rS rSrSr\S 5       rS rS rS r	S r
S rS	rg
)LinuxHidDevice   zImplementation of HID device for linux.

Implementation of HID device interface for linux that uses block
devices to interact with the device and sysfs to enumerate/discover
device metadata.
c               #     #    / n  [         R                  " S5      n U  H  n[         R
                  R                  SUS5      n[         R
                  R                  SUS5      n[        US5      n[        US5      n[        R                  " 5       n[         R
                  R                  SU5      Ul        [        UR                  5       U5        [        UR                  5       U5        UR                  5         UR                  5         UR                  5       v   M     g ! [         a    [        R                  " S5      ef = f7f)N/sys/class/hidrawzNo hidraw device is availabledevice/report_descriptorzdevice/ueventrbz/dev/)oslistdirFileNotFoundErrorr   
OsHidErrorpathjoinopenr   DeviceDescriptorrC   readrY   closeToPublicDict)hidraw_devicesdevrd_pathuevent_pathrd_fileuevent_filer=   s          r   	EnumerateLinuxHidDevice.Enumerate   s    N?zz"56n 
'',,!3(*  GGLL!4c?KkWd#gd+k""$d'',,w,diGLLND1+""$d+mmo   ?=>>?s   ED  DE !EEc                    [         R                  R                  X5        [        R                  " U[        R
                  5      U l        [         R                  " 5       U l        XR                  l	        [	        [        R                  R                  S[        R                  R                  U5      S5      S5      n[        UR                  5       U R                  5        UR                  5         g )Nr^   r_   r`   )r   	HidDevice__init__ra   rg   O_RDWRrm   rh   r=   re   rf   basenamerC   ri   rj   )selfre   rp   s      r   rv   LinuxHidDevice.__init__   s    NND'wwtRYY'DH%%'DIIIN277<< 3 " 0 0 6 :<=ACG ',,.$))4MMOr   c                 .    U R                   R                  $ See base class.)r=   r3   ry   s    r   GetInReportDataLength$LinuxHidDevice.GetInReportDataLength   s    99///r   c                 .    U R                   R                  $ r|   )r=   r5   r~   s    r   GetOutReportDataLength%LinuxHidDevice.GetOutReportDataLength   s    99000r   c                 d    [        S/U-   5      n[        R                  " U R                  U5        g)r}   r   N)r   ra   writerm   )ry   packetouts      r   WriteLinuxHidDevice.Write   s$    
QC&L
!CHHTXXsr   c                     [         R                  " U R                  U R                  5       5      n[	        [        U5      5      nU$ r|   )ra   ri   rm   r   listr   )ry   raw_in
decoded_ins      r   ReadLinuxHidDevice.Read   s4    WWTXXt99;<Fi'(Jr   )r=   rm   N)r*   r+   r,   r-   __doc__staticmethodrr   rv   r   r   r   r   r.   r)   r   r   r[   r[      s4        0	01
r   r[   )r   
__future__r   ra   r   pyu2fr   	pyu2f.hidr   r0   r   r4   r1   r6   r9   r:   r;   r<   r   r%   	Exceptionr'   rC   rY   ru   r[   r)   r   r   <module>r      s{     J  	   !  

%)P" <~%=T^^ =r   