Debian Codegraph @

doc/applguide.html

raw symbols SCIP
1 <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5 <title>LinPac External Application Programming Guide</title>
6 <style>
7 body {
8 width: 650px;
9 margin: 50px;
10 line-height: 120%;
11 }
12 code {
13 background-color: #eee;
14 border-radius: 3px;
15 }
16 code > hr {
17 display: none;
18 }
19 pre > code {
20 border: 1px solid #999;
21 display: block;
22 padding: 0px 20px 20px 20px;
23 }
24 :not(pre) > code {
25 padding: 0px 2px 2px;
26 }
27 </style>
28 </head>
29 <body>
30
31 <h1><center>LinPac - Packet Radio Terminal for Linux</center></h1>
32 <hr>
33 <center>
34 <p>Version 0.28</p>
35 <p>
36 (c) Martin Cooper KD6YAM 2020<br>
37 (c) David Ranch KI6ZHD ([email protected]) 2002 - 2020<br>
38 (c) 1998 - 2001 by Radek Burget OK2JBG
39 </p>
40 </center>
41
42 <h2><center>External Application Programming Guide</center></h2>
43
44 <b>Contents</b>
45 <p>1 <a href="#POS1">Introduction</a>
46 <p>2 <a href="#POS2">What is an external program?</a>
47 <p>3 <a href="#POS3">Basic concepts of the API</a>
48 <p>4 <a href="#POS4">How applications communicate with LinPac</a>
49 <p>5 <a href="#POS5">Using the application library</a>
50 <br>5.1 <a href="#POS5_1">The simplest application</a>
51 <p>6 <a href="#POS6">Application programming</a>
52 <br>6.1 <a href="#POS6_1">Events</a>
53 <br>6.2 <a href="#POS6_2">Sending and receiving events</a>
54 <br>6.3 <a href="#POS6_3">Synchronization</a>
55 <br>6.4 <a href="#POS6_4">Shared variables and configuration</a>
56 <br>6.5 <a href="#POS6_5">Connection status</a>
57 <br>6.6 <a href="#POS6_6">Event usage examples</a>
58 <br>6.6.1 <a href="#POS6_6_1">Connecting to a remote station</a>
59 <br>6.6.2 <a href="#POS6_6_2">Using LinPac commands in programs</a>
60 <p>7 <a href="#POS7">The application library interface</a>
61 <br>7.1 <a href="#POS7_1">Constants</a>
62 <br>7.2 <a href="#POS7_2">Data structures</a>
63 <br>7.3 <a href="#POS7_3">Functions</a>
64 <br>7.3.1 <a href="#POS7_3_1">Uninterruptable versions of some system calls</a>
65 <br>7.3.2 <a href="#POS7_3_2">Basic communication functions</a>
66 <br>7.3.3 <a href="#POS7_3_3">Automatic event handling functions</a>
67 <br>7.3.4 <a href="#POS7_3_4">Environment functions</a>
68 <br>7.3.5 <a href="#POS7_3_5">User functions</a>
69 <br>7.3.6 <a href="#POS7_3_6">Tool functions</a>
70 <br>7.3.7 <a href="#POS7_3_7">Application information functions</a>
71 <p>8 <a href="#POS8">The shell interface</a>
72 <br>8.1 <a href="#POS8_1">Writing the script</a>
73 <br>8.1.1 <a href="#POS8_1_1">Ensuring that LinPac is running</a>
74 <br>8.1.2 <a href="#POS8_1_2">Accessing configuration and state</a>
75 <br>8.1.3 <a href="#POS8_1_3">Executing commands</a>
76 <br>8.1.4 <a href="#POS8_1_4">Simple shell example</a>
77 <br>8.2 <a href="#POS8_2">Creating the command</a>
78
79 <p><a NAME="POS1"></a>
80 <h2>1 Introduction</h2>
81 This guide is written for programmers who want to add some new functions
82 to LinPac. In the following text, basic knowledge of Linux programming
83 is assumed. Reading the LinPac User Manual before proceeding is also
84 recommended.
85
86 <p><a NAME="POS2"></a>
87 <h2>2 What is an external program?</h2>
88 An external program is a standard Linux application which uses LinPac to
89 communicate with remote stations. There are basically two types of LinPac
90 external programs:
91 <ul>
92 <li>
93 Standard Linux applications that use standard input and output streams.
94 These programs can be used without LinPac too. LinPac allows redirection
95 of the input stream (stdin) of the application and any of the output streams
96 (stdout, stderr) or both of them. LinPac can also provide CR/LF conversions
97 in these streams. These parameters are set when adding the external program
98 to LinPac - see <a href="manual.html#POS7">Section 7</a>, "Creating
99 new commands", in the User Manual.</li>
100
101 <li>
102 Applications written using LinPac's application interface. These applications
103 can share some information with LinPac, and they can control almost all
104 functions of LinPac.</li>
105 </ul>
106 No special knowledge of LinPac is needed for creating the first type
107 of programs, because no LinPac functions are used. The remainder of
108 this guide is dedicated to the second type of applications.
109
110 <p><a NAME="POS3"></a>
111 <h2>3 Basic concepts of the API</h2>
112 The LinPac application interface is based on <em>events</em>. Every action
113 in LinPac, such as pressing a key, receiving data, executing a command,
114 etc., is represented by a data structure called an <em>event</em>, which describes
115 exactly what happened. LinPac's internal structure consists of separate
116 <em>objects</em> which
117 communicate with each other using these events. Each object provides its
118 own functionality, and often generates events. For example, the object
119 <code>Keyscan</code> produces an event each time any key is pressed.
120 <p>All the events generated by any object are put into the common <em>event
121 queue</em>. The LinPac kernel simply takes the events from the queue one by
122 one and sends them to all the objects (including the one that generated
123 the event). Thus <strong>each event generated by any part of LinPac is forwarded
124 to all of the objects</strong>. The reaction of each object fully depends on its
125 functionality, but of course it can include generation of other events.
126 <p>The list of LinPac internal objects and their names can be found in
127 the file <a href="objects.txt">objects.txt</a>.
128 <p>An additional feature of the API is a shared variable environment. This
129 is a set of variables that is shared with LinPac; the values are automatically
130 synchronized with all the external programs. These variables are accessed
131 using special functions of the API as described below.
132
133 <p><a NAME="POS4"></a>
134 <h2>4 How applications communicate with LinPac</h2>
135 LinPac uses TCP/IP sockets for communication with external
136 programs (applications). The connection is always initiated by the application
137 after it starts. There are basically two ways that an application may be started:
138 <ul>
139 <li>
140 The application is started by LinPac, through being associated with a LinPac command.
141 In this case LinPac registers the channel on which the application was started
142 and expects the connection attempt from the application. After the application
143 initializes and contacts LinPac, it is provided with the working channel and whether
144 it was started by a remote or local user. LinPac may also redirect the input
145 and output streams of the application (depending upon LinPac settings).</li>
146
147 <li>
148 The application is started without using LinPac. In this case LinPac assumes
149 that the application runs on channel 0 and was started by the local user. The
150 input and output streams of the application are not redirected.</li>
151 </ul>
152 In both cases, the start of an application causes the creation of a new
153 object in LinPac called an <em>event gate</em>. This object represents the application
154 within LinPac. Bearing in mind the previous chapter, this means that the application
155 is forwarded all of the events generated by any LinPac object, and all of the
156 events generated by the application are forwarded to all LinPac objects.
157 <p><strong>NOTE:</strong> All of the communication via TCP/IP sockets is provided by
158 the LinPac application library. The user application should not attempt to access the
159 sockets directly.
160
161 <p><a NAME="POS5"></a>
162 <h2>
163 5 Using the application library</h2>
164 During LinPac installation, the application library <code>liblinpac</code> is
165 created and installed by default to <code>/usr/local/lib</code>. The interface
166 to this library is contained in the file <code>lpapp.h</code>, which is installed
167 by default to <code>/usr/local/include/linpac</code>. The next chapter
168 illustrates how to use the library within a user application.
169
170 <p><a NAME="POS5_1"></a>
171 <h3>5.1 The simplest application</h3>
172 The following application example just tries to contact LinPac, and prints the
173 result.
174
175 <pre><code><hr>
176 #include &lt;stdio.h&gt;
177 #include &lt;unistd.h&gt;
178 #include &lt;linpac/lpapp.h&gt;
179
180 int main()
181 {
182 if (lp_start_appl())
183 {
184 printf("Application started\n");
185 sleep(10);
186 printf("Application finished\n");
187 lp_end_appl();
188 }
189 else
190 {
191 printf("LinPac is not running\n");
192 return 1;
193 }
194 return 0;
195 }
196 <hr></code></pre>
197
198 <p>The function <code>lp_start_appl()</code> tries to contact LinPac and returns
199 <strong>1</strong>
200 in case of success or <strong>0</strong> when a connection to LinPac cannot be made (probably
201 because it's not running). This function should precede the usage of any other
202 application library functions.
203 <p>The function <code>lp_end_appl()</code> closes the connection to LinPac.
204 <p>To compile this example, use:
205 <p><code>$ gcc -o test1 test1.c -llinpac</code>
206 <p>This example just detects if LinPac is running, and it can be executed
207 directly from the shell. When running from the shell, no streams are redirected
208 to LinPac, and the application appears to run on channel 0 of LinPac. This is
209 useful for some applications that are used to control LinPac from outside.
210 However this is not a typical use case.
211 <p>For most applications, it is better to copy the executable to the
212 <code>$LINPACDIR/bin</code>
213 directory and then add it to the file
214 <code>$LINPACDIR/bin/commands</code> as described in the <a href="manual.html">User
215 Manual</a>. After this, the application can be executed as a LinPac command.
216 In this scenario, the streams are properly redirected and the application output
217 is visible in the LinPac QSO window. It is also possible to select the channel
218 on which to run the application.
219 <p><strong>NOTE:</strong> The LinPac application library (<code>liblinpac</code>) can be
220 linked without problems with both C and C++ code.
221
222 <p><a NAME="POS6"></a>
223 <h2>6 Application programming</h2>
224
225 <a NAME="POS6_1"></a>
226 <h3>6.1 Events</h3>
227 LinPac is completely driven by events. Each part of LinPac, including
228 applications, can generate events to inform other parts (internal modules
229 or applications) that something has happened. Each event is sent to all
230 LinPac components and applications. For example when some station connects
231 to some LinPac channel, the internal AX.25 interface generates an event
232 reporting that the station has connected and includes its callsign. All
233 components and applications now know who has connected, and they can take
234 some actions. For example, the output window prints information about the connection,
235 the macro processor executes the cinit.mac macro, and so on. Each application
236 can handle all of the events too, and can generate events which are handled
237 by other components.
238 <p>An event is represented by the following structure:
239 <pre><code>
240 struct Event
241 {
242 int type;
243 int chn;
244 int x,y;
245 char ch;
246 void *data;
247 };
248 </code></pre>
249 <p>The meaning of each field is as follows:
250 <dl>
251 <dt><code>type</code></dt>
252 <dd>Specifies the type of the event. Actually it determines what happened.
253 There is a symbolic constant defined for each known event type.</dd>
254 <dt><code>chn</code></dt>
255 <dd>Specifies the channel to which the event applies.
256 For example, if the type of the event specifies that some data was received,
257 the <code>chn</code> field contains the number of the channel which has
258 received that data. There are many events that apply for all the channels. For
259 those events, this field is not significant.</dd>
260 <dt><code>x, y</code></dt>
261 <dd>The meaning of these fields depends on the event type. The <code>y</code>
262 field is usually not used, other than by some internal events.</dd>
263 <dt><code>ch</code></dt>
264 <dd>This field is used only by some internal events.</dd>
265 <dt><code>data</code></dt>
266 <dd>Depends on the type of the event. It usually points to some string data
267 or a char buffer.</dd>
268 </dl>
269 <p>All of the event types are described in the <a href="events.txt">event
270 list</a>.
271
272 <p><a NAME="POS6_2"></a>
273 <h3>6.2 Sending and receiving events</h3>
274 For sending events the following function is used:
275 <p><code>int lp_emit_event(int chn, int type, int x, void *data);</code>
276 <p>This generates a new event using the specified values. Each argument
277 corresponds to one of the fields in the <code>Event</code> structure.
278 <p>There are two modes for handling incoming events:
279 <p><strong><em>a) Reading each event on demand</em></strong>
280 <p>This mode is started by the <code>lp_event_handling_off()</code> call.
281 In this mode, events are read explicitly using the function:
282 <p><code>int get_event(Event *ev);</code>
283 <p>This function returns <strong>0</strong> when no event is available. When an
284 event is available, it returns <strong>1</strong> and fills the <code>Event</code>
285 structure with the received event data.
286 <p><strong>WARNING #1:</strong> The <code>data</code> field in your <code>Event</code> structure must
287 point to a dynamically allocated buffer. The size of the buffer is reallocated
288 automatically after receiving an event. When the <code>data</code> field is
289 set to <code>NULL</code>, a new buffer is allocated. This field <em>must be initialized</em>.
290 <p><strong>WARNING #2:</strong> In this mode, the application <em>must</em> read all events.
291 It is not a good idea to stop reading events, because the event
292 queue may overflow. LinPac will automatically kill the application when the
293 event queue exceeds some reasonable number of events.
294 <p><strong><em>b) Automatic event processing</em></strong>
295 <p>This mode is started by the <code>lp_event_handling_on()</code> call. All
296 events are read automatically. Programmers can define their own function
297 that will be called automatically when an event occurs. When no such
298 function is defined, all events are discarded.
299 <p>The event handling function must have following prototype:
300 <p><code>void my_event_handler(Event *ev);</code>
301 <p>(The function name may be different.) After initializing the application,
302 the event handling function must be registered using the function:
303 <p><code>lp_set_event_handler()</code>
304 <p>from the application library.
305 <p>The following example is an application that prints the types of all events
306 received and stops when an <code>EV_ABORT</code> event is received. This event
307 can be generated using the <code>:ABort</code> command in LinPac.
308
309 <pre><code><hr>
310 #include &lt;stdio.h&gt;
311 #include &lt;linpac/lpapp.h&gt;
312
313 int aborted = 0;
314
315 //User event handling function. This function is called each time
316 //an event occurs
317 void my_event_handler(Event *ev)
318 {
319 printf("The event of type %i has been received\n", ev->type);
320 if (ev->type == EV_ABORT) aborted = 1;
321 }
322
323 int main()
324 {
325 if (lp_start_appl())
326 {
327 lp_event_handling_on(); //turn on automatic event handling
328 lp_set_event_handler(my_event_handler); //define own event handler
329
330 printf("Application started\n");
331 printf("Stop with the ':Abort' command\n");
332
333 do ; while(!aborted); //wait until application is aborted
334
335 printf("Application finished\n");
336 lp_end_appl();
337 }
338 else
339 {
340 printf("LinPac is not running\n");
341 return 1;
342 }
343
344 return 0;
345 }
346 <hr></code></pre>
347
348 <p>This example contains "active waiting" (the <code>do ; while(...)</code>
349 construction). This is very ugly and inefficient. For this reason, the LinPac API offers an
350 alternative for waiting for events, namely the <code>lp_wait_event()</code> function.
351 Let's change the example to use this function:
352
353 <pre><code><hr>
354 #include &lt;stdio.h&gt;
355 #include &lt;linpac/lpapp.h&gt;
356
357 int main()
358 {
359 if (lp_start_appl())
360 {
361 lp_event_handling_on(); //turn on automatic event handling
362
363 printf("Application started\n");
364 printf("Stop with the ':Abort' command\n");
365
366 lp_wait_event(lp_channel(), EV_ABORT); //wait for the abort event
367
368 printf("Application finished\n");
369 lp_end_appl();
370 }
371 else
372 {
373 printf("LinPac is not running\n");
374 return 1;
375 }
376
377 return 0;
378 }
379 <hr></code></pre>
380
381 <p><strong>WARNING:</strong> Note that some system calls may be interrupted when
382 an event is received. An interrupted system call returns an error result (for
383 example the <code>read()</code> call returns -1), sets <code>errno</code> to
384 <code>EAGAIN</code>, and must be called again. To avoid this, use the interrupt-safe
385 versions of the system calls contained in this application library. (See
386 <a href="#POS7_3_1">Chapter 7.3.1</a>.)
387
388 <p><a NAME="POS6_3"></a>
389 <h3>6.3 Synchronization</h3>
390 An event generated by an application is sent to all modules and applications,
391 including the application that generated the event. When there is a
392 need to wait until the event is accepted by LinPac, the simplest way is
393 to wait until the event we have sent is received back.
394 <p>For testing that all events were processed, there is an <code>EV_VOID</code> event.
395 It is not handled by any module. After sending all events, just generate
396 an <code>EV_VOID</code> event and wait until it returns. After that, you can be
397 assured that all previous events have been processed.
398
399 <p><a NAME="POS6_4"></a>
400 <h3>6.4 Shared variables and configuration</h3>
401 Each shared variable is denoted by its name (string) and channel number. Each
402 variable has a value represented by a string (char[]). Access to
403 these variables is provided by following functions:
404 <dl>
405 <dt><code>void lp_set_var(int chn, const char *name, const char *value)</code></dt>
406 <dd>Changes the value of the variable. If the variable doesn't exist, it
407 is created.</dd>
408 <dt><code>char *lp_get_var(int chn, const char *name)</code></dt>
409 <dd>Reads the value of the variable. Returns NULL if the variable doesn't
410 exist.</dd>
411 <dt><code>void lp_del_var(int chn, const char *name)</code></dt>
412 <dd>Deletes the variable.</dd>
413 <dt><code>void lp_clear_var_names(int chn, const char *prefix)</code></dt>
414 <dd>Deletes all variables with the specified name.</dd>
415 </dl>
416 <p>The value of each variable is automatically synchronized with LinPac
417 and all running applications. The variables whose names start with "_"
418 are reserved for system use. These variables can be used for obtaining
419 system configuration and status but it may be potentialy dangerous to change
420 some of these variables.
421 <p><strong>NOTE:</strong> Many of the variables contain boolean values. Their
422 value is <strong>1</strong> when true, <strong>0</strong> otherwise.
423 <p>Currently the following system variables are defined (in channel 0):
424
425 <dl>
426 <dt><code>_remote</code></dt>
427 <dd>1 when remote commands are enabled</dd>
428 </dl>
429 <dl>
430 <dt><code>_cbell</code></dt>
431 <dd>1 when connect sound is enabled</dd>
432 <dt><code>_knax</code></dt>
433 <dd>1 when sound signal for each frame is enabled</dd>
434 </dl>
435 <dl>
436 <dt><code>_def_port</code></dt>
437 <dd>default port name</dd>
438 <dt><code>_unportname</code></dt>
439 <dd>port name for unproto frames</dd>
440 <dt><code>_unport</code></dt>
441 <dd>port number for unproto frames</dd>
442 </dl>
443 <dl>
444 <dt><code>_info_level</code></dt>
445 <dd>status-line level (0 to 2)</dd>
446 <dt><code>_no_name</code></dt>
447 <dd>default station name</dd>
448 <dt><code>_timezone</code></dt>
449 <dd>local timezone name</dd>
450 <dt><code>_swap_edit</code></dt>
451 <dd>1 when swapedit is on</dd>
452 <dt><code>_fixpath</code></dt>
453 <dd>1 when fixpath is on</dd>
454 <dt><code>_daemon</code></dt>
455 <dd>1 when linpac works as a daemon</dd>
456 <dt><code>_monitor</code></dt>
457 <dd>1 when monitor is enabled</dd>
458 <dt><code>_no_monitor</code></dt>
459 <dd>1 when monitor is disabled from command line</dd>
460 <dt><code>_listen</code></dt>
461 <dd>1 when accepting connections is enabled</dd>
462 <dt><code>_disable_spyd</code></dt>
463 <dd>1 when ax25spyd support is disabled from command line</dd>
464 <dt><code>_mon_bin</code></dt>
465 <dd>1 when monitor filters binary characters</dd>
466 <dt><code>_monparms</code></dt>
467 <dd>command line arguments for 'listen' program</dd>
468 <dt><code>_maxchn</code></dt>
469 <dd>maximal number of channels</dd>
470 <dt><code>_last_act</code></dt>
471 <dd>time of last user activity (same format as the time() system call).</dd>
472 </dl>
473
474 <p>These variables may be read using the <code>lp_get_var()</code> function defined
475 above. Furthermore there are two special functions defined for reading
476 these variables. These functions expect the name of the system variable
477 <em>without</em> the leading underscore.
478
479 <dl>
480 <dt><code>char *lp_sconfig(const char *name)</code></dt>
481 <dd>Returns the value of the configuration variable as a string.</dd>
482 <dt><code>int lp_iconfig(const char *name)</code></dt>
483 <dd>Returns the value of the configuration variable as an integer.</dd>
484 </dl>
485
486 <p>The following system variables are defined for each channel:
487
488 <dl>
489 <dt><code>_call</code></dt>
490 <dd>callsign for each channel</dd>
491 <dt><code>_cwit</code></dt>
492 <dd>connected with callsign</dd>
493 <dt><code>_cphy</code></dt>
494 <dd>physical connection to</dd>
495 <dt><code>_port</code></dt>
496 <dd>connected on which port</dd>
497 <dt><code>_state</code></dt>
498 <dd>connection status</dd>
499 </dl>
500
501 <p>There are also special functions for reading values of these variables:
502
503 <dl>
504 <dt><code>int lp_chn_status(int chn)</code></dt>
505 <dd>Returns the status of a channel. The following status constants are defined:
506 <dl>
507 <dt><code>ST_DISC</code></dt>
508 <dd>channel disconnected</dd>
509 <dt><code>ST_DISP</code></dt>
510 <dd>disconnect in progress</dd>
511 <dt><code>ST_TIME</code></dt>
512 <dd>disconnecting for timeout</dd>
513 <dt><code>ST_CONN</code></dt>
514 <dd>channel connected</dd>
515 <dt><code>ST_CONP</code></dt>
516 <dd>connecting in progress</dd>
517 </dl>
518 </dd>
519 <dt><code>char *lp_chn_call(int chn)</code></dt>
520 <dd>Returns channel callsign.</dd>
521 <dt><code>char *lp_chn_cwit(int chn)</code></dt>
522 <dd>Returns the callsign of connected station.</dd>
523 <dt><code>char *lp_chn_cphy(int chn)</code></dt>
524 <dd>Returns the callsign of physicaly connected station (the first connected station).</dd>
525 <dt><code>int lp_chn_port(int chn)</code></dt>
526 <dd>Returns the number of the port used for connection.</dd>
527 </dl>
528
529 <p>The last two functions enable changing the time of last users's response:
530
531 <dl>
532 <dt><code>time_t lp_last_activity()</code></dt>
533 <dd>Returns the time of last activity of the user.</dd>
534 <dt><code>void lp_set_last_activity(time_t timeval)</code></dt>
535 <dd>Sets the last activity time.</dd>
536 </dl>
537
538 <p><a NAME="POS6_5"></a>
539 <h3>6.5 Connection status</h3>
540 There are two special events reserved for obtaining the AX.25 connection
541 status. When an application wants to get the status of the connection
542 on a certain LinPac channel, it generates the <code>EV_STAT_REQ</code> event on this channel.
543 In response, LinPac generates the <code>EV_STATUS</code> event. The <code>data</code> field
544 of this event points to the <code>ax25_status</code> structure (see <a href="#POS7_2">Chapter
545 7.2</a>). When there is no active connection on the channel, no <code>EV_STATUS</code>
546 event is generated.
547
548 <p><a NAME="POS6_6"></a>
549 <h3>6.6 Event usage examples</h3>
550 The following examples show how to use events for controlling LinPac. The
551 complete list of events can be found in the file <a href="events.txt">events.txt</a>.
552
553 <p><a NAME="POS6_6_1"></a>
554 <h4>6.6.1 Connecting to a remote station</h4>
555 When initiating the connection, the first step is to check that the channel
556 is free (i.e. it is not in use for another connection). The status of a channel
557 can be checked using the <code>lp_chn_status()</code> function (see
558 <a href="#POS6_4">Chapter 6.4</a>).
559 <p>The second step is emiting the <code>EV_CONN_LOC</code> event on the appropriate
560 channel. The <code>data</code> field of the event contains a C string with
561 the destination address in the form <code>port:callsign [digi [digi ...]]</code>.
562 <p>The last step is to wait until the connection is established. For this
563 the function <code>lp_wait_connect()</code> can be used. An example piece of
564 code follows:
565
566 <pre><code><hr>
567 int chn = lp_channel();
568 if (lp_chn_status(chn) == ST_DISC)
569 {
570 char addr[30];
571 strcpy(addr, "kiss:OK0PAB OK0NMA");
572 lp_emit_event(chn, EV_CONN_LOC, 0, addr);
573 lp_wait_connect(chn, "OK0PAB");
574 /* ... connection established ... */
575 }
576 <hr></code></pre>
577
578 <p><a NAME="POS6_6_2"></a>
579 <h4>6.6.2 Using LinPac commands in programs</h4>
580 Any application may run LinPac commands by emitting the <code>EV_DO_COMMAND</code>
581 event. For example, to download a message from a BBS using the <code>getmsg</code>
582 command following code can be used:
583
584 <pre><code><hr>
585 char cmd[30];
586 sprintf(cmd, "getmsg %i", message_number);
587 lp_emit_event(lp_channel(), EV_DO_COMMAND, 0, cmd);
588 <hr></code></pre>
589
590 <p>Another option is to use the <code>EV_WANT_RESULT</code> event. The usage
591 is similar to the previous example, but the <code>x</code> field of the event has
592 special meaning: as a response to this event, LinPac will generate an
593 <code>EV_CMD_RESULT</code> event
594 with the same <code>x</code> field and with the <code>data</code> field containing
595 the result of the command.
596 <p>An example follows:
597
598 <pre><code><hr>
599 char cmd[30];
600 int id = 0;
601 Event *ev;
602
603 sprintf(cmd, "getmsg %i", message_number);
604 lp_emit_event(lp_channel(), EV_WANT_RESULT, 1234, cmd);
605 while (id != 1234) /* wait for the command result */
606 {
607 lp_wait_event(lp_channel(), EV_CMD_RESULT);
608 ev = lp_awaited_event();
609 id = ev->x;
610 }
611 printf("The result is: %s\n", (char *)ev.data);
612 <hr></code></pre>
613
614 <p><a NAME="POS7"></a>
615 <h2>7 The application library interface</h2>
616 <a NAME="POS7_1"></a>
617 <h3>7.1 Constants</h3>
618
619 <dl>
620 <dt><code>LPAPP_VERSION</code></dt>
621 <dd>the version of LinPac that the library came with</dd>
622 <dt><code>ST_xxxx</code></dt>
623 <dd>connection status constants (see <a href="#POS6_4">Chapter 6.4</a>)</dd>
624 </dl>
625
626 <p><a NAME="POS7_2"></a>
627 <h3>7.2 Data structures</h3>
628
629 <dl>
630 <dt><code>struct ax25_status</code></dt>
631 <dd>contains the AX.25 connection status:</dd>
632 </dl>
633
634 <pre><code>
635 typedef struct
636 {
637 char devname[8];
638 int state;
639 int vs, vr, va;
640 int t1, t2, t3, t1max, t2max, t3max;
641 int idle, idlemax;
642 int n2, n2max;
643 int rtt;
644 int window;
645 int paclen;
646 bool dama;
647 int sendq, recvq;
648 } ax25_status;
649 </code></pre>
650
651 <p><a NAME="POS7_3"></a>
652 <h3>7.3 Functions</h3>
653 <a NAME="POS7_3_1"></a>
654 <h4>7.3.1 Uninterruptable versions of some system calls</h4>
655 <p>The following functions work the same way as the original system calls, but
656 they are interrupt-safe. That is, they don't fail with <code>errno == EAGAIN</code>.
657
658 <dl>
659 <dt><code>size_t safe_read(int fd, void *buf, size_t count);</code></dt>
660 <dt><code>size_t safe_write(int fd, const void *buf, size_t count);</code></dt>
661 <dt><code>char *safe_fgets(char *s, int size, FILE *stream);</code></dt>
662 <dt><code>int safe_fgetc(FILE *stream);</code></dt>
663 </dl>
664
665 <p><a NAME="POS7_3_2"></a>
666 <h4>7.3.2 Basic communication functions</h4>
667
668 <dl>
669 <dt><code>int lp_start_appl()</code></dt>
670 <dd>Starts communication with LinPac. A non-zero return value means success; a
671 zero value means that LinPac cannot be contacted (probably because it's not
672 running).</dd>
673 <dt><code>int lp_get_event(Event *ev)</code></dt>
674 <dd>Read an event from the queue. A non-zero return value means a successful
675 read; a zero value means that the event queue is empty. The <code>data</code>
676 field of the event structure <em>must</em> be initialized before using this
677 function (to NULL or to some buffer). This function should not be used when
678 automatic event processing is used.</dd>
679 <dt><code>int lp_emit_event(int chn, int type, int x, void *data)</code></dt>
680 <dd>Generate a new event. The arguments correspond to the fields in the
681 event structure. The return value is always 0.</dd>
682 <dt><code>void lp_wait_event(int chn, int type)</code></dt>
683 <dd>Wait until an event with the same <code>chn</code> and <code>type</code>
684 values are received.</dd>
685 <dt><code>void lp_wait_init(int chn, int type)</code></dt>
686 <dd>The same as <code>lp_wait_event()</code> but returns immediately. Waiting
687 is provided by following function <code>lp_wait_realize()</code>.</dd>
688 <dt><code>void lp_wait_realize()</code></dt>
689 <dd>Realizes waiting initialized by <code>lp_wait_init()</code>. All the events
690 that arrived since the last <code>lp_wait_init()</code> call are ignored.
691 <code>lp_wait_realize()</code> may exit immediately if the event has already
692 arrived.</dd>
693 <dt><code>Event *lp_awaited_event()</code></dt>
694 <dd>After return from a call to <code>lp_wait_event()</code> or
695 <code>lp_wait_realize()</code>, this function returns the event that stopped
696 the waiting.</dd>
697 <dt><code>Event *lp_copy_event(Event *dest, const Event *src)</code></dt>
698 <dd>Copy the event structure (using a deep copy).</dd>
699 <dt><code>void lp_discard_event(Event *ev)</code></dt>
700 <dd>Free the memory used by the data field of Event structure received
701 using <code>lp_get_event()</code>.</dd>
702 <dt><code>void lp_clear_event_queue()</code></dt>
703 <dd>Remove all events from the event queue. This has no purpose when automatic
704 event processing is enabled.</dd>
705 <dt><code>void lp_end_appl()</code></dt>
706 <dd>Closes the connection to LinPac.</dd>
707 </dl>
708
709 <p><a NAME="POS7_3_3"></a>
710 <h4>7.3.3 Automatic event handling functions</h4>
711
712 <dl>
713 <dt><code>void lp_event_handling_on()</code></dt>
714 <dd>Enables automatic event handling. From this point onwards, each event
715 is automatically read from the queue, treated with an event handler function
716 (if defined) and then discarded.</dd>
717 <dt><code>void lp_event_handling_off()</code></dt>
718 <dd>Disables automatic event handling. Events must be read from the queue
719 using the <code>lp_get_event()</code> function.</dd>
720 <dt><code>void lp_set_event_handler(handler_type handler)</code></dt>
721 <dd>Defines the event handler function, a function with signature
722 <code>void my_event_handler(Event *ev)</code>. The event handler is
723 called automatically each time an event is received, when automatic event
724 handling is enabled.</dd>
725 </dl>
726
727 <p><a NAME="POS7_3_4"></a>
728 <h4>7.3.4 Environment functions</h4>
729 LinPac has its own environment for storing variables. Each application
730 can share and modify this environment using environment-related functions. Each
731 channel has its own environment. See <a href="#POS6_4">Chapter 6.4</a> for a
732 detailed description and list of functions.
733
734 <p><a NAME="POS7_3_5"></a>
735 <h4>7.3.5 User functions</h4>
736
737 <dl>
738 <dt><code>void lp_appl_result(const char *fmt, ...)</code></dt>
739 <dd>Set the final result of the application. This function generates an
740 <code>EV_APP_RESULT</code> event with the specified message string. The
741 argument format is the same as for <code>printf()</code>.</dd>
742 <dt><code>void lp_statline(const char *fmt, ...)</code></dt>
743 <dd>Displays or changes the additional status line in the LinPac user
744 interface. Only one additional status line is displayed. This function
745 generates a <code>EV_CHANGE_STLINE</code> event with the <code>x</code> field
746 (line ID) containing the PID of the application. To display more than one
747 status line for the application, additional <code>EV_CHANGE_STLINE</code>
748 events must be generated manually.</dd>
749 <dt><code>void lp_remove_statline()</code></dt>
750 <dd>Removes the additional status line.</dd>
751 <dt><code>void lp_disable_screen()</code></dt>
752 <dd>Disables displaying data in the QSO window on application's channel.
753 The <code>EV_DISABLE_SCREEN</code> event is used to accomplish this.</dd>
754 <dt><code>void lp_enable_screen()</code></dt>
755 <dd>Enables displaying data in the QSO window. The <code>EV_ENABLE_SCREEN</code>
756 event is used to accomplish this.</dd>
757 <dt><code>void lp_wait_connect(int chn, const char *call)</code></dt>
758 <dd>Waits for a connection with the specified callsign on the specified
759 channel.</dd>
760 </dl>
761
762 <p><a NAME="POS7_3_6"></a>
763 <h4>7.3.6 Tool functions</h4>
764
765 <dl>
766 <dt><code>char *time_stamp(int utc)</code></dt>
767 <dd>Returns a pointer to a C string that contains the actual time. If
768 <code>utc</code> is 0, then local time is used; otherwise UTC time is used.</dd>
769 <dt><code>char *date_stamp(int utc)</code></dt>
770 <dd>Returns the date-string.</dd>
771 <dt><code>void replace_macros(int chn, char *s)</code></dt>
772 <dd>Replaces macro variables in the string (i.e. <code>%xxx</code>) with their
773 values. The <code>%(command)</code> macro is not replaced.</dd>
774 <dt><code>void get_port_name(int n)</code></dt>
775 <dd>Returns the name of the nth port in axports (starting from 0).</dd>
776 </dl>
777
778 <p><a NAME="POS7_3_7"></a>
779 <h4>7.3.7 Application information functions</h4>
780
781 <dl>
782 <dt><code>char *lp_version()</code></dt>
783 <dd>Returns the current LinPac version.</dd>
784 <dt><code>int lp_channel()</code></dt>
785 <dd>Returns the channel number on which the application was started. Returns
786 0 when the application was not started using LinPac.</dd>
787 <dt><code>int lp_app_remote()</code></dt>
788 <dd>Returns a non-zero value if the application was started using a remote
789 command in LinPac (i.e. the application is communicating with a remote user).</dd>
790 </dl>
791
792 <p><a NAME="POS8"></a>
793 <h2>8 The shell interface</h2>
794 In addition to the C API described in the above chapters, LinPac also provides
795 a set of functions that allow programmers to create new LinPac commands using
796 bash shell scripts. While this set of functions is limited, it is sufficient
797 to enable quite sophisticated and complex commands to be added to LinPac
798 without requiring programming in C or C++.
799
800 <p><a NAME="POS8_1"></a>
801 <h3>8.1 Writing the script</h3>
802
803 <p>The shell functions are all accessed through the <code>lpapi</code> command,
804 which has the following form:
805 <p><code>lpapi &lt;channel&gt; [command] [arguments]</code>
806 <p>The specifics of the available commands are described in the following sections.
807 <p>The channel on which the script application is running is made available to
808 the script as the <code>$LPCHN</code> shell environment variable. The use of
809 this variable is demonstrated below.
810
811 <p><a NAME="POS8_1_1"></a>
812 <h4>8.1.1 Ensuring that LinPac is running</h4>
813 The first step in any LinPac shell command should be to ensure that LinPac is
814 running, such that subsequent use of the shell interface can proceed. This is
815 accomplished by calling the API as follows:
816 <p><code>lpapi 0</code>
817 <p>This command will exit with a 0 result code if LinPac is running. Any other
818 result code indicates that LinPac is not running, in which case the script
819 should most likely echo an error message to the user and exit. As usual, the
820 result code can be determined by examining the special <code>$?</code> value.
821
822 <p><a NAME="POS8_1_2"></a>
823 <h4>8.1.2 Accessing configuration and state</h4>
824 It is important that a shell script be able to determine the current status of
825 LinPac and retrieve configuration values. The API provides several functions
826 to enable this.
827 <p>All of these functions return values that can be assigned to
828 a shell variable, and are used in the following manner (using "call" as an
829 example here):
830 <p><code>MYVAR=`lpapi $LPCHN call`</code>
831 <p>The available functions are as follows:
832 <dl>
833 <dt><code>call</code></dt>
834 <dd>Return the callsign for the specified channel.</dd>
835 <dt><code>cwit</code></dt>
836 <dd>Return the callsign of the station connected on the specified channel.</dd>
837 <dt><code>port</code></dt>
838 <dd>Return the port of the station connected on the specified channel.</dd>
839 <dt><code>state</code></dt>
840 <dd>Return current status of the specified channel (as a number).</dd>
841 <dt><code>pname &lt;number&gt;</code></dt>
842 <dd>Return the name of port with the specified number.</dd>
843 <dt><code>get &lt;variable&gt;</code></dt>
844 <dd>Return the value of specified variable on the specified channel.</dd>
845 </dl>
846
847 <p><a NAME="POS8_1_3"></a>
848 <h4>8.1.3 Executing commands</h4>
849 Accomplishing real work with the shell interface will usually require calling
850 into LinPac to execute other LinPac commands. This is achieved using the
851 <code>do</code> command, which is functionally equivalent to generating the
852 <code>EV_DO_COMMAND</code> event from the C API.
853 <p>The <code>do</code> command has the following form:
854 <p><code>lpapi &lt;channel&gt; do &lt;command&gt; [arguments]</code>
855 <p>However, since <code>do</code> is a reserved word in Bash shell scripts,
856 it is necessary to use Bash's <code>eval</code> command to invoke it as a
857 LinPac function, as follows:
858 <p><code>eval lpapi &lt;channel&gt; do "&lt;command&gt; [arguments]"</code>
859 <p><strong>IMPORTANT:</strong> The <code>do</code> command is asynchronous.
860 That is, the shell command may return before the command issued to LinPac has
861 completed. A script will need to account for this in considering subsequent
862 actions.
863
864 <p><a NAME="POS8_1_4"></a>
865 <h4>8.1.4 Simple shell example</h4>
866
867 TBD
868
869 <p><a NAME="POS8_2"></a>
870 <h3>8.2 Creating the command</h3>
871 The steps to add the script to LinPac as a new command are essentially the same
872 as for an application written in C, described in <a href="#POS5_1">Chapter 5.1</a>
873 above. That is:
874 <ol>
875 <li>Copy the script to the <code>$LINPACDIR/bin</code> directory, making
876 sure that the permissions allow execution.</li>
877 <li>Add the new command to the file <code>$LINPACDIR/bin/commands</code> as
878 described in the <a href="manual.html">User Manual</a>.</li>
879 </ol>
880
881 <hr>
882 <center>
883 <p><i><font size=-1>Last update: 16 Feb 2020</font></i>
884 </center>
885
886 </body>
887 </html>