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