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 static char *parkedcall = "ParkedCall";
41 /* No more than 45 seconds parked before you do something with them */
42 static int parkingtime = 45000;
44 /* Context for which parking is made accessible */
45 static char parking_con[AST_MAX_EXTENSION] = "parkedcalls";
47 /* Extension you type to park the call */
48 static char parking_ext[AST_MAX_EXTENSION] = "700";
50 /* First available extension for parking */
51 static int parking_start = 701;
53 /* Last available extension for parking */
54 static int parking_stop = 750;
56 /* Registrar for operations */
57 static char *registrar = "res_parking";
59 static char *synopsis = "Answer a parked call";
61 static char *descrip = "ParkedCall(exten):"
62 "Used to connect to a parked call. This Application is always\n"
63 "registered internally and does not need to be explicitly added\n"
64 "into the dialplan, although you should include the 'parkedcalls'\n"
68 struct ast_channel *chan;
71 /* Where to go if our parking time expires */
72 char context[AST_MAX_EXTENSION];
73 char exten[AST_MAX_EXTENSION];
75 struct parkeduser *next;
78 static struct parkeduser *parkinglot;
80 static pthread_mutex_t parking_lock = AST_MUTEX_INITIALIZER;
82 static pthread_t parking_thread;
88 char *ast_parking_ext(void)
93 int ast_park_call(struct ast_channel *chan, struct ast_channel *peer)
95 /* We put the user in the parking list, then wake up the parking thread to be sure it looks
96 after these channels too */
97 struct parkeduser *pu, *cur;
99 pu = malloc(sizeof(struct parkeduser));
101 ast_pthread_mutex_lock(&parking_lock);
102 for (x=parking_start;x<=parking_stop;x++) {
105 if (cur->parkingnum == x)
112 if (x <= parking_stop) {
114 ast_moh_start(pu->chan, NULL);
115 gettimeofday(&pu->start, NULL);
117 /* Remember what had been dialed, so that if the parking
118 expires, we try to come back to the same place */
119 strncpy(pu->context, chan->context, sizeof(pu->context)-1);
120 strncpy(pu->exten, chan->exten, sizeof(pu->exten)-1);
121 pu->priority = chan->priority;
122 pu->next = parkinglot;
124 ast_pthread_mutex_unlock(&parking_lock);
125 /* Wake up the (presumably select()ing) thread */
126 pthread_kill(parking_thread, SIGURG);
127 if (option_verbose > 1)
128 ast_verbose(VERBOSE_PREFIX_2 "Parked %s on %d\n", pu->chan->name, pu->parkingnum);
129 ast_say_digits(peer, pu->parkingnum, "", peer->language);
130 /* Start music on hold */
133 ast_log(LOG_WARNING, "No more parking spaces\n");
135 ast_pthread_mutex_unlock(&parking_lock);
139 ast_log(LOG_WARNING, "Out of memory\n");
145 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer)
147 struct ast_channel *chan;
149 /* Make a new, fake channel that we'll use to masquerade in the real one */
150 chan = ast_channel_alloc(0);
152 /* Let us keep track of the channel name */
153 snprintf(chan->name, sizeof (chan->name), "Parked/%s",rchan->name);
154 /* Make formats okay */
155 chan->readformat = rchan->readformat;
156 chan->writeformat = rchan->writeformat;
157 ast_channel_masquerade(chan, rchan);
158 /* Setup the extensions and such */
159 strncpy(chan->context, rchan->context, sizeof(chan->context) - 1);
160 strncpy(chan->exten, rchan->exten, sizeof(chan->exten) - 1);
161 chan->priority = rchan->priority;
162 /* Make the masq execute */
166 ast_park_call(chan, peer);
168 ast_log(LOG_WARNING, "Unable to create parked channel\n");
174 int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, int allowredirect)
176 /* Copy voice back and forth between the two channels. Give the peer
177 the ability to transfer calls with '#<extension' syntax. */
180 struct ast_channel *who;
181 char newext[256], *ptr;
183 struct ast_option_header *aoh;
184 /* Answer if need be */
185 if (chan->state != AST_STATE_UP) {
186 if (ast_answer(chan))
189 peer->appl = "Bridged Call";
190 peer->data = chan->name;
192 res = ast_channel_bridge(chan, peer, allowredirect ? AST_BRIDGE_DTMF_CHANNEL_1 : 0, &f, &who);
194 ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
198 if (!f || ((f->frametype == AST_FRAME_CONTROL) && ((f->subclass == AST_CONTROL_HANGUP) || (f->subclass == AST_CONTROL_BUSY) ||
199 (f->subclass == AST_CONTROL_CONGESTION)))) {
203 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RINGING)) {
205 ast_indicate(peer, AST_CONTROL_RINGING);
207 ast_indicate(chan, AST_CONTROL_RINGING);
209 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_OPTION)) {
211 /* Forward option Requests */
212 if (aoh && (aoh->flag == AST_OPTION_FLAG_REQUEST)) {
214 ast_channel_setoption(peer, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
216 ast_channel_setoption(chan, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
219 if ((f->frametype == AST_FRAME_DTMF) && (who == peer) && allowredirect &&
220 (f->subclass == '#')) {
221 memset(newext, 0, sizeof(newext));
224 if ((res=ast_streamfile(peer, "pbx-transfer", chan->language)))
226 if ((res=ast_waitstream(peer, AST_DIGIT_ANY)) < 0)
228 ast_stopstream(peer);
230 /* If they've typed a digit already, handle it */
236 while(strlen(newext) < sizeof(newext - 1)) {
237 res = ast_waitfordigit(peer, 3000);
241 if (!ast_canmatch_extension(peer, peer->context, newext, 1, peer->callerid) ||
242 ast_exists_extension(peer, peer->context, newext, 1, peer->callerid)) {
249 if (!strcmp(newext, ast_parking_ext())) {
250 if (!ast_park_call(chan, peer)) {
251 /* We return non-zero, but tell the PBX not to hang the channel when
252 the thread dies -- We have to be careful now though. We are responsible for
253 hanging up the channel, else it will never be hung up! */
254 res=AST_PBX_KEEPALIVE;
257 ast_log(LOG_WARNING, "Unable to park call %s\n", chan->name);
259 /* XXX Maybe we should have another message here instead of invalid extension XXX */
260 } else if (ast_exists_extension(chan, peer->context, newext, 1, peer->callerid)) {
261 /* Set the channel's new extension, since it exists, using peer context */
262 strncpy(chan->exten, newext, sizeof(chan->exten)-1);
263 strncpy(chan->context, peer->context, sizeof(chan->context)-1);
266 if (option_verbose > 2)
267 ast_verbose(VERBOSE_PREFIX_3 "Transferring %s to '%s' (context %s) priority 1\n", chan->name, chan->exten, chan->context);
271 if (option_verbose > 2)
272 ast_verbose(VERBOSE_PREFIX_3 "Unable to find extension '%s' in context %s\n", newext, peer->context);
274 res = ast_streamfile(peer, "pbx-invalid", chan->language);
277 res = ast_waitstream(peer, AST_DIGIT_ANY);
278 ast_stopstream(peer);
281 if (f && (f->frametype == AST_FRAME_DTMF)) {
288 ast_log(LOG_DEBUG, "Read from %s (%d,%d)\n", who->name, f->frametype, f->subclass);
297 static void *do_parking_thread(void *ignore)
300 struct parkeduser *pu, *pl, *pt = NULL;
311 ast_pthread_mutex_lock(&parking_lock);
314 gettimeofday(&tv, NULL);
318 tms = (tv.tv_sec - pu->start.tv_sec) * 1000 + (tv.tv_usec - pu->start.tv_usec) / 1000;
319 if (tms > parkingtime) {
320 /* They've been waiting too long, send them back to where they came. Theoretically they
321 should have their original extensions and such, but we copy to be on the safe side */
322 strncpy(pu->chan->exten, pu->exten, sizeof(pu->chan->exten)-1);
323 strncpy(pu->chan->context, pu->context, sizeof(pu->chan->context)-1);
324 pu->chan->priority = pu->priority;
325 /* Stop music on hold */
326 ast_moh_stop(pu->chan);
327 /* Start up the PBX, or hang them up */
328 if (ast_pbx_start(pu->chan)) {
329 ast_log(LOG_WARNING, "Unable to restart the PBX for user on '%s', hanging them up...\n", pu->chan->name);
330 ast_hangup(pu->chan);
332 /* And take them out of the parking lot */
336 parkinglot = pu->next;
341 for (x=0;x<AST_MAX_FDS;x++) {
342 if ((pu->chan->fds[x] > -1) && (FD_ISSET(pu->chan->fds[x], &rfds) || FD_ISSET(pu->chan->fds[x], &efds))) {
343 if (FD_ISSET(pu->chan->fds[x], &efds))
344 pu->chan->exception = 1;
346 /* See if they need servicing */
347 f = ast_read(pu->chan);
348 if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
349 /* There's a problem, hang them up*/
350 if (option_verbose > 1)
351 ast_verbose(VERBOSE_PREFIX_2 "%s got tired of being parked\n", pu->chan->name);
352 ast_hangup(pu->chan);
353 /* And take them out of the parking lot */
357 parkinglot = pu->next;
363 /* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
365 goto std; /* XXX Ick: jumping into an else statement??? XXX */
369 if (x >= AST_MAX_FDS) {
370 std: for (x=0;x<AST_MAX_FDS;x++) {
371 /* Keep this one for next one */
372 if (pu->chan->fds[x] > -1) {
373 FD_SET(pu->chan->fds[x], &nrfds);
374 FD_SET(pu->chan->fds[x], &nefds);
375 if (pu->chan->fds[x] > max)
376 max = pu->chan->fds[x];
379 /* Keep track of our longest wait */
380 if ((tms < ms) || (ms < 0))
387 ast_pthread_mutex_unlock(&parking_lock);
390 tv.tv_sec = ms / 1000;
391 tv.tv_usec = (ms % 1000) * 1000;
392 /* Wait for something to happen */
393 select(max + 1, &rfds, NULL, &efds, (ms > -1) ? &tv : NULL);
394 pthread_testcancel();
396 return NULL; /* Never reached */
399 static int park_exec(struct ast_channel *chan, void *data)
403 struct ast_channel *peer=NULL;
404 struct parkeduser *pu, *pl=NULL;
407 ast_log(LOG_WARNING, "Park requires an argument (extension number)\n");
411 park = atoi((char *)data);
412 ast_pthread_mutex_lock(&parking_lock);
415 if (pu->parkingnum == park) {
419 parkinglot = pu->next;
424 ast_pthread_mutex_unlock(&parking_lock);
431 res = ast_channel_make_compatible(chan, peer);
433 ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
437 /* This runs sorta backwards, since we give the incoming channel control, as if it
438 were the person called. */
439 if (option_verbose > 2)
440 ast_verbose(VERBOSE_PREFIX_3 "Channel %s connected to parked call %d\n", chan->name, park);
441 res = ast_bridge_call(peer, chan, 1);
442 /* Simulate the PBX hanging up */
443 if (res != AST_PBX_KEEPALIVE)
447 /* XXX Play a message XXX */
448 if (option_verbose > 2)
449 ast_verbose(VERBOSE_PREFIX_3 "Channel %s tried to talk to non-existant parked call %d\n", chan->name, park);
452 LOCAL_USER_REMOVE(u);
456 int load_module(void)
461 struct ast_context *con;
462 char exten[AST_MAX_EXTENSION];
463 struct ast_config *cfg;
464 struct ast_variable *var;
465 cfg = ast_load("parking.conf");
467 var = ast_variable_browse(cfg, "general");
469 if (!strcasecmp(var->name, "parkext")) {
470 strncpy(parking_ext, var->value, sizeof(parking_ext) - 1);
471 } else if (!strcasecmp(var->name, "context")) {
472 strncpy(parking_con, var->value, sizeof(parking_con) - 1);
473 } else if (!strcasecmp(var->name, "parkpos")) {
474 if (sscanf(var->value, "%i-%i", &start, &end) != 2) {
475 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);
477 parking_start = start;
485 con = ast_context_find(parking_con);
487 con = ast_context_create(parking_con, registrar);
489 ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", parking_con);
493 for(x=parking_start; x<=parking_stop;x++) {
494 snprintf(exten, sizeof(exten), "%d", x);
495 ast_add_extension2(con, 1, exten, 1, NULL, parkedcall, strdup(exten), free, registrar);
497 pthread_create(&parking_thread, NULL, do_parking_thread, NULL);
498 res = ast_register_application(parkedcall, park_exec, synopsis, descrip);
502 int unload_module(void)
504 STANDARD_HANGUP_LOCALUSERS;
505 return ast_unregister_application(parkedcall);
508 char *description(void)
510 return "Call Parking Resource";
515 /* Never allow parking to be unloaded because it will
516 unresolve needed symbols in the dialer */
519 STANDARD_USECOUNT(res);
528 return ASTERISK_GPL_KEY;