3 M]W#@s.ddlZddlZddlmZejjejjdZejjejjdZ ej j edZ ej j e dZ dZej ej_ej ej ej gej_ej ej_ej ej ej gej_ej ej ej ejgej_ej ej_ej ej ej gej_ej ej_ej ej gej_ej ej_ej ej gej_ej ej_ej gej_ej ejejge j_ej e j_ej ejge j_eje j_ej ejej ge j_ej e j_ej ge j_de j_dZ dZ!d Z"d Z#d d Z$d dZ%ddZddZ&ddZ'ddZ(Gddde)Z*ddZ+ddZ,ddZ-e.dkr*x,e/e-D]\Z0Z1Z2e3d j4e0e1e2qWdS)!N)list_ports_commonZIOKitZCoreFoundationkIOMasterPortDefaultkCFAllocatorDefaultcCsXtjt|jdt}tj||td}d}|rTtj|d}|dk rJ|jd}tj ||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_romanrN) cfCFStringCreateWithCStringrencodekCFStringEncodingMacRomaniokitIORegistryEntryCreateCFPropertyCFStringGetCStringPtrdecode CFRelease) device_typepropertykey CFContaineroutputr$/usr/lib/python3.6/list_ports_osx.pyget_string_propertyOs    rcCsrtjt|jdt}tj||td}|rn|tkr:tj }n|t krJtj }tj ||tj |tj||jSdS)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 rN)r r rr r rrkCFNumberSInt32Typectypesc_uint32kCFNumberSInt16TypeZc_uint16CFNumberGetValuebyrefrvalue)rrZcf_number_typerrZnumberrrrget_int_propertyks$   r"cCs"tjd}tj|tj||jS)Nd)rZcreate_string_bufferrIOObjectGetClassr r!)devicepathnamerrrIORegistryEntryGetNames r'cCsR|jd}xBt||krLtj}tj|djdtj|}|dkrFdS|}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 IOServicerN)r r'rc_void_prIORegistryEntryGetParentEntryr )r%Z parent_typeparentZresponserrrGetParentDeviceByTypes  r+cCsdtj}tjttj|jdtj|g}x(tj|rTtj |}|sHP|j |q.Wtj ||S)z6 returns iterator over specified service_type r ) rr(rIOServiceGetMatchingServicesrIOServiceMatchingr r ZIOIteratorIsValidZIOIteratorNextappendIOObjectRelease)Z service_typeZserial_port_iteratorservicesservicerrrGetIOServicesByTypes    r2cCs^dj|d?g}xB|d@rRt|dkr0|jd|jdj|d?d@|d K}qWd j|S) zA helper to calculate port and bus number from locationID z{}-ir.z{}r)formatlenr.join) locationIDlocrrrlocation_to_strings    r=c@s eZdZdS)SuitableSerialInterfaceN)__name__ __module__ __qualname__rrrrr>sr>cCspg}xftdD]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) r2rr+r"rr>idnamer.)Z interfacesr1r% usb_devicerEr;irrrscan_interfacess  rHcCs"x|D]}|j|kr|jSqWdS)N)rDrE)serial_interfacesr; interfacerrr#search_for_locationID_in_interfacess   rKcCstd}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) NrBrCZ IOUSBDeviceZidVendorZ idProductzUSB Serial NumberzUSB Product Namezn/azUSB Vendor Namer;)r2rHrrZ ListPortInfor+r"rZvidpidZ serial_numberproductZ manufacturerrr=locationrKrJZapply_usb_infor.)r0ZportsrIr1r%inforFr;rrrcomportss(         rP__main__z {}: {} [{}])5rZ ctypes.utilZ serial.toolsrZcdllZ LoadLibraryutilZ find_libraryrr r(Zin_dllrrr r-Zrestyper,Zargtypesr)rrZIORegistryEntryGetPathr'r$r/Zc_char_pZc_int32r rrrZkCFNumberSInt8TyperrZkCFNumberSInt64Typerr"r+r2r=objectr>rHrKrPr?sortedZportZdescZhwidprintr8rrrrsZ