Skip to content

Commit

Permalink
Merge pull request #374 from smartdevicelink/develop
Browse files Browse the repository at this point in the history
Pull in PR #367 as hotfix into master
  • Loading branch information
joeygrover authored Jan 17, 2017
2 parents fa3031b + a2264a5 commit 5a5ad93
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 30 deletions.
77 changes: 47 additions & 30 deletions sdl_android_lib/src/com/smartdevicelink/transport/USBTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import com.smartdevicelink.exception.SdlExceptionCause;
import com.smartdevicelink.protocol.SdlPacket;
import com.smartdevicelink.trace.SdlTrace;
import com.smartdevicelink.trace.enums.InterfaceActivityDirection;
import com.smartdevicelink.transport.ITransportListener;
import com.smartdevicelink.transport.SdlTransport;
import com.smartdevicelink.transport.SiphonServer;
import com.smartdevicelink.trace.enums.InterfaceActivityDirection;
import com.smartdevicelink.transport.enums.TransportType;
import com.smartdevicelink.util.DebugTool;

Expand All @@ -37,7 +34,10 @@
*/
@SuppressLint("NewApi")
public class USBTransport extends SdlTransport {
/**

// Boolean to monitor if the transport is in a disconnecting state
private boolean _disconnecting = false;
/**
* Broadcast action: sent when a USB accessory is attached.
*
* UsbManager.EXTRA_ACCESSORY extra contains UsbAccessory object that has
Expand Down Expand Up @@ -278,7 +278,6 @@ public void openConnection() throws SdlException {
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
filter.addAction(ACTION_USB_PERMISSION);
getContext().registerReceiver(mUSBReceiver, filter);

initializeAccessory();
} catch (Exception e) {
String msg = "Couldn't start opening connection";
Expand Down Expand Up @@ -353,6 +352,14 @@ private void stopReaderThread() {
* @param ex Disconnect exception, if any
*/
private void disconnect(String msg, Exception ex) {

// If already disconnecting, return
if (_disconnecting) {
// No need to recursively call
return;
}
_disconnecting = true;

final State state = getState();
switch (state) {
case LISTENING:
Expand All @@ -372,6 +379,7 @@ private void disconnect(String msg, Exception ex) {
if (mOutputStream != null) {
try {
mOutputStream.close();
mOutputStream = null;
} catch (IOException e) {
logW("Can't close output stream", e);
mOutputStream = null;
Expand All @@ -380,6 +388,7 @@ private void disconnect(String msg, Exception ex) {
if (mInputStream != null) {
try {
mInputStream.close();
mInputStream = null;
} catch (IOException e) {
logW("Can't close input stream", e);
mInputStream = null;
Expand All @@ -388,6 +397,7 @@ private void disconnect(String msg, Exception ex) {
if (mParcelFD != null) {
try {
mParcelFD.close();
mParcelFD = null;
} catch (IOException e) {
logW("Can't close file descriptor", e);
mParcelFD = null;
Expand Down Expand Up @@ -428,6 +438,7 @@ private void disconnect(String msg, Exception ex) {
"; doing nothing");
break;
}
_disconnecting = false;
}

/**
Expand All @@ -440,31 +451,34 @@ private void disconnect(String msg, Exception ex) {
public TransportType getTransportType() {
return TransportType.USB;
}

/**
* Looks for an already connected compatible accessory and connect to it.
*/
private void initializeAccessory() {
logI("Looking for connected accessories");
if (!mConfig.getQueryUsbAcc()){
logI("Query for accessory is disabled.");
return;
}
logI("Looking for connected accessories");
UsbAccessory acc = mConfig.getUsbAccessory();
if(acc == null || !isAccessorySupported(acc)){ //Check to see if our config included an accessory and that it is supported. If not, see if there are any other accessories connected.
UsbManager usbManager = getUsbManager();
UsbAccessory[] accessories = usbManager.getAccessoryList();
if (accessories != null) {
logD("Found total " + accessories.length + " accessories");
for (UsbAccessory accessory : accessories) {
if (isAccessorySupported(accessory)) {
acc = accessory;
break;
}
}
} else {
logI("No connected accessories found");
return;
}
}

connectToAccessory(acc);
if( acc == null || !isAccessorySupported(acc)){ //Check to see if our config included an accessory and that it is supported. If not, see if there are any other accessories connected.
UsbManager usbManager = getUsbManager();
UsbAccessory[] accessories = usbManager.getAccessoryList();
if (accessories != null) {
logD("Found total " + accessories.length + " accessories");
for (UsbAccessory accessory : accessories) {
if (isAccessorySupported(accessory)) {
acc = accessory;
break;
}
}
} else {
logI("No connected accessories found");
return;
}
}
connectToAccessory(acc);
}

/**
Expand Down Expand Up @@ -647,7 +661,7 @@ private enum State {
* Internal task that connects to and reads data from a USB accessory.
*
* Since the class has to have access to the parent class' variables,
* sdlhronization must be taken in consideration! For now, all access
* synchronization must be taken in consideration! For now, all access
* to variables of USBTransport must be surrounded with
* synchronized (USBTransport.this) { … }
*/
Expand Down Expand Up @@ -728,8 +742,8 @@ private boolean connect() {
}

logI("Accessory opened!");

synchronized (USBTransport.this) {
synchronized (USBTransport.this) {
setState(State.CONNECTED);
handleTransportConnected();
}
Expand Down Expand Up @@ -758,7 +772,10 @@ private void readFromTransport() {
// read loop
while (!isInterrupted()) {
try {
bytesRead = mInputStream.read(buffer);
if (mInputStream == null)
continue;

bytesRead = mInputStream.read(buffer);
if (bytesRead == -1) {
if (isInterrupted()) {
logI("EOF reached, and thread is interrupted");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class USBTransportConfig extends BaseTransportConfig {

private Context mainActivity = null;
private UsbAccessory usbAccessory = null;
private Boolean queryUsbAcc = true;

public USBTransportConfig (Context mainActivity) {
this.mainActivity = mainActivity;
Expand All @@ -19,6 +20,23 @@ public USBTransportConfig (Context mainActivity, UsbAccessory usbAccessory) {
this.usbAccessory = usbAccessory;
}

public USBTransportConfig (Context mainActivity, boolean shareConnection, boolean queryUsbAcc) {
this.mainActivity = mainActivity;
this.queryUsbAcc = queryUsbAcc;
super.shareConnection = shareConnection;
}

public USBTransportConfig (Context mainActivity, UsbAccessory usbAccessory, boolean shareConnection, boolean queryUsbAcc) {
this.mainActivity = mainActivity;
this.queryUsbAcc = queryUsbAcc;
this.usbAccessory = usbAccessory;
super.shareConnection = shareConnection;
}

public Boolean getQueryUsbAcc () {
return queryUsbAcc;
}

public Context getUSBContext () {
return mainActivity;
}
Expand Down

0 comments on commit 5a5ad93

Please sign in to comment.