Finished autoconf.
[lcr.git] / vbox.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** answering machine port                                                    **
9 **                                                                           **
10 ** this is a child of the port class, which emulates the recorder function   **
11 ** of an answering machine                                                   **
12 ** it will directly answer to the setup message with a connect               **
13 **                                                                           **
14 \*****************************************************************************/ 
15
16 #include "main.h"
17
18 /* note: recording log is written at endpoint */
19
20 /*
21  * initialize vbox port
22  */
23 VBoxPort::VBoxPort(int type, struct port_settings *settings) : Port(type, "vbox", settings)
24 {
25         p_vbox_timeout = 0;
26         p_vbox_announce_fh = -1;
27         p_vbox_audio_start = 0;
28         p_vbox_audio_transferred = 0;
29         p_vbox_record_start = 0;
30         p_vbox_record_limit = 0;
31 }
32
33
34 /*
35  * destructor
36  */
37 VBoxPort::~VBoxPort()
38 {
39         if (p_vbox_announce_fh >= 0)
40         {
41                 close(p_vbox_announce_fh);
42                 p_vbox_announce_fh = -1;
43                 fhuse--;
44         }
45 }
46
47
48 static void vbox_trace_header(class VBoxPort *vbox, const char *message, int direction)
49 {
50         /* init trace with given values */
51         start_trace(-1,
52                     NULL,
53                     vbox?numberrize_callerinfo(vbox->p_callerinfo.id, vbox->p_callerinfo.ntype, options.national, options.international):NULL,
54                     vbox?vbox->p_dialinginfo.id:NULL,
55                     direction,
56                     CATEGORY_CH,
57                     vbox?vbox->p_serial:0,
58                     message);
59 }
60
61
62 /*
63  * handler of vbox
64  */
65 int VBoxPort::handler(void)
66 {
67         struct lcr_msg  *message;
68         unsigned int    tosend;
69         unsigned char   buffer[ISDN_TRANSMIT];
70         time_t          currenttime;
71         class Endpoint  *epoint;
72         int             ret;
73
74         if ((ret = Port::handler()))
75                 return(ret);
76
77         if (p_vbox_record_start && p_vbox_record_limit)
78         {
79                 time(&currenttime);
80                 if (currenttime > (p_vbox_record_limit+p_vbox_record_start))
81                 {
82                         while(p_epointlist)
83                         {
84                                 /* send release */
85                                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
86                                 message->param.disconnectinfo.cause = 16;
87                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
88                                 message_put(message);
89                                 vbox_trace_header(this, "RELEASE from VBox (recoding limit reached)", DIRECTION_IN);
90                                 add_trace("cause", "value", "%d", message->param.disconnectinfo.cause);
91                                 add_trace("cause", "location", "%d", message->param.disconnectinfo.location);
92                                 end_trace();
93                                 /* remove epoint */
94                                 free_epointlist(p_epointlist);
95                         }
96                         /* recording is close during destruction */
97                         delete this;
98                         return(-1); /* must return because port is gone */
99                 }
100         }
101
102         /* set time the first time */
103         if (p_vbox_audio_start < 1)
104         {
105                 p_vbox_audio_start = now_d;
106                 return(0);
107         }
108         
109         /* calculate the number of bytes */
110         tosend = (unsigned int)((now_d-p_vbox_audio_start)*8000) - p_vbox_audio_transferred;
111
112         /* wait for more */
113         if (tosend < sizeof(buffer))
114                 return(0);
115         tosend = sizeof(buffer);
116
117         /* add the number of samples elapsed */
118         p_vbox_audio_transferred += tosend;
119
120         /* if announcement is currently played, send audio data */
121         if (p_vbox_announce_fh >=0)
122         {
123                 tosend = read_tone(p_vbox_announce_fh, buffer, p_vbox_announce_codec, tosend, p_vbox_announce_size, &p_vbox_announce_left, 1);
124                 if (tosend <= 0)
125                 {
126                         /* end of file */
127                         close(p_vbox_announce_fh);
128                         p_vbox_announce_fh = -1;
129                         fhuse--;
130
131                         time(&currenttime);
132                         p_vbox_record_start = currenttime;
133
134                         /* connect if not already */
135                         epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
136                         if (epoint)
137                         {
138                                 /* if we sent our announcement during ringing, we must now connect */
139                                 if (p_vbox_ext.vbox_free)
140                                 {
141                                         /* send connect message */
142                                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
143                                         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
144                                         message_put(message);
145                                         vbox_trace_header(this, "CONNECT from VBox (announcement is over)", DIRECTION_IN);
146                                         end_trace();
147                                         new_state(PORT_STATE_CONNECT);
148                                 }
149                         }
150
151                         /* start recording, if not already */
152                         if (p_vbox_mode == VBOX_MODE_NORMAL)
153                         {
154                                 /* recording start */
155                                 open_record(p_vbox_ext.vbox_codec, 2, 0, p_vbox_ext.number, p_vbox_ext.anon_ignore, p_vbox_ext.vbox_email, p_vbox_ext.vbox_email_file);
156                                 vbox_trace_header(this, "RECORDING (announcement is over)", DIRECTION_IN);
157                                 end_trace();
158                         } else // else!!
159                         if (p_vbox_mode == VBOX_MODE_ANNOUNCEMENT)
160                         {
161                                 /* send release */
162                                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
163                                 message->param.disconnectinfo.cause = 16;
164                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
165                                 message_put(message);
166                                 vbox_trace_header(this, "RELEASE from VBox (after annoucement)", DIRECTION_IN);
167                                 add_trace("cause", "value", "%d", message->param.disconnectinfo.cause);
168                                 add_trace("cause", "location", "%d", message->param.disconnectinfo.location);
169                                 end_trace();
170                                 /* recording is close during destruction */
171                                 delete this;
172                                 return(-1); /* must return because port is gone */
173                         }
174                 } else
175                 {
176                         if (p_record)
177                                 record(buffer, tosend, 0); // from down
178                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DATA);
179                         message->param.data.len = tosend;
180                         memcpy(message->param.data.data, buffer, tosend);
181                         message_put(message);
182                 }
183         }
184
185         return(1);
186 }
187
188
189 /*
190  * endpoint sends messages to the vbox port
191  */
192 int VBoxPort::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
193 {
194         struct lcr_msg *message;
195         class Endpoint *epoint;
196         char filename[256], *c;
197
198         if (Port::message_epoint(epoint_id, message_id, param))
199                 return(1);
200
201         epoint = find_epoint_id(epoint_id);
202         if (!epoint)
203         {
204                 PDEBUG(DEBUG_EPOINT|DEBUG_VBOX, "PORT(%s) no endpoint object found where the message is from.\n", p_name);
205                 return(0);
206         }
207
208         switch(message_id)
209         {
210                 case MESSAGE_DATA:
211                 record(param->data.data, param->data.len, 1); // from up
212                 return(1);
213
214                 case MESSAGE_DISCONNECT: /* call has been disconnected */
215                 new_state(PORT_STATE_OUT_DISCONNECT);
216                 vbox_trace_header(this, "DISCONNECT to VBox", DIRECTION_OUT);
217                 add_trace("cause", "value", "%d", param->disconnectinfo.cause);
218                 add_trace("cause", "location", "%d", param->disconnectinfo.location);
219                 end_trace();
220
221                 while(p_epointlist)
222                 {
223                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
224                         message->param.disconnectinfo.cause = CAUSE_NORMAL;
225                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
226                         message_put(message);
227                         vbox_trace_header(this, "RELEASE from VBox (after disconnect)", DIRECTION_IN);
228                         add_trace("cause", "value", "%d", message->param.disconnectinfo.cause);
229                         add_trace("cause", "location", "%d", message->param.disconnectinfo.location);
230                         end_trace();
231                         /* remove epoint */
232                         free_epointlist(p_epointlist);
233                 }
234                 /* recording is close during destruction */
235                 delete this;
236                 return(-1); /* must return because port is gone */
237                 break;
238
239                 case MESSAGE_RELEASE: /* release vbox port */
240                 vbox_trace_header(this, "RELEASE to VBox", DIRECTION_OUT);
241                 add_trace("cause", "value", "%d", param->disconnectinfo.cause);
242                 add_trace("cause", "location", "%d", param->disconnectinfo.location);
243                 end_trace();
244
245                 /* we are done */
246                 /* recording is close during destruction */
247                 delete this;
248                 return(-1); /* must return because port is gone */
249                 break;
250
251                 case MESSAGE_SETUP: /* dial-out command received from epoint, answer with connect */
252                 /* get apppbx */
253                 memcpy(&p_vbox_ext, &((class EndpointAppPBX *)(epoint->ep_app))->e_ext, sizeof(p_vbox_ext));
254                 /* extract optional announcement file */
255                 if ((c = strchr(param->setup.dialinginfo.id, ',')))
256                 {
257                         if (c[1] == '/')
258                                 SPRINT(filename, c+1);
259                         else
260                                 SPRINT(filename, "%s/%s/vbox/%s", EXTENSION_DATA, p_vbox_ext.number);
261                         *c = '\0';
262                 } else
263                 {
264                         SPRINT(filename, "%s/%s/vbox/announcement", EXTENSION_DATA, p_vbox_ext.number);
265                 }
266                 vbox_trace_header(this, "SETUP to VBox", DIRECTION_OUT);
267                 add_trace("from", "id", "%s", param->setup.callerinfo.id);
268                 add_trace("to", "box", "%s", param->setup.dialinginfo.id);
269                 end_trace();
270                 memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
271                 memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
272                 memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
273                 /* link relation */
274                 if (p_epointlist)
275                         FATAL("PORT(%s) Epoint pointer is set in idle state, how bad!!\n", p_name);
276                 epointlist_new(epoint_id);
277
278                 /* copy setup infos to port */
279                 SCPY(p_vbox_extension, param->setup.dialinginfo.id);
280
281                 /* create connect info */
282                 SCPY(p_connectinfo.id, p_vbox_extension);
283                 p_connectinfo.itype = INFO_ITYPE_VBOX;
284                 p_connectinfo.present = INFO_PRESENT_ALLOWED;
285                 p_connectinfo.screen = INFO_SCREEN_NETWORK;
286
287                 /* connect unless we can send announcement while ringing */
288                 if (!p_vbox_ext.vbox_free)
289                 {
290                         /* send connect message */
291                         message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_CONNECT);
292                         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
293                         message_put(message);
294                         vbox_trace_header(this, "CONNECT from VBox (after setup)", DIRECTION_IN);
295                         end_trace();
296                         new_state(PORT_STATE_CONNECT);
297                 } else 
298                 {
299                         /* send alerting message */
300                         message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_ALERTING);
301                         message_put(message);
302                         vbox_trace_header(this, "ALERTING from VBox (play announcement before connect)", DIRECTION_IN);
303                         end_trace();
304                         new_state(PORT_STATE_IN_ALERTING);
305                 }
306
307                 /* play the announcement */
308                 if ((p_vbox_announce_fh = open_tone(filename, &p_vbox_announce_codec, &p_vbox_announce_size, &p_vbox_announce_left)) >= 0)
309                 {
310                         fhuse++;
311                 } 
312                 vbox_trace_header(this, "ANNOUNCEMENT", DIRECTION_OUT);
313                 add_trace("file", "name", "%s", filename);
314                 add_trace("file", "exists", "%s", (p_vbox_announce_fh>=0)?"yes":"no");
315                 end_trace();
316                 /* start recording if desired */
317                 p_vbox_mode = p_vbox_ext.vbox_mode;
318                 p_vbox_record_limit = p_vbox_ext.vbox_time;
319                 if (p_vbox_announce_fh<0 || p_vbox_mode==VBOX_MODE_PARALLEL)
320                 {
321                         /* recording start */
322                         open_record(p_vbox_ext.vbox_codec, 2, 0, p_vbox_ext.number, p_vbox_ext.anon_ignore, p_vbox_ext.vbox_email, p_vbox_ext.vbox_email_file);
323                         vbox_trace_header(this, "RECORDING", DIRECTION_IN);
324                         end_trace();
325                 }
326                 break;
327
328                 default:
329                 PDEBUG(DEBUG_VBOX, "PORT(%s) vbox port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
330         }
331
332         return(0);
333 }
334
335