o
    1»_ª+  ã                   @   s^  d dl mZ d dlZd dlmZ ej d¡Zej d¡Zd Z	ej
 ed¡Zd ZdZdZd	Zd
Zd ZejZej
ej_ej
ej
ej
gej_eej_ej
ej
ej
gej_eej_ej
ej
ej
ejgej_ej
ej_ej
ej
ej
gej_eej_ej
ej
gej_eej_ej
ej
gej_eej_ej
gej_ej
ej ej!gej"_ej
ej"_ej
ejgej#_ej ej#_ej
ej
ej$ejgej%_ej&ej%_ej
ejej
gej'_ej
ej'_ej
gej(_dej(_dZ)dZ*dZ+dZ,dd„ Z-dd„ Z.dd„ Zdd„ Zdd„ Z/dd„ Z0dd„ Z1G dd„ de2ƒZ3dd „ Z4d!d"„ Z5d(d$d%„Z6e7d&k�r+e8e6ƒ ƒD ]\Z9Z:Z;e<d' =e9e:e;¡ƒ �qdS dS ))é    )Úabsolute_importN)Úlist_ports_commonz0/System/Library/Frameworks/IOKit.framework/IOKitzB/System/Library/Frameworks/CoreFoundation.framework/CoreFoundationÚkCFAllocatorDefaulti  zUSB Vendor NamezUSB Serial Numberé€   é   é   é   é   c                 C   sŠ   t  t| d¡t¡}t | |td¡}d}|rCt  |d¡}|dur&| d¡}nt	 
t¡}t  |t	 |¡tt¡}|r>|j d¡}t  |¡ |S )zÙ
    Search the given device for the specified string property

    @param device_type Type of Device
    @param property String to search for
    @return Python string containing the value, or None if not found.
    úutf-8r   N)ÚcfÚCFStringCreateWithCStringr   ÚencodeÚkCFStringEncodingUTF8ÚiokitÚIORegistryEntryCreateCFPropertyÚCFStringGetCStringPtrÚdecodeÚctypesÚcreate_string_bufferÚio_name_sizeÚCFStringGetCStringÚbyrefÚvalueÚ	CFRelease)Údevice_typeÚpropertyÚkeyÚCFContainerÚoutputÚbufferÚsuccess© r!   ú=/usr/lib/python3/dist-packages/serial/tools/list_ports_osx.pyÚget_string_propertyc   s,   ýü

r#   c                 C   sr   t  t| d¡t¡}t | |td¡}|r7|tkrt 	¡ }n|t
kr%t ¡ }t  ||t |¡¡ t  |¡ |jS dS )a  
    Search the given device for the specified string property

    @param device_type Device to search
    @param property String to search for
    @param cf_number_type CFType number

    @return Python string containing the value, or None if not found.
    r
   r   N)r   r   r   r   r   r   r   ÚkCFNumberSInt32Typer   Úc_uint32ÚkCFNumberSInt16TypeÚc_uint16ÚCFNumberGetValuer   r   r   )r   r   Úcf_number_typer   r   Únumberr!   r!   r"   Úget_int_property„   s(   
ýü

r+   c                 C   s4   t  t¡}t | t  |¡¡}|tkrd S |j d¡S )Nr
   )	r   r   r   r   ÚIORegistryEntryGetNamer   ÚKERN_SUCCESSr   r   )ÚdeviceÚ
devicenameÚresr!   r!   r"   r,   £   s
   
r,   c                 C   s"   t  t¡}t | t  |¡¡ |jS ©N)r   r   r   r   ÚIOObjectGetClassr   r   )r.   Ú	classnamer!   r!   r"   r2   ¬   s   
r2   c                 C   sX   |  d¡}t| ƒ|kr*t ¡ }t | d  d¡t |¡¡}|tkr"dS |} t| ƒ|ks| S )z¼ Find the first parent of a device that implements the parent_type
        @param IOService Service to inspect
        @return Pointer to the parent type, or None if it was not found.
    r
   Ú	IOServiceN)r   r2   r   Úc_void_pr   ÚIORegistryEntryGetParentEntryr   r-   )r.   Úparent_typeÚparentÚresponser!   r!   r"   ÚGetParentDeviceByType±   s   
