Package libprs500 :: Module communicate :: Class PRS500Device
[hide private]
[frames] | no frames]

Class PRS500Device

source code


Contains the logic for performing various tasks on the reader.

The implemented tasks are:
  1. Getting information about the device
  2. Getting a file from the device
  3. Listing of directories. See the list method.


Instance Methods [hide private]
  safe(func)
Decorator that wraps a call to func to ensure that exceptions are handled correctly.
  __init__(self, log_packets=False)
  open(self)
Claim an interface on the device for communication.
  close(self)
Release device interface
  _send_command(self, command, response_type=<class 'libprs500.prstypes.Response'>, timeout=100)
Send command to device and return its response.
  _send_validated_command(self, command, cnumber=None, response_type=<class 'libprs500.prstypes.Response'>, timeout=100)
Wrapper around _send_command that checks if the Response.rnumber == cnumber or command.number if cnumber==None.
  _bulk_write(self, data, packet_size=0x1000)
Send data to device via a bulk transfer.
  _bulk_read(self, bytes, command_number=0x00, packet_size=4096, data_type=<class 'libprs500.prstypes.Answer'>)
Read in bytes bytes via a bulk transfer in packets of size ≤ packet_size
  get_device_information(*args, **kwargs)
Ask device for device information.
  path_properties(*args, **kwargs)
Send command asking device for properties of path.
  get_file(*args, **kwargs)
Read the file at path on the device and write it to outfile.
  list(*args, **kwargs)
Return a listing of path.
  available_space(*args, **kwargs)
Get free space available on the mountpoints:
  _exists(self, path)
Return (True, FileProperties) if path exists or (False, None) otherwise
  touch(*args, **kwargs)
Create a file at path
  put_file(*args, **kwargs)
  del_file(*args, **kwargs)
  mkdir(*args, **kwargs)
  rm(*args, **kwargs)
Delete path from device if it is a file or an empty directory

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__


Class Methods [hide private]
  _validate_response(cls, res, type=0x00, number=0x00)
Raise a ProtocolError if the type and number of res is not the same as type and number.

Class Variables [hide private]
  SONY_VENDOR_ID = 1356
SONY Vendor Id
  PRS500_PRODUCT_ID = 667
Product Id for the PRS-500
  PRS500_INTERFACE_ID = 0
The interface we use to talk to the device
  PRS500_BULK_IN_EP = 129
Endpoint for Bulk reads
  PRS500_BULK_OUT_EP = 2
Endpoint for Bulk writes

Properties [hide private]

Inherited from object: __class__


Method Details [hide private]

safe(func)

source code 

Decorator that wraps a call to func to ensure that exceptions are handled correctly.

As a convenience, safe automatically sends the a USBConnect after calling func, unless func has a keyword argument named end_session set to False.

An ArgumentError will cause the USBConnect command to be sent to the device, unless end_session is set to False. An usb.USBError will cause the library to release control of the USB interface via a call to close.

__init__(self, log_packets=False)
(Constructor)

source code 
Parameters:
  • log_packets - If true the packet stream to/from the device is logged
Overrides: object.__init__

_validate_response(cls, res, type=0x00, number=0x00)
Class Method

source code 
Raise a ProtocolError if the type and number of res is not the same as type and number.
Decorators:

open(self)

source code 
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 sequenceof initialization commands.

To Do:

  • Implement unlocking of the device
  • Check this on Mac OSX

close(self)

source code 
Release device interface

_send_command(self, command, response_type=<class 'libprs500.prstypes.Response'>, timeout=100)

source code 
Send command to device and return its response.
Parameters:
  • command - an object of type Command or one of its derived classes
  • response_type - an object of type 'type'. The return packet from the device is returned as an object of type response_type.
  • timeout - the time to wait for a response from the device, in milliseconds. If there is no response, a usb.USBError is raised.

_send_validated_command(self, command, cnumber=None, response_type=<class 'libprs500.prstypes.Response'>, timeout=100)

source code 
Wrapper around _send_command that checks if the Response.rnumber == cnumber or command.number if cnumber==None. Also check that Response.type == Command.type.

_bulk_write(self, data, packet_size=0x1000)

source code 
Send data to device via a bulk transfer.
Parameters:
  • packet_size - Size of packets to be sent to device. data is broken up into packets to be sent to device.

_bulk_read(self, bytes, command_number=0x00, packet_size=4096, data_type=<class 'libprs500.prstypes.Answer'>)

source code 
Read in bytes bytes via a bulk transfer in packets of size ≤ packet_size
Parameters:
  • data_type - an object of type type. The data packet is returned as an object of type data_type.
Returns:
A list of packets read from the device. Each packet is of type data_type

get_device_information(*args, **kwargs)

source code 
Ask device for device information. See DeviceInfoQuery.
Returns:
(device name, device version, software version on device, mime type)
Decorators:
  • @safe

path_properties(*args, **kwargs)

source code 
Send command asking device for properties of path. Return FileProperties.
Decorators:
  • @safe

get_file(*args, **kwargs)

source code 

Read the file at path on the device and write it to outfile. For the logic see _get_file.

The data is fetched in chunks of size ≤ 32K. Each chunk is make of packets of size ≤ 4K. See FileOpen, FileRead and FileClose for details on the command packets used.
Parameters:
  • outfile - file object like sys.stdout or the result of an open call
Decorators:
  • @safe

list(*args, **kwargs)

source code 
Return a listing of path. See the code for details. See DirOpen, DirRead and DirClose for details on the command packets used.
Parameters:
  • path (string) - The path to list
  • recurse (boolean) - If true do a recursive listing
Returns:
A list of tuples. The first element of each tuple is a path. The second element is a list of Files. The path is the path we are listing, the Files are the files/directories in that path. If it is a recursive list, then the first element will be (path, children), the next will be (child, its children) and so on. If it is not recursive the length of the outermost list will be 1.
Decorators:
  • @safe

available_space(*args, **kwargs)

source code 
Get free space available on the mountpoints:
  1. /Data/ Device memory
  2. a:/ Memory Stick
  3. b:/ SD Card
Returns:
A list of tuples. Each tuple has form ("location", free space, total space)
Decorators:
  • @safe

_exists(self, path)

source code 
Return (True, FileProperties) if path exists or (False, None) otherwise

touch(*args, **kwargs)

source code 
Create a file at path
Decorators:
  • @safe

To Do: Update file modification time if it exists

put_file(*args, **kwargs)

source code 
None
Decorators:
  • @safe

del_file(*args, **kwargs)

source code 
None
Decorators:
  • @safe

mkdir(*args, **kwargs)

source code 
None
Decorators:
  • @safe

rm(*args, **kwargs)

source code 
Delete path from device if it is a file or an empty directory
Decorators:
  • @safe

Class Variable Details [hide private]

SONY_VENDOR_ID

SONY Vendor Id
Value:
1356                                                                  
      

PRS500_PRODUCT_ID

Product Id for the PRS-500
Value:
667                                                                   
      

PRS500_INTERFACE_ID

The interface we use to talk to the device
Value:
0                                                                     
      

PRS500_BULK_IN_EP

Endpoint for Bulk reads
Value:
129                                                                   
      

PRS500_BULK_OUT_EP

Endpoint for Bulk writes
Value:
2