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