Add global variable for Law encoded silence
[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 struct port_bridge_member {
153         struct port_bridge_member *next;
154         class Port *port;
155 };
156
157 /* port bridge instance */
158 struct port_bridge {
159         struct port_bridge *next;               /* next bridge node */
160         unsigned int bridge_id;                 /* unique ID to identify bridge */
161         struct port_bridge_member *first;       /* list of ports that are bridged */
162 };
163
164 extern struct port_bridge *p_bridge_first;
165
166 /* generic port class */
167 class Port
168 {
169         public:
170         /* methods */
171         Port(int type, const char *portname, struct port_settings *settings);
172         virtual ~Port();
173         class Port *next;                       /* next port in list */
174         int p_type;                             /* type of port */
175         virtual int message_epoint(unsigned int epoint_id, int message, union parameter *param);
176         virtual void set_echotest(int echotest);
177         virtual void set_tone(const char *dir, const char *name);
178         virtual int read_audio(unsigned char *buffer, int length);
179         virtual void update_load(void);
180
181         struct port_settings p_settings;
182         
183         /* tone */
184         char p_tone_dir[256];                   /* name of current directory */
185         char p_tone_name[256];                  /* name of current tone */
186         char p_tone_fh;                         /* file descriptor of current tone or -1 if not open */
187         void *p_tone_fetched;                   /* pointer to fetched data */
188         int p_tone_codec;                       /* codec that the tone is made of */
189         signed int p_tone_size, p_tone_left;    /* size of tone in bytes (not samples), bytes left */
190         signed int p_tone_eof;                  /* flag that makes the use of eof message */
191         signed int p_tone_counter;              /* flag that makes the use of counter message */
192         signed int p_tone_speed;                /* speed of current tone, 1=normal, may also be negative */
193 //      char p_knock_fh;                        /* file descriptor of knocking tone or -1 if not open */
194 //      void *p_knock_fetched;                  /* pointer to fetched data */
195 //      int p_knock_codec;
196 //      signed int p_knock_size, p_knock_left;
197         void set_vbox_tone(const char *dir, const char *name);/* tone of answering machine */
198         void set_vbox_play(const char *name, int offset); /* sample of answ. */
199         void set_vbox_speed(int speed); /* speed of answ. */
200
201         /* identification */
202         unsigned int p_serial;                  /* serial unique id of port */
203         char p_name[128];                       /* name of port or token (h323) */
204
205         /* endpoint relation */
206         struct epoint_list *p_epointlist;       /* endpoint relation */
207
208         /* audio bridging */
209         struct port_bridge *p_bridge;           /* linked to a port bridge or NULL */
210         void bridge(unsigned int bridge_id);    /* join a bridge */
211         class Port *bridge_remote(void);        /* get remote port */
212         int bridge_tx(unsigned char *data, int len); /* used to transmit data to remote port */
213         virtual int bridge_rx(unsigned char *data, int len); /* function to be inherited, so data is received */
214
215         /* state */
216         int p_state;                            /* state of port */
217         void new_state(int state);              /* set new state */
218         struct caller_info p_callerinfo;        /* information about the caller */
219         struct dialing_info p_dialinginfo;      /* information about dialing */
220         struct connect_info p_connectinfo;      /* information about connected line */
221         struct redir_info p_redirinfo;          /* info on redirection (to the calling user) */
222         struct capa_info p_capainfo;    /* info on l2,l3 capacity */
223         int p_echotest;                         /* set to echo audio data FROM port back to port's mixer */
224
225         /* recording */
226         int open_record(int type, int mode, int skip, char *terminal, int anon_ignore, const char *vbox_email, int vbox_email_file);
227         void close_record(int beep, int mute);
228         void record(unsigned char *data, int length, int dir_fromup);
229         FILE *p_record;                         /* recording fp: if not NULL, recording is enabled */
230         int p_record_type;                      /* codec to use: RECORD_MONO, RECORD_STEREO, ... */
231         int p_record_skip;                      /* skip bytes before writing the sample */
232         unsigned int p_record_length;           /* size of what's written so far */
233
234         signed short p_record_buffer[RECORD_BUFFER_LENGTH];
235         unsigned int p_record_buffer_readp;
236         unsigned int p_record_buffer_writep;
237         int p_record_buffer_dir;                /* current direction in buffer */
238
239         char p_record_filename[256];            /* record filename */
240         int p_record_vbox;                      /* 0= normal recording, 1= announcement, 2= record to vbox dir */
241         int p_record_vbox_year;                 /* time when vbox recording started */
242         int p_record_vbox_mon;
243         int p_record_vbox_mday;
244         int p_record_vbox_hour;
245         int p_record_vbox_min;
246         char p_record_extension[32];            /* current name (digits) of extension */
247         int p_record_anon_ignore;
248         char p_record_vbox_email[128];
249         int p_record_vbox_email_file;
250         virtual void update_rxoff(void);        /* inherited by mISDNport, to control rxoff */
251
252         void free_epointlist(struct epoint_list *epointlist);
253         void free_epointid(unsigned int epoint_id);
254         struct epoint_list *epointlist_new(unsigned int epoint_id);
255 };
256
257
258 extern Port *port_first;
259 extern unsigned int port_serial;
260
261 class Port *find_port_with_token(char *name);
262 class Port *find_port_id(unsigned int port_id);
263
264
265 #endif // PORT_HEADER
266
267