Version 0.28
(c) Martin Cooper KD6YAM 2020
(c) David Ranch KI6ZHD (linpac@trinnet.net) 2002 - 2020
(c) 1998 - 2001 by Radek Burget OK2JBG
2 What is an external program?
4 How applications communicate with LinPac
5 Using the application library
5.1 The simplest application
6 Application programming
6.1 Events
6.2 Sending and receiving events
6.3 Synchronization
6.4 Shared variables and configuration
6.5 Connection status
6.6 Event usage examples
6.6.1 Connecting to a remote station
6.6.2 Using LinPac commands in programs
7 The application library interface
7.1 Constants
7.2 Data structures
7.3 Functions
7.3.1 Uninterruptable versions of some system calls
7.3.2 Basic communication functions
7.3.3 Automatic event handling functions
7.3.4 Environment functions
7.3.5 User functions
7.3.6 Tool functions
7.3.7 Application information functions
8 The shell interface
8.1 Writing the script
8.1.1 Ensuring that LinPac is running
8.1.2 Accessing configuration and state
8.1.3 Executing commands
8.1.4 Simple shell example
8.2 Creating the command
Keyscan
produces an event each time any key is pressed.
All the events generated by any object are put into the common event queue. The LinPac kernel simply takes the events from the queue one by one and sends them to all the objects (including the one that generated the event). Thus each event generated by any part of LinPac is forwarded to all of the objects. The reaction of each object fully depends on its functionality, but of course it can include generation of other events.
The list of LinPac internal objects and their names can be found in the file objects.txt.
An additional feature of the API is a shared variable environment. This is a set of variables that is shared with LinPac; the values are automatically synchronized with all the external programs. These variables are accessed using special functions of the API as described below.
NOTE: All of the communication via TCP/IP sockets is provided by the LinPac application library. The user application should not attempt to access the sockets directly.
liblinpac
is
created and installed by default to /usr/local/lib
. The interface
to this library is contained in the file lpapp.h
, which is installed
by default to /usr/local/include/linpac
. The next chapter
illustrates how to use the library within a user application.
#include <stdio.h>
#include <unistd.h>
#include <linpac/lpapp.h>
int main()
{
if (lp_start_appl())
{
printf("Application started\n");
sleep(10);
printf("Application finished\n");
lp_end_appl();
}
else
{
printf("LinPac is not running\n");
return 1;
}
return 0;
}
The function lp_start_appl()
tries to contact LinPac and returns
1
in case of success or 0 when a connection to LinPac cannot be made (probably
because it's not running). This function should precede the usage of any other
application library functions.
The function lp_end_appl()
closes the connection to LinPac.
To compile this example, use:
$ gcc -o test1 test1.c -llinpac
This example just detects if LinPac is running, and it can be executed directly from the shell. When running from the shell, no streams are redirected to LinPac, and the application appears to run on channel 0 of LinPac. This is useful for some applications that are used to control LinPac from outside. However this is not a typical use case.
For most applications, it is better to copy the executable to the
$LINPACDIR/bin
directory and then add it to the file
$LINPACDIR/bin/commands
as described in the User
Manual. After this, the application can be executed as a LinPac command.
In this scenario, the streams are properly redirected and the application output
is visible in the LinPac QSO window. It is also possible to select the channel
on which to run the application.
NOTE: The LinPac application library (liblinpac
) can be
linked without problems with both C and C++ code.
An event is represented by the following structure:
struct Event
{
int type;
int chn;
int x,y;
char ch;
void *data;
};
The meaning of each field is as follows:
type
chn
chn
field contains the number of the channel which has
received that data. There are many events that apply for all the channels. For
those events, this field is not significant.x, y
y
field is usually not used, other than by some internal events.ch
data
All of the event types are described in the event list.
int lp_emit_event(int chn, int type, int x, void *data);
This generates a new event using the specified values. Each argument
corresponds to one of the fields in the Event
structure.
There are two modes for handling incoming events:
a) Reading each event on demand
This mode is started by the lp_event_handling_off()
call.
In this mode, events are read explicitly using the function:
int get_event(Event *ev);
This function returns 0 when no event is available. When an
event is available, it returns 1 and fills the Event
structure with the received event data.
WARNING #1: The data
field in your Event
structure must
point to a dynamically allocated buffer. The size of the buffer is reallocated
automatically after receiving an event. When the data
field is
set to NULL
, a new buffer is allocated. This field must be initialized.
WARNING #2: In this mode, the application must read all events. It is not a good idea to stop reading events, because the event queue may overflow. LinPac will automatically kill the application when the event queue exceeds some reasonable number of events.
b) Automatic event processing
This mode is started by the lp_event_handling_on()
call. All
events are read automatically. Programmers can define their own function
that will be called automatically when an event occurs. When no such
function is defined, all events are discarded.
The event handling function must have following prototype:
void my_event_handler(Event *ev);
(The function name may be different.) After initializing the application, the event handling function must be registered using the function:
lp_set_event_handler()
from the application library.
The following example is an application that prints the types of all events
received and stops when an EV_ABORT
event is received. This event
can be generated using the :ABort
command in LinPac.
#include <stdio.h>
#include <linpac/lpapp.h>
int aborted = 0;
//User event handling function. This function is called each time
//an event occurs
void my_event_handler(Event *ev)
{
printf("The event of type %i has been received\n", ev->type);
if (ev->type == EV_ABORT) aborted = 1;
}
int main()
{
if (lp_start_appl())
{
lp_event_handling_on(); //turn on automatic event handling
lp_set_event_handler(my_event_handler); //define own event handler
printf("Application started\n");
printf("Stop with the ':Abort' command\n");
do ; while(!aborted); //wait until application is aborted
printf("Application finished\n");
lp_end_appl();
}
else
{
printf("LinPac is not running\n");
return 1;
}
return 0;
}
This example contains "active waiting" (the do ; while(...)
construction). This is very ugly and inefficient. For this reason, the LinPac API offers an
alternative for waiting for events, namely the lp_wait_event()
function.
Let's change the example to use this function:
#include <stdio.h>
#include <linpac/lpapp.h>
int main()
{
if (lp_start_appl())
{
lp_event_handling_on(); //turn on automatic event handling
printf("Application started\n");
printf("Stop with the ':Abort' command\n");
lp_wait_event(lp_channel(), EV_ABORT); //wait for the abort event
printf("Application finished\n");
lp_end_appl();
}
else
{
printf("LinPac is not running\n");
return 1;
}
return 0;
}
WARNING: Note that some system calls may be interrupted when
an event is received. An interrupted system call returns an error result (for
example the read()
call returns -1), sets errno
to
EAGAIN
, and must be called again. To avoid this, use the interrupt-safe
versions of the system calls contained in this application library. (See
Chapter 7.3.1.)
For testing that all events were processed, there is an EV_VOID
event.
It is not handled by any module. After sending all events, just generate
an EV_VOID
event and wait until it returns. After that, you can be
assured that all previous events have been processed.
void lp_set_var(int chn, const char *name, const char *value)
char *lp_get_var(int chn, const char *name)
void lp_del_var(int chn, const char *name)
void lp_clear_var_names(int chn, const char *prefix)
The value of each variable is automatically synchronized with LinPac and all running applications. The variables whose names start with "_" are reserved for system use. These variables can be used for obtaining system configuration and status but it may be potentialy dangerous to change some of these variables.
NOTE: Many of the variables contain boolean values. Their value is 1 when true, 0 otherwise.
Currently the following system variables are defined (in channel 0):
_remote
_cbell
_knax
_def_port
_unportname
_unport
_info_level
_no_name
_timezone
_swap_edit
_fixpath
_daemon
_monitor
_no_monitor
_listen
_disable_spyd
_mon_bin
_monparms
_maxchn
_last_act
These variables may be read using the lp_get_var()
function defined
above. Furthermore there are two special functions defined for reading
these variables. These functions expect the name of the system variable
without the leading underscore.
char *lp_sconfig(const char *name)
int lp_iconfig(const char *name)
The following system variables are defined for each channel:
_call
_cwit
_cphy
_port
_state
There are also special functions for reading values of these variables:
int lp_chn_status(int chn)
ST_DISC
ST_DISP
ST_TIME
ST_CONN
ST_CONP
char *lp_chn_call(int chn)
char *lp_chn_cwit(int chn)
char *lp_chn_cphy(int chn)
int lp_chn_port(int chn)
The last two functions enable changing the time of last users's response:
time_t lp_last_activity()
void lp_set_last_activity(time_t timeval)
EV_STAT_REQ
event on this channel.
In response, LinPac generates the EV_STATUS
event. The data
field
of this event points to the ax25_status
structure (see Chapter
7.2). When there is no active connection on the channel, no EV_STATUS
event is generated.
lp_chn_status()
function (see
Chapter 6.4).
The second step is emiting the EV_CONN_LOC
event on the appropriate
channel. The data
field of the event contains a C string with
the destination address in the form port:callsign [digi [digi ...]]
.
The last step is to wait until the connection is established. For this
the function lp_wait_connect()
can be used. An example piece of
code follows:
int chn = lp_channel();
if (lp_chn_status(chn) == ST_DISC)
{
char addr[30];
strcpy(addr, "kiss:OK0PAB OK0NMA");
lp_emit_event(chn, EV_CONN_LOC, 0, addr);
lp_wait_connect(chn, "OK0PAB");
/* ... connection established ... */
}
EV_DO_COMMAND
event. For example, to download a message from a BBS using the getmsg
command following code can be used:
char cmd[30];
sprintf(cmd, "getmsg %i", message_number);
lp_emit_event(lp_channel(), EV_DO_COMMAND, 0, cmd);
Another option is to use the EV_WANT_RESULT
event. The usage
is similar to the previous example, but the x
field of the event has
special meaning: as a response to this event, LinPac will generate an
EV_CMD_RESULT
event
with the same x
field and with the data
field containing
the result of the command.
An example follows:
char cmd[30];
int id = 0;
Event *ev;
sprintf(cmd, "getmsg %i", message_number);
lp_emit_event(lp_channel(), EV_WANT_RESULT, 1234, cmd);
while (id != 1234) /* wait for the command result */
{
lp_wait_event(lp_channel(), EV_CMD_RESULT);
ev = lp_awaited_event();
id = ev->x;
}
printf("The result is: %s\n", (char *)ev.data);
LPAPP_VERSION
ST_xxxx
struct ax25_status
typedef struct
{
char devname[8];
int state;
int vs, vr, va;
int t1, t2, t3, t1max, t2max, t3max;
int idle, idlemax;
int n2, n2max;
int rtt;
int window;
int paclen;
bool dama;
int sendq, recvq;
} ax25_status;
The following functions work the same way as the original system calls, but
they are interrupt-safe. That is, they don't fail with errno == EAGAIN
.
size_t safe_read(int fd, void *buf, size_t count);
size_t safe_write(int fd, const void *buf, size_t count);
char *safe_fgets(char *s, int size, FILE *stream);
int safe_fgetc(FILE *stream);
int lp_start_appl()
int lp_get_event(Event *ev)
data
field of the event structure must be initialized before using this
function (to NULL or to some buffer). This function should not be used when
automatic event processing is used.int lp_emit_event(int chn, int type, int x, void *data)
void lp_wait_event(int chn, int type)
chn
and type
values are received.void lp_wait_init(int chn, int type)
lp_wait_event()
but returns immediately. Waiting
is provided by following function lp_wait_realize()
.void lp_wait_realize()
lp_wait_init()
. All the events
that arrived since the last lp_wait_init()
call are ignored.
lp_wait_realize()
may exit immediately if the event has already
arrived.Event *lp_awaited_event()
lp_wait_event()
or
lp_wait_realize()
, this function returns the event that stopped
the waiting.Event *lp_copy_event(Event *dest, const Event *src)
void lp_discard_event(Event *ev)
lp_get_event()
.void lp_clear_event_queue()
void lp_end_appl()
void lp_event_handling_on()
void lp_event_handling_off()
lp_get_event()
function.void lp_set_event_handler(handler_type handler)
void my_event_handler(Event *ev)
. The event handler is
called automatically each time an event is received, when automatic event
handling is enabled.void lp_appl_result(const char *fmt, ...)
EV_APP_RESULT
event with the specified message string. The
argument format is the same as for printf()
.void lp_statline(const char *fmt, ...)
EV_CHANGE_STLINE
event with the x
field
(line ID) containing the PID of the application. To display more than one
status line for the application, additional EV_CHANGE_STLINE
events must be generated manually.void lp_remove_statline()
void lp_disable_screen()
EV_DISABLE_SCREEN
event is used to accomplish this.void lp_enable_screen()
EV_ENABLE_SCREEN
event is used to accomplish this.void lp_wait_connect(int chn, const char *call)
char *time_stamp(int utc)
utc
is 0, then local time is used; otherwise UTC time is used.char *date_stamp(int utc)
void replace_macros(int chn, char *s)
%xxx
) with their
values. The %(command)
macro is not replaced.void get_port_name(int n)
char *lp_version()
int lp_channel()
int lp_app_remote()
The shell functions are all accessed through the lpapi
command,
which has the following form:
lpapi <channel> [command] [arguments]
The specifics of the available commands are described in the following sections.
The channel on which the script application is running is made available to
the script as the $LPCHN
shell environment variable. The use of
this variable is demonstrated below.
lpapi 0
This command will exit with a 0 result code if LinPac is running. Any other
result code indicates that LinPac is not running, in which case the script
should most likely echo an error message to the user and exit. As usual, the
result code can be determined by examining the special $?
value.
All of these functions return values that can be assigned to a shell variable, and are used in the following manner (using "call" as an example here):
MYVAR=`lpapi $LPCHN call`
The available functions are as follows:
call
cwit
port
state
pname <number>
get <variable>
do
command, which is functionally equivalent to generating the
EV_DO_COMMAND
event from the C API.
The do
command has the following form:
lpapi <channel> do <command> [arguments]
However, since do
is a reserved word in Bash shell scripts,
it is necessary to use Bash's eval
command to invoke it as a
LinPac function, as follows:
eval lpapi <channel> do "<command> [arguments]"
IMPORTANT: The do
command is asynchronous.
That is, the shell command may return before the command issued to LinPac has
completed. A script will need to account for this in considering subsequent
actions.
$LINPACDIR/bin
directory, making
sure that the permissions allow execution.$LINPACDIR/bin/commands
as
described in the User Manual.Last update: 16 Feb 2020