2 * Asterisk -- A telephony toolkit for Linux.
4 * Routines implementing call parking
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/options.h>
20 #include <asterisk/module.h>
21 #include <asterisk/translate.h>
22 #include <asterisk/say.h>
23 #include <asterisk/channel_pvt.h>
24 #include <asterisk/parking.h>
25 #include <asterisk/musiconhold.h>
26 #include <asterisk/config.h>
34 #include <sys/signal.h>
35 #include <netinet/in.h>
39 #define DEFAULT_PARK_TIME 45000
41 static char *parkedcall = "ParkedCall";
43 /* No more than 45 seconds parked before you do something with them */
44 static int parkingtime = DEFAULT_PARK_TIME;
46 /* Context for which parking is made accessible */
47 static char parking_con[AST_MAX_EXTENSION] = "parkedcalls";
49 /* Extension you type to park the call */
50 static char parking_ext[AST_MAX_EXTENSION] = "700";
52 /* First available extension for parking */
53 static int parking_start = 701;
55 /* Last available extension for parking */
56 static int parking_stop = 750;
58 /* Registrar for operations */
59 static char *registrar = "res_parking";
61 static char *synopsis = "Answer a parked call";
63 static char *descrip = "ParkedCall(exten):"
64 "Used to connect to a parked call. This Application is always\n"
65 "registered internally and does not need to be explicitly added\n"
66 "into the dialplan, although you should include the 'parkedcalls'\n"
70 struct ast_channel *chan;
73 /* Where to go if our parking time expires */
74 char context[AST_MAX_EXTENSION];
75 char exten[AST_MAX_EXTENSION];
78 struct parkeduser *next;
81 static struct parkeduser *parkinglot;
83 static pthread_mutex_t parking_lock = AST_MUTEX_INITIALIZER;
85 static pthread_t parking_thread;
91 char *ast_parking_ext(void)
96 int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeout, int *extout)
98 /* We put the user in the parking list, then wake up the parking thread to be sure it looks
99 after these channels too */
100 struct parkeduser *pu, *cur;
102 pu = malloc(sizeof(struct parkeduser));
104 ast_pthread_mutex_lock(&parking_lock);
105 for (x=parking_start;x<=parking_stop;x++) {
108 if (cur->parkingnum == x)
115 if (x <= parking_stop) {
117 ast_moh_start(pu->chan, NULL);
118 gettimeofday(&pu->start, NULL);
121 pu->parkingtime = timeout;
123 pu->parkingtime = parkingtime;
126 /* Remember what had been dialed, so that if the parking
127 expires, we try to come back to the same place */
128 strncpy(pu->context, chan->context, sizeof(pu->context)-1);
129 strncpy(pu->exten, chan->exten, sizeof(pu->exten)-1);
130 pu->priority = chan->priority;
131 pu->next = parkinglot;
133 ast_pthread_mutex_unlock(&parking_lock);
134 /* Wake up the (presumably select()ing) thread */
135 pthread_kill(parking_thread, SIGURG);
136 if (option_verbose > 1)
137 ast_verbose(VERBOSE_PREFIX_2 "Parked %s on %d\n", pu->chan->name, pu->parkingnum);
139 ast_say_digits(peer, pu->parkingnum, "", peer->language);
140 /* Start music on hold */
143 ast_log(LOG_WARNING, "No more parking spaces\n");
145 ast_pthread_mutex_unlock(&parking_lock);
149 ast_log(LOG_WARNING, "Out of memory\n");
155 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
157 struct ast_channel *chan;
159 /* Make a new, fake channel that we'll use to masquerade in the real one */
160 chan = ast_channel_alloc(0);
162 /* Let us keep track of the channel name */
163 snprintf(chan->name, sizeof (chan->name), "Parked/%s",rchan->name);
164 /* Make formats okay */
165 chan->readformat = rchan->readformat;
166 chan->writeformat = rchan->writeformat;
167 ast_channel_masquerade(chan, rchan);
168 /* Setup the extensions and such */
169 strncpy(chan->context, rchan->context, sizeof(chan->context) - 1);
170 strncpy(chan->exten, rchan->exten, sizeof(chan->exten) - 1);
171 chan->priority = rchan->priority;
172 /* Make the masq execute */
176 ast_park_call(chan, peer, timeout, extout);
178 ast_log(LOG_WARNING, "Unable to create parked channel\n");
184 int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, int allowredirect, int allowdisconnect)
186 /* Copy voice back and forth between the two channels. Give the peer
187 the ability to transfer calls with '#<extension' syntax. */
190 struct ast_channel *who;
191 char newext[256], *ptr;
193 struct ast_option_header *aoh;
194 /* Answer if need be */
195 if (ast_answer(chan))
197 peer->appl = "Bridged Call";
198 peer->data = chan->name;
200 res = ast_channel_bridge(chan, peer, (allowdisconnect ? AST_BRIDGE_DTMF_CHANNEL_0 : 0) + (allowredirect ? AST_BRIDGE_DTMF_CHANNEL_1 : 0), &f, &who);
202 ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
206 if (!f || ((f->frametype == AST_FRAME_CONTROL) && ((f->subclass == AST_CONTROL_HANGUP) || (f->subclass == AST_CONTROL_BUSY) ||
207 (f->subclass == AST_CONTROL_CONGESTION)))) {
211 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RINGING)) {
213 ast_indicate(peer, AST_CONTROL_RINGING);
215 ast_indicate(chan, AST_CONTROL_RINGING);
217 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == -1)) {
219 ast_indicate(peer, -1);
221 ast_indicate(chan, -1);
223 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_OPTION)) {
225 /* Forward option Requests */
226 if (aoh && (aoh->flag == AST_OPTION_FLAG_REQUEST)) {
228 ast_channel_setoption(peer, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
230 ast_channel_setoption(chan, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
233 if (f && (f->frametype == AST_FRAME_DTMF) && (who == chan) && allowdisconnect &&
234 (f->subclass == '*')) {
235 if (option_verbose > 3)
236 ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
241 if ((f->frametype == AST_FRAME_DTMF) && (who == peer) && allowredirect &&
242 (f->subclass == '#')) {
243 /* Start autoservice on chan while we talk
245 ast_autoservice_start(chan);
246 memset(newext, 0, sizeof(newext));
249 if ((res=ast_streamfile(peer, "pbx-transfer", peer->language))) {
250 ast_autoservice_stop(chan);
253 if ((res=ast_waitstream(peer, AST_DIGIT_ANY)) < 0) {
254 ast_autoservice_stop(chan);
257 ast_stopstream(peer);
259 /* If they've typed a digit already, handle it */
265 while(strlen(newext) < sizeof(newext) - 1) {
266 res = ast_waitfordigit(peer, 3000);
272 if (!ast_matchmore_extension(peer, peer->context, newext, 1, peer->callerid)) {
278 ast_autoservice_stop(chan);
281 if (!strcmp(newext, ast_parking_ext())) {
282 if (ast_autoservice_stop(chan))
284 else if (!ast_park_call(chan, peer, 0, NULL)) {
285 /* We return non-zero, but tell the PBX not to hang the channel when
286 the thread dies -- We have to be careful now though. We are responsible for
287 hanging up the channel, else it will never be hung up! */
288 res=AST_PBX_KEEPALIVE;
291 ast_log(LOG_WARNING, "Unable to park call %s\n", chan->name);
293 /* XXX Maybe we should have another message here instead of invalid extension XXX */
294 } else if (ast_exists_extension(chan, peer->context, newext, 1, peer->callerid)) {
295 res=ast_autoservice_stop(chan);
297 /* Doh! Use our handy async_goto funcitons */
298 if (option_verbose > 2)
299 ast_verbose(VERBOSE_PREFIX_3 "Transferring %s to '%s' (context %s) priority 1\n", chan->name, chan->exten, chan->context);
300 if (ast_async_goto(chan, peer->context, newext, 1, 1))
301 ast_log(LOG_WARNING, "Async goto fialed :(\n");
303 /* Set the channel's new extension, since it exists, using peer context */
304 strncpy(chan->exten, newext, sizeof(chan->exten)-1);
305 strncpy(chan->context, peer->context, sizeof(chan->context)-1);
311 if (option_verbose > 2)
312 ast_verbose(VERBOSE_PREFIX_3 "Unable to find extension '%s' in context %s\n", newext, peer->context);
314 res = ast_streamfile(peer, "pbx-invalid", chan->language);
316 ast_autoservice_stop(chan);
319 res = ast_waitstream(peer, AST_DIGIT_ANY);
320 ast_stopstream(peer);
321 res = ast_autoservice_stop(chan);
323 if (option_verbose > 1)
324 ast_verbose(VERBOSE_PREFIX_2 "Hungup during autoservice stop on '%s'\n", chan->name);
327 if (f && (f->frametype == AST_FRAME_DTMF)) {
334 ast_log(LOG_DEBUG, "Read from %s (%d,%d)\n", who->name, f->frametype, f->subclass);
343 static void *do_parking_thread(void *ignore)
346 struct parkeduser *pu, *pl, *pt = NULL;
357 ast_pthread_mutex_lock(&parking_lock);
360 gettimeofday(&tv, NULL);
364 tms = (tv.tv_sec - pu->start.tv_sec) * 1000 + (tv.tv_usec - pu->start.tv_usec) / 1000;
365 if (tms > pu->parkingtime) {
366 /* They've been waiting too long, send them back to where they came. Theoretically they
367 should have their original extensions and such, but we copy to be on the safe side */
368 strncpy(pu->chan->exten, pu->exten, sizeof(pu->chan->exten)-1);
369 strncpy(pu->chan->context, pu->context, sizeof(pu->chan->context)-1);
370 pu->chan->priority = pu->priority;
371 /* Stop music on hold */
372 ast_moh_stop(pu->chan);
373 /* Start up the PBX, or hang them up */
374 if (ast_pbx_start(pu->chan)) {
375 ast_log(LOG_WARNING, "Unable to restart the PBX for user on '%s', hanging them up...\n", pu->chan->name);
376 ast_hangup(pu->chan);
378 /* And take them out of the parking lot */
382 parkinglot = pu->next;
387 for (x=0;x<AST_MAX_FDS;x++) {
388 if ((pu->chan->fds[x] > -1) && (FD_ISSET(pu->chan->fds[x], &rfds) || FD_ISSET(pu->chan->fds[x], &efds))) {
389 if (FD_ISSET(pu->chan->fds[x], &efds))
390 pu->chan->exception = 1;
392 /* See if they need servicing */
393 f = ast_read(pu->chan);
394 if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
395 /* There's a problem, hang them up*/
396 if (option_verbose > 1)
397 ast_verbose(VERBOSE_PREFIX_2 "%s got tired of being parked\n", pu->chan->name);
398 ast_hangup(pu->chan);
399 /* And take them out of the parking lot */
403 parkinglot = pu->next;
409 /* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
411 goto std; /* XXX Ick: jumping into an else statement??? XXX */
415 if (x >= AST_MAX_FDS) {
416 std: for (x=0;x<AST_MAX_FDS;x++) {
417 /* Keep this one for next one */
418 if (pu->chan->fds[x] > -1) {
419 FD_SET(pu->chan->fds[x], &nrfds);
420 FD_SET(pu->chan->fds[x], &nefds);
421 if (pu->chan->fds[x] > max)
422 max = pu->chan->fds[x];
425 /* Keep track of our longest wait */
426 if ((tms < ms) || (ms < 0))
433 ast_pthread_mutex_unlock(&parking_lock);
436 tv.tv_sec = ms / 1000;
437 tv.tv_usec = (ms % 1000) * 1000;
438 /* Wait for something to happen */
439 select(max + 1, &rfds, NULL, &efds, (ms > -1) ? &tv : NULL);
440 pthread_testcancel();
442 return NULL; /* Never reached */
445 static int park_exec(struct ast_channel *chan, void *data)
449 struct ast_channel *peer=NULL;
450 struct parkeduser *pu, *pl=NULL;
454 ast_log(LOG_WARNING, "Park requires an argument (extension number)\n");
458 park = atoi((char *)data);
459 ast_pthread_mutex_lock(&parking_lock);
462 if (pu->parkingnum == park) {
466 parkinglot = pu->next;
472 ast_pthread_mutex_unlock(&parking_lock);
477 /* JK02: it helps to answer the channel if not already up */
478 if (chan->_state != AST_STATE_UP) {
484 res = ast_channel_make_compatible(chan, peer);
486 ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
490 /* This runs sorta backwards, since we give the incoming channel control, as if it
491 were the person called. */
492 if (option_verbose > 2)
493 ast_verbose(VERBOSE_PREFIX_3 "Channel %s connected to parked call %d\n", chan->name, park);
494 res = ast_bridge_call(peer, chan, 1, 0);
495 /* Simulate the PBX hanging up */
496 if (res != AST_PBX_KEEPALIVE)
500 /* XXX Play a message XXX */
501 dres = ast_streamfile(chan, "pbx-invalidpark", chan->language);
503 dres = ast_waitstream(chan, "");
505 ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", "pbx-invalidpark", chan->name);
508 if (option_verbose > 2)
509 ast_verbose(VERBOSE_PREFIX_3 "Channel %s tried to talk to non-existant parked call %d\n", chan->name, park);
512 LOCAL_USER_REMOVE(u);
516 int load_module(void)
521 struct ast_context *con;
522 char exten[AST_MAX_EXTENSION];
523 struct ast_config *cfg;
524 struct ast_variable *var;
525 cfg = ast_load("parking.conf");
527 var = ast_variable_browse(cfg, "general");
529 if (!strcasecmp(var->name, "parkext")) {
530 strncpy(parking_ext, var->value, sizeof(parking_ext) - 1);
531 } else if (!strcasecmp(var->name, "context")) {
532 strncpy(parking_con, var->value, sizeof(parking_con) - 1);
533 } else if (!strcasecmp(var->name, "parkingtime")) {
534 if ((sscanf(var->value, "%d", &parkingtime) != 1) || (parkingtime < 1)) {
535 ast_log(LOG_WARNING, "%s is not a valid parkingtime\n", var->value);
536 parkingtime = DEFAULT_PARK_TIME;
538 parkingtime = parkingtime * 1000;
539 } else if (!strcasecmp(var->name, "parkpos")) {
540 if (sscanf(var->value, "%i-%i", &start, &end) != 2) {
541 ast_log(LOG_WARNING, "Format for parking positions is a-b, where a and b are numbers at line %d of parking.conf\n", var->lineno);
543 parking_start = start;
551 con = ast_context_find(parking_con);
553 con = ast_context_create(parking_con, registrar);
555 ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", parking_con);
559 for(x=parking_start; x<=parking_stop;x++) {
560 snprintf(exten, sizeof(exten), "%d", x);
561 ast_add_extension2(con, 1, exten, 1, NULL, parkedcall, strdup(exten), free, registrar);
563 pthread_create(&parking_thread, NULL, do_parking_thread, NULL);
564 res = ast_register_application(parkedcall, park_exec, synopsis, descrip);
568 int unload_module(void)
570 STANDARD_HANGUP_LOCALUSERS;
571 return ast_unregister_application(parkedcall);
574 char *description(void)
576 return "Call Parking Resource";
581 /* Never allow parking to be unloaded because it will
582 unresolve needed symbols in the dialer */
585 STANDARD_USECOUNT(res);
594 return ASTERISK_GPL_KEY;