status and state, maybe progress?

This commit is contained in:
rusefi 2020-07-05 18:12:37 -04:00
parent 326b01c475
commit fd279e266c
7 changed files with 122 additions and 74 deletions

View File

@ -4,11 +4,11 @@ import com.rusefi.dfu.commands.DfuCommandGetStatus;
public class DfuConnectionUtil {
public static void waitStatus(DfuLogic.Logger logger, DfuConnection device) {
DfuCommandGetStatus.State state = DfuCommandGetStatus.read(logger, device);
System.out.println(" state " + state);
while (state == DfuCommandGetStatus.State.DFU_DOWNLOAD_BUSY || state == DfuCommandGetStatus.State.DFU_ERROR) {
DfuCommandGetStatus.DeviceStatus state = DfuCommandGetStatus.read(logger, device);
logger.info("First state " + state);
while (state.getState() == DfuCommandGetStatus.State.DFU_DOWNLOAD_BUSY || state.getState() == DfuCommandGetStatus.State.DFU_ERROR) {
state = DfuCommandGetStatus.read(logger, device);
System.out.println(" state " + state);
logger.info("Loop state " + state);
}
}

View File

@ -52,11 +52,12 @@ public class DfuLogic {
}
public static void startup(Logger logger, DfuConnection device) {
DfuCommandGetStatus.State state = DfuCommandGetStatus.read(logger, device);
DfuCommandGetStatus.DeviceStatus state = DfuCommandGetStatus.read(logger, device);
logger.info("DFU state: " + state);
switch (state) {
switch (state.getState()) {
case DFU_IDLE:
// best status
logger.info("startup status " + state.getStatus());
break;
case DFU_ERROR:
DfuCommandClearStatus.execute(device);
@ -73,8 +74,8 @@ public class DfuLogic {
throw new IllegalStateException("Unexpected state " + state);
}
state = DfuCommandGetStatus.read(logger, device);
if (state != DfuCommandGetStatus.State.DFU_IDLE)
throw new IllegalStateException("Not idle on start-up");
if (state.getState() != DfuCommandGetStatus.State.DFU_IDLE)
throw new IllegalStateException("Not idle on start-up: " + state.getState());
}
public interface Logger {

View File

@ -6,6 +6,7 @@ import cz.jaybee.intelhex.Parser;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
public class HexImage implements BinaryImage {
@ -19,8 +20,9 @@ public class HexImage implements BinaryImage {
this.maxOffset = maxOffset;
}
static HexImage loadHexToBuffer(InputStream is, FlashRange range) throws IntelHexException, IOException {
byte[] image = new byte[range.getTotalLength()];
static HexImage loadHexToBuffer(InputStream is, FlashRange flashRange) throws IntelHexException, IOException {
Objects.requireNonNull(flashRange, "flashRange");
byte[] image = new byte[flashRange.getTotalLength()];
// create IntelHexParserObject
Parser ihp = new Parser(is);
@ -37,12 +39,12 @@ public class HexImage implements BinaryImage {
maxOffset.set((int) Math.max(maxOffset.get(), address + data.length));
if (address < range.getBaseAddress() || address + data.length > range.getBaseAddress() + range.getTotalLength())
throw new IllegalStateException(String.format("Image data out of range: %x@%x not withiin %s",
if (address < flashRange.getBaseAddress() || address + data.length > flashRange.getBaseAddress() + flashRange.getTotalLength())
throw new IllegalStateException(String.format("Image data out of range: %x@%x not within %s",
data.length,
address,
range.toString()));
System.arraycopy(data, 0, image, (int) (address - range.getBaseAddress()), data.length);
flashRange.toString()));
System.arraycopy(data, 0, image, (int) (address - flashRange.getBaseAddress()), data.length);
}
@Override

View File

@ -31,8 +31,8 @@ public class DfuDeviceLocator {
UsbDeviceConnection connection = usbManager.openDevice(dfuDevice);
UsbInterface intf = dfuDevice.getInterface(0);
connection.claimInterface(intf, true);
//UsbInterface intf = dfuDevice.getInterface(0);
connection.claimInterface(usbInterface, true);
byte[] rawDescs = connection.getRawDescriptors();
// todo: need proper handling of this rawDesc area since I have no clue what's the format

View File

@ -6,24 +6,28 @@ import com.rusefi.dfu.DfuConnectionUtil;
import com.rusefi.dfu.DfuLogic;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;
public class DfuCommandGetStatus {
private static final int PACKET_SIZE = 6;
public static State read(DfuLogic.Logger logger, DfuConnection session) {
private final static AtomicInteger STATUS_COUNTER = new AtomicInteger();
public static DeviceStatus read(DfuLogic.Logger logger, DfuConnection session) {
ByteBuffer buffer = ByteBuffer.allocateDirect(PACKET_SIZE);
int count = session.receiveData(DfuCommmand.GETSTATUS, (short) 0, buffer);
if (count != PACKET_SIZE)
return State.DFU_ERROR;
return new DeviceStatus(null, State.DFU_ERROR);
buffer.rewind();
Status status = Status.valueOf(buffer.get()); // status
int timeout = buffer.get();
timeout = timeout | (buffer.get() << 8);
timeout = timeout | (buffer.get() << 8);
System.out.println("During " + status + " timeout=" + timeout);
byte state = buffer.get();
byte stateCode = buffer.get();
DfuConnectionUtil.sleep(logger, timeout);
return State.valueOf(state);
State state = State.valueOf(stateCode);
logger.info(STATUS_COUNTER.incrementAndGet() + " GETSTATUS " + status + " timeout=" + timeout + " " + state);
return new DeviceStatus(status, state);
}
public enum Status {
@ -88,4 +92,30 @@ public class DfuCommandGetStatus {
return null;
}
}
public static class DeviceStatus {
private final Status status;
private final State state;
public DeviceStatus(Status status, State state) {
this.status = status;
this.state = state;
}
public Status getStatus() {
return status;
}
public State getState() {
return state;
}
@Override
public String toString() {
return "DeviceStatus{" +
"status=" + status +
", state=" + state +
'}';
}
}
}

View File

@ -65,7 +65,7 @@ public class DfuDeviceLocator {
DeviceHandle deviceHandle = open(device);
int transferSize = 0;
FlashRange flashRange = null;
FlashRange flashRange;
for (int configurationIndex = 0; configurationIndex < numConfigurations; configurationIndex++) {
ConfigDescriptor config = new ConfigDescriptor();
@ -79,58 +79,19 @@ public class DfuDeviceLocator {
byte numInterfaces = config.bNumInterfaces();
appendInfo(numInterfaces + " interface(s)");
for (int interfaceIndex = 0; interfaceIndex < numInterfaces; interfaceIndex++) {
Interface iface = config.iface()[interfaceIndex];
transferSize = scanTransferSize(config, numInterfaces);
for (int s = 0; s < iface.numAltsetting(); s++) {
InterfaceDescriptor setting = iface.altsetting()[s];
System.out.println("setting " + setting);
ByteBuffer extra = setting.extra();
if (extra.limit() > 2) {
int len = extra.get();
byte type = extra.get();
if (type == DfuLogic.USB_DT_DFU) {
System.out.println(len + " " + type);
extra.get(); // bmAttributes
extra.get(); // wDetachTimeOut
extra.get();
transferSize = swap16(extra.getShort());
System.out.println("transferSize " + transferSize);
}
}
}
}
scanDescriptor(deviceHandle, config, numInterfaces);
for (int interfaceIndex = 0; interfaceIndex < numInterfaces; interfaceIndex++) {
Interface iface = config.iface()[interfaceIndex];
for (int s = 0; s < iface.numAltsetting(); s++) {
InterfaceDescriptor setting = iface.altsetting()[s];
appendInfo("Interface #" + interfaceIndex + " setting #" + s + ":");
byte interfaceNumber = setting.bInterfaceNumber();
appendInfo(String.format("Setting %d: %x %x class %x, subclass %x, protocol: %x", s,
interfaceNumber,
setting.iInterface(),
setting.bInterfaceClass(),
setting.bInterfaceSubClass(),
setting.bInterfaceProtocol()
));
String stringDescriptor = LibUsb.getStringDescriptor(deviceHandle, setting.iInterface());
appendInfo("Descriptor " + stringDescriptor);
}
}
for (int interfaceIndex = 0; interfaceIndex < numInterfaces; interfaceIndex++) {
Interface iface = config.iface()[interfaceIndex];
for (int s = 0; s < iface.numAltsetting(); s++) {
InterfaceDescriptor setting = iface.altsetting()[s];
log.info("Settings " + setting);
byte interfaceNumber = setting.bInterfaceNumber();
log.info("Settings " + setting + " " + interfaceNumber);
if (setting.bInterfaceClass() == (byte) DfuLogic.USB_CLASS_APP_SPECIFIC &&
setting.bInterfaceSubClass() == DfuLogic.DFU_SUBCLASS) {
@ -138,19 +99,21 @@ public class DfuDeviceLocator {
String stringDescriptor = LibUsb.getStringDescriptor(deviceHandle, setting.iInterface());
log.info("StringDescriptor: " + stringDescriptor);
if (stringDescriptor.contains(DfuLogic.FLASH_TAG))
if (stringDescriptor.contains(DfuLogic.FLASH_TAG)) {
flashRange = DfuSeFlashDescriptor.parse(stringDescriptor);
result = LibUsb.claimInterface(deviceHandle, interfaceNumber);
if (result != LibUsb.SUCCESS) {
throw new LibUsbException("claimInterface", result);
result = LibUsb.claimInterface(deviceHandle, interfaceNumber);
if (result != LibUsb.SUCCESS) {
throw new LibUsbException("claimInterface", result);
}
USBDfuConnection session = new USBDfuConnection(deviceHandle, interfaceNumber, transferSize, flashRange);
System.out.printf("info:\n" + usbInfo);
return session;
}
USBDfuConnection session = new USBDfuConnection(deviceHandle, interfaceNumber, transferSize, flashRange);
System.out.printf("info:\n" + usbInfo);
return session;
}
}
}
@ -158,6 +121,56 @@ public class DfuDeviceLocator {
return null;
}
private static void scanDescriptor(DeviceHandle deviceHandle, ConfigDescriptor config, byte numInterfaces) {
for (int interfaceIndex = 0; interfaceIndex < numInterfaces; interfaceIndex++) {
Interface iface = config.iface()[interfaceIndex];
for (int s = 0; s < iface.numAltsetting(); s++) {
InterfaceDescriptor setting = iface.altsetting()[s];
appendInfo("Interface #" + interfaceIndex + " setting #" + s + ":");
byte interfaceNumber = setting.bInterfaceNumber();
appendInfo(String.format("Setting %d: %x %x class %x, subclass %x, protocol: %x", s,
interfaceNumber,
setting.iInterface(),
setting.bInterfaceClass(),
setting.bInterfaceSubClass(),
setting.bInterfaceProtocol()
));
String stringDescriptor = LibUsb.getStringDescriptor(deviceHandle, setting.iInterface());
appendInfo("Descriptor " + stringDescriptor);
}
}
}
private static int scanTransferSize(ConfigDescriptor config, byte numInterfaces) {
int transferSize = 0;
for (int interfaceIndex = 0; interfaceIndex < numInterfaces; interfaceIndex++) {
Interface iface = config.iface()[interfaceIndex];
for (int s = 0; s < iface.numAltsetting(); s++) {
InterfaceDescriptor setting = iface.altsetting()[s];
System.out.println("setting " + setting);
ByteBuffer extra = setting.extra();
if (extra.limit() > 2) {
int len = extra.get();
byte type = extra.get();
if (type == DfuLogic.USB_DT_DFU) {
System.out.println(len + " " + type);
extra.get(); // bmAttributes
extra.get(); // wDetachTimeOut
extra.get();
transferSize = swap16(extra.getShort());
System.out.println("transferSize " + transferSize);
}
}
}
}
return transferSize;
}
private static void appendInfo(String message) {
log.info(message);
usbInfo.append(message).append("\n");

View File

@ -7,6 +7,7 @@ import org.usb4java.DeviceHandle;
import org.usb4java.LibUsb;
import java.nio.ByteBuffer;
import java.util.Objects;
public class USBDfuConnection implements DfuConnection {
private final DeviceHandle deviceHandle;
@ -17,6 +18,7 @@ public class USBDfuConnection implements DfuConnection {
public USBDfuConnection(DeviceHandle deviceHandle, byte interfaceNumber, int transferSize, FlashRange flashRange) {
if (transferSize == 0)
throw new IllegalArgumentException("transfer size not detected");
Objects.requireNonNull(flashRange, "flashRange");
this.deviceHandle = deviceHandle;
this.interfaceNumber = interfaceNumber;
this.transferSize = transferSize;