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