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) {
40 close(p_vbox_announce_fh);
41 p_vbox_announce_fh = -1;
47 static void vbox_trace_header(class VBoxPort *vbox, const char *message, int direction)
49 /* init trace with given values */
52 vbox?numberrize_callerinfo(vbox->p_callerinfo.id, vbox->p_callerinfo.ntype, options.national, options.international):NULL,
53 vbox?vbox->p_dialinginfo.id:NULL,
56 vbox?vbox->p_serial:0,
64 int VBoxPort::handler(void)
66 struct lcr_msg *message;
68 unsigned char buffer[ISDN_TRANSMIT];
70 class Endpoint *epoint;
73 if ((ret = Port::handler()))
76 if (p_vbox_record_start && p_vbox_record_limit) {
78 if (currenttime > (p_vbox_record_limit+p_vbox_record_start)) {
81 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
82 message->param.disconnectinfo.cause = 16;
83 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
85 vbox_trace_header(this, "RELEASE from VBox (recoding limit reached)", DIRECTION_IN);
86 add_trace("cause", "value", "%d", message->param.disconnectinfo.cause);
87 add_trace("cause", "location", "%d", message->param.disconnectinfo.location);
90 free_epointlist(p_epointlist);
92 /* recording is close during destruction */
94 return(-1); /* must return because port is gone */
98 /* set time the first time */
99 if (p_vbox_audio_start < 1) {
100 p_vbox_audio_start = now_d;
104 /* calculate the number of bytes */
105 tosend = (unsigned int)((now_d-p_vbox_audio_start)*8000) - p_vbox_audio_transferred;
108 if (tosend < sizeof(buffer))
110 tosend = sizeof(buffer);
112 /* add the number of samples elapsed */
113 p_vbox_audio_transferred += tosend;
115 /* if announcement is currently played, send audio data */
116 if (p_vbox_announce_fh >=0) {
117 tosend = read_tone(p_vbox_announce_fh, buffer, p_vbox_announce_codec, tosend, p_vbox_announce_size, &p_vbox_announce_left, 1);
120 close(p_vbox_announce_fh);
121 p_vbox_announce_fh = -1;
125 p_vbox_record_start = currenttime;
127 /* connect if not already */
128 epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
130 /* if we sent our announcement during ringing, we must now connect */
131 if (p_vbox_ext.vbox_free) {
132 /* send connect message */
133 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
134 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
135 message_put(message);
136 vbox_trace_header(this, "CONNECT from VBox (announcement is over)", DIRECTION_IN);
138 new_state(PORT_STATE_CONNECT);
142 /* start recording, if not already */
143 if (p_vbox_mode == VBOX_MODE_NORMAL) {
144 /* recording start */
145 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);
146 vbox_trace_header(this, "RECORDING (announcement is over)", DIRECTION_IN);
149 if (p_vbox_mode == VBOX_MODE_ANNOUNCEMENT) {
151 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
152 message->param.disconnectinfo.cause = 16;
153 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
154 message_put(message);
155 vbox_trace_header(this, "RELEASE from VBox (after annoucement)", DIRECTION_IN);
156 add_trace("cause", "value", "%d", message->param.disconnectinfo.cause);
157 add_trace("cause", "location", "%d", message->param.disconnectinfo.location);
159 /* recording is close during destruction */
161 return(-1); /* must return because port is gone */
165 record(buffer, tosend, 0); // from down
166 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DATA);
167 message->param.data.len = tosend;
168 memcpy(message->param.data.data, buffer, tosend);
169 message_put(message);
178 * endpoint sends messages to the vbox port
180 int VBoxPort::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
182 struct lcr_msg *message;
183 class Endpoint *epoint;
184 char filename[256], *c;
186 if (Port::message_epoint(epoint_id, message_id, param))
189 epoint = find_epoint_id(epoint_id);
191 PDEBUG(DEBUG_EPOINT|DEBUG_VBOX, "PORT(%s) no endpoint object found where the message is from.\n", p_name);
197 record(param->data.data, param->data.len, 1); // from up
200 case MESSAGE_DISCONNECT: /* call has been disconnected */
201 new_state(PORT_STATE_OUT_DISCONNECT);
202 vbox_trace_header(this, "DISCONNECT to VBox", DIRECTION_OUT);
203 add_trace("cause", "value", "%d", param->disconnectinfo.cause);
204 add_trace("cause", "location", "%d", param->disconnectinfo.location);
207 while(p_epointlist) {
208 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
209 message->param.disconnectinfo.cause = CAUSE_NORMAL;
210 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
211 message_put(message);
212 vbox_trace_header(this, "RELEASE from VBox (after disconnect)", DIRECTION_IN);
213 add_trace("cause", "value", "%d", message->param.disconnectinfo.cause);
214 add_trace("cause", "location", "%d", message->param.disconnectinfo.location);
217 free_epointlist(p_epointlist);
219 /* recording is close during destruction */
221 return(-1); /* must return because port is gone */
224 case MESSAGE_RELEASE: /* release vbox port */
225 vbox_trace_header(this, "RELEASE to VBox", DIRECTION_OUT);
226 add_trace("cause", "value", "%d", param->disconnectinfo.cause);
227 add_trace("cause", "location", "%d", param->disconnectinfo.location);
231 /* recording is close during destruction */
233 return(-1); /* must return because port is gone */
236 case MESSAGE_SETUP: /* dial-out command received from epoint, answer with connect */
238 memcpy(&p_vbox_ext, &((class EndpointAppPBX *)(epoint->ep_app))->e_ext, sizeof(p_vbox_ext));
239 /* extract optional announcement file */
240 if ((c = strchr(param->setup.dialinginfo.id, ','))) {
242 SPRINT(filename, c+1);
244 SPRINT(filename, "%s/%s/vbox/%s", EXTENSION_DATA, p_vbox_ext.number);
247 SPRINT(filename, "%s/%s/vbox/announcement", EXTENSION_DATA, p_vbox_ext.number);
249 vbox_trace_header(this, "SETUP to VBox", DIRECTION_OUT);
250 add_trace("from", "id", "%s", param->setup.callerinfo.id);
251 add_trace("to", "box", "%s", param->setup.dialinginfo.id);
253 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
254 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
255 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
258 FATAL("PORT(%s) Epoint pointer is set in idle state, how bad!!\n", p_name);
259 epointlist_new(epoint_id);
261 /* copy setup infos to port */
262 SCPY(p_vbox_extension, param->setup.dialinginfo.id);
264 /* create connect info */
265 SCPY(p_connectinfo.id, p_vbox_extension);
266 p_connectinfo.itype = INFO_ITYPE_VBOX;
267 p_connectinfo.present = INFO_PRESENT_ALLOWED;
268 p_connectinfo.screen = INFO_SCREEN_NETWORK;
270 /* connect unless we can send announcement while ringing */
271 if (!p_vbox_ext.vbox_free) {
272 /* send connect message */
273 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_CONNECT);
274 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
275 message_put(message);
276 vbox_trace_header(this, "CONNECT from VBox (after setup)", DIRECTION_IN);
278 new_state(PORT_STATE_CONNECT);
280 /* send alerting message */
281 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_ALERTING);
282 message_put(message);
283 vbox_trace_header(this, "ALERTING from VBox (play announcement before connect)", DIRECTION_IN);
285 new_state(PORT_STATE_IN_ALERTING);
288 /* play the announcement */
289 if ((p_vbox_announce_fh = open_tone(filename, &p_vbox_announce_codec, &p_vbox_announce_size, &p_vbox_announce_left)) >= 0) {
292 vbox_trace_header(this, "ANNOUNCEMENT", DIRECTION_OUT);
293 add_trace("file", "name", "%s", filename);
294 add_trace("file", "exists", "%s", (p_vbox_announce_fh>=0)?"yes":"no");
296 /* start recording if desired */
297 p_vbox_mode = p_vbox_ext.vbox_mode;
298 p_vbox_record_limit = p_vbox_ext.vbox_time;
299 if (p_vbox_announce_fh<0 || p_vbox_mode==VBOX_MODE_PARALLEL) {
300 /* recording start */
301 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);
302 vbox_trace_header(this, "RECORDING", DIRECTION_IN);
308 PDEBUG(DEBUG_VBOX, "PORT(%s) vbox port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);