📄 cservice.java
字号:
/**
* Returns the current number of retries.
*
* @return The number of retries.
* @see CService#setRetriesNoResponse(int)
*/
public int getRetriesNoResponse()
{
return retriesNoResponse;
}
/**
* Sets the delay between consecutive attemps for dispatching a message.
*
* @param delay
* The delay in millisecs.
* @see CService#getDelayNoResponse()
* @see CService#setRetriesNoResponse(int)
* @see CService#getRetriesNoResponse()
*/
public void setDelayNoResponse(int delay)
{
this.delayNoResponse = delay * 1000;
}
/**
* Gets the delay between consecutive attemps for dispatching a message.
*
* @return delay The delay in millisecs.
* @see CService#getDelayNoResponse()
* @see CService#setRetriesNoResponse(int)
* @see CService#getRetriesNoResponse()
*/
public int getDelayNoResponse()
{
return delayNoResponse;
}
public void setRetriesCmsErrors(int retries)
{
this.retriesCmsErrors = retries;
}
public int getRetriesCmsErrors()
{
return retriesCmsErrors;
}
public void setDelayCmsErrors(int delay)
{
this.delayCmsErrors = delay * 1000;
}
public int getDelayCmsErrors()
{
return delayCmsErrors;
}
/**
* Returns the Log4J logger object used by SMSLib.
*
* @return The Log4J logger object.
*/
public Logger getLogger()
{
return log;
}
/**
* Sets the logger to a custom Log4J Logger object. You can also use this call to completely disable logging, by passing a null object.
*
* @param log
* A Log4J logger object.
*/
public void setLogger(Logger log)
{
this.log = log;
}
/**
* Sets the receive mode.
*
* @param receiveMode
* The receive mode.
* @see CService.ReceiveMode
*/
public void setReceiveMode(int receiveMode) throws Exception
{
synchronized (_SYNC_)
{
this.receiveMode = receiveMode;
if (connected)
{
if (receiveMode == ReceiveMode.AsyncCnmi)
{
if (!atHandler.enableIndications())
{
if (log != null) log.warn("Could not enable CMTI indications, continuing without them...");
}
}
else
{
if (!atHandler.disableIndications())
{
if (log != null) log.warn("Could not disable CMTI indications, continuing without them...");
}
}
}
}
}
/**
* Returns the Receive Mode.
*
* @return The Receive Mode.
* @see CService.ReceiveMode
*/
public int getReceiveMode()
{
return receiveMode;
}
/**
* Sets the protocol to be used.
* <p>
* The default protocol is PDU. If you want to change it, you must call this method after constructing the CService object and before connecting. Otherwise, you will get an exception.
*
* @param protocol
* The protocol to be used.
* @see CService#getProtocol()
* @see CService.Protocol
*/
public void setProtocol(int protocol) throws Exception
{
if (getConnected()) throw new OopsException("Cannot change protocol while connected!");
else this.protocol = protocol;
}
/**
* Returns the protocol in use.
*
* @return The protocol use.
* @see CService.Protocol
*/
public int getProtocol()
{
return protocol;
}
/**
* Sets the storage locations to be read by SMSLib.
* <p>
* Normally, SMSLib tries to read the available storage locations reported by the modem itself. Sometimes, the modem does not report all storage locations, so you can use this method to define yours, without messing with main SMSLib code.
*
* @param loc
* The storage locations (i.e. a string similar to "SMME", which is specific to each modem)
*/
public void setStorageLocations(String loc)
{
atHandler.setStorageLocations(loc);
}
/**
* Connects to the GSM modem.
* <p>
* The connect() function should be called before any operations. Its purpose is to open the serial link, check for modem existence, initialize modem, start background threads and prepare for subsequent operations.
*
* @see #disconnect()
* @throws NotConnectedException
* Nobody is answering.
* @throws AlreadyConnectedException
* Already connected.
* @throws NoPinException
* If PIN is requested from the modem but no PIN is defined.
* @throws InvalidPinException
* If the defined PIN is not accepted by the modem.
* @throws NoPduSupportException
* The modem does not support PDU mode - fatal error!
*/
public void connect() throws Exception
{
synchronized (_SYNC_)
{
if (getConnected()) throw new AlreadyConnectedException();
else try
{
serialDriver.open();
connected = true;
atHandler.sync();
serialDriver.emptyBuffer();
atHandler.reset();
serialDriver.setNewMsgMonitor(newMsgMonitor);
if (atHandler.isAlive())
{
if (atHandler.waitingForPin())
{
if (getSimPin() == null) throw new NoPinException();
else if (!atHandler.enterPin(getSimPin())) throw new InvalidPinException();
if (atHandler.waitingForPin())
{
if (getSimPin2() == null) throw new NoPin2Exception();
else if (!atHandler.enterPin(getSimPin2())) throw new InvalidPin2Exception();
}
}
atHandler.init();
atHandler.echoOff();
waitForNetworkRegistration();
atHandler.setVerboseErrors();
if (atHandler.storageLocations.length() == 0) atHandler.getStorageLocations();
if (log != null) log.info("MEM: Storage Locations Found: " + atHandler.storageLocations);
switch (protocol)
{
case Protocol.PDU:
if (log != null) log.info("PROT: Using PDU protocol.");
if (!atHandler.setPduMode()) throw new NoPduSupportException();
break;
case Protocol.TEXT:
if (log != null) log.info("PROT: Using TEXT protocol.");
if (!atHandler.setTextMode()) throw new NoTextSupportException();
break;
default:
throw new OopsException("Invalid protocol! Should be PDU or TEXT.");
}
setReceiveMode(receiveMode);
refreshDeviceInfo();
receiveThread = new CReceiveThread();
receiveThread.start();
keepAliveThread = new CKeepAliveThread();
keepAliveThread.start();
}
else throw new NotConnectedException("GSM device is not responding.");
}
catch (Exception e)
{
try
{
disconnect();
}
catch (Exception e2)
{
}
throw e;
}
}
}
/**
* Disconnects from the GSM modem.
* <p>
* This should be the last function called. Closes serial connection, shuts down background threads and performs clean-up.
* <p>
* <strong>Notes</strong>
* <ul>
* <li>Do not connect and disconnect continously - at least if you can avoid it. It takes time and resources. Connect once and stay connected.</li>
* </ul>
*
* @see CService#connect()
*/
public void disconnect() throws Exception
{
if (getConnected())
{
assert (receiveThread != null);
assert (keepAliveThread != null);
final int wait = 100;
int timeout = DISCONNECT_TIMEOUT;
receiveThread.killMe();
keepAliveThread.killMe();
while (timeout > 0 && !receiveThread.killed() && !keepAliveThread.killed())
{
Thread.sleep(wait);
timeout -= wait;
}
try
{
serialDriver.killMe();
if (!receiveThread.killed())
{
receiveThread.interrupt();
receiveThread.join();
}
if (!keepAliveThread.killed())
{
keepAliveThread.interrupt();
keepAliveThread.join();
}
}
finally
{
receiveThread = null;
keepAliveThread = null;
serialDriver.close();
connected = false;
}
}
else throw new NotConnectedException();
}
/**
* Reads all SMS messages from the GSM modem.
*
* @param messageList
* The list to be populated with messages.
* @param messageClass
* The message class of the messages to read.
* @throws NotConnectedException
* Either connect() is not called or modem has been disconnected.
* @see CService#readMessages(LinkedList, int, int)
* @see CIncomingMessage
* @see CIncomingMessage.MessageClass
* @see CService#sendMessage(COutgoingMessage)
*/
public void readMessages(LinkedList messageList, int messageClass) throws Exception
{
switch (protocol)
{
case Protocol.PDU:
readMessages_PDU(messageList, messageClass, 0);
break;
case Protocol.TEXT:
readMessages_TEXT(messageList, messageClass, 0);
}
}
/**
* Reads up to a specific number of SMS messages from the GSM modem.
*
* @param messageList
* The list to be populated with messages.
* @param messageClass
* The message class of the messages to read.
* @param limit
* Read up to <limit> number of messages. If limit is set to 0, read all messages.
* @throws NotConnectedException
* Either connect() is not called or modem has been disconnected.
* @see CService#readMessages(LinkedList, int)
* @see CIncomingMessage
* @see CIncomingMessage.MessageClass
* @see CService#sendMessage(COutgoingMessage)
*/
public void readMessages(LinkedList messageList, int messageClass, int limit) throws Exception
{
switch (protocol)
{
case Protocol.PDU:
readMessages_PDU(messageList, messageClass, limit);
break;
case Protocol.TEXT:
readMessages_TEXT(messageList, messageClass, limit);
break;
}
}
//@SuppressWarnings("unchecked")
private void readMessages_PDU(LinkedList messageList, int messageClass, int limit) throws Exception
{
int i, j, memIndex;
String response, line, pdu;
BufferedReader reader;
CIncomingMessage mpMsg;
if (limit < 0) limit = 0;
mpMsg = null;
synchronized (_SYNC_)
{
if (getConnected())
{
atHandler.switchToCmdMode();
for (int ml = 0; ml < (atHandler.storageLocations.length() / 2); ml++)
{
if (atHandler.setMemoryLocation(atHandler.storageLocations.substring((ml * 2), (ml * 2) + 2)))
{
response = atHandler.listMessages(messageClass);
response = response.replaceAll("\\s+OK\\s+", "\nOK");
reader = new BufferedReader(new StringReader(response));
for (;;)
{
line = reader.readLine().trim();
if (line == null) break;
line = line.trim();
if (line.length() > 0) break;
}
while (true)
{
if (line == null) break;
line = line.trim();
if (line.length() <= 0 || line.equalsIgnoreCase("OK")) break;
i = line.indexOf(':');
j = line.indexOf(',');
memIndex = Integer.parseInt(line.substring(i + 1, j).trim());
pdu = reader.readLine().trim();
try
{
if (isIncomingMessage(pdu))
{
CIncomingMessage msg;
msg = new CIncomingMessage(pdu, memIndex, atHandler.storageLocations.substring((ml * 2), (ml * 2) + 2));
if (log != null) log.debug("IN-DTLS: MI:" + msg.getMemIndex() + " REF:" + msg.getMpRefNo() + " MAX:" + msg.getMpMaxNo() + " SEQ:" + msg.getMpSeqNo());
if (msg.getMpRefNo() == 0)
{
if (mpMsg != null) mpMsg = null;
messageList.add(msg);
deviceInfo.getStatistics().incTotalIn();
}
else
{
int k, l;
LinkedList tmpList;
CIncomingMessage listMsg;
boolean found, duplicate;
found = false;
for (k = 0; k < mpMsgList.size(); k++)
{
tmpList = (LinkedList) mpMsgList.get(k);
listMsg = (CIncomingMessage) tmpList.get(0);
if (listMsg.getMpRefNo() == msg.getMpRefNo())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -