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[ISDN_TRANSMIT];
107 class Endpoint *epoint;
109 struct timeval current_time;
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;
119 /* set time the first time */
120 if (!p_vbox_audio_start)
121 p_vbox_audio_start = now - ISDN_TRANSMIT;
123 /* calculate the number of bytes */
124 tosend = (unsigned int)(now - p_vbox_audio_start) - p_vbox_audio_transferred;
126 /* schedule next event */
127 temp = ISDN_TRANSMIT + ISDN_TRANSMIT - tosend;
130 schedule_timer(&p_vbox_announce_timer, 0, temp*125);
132 if (tosend > sizeof(buffer))
133 tosend = sizeof(buffer);
135 /* add the number of samples elapsed */
136 p_vbox_audio_transferred += tosend;
138 /* if announcement is currently played, send audio data */
139 tosend = read_tone(p_vbox_announce_fh, buffer, p_vbox_announce_codec, tosend, p_vbox_announce_size, &p_vbox_announce_left, 1);
142 close(p_vbox_announce_fh);
143 p_vbox_announce_fh = -1;
146 if (p_vbox_record_limit)
147 schedule_timer(&p_vbox_record_timeout, p_vbox_record_limit, 0);
149 /* connect if not already */
150 epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
152 /* if we sent our announcement during ringing, we must now connect */
153 if (p_vbox_ext.vbox_free) {
154 /* send connect message */
155 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
156 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
157 message_put(message);
158 vbox_trace_header(this, "CONNECT from VBox (announcement is over)", DIRECTION_IN);
160 new_state(PORT_STATE_CONNECT);
164 /* start recording, if not already */
165 if (p_vbox_mode == VBOX_MODE_NORMAL) {
166 /* recording start */
167 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);
168 vbox_trace_header(this, "RECORDING (announcement is over)", DIRECTION_IN);
171 if (p_vbox_mode == VBOX_MODE_ANNOUNCEMENT) {
173 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
174 message->param.disconnectinfo.cause = 16;
175 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
176 message_put(message);
177 vbox_trace_header(this, "RELEASE from VBox (after annoucement)", DIRECTION_IN);
178 add_trace("cause", "value", "%d", message->param.disconnectinfo.cause);
179 add_trace("cause", "location", "%d", message->param.disconnectinfo.location);
181 /* recording is close during destruction */
183 return; /* 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);
197 * endpoint sends messages to the vbox port
199 int VBoxPort::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
201 struct lcr_msg *message;
202 class Endpoint *epoint;
203 char filename[256], *c;
205 if (Port::message_epoint(epoint_id, message_id, param))
208 epoint = find_epoint_id(epoint_id);
210 PDEBUG(DEBUG_EPOINT|DEBUG_VBOX, "PORT(%s) no endpoint object found where the message is from.\n", p_name);
216 record(param->data.data, param->data.len, 1); // from up
219 case MESSAGE_DISCONNECT: /* call has been disconnected */
220 new_state(PORT_STATE_OUT_DISCONNECT);
221 vbox_trace_header(this, "DISCONNECT to VBox", DIRECTION_OUT);
222 add_trace("cause", "value", "%d", param->disconnectinfo.cause);
223 add_trace("cause", "location", "%d", param->disconnectinfo.location);
226 while(p_epointlist) {
227 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
228 message->param.disconnectinfo.cause = CAUSE_NORMAL;
229 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
230 message_put(message);
231 vbox_trace_header(this, "RELEASE from VBox (after disconnect)", DIRECTION_IN);
232 add_trace("cause", "value", "%d", message->param.disconnectinfo.cause);
233 add_trace("cause", "location", "%d", message->param.disconnectinfo.location);
236 free_epointlist(p_epointlist);
238 /* recording is close during destruction */
240 return(-1); /* must return because port is gone */
243 case MESSAGE_RELEASE: /* release vbox port */
244 vbox_trace_header(this, "RELEASE to VBox", DIRECTION_OUT);
245 add_trace("cause", "value", "%d", param->disconnectinfo.cause);
246 add_trace("cause", "location", "%d", param->disconnectinfo.location);
250 /* recording is close during destruction */
252 return(-1); /* must return because port is gone */
255 case MESSAGE_SETUP: /* dial-out command received from epoint, answer with connect */
257 memcpy(&p_vbox_ext, &((class EndpointAppPBX *)(epoint->ep_app))->e_ext, sizeof(p_vbox_ext));
258 /* extract optional announcement file */
259 if ((c = strchr(param->setup.dialinginfo.id, ','))) {
261 SPRINT(filename, c+1);
263 SPRINT(filename, "%s/%s/vbox/%s", EXTENSION_DATA, p_vbox_ext.number);
266 SPRINT(filename, "%s/%s/vbox/announcement", EXTENSION_DATA, p_vbox_ext.number);
268 vbox_trace_header(this, "SETUP to VBox", DIRECTION_OUT);
269 add_trace("from", "id", "%s", param->setup.callerinfo.id);
270 add_trace("to", "box", "%s", param->setup.dialinginfo.id);
272 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
273 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
274 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
277 FATAL("PORT(%s) Epoint pointer is set in idle state, how bad!!\n", p_name);
278 epointlist_new(epoint_id);
280 /* copy setup infos to port */
281 SCPY(p_vbox_extension, param->setup.dialinginfo.id);
283 /* create connect info */
284 SCPY(p_connectinfo.id, p_vbox_extension);
285 p_connectinfo.itype = INFO_ITYPE_VBOX;
286 p_connectinfo.present = INFO_PRESENT_ALLOWED;
287 p_connectinfo.screen = INFO_SCREEN_NETWORK;
289 /* connect unless we can send announcement while ringing */
290 if (!p_vbox_ext.vbox_free) {
291 /* send connect message */
292 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_CONNECT);
293 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
294 message_put(message);
295 vbox_trace_header(this, "CONNECT from VBox (after setup)", DIRECTION_IN);
297 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) {
310 schedule_timer(&p_vbox_announce_timer, 0, 300000);
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) {
320 /* recording start */
321 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);
322 vbox_trace_header(this, "RECORDING", DIRECTION_IN);
328 PDEBUG(DEBUG_VBOX, "PORT(%s) vbox port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);