Skip to content

Commit

Permalink
impl capacity response parser.
Browse files Browse the repository at this point in the history
get the capacity response's size for further use.
  • Loading branch information
janseeger committed Sep 7, 2014
1 parent e087156 commit d81708b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
41 changes: 27 additions & 14 deletions lib/src/main/java/net/alphadev/usbstorage/bbb/BulkBlockDevice.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.alphadev.usbstorage.bbb;

import net.alphadev.usbstorage.api.BulkDevice;
import net.alphadev.usbstorage.scsi.answer.ReadCapacityResponse;
import net.alphadev.usbstorage.scsi.answer.ReadFormatCapacitiesEntry;
import net.alphadev.usbstorage.scsi.answer.ReadFormatCapacitiesHeader;
import net.alphadev.usbstorage.scsi.answer.StandardInquiryAnswer;
import net.alphadev.usbstorage.scsi.command.Inquiry;
import net.alphadev.usbstorage.scsi.command.ReadCapacity;
import net.alphadev.usbstorage.scsi.command.ReadFormatCapacities;
import net.alphadev.usbstorage.scsi.command.ScsiCommand;
import net.alphadev.usbstorage.scsi.command.TestUnitReady;
Expand All @@ -30,27 +32,21 @@ public BulkBlockDevice(BulkDevice usbBlockDevice) throws IOException {

setupInquiryPhase();
setupCapacityPhase();
testReady();
testUnitReady();
}

private void testReady() throws IOException {
private void testUnitReady() throws IOException {
send_mass_storage_command(new TestUnitReady());

CommandStatusWrapper csw = retrieve_mass_storage_answer();
if (CommandStatusWrapper.Status.COMMAND_PASSED != csw.getStatus()) {
throw new IllegalStateException("device signaled error state!");
}
checkDeviceStatus();
}

private void setupCapacityPhase() throws IOException {
try {
send_mass_storage_command(new ReadFormatCapacities());
byte[] answer = mAbstractBulkDevice.retrieve_data_packet(ReadFormatCapacitiesHeader.LENGTH);
ReadFormatCapacitiesHeader capacity = new ReadFormatCapacitiesHeader(answer);
CommandStatusWrapper csw = retrieve_mass_storage_answer();
if (CommandStatusWrapper.Status.COMMAND_PASSED != csw.getStatus()) {
throw new IllegalStateException("device signaled error state!");
}
checkDeviceStatus();

for (int i = 0; i < capacity.getCapacityEntryCount(); i++) {
byte[] capacityData = mAbstractBulkDevice.retrieve_data_packet(ReadFormatCapacitiesEntry.LENGTH);
Expand All @@ -68,6 +64,26 @@ private void setupCapacityPhase() throws IOException {
} catch (IllegalArgumentException ex) {
// do nothing as the read format capacities command is optional.
}

try {
send_mass_storage_command(new ReadCapacity());
byte[] answer = mAbstractBulkDevice.retrieve_data_packet(ReadCapacityResponse.LENGTH);
ReadCapacityResponse capacity = new ReadCapacityResponse(answer);

checkDeviceStatus();

mDeviceBoundaries = capacity.getNumberOfBlocks();
mBlockSize = capacity.getBlockSize();
} catch (IllegalArgumentException e) {
// whaaat!
}
}

private void checkDeviceStatus() {
CommandStatusWrapper csw = retrieve_mass_storage_answer();
if (CommandStatusWrapper.Status.COMMAND_PASSED != csw.getStatus()) {
throw new IllegalStateException("device signaled error state!");
}
}

private void setupInquiryPhase() throws IOException {
Expand All @@ -76,10 +92,7 @@ private void setupInquiryPhase() throws IOException {
byte[] answer = mAbstractBulkDevice.retrieve_data_packet(StandardInquiryAnswer.LENGTH);
new StandardInquiryAnswer(answer);

CommandStatusWrapper csw = retrieve_mass_storage_answer();
if (CommandStatusWrapper.Status.COMMAND_PASSED != csw.getStatus()) {
throw new IllegalStateException("device signaled error state!");
}
checkDeviceStatus();
}

private CommandStatusWrapper retrieve_mass_storage_answer() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
package net.alphadev.usbstorage.scsi.answer;

import static net.alphadev.usbstorage.util.BitStitching.convertToInt;

/**
* @author Jan Seeger <[email protected]>
*/
public class ReadCapacityResponse {
public static final int LENGTH = 8;

private int mBlockSize;
private int mNumberOfBlocks;

public ReadCapacityResponse(byte[] answer) {
mNumberOfBlocks = convertToInt(answer, 0);
mBlockSize = convertToInt(answer, 4);
}

public int getBlockSize() {
return mBlockSize;
}

public int getNumberOfBlocks() {
return mNumberOfBlocks;
}
}

0 comments on commit d81708b

Please sign in to comment.