?? amodemdriver.java
字號(hào):
// SMSLib for Java v3
// A Java API library for sending and receiving SMS via a GSM modem
// or other supported gateways.
// Web Site: http://www.smslib.org
//
// Copyright (C) 2002-2009, Thanasis Delenikas, Athens/GREECE.
// SMSLib is distributed under the terms of the Apache License version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.smslib.modem;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.smslib.GatewayException;
import org.smslib.InboundMessage;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.AGateway.AsyncEvents;
import org.smslib.AGateway.GatewayStatuses;
import org.smslib.AGateway.Protocols;
import org.smslib.InboundMessage.MessageClasses;
import org.smslib.scheduler.ASchedulerTask;
/**
* Abstract implementation of a generic GSM modem driver.
*/
public abstract class AModemDriver
{
private static final String rxErrorWithCode = "\\s*[\\p{ASCII}]*\\s*\\+(CM[ES])\\s+ERROR: (\\d+)\\s";
private static final String rxPlainError = "\\s*[\\p{ASCII}]*\\s*(ERROR|NO CARRIER|NO DIALTONE)\\s";
private Object SYNC_Reader, SYNC_Commander, SYNC_InboundReader;
private ModemGateway gateway;
private boolean dataReceived;
private volatile boolean connected;
private CharQueue charQueue;
private ModemReader modemReader;
private KeepAlive keepAlive;
private AsyncNotifier asyncNotifier;
private AsyncMessageProcessor asyncMessageProcessor;
private CNMIEmulatorProcessor cnmiEmulationProcessor;
/**
* Code of last error
*
* <pre>
* -1 = empty or invalid response
* 0 = OK
* 5xxx = CME error xxx
* 6xxx = CMS error xxx
* 9000 = ERROR
* </pre>
*/
private int lastError;
static int OK = 0;
protected AModemDriver(ModemGateway myGateway, String deviceParms)
{
setSYNCReader(new Object());
setSYNCCommander(new Object());
setSYNCInboundReader(new Object());
setGateway(myGateway);
setConnected(false);
setDataReceived(false);
setCharQueue(new CharQueue());
}
protected abstract void connectPort() throws GatewayException, IOException, InterruptedException;
protected abstract void disconnectPort() throws IOException, InterruptedException;
protected abstract void clear() throws IOException;
protected void connect() throws TimeoutException, GatewayException, IOException, InterruptedException
{
String response;
synchronized (getSYNCCommander())
{
try
{
connectPort();
setConnected(true);
setKeepAlive(new KeepAlive("KeepAlive [" + getGateway().getGatewayId() + "]", getGateway().getService()));
getGateway().getService().getScheduler().scheduleAtFixedRate(getKeepAlive(), getGateway().getService().getSettings().SERIAL_KEEPALIVE_INTERVAL, getGateway().getService().getSettings().SERIAL_KEEPALIVE_INTERVAL, TimeUnit.SECONDS);
getKeepAlive().enable();
setCnmiEmulationProcessor(new CNMIEmulatorProcessor("CNMIEmulatorProcessor [" + getGateway().getGatewayId() + "]", getGateway().getService()));
getGateway().getService().getScheduler().scheduleAtFixedRate(getCnmiEmulationProcessor(), getGateway().getService().getSettings().CNMI_EMULATOR_INTERVAL, getGateway().getService().getSettings().CNMI_EMULATOR_INTERVAL, TimeUnit.SECONDS);
setModemReader(new ModemReader());
setAsyncNotifier(new AsyncNotifier());
setAsyncMessageProcessor(new AsyncMessageProcessor());
clearBuffer();
getGateway().getATHandler().reset();
getGateway().getATHandler().sync();
getGateway().getATHandler().echoOff();
if ((getGateway().getCustomInitString() != null) && (getGateway().getCustomInitString().length() > 0))
{
write(getGateway().getCustomInitString() + "\r");
getGateway().getATHandler().echoOff();
}
while (true)
{
response = getGateway().getATHandler().getSimStatus();
while (response.indexOf("BUSY") >= 0)
{
getGateway().getService().getLogger().logDebug("SIM found busy, waiting...", null, getGateway().getGatewayId());
Thread.sleep(getGateway().getService().getSettings().AT_WAIT_SIMPIN);
response = getGateway().getATHandler().getSimStatus();
}
if (response.indexOf("SIM PIN2") >= 0)
{
getGateway().getService().getLogger().logDebug("SIM requesting PIN2.", null, getGateway().getGatewayId());
if ((getGateway().getSimPin2() == null) || (getGateway().getSimPin2().length() == 0)) throw new GatewayException("The GSM modem requires SIM PIN2 to operate.");
if (!getGateway().getATHandler().enterPin(getGateway().getSimPin2())) throw new GatewayException("SIM PIN2 provided is not accepted by the GSM modem.");
Thread.sleep(getGateway().getService().getSettings().AT_WAIT_SIMPIN);
continue;
}
else if (response.indexOf("SIM PIN") >= 0)
{
getGateway().getService().getLogger().logDebug("SIM requesting PIN.", null, getGateway().getGatewayId());
if ((getGateway().getSimPin() == null) || (getGateway().getSimPin().length() == 0)) throw new GatewayException("The GSM modem requires SIM PIN to operate.");
if (!getGateway().getATHandler().enterPin(getGateway().getSimPin())) throw new GatewayException("SIM PIN provided is not accepted by the GSM modem.");
Thread.sleep(getGateway().getService().getSettings().AT_WAIT_SIMPIN);
continue;
}
else if (response.indexOf("READY") >= 0) break;
else if (response.indexOf("OK") >= 0) break;
else if (response.indexOf("ERROR") >= 0)
{
getGateway().getService().getLogger().logWarn("Erroneous CPIN response, proceeding with defaults.", null, getGateway().getGatewayId());
}
getGateway().getService().getLogger().logWarn("Cannot understand SIMPIN response: " + response + ", will wait for a while...", null, getGateway().getGatewayId());
Thread.sleep(getGateway().getService().getSettings().AT_WAIT_SIMPIN);
}
getGateway().getATHandler().echoOff();
getGateway().getATHandler().init();
getGateway().getATHandler().echoOff();
waitForNetworkRegistration();
getGateway().getATHandler().setVerboseErrors();
if (getGateway().getATHandler().getStorageLocations().length() == 0)
{
try
{
getGateway().getATHandler().readStorageLocations();
getGateway().getService().getLogger().logInfo("MEM: Storage Locations Found: " + getGateway().getATHandler().getStorageLocations(), null, getGateway().getGatewayId());
}
catch (Exception e)
{
getGateway().getATHandler().setStorageLocations("--");
getGateway().getService().getLogger().logWarn("Storage locations could *not* be retrieved, will proceed with defaults.", e, getGateway().getGatewayId());
}
}
if (!getGateway().getATHandler().setIndications())
{
getGateway().getService().getLogger().logWarn("Callback indications were *not* set succesfully!", null, getGateway().getGatewayId());
getCnmiEmulationProcessor().enable();
}
else
{
if (getGateway().getATHandler().getIndications().getMode().equals("0")) getCnmiEmulationProcessor().enable();
}
if (getGateway().getProtocol() == Protocols.PDU)
{
if (!getGateway().getATHandler().setPduProtocol()) throw new GatewayException("The GSM modem does not support the PDU protocol.");
}
else if (getGateway().getProtocol() == Protocols.TEXT)
{
if (!getGateway().getATHandler().setTextProtocol()) throw new GatewayException("The GSM modem does not support the TEXT protocol.");
}
}
catch (TimeoutException t)
{
// this is to prevent serial port from being held
// when a timeout during initialization occurs
try
{
disconnect();
}
catch (Exception e)
{
// Discard all here.
}
throw t;
}
}
}
protected void disconnect() throws IOException, InterruptedException
{
//deadlocking is still possible in this method
//however it is nearly impossible, since keepAlive will
//give other threads opportunity to either exit global "while"
//or enter waiting state
if (getAsyncNotifier() != null)
{
getGateway().getService().getLogger().logDebug("Trying to shutdown asyncNotifier thread...", null, getGateway().getGatewayId());
}
if (getAsyncMessageProcessor() != null)
{
getGateway().getService().getLogger().logDebug("Trying to shutdown asyncMessageProcessor thread...", null, getGateway().getGatewayId());
}
if (getModemReader() != null)
{
getGateway().getService().getLogger().logDebug("Trying to shutdown modemReader thread...", null, getGateway().getGatewayId());
}
setConnected(false);
getGateway().getService().getScheduler().remove(getKeepAlive());
getGateway().getService().getScheduler().remove(getCnmiEmulationProcessor());
if (getAsyncNotifier() != null)
{
getAsyncNotifier().interrupt();
getAsyncNotifier().join();
setAsyncNotifier(null);
}
if (getAsyncMessageProcessor() != null)
{
getAsyncMessageProcessor().interrupt();
getAsyncMessageProcessor().join();
setAsyncMessageProcessor(null);
}
if (getModemReader() != null)
{
getModemReader().interrupt();
getModemReader().join();
setModemReader(null);
}
disconnectPort();
}
public abstract void write(char c) throws IOException;
public abstract void write(byte[] s) throws IOException;
protected abstract int read() throws IOException;
protected abstract boolean portHasData() throws IOException;
public boolean dataAvailable() throws InterruptedException
{
return (getCharQueue().peek() == -1 ? false : true);
}
public void write(String s) throws IOException
{
getGateway().getService().getLogger().logDebug("SEND :" + formatLog(new StringBuffer(s)), null, getGateway().getGatewayId());
write(s.getBytes());
}
public void AddToQueue(String s)
{
for (int i = 0; i < s.length(); i++)
getCharQueue().put((byte) s.charAt(i));
}
public String getResponse() throws GatewayException, TimeoutException, IOException, InterruptedException
{
StringBuffer buffer;
String response;
byte c;
boolean terminate;
int i;
String terminators[];
setLastError(-1);
terminators = getGateway().getATHandler().getTerminators();
buffer = new StringBuffer(getGateway().getService().getSettings().SERIAL_BUFFER_SIZE);
try
{
while (true)
{
while ((getCharQueue().peek() == 0x0a) || (getCharQueue().peek() == 0x0d))
{
getCharQueue().get();
}
while (true)
{
c = getCharQueue().get();
if (System.getProperty("smslib.dumpqueues") != null) getGateway().getService().getLogger().logDebug("OUT READER QUEUE : " + (int) c + " / " + (char) c, null, getGateway().getGatewayId());
if (c != 0x0a) buffer.append((char) c);
else break;
}
if (buffer.charAt(buffer.length() - 1) != 0x0d) buffer.append((char) 0x0d);
response = buffer.toString();
terminate = false;
for (i = 0; i < terminators.length; i++)
{
if (response.matches(terminators[i]))
{
terminate = true;
break;
}
}
if (terminate) break;
}
getGateway().getService().getLogger().logDebug("BUFFER: " + buffer, null, getGateway().getGatewayId());
if (i >= terminators.length - 4)
{
AsyncEvents event = getGateway().getATHandler().processUnsolicitedEvents(buffer.toString());
if ((event == AsyncEvents.INBOUNDMESSAGE) || (event == AsyncEvents.INBOUNDSTATUSREPORTMESSAGE) || (event == AsyncEvents.INBOUNDCALL)) getAsyncNotifier().setEvent(event, buffer.toString());
return getResponse();
}
// Try to interpret error code
if (response.matches(rxErrorWithCode))
{
Pattern p = Pattern.compile(rxErrorWithCode);
Matcher m = p.matcher(response);
if (m.find())
{
try
{
if (m.group(1).equals("CME"))
{
int code = Integer.parseInt(m.group(2));
setLastError(5000 + code);
}
else if (m.group(1).equals("CMS"))
{
int code = Integer.parseInt(m.group(2));
setLastError(6000 + code);
}
else throw new GatewayException("Invalid error response: " + m.group(1));
}
catch (NumberFormatException e)
{
getGateway().getService().getLogger().logDebug("Error on number conversion while interpreting response: ", null, getGateway().getGatewayId());
throw new GatewayException("Cannot convert error code number.");
}
}
else throw new GatewayException("Cannot match error code. Should never happen!");
}
else if (response.matches(rxPlainError)) setLastError(9000);
else if (response.indexOf("OK") >= 0) setLastError(0);
else setLastError(10000);
getGateway().getService().getLogger().logDebug("RECV :" + formatLog(buffer), null, getGateway().getGatewayId());
}
catch (InterruptedException e)
{
getGateway().getService().getLogger().logWarn("GetResponse() Interrupted.", e, getGateway().getGatewayId());
throw e;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -