dect
/
asterisk
Archived
13
0
Fork 0

Version 0.1.8 from FTP

git-svn-id: http://svn.digium.com/svn/asterisk/trunk@316 f38db490-d61c-443f-a65b-d21fe96a405b
This commit is contained in:
markster 2001-05-07 03:15:48 +00:00
parent 99b3bfd61d
commit b92c0a231f
1 changed files with 181 additions and 142 deletions

View File

@ -27,6 +27,7 @@
#include <stdio.h> #include <stdio.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/signal.h> #include <sys/signal.h>
#include <netinet/in.h>
#include <pthread.h> #include <pthread.h>
@ -36,11 +37,31 @@ static char *app = "Dial";
static char *parkedcall = "ParkedCall"; static char *parkedcall = "ParkedCall";
/* No more than 90 seconds parked before you do something with them */ static char *synopsis = "Place an call and connect to the current channel";
static int parkingtime = 90000;
static char *registrar = "app_dial";
static char *descrip =
" Dial(Technology/resource[&Technology2/resource2...][|timeout][|transfer]): Requests one or more channels\n"
" and places specified outgoing calls on them. As soon as a channel answers, the Dial app\n"
" will answer the originating channel (if it needs to be answered) and will bridge a call\n"
" with the channel which first answered. All other calls placed by the Dial app will be\n"
" hung up. If a timeout is not specified, the Dial application will wait indefinitely until\n"
" either one of the called channels answers, the user hangs up, or all channels return busy or\n"
" error. In general, the dialler will return 0 if it was unable to place the call, or the\n"
" timeout expired. However, if all channels were busy, and there exists an extension with\n"
" priority n+101 (where n is the priority of the dialler instance), then it will be the\n"
" next executed extension (this allows you to setup different behavior on busy from no-answer)\n."
" This application returns -1 if the originating channel hangs up, or if the call is bridged and\n"
" either of the parties in the bridge terminate the call. The transfer string may contain a\n"
" 't' to allow the called user transfer a call or 'T' to allow the calling user to transfer the call.\n"
" In addition to transferring the call, a call may be parked and then picked up by another user.\n";
/* No more than 45 seconds parked before you do something with them */
static int parkingtime = 45000;
/* Context for which parking is made accessible */ /* Context for which parking is made accessible */
static char parking_con[AST_MAX_EXTENSION] = "default"; static char parking_con[AST_MAX_EXTENSION] = "parkedcalls";
/* Extension you type to park the call */ /* Extension you type to park the call */
static char parking_ext[AST_MAX_EXTENSION] = "700"; static char parking_ext[AST_MAX_EXTENSION] = "700";
@ -92,7 +113,7 @@ static int park_call(struct ast_channel *chan, struct ast_channel *peer)
int x; int x;
pu = malloc(sizeof(struct parkeduser)); pu = malloc(sizeof(struct parkeduser));
if (pu) { if (pu) {
pthread_mutex_lock(&parking_lock); ast_pthread_mutex_lock(&parking_lock);
for (x=parking_start;x<=parking_stop;x++) { for (x=parking_start;x<=parking_stop;x++) {
cur = parkinglot; cur = parkinglot;
while(cur) { while(cur) {
@ -114,7 +135,7 @@ static int park_call(struct ast_channel *chan, struct ast_channel *peer)
pu->priority = chan->priority; pu->priority = chan->priority;
pu->next = parkinglot; pu->next = parkinglot;
parkinglot = pu; parkinglot = pu;
pthread_mutex_unlock(&parking_lock); ast_pthread_mutex_unlock(&parking_lock);
/* Wake up the (presumably select()ing) thread */ /* Wake up the (presumably select()ing) thread */
pthread_kill(parking_thread, SIGURG); pthread_kill(parking_thread, SIGURG);
if (option_verbose > 1) if (option_verbose > 1)
@ -124,7 +145,7 @@ static int park_call(struct ast_channel *chan, struct ast_channel *peer)
} else { } else {
ast_log(LOG_WARNING, "No more parking spaces\n"); ast_log(LOG_WARNING, "No more parking spaces\n");
free(pu); free(pu);
pthread_mutex_unlock(&parking_lock); ast_pthread_mutex_unlock(&parking_lock);
return -1; return -1;
} }
} else { } else {
@ -140,6 +161,7 @@ static void *do_parking_thread(void *ignore)
struct parkeduser *pu, *pl, *pt = NULL; struct parkeduser *pu, *pl, *pt = NULL;
struct timeval tv; struct timeval tv;
struct ast_frame *f; struct ast_frame *f;
int x;
fd_set rfds, efds; fd_set rfds, efds;
fd_set nrfds, nefds; fd_set nrfds, nefds;
FD_ZERO(&rfds); FD_ZERO(&rfds);
@ -147,7 +169,7 @@ static void *do_parking_thread(void *ignore)
for (;;) { for (;;) {
ms = -1; ms = -1;
max = -1; max = -1;
pthread_mutex_lock(&parking_lock); ast_pthread_mutex_lock(&parking_lock);
pl = NULL; pl = NULL;
pu = parkinglot; pu = parkinglot;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
@ -174,43 +196,50 @@ static void *do_parking_thread(void *ignore)
pt = pu; pt = pu;
pu = pu->next; pu = pu->next;
free(pt); free(pt);
} else if (FD_ISSET(pu->chan->fd, &rfds) || FD_ISSET(pu->chan->fd, &efds)) { } else {
if (FD_ISSET(pu->chan->fd, &efds)) for (x=0;x<AST_MAX_FDS;x++) {
pu->chan->exception = 1; if ((pu->chan->fds[x] > -1) && (FD_ISSET(pu->chan->fds[x], &rfds) || FD_ISSET(pu->chan->fds[x], &efds))) {
/* See if they need servicing */ if (FD_ISSET(pu->chan->fds[x], &efds))
f = ast_read(pu->chan); pu->chan->exception = 1;
if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) { pu->chan->fdno = x;
/* There's a problem, hang them up*/ /* See if they need servicing */
if (option_verbose > 1) f = ast_read(pu->chan);
ast_verbose(VERBOSE_PREFIX_2 "%s got tired of being parked\n", pu->chan->name); if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
ast_hangup(pu->chan); /* There's a problem, hang them up*/
/* And take them out of the parking lot */ if (option_verbose > 1)
if (pl) ast_verbose(VERBOSE_PREFIX_2 "%s got tired of being parked\n", pu->chan->name);
pl->next = pu->next; ast_hangup(pu->chan);
else /* And take them out of the parking lot */
parkinglot = pu->next; if (pl)
pt = pu; pl->next = pu->next;
pu = pu->next; else
free(pt); parkinglot = pu->next;
} else { pt = pu;
/* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */ pu = pu->next;
ast_frfree(f); free(pt);
goto std; /* XXX Ick: jumping into an else statement??? XXX */ break;
} else {
/* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
ast_frfree(f);
goto std; /* XXX Ick: jumping into an else statement??? XXX */
}
}
}
if (x >= AST_MAX_FDS) {
/* Keep this one for next one */
std: FD_SET(pu->chan->fds[x], &nrfds);
FD_SET(pu->chan->fds[x], &nefds);
/* Keep track of our longest wait */
if ((tms < ms) || (ms < 0))
ms = tms;
if (pu->chan->fds[x] > max)
max = pu->chan->fds[x];
pl = pu;
pu = pu->next;
} }
} else {
/* Keep this one for next one */
std: FD_SET(pu->chan->fd, &nrfds);
FD_SET(pu->chan->fd, &nefds);
/* Keep track of our longest wait */
if ((tms < ms) || (ms < 0))
ms = tms;
if (pu->chan->fd > max)
max = pu->chan->fd;
pl = pu;
pu = pu->next;
} }
} }
pthread_mutex_unlock(&parking_lock); ast_pthread_mutex_unlock(&parking_lock);
rfds = nrfds; rfds = nrfds;
efds = nefds; efds = nefds;
tv.tv_sec = ms / 1000; tv.tv_sec = ms / 1000;
@ -236,43 +265,46 @@ static void hanguptree(struct localuser *outgoing, struct ast_channel *exception
} }
} }
#define MAX 256
static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localuser *outgoing, int *to, int *allowredir) static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localuser *outgoing, int *to, int *allowredir)
{ {
fd_set rfds, efds;
struct localuser *o; struct localuser *o;
int found; int found;
int numlines; int numlines;
int sentringing = 0;
int numbusies = 0; int numbusies = 0;
int orig = *to; int orig = *to;
struct timeval tv;
struct ast_frame *f; struct ast_frame *f;
struct ast_channel *peer = NULL; struct ast_channel *peer = NULL;
/* Watch all outgoing channels looking for an answer of some sort. */ struct ast_channel *watchers[MAX];
tv.tv_sec = *to / 1000; int pos;
tv.tv_usec = (*to % 1000) * 1000; int single;
while((tv.tv_sec || tv.tv_usec) && !peer) { struct ast_channel *winner;
FD_ZERO(&rfds);
FD_ZERO(&efds); single = (outgoing && !outgoing->next);
/* Always watch the input fd */
FD_SET(in->fd, &rfds); if (single) {
FD_SET(in->fd, &efds); /* If we are calling a single channel, make them compatible for in-band tone purpose */
ast_channel_make_compatible(outgoing->chan, in);
}
while(*to && !peer) {
o = outgoing; o = outgoing;
found = -1; found = -1;
pos = 1;
numlines = 0; numlines = 0;
watchers[0] = in;
while(o) { while(o) {
/* Keep track of important channels */
if (o->stillgoing) { if (o->stillgoing) {
/* Pay attention to this one */ watchers[pos++] = o->chan;
CHECK_BLOCKING(o->chan); found = 1;
FD_SET(o->chan->fd, &rfds);
FD_SET(o->chan->fd, &efds);
if (o->chan->fd > found)
found = o->chan->fd;
} }
numlines++;
o = o->next; o = o->next;
numlines++;
} }
/* If nobody is left, just go ahead and stop */ if (found < 0) {
if (found<0) {
if (numlines == numbusies) { if (numlines == numbusies) {
if (option_verbose > 2) if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_2 "Everyone is busy at this time\n"); ast_verbose( VERBOSE_PREFIX_2 "Everyone is busy at this time\n");
@ -283,76 +315,58 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localu
if (option_verbose > 2) if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_2 "No one is available to answer at this time\n"); ast_verbose( VERBOSE_PREFIX_2 "No one is available to answer at this time\n");
} }
break; *to = 0;
}
if (in->fd > found)
found = in->fd;
if (*to > -1)
found = select(found + 1, &rfds, NULL, &efds, &tv);
else
found = select(found + 1, &rfds, NULL, &efds, NULL);
if (found < 0) {
ast_log(LOG_WARNING, "select failed, returned %d (%s)\n", errno, strerror(errno));
*to = -1;
o = outgoing;
while(o) {
if (o->stillgoing) {
o->chan->blocking = 0;
}
o = o->next;
}
return NULL; return NULL;
} }
winner = ast_waitfor_n(watchers, pos, to);
o = outgoing; o = outgoing;
while(o) { while(o) {
if (o->stillgoing) { if (o->chan == winner) {
o->chan->blocking = 0; f = ast_read(winner);
if (FD_ISSET(o->chan->fd, &rfds) || FD_ISSET(o->chan->fd, &efds)) { if (f) {
if (FD_ISSET(o->chan->fd, &efds)) if (f->frametype == AST_FRAME_CONTROL) {
o->chan->exception = 1; switch(f->subclass) {
f = ast_read(o->chan); case AST_CONTROL_ANSWER:
if (f) { /* This is our guy if someone answered. */
if (f->frametype == AST_FRAME_CONTROL) { if (!peer) {
switch(f->subclass) {
case AST_CONTROL_ANSWER:
/* This is our guy if someone answered. */
if (!peer) {
if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
peer = o->chan;
*allowredir = o->allowredirect;
}
break;
case AST_CONTROL_BUSY:
if (option_verbose > 2) if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", o->chan->name); ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
o->stillgoing = 0; peer = o->chan;
numbusies++; *allowredir = o->allowredirect;
break;
case AST_CONTROL_RINGING:
if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", o->chan->name);
break;
case AST_CONTROL_OFFHOOK:
/* Ignore going off hook */
break;
default:
ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
} }
break;
case AST_CONTROL_BUSY:
if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", o->chan->name);
o->stillgoing = 0;
numbusies++;
break;
case AST_CONTROL_RINGING:
if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", o->chan->name);
if (!sentringing) {
ast_indicate(in, AST_CONTROL_RINGING);
sentringing++;
}
break;
case AST_CONTROL_OFFHOOK:
/* Ignore going off hook */
break;
default:
ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
} }
ast_frfree(f); } else if (single && (f->frametype == AST_FRAME_VOICE)) {
} else { if (ast_write(in, f))
o->stillgoing = 0; ast_log(LOG_WARNING, "Unable to forward frame\n");
} }
ast_frfree(f);
} else {
o->stillgoing = 0;
} }
} }
o = o->next; o = o->next;
} }
if (FD_ISSET(in->fd, &rfds) || FD_ISSET(in->fd, &efds)) { if (winner == in) {
/* After unblocking the entirity of the list, check for the main channel */
if (FD_ISSET(in->fd, &efds))
in->exception = 1;
f = ast_read(in); f = ast_read(in);
#if 0 #if 0
if (f && (f->frametype != AST_FRAME_VOICE)) if (f && (f->frametype != AST_FRAME_VOICE))
@ -364,12 +378,11 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localu
return NULL; return NULL;
} }
} }
if (!*to && (option_verbose > 2))
ast_verbose( VERBOSE_PREFIX_3 "Nobody picked up in %d ms\n", orig);
} }
if (!(tv.tv_sec || tv.tv_usec) && (option_verbose > 2))
ast_verbose( VERBOSE_PREFIX_3 "Nobody picked up in %d ms\n", orig);
*to = 0;
return peer; return peer;
} }
static int bridge_call(struct ast_channel *chan, struct ast_channel *peer, int allowredirect) static int bridge_call(struct ast_channel *chan, struct ast_channel *peer, int allowredirect)
@ -381,6 +394,7 @@ static int bridge_call(struct ast_channel *chan, struct ast_channel *peer, int a
struct ast_channel *who; struct ast_channel *who;
char newext[256], *ptr; char newext[256], *ptr;
int res; int res;
struct ast_option_header *aoh;
/* Answer if need be */ /* Answer if need be */
if (chan->state != AST_STATE_UP) if (chan->state != AST_STATE_UP)
if (ast_answer(chan)) if (ast_answer(chan))
@ -399,6 +413,22 @@ static int bridge_call(struct ast_channel *chan, struct ast_channel *peer, int a
res = -1; res = -1;
break; break;
} }
if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RINGING)) {
if (who == chan)
ast_indicate(peer, AST_CONTROL_RINGING);
else
ast_indicate(chan, AST_CONTROL_RINGING);
}
if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_OPTION)) {
aoh = f->data;
/* Forward option Requests */
if (aoh && (aoh->flag == AST_OPTION_FLAG_REQUEST)) {
if (who == chan)
ast_channel_setoption(peer, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
else
ast_channel_setoption(chan, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
}
}
if ((f->frametype == AST_FRAME_DTMF) && (who == peer) && allowredirect && if ((f->frametype == AST_FRAME_DTMF) && (who == peer) && allowredirect &&
(f->subclass == '#')) { (f->subclass == '#')) {
memset(newext, 0, sizeof(newext)); memset(newext, 0, sizeof(newext));
@ -438,14 +468,8 @@ static int bridge_call(struct ast_channel *chan, struct ast_channel *peer, int a
strncpy(chan->context, peer->context, sizeof(chan->context)); strncpy(chan->context, peer->context, sizeof(chan->context));
chan->priority = 0; chan->priority = 0;
ast_frfree(f); ast_frfree(f);
res=0; if (option_verbose > 2)
break; ast_verbose(VERBOSE_PREFIX_3 "Transferring %s to '%s' (context %s) priority 1\n", chan->name, chan->exten, chan->context);
} else if (ast_exists_extension(chan, "default", newext, 1)) {
/* Set the channel's new extension, since it exists, using peer context */
strncpy(chan->exten, newext, sizeof(chan->exten));
strncpy(chan->context, "default", sizeof(chan->context));
chan->priority = 0;
ast_frfree(f);
res=0; res=0;
break; break;
} }
@ -477,7 +501,7 @@ static int park_exec(struct ast_channel *chan, void *data)
} }
LOCAL_USER_ADD(u); LOCAL_USER_ADD(u);
park = atoi((char *)data); park = atoi((char *)data);
pthread_mutex_lock(&parking_lock); ast_pthread_mutex_lock(&parking_lock);
pu = parkinglot; pu = parkinglot;
while(pu) { while(pu) {
if (pu->parkingnum == park) { if (pu->parkingnum == park) {
@ -489,7 +513,7 @@ static int park_exec(struct ast_channel *chan, void *data)
} }
pu = pu->next; pu = pu->next;
} }
pthread_mutex_unlock(&parking_lock); ast_pthread_mutex_unlock(&parking_lock);
if (pu) { if (pu) {
peer = pu->chan; peer = pu->chan;
free(pu); free(pu);
@ -531,6 +555,7 @@ static int dial_exec(struct ast_channel *chan, void *data)
int allowredir=0; int allowredir=0;
char numsubst[AST_MAX_EXTENSION]; char numsubst[AST_MAX_EXTENSION];
char restofit[AST_MAX_EXTENSION]; char restofit[AST_MAX_EXTENSION];
char *transfer = NULL;
char *newnum; char *newnum;
if (!data) { if (!data) {
@ -553,6 +578,11 @@ static int dial_exec(struct ast_channel *chan, void *data)
if (timeout) { if (timeout) {
*timeout = '\0'; *timeout = '\0';
timeout++; timeout++;
transfer = strchr(timeout, '|');
if (transfer) {
*transfer = '\0';
transfer++;
}
} }
} else } else
timeout = NULL; timeout = NULL;
@ -583,14 +613,15 @@ static int dial_exec(struct ast_channel *chan, void *data)
ast_log(LOG_WARNING, "Out of memory\n"); ast_log(LOG_WARNING, "Out of memory\n");
goto out; goto out;
} }
tmp->allowredirect = 1; if (transfer && (strchr(transfer, 't')))
tmp->allowredirect = 1;
else
tmp->allowredirect = 0;
strncpy(numsubst, number, sizeof(numsubst)); strncpy(numsubst, number, sizeof(numsubst));
/* If we're dialing by extension, look at the extension to know what to dial */ /* If we're dialing by extension, look at the extension to know what to dial */
if ((newnum = strstr(numsubst, "BYEXTENSION"))) { if ((newnum = strstr(numsubst, "BYEXTENSION"))) {
strncpy(restofit, newnum + strlen("BYEXTENSION"), sizeof(restofit)); strncpy(restofit, newnum + strlen("BYEXTENSION"), sizeof(restofit));
snprintf(newnum, sizeof(numsubst) - (newnum - numsubst), "%s%s", chan->exten,restofit); snprintf(newnum, sizeof(numsubst) - (newnum - numsubst), "%s%s", chan->exten,restofit);
/* By default, if we're dialing by extension, don't permit redirecting */
tmp->allowredirect = 0;
if (option_debug) if (option_debug)
ast_log(LOG_DEBUG, "Dialing by extension %s\n", numsubst); ast_log(LOG_DEBUG, "Dialing by extension %s\n", numsubst);
} }
@ -605,8 +636,12 @@ static int dial_exec(struct ast_channel *chan, void *data)
} }
tmp->chan->appl = "AppDial"; tmp->chan->appl = "AppDial";
tmp->chan->data = "(Outgoing Line)"; tmp->chan->data = "(Outgoing Line)";
if (tmp->chan->callerid)
free(tmp->chan->callerid);
if (chan->callerid) if (chan->callerid)
tmp->chan->callerid = strdup(chan->callerid); tmp->chan->callerid = strdup(chan->callerid);
else
tmp->chan->callerid = NULL;
/* Place the call, but don't wait on the answer */ /* Place the call, but don't wait on the answer */
res = ast_call(tmp->chan, numsubst, 0); res = ast_call(tmp->chan, numsubst, 0);
if (res) { if (res) {
@ -617,6 +652,7 @@ static int dial_exec(struct ast_channel *chan, void *data)
ast_verbose(VERBOSE_PREFIX_3 "Couldn't call %s\n", numsubst); ast_verbose(VERBOSE_PREFIX_3 "Couldn't call %s\n", numsubst);
ast_hangup(tmp->chan); ast_hangup(tmp->chan);
free(tmp); free(tmp);
cur = rest;
continue; continue;
} else } else
if (option_verbose > 2) if (option_verbose > 2)
@ -630,7 +666,7 @@ static int dial_exec(struct ast_channel *chan, void *data)
cur = rest; cur = rest;
} while(cur); } while(cur);
if (timeout) if (timeout && strlen(timeout))
to = atoi(timeout) * 1000; to = atoi(timeout) * 1000;
else else
to = -1; to = -1;
@ -682,17 +718,20 @@ int load_module(void)
char exten[AST_MAX_EXTENSION]; char exten[AST_MAX_EXTENSION];
con = ast_context_find(parking_con); con = ast_context_find(parking_con);
if (!con) { if (!con) {
ast_log(LOG_ERROR, "Parking context '%s' does not exist\n", parking_con); con = ast_context_create(parking_con, registrar);
return -1; if (!con) {
ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", parking_con);
return -1;
}
} }
for(x=parking_start; x<=parking_stop;x++) { for(x=parking_start; x<=parking_stop;x++) {
snprintf(exten, sizeof(exten), "%d", x); snprintf(exten, sizeof(exten), "%d", x);
ast_add_extension2(con, 1, exten, 1, parkedcall, strdup(exten), free); ast_add_extension2(con, 1, exten, 1, parkedcall, strdup(exten), free, registrar);
} }
pthread_create(&parking_thread, NULL, do_parking_thread, NULL); pthread_create(&parking_thread, NULL, do_parking_thread, NULL);
res = ast_register_application(parkedcall, park_exec); res = ast_register_application(parkedcall, park_exec, synopsis, descrip);
if (!res) if (!res)
res = ast_register_application(app, dial_exec); res = ast_register_application(app, dial_exec, synopsis, descrip);
return res; return res;
} }