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 */
20 int announce_timer(struct lcr_timer *timer, void *instance, int index);
21 int record_timeout(struct lcr_timer *timer, void *instance, int index);
24 * initialize vbox port
26 VBoxPort::VBoxPort(int type, struct port_settings *settings) : Port(type, "vbox", settings)
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);
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;
55 static void vbox_trace_header(class VBoxPort *vbox, const char *message, int direction)
57 /* init trace with given values */
60 vbox?numberrize_callerinfo(vbox->p_callerinfo.id, vbox->p_callerinfo.ntype, options.national, options.international):NULL,
61 vbox?vbox->p_dialinginfo.id:NULL,
64 vbox?vbox->p_serial:0,
69 int record_timeout(struct lcr_timer *timer, void *instance, int index)
71 class VBoxPort *vboxport = (class VBoxPort *)instance;
72 struct lcr_msg *message;
74 while(vboxport->p_epointlist) {
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;
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);
85 vboxport->free_epointlist(vboxport->p_epointlist);
87 /* recording is close during destruction */
92 int announce_timer(struct lcr_timer *timer, void *instance, int index)
94 class VBoxPort *vboxport = (class VBoxPort *)instance;
96 /* port my self destruct here */
97 vboxport->send_announcement();
102 void VBoxPort::send_announcement(void)
104 struct lcr_msg *message;
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;
109 struct timeval current_time;
110 long long now; /* Time in samples */
112 /* don't restart timer, if announcement is played */
113 if (p_vbox_announce_fh < 0)
116 gettimeofday(¤t_time, NULL);
117 now = (current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec)/125;
119 /* set time the first time */
120 if (!p_vbox_audio_start)
121 p_vbox_audio_start = now - PORT_TRANSMIT;
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);
128 /* schedule next event */
129 temp = PORT_TRANSMIT + PORT_TRANSMIT - tosend;
132 schedule_timer(&p_vbox_announce_timer, 0, temp*125);
134 /* add the number of samples elapsed */
135 p_vbox_audio_transferred += tosend;
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);
141 close(p_vbox_announce_fh);
142 p_vbox_announce_fh = -1;
145 if (p_vbox_record_limit)
146 schedule_timer(&p_vbox_record_timeout, p_vbox_record_limit, 0);
148 /* connect if not already */
149 epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
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);
159 new_state(PORT_STATE_CONNECT);
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);
170 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; /* must return because port is gone */
186 record(buffer, tosend, 0); // from down
187 /* send to remote, if bridged */
188 bridge_tx(buffer, tosend);
192 int VBoxPort::bridge_rx(unsigned char *data, int len)
195 record(data, len, 1); // from up
200 * endpoint sends messages to the vbox port
202 int VBoxPort::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
204 struct lcr_msg *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);
213 PDEBUG(DEBUG_EPOINT|DEBUG_VBOX, "PORT(%s) no endpoint object found where the message is from.\n", p_name);
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);
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);
235 free_epointlist(p_epointlist);
237 /* recording is close during destruction */
239 return(-1); /* must return because port is gone */
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);
248 /* recording is close during destruction */
250 return(-1); /* must return because port is gone */
252 case MESSAGE_SETUP: /* dial-out command received from epoint, answer with connect */
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, ','))) {
259 SPRINT(filename, c+1);
261 SPRINT(filename, "%s/%s/vbox/%s", EXTENSION_DATA, p_vbox_ext.number);
264 SPRINT(filename, "%s/%s/vbox/announcement", EXTENSION_DATA, 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) {
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);
295 new_state(PORT_STATE_CONNECT);
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);
302 new_state(PORT_STATE_IN_ALERTING);
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) {
308 schedule_timer(&p_vbox_announce_timer, 0, 300000);
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");
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);
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);