Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dedicated class for WebSocket Errors #941

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/qz/communication/DeviceException.java

This file was deleted.

12 changes: 6 additions & 6 deletions src/qz/communication/DeviceIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ public interface DeviceIO {
String getProductId();


void open() throws DeviceException;
void open() throws UsbException;

boolean isOpen();

void close() throws DeviceException;
void close() throws UsbException;


void setStreaming(boolean streaming);

boolean isStreaming();


byte[] readData(int responseSize, Byte exchangeConfig) throws DeviceException;
byte[] readData(int responseSize, Byte exchangeConfig) throws UsbException;

void sendData(byte[] data, Byte exchangeConfig) throws DeviceException;
void sendData(byte[] data, Byte exchangeConfig) throws UsbException;


byte[] getFeatureReport(int responseSize, Byte reportId) throws DeviceException;
byte[] getFeatureReport(int responseSize, Byte reportId) throws UsbException;

void sendFeatureReport(byte[] data, Byte reportId) throws DeviceException;
void sendFeatureReport(byte[] data, Byte reportId) throws UsbException;
}
23 changes: 12 additions & 11 deletions src/qz/communication/H4J_HidIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ public class H4J_HidIO implements DeviceIO {
private boolean streaming;


public H4J_HidIO(DeviceOptions dOpts) throws DeviceException {
public H4J_HidIO(UsbOptions dOpts) throws UsbException {
this(H4J_HidUtilities.findDevice(dOpts));
}

public H4J_HidIO(HidDevice device) throws DeviceException {
public H4J_HidIO(HidDevice device) throws UsbException {
if (device == null) {
throw new DeviceException("HID device could not be found");
// FIXME: This shouldn't generate a general exception up the stack, but it does
throw new UsbException("HID device could not be found");
}

this.device = device;
Expand Down Expand Up @@ -49,45 +50,45 @@ public String getProductId() {
return UsbUtil.toHexString(device.getProductId());
}

public byte[] readData(int responseSize, Byte unused) throws DeviceException {
public byte[] readData(int responseSize, Byte unused) throws UsbException {
byte[] response = new byte[responseSize];

int read = device.read(response);
if (read == -1) {
throw new DeviceException("Failed to read from device");
throw new UsbException("Failed to read from device");
}

return response;
}

public void sendData(byte[] data, Byte reportId) throws DeviceException {
public void sendData(byte[] data, Byte reportId) throws UsbException {
if (reportId == null) { reportId = (byte)0x00; }

int wrote = device.write(data, data.length, reportId);
if (wrote == -1) {
throw new DeviceException("Failed to write to device");
throw new UsbException("Failed to write to device");
}
}

public byte[] getFeatureReport(int responseSize, Byte reportId) throws DeviceException {
public byte[] getFeatureReport(int responseSize, Byte reportId) throws UsbException {
if (reportId == null) { reportId = (byte)0x00; }
byte[] response = new byte[responseSize];

int read = device.getFeatureReport(response, reportId);
if (read == -1) {
throw new DeviceException("Failed to read from device");
throw new UsbException("Failed to read from device");
}

return response;

}

public void sendFeatureReport(byte[] data, Byte reportId) throws DeviceException {
public void sendFeatureReport(byte[] data, Byte reportId) throws UsbException {
if (reportId == null) { reportId = (byte)0x00; }

int wrote = device.sendFeatureReport(data, reportId);
if (wrote == -1) {
throw new DeviceException("Failed to write to device");
throw new UsbException("Failed to write to device");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/qz/communication/H4J_HidUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static JSONArray getHidDevicesJSON() throws JSONException {
return devicesJSON;
}

public static HidDevice findDevice(DeviceOptions dOpts) {
public static HidDevice findDevice(UsbOptions dOpts) {
if (dOpts.getVendorId() == null) {
throw new IllegalArgumentException("Vendor ID cannot be null");
}
Expand Down
24 changes: 12 additions & 12 deletions src/qz/communication/PJHA_HidIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public class PJHA_HidIO implements DeviceIO {
private boolean streaming;


public PJHA_HidIO(DeviceOptions dOpts) throws DeviceException {
public PJHA_HidIO(UsbOptions dOpts) throws UsbException {
this(PJHA_HidUtilities.findDevice(dOpts));
}

public PJHA_HidIO(HidDeviceInfo deviceInfo) throws DeviceException {
public PJHA_HidIO(HidDeviceInfo deviceInfo) throws UsbException {
if (deviceInfo == null) {
throw new DeviceException("HID device could not be found");
throw new UsbException("HID device could not be found");
}

this.deviceInfo = deviceInfo;
Expand All @@ -46,7 +46,7 @@ public synchronized boolean add(byte[] e) {
};
}

public void open() throws DeviceException {
public void open() throws UsbException {
if (!isOpen()) {
try {
device = PureJavaHidApi.openDevice(deviceInfo);
Expand All @@ -60,7 +60,7 @@ public void onInputReport(HidDevice source, byte id, byte[] data, int len) {
});
}
catch(IOException ex) {
throw new DeviceException(ex);
throw new UsbException(ex);
}
}
}
Expand All @@ -85,7 +85,7 @@ public String getProductId() {
return UsbUtil.toHexString(deviceInfo.getProductId());
}

public byte[] readData(int responseSize, Byte unused) throws DeviceException {
public byte[] readData(int responseSize, Byte unused) throws UsbException {
byte[] response = new byte[responseSize];
if (dataBuffer.isEmpty()) {
return new byte[0]; //no data received yet
Expand All @@ -101,30 +101,30 @@ public byte[] readData(int responseSize, Byte unused) throws DeviceException {
return response;
}

public void sendData(byte[] data, Byte reportId) throws DeviceException {
public void sendData(byte[] data, Byte reportId) throws UsbException {
if (reportId == null) { reportId = (byte)0x00; }

int wrote = device.setOutputReport(reportId, data, data.length);
if (wrote == -1) {
throw new DeviceException("Failed to write to device");
throw new UsbException("Failed to write to device");
}
}

public byte[] getFeatureReport(int responseSize, Byte unused) throws DeviceException {
public byte[] getFeatureReport(int responseSize, Byte unused) throws UsbException {
byte[] response = new byte[responseSize];
int read = device.getFeatureReport(response, responseSize);
if (read == -1) {
throw new DeviceException("Failed to read from device");
throw new UsbException("Failed to read from device");
}
return response;
}

public void sendFeatureReport(byte[] data, Byte reportId) throws DeviceException {
public void sendFeatureReport(byte[] data, Byte reportId) throws UsbException {
if (reportId == null) { reportId = (byte)0x00; }
int wrote = device.setFeatureReport(reportId, data, data.length);

if (wrote == -1) {
throw new DeviceException("Failed to write to device");
throw new UsbException("Failed to write to device");
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/qz/communication/PJHA_HidUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static JSONArray getHidDevicesJSON() throws JSONException {
return devicesJSON;
}

public static HidDeviceInfo findDevice(DeviceOptions dOpts) {
public static HidDeviceInfo findDevice(UsbOptions dOpts) {
if (dOpts.getVendorId() == null) {
throw new IllegalArgumentException("Vendor ID cannot be null");
}
Expand Down
13 changes: 13 additions & 0 deletions src/qz/communication/UsbException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package qz.communication;

public class UsbException extends Exception {

public UsbException(String message) {
super(message);
}

public UsbException(Throwable cause) {
super(cause);
}

}
38 changes: 19 additions & 19 deletions src/qz/communication/UsbIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public class UsbIO implements DeviceIO {
private boolean streaming;


public UsbIO(DeviceOptions dOpts) throws DeviceException {
public UsbIO(UsbOptions dOpts) throws UsbException {
UsbDevice device = UsbUtilities.findDevice(dOpts.getVendorId().shortValue(), dOpts.getProductId().shortValue());

if (device == null) {
throw new DeviceException("USB device could not be found");
throw new UsbException("USB device could not be found");
}
if (dOpts.getInterfaceId() == null) {
throw new IllegalArgumentException("Device interface cannot be null");
Expand All @@ -27,7 +27,7 @@ public UsbIO(DeviceOptions dOpts) throws DeviceException {
this.iface = device.getActiveUsbConfiguration().getUsbInterface(dOpts.getInterfaceId());
}

public void open() throws DeviceException {
public void open() throws UsbException {
try {
iface.claim(new UsbInterfacePolicy() {
@Override
Expand All @@ -37,8 +37,8 @@ public boolean forceClaim(UsbInterface usbInterface) {
}
});
}
catch(UsbException e) {
throw new DeviceException(e);
catch(javax.usb.UsbException e) {
throw new UsbException(e);
}
}

Expand Down Expand Up @@ -66,32 +66,32 @@ public String getInterface() {
return UsbUtil.toHexString(iface.getUsbInterfaceDescriptor().iInterface());
}

public byte[] readData(int responseSize, Byte endpoint) throws DeviceException {
public byte[] readData(int responseSize, Byte endpoint) throws UsbException {
try {
byte[] response = new byte[responseSize];
exchangeData(endpoint, response);
return response;
}
catch(UsbException e) {
throw new DeviceException(e);
catch(javax.usb.UsbException e) {
throw new UsbException(e);
}
}

public void sendData(byte[] data, Byte endpoint) throws DeviceException {
public void sendData(byte[] data, Byte endpoint) throws UsbException {
try {
exchangeData(endpoint, data);
}
catch(UsbException e) {
throw new DeviceException(e);
catch(javax.usb.UsbException e) {
throw new UsbException(e);
}
}

public byte[] getFeatureReport(int responseSize, Byte reportId) throws DeviceException {
throw new DeviceException("USB feature reports are not supported");
public byte[] getFeatureReport(int responseSize, Byte reportId) throws UsbException {
throw new UsbException("USB feature reports are not supported");
}

public void sendFeatureReport(byte[] data, Byte reportId) throws DeviceException {
throw new DeviceException("USB feature reports are not supported");
public void sendFeatureReport(byte[] data, Byte reportId) throws UsbException {
throw new UsbException("USB feature reports are not supported");
}

/**
Expand All @@ -100,7 +100,7 @@ public void sendFeatureReport(byte[] data, Byte reportId) throws DeviceException
* @param endpoint Endpoint on the usb device interface to pass data across
* @param data Byte array of data to send, or to be written from a receive
*/
private synchronized void exchangeData(Byte endpoint, byte[] data) throws UsbException {
private synchronized void exchangeData(Byte endpoint, byte[] data) throws javax.usb.UsbException {
if (endpoint == null) {
throw new IllegalArgumentException("Interface endpoint cannot be null");
}
Expand All @@ -116,13 +116,13 @@ private synchronized void exchangeData(Byte endpoint, byte[] data) throws UsbExc
}
}

public void close() throws DeviceException {
public void close() throws UsbException {
if (iface.isClaimed()) {
try {
iface.release();
}
catch(UsbException e) {
throw new DeviceException(e);
catch(javax.usb.UsbException e) {
throw new UsbException(e);
}
}
streaming = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.codehaus.jettison.json.JSONObject;
import qz.utils.UsbUtilities;

public class DeviceOptions {
public class UsbOptions {

public enum DeviceMode {
HID,
Expand All @@ -24,6 +24,9 @@ public static DeviceMode parse(String callName) {

private DeviceMode deviceMode;

private String vendorIdString;
private String productIdString;

private Integer vendorId;
private Integer productId;

Expand All @@ -37,11 +40,13 @@ public static DeviceMode parse(String callName) {
private Integer usagePage;
private String serial;

public DeviceOptions(JSONObject parameters, DeviceMode deviceMode) {
public UsbOptions(JSONObject parameters, DeviceMode deviceMode) {
this.deviceMode = deviceMode;

vendorId = UsbUtilities.hexToInt(parameters.optString("vendorId"));
productId = UsbUtilities.hexToInt(parameters.optString("productId"));
vendorIdString = parameters.optString("vendorId");
vendorId = UsbUtilities.hexToInt(vendorIdString);
productIdString = parameters.optString("productId");
productId = UsbUtilities.hexToInt(productIdString);

if (!parameters.isNull("interface")) {
interfaceId = UsbUtilities.hexToByte(parameters.optString("interface"));
Expand Down Expand Up @@ -71,6 +76,14 @@ public Integer getProductId() {
return productId;
}

public String getVendorIdString() {
return vendorIdString;
}

public String getProductIdString() {
return productIdString;
}

public Byte getInterfaceId() {
return interfaceId;
}
Expand All @@ -97,9 +110,9 @@ public String getSerial() {

@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof DeviceOptions)) { return false; }
if (obj == null || !(obj instanceof UsbOptions)) { return false; }

DeviceOptions that = (DeviceOptions)obj;
UsbOptions that = (UsbOptions)obj;

if (this.getVendorId().equals(that.getVendorId()) && this.getProductId().equals(that.getProductId())) {
if (deviceMode == DeviceMode.USB
Expand Down
Loading