6eb0ce20e0bf59ebbf9fdec060228f3357f231f8
[lcr.git] / vbox.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** answering machine port                                                    **
9 **                                                                           **
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               **
13 **                                                                           **
14 \*****************************************************************************/ 
15
16 #include "main.h"
17
18 /* note: recording log is written at endpoint */
19
20 int announce_timer(struct lcr_timer *timer, void *instance, int index);
21 int record_timeout(struct lcr_timer *timer, void *instance, int index);
22
23 /*
24  * initialize vbox port
25  */
26 VBoxPort::VBoxPort(int type, struct port_settings *settings) : Port(type, "vbox", settings)
27 {
28         p_vbox_timeout = 0;
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);
37 }
38
39
40 /*
41  * destructor
42  */
43 VBoxPort::~VBoxPort()
44 {
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;
50                 fhuse--;
51         }
52 }
53
54
55 static void vbox_trace_header(class VBoxPort *vbox, const char *message, int direction)
56 {
57         /* init trace with given values */
58         start_trace(-1,
59                     NULL,
60                     vbox?numberrize_callerinfo(vbox->p_callerinfo.id, vbox->p_callerinfo.ntype, options.national, options.international):NULL,
61                     vbox?vbox->p_dialinginfo.id:NULL,
62                     direction,
63                     CATEGORY_CH,
64                     vbox?vbox->p_serial:0,
65                     message);
66 }
67
68
69 int record_timeout(struct lcr_timer *timer, void *instance, int index)
70 {
71         class VBoxPort *vboxport = (class VBoxPort *)instance;
72         struct lcr_msg  *message;
73
74         while(vboxport->p_epointlist) {
75                 /* send release */
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;
79                 message_put(message);
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);
83                 end_trace();
84                 /* remove epoint */
85                 vboxport->free_epointlist(vboxport->p_epointlist);
86         }
87         /* recording is close during destruction */
88         delete vboxport;
89         return 0;
90 }
91
92 int announce_timer(struct lcr_timer *timer, void *instance, int index)
93 {
94         class VBoxPort *vboxport = (class VBoxPort *)instance;
95
96         /* port my self destruct here */
97         vboxport->send_announcement();
98
99         return 0;
100 }
101
102 void VBoxPort::send_announcement(void)
103 {
104         struct lcr_msg  *message;
105         unsigned int    tosend;
106         unsigned char   buffer[ISDN_TRANSMIT];
107         class Endpoint  *epoint;
108         int             temp;
109         struct timeval current_time;
110         long long       now;
111
112         /* don't restart timer, if announcement is played */
113         if (p_vbox_announce_fh < 0)
114                 return;
115
116         gettimeofday(&current_time, NULL);
117         now = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec;
118
119         /* set time the first time */
120         if (!p_vbox_audio_start)
121                 p_vbox_audio_start = now - ISDN_TRANSMIT;
122         
123         /* calculate the number of bytes */
124         tosend = (unsigned int)(now - p_vbox_audio_start) - p_vbox_audio_transferred;
125
126         /* schedule next event */
127         temp = ISDN_TRANSMIT + ISDN_TRANSMIT - tosend;
128         if (temp < 0)
129                 temp = 0;
130         schedule_timer(&p_vbox_announce_timer, 0, temp*125);
131
132         if (tosend > sizeof(buffer))
133                 tosend = sizeof(buffer);
134
135         /* add the number of samples elapsed */
136         p_vbox_audio_transferred += tosend;
137
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);
140         if (tosend <= 0) {
141                 /* end of file */
142                 close(p_vbox_announce_fh);
143                 p_vbox_announce_fh = -1;
144                 fhuse--;
145
146                 if (p_vbox_record_limit)
147                         schedule_timer(&p_vbox_record_timeout, p_vbox_record_limit, 0);
148
149                 /* connect if not already */
150                 epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
151                 if (epoint) {
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);
159                                 end_trace();
160                                 new_state(PORT_STATE_CONNECT);
161                         }
162                 }
163
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);
169                         end_trace();
170                 } else // else!!
171                 if (p_vbox_mode == VBOX_MODE_ANNOUNCEMENT) {
172                         /* send release */
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);
180                         end_trace();
181                         /* recording is close during destruction */
182                         delete this;
183                         return; /* must return because port is gone */
184                 }
185         } else {
186                 if (p_record)
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);
192         }
193 }
194
195
196 /*
197  * endpoint sends messages to the vbox port
198  */
199 int VBoxPort::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
200 {
201         struct lcr_msg *message;
202         class Endpoint *epoint;
203         char filename[256], *c;
204
205         if (Port::message_epoint(epoint_id, message_id, param))
206                 return(1);
207
208         epoint = find_epoint_id(epoint_id);
209         if (!epoint) {
210                 PDEBUG(DEBUG_EPOINT|DEBUG_VBOX, "PORT(%s) no endpoint object found where the message is from.\n", p_name);
211                 return(0);
212         }
213
214         switch(message_id) {
215                 case MESSAGE_DATA:
216                 record(param->data.data, param->data.len, 1); // from up
217                 return(1);
218
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);
224                 end_trace();
225
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);
234                         end_trace();
235                         /* remove epoint */
236                         free_epointlist(p_epointlist);
237                 }
238                 /* recording is close during destruction */
239                 delete this;
240                 return(-1); /* must return because port is gone */
241                 break;
242
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);
247                 end_trace();
248
249                 /* we are done */
250                 /* recording is close during destruction */
251                 delete this;
252                 return(-1); /* must return because port is gone */
253                 break;
254
255                 case MESSAGE_SETUP: /* dial-out command received from epoint, answer with connect */
256                 /* get apppbx */
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, ','))) {
260                         if (c[1] == '/')
261                                 SPRINT(filename, c+1);
262                         else
263                                 SPRINT(filename, "%s/%s/vbox/%s", EXTENSION_DATA, p_vbox_ext.number);
264                         *c = '\0';
265                 } else {
266                         SPRINT(filename, "%s/%s/vbox/announcement", EXTENSION_DATA, p_vbox_ext.number);
267                 }
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);
271                 end_trace();
272                 memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
273                 memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
274                 memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
275                 /* link relation */
276                 if (p_epointlist)
277                         FATAL("PORT(%s) Epoint pointer is set in idle state, how bad!!\n", p_name);
278                 epointlist_new(epoint_id);
279
280                 /* copy setup infos to port */
281                 SCPY(p_vbox_extension, param->setup.dialinginfo.id);
282
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;
288
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);
296                         end_trace();
297                         new_state(PORT_STATE_CONNECT);
298                 } else {
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);
303                         end_trace();
304                         new_state(PORT_STATE_IN_ALERTING);
305                 }
306
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) {
309                         fhuse++;
310                         schedule_timer(&p_vbox_announce_timer, 0, 300000);
311                 } 
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");
315                 end_trace();
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);
323                         end_trace();
324                 }
325                 break;
326
327                 default:
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);
329         }
330
331         return(0);
332 }
333
334