
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
M]W#                 @   s.  d dl Z d dlZ d dlmZ e jje jjdZe jje jjdZ	e j
jedZe j
je	dZd Z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
e jgej_e j
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
gej_e j
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
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 Z(G dd de)Z*dd Z+dd Z,dd Z-e.dkr*x,e/e- D ]\Z0Z1Z2e3d j4e0e1e2 qW dS )!    N)list_ports_commonZIOKitZCoreFoundationkIOMasterPortDefaultkCFAllocatorDefault            c             C   sX   t jt|jdt}tj| |td}d}|rTt j|d}|dk	rJ|jd}t j	| |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.
    	mac_romanr   N)
cfCFStringCreateWithCStringr   encodekCFStringEncodingMacRomaniokitIORegistryEntryCreateCFPropertyCFStringGetCStringPtrdecode	CFRelease)device_typepropertykeyCFContaineroutput r   $/usr/lib/python3.6/list_ports_osx.pyget_string_propertyO   s     

r   c             C   sr   t jt|jdt}tj| |td}|rn|tkr:tj	 }n|t
krJtj }t j||tj| t j| |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   kCFNumberSInt32Typectypesc_uint32kCFNumberSInt16TypeZc_uint16CFNumberGetValuebyrefr   value)r   r   Zcf_number_typer   r   Znumberr   r   r   get_int_propertyk   s$    


r"   c             C   s"   t jd}tj| t j| |jS )Nd   )r   Zcreate_string_bufferr   IOObjectGetClassr    r!   )devicepathnamer   r   r   IORegistryEntryGetName   s    
r'   c             C   sR   |j d}xBt| |krLtj }tj| dj dtj|}|dkrFdS |} qW | 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	   Z	IOServicer   N)r   r'   r   c_void_pr   IORegistryEntryGetParentEntryr    )r%   Zparent_typeparentZresponser   r   r   GetParentDeviceByType   s    
r+   c             C   sd   t j }tjttj| jdt j| g }x(tj|rTtj	|}|sHP |j
| q.W tj| |S )z6
    returns iterator over specified service_type
    r	   )r   r(   r   IOServiceGetMatchingServicesr   IOServiceMatchingr   r    ZIOIteratorIsValidZIOIteratorNextappendIOObjectRelease)Zservice_typeZserial_port_iteratorservicesservicer   r   r   GetIOServicesByType   s    

r2   c             C   s^   dj | d? g}xB| d@ rRt|dkr0|jd |jdj | d? d@  | d	K } qW d
j|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    

r=   c               @   s   e Zd ZdS )SuitableSerialInterfaceN)__name__
__module____qualname__r   r   r   r   r>      s   r>   c              C   sp   g } xft dD ]Z}t|d}|rt|d}|rt|dp:d}t|dtpJd}t }||_||_| j| qW | S )z
    helper function to scan USB interfaces
    returns a list of SuitableSerialInterface objects with name and id attributes
    IOSerialBSDClientIOCalloutDeviceZIOUSBInterfacezUSB Interface NameNr;   r7   )	r2   r   r+   r"   r   r>   idnamer.   )Z
interfacesr1   r%   
usb_devicerE   r;   ir   r   r   scan_interfaces   s    

rH   c             C   s"   x| D ]}|j |kr|jS qW d S )N)rD   rE   )serial_interfacesr;   	interfacer   r   r   #search_for_locationID_in_interfaces   s    


rK   c              C   s   t d} g }t }x| D ]}t|d}|rtj|}t|d}|rt|dt|_t|dt|_	t|d|_
t|dpvd|_t|d	|_t|d
t}t||_t|||_|j  |j| qW |S )NrB   rC   ZIOUSBDeviceZidVendorZ	idProductzUSB Serial NumberzUSB Product Namezn/azUSB Vendor Namer;   )r2   rH   r   r   ZListPortInfor+   r"   r   ZvidpidZserial_numberproductZmanufacturerr   r=   locationrK   rJ   Zapply_usb_infor.   )r0   ZportsrI   r1   r%   inforF   r;   r   r   r   comports   s(    




rP   __main__z{}: {} [{}])5r   Zctypes.utilZserial.toolsr   ZcdllZLoadLibraryutilZfind_libraryr   r
   r(   Zin_dllr   r   r   r-   Zrestyper,   Zargtypesr)   r   r   ZIORegistryEntryGetPathr'   r$   r/   Zc_char_pZc_int32r   r   r   r   ZkCFNumberSInt8Typer   r   ZkCFNumberSInt64Typer   r"   r+   r2   r=   objectr>   rH   rK   rP   r?   sortedZportZdescZhwidprintr8   r   r   r   r   <module>   sZ   








 
