Fix: Bind RTP/RTCP socket pairs correctly
[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_SS5          0x1300
22 #define PORT_CLASS_SIP          0x2000
23 #define PORT_CLASS_GSM          0x3000
24 #define PORT_CLASS_GSM_BS       0x3100
25 #define PORT_CLASS_GSM_MS       0x3200
26 #define PORT_CLASS_REMOTE       0x4000
27 #define PORT_CLASS_MASK         0xf000
28 #define PORT_CLASS_mISDN_MASK   0xff00
29 #define PORT_CLASS_DSS1_MASK    0xfff0
30 #define PORT_CLASS_GSM_MASK     0xff00
31 #define PORT_CLASS_DIR_MASK     0x000f
32 #define PORT_CLASS_DIR_IN       0x0001
33 #define PORT_CLASS_DIR_OUT      0x0002
34         /* nt-mode */
35 #define PORT_TYPE_DSS1_NT_IN    0x1111
36 #define PORT_TYPE_DSS1_NT_OUT   0x1112
37         /* te-mode */
38 #define PORT_TYPE_DSS1_TE_IN    0x1121
39 #define PORT_TYPE_DSS1_TE_OUT   0x1122
40         /* gsm */
41 #define PORT_TYPE_GSM_BS_IN     0x3101
42 #define PORT_TYPE_GSM_BS_OUT    0x3102
43 #define PORT_TYPE_GSM_MS_IN     0x3201
44 #define PORT_TYPE_GSM_MS_OUT    0x3202
45         /* ss5 */
46 #define PORT_TYPE_SS5_IN        0x1311
47 #define PORT_TYPE_SS5_OUT       0x1312
48 #define PORT_TYPE_SS5_IDLE      0x1313
49         /* remote */
50 #define PORT_TYPE_REMOTE_IN     0x4001
51 #define PORT_TYPE_REMOTE_OUT    0x4002
52         /* SIP */
53 #define PORT_TYPE_SIP_IN        0x2001
54 #define PORT_TYPE_SIP_OUT       0x2002
55         /* answering machine */
56 #define PORT_TYPE_VBOX_OUT      0xf111
57
58
59 enum { /* states of call */
60         PORT_STATE_IDLE,        /* no call */
61         PORT_STATE_IN_SETUP,    /* incoming connection */
62         PORT_STATE_OUT_SETUP,   /* outgoing connection */
63         PORT_STATE_IN_OVERLAP,  /* more informatiopn needed */
64         PORT_STATE_OUT_OVERLAP, /* more informatiopn needed */
65         PORT_STATE_IN_PROCEEDING,/* call is proceeding */
66         PORT_STATE_OUT_PROCEEDING,/* call is proceeding */
67         PORT_STATE_IN_ALERTING, /* call is ringing */
68         PORT_STATE_OUT_ALERTING,/* call is ringing */
69         PORT_STATE_CONNECT_WAITING,/* connect is sent to the network, waiting for acknowledge */
70         PORT_STATE_CONNECT,     /* call is connected and transmission is enabled */
71         PORT_STATE_IN_DISCONNECT,/* incoming disconnected */
72         PORT_STATE_OUT_DISCONNECT,/* outgoing disconnected */
73         PORT_STATE_RELEASE,     /* call released */
74 };
75
76 #define PORT_STATE_NAMES \
77 static const char *state_name[] = { \
78         "PORT_STATE_IDLE", \
79         "PORT_STATE_IN_SETUP", \
80         "PORT_STATE_OUT_SETUP", \
81         "PORT_STATE_IN_OVERLAP", \
82         "PORT_STATE_OUT_OVERLAP", \
83         "PORT_STATE_IN_PROCEEDING", \
84         "PORT_STATE_OUT_PROCEEDING", \
85         "PORT_STATE_IN_ALERTING", \
86         "PORT_STATE_OUT_ALERTING", \
87         "PORT_STATE_CONNECT_WAITING", \
88         "PORT_STATE_CONNECT", \
89         "PORT_STATE_IN_DISCONNECT", \
90         "PORT_STATE_OUT_DISCONNECT", \
91         "PORT_STATE_RELEASE", \
92 };
93
94
95 enum { /* event list from listening to tty */
96         TTYI_EVENT_nodata,      /* no data was received nor processed */
97         TTYI_EVENT_NONE,        /* nothing happens */
98         TTYI_EVENT_CONNECT,     /* a connection is made */
99         TTYI_EVENT_RING,        /* incoming call */
100         TTYI_EVENT_CALLER,      /* caller id information */
101         TTYI_EVENT_INFO,        /* dialing information */
102         TTYI_EVENT_OVERLAP,     /* setup complete, awaiting more dialing info */
103         TTYI_EVENT_PROC,        /* proceeding */
104         TTYI_EVENT_ALRT,        /* alerting */
105         TTYI_EVENT_CONN,        /* connect */
106         TTYI_EVENT_DISC,        /* disconnect */
107         TTYI_EVENT_RELE,        /* release signal */
108         TTYI_EVENT_BUSY,        /* channel unavailable */
109 };
110
111 #define RECORD_BUFFER_LENGTH    1024 // must be a binary border & must be greater 256, because 256 will be written if buffer overflows
112 #define RECORD_BUFFER_MASK      1023
113
114 #define PORT_TRANSMIT           256 // how much to transmit via bridge, if it is not defined by received data length
115
116 /* structure of epoint_list */
117 struct epoint_list {
118         struct epoint_list      *next;
119         unsigned int            epoint_id;
120         int                     active;
121 };
122
123 inline unsigned int ACTIVE_EPOINT(struct epoint_list *epointlist)
124 {
125         while(epointlist)
126         {
127                 if (epointlist->active)
128                         return(epointlist->epoint_id);
129                 epointlist = epointlist->next;
130         }
131         return(0);
132 }
133
134 inline unsigned int INACTIVE_EPOINT(struct epoint_list *epointlist)
135 {
136         while(epointlist)
137         {
138                 if (!epointlist->active)
139                         return(epointlist->epoint_id);
140                 epointlist = epointlist->next;
141         }
142         return(0);
143 }
144
145
146 /* structure of port settings */
147 struct port_settings {
148         char tones_dir[256];                    /* directory of current tone */
149         int no_seconds;
150 };
151
152 #define BRIDGE_BUFFER 4096
153
154 struct port_bridge_member {
155         struct port_bridge_member *next;
156         class Port *port;
157         unsigned char buffer[BRIDGE_BUFFER];
158         int write_p;                            /* points to write position in buffer */
159         int min_space;                          /* minimum space to calculate how much delay can be removed */
160 };
161
162 /* port bridge instance */
163 struct port_bridge {
164         struct port_bridge *next;               /* next bridge node */
165         unsigned int bridge_id;                 /* unique ID to identify bridge */
166         struct port_bridge_member *first;       /* list of ports that are bridged */
167         signed long sum_buffer[BRIDGE_BUFFER];
168         int read_p;                             /* points to read position in buffer */
169         struct lcr_timer timer;                 /* clock to transmit sum data */
170         int sample_count;                       /* counter of samples since last delay check */
171 };
172
173 extern struct port_bridge *p_bridge_first;
174
175 /* generic port class */
176 class Port
177 {
178         public:
179         /* methods */
180         Port(int type, const char *portname, struct port_settings *settings, struct interface *interface);
181         virtual ~Port();
182         class Port *next;                       /* next port in list */
183         int p_type;                             /* type of port */
184         virtual int message_epoint(unsigned int epoint_id, int message, union parameter *param);
185         virtual void set_echotest(int echotest);
186         virtual void set_tone(const char *dir, const char *name);
187         virtual int read_audio(unsigned char *buffer, int length);
188         virtual void update_load(void);
189
190         struct port_settings p_settings;
191         char p_interface_name[64];
192         
193         /* tone */
194         char p_tone_dir[256];                   /* name of current directory */
195         char p_tone_name[256];                  /* name of current tone */
196         char p_tone_fh;                         /* file descriptor of current tone or -1 if not open */
197         void *p_tone_fetched;                   /* pointer to fetched data */
198         int p_tone_codec;                       /* codec that the tone is made of */
199         signed int p_tone_size, p_tone_left;    /* size of tone in bytes (not samples), bytes left */
200         signed int p_tone_eof;                  /* flag that makes the use of eof message */
201         signed int p_tone_counter;              /* flag that makes the use of counter message */
202         signed int p_tone_speed;                /* speed of current tone, 1=normal, may also be negative */
203 //      char p_knock_fh;                        /* file descriptor of knocking tone or -1 if not open */
204 //      void *p_knock_fetched;                  /* pointer to fetched data */
205 //      int p_knock_codec;
206 //      signed int p_knock_size, p_knock_left;
207         void set_vbox_tone(const char *dir, const char *name);/* tone of answering machine */
208         void set_vbox_play(const char *name, int offset); /* sample of answ. */
209         void set_vbox_speed(int speed); /* speed of answ. */
210
211         /* identification */
212         unsigned int p_serial;                  /* serial unique id of port */
213         char p_name[128];                       /* name of port or token (h323) */
214
215         /* endpoint relation */
216         struct epoint_list *p_epointlist;       /* endpoint relation */
217
218         /* audio bridging */
219         struct port_bridge *p_bridge;           /* linked to a port bridge or NULL */
220         void bridge(unsigned int bridge_id);    /* join a bridge */
221         int bridge_tx(unsigned char *data, int len); /* used to transmit data to remote port */
222         virtual int bridge_rx(unsigned char *data, int len); /* function to be inherited, so data is received */
223
224         /* state */
225         int p_state;                            /* state of port */
226         void new_state(int state);              /* set new state */
227         struct caller_info p_callerinfo;        /* information about the caller */
228         struct dialing_info p_dialinginfo;      /* information about dialing */
229         struct connect_info p_connectinfo;      /* information about connected line */
230         struct redir_info p_redirinfo;          /* info on redirection (to the calling user) */
231         struct capa_info p_capainfo;    /* info on l2,l3 capacity */
232         int p_echotest;                         /* set to echo audio data FROM port back to port's mixer */
233
234         /* recording */
235         int open_record(int type, int mode, int skip, char *terminal, int anon_ignore, const char *vbox_email, int vbox_email_file);
236         void close_record(int beep, int mute);
237         void record(unsigned char *data, int length, int dir_fromup);
238         FILE *p_record;                         /* recording fp: if not NULL, recording is enabled */
239         int p_record_type;                      /* codec to use: RECORD_MONO, RECORD_STEREO, ... */
240         int p_record_skip;                      /* skip bytes before writing the sample */
241         unsigned int p_record_length;           /* size of what's written so far */
242
243         signed short p_record_buffer[RECORD_BUFFER_LENGTH];
244         unsigned int p_record_buffer_readp;
245         unsigned int p_record_buffer_writep;
246         int p_record_buffer_dir;                /* current direction in buffer */
247
248         char p_record_filename[256];            /* record filename */
249         int p_record_vbox;                      /* 0= normal recording, 1= announcement, 2= record to vbox dir */
250         int p_record_vbox_year;                 /* time when vbox recording started */
251         int p_record_vbox_mon;
252         int p_record_vbox_mday;
253         int p_record_vbox_hour;
254         int p_record_vbox_min;
255         char p_record_extension[32];            /* current name (digits) of extension */
256         int p_record_anon_ignore;
257         char p_record_vbox_email[128];
258         int p_record_vbox_email_file;
259         virtual void update_rxoff(void);        /* inherited by mISDNport, to control rxoff */
260
261         void free_epointlist(struct epoint_list *epointlist);
262         void free_epointid(unsigned int epoint_id);
263         struct epoint_list *epointlist_new(unsigned int epoint_id);
264 };
265
266
267 extern Port *port_first;
268 extern unsigned int port_serial;
269
270 class Port *find_port_with_token(char *name);
271 class Port *find_port_id(unsigned int port_id);
272
273
274 #endif // PORT_HEADER
275
276