public class ATCommand extends Object
Because the different AT-Channels of the device might have different capabilities, these capabilities can be queried (Fax, Data-Transfers). It might be possible (or better be probable) that there is e.g. only one instance with CSD capabilities available.
Additionally, a callback class for unsolicited AT-Events (URCs) can be registered.
Please be aware that corresponding to the setting made by the "AT+CSCS" command the string parameters of several AT commands has to be passed in the GSM or UCS2 character set and the responses might contain strings in that character set as well. The ATCommand class does no string conversion at all which means that all converting between Java strings and GSM respectively UCS2 strings has to be done by the application itself in both directions. For easy conversion between the different character sets the class ATStringConversion can be used.
The usage of the short result code format as configured with "ATV0" is not possible when using the ATCommand class.
To be able to deal with data connections created by this class e.g. by using the ATD command there are member variables for input and output streaming from and to data connections which are accessible via the getDataOutputStream() and getDataInputStream() functions. If the module is in transparent data mode (a data connection is active) the call of the send functions is forbidden and will cause an IllegalStateException.
Constructor and Description |
---|
ATCommand(boolean csdSupport)
Creates a new instance of the ATCommand class.
If there are no more instances possible or if it is not possible to provide possibly requested CSD support the constructor will fail with an exception. Please note that it is not possible to restore the state of an previously closed instance in a new instance of this class. |
ATCommand(boolean csdSupport,
long atResponseMaxSize)
Creates a new instance of the ATCommand class.
If there are no more instances possible or if it is not possible to provide possibly requested CSD support the constructor will fail with an exception. Please note that it is not possible to restore the state of an previously closed instance in a new instance of this class. |
Modifier and Type | Method and Description |
---|---|
void |
addListener(ATCommandListener listener)
Registers listeners to receive unsolicited AT-Events (URCs).
|
String |
breakConnection()
This function switches the module, when in CSD data call, back from
transparent data mode to AT command mode.
|
void |
cancelCommand()
Cancel a running AT command.
|
boolean |
csdSupported()
Get information if the connected AT-Interpreter is able to handle CSD
connections.
|
boolean |
getCONN()
Get the state of a data connection of the devices AT channel.
|
InputStream |
getDataInputStream()
This function returns the input stream for data connections.
|
OutputStream |
getDataOutputStream()
This function returns the output stream for data connections.
|
boolean |
getDCD()
Get the state of the DCD (Data Carrier Detect) signal of the devices AT
channel.
|
boolean |
getDSR()
Get the state of the DSR (Data Set Ready) signal of the devices AT channel.
|
boolean |
getRING()
Get the state of the RING signal of the devices AT channel.
|
void |
release()
Release resources locked by the ATCommand class.
|
String |
removeCharacter(String strResource,
char chRemove) |
void |
removeListener(ATCommandListener listener)
Removes a listener object which has been previously added from the internal
list table of listener objects.
|
String |
send(String ATCmd)
Send an AT command to the device.
|
void |
send(String strATCmd,
ATCommandResponseListener Listener)
Send an AT command to the device.
|
void |
setDTR(boolean SignalState)
Set the state of the DTR (Data Terminal Ready) signal of the devices AT
channel.
|
public ATCommand(boolean csdSupport) throws ATCommandFailedException
csdSupport
- A flag indicating if the instance of this class should have
CSD support or not. It might be possible that there is an
instance with CSD support created, even if it wasn't
requested if there are only instances with CSD support
available. But it can never happen that there is CSD
support requested and an instance without is created. If
CSD support is requested and there is no instance with CSD
available an IllegalStateException will be thrown.IllegalStateException
- if there isn't any instance of the AT
interpreter with the requested capabilities
available.ATCommandFailedException
- if the connection to the AT-Interpreter
fails because of some internal reasons (e.g.
because the virtual COM port cannot be
opened in the PC emulator).ATCommandListener
,
release()
public ATCommand(boolean csdSupport, long atResponseMaxSize) throws ATCommandFailedException
csdSupport
- A flag indicating if the instance of this class
should have CSD support or not. It might be possible
that there is an instance with CSD support created,
even if it wasn't requested if there are only
instances with CSD support available. But it can
never happen that there is CSD support requested and
an instance without is created. If CSD support is
requested and there is no instance with CSD
available an IllegalStateException will be thrown.atResponseMaxSize
- Maximum size of internal response buffer for method
#send(String ATCmd). Setting value exceeds 1048576
(1Mbytes) to this parameter will be rounded to
1048576 (1Mbytes), and Setting value less than 61440
(60Kbytes) to 61440 (60Kbytes).IllegalStateException
- if there isn't any instance of the AT
interpreter with the requested capabilities
available.ATCommandFailedException
- if the connection to the AT-Interpreter
fails because of some internal reasons (e.g.
because the virtual COM port cannot be
opened in the PC emulator).ATCommandListener
,
release()
public void addListener(ATCommandListener listener)
listener
- The ATCommandListener object to be registered.ATCommandListener
,
removeListener(com.cinterion.io.ATCommandListener)
public String breakConnection() throws IOException
Before it is switched back to AT command mode the output buffer of the OutputStream will be flushed automatically. Possible pending read operations via the InputStream stream will return after the data connection has been closed.
Calling breakConnection() to stop an ongoing data transfer of another thread provokes an IOException.
This method is exclusive for CSD data calls. It cannot be called for other procedures using the data stream mode like I2C or RSA, which have their own way to change to AT command mode.
IOException
- if there is no open data connection available, the stream
has been closed before or an IO error occurs.getDataInputStream()
,
getDataOutputStream()
public void cancelCommand() throws ATCommandFailedException
ATCommandFailedException
- if canceling command fails because of some
internal reasons.IllegalStateException
- if canceling command fails because:send(String ATCmd, ATCommandResponseListener response)
public boolean csdSupported()
public boolean getCONN()
ATCommandListener
,
addListener(com.cinterion.io.ATCommandListener)
public InputStream getDataInputStream()
If there are data connections created with the ATCommand class (typically with "ATD" or "AT^SSPI") this stream is used to read input data from the data connection. It is already open but can only be used if the module is in transparanet data mode (an open data connection exists). If the module is in AT command mode calling of any function of this stream causes an IOException. The stream remains open until the release function of the ATCommand class is called. This means that the close() function of this stream hasn't any consequence. Possible pending read() or skip() functions of this stream will return with the already read or skipped data or -1 in case of the single byte read() if the data connection is interrupted by calling the breakConnection() function. The mark feature of the InputStream class is not supported.
To avoid additional copying of transferred data the read functions are directly mapped to the same native read function which serves also the AT command handling. To avoid locks of the internal state machine the user must always read all data available in transparent data mode.
InputStream
,
breakConnection()
public OutputStream getDataOutputStream()
If there are data connections created with the ATCommand class (typically with "ATD" or "AT^SSPI") this stream is used to send output data to the data connection. It is already open but accepts only data if the module is in transparanet data mode (an open data connection exists). If the module is in AT command mode calling of any function of this stream causes an IOException. The stream remains open until the release function of the ATCommand class is called. This means that the close() function of this stream hasn't any consequence.
The call of this function blocks until all data has been handled by the native code. Because the data is buffered inside the radio stack before it is sent out to air it is possible that the function returns before the data has been left the module, though. Please note that the way of the data through the radio stack cannot be influenced any more after it has been left the write() function. So the flush() function is still available for backwards compatibility but has no effect any more.
For performance reasons there is no synchronization done in this stream class. If an instance of this class has to be accessed from different threads it has to be ensured that the write() functions and the flush() function are synchronized in the user implementation.
OutputStream
,
release()
public boolean getDCD()
ATCommandListener
,
addListener(com.cinterion.io.ATCommandListener)
public boolean getDSR()
ATCommandListener
,
addListener(com.cinterion.io.ATCommandListener)
public boolean getRING()
ATCommandListener
,
addListener(com.cinterion.io.ATCommandListener)
public void release() throws IOException
IOException
- if releasing locked resources fails because of
some internal reasonsIllegalStateException
- if releasing locked resources fails because of:
illegal instance state (e.g. released) some
internal reasonspublic void removeListener(ATCommandListener listener)
Please note that the listener callback handling has been decoupled from the internal event loop to avoid deadlocks. This has been achieved by calling each waiting listener in a separate thread. For this reason it can occur that a waiting listener might be called once even after it has been already removed. This happens when the listener thread for the corresponding event has already been started before this function is called.
listener
- The ATCommandListener object to be removed from the list.ATCommandListener
,
addListener(com.cinterion.io.ATCommandListener)
public String send(String ATCmd) throws ATCommandFailedException
Attention:
If there are strings sent to the AT command interpreter by this method which
are not valid AT commands because they don't start with "AT" they will be
ignored by the AT command interpreter, no response will be sent and this
method is blocking infinitely! The application should protect itself of
sending wrong AT commands or call the method cancelCommand() if the blocking
method send() does not return.
Continous commands which are delivering ongoing status information without ever ending or commands with very long responses are aborted after the internal response buffer has exceeded the default size (61440bytes) or the size set by #ATCommand(boolean csdSupport, long atResponseMaxSize) to avoid internal out of memory problems. If the usage of commands with oversized responses cannot be avoided the nonblocking send function must be used.
ATCmd
- The command to send.ATCommandFailedException
- if sending of the command fails because:IllegalStateException
- if sending of the command not possible
because:IllegalArgumentException
- if the command is invalid because:send(String, ATCommandResponseListener)
public void send(String strATCmd, ATCommandResponseListener Listener) throws ATCommandFailedException
If anything went wrong the response listener is called with a null as parameter to indicate the problem. If already this send function throws an exception the listener is not called anymore.
Attention:
If there are strings sent to the AT command interpreter by this method which
are not valid AT commands because they don't start with "AT" they will be
ignored by the AT command interpreter, no response will be sent and the
waiting listener will never be called! If an invalid AT command is cancelled
with the cancelCommand() function the waiting response listener will be
called with an "ERROR" as response.
For continous commands which are delivering ongoing status information without ever ending or commands with very long reponses the response listener is called every time the internal response buffer has exceeded 1024 characters with the intermediate data. This is done until the command is caneceled or has finished.
strATCmd
- The command to send.Listener
- Instance of the callback class which will receive the
response to the command.ATCommandFailedException
- if sending of the command fails because of
some internal reasonsIllegalStateException
- if sending of the command not possible
because:IllegalArgumentException
- if the command is invalid because:send(String)
,
cancelCommand()
public void setDTR(boolean SignalState) throws IOException
SignalState
- The new state of the DTR signal.IOException
- if setting of the DTR signal failed because of some
internal reasons.