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 \*****************************************************************************/
18 /* note: recording log is written at endpoint */
21 * initialize vbox port
23 VBoxPort::VBoxPort(int type, struct port_settings *settings) : Port(type, "vbox", settings)
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;
39 if (p_vbox_announce_fh >= 0)
41 close(p_vbox_announce_fh);
42 p_vbox_announce_fh = -1;
48 static void vbox_trace_header(class VBoxPort *vbox, char *message, int direction)
50 /* init trace with given values */
53 vbox?numberrize_callerinfo(vbox->p_callerinfo.id, vbox->p_callerinfo.ntype, options.national, options.international):NULL,
54 vbox?vbox->p_dialinginfo.id:NULL,
57 vbox?vbox->p_serial:0,
65 int VBoxPort::handler(void)
67 struct lcr_msg *message;
69 unsigned char buffer[ISDN_TRANSMIT];
71 class Endpoint *epoint;
74 if ((ret = Port::handler()))
77 if (p_vbox_record_start && p_vbox_record_limit)
80 if (currenttime > (p_vbox_record_limit+p_vbox_record_start))
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;
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);
94 free_epointlist(p_epointlist);
96 /* recording is close during destruction */
98 return(-1); /* must return because port is gone */
102 /* set time the first time */
103 if (p_vbox_audio_start < 1)
105 p_vbox_audio_start = now_d;
109 /* calculate the number of bytes */
110 tosend = (unsigned int)((now_d-p_vbox_audio_start)*8000) - p_vbox_audio_transferred;
113 if (tosend < sizeof(buffer))
115 tosend = sizeof(buffer);
117 /* add the number of samples elapsed */
118 p_vbox_audio_transferred += tosend;
120 /* if announcement is currently played, send audio data */
121 if (p_vbox_announce_fh >=0)
123 tosend = read_tone(p_vbox_announce_fh, buffer, p_vbox_announce_codec, tosend, p_vbox_announce_size, &p_vbox_announce_left, 1);
127 close(p_vbox_announce_fh);
128 p_vbox_announce_fh = -1;
132 p_vbox_record_start = currenttime;
134 /* connect if not already */
135 epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
138 /* if we sent our announcement during ringing, we must now connect */
139 if (p_vbox_ext.vbox_free)
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);
147 new_state(PORT_STATE_CONNECT);
151 /* start recording, if not already */
152 if (p_vbox_mode == VBOX_MODE_NORMAL)
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);
159 if (p_vbox_mode == VBOX_MODE_ANNOUNCEMENT)
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);
170 /* recording is close during destruction */
172 return(-1); /* must return because port is gone */
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);
190 * endpoint sends messages to the vbox port
192 int VBoxPort::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
194 struct lcr_msg *message;
195 class Endpoint *epoint;
196 char filename[256], *c;
198 if (Port::message_epoint(epoint_id, message_id, param))
201 epoint = find_epoint_id(epoint_id);
204 PDEBUG(DEBUG_EPOINT|DEBUG_VBOX, "PORT(%s) no endpoint object found where the message is from.\n", p_name);
211 record(param->data.data, param->data.len, 1); // from up
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);
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);
232 free_epointlist(p_epointlist);
234 /* recording is close during destruction */
236 return(-1); /* must return because port is gone */
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);
246 /* recording is close during destruction */
248 return(-1); /* must return because port is gone */
251 case MESSAGE_SETUP: /* dial-out command received from epoint, answer with connect */
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, ',')))
258 SPRINT(filename, c+1);
260 SPRINT(filename, "%s/%s/%s/vbox/%s", INSTALL_DATA, options.extensions_dir, p_vbox_ext.number);
264 SPRINT(filename, "%s/%s/%s/vbox/announcement", INSTALL_DATA, options.extensions_dir, p_vbox_ext.number);
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);
270 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
271 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
272 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
275 FATAL("PORT(%s) Epoint pointer is set in idle state, how bad!!\n", p_name);
276 epointlist_new(epoint_id);
278 /* copy setup infos to port */
279 SCPY(p_vbox_extension, param->setup.dialinginfo.id);
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;
287 /* connect unless we can send announcement while ringing */
288 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);
296 new_state(PORT_STATE_CONNECT);
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);
304 new_state(PORT_STATE_IN_ALERTING);
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)
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");
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)
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);
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);