Added support for unlocking the reader in both CLI and GUI. Version bump.

This commit is contained in:
Kovid Goyal 2007-01-30 02:26:50 +00:00
parent 6b0df0e357
commit 509fdf081b
8 changed files with 51 additions and 13 deletions

View File

@ -65,6 +65,7 @@ setup(
'console_scripts': [ \ 'console_scripts': [ \
'prs500 = libprs500.cli.main:main', \ 'prs500 = libprs500.cli.main:main', \
'lrf-meta = libprs500.lrf.meta:main', \ 'lrf-meta = libprs500.lrf.meta:main', \
'rtf-meta = libprs500.metadata.rtf:main', \
'makelrf = libprs500.lrf.makelrf:main'\ 'makelrf = libprs500.lrf.makelrf:main'\
], ],
'gui_scripts' : [ 'prs500-gui = libprs500.gui.main:main'] 'gui_scripts' : [ 'prs500-gui = libprs500.gui.main:main']

View File

@ -37,7 +37,7 @@ You may have to adjust the GROUP and the location of the rules file to
suit your distribution. suit your distribution.
""" """
__version__ = "0.3.7" __version__ = "0.3.8"
__docformat__ = "epytext" __docformat__ = "epytext"
__author__ = "Kovid Goyal <kovid@kovidgoyal.net>" __author__ = "Kovid Goyal <kovid@kovidgoyal.net>"

View File

@ -24,7 +24,7 @@ from optparse import OptionParser
from libprs500 import __version__ as VERSION from libprs500 import __version__ as VERSION
from libprs500.communicate import PRS500Device from libprs500.communicate import PRS500Device
from terminfo import TerminalController from terminfo import TerminalController
from libprs500.errors import ArgumentError, DeviceError from libprs500.errors import ArgumentError, DeviceError, DeviceLocked
MINIMUM_COL_WIDTH = 12 #: Minimum width of columns in ls output MINIMUM_COL_WIDTH = 12 #: Minimum width of columns in ls output
@ -185,6 +185,8 @@ def main():
parser.add_option("--log-packets", help="print out packet stream to stdout. "+\ parser.add_option("--log-packets", help="print out packet stream to stdout. "+\
"The numbers in the left column are byte offsets that allow the packet size to be read off easily.", "The numbers in the left column are byte offsets that allow the packet size to be read off easily.",
dest="log_packets", action="store_true", default=False) dest="log_packets", action="store_true", default=False)
parser.add_option("--unlock", help="Unlock device with KEY. For e.g. --unlock=1234", \
dest='key', default='-1')
parser.remove_option("-h") parser.remove_option("-h")
parser.disable_interspersed_args() # Allow unrecognized options parser.disable_interspersed_args() # Allow unrecognized options
options, args = parser.parse_args() options, args = parser.parse_args()
@ -195,7 +197,7 @@ def main():
command = args[0] command = args[0]
args = args[1:] args = args[1:]
dev = PRS500Device(log_packets=options.log_packets) dev = PRS500Device(key=options.key, log_packets=options.log_packets)
try: try:
if command == "df": if command == "df":
total = dev.total_space(end_session=False) total = dev.total_space(end_session=False)
@ -301,6 +303,8 @@ def main():
parser.print_help() parser.print_help()
if dev.handle: dev.close() if dev.handle: dev.close()
return 1 return 1
except DeviceLocked:
print >> sys.stderr, "The device is locked. Use the --unlock option"
except (ArgumentError, DeviceError), e: except (ArgumentError, DeviceError), e:
print >>sys.stderr, e print >>sys.stderr, e
return 1 return 1

View File

@ -188,8 +188,9 @@ class PRS500Device(Device):
return run_session return run_session
def __init__(self, log_packets=False, report_progress=None) : def __init__(self, key='-1', log_packets=False, report_progress=None) :
""" """
@param key: The key to unlock the device
@param log_packets: If true the packet stream to/from the device is logged @param log_packets: If true the packet stream to/from the device is logged
@param report_progress: Function that is called with a % progress @param report_progress: Function that is called with a % progress
(number between 0 and 100) for various tasks (number between 0 and 100) for various tasks
@ -202,6 +203,11 @@ class PRS500Device(Device):
self.handle = None self.handle = None
self.log_packets = log_packets self.log_packets = log_packets
self.report_progress = report_progress 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
def reconnect(self): def reconnect(self):
""" Only recreates the device node and deleted the connection handle """ """ Only recreates the device node and deleted the connection handle """
@ -265,9 +271,9 @@ class PRS500Device(Device):
unknown = 2)) unknown = 2))
if res.code != 0: if res.code != 0:
raise ProtocolError("Unable to set bulk size.") raise ProtocolError("Unable to set bulk size.")
self.send_validated_command(UnlockDevice(key=0x312d)) res = self.send_validated_command(UnlockDevice(key=self.key))#0x312d))
if res.code != 0: if res.code != 0:
raise ProtocolError("Unlocking of device not implemented. Remove locking and retry.") raise DeviceLocked()
res = self.send_validated_command(SetTime()) res = self.send_validated_command(SetTime())
if res.code != 0: if res.code != 0:
raise ProtocolError("Could not set time on device") raise ProtocolError("Could not set time on device")

