Beispiel #1
0
 def reset(self,
           key='-1',
           log_packets=False,
           report_progress=None,
           detected_device=None):
     """
     @param key: The key to unlock the device
     @param log_packets: If true the packet stream to/from the device is logged
     @param report_progress: Function that is called with a % progress
                             (number between 0 and 100) for various tasks
                             If it is called with -1 that means that the
                             task does not have any progress information
     """
     with lock:
         self.device = get_device_by_id(self.VENDOR_ID, self.PRODUCT_ID)
         # Handle that is used to communicate with device. Setup in L{open}
         self.handle = None
         self.in_session = False
         self.log_packets = log_packets
         self.report_progress = report_progress
         if len(key) > 8:
             key = key[:8]
         elif len(key) < 8:
             key += ''.join(['\0' for i in xrange(8 - len(key))])
         self.key = key
Beispiel #2
0
 def open(self, connected_device, library_uuid):
     """
     Claim an interface on the device for communication.
     Requires write privileges to the device file.
     Also initialize the device.
     See the source code for the sequence of initialization commands.
     """
     with lock:
         if not hasattr(self, 'key'):
             self.reset()
         self.device = get_device_by_id(self.VENDOR_ID, self.PRODUCT_ID)
         if not self.device:
             raise DeviceError()
         configs = self.device.configurations
         try:
             self.handle = self.device.open()
             config = configs[0]
             try:
                 self.handle.set_configuration(configs[0])
             except USBError:
                 self.handle.set_configuration(configs[1])
                 config = configs[1]
             _id = config.interface.contents.altsetting.contents
             ed1 = _id.endpoint[0]
             ed2 = _id.endpoint[1]
             if ed1.EndpointAddress == self.BULK_IN_EP:
                 red, wed = ed1, ed2
             else:
                 red, wed = ed2, ed1
             self.bulk_read_max_packet_size = red.MaxPacketSize
             self.bulk_write_max_packet_size = wed.MaxPacketSize
             self.handle.claim_interface(self.INTERFACE_ID)
         except USBError as err:
             raise DeviceBusy(str(err))
         # Large timeout as device may still be initializing
         res = self.send_validated_command(GetUSBProtocolVersion(),
                                           timeout=20000)
         if res.code != 0:
             raise ProtocolError("Unable to get USB Protocol version.")
         version = self._bulk_read(24,
                                   data_type=USBProtocolVersion)[0].version
         if version not in KNOWN_USB_PROTOCOL_VERSIONS:
             print >> sys.stderr, "WARNING: Usb protocol version " + \
                                 hex(version) + " is unknown"
         res = self.send_validated_command(SetBulkSize(\
                         chunk_size = 512*self.bulk_read_max_packet_size, \
                         unknown = 2))
         if res.code != 0:
             raise ProtocolError("Unable to set bulk size.")
         res = self.send_validated_command(
             UnlockDevice(key=self.key))  #0x312d))
         if res.code != 0:
             raise DeviceLocked()
         res = self.send_validated_command(SetTime())
         if res.code != 0:
             raise ProtocolError("Could not set time on device")
Beispiel #3
0
 def is_connected(cls, helper=None):
     """
     This method checks to see whether the device is physically connected.
     It does not return any information about the validity of the
     software connection. You may need to call L{reconnect} if you keep
     getting L{DeviceError}.
     """
     try:
         return get_device_by_id(cls.VENDOR_ID, cls.PRODUCT_ID) != None
     except USBError:
         return False
Beispiel #4
0
 def is_connected(cls, helper=None):
     """
     This method checks to see whether the device is physically connected.
     It does not return any information about the validity of the
     software connection. You may need to call L{reconnect} if you keep
     getting L{DeviceError}.
     """
     try:
         return get_device_by_id(cls.VENDOR_ID, cls.PRODUCT_ID) != None
     except USBError:
         return False
Beispiel #5
0
 def open(self, connected_device, library_uuid):
     """
     Claim an interface on the device for communication.
     Requires write privileges to the device file.
     Also initialize the device.
     See the source code for the sequence of initialization commands.
     """
     with lock:
         if not hasattr(self, "key"):
             self.reset()
         self.device = get_device_by_id(self.VENDOR_ID, self.PRODUCT_ID)
         if not self.device:
             raise DeviceError()
         configs = self.device.configurations
         try:
             self.handle = self.device.open()
             config = configs[0]
             try:
                 self.handle.set_configuration(configs[0])
             except USBError:
                 self.handle.set_configuration(configs[1])
                 config = configs[1]
             _id = config.interface.contents.altsetting.contents
             ed1 = _id.endpoint[0]
             ed2 = _id.endpoint[1]
             if ed1.EndpointAddress == self.BULK_IN_EP:
                 red, wed = ed1, ed2
             else:
                 red, wed = ed2, ed1
             self.bulk_read_max_packet_size = red.MaxPacketSize
             self.bulk_write_max_packet_size = wed.MaxPacketSize
             self.handle.claim_interface(self.INTERFACE_ID)
         except USBError as err:
             raise DeviceBusy(str(err))
         # Large timeout as device may still be initializing
         res = self.send_validated_command(GetUSBProtocolVersion(), timeout=20000)
         if res.code != 0:
             raise ProtocolError("Unable to get USB Protocol version.")
         version = self._bulk_read(24, data_type=USBProtocolVersion)[0].version
         if version not in KNOWN_USB_PROTOCOL_VERSIONS:
             print >>sys.stderr, "WARNING: Usb protocol version " + hex(version) + " is unknown"
         res = self.send_validated_command(SetBulkSize(chunk_size=512 * self.bulk_read_max_packet_size, unknown=2))
         if res.code != 0:
             raise ProtocolError("Unable to set bulk size.")
         res = self.send_validated_command(UnlockDevice(key=self.key))  # 0x312d))
         if res.code != 0:
             raise DeviceLocked()
         res = self.send_validated_command(SetTime())
         if res.code != 0:
             raise ProtocolError("Could not set time on device")
Beispiel #6
0
 def reset(self, key="-1", log_packets=False, report_progress=None, detected_device=None):
     """
     @param key: The key to unlock the device
     @param log_packets: If true the packet stream to/from the device is logged
     @param report_progress: Function that is called with a % progress
                             (number between 0 and 100) for various tasks
                             If it is called with -1 that means that the
                             task does not have any progress information
     """
     with lock:
         self.device = get_device_by_id(self.VENDOR_ID, self.PRODUCT_ID)
         # Handle that is used to communicate with device. Setup in L{open}
         self.handle = None
         self.in_session = False
         self.log_packets = log_packets
         self.report_progress = report_progress
         if len(key) > 8:
             key = key[:8]
         elif len(key) < 8:
             key += "".join(["\0" for i in xrange(8 - len(key))])
         self.key = key
Beispiel #7
0
 def reconnect(self):
     """ Only recreates the device node and deleted the connection handle """
     self.device = get_device_by_id(self.VENDOR_ID, self.PRODUCT_ID)
     self.handle = None
Beispiel #8
0
 def reconnect(self):
     """ Only recreates the device node and deleted the connection handle """
     self.device = get_device_by_id(self.VENDOR_ID, self.PRODUCT_ID)
     self.handle = None