Made serial port's state volatile, added method for stopping modem.

This commit is contained in:
akwizgran
2012-12-06 15:09:04 +00:00
parent 25fa4decfb
commit a4712140e6
4 changed files with 31 additions and 7 deletions

Binary file not shown.

View File

@@ -13,7 +13,13 @@ interface Modem {
/** /**
* Call this method after creating the modem and before making any calls. * Call this method after creating the modem and before making any calls.
*/ */
void init() throws IOException; void start() throws IOException;
/**
* Call this method when the modem is no longer needed. If a call is in
* progress it will be terminated.
*/
void stop() throws IOException;
/** /**
* Initiates an outgoing call and returns true if the call connects. If the * Initiates an outgoing call and returns true if the call connects. If the

View File

@@ -51,7 +51,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
reliabilityLayer = new ReliabilityLayer(this); reliabilityLayer = new ReliabilityLayer(this);
} }
public void init() throws IOException { public void start() throws IOException {
if(LOG.isLoggable(INFO)) LOG.info("Initialising"); if(LOG.isLoggable(INFO)) LOG.info("Initialising");
try { try {
if(!port.openPort()) if(!port.openPort())
@@ -90,6 +90,14 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
throw new IOException("Modem did not respond"); throw new IOException("Modem did not respond");
} }
public void stop() throws IOException {
try {
port.closePort();
} catch(SerialPortException e) {
throw new IOException(e.toString());
}
}
public boolean dial(String number) throws IOException { public boolean dial(String number) throws IOException {
if(!offHook.tryAcquire()) { if(!offHook.tryAcquire()) {
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
@@ -178,9 +186,8 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
if(LOG.isLoggable(INFO)) LOG.info("Modem status: " + s); if(LOG.isLoggable(INFO)) LOG.info("Modem status: " + s);
if(s.startsWith("CONNECT")) { if(s.startsWith("CONNECT")) {
synchronized(connected) { synchronized(connected) {
if(connected.getAndSet(true)) if(!connected.getAndSet(true))
throw new IOException("Connected twice"); connected.notifyAll();
connected.notifyAll();
} }
// There might be data in the buffer as well as text // There might be data in the buffer as well as text
int off = i + 1; int off = i + 1;
@@ -190,6 +197,10 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
reliabilityLayer.handleRead(data); reliabilityLayer.handleRead(data);
} }
return; return;
} else if(s.equals("BUSY") || s.equals("NO DIALTONE")) {
synchronized(connected) {
connected.notifyAll();
}
} else if(s.equals("OK")) { } else if(s.equals("OK")) {
synchronized(initialised) { synchronized(initialised) {
if(!initialised.getAndSet(true)) if(!initialised.getAndSet(true))

View File

@@ -72,7 +72,7 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
LOG.info("Trying to initialise modem on " + portName); LOG.info("Trying to initialise modem on " + portName);
modem = modemFactory.createModem(this, portName); modem = modemFactory.createModem(this, portName);
try { try {
modem.init(); modem.start();
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
LOG.info("Initialised modem on " + portName); LOG.info("Initialised modem on " + portName);
running = true; running = true;
@@ -86,6 +86,13 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
public void stop() { public void stop() {
running = false; running = false;
if(modem != null) {
try {
modem.stop();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
} }
private boolean resetModem() { private boolean resetModem() {
@@ -93,7 +100,7 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
for(String portName : SerialPortList.getPortNames()) { for(String portName : SerialPortList.getPortNames()) {
modem = modemFactory.createModem(this, portName); modem = modemFactory.createModem(this, portName);
try { try {
modem.init(); modem.start();
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
LOG.info("Initialised modem on " + portName); LOG.info("Initialised modem on " + portName);
return true; return true;