View File

@ -42,6 +42,11 @@ class DeviceBusy(ProtocolError):
ProtocolError.__init__(self, "Device is in use by another application:"\ ProtocolError.__init__(self, "Device is in use by another application:"\
"\nUnderlying error:" + str(uerr)) "\nUnderlying error:" + str(uerr))
class DeviceLocked(ProtocolError):
""" Raised when device has been locked """
def __init__(self):
ProtocolError.__init__(self, "Device is locked")
class PacketError(ProtocolError): class PacketError(ProtocolError):
""" Errors with creating/interpreting packets """ """ Errors with creating/interpreting packets """

View File

@ -12,6 +12,9 @@
## You should have received a copy of the GNU General Public License along ## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc., ## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Backend that implements storage of ebooks in an sqlite database.
"""
import sqlite3 as sqlite import sqlite3 as sqlite
import os import os
from zlib import compress, decompress from zlib import compress, decompress

View File

@ -22,8 +22,8 @@ import tempfile
from PyQt4.QtCore import Qt, SIGNAL, QObject, QCoreApplication, \ from PyQt4.QtCore import Qt, SIGNAL, QObject, QCoreApplication, \
QSettings, QVariant, QSize, QEventLoop, QString, \ QSettings, QVariant, QSize, QEventLoop, QString, \
QBuffer, QIODevice, QModelIndex QBuffer, QIODevice, QModelIndex
from PyQt4.QtGui import QPixmap, QErrorMessage, \ from PyQt4.QtGui import QPixmap, QErrorMessage, QLineEdit, \
QMessageBox, QFileDialog, QIcon, QDialog QMessageBox, QFileDialog, QIcon, QDialog, QInputDialog
from PyQt4.Qt import qDebug, qFatal, qWarning, qCritical from PyQt4.Qt import qDebug, qFatal, qWarning, qCritical
from libprs500.communicate import PRS500Device as device from libprs500.communicate import PRS500Device as device
@ -404,7 +404,10 @@ class Main(QObject, Ui_MainWindow):
QObject.__init__(self) QObject.__init__(self)
Ui_MainWindow.__init__(self) Ui_MainWindow.__init__(self)
self.dev = device(report_progress=self.progress, log_packets=log_packets) self.key = '-1'
self.log_packets = log_packets
self.dev = device(key=self.key, report_progress=self.progress, \
log_packets=self.log_packets)
self.setupUi(window) self.setupUi(window)
self.card = None self.card = None
self.window = window self.window = window
@ -532,6 +535,17 @@ class Main(QObject, Ui_MainWindow):
self.dev.reconnect() self.dev.reconnect()
self.thread().msleep(100) self.thread().msleep(100)
return self.establish_connection() return self.establish_connection()
except DeviceLocked:
key, ok = QInputDialog.getText(self.window, 'Unlock device', \
'Key to unlock device:', QLineEdit.Password)
self.key = str(key)
if not ok:
self.status('Device locked')
self.window.setCursor(Qt.ArrowCursor)
return
else:
self.dev.key = key
return self.establish_connection()
except ProtocolError, e: except ProtocolError, e:
traceback.print_exc(e) traceback.print_exc(e)
qFatal("Unable to connect to device. Please try unplugging and"+\ qFatal("Unable to connect to device. Please try unplugging and"+\

View File

@ -415,12 +415,17 @@ class SetBulkSize(Command):
self.chunk_size = chunk_size self.chunk_size = chunk_size
self.unknown = unknown self.unknown = unknown
class UnlockDevice(ShortCommand): class UnlockDevice(Command):
""" Unlock the device """ """ Unlock the device """
NUMBER = 0x106 #: Command number NUMBER = 0x106 #: Command number
def __init__(self, key=0x312d): key = stringfield(8, start=16) #: The key defaults to -1
ShortCommand.__init__(self, \
number=UnlockDevice.NUMBER, type=0x01, command=key) def __init__(self, key='-1\0\0\0\0\0\0'):
Command.__init__(self, 24)
self.number = UnlockDevice.NUMBER
self.type = 0x01
self.length = 8
self.key = key
class LongCommand(Command): class LongCommand(Command):