Example #1
0
    def response_length(self):
        """Extract resplen from the response header

        For cmd3=0x201 (fixed response length) commands word at 0x00 is
        response length excluding the first 0x40, as well as the word at
        0x48, the header of the next layer?

        """
        if not self.response_header:
            raise CanonError("_send() this command first.")
        return le32toi(self.response_header, 0)
Example #2
0
 def bulk_read(self, size, timeout=None):
     start = time.time()
     data = self.ep_in.read(size, timeout)
     end = time.time()
     data_size = len(data)
     if not data_size == size:
         _log.warn(
             "bulk_read: WRONG SIZE: 0x{:x} bytes instead of 0x{:x}".format(
                 data_size, size))
         _log.debug('\n' + hexdump(data))
         raise CanonError(
             "unexpected data length ({} instead of {})".format(
                 len(data), size))
     _log.info("bulk_read got {} (0x{:x}) b in {:.6f} sec".format(
         len(data), len(data), end - start))
     _log.debug("\n" + hexdump(data))
     return data
Example #3
0
 def control_write(self, wValue, data='', timeout=None):
     # bRequest is 0x4 if length of data is >1, 0x0c otherwise (length >1 ? 0x04 : 0x0C)
     bRequest = 0x04 if len(data) > 1 else 0x0c
     _log.info(
         "control_write (rt: 0x{:x}, req: 0x{:x}, wValue: 0x{:x}) 0x{:x} bytes"
         .format(0x40, bRequest, wValue, len(data)))
     _log.debug("\n" + hexdump(data))
     # bmRequestType is 0xC0 during read and 0x40 during write.
     i = self.device.ctrl_transfer(0x40,
                                   bRequest,
                                   wValue=wValue,
                                   wIndex=0,
                                   data_or_wLength=data,
                                   timeout=timeout)
     if i != len(data):
         raise CanonError("control write was incomplete")
     return i
Example #4
0
    def control_read(self, wValue, data_length=0, timeout=None):
        """Read from the control pipe.

        ``bRequest`` is 0x4 if length of data is >1, 0x0c otherwise (length >1 ? 0x04 : 0x0C)
        ``bmRequestType`` is 0xC0 during read and 0x40 during write.

        """
        #
        bRequest = 0x04 if data_length > 1 else 0x0c
        _log.info(
            "control_read (req: 0x{:x} wValue: 0x{:x}) reading 0x{:x} bytes".
            format(bRequest, wValue, data_length))

        response = self.device.ctrl_transfer(0xc0,
                                             bRequest,
                                             wValue=wValue,
                                             wIndex=0,
                                             data_or_wLength=data_length,
                                             timeout=timeout)
        if len(response) != data_length:
            raise CanonError("incorrect response length form camera")
        _log.debug('\n' + hexdump(response))
        return response
Example #5
0
            self._device.set_configuration()
            self._device.set_interface_altsetting()

        # Clear endpoint HALTs, not sure if needed, but doesn't hurt
        for ep in (self._usb.ep_in, self._usb.ep_int, self._usb.ep_out):
            try:
                usb.control.clear_feature(self._device,
                                          usb.control.ENDPOINT_HALT, ep)
            except USBError, e:
                _log.info("Clearing HALT on {} failed: {}".format(ep, e))

        # while polling, with a gracious timeout, do the dance
        with self._usb.poller_ctx() as p, self._usb.timeout_ctx(2000):
            camstat = self._usb.control_read(0x55, 1).tostring()
            if camstat not in ('A', 'C'):
                raise CanonError('Some kind of init error, camstat: %s',
                                 camstat)

            msg = self._usb.control_read(0x01, 0x58)
            if camstat == 'A':
                _log.debug("Camera was already active")
                self._usb.control_read(0x04, 0x50)
                return camstat

            _log.debug("Camera woken up, initializing")

            msg[0:0x40] = array('B', [0] * 0x40)
            msg[0] = 0x10
            msg[0x40:] = msg[-0x10:]
            self._usb.control_write(0x11, msg)
            self._usb.bulk_read(0x44)
Example #6
0
 def stop_poller(self):
     if not self._poller:
         raise CanonError("There's no poller to stop.")
     if self._poller.isAlive():
         self._poller.stop()
     self._poller = None
Example #7
0
 def start_poller(self, size=None, timeout=None):
     if self._poller and self._poller.isAlive():
         raise CanonError("Poller already started.")
     self._poller = InterruptPoller(self, size, timeout=timeout)
     self._poller.start()
Example #8
0
 def response_length(self):
     """Return the response length, excluding the first 0x40 bytes.
     """
     if not self.response_header:
         raise CanonError("_send() this command first.")
     return le32toi(self.response_header, 6)