Add missing braces to chan_lcr.c
[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, NULL)
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[PORT_TRANSMIT + PORT_TRANSMIT]; /* use twice of the buffer, so we can send more in case of delayed main loop execution */
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 - PORT_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 = PORT_TRANSMIT + PORT_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                 /* send to remote, if bridged */
188                 bridge_tx(buffer, tosend);
189         }
190 }
191
192 int VBoxPort::bridge_rx(unsigned char *data, int len)
193 {
194         if (p_record)
195                 record(data, len, 1); // from up
196         return 0;
197 }
198
199 /*
200  * endpoint sends messages to the vbox port
201  */
202 int VBoxPort::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
203 {
204         struct lcr_msg *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                 PDEBUG(DEBUG_EPOINT|DEBUG_VBOX, "PORT(%s) no endpoint object found where the message is from.\n", p_name);
214                 return(0);
215         }
216
217         switch(message_id) {
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
241                 case MESSAGE_RELEASE: /* release vbox port */
242                 vbox_trace_header(this, "RELEASE to VBox", DIRECTION_OUT);
243                 add_trace("cause", "value", "%d", param->disconnectinfo.cause);
244                 add_trace("cause", "location", "%d", param->disconnectinfo.location);
245                 end_trace();
246
247                 /* we are done */
248                 /* recording is close during destruction */
249                 delete this;
250                 return(-1); /* must return because port is gone */
251
252                 case MESSAGE_SETUP: /* dial-out command received from epoint, answer with connect */
253                 /* get apppbx */
254                 if (epoint->ep_app_type == EAPP_TYPE_PBX)
255                         memcpy(&p_vbox_ext, &((class EndpointAppPBX *)(epoint->ep_app))->e_ext, sizeof(p_vbox_ext));
256                 /* extract optional announcement file */
257                 if ((c = strchr(param->setup.dialinginfo.id, ','))) {
258                         if (c[1] == '/')
259                                 SPRINT(filename, c+1);
260                         else
261                                 SPRINT(filename, "%s/%s/vbox/%s", EXTENSION_DATA, p_vbox_ext.number);
262                         *c = '\0';
263                 } else {
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                         /* send connect message */
290                         message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_CONNECT);
291                         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
292                         message_put(message);
293                         vbox_trace_header(this, "CONNECT from VBox (after setup)", DIRECTION_IN);
294                         end_trace();
295                         new_state(PORT_STATE_CONNECT);
296                 } else {
297                         /* send alerting message */
298                         message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_ALERTING);
299                         message_put(message);
300                         vbox_trace_header(this, "ALERTING from VBox (play announcement before connect)", DIRECTION_IN);
301                         end_trace();
302                         new_state(PORT_STATE_IN_ALERTING);
303                 }
304
305                 /* play the announcement */
306                 if ((p_vbox_announce_fh = open_tone(filename, &p_vbox_announce_codec, &p_vbox_announce_size, &p_vbox_announce_left)) >= 0) {
307                         fhuse++;
308                         schedule_timer(&p_vbox_announce_timer, 0, 300000);
309                 } 
310                 vbox_trace_header(this, "ANNOUNCEMENT", DIRECTION_OUT);
311                 add_trace("file", "name", "%s", filename);
312                 add_trace("file", "exists", "%s", (p_vbox_announce_fh>=0)?"yes":"no");
313                 end_trace();
314                 /* start recording if desired */
315                 p_vbox_mode = p_vbox_ext.vbox_mode;
316                 p_vbox_record_limit = p_vbox_ext.vbox_time;
317                 if (p_vbox_announce_fh<0 || p_vbox_mode==VBOX_MODE_PARALLEL) {
318                         /* recording start */
319                         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);
320                         vbox_trace_header(this, "RECORDING", DIRECTION_IN);
321                         end_trace();
322                 }
323                 return(1);
324
325                 default:
326                 PDEBUG(DEBUG_VBOX, "PORT(%s) vbox port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
327         }
328
329         return(0);
330 }
331
332