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   





!	

#