1 /*
2  *
3  * XASTIR, Amateur Station Tracking and Information Reporting
4  * Copyright (C) 1999,2000  Frank Giannandrea
5  * Copyright (C) 2000-2019 The Xastir Group
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * Look at the README for more information on the program.
22  */
23 
24 #ifndef __XASTIR_INTERFACE_H
25 #define __XASTIR_INTERFACE_H
26 
27 #include <termios.h>
28 #include <unistd.h>
29 #include "util.h"
30 #include "xastir.h"
31 
32 
33 #define MAX_DEVICE_NAME 128
34 #define MAX_DEVICE_BUFFER 4096
35 #define MAX_DEVICE_BUFFER_UNTIL_BINARY_SWITCH 700
36 #define MAX_DEVICE_HOSTNM 40
37 #define MAX_DEVICE_HOSTPW 40
38 
39 #define MAX_IFACE_DEVICES 15
40 
41 #define NET_CONNECT_TIMEOUT 20
42 
43 #define DEFAULT_GPS_RETR 0x05 /* CTRL-E */
44 
45 // Define a 60 second max wait on a serial port (in microseconds)
46 #define SERIAL_MAX_WAIT 60000000
47 
48 // KISS Protocol Special Characters & Commands:
49 #define KISS_FEND           0xc0  // Frame End
50 #define KISS_FESC           0xdb  // Frame Escape
51 #define KISS_TFEND          0xdc  // Transposed Frame End
52 #define KISS_TFESC          0xdd  // Transposed Frame Escape
53 #define KISS_DATA           0x00
54 #define KISS_TXDELAY        0x01
55 #define KISS_PERSISTENCE    0x02
56 #define KISS_SLOTTIME       0x03
57 #define KISS_TXTAIL         0x04
58 #define KISS_FULLDUPLEX     0x05
59 #define KISS_SETHARDWARE    0x06
60 #define KISS_RETURN         0xff
61 
62 
63 
64 #define MAX_IFACE_DEVICE_TYPES 15
65 
66 /* Define Device Types */
67 enum Device_Types
68 {
69   DEVICE_NONE,
70   DEVICE_SERIAL_TNC,
71   DEVICE_SERIAL_TNC_HSP_GPS,
72   DEVICE_SERIAL_GPS,
73   DEVICE_SERIAL_WX,
74   DEVICE_NET_STREAM,
75   DEVICE_AX25_TNC,
76   DEVICE_NET_GPSD,
77   DEVICE_NET_WX,
78   DEVICE_SERIAL_TNC_AUX_GPS,  // KB6MER -> KAM XL or other TNC w/GPS on AUX port
79   DEVICE_SERIAL_KISS_TNC,     // KISS TNC on serial port (not ax.25 kernel device)
80   DEVICE_NET_DATABASE,
81   DEVICE_NET_AGWPE,
82   DEVICE_SERIAL_MKISS_TNC,    // Multi-port KISS TNC, like the Kantronics KAM
83   DEVICE_SQL_DATABASE         // SQL server (MySQL/Postgis) database
84 };
85 
86 enum Device_Active
87 {
88   DEVICE_NOT_IN_USE,
89   DEVICE_IN_USE
90 };
91 
92 enum Device_Status
93 {
94   DEVICE_DOWN,
95   DEVICE_UP,
96   DEVICE_ERROR
97 };
98 
99 
100 typedef struct
101 {
102   int    device_type;                           /* device type                             */
103   int    active;                                /* channel in use                          */
104   int    status;                                /* current status (up or down)             */
105   char   device_name[MAX_DEVICE_NAME+1];        /* device name                             */
106   char   device_host_name[MAX_DEVICE_HOSTNM+1]; /* device host name for network            */
107   struct addrinfo *addr_list;                   /* possible network addresses              */
108   unsigned long int address;                    /* XXX Delete this                         */
109   int    thread_status;                         /* thread status for connect thread        */
110   int    connect_status;                        /* connect status for connect thread       */
111   int    decode_errors;                         /* decode error count, used for data type  */
112   int    data_type;                             /* 0=normal 1=wx_binary                    */
113   int    socket_port;                           /* socket port# for network                */
114   char   device_host_pswd[MAX_DEVICE_HOSTPW+1]; /* host password                           */
115   int    channel;                               /* for serial and net ports                */
116   int    channel2;                              /* for AX25 ports                          */
117   char   ui_call[30];                           /* current call for this port              */
118   struct termios t,t_old;                       /* terminal struct for serial port         */
119   int    dtr;                                   /* dtr signal for HSP cable (status)       */
120   int    sp;                                    /* serial port speed                       */
121   int    style;                                 /* serial port style                       */
122   int    scan;                                  /* data read available                     */
123   int    errors;                                /* errors for this port                    */
124   int    reconnect;                             /* reconnect on net failure                */
125   int    reconnects;                            /* total number of reconnects by this port */
126   unsigned long   bytes_input;                  /* total bytes read by this port           */
127   unsigned long   bytes_output;                 /* total bytes written by this port        */
128   unsigned long   bytes_input_last;             /* total bytes read last check             */
129   unsigned long   bytes_output_last;            /* total bytes read last check             */
130   int    port_activity;                         /* 0 if no activity between checks         */
131   pthread_t read_thread;                        /* read thread                             */
132   int    read_in_pos;                           /* current read buffer input pos           */
133   int    read_out_pos;                          /* current read buffer output pos          */
134   char   device_read_buffer[MAX_DEVICE_BUFFER]; /* read buffer for this port               */
135   xastir_mutex read_lock;                       /* Lock for reading the port data          */
136   pthread_t write_thread;                       /* write thread                            */
137   int    write_in_pos;                          /* current write buffer input pos          */
138   int    write_out_pos;                         /* current write buffer output pos         */
139   xastir_mutex write_lock;                      /* Lock for writing the port data          */
140   char   device_write_buffer[MAX_DEVICE_BUFFER];/* write buffer for this port              */
141 } iface;
142 
143 typedef struct
144 {
145   char device_name[100];
146 } iodevices;
147 
148 
149 typedef struct
150 {
151   int    device_type;                           /* device type                             */
152   char   device_name[MAX_DEVICE_NAME+1];        /* device name                             */
153   char   radio_port[3];                         /* port for multi-port TNC's               */
154   char   device_host_name[MAX_DEVICE_HOSTNM+1]; /* device host name for network            */
155   char   device_host_pswd[MAX_DEVICE_HOSTPW+1]; /* host password also WX device data type  */
156   char   device_host_filter_string[201];        /* host filter string                      */
157   char   device_converse_string[10+1];          /* string used to enter converse mode      */
158   char   comment[50];                           /* Local comment or name for port          */
159   char   unproto1[50];                          /* unproto path 1 for this port            */
160   char   unproto2[50];                          /* unproto path 2 for this port            */
161   char   unproto3[50];                          /* unproto path 3 for this port            */
162   char   unproto_igate[50];                     /* unproto igate path for this port        */
163   int    unprotonum;                            /* unproto path selection                  */
164   char   tnc_up_file[100];                      /* file for setting up TNC on this port    */
165   char   tnc_down_file[100];                    /* file for shutting down TNC on this port */
166   int    sp;                                    /* serial port speed/Net port              */
167   int    style;                                 /* serial port style                       */
168   int    igate_options;                         /* Igate options (0=none,1=input,2=in/out) */
169   int    transmit_data;                         /* Data transmit out of this port          */
170   int    reconnect;                             /* reconnect on net failure                */
171   int    connect_on_startup;                    /* connect to this device on startup       */
172   int    gps_retrieve;                          /* Character to cause SERIAL_TNC_AUX_GPS to spit out current GPS data */
173   int    tnc_extra_delay;                       /* Introduces fixed delay when talking to TNC in command-mode */
174   int    set_time;                              /* Set System Time from GPS on this port   */
175   char   txdelay[4];                            /* KISS parameter */
176   char   persistence[4];                        /* KISS parameter */
177   char   slottime[4];                           /* KISS parameter */
178   char   txtail[4];                             /* KISS parameter */
179   int    fullduplex;                            /* KISS parameter */
180   int    relay_digipeat;                        /* If 1: interface should RELAY digipeat */
181   int    init_kiss;                             /* Initialize KISS-Mode on startup */
182 #ifdef HAVE_DB
183   // to support connections to sql server databases for db_gis.c
184   char   database_username[20];                 /* Username to use to connect to database  */
185   int    database_type;                         /* Type of dbms (posgresql, mysql, etc)    */
186   char   database_schema[20];                   /* Name of database or schema to use       */
187   char   database_errormessage[255];            /* Most recent error message from
188                                                      attempting to make a
189                                                      connection with using this descriptor.  */
190   int    database_schema_type;                  /* table structures to use in the database
191                                                      A database schema could contain both
192                                                      APRSWorld and XASTIR table structures,
193                                                      but a separate database descriptor
194                                                      needs to be defined for each.  */
195   char   database_unix_socket[255];             /* MySQL - unix socket parameter (path and
196                                                      filename) */
197   // Need a pointer here, and one in connection pointing back here.  How to do????
198   //Connection *database_connection;
199   /* Pointer to database connection that
200      contains database handle (with type
201      of handle being dependent on type of
202      database.  */
203   int    query_on_startup;                      /* Load stations from this database on
204                                                      startup. */
205   // Use of other ioparam variables for sql server database connections:
206   // device_host_name = hostname for database server
207   // sp = port on which to connect to database server
208   // device_host_pswd =  password to use to connect to database -- security issue needs to be addressed
209 #endif  // HAVE_DB
210 } ioparam;
211 
212 
213 extern iodevices dtype[];
214 
215 extern xastir_mutex port_data_lock; // Protects the port_data[] array of structs
216 extern xastir_mutex devices_lock;    // Protects the devices[] array
217 
218 extern iface port_data[];
219 extern int port_id[];
220 extern int get_device_status(int port);
221 extern int del_device(int port);
222 extern int get_open_device(void);
223 extern int add_device(int port_avail,int dev_type,
224                       char *dev_nm,
225                       char *passwd,
226                       int dev_sck_p, int dev_sp,
227                       int dev_sty,
228                       int reconnect,
229                       char *filter_string);
230 
231 extern xastir_mutex data_lock;          // Protects incoming_data_queue
232 extern xastir_mutex output_data_lock;   // Protects interface.c:channel_data() function only
233 extern xastir_mutex connect_lock;       // Protects port_data[].thread_status and port_data[].connect_status
234 
235 extern ioparam devices[];
236 
237 #if !HAVE_SOCKLEN_T
238   typedef unsigned int socklen_t;
239 #endif
240 
241 /* from interface_gui.c */
242 extern void interface_gui_init(void);
243 extern void Configure_interface_destroy_shell(Widget widget, XtPointer clientData, XtPointer callData);
244 extern void Configure_interface(Widget w, XtPointer clientData, XtPointer callData);
245 extern void output_my_aprs_data(void);
246 extern void control_interface(Widget w, XtPointer clientData, XtPointer callData);
247 extern void dtr_all_set(int dtr);
248 extern void interface_status(Widget w);
249 extern void update_interface_list(void);
250 extern int WX_rain_gauge_type;
251 
252 /* interface.c */
253 extern int is_local_interface(int port);
254 extern int is_network_interface(int port);
255 extern void send_agwpe_packet(int xastir_interface, int RadioPort, unsigned char type, unsigned char *FromCall, unsigned char *ToCall, unsigned char *Path, unsigned char *Data, int length);
256 
257 extern int pop_incoming_data(unsigned char *data_string, int *port);
258 extern int push_incoming_data(unsigned char *data_string, int length, int port);
259 
260 extern unsigned char incoming_data_copy[MAX_LINE_SIZE];
261 extern unsigned char incoming_data_copy_previous[MAX_LINE_SIZE];
262 extern int NETWORK_WAITTIME;
263 extern void startup_all_or_defined_port(int port);
264 extern void shutdown_all_active_or_defined_port(int port);
265 extern void check_ports(void);
266 extern void clear_all_port_data(void);
267 extern char aprs_station_message_type;
268 extern void port_dtr(int port, int dtr);
269 extern void send_kiss_config(int port, int device, int command, int value);
270 void port_write_string(int port, char *data);
271 extern void init_device_names(void);
272 extern void output_my_data(char *message, int port, int type, int loopback_only, int use_igate_path, char *path);
273 int tnc_get_data_type(char *buf, int port);
274 void tnc_data_clean(char *buf);
275 extern void output_waypoint_data(char *message);
276 extern void send_ax25_frame(int port, char *source, char *destination, char *path, char *data);
277 
278 extern pid_t getpgid(pid_t pid);
279 
280 
281 #endif /* XASTIR_INTERFACE_H */
282 
283