import re
from ctypes import *
from ctypes.util import find_library

libc = cdll.LoadLibrary(find_library("c"))
sysctlbyname = libc.sysctlbyname

def posix_sysctlbyname(name):
    _len = c_uint(0)
    result = sysctlbyname(name,None , byref(_len), None, 0)
    _mem = create_string_buffer(_len.value)
    result = sysctlbyname(name, _mem, byref(_len), None, 0)
    if result != 0:
        raise Exception('sysctlbyname returned with error %s' % result)
    return _mem.value

def usb_vid_pid(name):
    limitcount = 100
    n = 0
    # iterate through all possible umodem devices
    while n < limitcount:
        try:
            result = (posix_sysctlbyname(b'dev.umodem.'+bytes(str(n),'ascii')+b'.%pnpinfo')).decode('ascii')
            ttyname = (posix_sysctlbyname(b'dev.umodem.'+bytes(str(n),'ascii')+b'.ttyname')).decode('ascii')
            if name == '/dev/cua' + ttyname:
                break
        except:
            pass
        # if there is a device that matches our name, we are done
        n += 1
    if n >= limitcount:
        # dummy return, causes next higher layer to fail comparison
        return([0, 0])

    items=result.split(' ')
    vendor=int(items[0].split('=')[1],0)
    product=int(items[1].split('=')[1],0)
    return([vendor,product])