📄 modemdriver.java
字号:
// 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
//
// SMSLib is distributed under the terms of the Apache License version 2.0
//
// Copyright (C) 2002-2007, Thanasis Delenikas, Athens/GREECE
// Portions Copyright:
// Davide Bettoni, Clusone/ITALY, dbettoni@users.sourceforge.net
// Tomek Cejner, Polland, heretique@users.sourceforge.net
//
// 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;
import java.io.*;
import java.util.*;
import java.util.regex.*;
abstract class ModemDriver
{
protected static final int RECEIVE_TIMEOUT = 30 * 1000;
protected static final int KEEP_ALIVE = 20 * 1000;
protected static final int BUFFER_SIZE = 16384;
protected static final int CLEAR_WAIT = 1000;
protected boolean ENABLE_QUEUE_DEBUG = false;
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\\s";
protected Object SYNC_Reader, SYNC_Commander;
protected ModemGateway gateway;
protected boolean dataReceived;
private volatile boolean connected;
private CharQueue queue;
private ModemReader modemReader;
private KeepAlive keepAlive;
private AsyncNotifier asyncNotifier;
/**
* 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;
ModemDriver(ModemGateway gateway, String deviceParms)
{
SYNC_Reader = new Object();
SYNC_Commander = new Object();
this.gateway = gateway;
connected = false;
dataReceived = false;
queue = new CharQueue();
if (System.getProperty("smslib.queuedebug") != null) ENABLE_QUEUE_DEBUG = true;
}
abstract void connectPort() throws GatewayException, IOException, InterruptedException;
abstract void disconnectPort() throws IOException, InterruptedException;
abstract void clear() throws IOException;
void connect() throws TimeoutException, GatewayException, IOException, InterruptedException
{
String response;
synchronized (SYNC_Commander)
{
connectPort();
clearBuffer();
gateway.getATHandler().reset();
gateway.getATHandler().sync();
gateway.getATHandler().echoOff();
while (true)
{
response = gateway.getATHandler().getSimStatus();
while (response.indexOf("BUSY") >= 0)
{
gateway.getLogger().debug("SIM found busy, waiting...");
Thread.sleep(5000);
response = gateway.getATHandler().getSimStatus();
}
if (response.indexOf("SIM PIN2") >= 0)
{
gateway.getLogger().debug("SIM requesting PIN2.");
if ((gateway.getSimPin2() == null) || (gateway.getSimPin2().length() == 0)) throw new GatewayException("The GSM modem requires SIM PIN2 to operate.");
if (!gateway.getATHandler().enterPin(gateway.getSimPin2())) throw new GatewayException("SIM PIN2 provided is not accepted by the GSM modem.");
Thread.sleep(10000);
}
else if (response.indexOf("SIM PIN") >= 0)
{
gateway.getLogger().debug("SIM requesting PIN.");
if ((gateway.getSimPin() == null) || (gateway.getSimPin().length() == 0)) throw new GatewayException("The GSM modem requires SIM PIN to operate.");
if (!gateway.getATHandler().enterPin(gateway.getSimPin())) throw new GatewayException("SIM PIN provided is not accepted by the GSM modem.");
Thread.sleep(10000);
}
if (response.indexOf("READY") >= 0) break;
}
gateway.getATHandler().echoOff();
gateway.getATHandler().init();
gateway.getATHandler().echoOff();
waitForNetworkRegistration();
gateway.getATHandler().setVerboseErrors();
if (gateway.getATHandler().storageLocations.length() == 0)
{
try
{
gateway.getATHandler().readStorageLocations();
gateway.getLogger().info("MEM: Storage Locations Found: " + gateway.getATHandler().storageLocations);
}
catch (Exception e)
{
gateway.getATHandler().storageLocations = "--";
gateway.getATHandler().description = "";
gateway.getLogger().warn("Storage locations could *not* be retrieved, will proceed with defaults.");
}
}
if (!gateway.getATHandler().setIndications()) gateway.getLogger().warn("Callback indications were *not* set succesfully!");
if (gateway.getProtocol() == MessageProtocols.PDU)
{
if (!gateway.getATHandler().setPduProtocol()) throw new GatewayException("The GSM modem does not support the PDU protocol.");
}
else if (gateway.getProtocol() == MessageProtocols.TEXT)
{
if (!gateway.getATHandler().setTextProtocol()) throw new GatewayException("The GSM modem does not support the TEXT protocol.");
}
}
}
void disconnect() throws IOException, InterruptedException
{
synchronized (SYNC_Commander)
{
disconnectPort();
}
}
void setConnected(boolean value) throws InterruptedException
{
connected = value;
if (connected)
{
keepAlive = new KeepAlive();
modemReader = new ModemReader(keepAlive);
asyncNotifier = new AsyncNotifier();
}
else
{
if (keepAlive != null)
{
gateway.getLogger().debug("Trying to shutdown keepAlive thread...");
keepAlive.interrupt();
keepAlive.join();
keepAlive = null;
}
if (asyncNotifier != null)
{
gateway.getLogger().debug("Trying to shutdown asyncNotifier thread...");
asyncNotifier.interrupt();
asyncNotifier.join();
asyncNotifier = null;
}
if (modemReader != null)
{
gateway.getLogger().debug("Trying to shutdown modemReader thread...");
modemReader.interrupt();
modemReader.join();
modemReader = null;
}
}
}
abstract void write(char c) throws IOException;
abstract int read() throws IOException;
abstract boolean portHasData() throws IOException;
boolean dataAvailable() throws IOException, InterruptedException
{
return (queue.peek() == -1 ? false : true);
}
void write(String s) throws IOException
{
gateway.getLogger().debug("SEND :" + formatLog(new StringBuffer(s)));
for (int i = 0; i < s.length(); i++)
write(s.charAt(i));
}
/**
* Read response from modem and return it as String. Also lastError member
* variable is set according to interpreted response.
*/
String getResponse() throws GatewayException, TimeoutException, IOException
{
StringBuffer buffer;
String response;
byte c;
boolean terminate;
int i;
String terminators[];
lastError = -1;
terminators = gateway.getATHandler().terminators;
buffer = new StringBuffer(BUFFER_SIZE);
try
{
while (true)
{
while ((queue.peek() == 0x0a) || (queue.peek() == 0x0d))
queue.get();
while (true)
{
c = queue.get();
if (ENABLE_QUEUE_DEBUG) gateway.getLogger().debug("OUT READER QUEUE : " + (int) c + " / " + (char) c);
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;
}
if (i >= terminators.length - 4)
{
AsyncEvents event =gateway.getATHandler().processUnsolicitedEvents(buffer.toString());
if ((event == AsyncEvents.INBOUNDMESSAGE) ||
(event == AsyncEvents.INBOUNDSTATUSREPORTMESSAGE) ||
(event == AsyncEvents.INBOUNDCALL))
asyncNotifier.setEvent(event);
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));
lastError = 5000 + code;
}
else if (m.group(1).equals("CMS"))
{
int code = Integer.parseInt(m.group(2));
lastError = 6000 + code;
}
else throw new GatewayException("Invalid error response: " + m.group(1));
}
catch (NumberFormatException e)
{
gateway.getLogger().error("Error on number conversion while interpreting response: ");
throw new GatewayException("Cannot convert error code number.");
}
}
else throw new GatewayException("Cannot match error code. Should never happen!");
}
else if (response.matches(rxPlainError)) lastError = 9000;
else if (response.indexOf("OK") >= 0) lastError = 0;
gateway.getLogger().debug("RECV :" + formatLog(buffer));
}
catch (InterruptedException e)
{
gateway.getLogger().warn("GetResponse() Interrupted.");
}
return buffer.toString();
}
void clearBuffer() throws IOException, InterruptedException
{
synchronized (SYNC_Commander)
{
gateway.getLogger().debug("clearBuffer() called.");
Thread.sleep(CLEAR_WAIT);
clear();
queue.clear();
}
}
boolean waitForNetworkRegistration() throws GatewayException, TimeoutException, IOException, InterruptedException
{
StringTokenizer tokens;
String response;
int answer;
while (true)
{
response = gateway.getATHandler().getNetworkRegistration();
if (response.indexOf("ERROR") > 0) return false;
response = response.replaceAll("\\s+OK\\s+", "");
response = response.replaceAll("\\s+", "");
response = response.replaceAll("\\+CREG:", "");
tokens = new StringTokenizer(response, ",");
tokens.nextToken();
try
{
answer = Integer.parseInt(tokens.nextToken());
}
catch (Exception e)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -