From 15c23f486ed3ad5e5f3458faf4571aa6ac2c859a Mon Sep 17 00:00:00 2001
From: akwizgran <michael@briarproject.org>
Date: Sat, 15 Dec 2012 02:56:34 +0000
Subject: [PATCH] Use +++ATH to hang up modem instead of lowering DTR
 (untested).

---
 .../net/sf/briar/plugins/modem/ModemImpl.java | 20 ++++++++++++++++---
 .../reliability/ReliabilityLayerImpl.java     |  9 ++++-----
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/briar-core/src/net/sf/briar/plugins/modem/ModemImpl.java b/briar-core/src/net/sf/briar/plugins/modem/ModemImpl.java
index 4266cde927..79731773b0 100644
--- a/briar-core/src/net/sf/briar/plugins/modem/ModemImpl.java
+++ b/briar-core/src/net/sf/briar/plugins/modem/ModemImpl.java
@@ -31,6 +31,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
 	};
 	private static final int OK_TIMEOUT = 5 * 1000; // Milliseconds
 	private static final int CONNECT_TIMEOUT = 2 * 60 * 1000; // Milliseconds
+	private static final int ESCAPE_SEQUENCE_GUARD_TIME = 1000; // Milliseconds
 
 	private final Executor executor;
 	private final ReliabilityLayerFactory reliabilityFactory;
@@ -79,8 +80,10 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
 						break;
 					}
 				}
-				if(!foundBaudRate)
+				if(!foundBaudRate) {
+					tryToClose(port);
 					throw new IOException("No suitable baud rate");
+				}
 				port.purgePort(PURGE_RXCLEAR | PURGE_TXCLEAR);
 				port.addEventListener(this);
 				port.writeBytes("ATZ\r\n".getBytes("US-ASCII")); // Reset
@@ -90,6 +93,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
 				throw new IOException(e.toString());
 			}
 			// Wait for the event thread to receive "OK"
+			boolean success = false;
 			try {
 				synchronized(this) {
 					long now = System.currentTimeMillis();
@@ -98,13 +102,14 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
 						wait(end - now);
 						now = System.currentTimeMillis();
 					}
-					if(initialised) return true;
+					success = initialised;
 				}
 			} catch(InterruptedException e) {
 				tryToClose(port);
 				Thread.currentThread().interrupt();
 				throw new IOException("Interrupted while initialising");
 			}
+			if(success) return true;
 			tryToClose(port);
 			return false;
 		} finally {
@@ -132,6 +137,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
 		try {
 			stateChange.acquire();
 		} catch(InterruptedException e) {
+			tryToClose(port);
 			Thread.currentThread().interrupt();
 			throw new IOException("Interrupted while waiting to stop");
 		}
@@ -163,7 +169,14 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
 		reliability.stop();
 		if(LOG.isLoggable(INFO)) LOG.info("Hanging up");
 		try {
-			port.setDTR(false);
+			Thread.sleep(ESCAPE_SEQUENCE_GUARD_TIME);
+			port.writeBytes("+++".getBytes("US-ASCII"));
+			Thread.sleep(ESCAPE_SEQUENCE_GUARD_TIME);
+			port.writeBytes("ATH\r\n".getBytes("US-ASCII"));
+		} catch(InterruptedException e) {
+			tryToClose(port);
+			Thread.currentThread().interrupt();
+			throw new IOException("Interrupted while hanging up");
 		} catch(SerialPortException e) {
 			tryToClose(port);
 			throw new IOException(e.toString());
@@ -246,6 +259,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
 		try {
 			stateChange.acquire();
 		} catch(InterruptedException e) {
+			tryToClose(port);
 			Thread.currentThread().interrupt();
 			throw new IOException("Interrupted while waiting to hang up");
 		}
diff --git a/briar-core/src/net/sf/briar/reliability/ReliabilityLayerImpl.java b/briar-core/src/net/sf/briar/reliability/ReliabilityLayerImpl.java
index 22a7186667..78c86ec6e7 100644
--- a/briar-core/src/net/sf/briar/reliability/ReliabilityLayerImpl.java
+++ b/briar-core/src/net/sf/briar/reliability/ReliabilityLayerImpl.java
@@ -54,6 +54,7 @@ class ReliabilityLayerImpl implements ReliabilityLayer, WriteHandler {
 						byte[] b = null;
 						while(now < next && b == null) {
 							b = writes.poll(next - now, MILLISECONDS);
+							if(!running) return;
 							now = System.currentTimeMillis();
 						}
 						if(b == null) {
@@ -94,13 +95,11 @@ class ReliabilityLayerImpl implements ReliabilityLayer, WriteHandler {
 
 	// The transport calls this method to pass data up to the SLIP decoder
 	public void handleRead(byte[] b) throws IOException {
-		if(!running) throw new IOException("Connection closed");
-		decoder.handleRead(b);
+		if(running) decoder.handleRead(b);
 	}
 
 	// The SLIP encoder calls this method to pass data down to the transport
-	public void handleWrite(byte[] b) throws IOException {
-		if(!running) throw new IOException("Connection closed");
-		if(b.length > 0) writes.add(b);
+	public void handleWrite(byte[] b) {
+		if(running && b.length > 0) writes.add(b);
 	}
 }
-- 
GitLab