ý÷
r:   c                 C   sh   t  ¡ }t tt |  d¡¡t  |¡¡ g }t |¡r-t 	|¡}|s#n
| 
|¡ t |¡st |¡ |S )z6
    returns iterator over specified service_type
    r
   )r   r5   r   ÚIOServiceGetMatchingServicesÚkIOMasterPortDefaultÚIOServiceMatchingr   r   ÚIOIteratorIsValidÚIOIteratorNextÚappendÚIOObjectRelease)Úservice_typeÚserial_port_iteratorÚservicesÚservicer!   r!   r"   ÚGetIOServicesByTypeÅ   s   ý



ü
rF   c                 C   s`   d  | d? ¡g}| d@ r+t|ƒdkr| d¡ | d  | d? d@ ¡¡ | d	K } | d@ sd
 |¡S )zA
    helper to calculate port and bus number from locationID
    z{}-é   i  ð r   Ú.z{}é   é   r	   Ú )ÚformatÚlenr@   Újoin)Ú
locationIDÚlocr!   r!   r"   Úlocation_to_stringÚ   s   
ü
rQ   c                   @   s   e Zd ZdS )ÚSuitableSerialInterfaceN)Ú__name__Ú
__module__Ú__qualname__r!   r!   r!   r"   rR   ç   s    rR   c                  C   sl   g } t dƒD ]-}t|dƒ}|r3t|dƒ}|r3t|dƒpd}t|dtƒp$d}tƒ }||_||_|  |¡ q| S )z‚
    helper function to scan USB interfaces
    returns a list of SuitableSerialInterface objects with name and id attributes
    ÚIOSerialBSDClientÚIOCalloutDeviceÚIOUSBInterfacezUSB Interface NameNrO   rK   )	rF   r#   r:   r+   r$   rR   ÚidÚnamer@   )Ú
interfacesrE   r.   Ú
usb_devicerZ   rO   Úir!   r!   r"   Úscan_interfacesë   s   


€r^   c                 C   s"   | D ]}|j |kr|j  S qd S r1   )rY   rZ   )Úserial_interfacesrO   Ú	interfacer!   r!   r"   Ú#search_for_locationID_in_interfacesÿ   s
   

ÿra   Fc           	      C   sÊ   t dƒ}g }tƒ }|D ]W}t|dƒ}|rbt |¡}t|dƒ}|s%t|dƒ}|r]t|dtƒ|_t|dtƒ|_	t|t
ƒ|_t|ƒp@d|_t|tƒ|_t|dtƒ}t|ƒ|_t||ƒ|_| ¡  | |¡ q|S )	NrV   rW   ÚIOUSBHostDeviceÚIOUSBDeviceÚidVendorÚ	idProductzn/arO   )rF   r^   r#   r   ÚListPortInfor:   r+   r&   ÚvidÚpidÚkUSBSerialNumberStringÚserial_numberr,   ÚproductÚkUSBVendorStringÚmanufacturerr$   rQ   Úlocationra   r`   Úapply_usb_infor@   )	Úinclude_linksrD   Úportsr_   rE   r.   Úinfor\   rO   r!   r!   r"   Úcomports  s.   





€rs   Ú__main__z{}: {} [{}])F)>Ú
__future__r   r   Úserial.toolsr   ÚcdllÚLoadLibraryr   r   r<   r5   Úin_dllr   ÚkCFStringEncodingMacRomanr   rl   ri   r   r-   Úc_intÚkern_return_tr=   Úrestyper;   Úargtypesr6   r%   r   ÚIORegistryEntryGetPathr,   r2   rA   Úc_char_pÚc_int32r   r   Úc_longr   Úc_boolr(   r   ÚkCFNumberSInt8Typer&   r$   ÚkCFNumberSInt64Typer#   r+   r:   rF   rQ   ÚobjectrR   r^   ra   rs   rS   ÚsortedÚportÚdescÚhwidÚprintrL   r!   r!   r!   r"   Ú<module>   st   





!	

#þ