modem_script()

Запустить скрипт на устройстве

Прототип:

#include <sys/modem.h>
int modem_script( int fd,
struct modem_script *table,
speed_t *baud,
void (*io)( char *progress,
char *in,
char *out ),
int (*cancel)(void) );

Аргументы:

fd
The file descriptor for the device that you want to read from; see modem_open().
table
An array of struct modem_script that comprise a script of commands that you want to run on the device.
baud
A pointer to a speed_t where the function can store the baud rate (if you script says to do so).
io
A function that's called to process each string that's emitted or received.
cancel
NULL, or a callback function that's called whenever the newquiet time period (specified in the script) expires while waiting for input.

Библиотека:

libc

Описание:

The modem_script() function runs the script table on the device associated with the file descriptor fd. The script implements a simple state machine that emits strings and waits for responses.

Each string that's emitted or received is passed to the function io() as follows:

Call Description
(*io)( str, 0, 0 ) Emitted progress string
(*io)( 0, str, 0 ) Received string
(*io)( 0, 0, str ) Emitted response string

This lets an application set up a callback that can display the script's interaction in a status window.

If you provide a cancel function, it's called once each newquiet 1/10 of a second while waiting for input. If this function returns a nonzero value, the read returns immediately with -1 and errno is set to ETIMEDOUT. You can use the cancel function as a callback in a graphical dialer that needs to support a cancel button to stop a script.

The table is an array of struct modem_script.

When this script is passed to modem_script(), the current state is set to 1, and the output is ATZ (the response in the first array element).

While in any state, modem_script() waits for input, matching it against the current state or the wildcard state of 0.

State 1

Input Action
*ok* Go to state 2 and emit ATDT1-591-0934. The flags to be used in the new state are set to 0, the quiet time in the new state is set to 5/10 of a second, and the timeout time in the new state is set to 30 seconds.
*no carrier* Go to state 0 (the termination newstate), return with the contents of retvalue (1).
*no answer* Go to state 0 (the termination newstate), return with the contents of retvalue (2).
*no dialtone* Go to state 0 (the termination newstate), return with the contents of retvalue (3).
*busy* Go to state 0 (the termination newstate), return with the contents of retvalue (4).

State 2

Input Action
*connect* Go to state 3 and don't emit anything to the device. The flags to be used in the new state are set to MODEM_LASTLINE, the quiet time in the new state is set to 5/10 of a second, and the timeout time in the new state is set to 10 seconds. Since the current flags are MODEM_BAUD, the baud rate is extracted from the connect message.
*no carrier* Same as previous table
*no answer* Same as previous table
*no dialtone* Same as previous table
*busy* Same as previous table

State 3

Input Action
*login* Go to state 4 and emit guest. The flags to be used in the new state are set to 0, the quiet time in the new state is set to 5/10 of a second, and the timeout time in the new state is set to 8 seconds.
*no carrier* Same as previous table
*no answer* Same as previous table
*no dialtone* Same as previous table
*busy* Same as previous table

State 4

Input Action
*password* Go to state 5 and emit xxxx. The flags to be used in the new state are set to 0, the quiet time in the new state is set to 5/10 of a second, and the timeout time in the new state is set to 15 seconds. Since the current flags are MODEM_NOECHO, the password response xxxx isn't sent to the io() callback.
*no carrier* Same as previous table
*no answer* Same as previous table
*no dialtone* Same as previous table
*busy* Same as previous table

State 5

Input Action
*$ * Go to state 0 (the termination newstate), return with the contents of retvalue (0).
*no carrier* Same as previous table
*no answer* Same as previous table
*no dialtone* Same as previous table
*busy* Same as previous table

If you set the flag MODEM_BAUD for a state, then any number embedded in a matching response is extracted and assigned as a number to the baud parameter.

If you don't set the flag MODEM_NOECHO for a state, then all emitted strings are also given to the passed io function as (*io)(0, 0, response).

Возвращаемое значение:

≠-1
The retvalue member of a script entry that terminates the script. This will always be a positive number.
-1
Возникла ошибка. Код ошибки записан в errno.

Коды ошибок:

EAGAIN
The O_NONBLOCK flag is set for the file descriptor, and the process would be delayed in the write operation.
EBADF
The file descriptor, fildes, isn't a valid file descriptor open for writing.
EINTR
The write operation was interrupted by a signal, and either no data was transferred, or the resource manager responsible for that file doesn't report partial transfers.
EIO
A physical I/O error occurred. The precise meaning depends on the device.
EPIPE
An attempt was made to write to a pipe (or FIFO) that isn't open for reading by any process. A SIGPIPE signal is also sent to the process.

Классификация:

ЗОСРВ «Нейтрино»

Точка остановки потока
Да
Обработчик прерываний
Нет
Обработчик сигналов
Смотри ниже
В потоке
Смотри ниже

Предостережения:

Depending on what you do in your cancel function, it might or might not be safe to call modem_script() from a signal handler or a multithreaded program.

Тематические ссылки:

struct modem_script, modem_open(), modem_read(), modem_write()




Предыдущий раздел: Описание API системной библиотеки