home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_780 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  11.7 KB  |  393 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
  6. from ctypes import cdll, POINTER, byref, pointer, Structure as _Structure, c_ubyte, c_ushort, c_int, c_char, c_void_p, c_byte, c_uint
  7. from errno import EBUSY, ENOMEM
  8. from calibre import iswindows, isosx, isfreebsd, load_library
  9. _libusb_name = 'libusb'
  10. if iswindows:
  11.     pass
  12. elif isosx or isfreebsd:
  13.     pass
  14.  
  15. PATH_MAX = 4096
  16. if iswindows:
  17.     
  18.     class Structure(_Structure):
  19.         _pack_ = 1
  20.  
  21.     _libusb_name = 'libusb0'
  22. else:
  23.     Structure = _Structure
  24.  
  25. try:
  26.     
  27.     try:
  28.         _libusb = load_library(_libusb_name, cdll)
  29.     except OSError:
  30.         _libusb = cdll.LoadLibrary('libusb-0.1.so.4')
  31.  
  32.     has_library = True
  33. except:
  34.     _libusb = None
  35.     has_library = False
  36.  
  37.  
  38. class DeviceDescriptor(Structure):
  39.     _fields_ = [
  40.         ('Length', c_ubyte),
  41.         ('DescriptorType', c_ubyte),
  42.         ('bcdUSB', c_ushort),
  43.         ('DeviceClass', c_ubyte),
  44.         ('DeviceSubClass', c_ubyte),
  45.         ('DeviceProtocol', c_ubyte),
  46.         ('MaxPacketSize0', c_ubyte),
  47.         ('idVendor', c_ushort),
  48.         ('idProduct', c_ushort),
  49.         ('bcdDevice', c_ushort),
  50.         ('Manufacturer', c_ubyte),
  51.         ('Product', c_ubyte),
  52.         ('SerialNumber', c_ubyte),
  53.         ('NumConfigurations', c_ubyte)]
  54.  
  55.  
  56. class EndpointDescriptor(Structure):
  57.     _fields_ = [
  58.         ('Length', c_ubyte),
  59.         ('DescriptorType', c_ubyte),
  60.         ('EndpointAddress', c_ubyte),
  61.         ('Attributes', c_ubyte),
  62.         ('MaxPacketSize', c_ushort),
  63.         ('Interval', c_ubyte),
  64.         ('Refresh', c_ubyte),
  65.         ('SynchAddress', c_ubyte),
  66.         ('extra', POINTER(c_char)),
  67.         ('extralen', c_int)]
  68.  
  69.  
  70. class InterfaceDescriptor(Structure):
  71.     _fields_ = [
  72.         ('Length', c_ubyte),
  73.         ('DescriptorType', c_ubyte),
  74.         ('InterfaceNumber', c_ubyte),
  75.         ('AlternateSetting', c_ubyte),
  76.         ('NumEndpoints', c_ubyte),
  77.         ('InterfaceClass', c_ubyte),
  78.         ('InterfaceSubClass', c_ubyte),
  79.         ('InterfaceProtocol', c_ubyte),
  80.         ('Interface', c_ubyte),
  81.         ('endpoint', POINTER(EndpointDescriptor)),
  82.         ('extra', POINTER(c_char)),
  83.         ('extralen', c_int)]
  84.  
  85.  
  86. class Interface(Structure):
  87.     _fields_ = [
  88.         ('altsetting', POINTER(InterfaceDescriptor)),
  89.         ('num_altsetting', c_int)]
  90.  
  91.  
  92. class ConfigDescriptor(Structure):
  93.     _fields_ = [
  94.         ('Length', c_ubyte),
  95.         ('DescriptorType', c_ubyte),
  96.         ('TotalLength', c_ushort),
  97.         ('NumInterfaces', c_ubyte),
  98.         ('Value', c_ubyte),
  99.         ('Configuration', c_ubyte),
  100.         ('Attributes', c_ubyte),
  101.         ('MaxPower', c_ubyte),
  102.         ('interface', POINTER(Interface)),
  103.         ('extra', POINTER(c_ubyte)),
  104.         ('extralen', c_int)]
  105.     
  106.     def __str__(self):
  107.         ans = ''
  108.         for field in self._fields_:
  109.             ans += field[0] + ': ' + str(eval('self.' + field[0])) + '\n'
  110.         
  111.         return ans.strip()
  112.  
  113.  
  114.  
  115. class Error(Exception):
  116.     pass
  117.  
  118.  
  119. class Device(Structure):
  120.     
  121.     def open(self):
  122.         handle = _libusb.usb_open(byref(self))
  123.         if not handle:
  124.             raise Error('Cannot open device')
  125.         handle
  126.         return handle.contents
  127.  
  128.     
  129.     def configurations(self):
  130.         doc = ' List of device configurations. See L{ConfigDescriptor} '
  131.         
  132.         def fget(self):
  133.             ans = []
  134.             for config in range(self.device_descriptor.NumConfigurations):
  135.                 ans.append(self.config_descriptor[config])
  136.             
  137.             return tuple(ans)
  138.  
  139.         return property(doc = doc, fget = fget)
  140.  
  141.     configurations = dynamic_property(configurations)
  142.  
  143.  
  144. class Bus(Structure):
  145.     
  146.     def device_list(self):
  147.         doc = '\n        Flat list of devices on this bus.\n        Note: children are not explored\n        TODO: Check if exploring children is neccessary (e.g. with an external hub)\n        '
  148.         
  149.         def fget(self):
  150.             if _libusb is None:
  151.                 return []
  152.             if _libusb.usb_find_devices() < 0:
  153.                 raise Error('Unable to search for USB devices')
  154.             _libusb.usb_find_devices() < 0
  155.             ndev = self.devices
  156.             ans = []
  157.             while ndev:
  158.                 dev = ndev.contents
  159.                 ans.append(dev)
  160.                 ndev = dev.next
  161.                 continue
  162.                 _libusb is None
  163.             return ans
  164.  
  165.         return property(doc = doc, fget = fget)
  166.  
  167.     device_list = dynamic_property(device_list)
  168.  
  169.  
  170. class DeviceHandle(Structure):
  171.     _fields_ = [
  172.         ('fd', c_int),
  173.         ('bus', POINTER(Bus)),
  174.         ('device', POINTER(Device)),
  175.         ('config', c_int),
  176.         ('interface', c_int),
  177.         ('altsetting', c_int),
  178.         ('impl_info', c_void_p)]
  179.     
  180.     def close(self):
  181.         _libusb.usb_close(byref(self))
  182.  
  183.     
  184.     def set_configuration(self, config):
  185.         
  186.         try:
  187.             num = config.Value
  188.         except AttributeError:
  189.             num = config
  190.  
  191.         ret = _libusb.usb_set_configuration(byref(self), num)
  192.         if ret < 0:
  193.             raise Error('Failed to set device configuration to: ' + str(num) + '. Error code: ' + str(ret))
  194.         ret < 0
  195.  
  196.     
  197.     def claim_interface(self, num):
  198.         ret = _libusb.usb_claim_interface(byref(self), num)
  199.         if -ret == ENOMEM:
  200.             raise Error('Insufficient memory to claim interface')
  201.         -ret == ENOMEM
  202.         if -ret == EBUSY:
  203.             raise Error('Device busy')
  204.         -ret == EBUSY
  205.         if ret < 0:
  206.             raise Error('Unknown error occurred while trying to claim USB interface: ' + str(ret))
  207.         ret < 0
  208.  
  209.     
  210.     def control_msg(self, rtype, request, bytes, value = 0, index = 0, timeout = 100):
  211.         size = 0
  212.         
  213.         try:
  214.             size = len(bytes)
  215.         except TypeError:
  216.             size = bytes
  217.             ArrayType = c_byte * size
  218.             _libusb.usb_control_msg.argtypes = [
  219.                 POINTER(DeviceHandle),
  220.                 c_int,
  221.                 c_int,
  222.                 c_int,
  223.                 c_int,
  224.                 POINTER(ArrayType),
  225.                 c_int,
  226.                 c_int]
  227.             arr = ArrayType()
  228.             rsize = _libusb.usb_control_msg(byref(self), rtype, request, value, index, byref(arr), size, timeout)
  229.             if rsize < size:
  230.                 raise Error('Could not read ' + str(size) + ' bytes on the control bus. Read: ' + str(rsize) + ' bytes.')
  231.             rsize < size
  232.             return arr
  233.  
  234.         ArrayType = c_byte * size
  235.         _libusb.usb_control_msg.argtypes = [
  236.             POINTER(DeviceHandle),
  237.             c_int,
  238.             c_int,
  239.             c_int,
  240.             c_int,
  241.             POINTER(ArrayType),
  242.             c_int,
  243.             c_int]
  244.         arr = ArrayType(*bytes)
  245.         return _libusb.usb_control_msg(byref(self), rtype, request, value, index, byref(arr), size, timeout)
  246.  
  247.     
  248.     def bulk_read(self, endpoint, size, timeout = 100):
  249.         ArrayType = c_byte * size
  250.         arr = ArrayType()
  251.         _libusb.usb_bulk_read.argtypes = [
  252.             POINTER(DeviceHandle),
  253.             c_int,
  254.             POINTER(ArrayType),
  255.             c_int,
  256.             c_int]
  257.         rsize = _libusb.usb_bulk_read(byref(self), endpoint, byref(arr), size, timeout)
  258.         if rsize < 0:
  259.             raise Error('Could not read ' + str(size) + ' bytes on the bulk bus. Error code: ' + str(rsize))
  260.         rsize < 0
  261.         if rsize == 0:
  262.             raise Error('Device sent zero bytes')
  263.         rsize == 0
  264.         if rsize < size:
  265.             arr = arr[:rsize]
  266.         
  267.         return arr
  268.  
  269.     
  270.     def bulk_write(self, endpoint, bytes, timeout = 100):
  271.         size = len(bytes)
  272.         ArrayType = c_byte * size
  273.         arr = ArrayType(*bytes)
  274.         _libusb.usb_bulk_write.argtypes = [
  275.             POINTER(DeviceHandle),
  276.             c_int,
  277.             POINTER(ArrayType),
  278.             c_int,
  279.             c_int]
  280.         _libusb.usb_bulk_write(byref(self), endpoint, byref(arr), size, timeout)
  281.  
  282.     
  283.     def release_interface(self, num):
  284.         ret = _libusb.usb_release_interface(pointer(self), num)
  285.         if ret < 0:
  286.             raise Error('Unknown error occurred while trying to release USB interface: ' + str(ret))
  287.         ret < 0
  288.  
  289.     
  290.     def reset(self):
  291.         ret = _libusb.usb_reset(pointer(self))
  292.         if ret < 0:
  293.             raise Error('Unknown error occurred while trying to reset USB device ' + str(ret))
  294.         ret < 0
  295.  
  296.  
  297. Bus._fields_ = [
  298.     ('next', POINTER(Bus)),
  299.     ('previous', POINTER(Bus)),
  300.     ('dirname', c_char * (PATH_MAX + 1)),
  301.     ('devices', POINTER(Device)),
  302.     ('location', c_uint),
  303.     ('root_dev', POINTER(Device))]
  304. Device._fields_ = [
  305.     ('next', POINTER(Device)),
  306.     ('previous', POINTER(Device)),
  307.     ('filename', c_char * (PATH_MAX + 1)),
  308.     ('bus', POINTER(Bus)),
  309.     ('device_descriptor', DeviceDescriptor),
  310.     ('config_descriptor', POINTER(ConfigDescriptor)),
  311.     ('dev', c_void_p),
  312.     ('devnum', c_ubyte),
  313.     ('num_children', c_ubyte),
  314.     ('children', POINTER(POINTER(Device)))]
  315. if _libusb is not None:
  316.     
  317.     try:
  318.         _libusb.usb_get_busses.restype = POINTER(Bus)
  319.         _libusb.usb_open.restype = POINTER(DeviceHandle)
  320.         _libusb.usb_open.argtypes = [
  321.             POINTER(Device)]
  322.         _libusb.usb_close.argtypes = [
  323.             POINTER(DeviceHandle)]
  324.         _libusb.usb_claim_interface.argtypes = [
  325.             POINTER(DeviceHandle),
  326.             c_int]
  327.         _libusb.usb_claim_interface.restype = c_int
  328.         _libusb.usb_release_interface.argtypes = [
  329.             POINTER(DeviceHandle),
  330.             c_int]
  331.         _libusb.usb_release_interface.restype = c_int
  332.         _libusb.usb_reset.argtypes = [
  333.             POINTER(DeviceHandle)]
  334.         _libusb.usb_reset.restype = c_int
  335.         _libusb.usb_control_msg.restype = c_int
  336.         _libusb.usb_bulk_read.restype = c_int
  337.         _libusb.usb_bulk_write.restype = c_int
  338.         _libusb.usb_set_configuration.argtypes = [
  339.             POINTER(DeviceHandle),
  340.             c_int]
  341.         _libusb.usb_set_configuration.restype = c_int
  342.         _libusb.usb_init()
  343.     _libusb = None
  344.  
  345.  
  346.  
  347. def busses():
  348.     if _libusb is None:
  349.         raise Error('Could not find libusb.')
  350.     _libusb is None
  351.     if _libusb.usb_find_busses() < 0:
  352.         raise Error('Unable to search for USB busses')
  353.     _libusb.usb_find_busses() < 0
  354.     if _libusb.usb_find_devices() < 0:
  355.         raise Error('Unable to search for USB devices')
  356.     _libusb.usb_find_devices() < 0
  357.     ans = []
  358.     nbus = _libusb.usb_get_busses()
  359.     while nbus:
  360.         bus = nbus.contents
  361.         ans.append(bus)
  362.         nbus = bus.next
  363.     return ans
  364.  
  365.  
  366. def get_device_by_id(idVendor, idProduct):
  367.     buslist = busses()
  368.     for bus in buslist:
  369.         devices = bus.device_list
  370.         for dev in devices:
  371.             if dev.device_descriptor.idVendor == idVendor and dev.device_descriptor.idProduct == idProduct:
  372.                 return dev
  373.         
  374.     
  375.  
  376.  
  377. def has_library():
  378.     return _libusb is not None
  379.  
  380.  
  381. def get_devices():
  382.     buslist = busses()
  383.     ans = []
  384.     for bus in buslist:
  385.         devices = bus.device_list
  386.         for dev in devices:
  387.             device = (dev.device_descriptor.idVendor, dev.device_descriptor.idProduct, dev.device_descriptor.bcdDevice)
  388.             ans.append(device)
  389.         
  390.     
  391.     return ans
  392.  
  393.