GSM: Fixes to GSM interface (multiple networks)
[lcr.git] / port.h
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** port header file                                                          **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #ifndef PORT_HEADER
13 #define PORT_HEADER
14
15 /* type of port */
16 #define PORT_TYPE_NULL          0x0000
17 #define PORT_CLASS_mISDN        0x1000
18 #define PORT_CLASS_DSS1         0x1100
19 #define PORT_CLASS_DSS1_NT      0x1110
20 #define PORT_CLASS_DSS1_TE      0x1120
21 #define PORT_CLASS_POTS         0x1200
22 #define PORT_CLASS_POTS_FXS     0x1210
23 #define PORT_CLASS_POTS_FXO     0x1220
24 #define PORT_CLASS_SS5          0x1300
25 #define PORT_CLASS_SIP          0x2000
26 #define PORT_CLASS_GSM          0x3000
27 #define PORT_CLASS_GSM_BS       0x3100
28 #define PORT_CLASS_GSM_MS       0x3200
29 #define PORT_CLASS_REMOTE       0x4000
30 #define PORT_CLASS_MASK         0xf000
31 #define PORT_CLASS_mISDN_MASK   0xff00
32 #define PORT_CLASS_DSS1_MASK    0xfff0
33 #define PORT_CLASS_POTS_MASK    0xfff0
34 #define PORT_CLASS_GSM_MASK     0xff00
35 #define PORT_CLASS_DIR_MASK     0x000f
36 #define PORT_CLASS_DIR_IN       0x0001
37 #define PORT_CLASS_DIR_OUT      0x0002
38         /* nt-mode */
39 #define PORT_TYPE_DSS1_NT_IN    0x1111
40 #define PORT_TYPE_DSS1_NT_OUT   0x1112
41         /* te-mode */
42 #define PORT_TYPE_DSS1_TE_IN    0x1121
43 #define PORT_TYPE_DSS1_TE_OUT   0x1122
44         /* FXS-mode */
45 #define PORT_TYPE_POTS_FXS_IN   0x1211
46 #define PORT_TYPE_POTS_FXS_OUT  0x1212
47         /* FXO-mode */
48 #define PORT_TYPE_POTS_FXO_IN   0x1221
49 #define PORT_TYPE_POTS_FXO_OUT  0x1222
50         /* gsm */
51 #define PORT_TYPE_GSM_BS_IN     0x3101
52 #define PORT_TYPE_GSM_BS_OUT    0x3102
53 #define PORT_TYPE_GSM_MS_IN     0x3201
54 #define PORT_TYPE_GSM_MS_OUT    0x3202
55         /* ss5 */
56 #define PORT_TYPE_SS5_IN        0x1311
57 #define PORT_TYPE_SS5_OUT       0x1312
58 #define PORT_TYPE_SS5_IDLE      0x1313
59         /* remote */
60 #define PORT_TYPE_REMOTE_IN     0x4001
61 #define PORT_TYPE_REMOTE_OUT    0x4002
62         /* SIP */
63 #define PORT_TYPE_SIP_IN        0x2001
64 #define PORT_TYPE_SIP_OUT       0x2002
65         /* answering machine */
66 #define PORT_TYPE_VBOX_OUT      0xf111
67
68
69 enum { /* states of call */
70         PORT_STATE_IDLE,        /* no call */
71         PORT_STATE_IN_SETUP,    /* incoming connection */
72         PORT_STATE_OUT_SETUP,   /* outgoing connection */
73         PORT_STATE_IN_OVERLAP,  /* more informatiopn needed */
74         PORT_STATE_OUT_OVERLAP, /* more informatiopn needed */
75         PORT_STATE_IN_PROCEEDING,/* call is proceeding */
76         PORT_STATE_OUT_PROCEEDING,/* call is proceeding */
77         PORT_STATE_IN_ALERTING, /* call is ringing */
78         PORT_STATE_OUT_ALERTING,/* call is ringing */
79         PORT_STATE_CONNECT_WAITING,/* connect is sent to the network, waiting for acknowledge */
80         PORT_STATE_CONNECT,     /* call is connected and transmission is enabled */
81         PORT_STATE_IN_DISCONNECT,/* incoming disconnected */
82         PORT_STATE_OUT_DISCONNECT,/* outgoing disconnected */
83         PORT_STATE_RELEASE,     /* call released */
84 };
85
86 #define PORT_STATE_NAMES \
87 static const char *state_name[] = { \
88         "PORT_STATE_IDLE", \
89         "PORT_STATE_IN_SETUP", \
90         "PORT_STATE_OUT_SETUP", \
91         "PORT_STATE_IN_OVERLAP", \
92         "PORT_STATE_OUT_OVERLAP", \
93         "PORT_STATE_IN_PROCEEDING", \
94         "PORT_STATE_OUT_PROCEEDING", \
95         "PORT_STATE_IN_ALERTING", \
96         "PORT_STATE_OUT_ALERTING", \
97         "PORT_STATE_CONNECT_WAITING", \
98         "PORT_STATE_CONNECT", \
99         "PORT_STATE_IN_DISCONNECT", \
100         "PORT_STATE_OUT_DISCONNECT", \
101         "PORT_STATE_RELEASE", \
102 };
103
104
105 enum { /* event list from listening to tty */
106         TTYI_EVENT_nodata,      /* no data was received nor processed */
107         TTYI_EVENT_NONE,        /* nothing happens */
108         TTYI_EVENT_CONNECT,     /* a connection is made */
109         TTYI_EVENT_RING,        /* incoming call */
110         TTYI_EVENT_CALLER,      /* caller id information */
111         TTYI_EVENT_INFO,        /* dialing information */
112         TTYI_EVENT_OVERLAP,     /* setup complete, awaiting more dialing info */
113         TTYI_EVENT_PROC,        /* proceeding */
114         TTYI_EVENT_ALRT,        /* alerting */
115         TTYI_EVENT_CONN,        /* connect */
116         TTYI_EVENT_DISC,        /* disconnect */
117         TTYI_EVENT_RELE,        /* release signal */
118         TTYI_EVENT_BUSY,        /* channel unavailable */
119 };
120
121 #define RECORD_BUFFER_LENGTH    1024 // must be a binary border & must be greater 256, because 256 will be written if buffer overflows
122 #define RECORD_BUFFER_MASK      1023
123
124 #define PORT_TRANSMIT           256 // how much to transmit via bridge, if it is not defined by received data length
125
126 /* structure of epoint_list */
127 struct epoint_list {
128         struct epoint_list      *next;
129         unsigned int            epoint_id;
130         int                     active;
131 };
132
133 inline unsigned int ACTIVE_EPOINT(struct epoint_list *epointlist)
134 {
135         while(epointlist)
136         {
137                 if (epointlist->active)
138                         return(epointlist->epoint_id);
139                 epointlist = epointlist->next;
140         }
141         return(0);
142 }
143
144 inline unsigned int INACTIVE_EPOINT(struct epoint_list *epointlist)
145 {
146         while(epointlist)
147         {
148                 if (!epointlist->active)
149                         return(epointlist->epoint_id);
150                 epointlist = epointlist->next;
151         }
152         return(0);
153 }
154
155
156 /* structure of port settings */
157 struct port_settings {
158         char tones_dir[256];                    /* directory of current tone */
159         int no_seconds;
160 };
161
162 #define BRIDGE_BUFFER 4096
163
164 struct port_bridge_member {
165         struct port_bridge_member *next;
166         class Port *port;
167         unsigned char buffer[BRIDGE_BUFFER];
168         int write_p;                            /* points to write position in buffer */
169         int min_space;                          /* minimum space to calculate how much delay can be removed */
170 };
171
172 /* port bridge instance */
173 struct port_bridge {
174         struct port_bridge *next;               /* next bridge node */
175         unsigned int bridge_id;                 /* unique ID to identify bridge */
176         struct port_bridge_member *first;       /* list of ports that are bridged */
177         signed long sum_buffer[BRIDGE_BUFFER];
178         int read_p;                             /* points to read position in buffer */
179         struct lcr_timer timer;                 /* clock to transmit sum data */
180         int sample_count;                       /* counter of samples since last delay check */
181 };
182
183 extern struct port_bridge *p_bridge_first;
184
185 enum dov_type {
186         DOV_TYPE_PWM,
187         DOV_TYPE_PCM,
188 };
189
190 /* generic port class */
191 class Port
192 {
193         public:
194         /* methods */
195         Port(int type, const char *portname, struct port_settings *settings, struct interface *interface);
196         virtual ~Port();
197         class Port *next;                       /* next port in list */
198         int p_type;                             /* type of port */
199         virtual int message_epoint(unsigned int epoint_id, int message, union parameter *param);
200         virtual void set_echotest(int echotest);
201         virtual void set_tone(const char *dir, const char *name);
202         virtual int read_audio(unsigned char *buffer, int length);
203         virtual void update_load(void);
204         virtual void set_display(const char *text);
205
206         struct port_settings p_settings;
207         char p_interface_name[64];
208         
209         /* tone */
210         char p_tone_dir[256];                   /* name of current directory */
211         char p_tone_name[256];                  /* name of current tone */
212         char p_tone_fh;                         /* file descriptor of current tone or -1 if not open */
213         void *p_tone_fetched;                   /* pointer to fetched data */
214         int p_tone_codec;                       /* codec that the tone is made of */
215         signed int p_tone_size, p_tone_left;    /* size of tone in bytes (not samples), bytes left */
216         signed int p_tone_eof;                  /* flag that makes the use of eof message */
217         signed int p_tone_counter;              /* flag that makes the use of counter message */
218         signed int p_tone_speed;                /* speed of current tone, 1=normal, may also be negative */
219 //      char p_knock_fh;                        /* file descriptor of knocking tone or -1 if not open */
220 //      void *p_knock_fetched;                  /* pointer to fetched data */
221 //      int p_knock_codec;
222 //      signed int p_knock_size, p_knock_left;
223         void set_vbox_tone(const char *dir, const char *name);/* tone of answering machine */
224         void set_vbox_play(const char *name, int offset); /* sample of answ. */
225         void set_vbox_speed(int speed); /* speed of answ. */
226
227         /* identification */
228         unsigned int p_serial;                  /* serial unique id of port */
229         char p_name[128];                       /* name of port or token (h323) */
230
231         /* endpoint relation */
232         struct epoint_list *p_epointlist;       /* endpoint relation */
233
234         /* audio bridging */
235         struct port_bridge *p_bridge;           /* linked to a port bridge or NULL */
236         void bridge(unsigned int bridge_id);    /* join a bridge */
237         int bridge_tx(unsigned char *data, int len); /* used to transmit data to remote port */
238         virtual int bridge_rx(unsigned char *data, int len); /* function to be inherited, so data is received */
239
240         /* state */
241         int p_state;                            /* state of port */
242         void new_state(int state);              /* set new state */
243         struct caller_info p_callerinfo;        /* information about the caller */
244         struct dialing_info p_dialinginfo;      /* information about dialing */
245         struct connect_info p_connectinfo;      /* information about connected line */
246         struct redir_info p_redirinfo;          /* info on redirection (to the calling user) */
247         struct capa_info p_capainfo;    /* info on l2,l3 capacity */
248         int p_echotest;                         /* set to echo audio data FROM port back to port's mixer */
249
250         /* recording/tapping */
251         int open_record(int type, int mode, int skip, char *terminal, int anon_ignore, const char *vbox_email, int vbox_email_file);
252         void close_record(int beep, int mute);
253         void record(unsigned char *data, int length, int dir_fromup);
254         void tap(unsigned char *data, int length, int dir_fromup);
255         FILE *p_record;                         /* recording fp: if not NULL, recording is enabled */
256         unsigned int p_tap;                     /* enpoint to send tapping audio to */
257         int p_record_type;                      /* codec to use: RECORD_MONO, RECORD_STEREO, ... */
258         int p_record_skip;                      /* skip bytes before writing the sample */
259         unsigned int p_record_length;           /* size of what's written so far */
260
261         signed short p_record_buffer[RECORD_BUFFER_LENGTH];
262         unsigned int p_record_buffer_readp;
263         unsigned int p_record_buffer_writep;
264         int p_record_buffer_dir;                /* current direction in buffer */
265
266         char p_record_filename[256];            /* record filename */
267         int p_record_vbox;                      /* 0= normal recording, 1= announcement, 2= record to vbox dir */
268         int p_record_vbox_year;                 /* time when vbox recording started */
269         int p_record_vbox_mon;
270         int p_record_vbox_mday;
271         int p_record_vbox_hour;
272         int p_record_vbox_min;
273         char p_record_extension[32];            /* current name (digits) of extension */
274         int p_record_anon_ignore;
275         char p_record_vbox_email[128];
276         int p_record_vbox_email_file;
277         virtual void update_rxoff(void);        /* inherited by mISDNport, to control rxoff */
278
279 #ifdef WITH_VOOTP
280         vootp_t *p_vootp;                       /* VoOTP instance */
281         void set_vootp(struct param_vootp *vootp);
282 #endif
283
284         /* DOV */
285         int p_dov_tx, p_dov_rx;
286         int p_dov_tx_sync, p_dov_rx_sync;
287         enum dov_type p_dov_tx_type, p_dov_rx_type;
288         unsigned char *p_dov_tx_data, *p_dov_rx_data;
289         int p_dov_tx_data_length;
290         int p_dov_tx_data_pos, p_dov_rx_data_pos;
291         int p_dov_tx_bit_pos, p_dov_rx_bit_pos;
292         int p_dov_tx_pwm_pos, p_dov_rx_pwm_pos;
293         int p_dov_rx_pwm_duration, p_dov_rx_pwm_polarity;
294         int p_dov_tx_up;
295         int p_dov_rx_sync_word;
296         unsigned char p_dov_up;
297         unsigned char p_dov_down;
298         void dov_init(void);
299         void dov_exit(void);
300         void dov_reset_tx(void);
301         void dov_reset_rx(void);
302         struct lcr_timer p_dov_tx_timer;
303         struct lcr_timer p_dov_rx_timer;
304         void dov_sendmsg(unsigned char *data, int length, enum dov_type type, int level);
305         int dov_tx(unsigned char *data, int length);
306         int dov_tx_pcm(unsigned char *data, int length);
307         int dov_tx_pwm(unsigned char *data, int length);
308         void dov_listen(enum dov_type type);
309         void dov_rx(unsigned char *data, int length);
310         void dov_rx_pcm(unsigned char *data, int length);
311         void dov_rx_pwm(unsigned char *data, int length);
312         void dov_message(unsigned char *data, int length);
313
314         void free_epointlist(struct epoint_list *epointlist);
315         void free_epointid(unsigned int epoint_id);
316         struct epoint_list *epointlist_new(unsigned int epoint_id);
317 };
318
319
320 extern Port *port_first;
321 extern unsigned int port_serial;
322
323 class Port *find_port_with_token(char *name);
324 class Port *find_port_id(unsigned int port_id);
325
326
327 #endif // PORT_HEADER
328
329