Added proper handling of sessions. Thanks to scotty for pointing that out.

This commit is contained in:
Kovid Goyal 2007-02-10 18:31:02 +00:00
parent 487764bcc9
commit 51b5017b43
2 changed files with 16 additions and 9 deletions

View File

@ -165,10 +165,14 @@ class PRS500Device(Device):
try:
if not dev.handle:
dev.open()
if not dev.in_session:
dev.send_validated_command(BeginEndSession(end=False))
dev.in_session = True
res = func(*args, **kwargs)
except ArgumentError:
if not kwargs.has_key("end_session") or kwargs["end_session"]:
dev.send_validated_command(EndSession())
dev.send_validated_command(BeginEndSession(end=True))
dev.in_session = False
raise
except USBError, err:
if "No such device" in str(err):
@ -183,7 +187,8 @@ class PRS500Device(Device):
dev.close()
raise
if not kwargs.has_key("end_session") or kwargs["end_session"]:
dev.send_validated_command(EndSession())
dev.send_validated_command(BeginEndSession(end=True))
dev.in_session = False
return res
return run_session
@ -200,7 +205,8 @@ class PRS500Device(Device):
Device.__init__(self)
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.handle = None
self.in_session = False
self.log_packets = log_packets
self.report_progress = report_progress
if len(key) > 8:
@ -286,6 +292,7 @@ class PRS500Device(Device):
except Exception, err:
print >> sys.stderr, err
self.handle, self.device = None, None
self.in_session = False
def _send_command(self, command, response_type=Response, timeout=1000):
"""

View File

@ -386,15 +386,15 @@ class DirClose(ShortCommand):
ShortCommand.__init__(self, number=DirClose.NUMBER, type=0x01,
command=_id)
class EndSession(ShortCommand):
class BeginEndSession(ShortCommand):
"""
Ask device to change status to 'USB connected' i.e., tell the
device that the present sequence of commands is complete
Ask device to either start or end a session.
"""
NUMBER = 0x1 #: Command number
def __init__(self):
NUMBER = 0x01 #: Command number
def __init__(self, end=True):
command = 0x00 if end else 0x01
ShortCommand.__init__(self, \
number=EndSession.NUMBER, type=0x01, command=0x00)
number=BeginEndSession.NUMBER, type=0x01, command=command)
class GetUSBProtocolVersion(ShortCommand):
""" Get USB Protocol version used by device """