3
M]W# @ s. d dl Z d dlZ d dlmZ e jje jjdZe jje jjdZ e j
jedZe j
je dZ
d Ze j
ej_e j
e j
e j
gej_e j
ej_e j
e j
e j
gej_e j
e j
e j
e jgej_e j
ej_e j
e j
e j
gej_e j
ej_e j
e j
gej_e j
ej_e j
e j
gej_e j
ej_e j
gej_e j
e je jge j_e j
e j_e j
e jge j_e je j_e j
e je j
ge j_e j
e j_e j
ge j_de j_dZ dZ!d Z"d
Z#dd Z$d
d Z%dd Zdd Z&dd Z'dd Z(G dd de)Z*dd Z+dd Z,dd Z-e.dkr*x,e/e- D ]\Z0Z1Z2e3d j4e0e1e2 qW dS )! N)list_ports_commonZIOKitZCoreFoundationkIOMasterPortDefaultkCFAllocatorDefault c C sX t jt|jdt}tj| |td}d}|rTt j|d}|dk rJ|jd}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)
cfCFStringCreateWithCStringr encodekCFStringEncodingMacRomaniokitIORegistryEntryCreateCFPropertyCFStringGetCStringPtrdecode CFRelease)device_typepropertykeyCFContaineroutput r $/usr/lib/python3.6/list_ports_osx.pyget_string_propertyO s
r c C sr t jt|jdt}tj| |td}|rn|tkr:tj }n|t
krJtj }t j||tj
| t j| |jS dS )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 kCFNumberSInt32Typectypesc_uint32kCFNumberSInt16TypeZc_uint16CFNumberGetValuebyrefr value)r r Zcf_number_typer r Znumberr r r get_int_propertyk s$
r" c C s" t jd}tj| t j| |jS )Nd )r Zcreate_string_bufferr IOObjectGetClassr r! )devicepathnamer r r IORegistryEntryGetName s
r' c C sR |j d}xBt| |krLtj }tj| dj dtj|}|dkrFdS |} qW | 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% Zparent_typeparentZresponser r r GetParentDeviceByType s
r+ c C sd t j }tjttj| jdt j| g }x(tj|rTtj |}|sHP |j
| q.W tj| |S )z6
returns iterator over specified service_type
r )r r( r IOServiceGetMatchingServicesr IOServiceMatchingr r ZIOIteratorIsValidZIOIteratorNextappendIOObjectRelease)Zservice_typeZserial_port_iteratorservicesservicer r r GetIOServicesByType s
r2 c C s^ dj | d? g}xB| d@ rRt|dkr0|jd |jdj | d? d@ | d K } qW d
j|S )zA
helper to calculate port and bus number from locationID
z{}- i r .z{} r )formatlenr. join)
locationIDlocr r r location_to_string s
r= c @ s e Zd ZdS )SuitableSerialInterfaceN)__name__
__module____qualname__r r r r r> s r> c C sp g } xft dD ]Z}t|d}|rt|d}|rt|dp:d}t|dtpJd}t }||_||_| j| qW | S )z
helper function to scan USB interfaces
returns a list of SuitableSerialInterface objects with name and id attributes
IOSerialBSDClientIOCalloutDeviceZIOUSBInterfacezUSB Interface NameNr; r7 ) r2 r r+ r" r r> idnamer. )Z
interfacesr1 r%
usb_devicerE r; ir r r scan_interfaces s
rH c C s" x| D ]}|j |kr|jS qW 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}|rtj|}t|d}|rt|dt|_t|dt|_ t|d|_
t|dpvd|_t|d |_t|d
t
}t||_t|||_|j |j| qW |S )NrB rC ZIOUSBDeviceZidVendorZ idProductzUSB Serial NumberzUSB Product Namezn/azUSB Vendor Namer; )r2 rH r r ZListPortInfor+ r" r ZvidpidZ
serial_numberproductZmanufacturerr r= locationrK rJ Zapply_usb_infor. )r0 ZportsrI r1 r% inforF r; r r r comports s(
rP __main__z{}: {} [{}])5r Zctypes.utilZserial.toolsr ZcdllZLoadLibraryutilZfind_libraryr r
r( Zin_dllr r r
r- Zrestyper, Zargtypesr) r r ZIORegistryEntryGetPathr' r$ r/ Zc_char_pZc_int32r r r r ZkCFNumberSInt8Typer r ZkCFNumberSInt64Typer r" r+ r2 r= objectr> rH rK rP r? sortedZportZdescZhwidprintr8 r r r r