Device subsystem: Make the USB ids of the device being opened available to the driver while open() is being called.

This commit is contained in:
Kovid Goyal 2011-11-30 09:34:23 +05:30
parent 23caca5f47
commit e082bf17f2
9 changed files with 69 additions and 49 deletions

View File

@ -39,15 +39,15 @@ def get_connected_device():
if ok:
dev = d
dev.reset(log_packets=False, detected_device=det)
connected_devices.append(dev)
connected_devices.append((det, dev))
if dev is None:
print >>sys.stderr, 'Unable to find a connected ebook reader.'
return
for d in connected_devices:
for det, d in connected_devices:
try:
d.open(None)
d.open(det, None)
except:
continue
else:
@ -121,7 +121,7 @@ def debug(ioreg_to_tmp=False, buf=None):
out('Trying to open', dev.name, '...', end=' ')
try:
dev.reset(detected_device=det)
dev.open(None)
dev.open(det, None)
out('OK')
except:
import traceback

View File

@ -808,7 +808,7 @@ class ITUNES(DriverBase):
self.log.info("ITUNES.get_file(): exporting '%s'" % path)
outfile.write(open(self.cached_books[path]['lib_book'].location().path).read())
def open(self, library_uuid):
def open(self, connected_device, library_uuid):
'''
Perform any device specific initialization. Called after the device is
detected but before any other functions that communicate with the device.
@ -3224,7 +3224,7 @@ class ITUNES_ASYNC(ITUNES):
only_presence=False):
return self.connected, self
def open(self, library_uuid):
def open(self, connected_device, library_uuid):
'''
Perform any device specific initialization. Called after the device is
detected but before any other functions that communicate with the device.

View File

@ -59,9 +59,9 @@ class BAMBOOK(DeviceConfig, DevicePlugin):
def reset(self, key='-1', log_packets=False, report_progress=None,
detected_device=None) :
self.open(None)
self.open(None, None)
def open(self, library_uuid):
def open(self, connected_device, library_uuid):
# Make sure the Bambook library is ready
if not is_bambook_lib_ready():
raise OpenFeedback(_("Unable to connect to Bambook, you need to install Bambook library first."))
@ -309,8 +309,8 @@ class BAMBOOK(DeviceConfig, DevicePlugin):
with TemporaryFile('.snb') as snbfile:
if self.bambook.PackageSNB(snbfile, tdir) and self.bambook.VerifySNB(snbfile):
guid = self.bambook.SendFile(snbfile, self.get_guid(metadata[i].uuid))
elif f[-3:].upper() == 'SNB':
elif f[-3:].upper() == 'SNB':
if self.bambook.VerifySNB(f):
guid = self.bambook.SendFile(f, self.get_guid(metadata[i].uuid))
else:

View File

@ -79,7 +79,7 @@ class FOLDER_DEVICE(USBMS):
only_presence=False):
return self.is_connected, self
def open(self, library_uuid):
def open(self, connected_device, library_uuid):
self.current_library_uuid = library_uuid
if not self._main_prefix:
return False

View File

@ -133,8 +133,14 @@ class DevicePlugin(Plugin):
if debug:
self.print_usb_device_info(device_id)
if only_presence or self.can_handle_windows(device_id, debug=debug):
return True
return False
try:
bcd = int(device_id.rpartition(
'rev_')[-1].replace(':', 'a'), 16)
except:
bcd = None
return True, (vendor_id, product_id, bcd, None,
None, None)
return False, None
def test_bcd(self, bcdDevice, bcd):
if bcd is None or len(bcd) == 0:
@ -154,7 +160,7 @@ class DevicePlugin(Plugin):
'''
if iswindows:
return self.is_usb_connected_windows(devices_on_system,
debug=debug, only_presence=only_presence), None
debug=debug, only_presence=only_presence)
vendors_on_system = set([x[0] for x in devices_on_system])
vendors = self.VENDOR_ID if hasattr(self.VENDOR_ID, '__len__') else [self.VENDOR_ID]
@ -224,7 +230,7 @@ class DevicePlugin(Plugin):
return True
def open(self, library_uuid):
def open(self, connected_device, library_uuid):
'''
Perform any device specific initialization. Called after the device is
detected but before any other functions that communicate with the device.
@ -238,6 +244,16 @@ class DevicePlugin(Plugin):
This method can raise an OpenFeedback exception to display a message to
the user.
:param connected_device: The device that we are trying to open. It is
a tuple of (vendor id, product id, bcd, manufacturer name, product
name, device serial number). However, some device have no serial number
and on windows only the first three fields are present, the rest are
None.
:param library_uuid: The UUID of the current calibre library. Can be
None if there is no library (for example when used from the command
line.
'''
raise NotImplementedError()

View File

@ -205,15 +205,15 @@ def main():
if ok:
dev = d
dev.reset(log_packets=options.log_packets, detected_device=det)
connected_devices.append(dev)
connected_devices.append((det, dev))
if dev is None:
print >>sys.stderr, 'Unable to find a connected ebook reader.'
return 1
for d in connected_devices:
for det, d in connected_devices:
try:
d.open(None)
d.open(det, None)
except:
continue
else:

View File

@ -240,7 +240,7 @@ class PRS500(DeviceConfig, DevicePlugin):
def set_progress_reporter(self, report_progress):
self.report_progress = report_progress
def open(self, library_uuid) :
def open(self, connected_device, library_uuid) :
"""
Claim an interface on the device for communication.
Requires write privileges to the device file.

View File

@ -847,38 +847,42 @@ class Device(DeviceConfig, DevicePlugin):
self._card_b_prefix = None
# ------------------------------------------------------
def open(self, library_uuid):
def open(self, connected_device, library_uuid):
time.sleep(5)
self._main_prefix = self._card_a_prefix = self._card_b_prefix = None
if islinux:
try:
self.open_linux()
except DeviceError:
time.sleep(7)
self.open_linux()
if isfreebsd:
self._main_dev = self._card_a_dev = self._card_b_dev = None
try:
self.open_freebsd()
except DeviceError:
subprocess.Popen(["camcontrol", "rescan", "all"])
time.sleep(2)
self.open_freebsd()
if iswindows:
try:
self.open_windows()
except DeviceError:
time.sleep(7)
self.open_windows()
if isosx:
try:
self.open_osx()
except DeviceError:
time.sleep(7)
self.open_osx()
self.device_being_opened = connected_device
try:
if islinux:
try:
self.open_linux()
except DeviceError:
time.sleep(7)
self.open_linux()
if isfreebsd:
self._main_dev = self._card_a_dev = self._card_b_dev = None
try:
self.open_freebsd()
except DeviceError:
subprocess.Popen(["camcontrol", "rescan", "all"])
time.sleep(2)
self.open_freebsd()
if iswindows:
try:
self.open_windows()
except DeviceError:
time.sleep(7)
self.open_windows()
if isosx:
try:
self.open_osx()
except DeviceError:
time.sleep(7)
self.open_osx()
self.current_library_uuid = library_uuid
self.post_open_callback()
self.current_library_uuid = library_uuid
self.post_open_callback()
finally:
self.device_being_opened = None
def post_open_callback(self):
pass

View File

@ -162,7 +162,7 @@ class DeviceManager(Thread): # {{{
try:
dev.reset(detected_device=detected_device,
report_progress=self.report_progress)
dev.open(self.current_library_uuid)
dev.open(detected_device, self.current_library_uuid)
except OpenFeedback as e:
if dev not in self.ejected_devices:
self.open_feedback_msg(dev.get_gui_name(), e)