1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** answering machine port **
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 **
14 \*****************************************************************************/
22 #include <sys/ioctl.h>
23 #include <sys/types.h>
28 /* note: recording log is written at endpoint */
31 * initialize vbox port
33 VBoxPort::VBoxPort(int type, struct port_settings *settings) : Port(type, "vbox", settings)
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;
49 if (p_vbox_announce_fh >= 0)
51 close(p_vbox_announce_fh);
52 p_vbox_announce_fh = -1;
58 static void vbox_trace_header(class VBoxPort *vbox, char *message, int direction)
60 /* init trace with given values */
63 vbox?numberrize_callerinfo(vbox->p_callerinfo.id, vbox->p_callerinfo.ntype):NULL,
64 vbox?vbox->p_dialinginfo.id:NULL,
67 vbox?vbox->p_serial:0,
75 int VBoxPort::handler(void)
77 struct message *message;
79 unsigned char buffer[ISDN_TRANSMIT];
81 class Endpoint *epoint;
84 if ((ret = Port::handler()))
87 if (p_vbox_record_start && p_vbox_record_limit)
90 if (currenttime > (p_vbox_record_limit+p_vbox_record_start))
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;
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);
104 free_epointlist(p_epointlist);
106 /* recording is close during destruction */
108 return(-1); /* must return because port is gone */
112 /* set time the first time */
113 if (p_vbox_audio_start < 1)
115 p_vbox_audio_start = now_d;
119 /* calculate the number of bytes */
120 tosend = (unsigned long)((now_d-p_vbox_audio_start)*8000) - p_vbox_audio_transferred;
123 if (tosend < sizeof(buffer))
125 tosend = sizeof(buffer);
127 /* add the number of samples elapsed */
128 p_vbox_audio_transferred += tosend;
130 /* if announcement is currently played, send audio data */
131 if (p_vbox_announce_fh >=0)
133 tosend = read_tone(p_vbox_announce_fh, buffer, p_vbox_announce_codec, tosend, p_vbox_announce_size, &p_vbox_announce_left, 1);
137 close(p_vbox_announce_fh);
138 p_vbox_announce_fh = -1;
142 p_vbox_record_start = currenttime;
144 /* connect if not already */
145 epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
148 /* if we sent our announcement during ringing, we must now connect */
149 if (p_vbox_ext.vbox_free)
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);
157 new_state(PORT_STATE_CONNECT);
161 /* start recording, if not already */
162 if (p_vbox_mode == VBOX_MODE_NORMAL)
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);
169 if (p_vbox_mode == VBOX_MODE_ANNOUNCEMENT)
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);
180 /* recording is close during destruction */
182 return(-1); /* must return because port is gone */
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);
200 * endpoint sends messages to the vbox port
202 int VBoxPort::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
204 struct message *message;
205 class Endpoint *epoint;
206 char filename[256], *c;
208 if (Port::message_epoint(epoint_id, message_id, param))
211 epoint = find_epoint_id(epoint_id);
214 PDEBUG(DEBUG_EPOINT|DEBUG_VBOX, "PORT(%s) no endpoint object found where the message is from.\n", p_name);
221 record(param->data.data, param->data.len, 1); // from up
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);
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);
242 free_epointlist(p_epointlist);
244 /* recording is close during destruction */
246 return(-1); /* must return because port is gone */
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);
256 /* recording is close during destruction */
258 return(-1); /* must return because port is gone */
261 case MESSAGE_SETUP: /* dial-out command received from epoint, answer with connect */
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, ',')))
268 SPRINT(filename, c+1);
270 SPRINT(filename, "%s/%s/%s/vbox/%s", INSTALL_DATA, options.extensions_dir, p_vbox_ext.number);
274 SPRINT(filename, "%s/%s/%s/vbox/announcement", INSTALL_DATA, options.extensions_dir, p_vbox_ext.number);
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);
280 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
281 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
282 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
285 FATAL("PORT(%s) Epoint pointer is set in idle state, how bad!!\n", p_name);
286 epointlist_new(epoint_id);
288 /* copy setup infos to port */
289 SCPY(p_vbox_extension, param->setup.dialinginfo.id);
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;
297 /* connect unless we can send announcement while ringing */
298 if (!p_vbox_ext.vbox_free)
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);
306 new_state(PORT_STATE_CONNECT);
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);
314 new_state(PORT_STATE_IN_ALERTING);
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)
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");
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)
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);
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);