Newer
Older
Mark Spencer
committed
/*
* Asterisk -- A telephony toolkit for Linux.
*
* Channel Management
*
* Copyright (C) 1999 - 2005, Digium, Inc.
* Mark Spencer <markster@digium.com>
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <asterisk/sched.h>
#include <asterisk/options.h>
#include <asterisk/channel.h>
#include <asterisk/musiconhold.h>
#include <asterisk/say.h>
#include <asterisk/cli.h>
#include <asterisk/manager.h>
#include <asterisk/chanvars.h>
#include <asterisk/linkedlists.h>
James Golovich
committed
#include <asterisk/lock.h>
#include <asterisk/app.h>
#ifdef ZAPTEL_OPTIMIZATIONS
#include <sys/ioctl.h>
#else
#include <zaptel.h>
#endif /* __linux__ */
#ifndef ZT_TIMERPING
#error "You need newer zaptel! Please cvs update zaptel"
#endif
#include "asterisk.h"
/* uncomment if you have problems with 'monitoring' synchronized files */
#if 0
#define MONITOR_CONSTANT_DELAY
#define MONITOR_DELAY 150 * 8 /* 150 ms of MONITORING DELAY */
#endif
Mark Spencer
committed
unsigned long global_fin = 0, global_fout = 0;
const struct ast_channel_tech *tech;
/* Protect the channel list (highly unlikely that two things would change
it at the same time, but still! */
static int show_channeltypes(int fd, int argc, char *argv[])
{
#define FORMAT "%-7.7s %-50.50s\n"
struct chanlist *cl = backends;
ast_cli(fd, FORMAT, "Type", "Description");
ast_cli(fd, FORMAT, "------", "-----------");
if (ast_mutex_lock(&chlock)) {
ast_log(LOG_WARNING, "Unable to lock channel list\n");
return -1;
}
while (cl) {
ast_cli(fd, FORMAT, cl->tech->type, cl->tech->description);
cl = cl->next;
}
ast_mutex_unlock(&chlock);
return RESULT_SUCCESS;
}
static char show_channeltypes_usage[] =
"Usage: show channeltypes\n"
" Shows available channel types registered in your Asterisk server.\n";
static struct ast_cli_entry cli_show_channeltypes =
{ { "show", "channeltypes", NULL }, show_channeltypes, "Show available channel types", show_channeltypes_usage };
int ast_check_hangup(struct ast_channel *chan)
{
time_t myt;
/* if soft hangup flag, return true */
/* if no technology private data, return true */
if (!chan->tech_pvt) return 1;
/* if no hangup scheduled, just return here */
if (!chan->whentohangup) return 0;
time(&myt); /* get current time */
/* return, if not yet */
if (chan->whentohangup > myt) return 0;
static int ast_check_hangup_locked(struct ast_channel *chan)
{
int res;
ast_mutex_lock(&chan->lock);
res = ast_check_hangup(chan);
ast_mutex_unlock(&chan->lock);
void ast_begin_shutdown(int hangup)
{
struct ast_channel *c;
shutting_down = 1;
if (hangup) {
ast_mutex_lock(&chlock);
ast_mutex_unlock(&chlock);
}
}
int ast_active_channels(void)
{
struct ast_channel *c;
int cnt = 0;
ast_mutex_lock(&chlock);
c = channels;
while(c) {
cnt++;
c = c->next;
}
ast_mutex_unlock(&chlock);
return cnt;
}
void ast_cancel_shutdown(void)
{
shutting_down = 0;
}
int ast_shutting_down(void)
{
return shutting_down;
}
void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
{
if (offset)
chan->whentohangup = myt + offset;
else
chan->whentohangup = 0;
int ast_channel_register(const struct ast_channel_tech *tech)
struct chanlist *chan;
ast_mutex_lock(&chlock);
if (!strcasecmp(tech->type, chan->tech->type)) {
ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
ast_mutex_unlock(&chlock);
chan = malloc(sizeof(*chan));
if (!chan) {
ast_log(LOG_WARNING, "Out of memory\n");
ast_mutex_unlock(&chlock);
chan->tech = tech;
chan->next = backends;
backends = chan;
ast_log(LOG_DEBUG, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
if (option_verbose > 1)
ast_verbose(VERBOSE_PREFIX_2 "Registered channel type '%s' (%s)\n", chan->tech->type,
chan->tech->description);
ast_mutex_unlock(&chlock);
/* XXX Not reentrant XXX */
static char localtmp[256];
switch(state) {
case AST_STATE_DOWN:
return "Down";
case AST_STATE_RESERVED:
return "Rsrvd";
case AST_STATE_OFFHOOK:
return "OffHook";
case AST_STATE_DIALING:
return "Dialing";
case AST_STATE_RING:
return "Ring";
case AST_STATE_RINGING:
return "Ringing";
case AST_STATE_UP:
return "Up";
case AST_STATE_BUSY:
return "Busy";
default:
snprintf(localtmp, sizeof(localtmp), "Unknown (%d)\n", state);
return localtmp;
int ast_best_codec(int fmts)
{
/* This just our opinion, expressed in code. We are asked to choose
the best codec to use, given no information */
int x;
static int prefs[] =
{
/* Okay, ulaw is used by all telephony equipment, so start with it */
AST_FORMAT_ULAW,
/* Unless of course, you're a silly European, so then prefer ALAW */
AST_FORMAT_ALAW,
/* Okay, well, signed linear is easy to translate into other stuff */
AST_FORMAT_SLINEAR,
/* G.726 is standard ADPCM */
AST_FORMAT_G726,
/* ADPCM has great sound quality and is still pretty easy to translate */
AST_FORMAT_ADPCM,
/* Okay, we're down to vocoders now, so pick GSM because it's small and easier to
translate and sounds pretty good */
AST_FORMAT_GSM,
/* Speex is free, but computationally more expensive than GSM */
AST_FORMAT_SPEEX,
/* Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
to use it */
AST_FORMAT_LPC10,
/* G.729a is faster than 723 and slightly less expensive */
AST_FORMAT_G729A,
/* Down to G.723.1 which is proprietary but at least designed for voice */
AST_FORMAT_G723_1,
};
for (x=0;x<sizeof(prefs) / sizeof(prefs[0]); x++)
if (fmts & prefs[x])
return prefs[x];
ast_log(LOG_WARNING, "Don't know any of 0x%x formats\n", fmts);
return 0;
}
static const struct ast_channel_tech null_tech = {
.type = "NULL",
.description "Null channel (should not see this)",
};
struct ast_channel *ast_channel_alloc(int needqueue)
/* If shutting down, don't allocate any new channels */
if (shutting_down)
return NULL;
ast_mutex_lock(&chlock);
if (!tmp) {
ast_log(LOG_WARNING, "Out of memory\n");
ast_mutex_unlock(&chlock);
return NULL;
}
memset(tmp, 0, sizeof(struct ast_channel));
tmp->sched = sched_context_create();
if (!tmp->sched) {
ast_log(LOG_WARNING, "Unable to create schedule context\n");
free(tmp);
ast_mutex_unlock(&chlock);
return NULL;
}
for (x=0;x<AST_MAX_FDS - 1;x++)
tmp->fds[x] = -1;
#ifdef ZAPTEL_OPTIMIZATIONS
tmp->timingfd = open("/dev/zap/timer", O_RDWR);
if (tmp->timingfd > -1) {
/* Check if timing interface supports new
ping/pong scheme */
flags = 1;
if (!ioctl(tmp->timingfd, ZT_TIMERPONG, &flags))
needqueue = 0;
}
#else
tmp->timingfd = -1;
#endif
if (needqueue) {
if (pipe(tmp->alertpipe)) {
ast_log(LOG_WARNING, "Alert pipe creation failed!\n");
ast_mutex_unlock(&chlock);
return NULL;
} else {
flags = fcntl(tmp->alertpipe[0], F_GETFL);
fcntl(tmp->alertpipe[0], F_SETFL, flags | O_NONBLOCK);
flags = fcntl(tmp->alertpipe[1], F_GETFL);
fcntl(tmp->alertpipe[1], F_SETFL, flags | O_NONBLOCK);
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* Make sure we've got it done right if they don't */
tmp->alertpipe[0] = tmp->alertpipe[1] = -1;
/* Always watch the alertpipe */
tmp->fds[AST_MAX_FDS-1] = tmp->alertpipe[0];
/* And timing pipe */
tmp->fds[AST_MAX_FDS-2] = tmp->timingfd;
strncpy(tmp->name, "**Unknown**", sizeof(tmp->name)-1);
/* Initial state */
tmp->_state = AST_STATE_DOWN;
tmp->streamid = -1;
tmp->appl = NULL;
tmp->data = NULL;
tmp->fin = global_fin;
tmp->fout = global_fout;
snprintf(tmp->uniqueid, sizeof(tmp->uniqueid), "%li.%d", (long)time(NULL), uniqueint++);
headp = &tmp->varshead;
ast_mutex_init(&tmp->lock);
AST_LIST_HEAD_INIT(headp);
strncpy(tmp->context, "default", sizeof(tmp->context)-1);
strncpy(tmp->language, defaultlanguage, sizeof(tmp->language)-1);
strncpy(tmp->exten, "s", sizeof(tmp->exten)-1);
tmp->priority = 1;
tmp->amaflags = ast_default_amaflags;
strncpy(tmp->accountcode, ast_default_accountcode, sizeof(tmp->accountcode)-1);
tmp->tech = &null_tech;
tmp->next = channels;
channels = tmp;
ast_mutex_unlock(&chlock);
Mark Spencer
committed
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
{
struct ast_frame *f;
struct ast_frame *prev, *cur;
int blah = 1;
int qlen = 0;
f = ast_frdup(fin);
if (!f) {
ast_log(LOG_WARNING, "Unable to duplicate frame\n");
return -1;
}
Mark Spencer
committed
ast_mutex_lock(&chan->lock);
cur = chan->readq;
Mark Spencer
committed
if ((cur->frametype == AST_FRAME_CONTROL) && (cur->subclass == AST_CONTROL_HANGUP)) {
/* Don't bother actually queueing anything after a hangup */
ast_frfree(f);
ast_mutex_unlock(&chan->lock);
return 0;
}
prev = cur;
cur = cur->next;
qlen++;
}
/* Allow up to 96 voice frames outstanding, and up to 128 total frames */
if (((fin->frametype == AST_FRAME_VOICE) && (qlen > 96)) || (qlen > 128)) {
if (fin->frametype != AST_FRAME_VOICE) {
ast_log(LOG_WARNING, "Exceptionally long queue length queuing to %s\n", chan->name);
CRASH;
} else {
ast_log(LOG_DEBUG, "Dropping voice to exceptionally long queue on %s\n", chan->name);
Mark Spencer
committed
ast_mutex_unlock(&chan->lock);
chan->readq = f;
if (chan->alertpipe[1] > -1) {
if (write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah))
ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d (qlen = %d): %s!\n",
chan->name, f->frametype, f->subclass, qlen, strerror(errno));
#ifdef ZAPTEL_OPTIMIZATIONS
} else if (chan->timingfd > -1) {
ioctl(chan->timingfd, ZT_TIMERPING, &blah);
#endif
} else if (ast_test_flag(chan, AST_FLAG_BLOCKING)) {
pthread_kill(chan->blocker, SIGURG);
Mark Spencer
committed
ast_mutex_unlock(&chan->lock);
Mark Spencer
committed
int ast_queue_hangup(struct ast_channel *chan)
{
struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
chan->_softhangup |= AST_SOFTHANGUP_DEV;
Mark Spencer
committed
return ast_queue_frame(chan, &f);
Mark Spencer
committed
int ast_queue_control(struct ast_channel *chan, int control)
{
struct ast_frame f = { AST_FRAME_CONTROL, };
f.subclass = control;
Mark Spencer
committed
return ast_queue_frame(chan, &f);
int ast_channel_defer_dtmf(struct ast_channel *chan)
{
int pre = 0;
if (chan) {
pre = ast_test_flag(chan, AST_FLAG_DEFER_DTMF);
ast_set_flag(chan, AST_FLAG_DEFER_DTMF);
}
return pre;
}
void ast_channel_undefer_dtmf(struct ast_channel *chan)
{
if (chan)
ast_clear_flag(chan, AST_FLAG_DEFER_DTMF);
struct ast_channel *ast_channel_walk_locked(struct ast_channel *prev)
/* Returns next channel (locked) */
struct ast_channel *l, *ret;
Mark Spencer
committed
int retries = 0;
retry:
ast_mutex_lock(&chlock);
if (l) {
if (ast_mutex_trylock(&l->lock)) {
if (retries < 10)
ast_log(LOG_DEBUG, "Avoiding initial deadlock for '%s'\n", l->name);
ast_log(LOG_WARNING, "Avoided initial deadlock for '%s', %d retries!\n", l->name, retries);
ast_mutex_unlock(&chlock);
if (retries < 10) {
usleep(1);
retries++;
goto retry;
} else
return NULL;
}
}
ast_mutex_unlock(&chlock);
return l;
}
while(l) {
if (l == prev)
ret = l->next;
l = l->next;
}
Mark Spencer
committed
if (ret) {
if (ast_mutex_trylock(&ret->lock)) {
if (retries < 10)
ast_log(LOG_DEBUG, "Avoiding deadlock for '%s'\n", ret->name);
else
ast_log(LOG_WARNING, "Avoided deadlock for '%s', %d retries!\n", ret->name, retries);
ast_mutex_unlock(&chlock);
if (retries < 10) {
usleep(1);
retries++;
goto retry;
} else
return NULL;
}
}
ast_mutex_unlock(&chlock);
struct ast_channel *ast_get_channel_by_name_locked(char *channame)
{
struct ast_channel *chan;
chan = ast_channel_walk_locked(NULL);
while(chan) {
if (!strcasecmp(chan->name, channame))
return chan;
ast_mutex_unlock(&chan->lock);
}
return NULL;
}
int ast_safe_sleep_conditional( struct ast_channel *chan, int ms,
int (*cond)(void*), void *data )
{
struct ast_frame *f;
while(ms > 0) {
if( cond && ((*cond)(data) == 0 ) )
return 0;
ms = ast_waitfor(chan, ms);
if (ms <0)
return -1;
if (ms > 0) {
f = ast_read(chan);
if (!f)
return -1;
ast_frfree(f);
}
}
return 0;
}
int ast_safe_sleep(struct ast_channel *chan, int ms)
{
struct ast_frame *f;
while(ms > 0) {
ms = ast_waitfor(chan, ms);
if (ms <0)
return -1;
if (ms > 0) {
f = ast_read(chan);
if (!f)
return -1;
ast_frfree(f);
}
}
return 0;
}
static void free_cid(struct ast_callerid *cid)
{
if (cid->cid_dnid)
free(cid->cid_dnid);
if (cid->cid_num)
free(cid->cid_num);
if (cid->cid_name)
free(cid->cid_name);
if (cid->cid_ani)
free(cid->cid_ani);
if (cid->cid_rdnis)
free(cid->cid_rdnis);
}
void ast_channel_free(struct ast_channel *chan)
{
struct ast_channel *last=NULL, *cur;
char name[AST_CHANNEL_NAME];
ast_mutex_lock(&chlock);
cur = channels;
while(cur) {
if (cur == chan) {
if (last)
last->next = cur->next;
else
channels = cur->next;
break;
}
last = cur;
cur = cur->next;
}
if (!cur)
ast_log(LOG_WARNING, "Unable to find channel in list\n");
else {
/* Lock and unlock the channel just to be sure nobody
has it locked still */
ast_mutex_lock(&cur->lock);
ast_mutex_unlock(&cur->lock);
}
if (chan->tech_pvt) {
ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
free(chan->tech_pvt);
}
strncpy(name, chan->name, sizeof(name)-1);
/* Stop monitoring */
if (chan->monitor) {
chan->monitor->stop( chan, 0 );
}
Mark Spencer
committed
/* If there is native format music-on-hold state, free it */
if(chan->music_state)
ast_moh_cleanup(chan);
if (chan->readtrans)
ast_translator_free_path(chan->readtrans);
if (chan->writetrans)
ast_translator_free_path(chan->writetrans);
if (chan->pbx)
ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
ast_mutex_destroy(&chan->lock);
if ((fd = chan->alertpipe[0]) > -1)
if ((fd = chan->alertpipe[1]) > -1)
if ((fd = chan->timingfd) > -1)
close(fd);
f = chan->readq;
chan->readq = NULL;
while(f) {
fp = f;
f = f->next;
ast_frfree(fp);
}
/* loop over the variables list, freeing all data and deleting list items */
/* no need to lock the list, as the channel is already locked */
while (!AST_LIST_EMPTY(headp)) { /* List Deletion. */
vardata = AST_LIST_REMOVE_HEAD(headp, entries);
/* printf("deleting var %s=%s\n",ast_var_name(vardata),ast_var_value(vardata)); */
ast_mutex_unlock(&chlock);
ast_device_state_changed(name);
int ast_softhangup_nolock(struct ast_channel *chan, int cause)
if (option_debug)
ast_log(LOG_DEBUG, "Soft-Hanging up channel '%s'\n", chan->name);
/* Inform channel driver that we need to be hung up, if it cares */
Mark Spencer
committed
ast_queue_frame(chan, &f);
/* Interrupt any poll call or such */
if (ast_test_flag(chan, AST_FLAG_BLOCKING))
pthread_kill(chan->blocker, SIGURG);
return res;
}
int ast_softhangup(struct ast_channel *chan, int cause)
{
int res;
ast_mutex_lock(&chan->lock);
ast_mutex_unlock(&chan->lock);
static void free_translation(struct ast_channel *clone)
{
if (clone->writetrans)
ast_translator_free_path(clone->writetrans);
if (clone->readtrans)
ast_translator_free_path(clone->readtrans);
clone->writetrans = NULL;
clone->readtrans = NULL;
clone->rawwriteformat = clone->nativeformats;
clone->rawreadformat = clone->nativeformats;
int ast_hangup(struct ast_channel *chan)
{
int res = 0;
/* Don't actually hang up a channel that will masquerade as someone else, or
if someone is going to masquerade as us */
ast_mutex_lock(&chan->lock);
Mark Spencer
committed
if (ast_do_masquerade(chan))
ast_log(LOG_WARNING, "Failed to perform masquerade\n");
}
if (chan->masq) {
ast_log(LOG_WARNING, "%s getting hung up, but someone is trying to masq into us?!?\n", chan->name);
ast_mutex_unlock(&chan->lock);
/* If this channel is one which will be masqueraded into something,
mark it as a zombie already, so we know to free it later */
if (chan->masqr) {
if (chan->stream)
ast_closestream(chan->stream);
if (chan->vstream)
ast_closestream(chan->vstream);
if (chan->sched)
sched_context_destroy(chan->sched);
/* Clear any tone stuff remaining */
if (chan->generatordata)
chan->generator->release(chan, chan->generatordata);
chan->generatordata = NULL;
chan->generator = NULL;
if (chan->cdr) {
/* End the CDR if it hasn't already */
ast_cdr_end(chan->cdr);
/* Post and Free the CDR */
ast_cdr_post(chan->cdr);
ast_cdr_free(chan->cdr);
}
if (ast_test_flag(chan, AST_FLAG_BLOCKING)) {
ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
"is blocked by thread %ld in procedure %s! Expect a failure\n",
(long)pthread_self(), chan->name, (long)chan->blocker, chan->blockproc);
if (!ast_test_flag(chan, AST_FLAG_ZOMBIE)) {
if (option_debug)
ast_log(LOG_DEBUG, "Hanging up channel '%s'\n", chan->name);
if (chan->tech->hangup)
res = chan->tech->hangup(chan);
} else
if (option_debug)
ast_log(LOG_DEBUG, "Hanging up zombie '%s'\n", chan->name);
ast_mutex_unlock(&chan->lock);
"Uniqueid: %s\r\n"
"Cause: %i\r\n",
chan->name, chan->uniqueid, chan->hangupcause);
void ast_channel_unregister(const struct ast_channel_tech *tech)
ast_log(LOG_DEBUG, "Unregistering channel type '%s'\n", tech->type);
ast_mutex_lock(&chlock);
while (chan) {
if (chan->tech == tech) {
if (last)
last->next = chan->next;
else
backends = backends->next;
free(chan);
ast_mutex_unlock(&chlock);
if (option_verbose > 1)
ast_verbose( VERBOSE_PREFIX_2 "Unregistered channel type '%s'\n", tech->type);
return;
}
last = chan;
chan = chan->next;
}
ast_mutex_unlock(&chlock);
}
int ast_answer(struct ast_channel *chan)
{
/* Stop if we're a zombie or need a soft hangup */
if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
if (chan->tech->answer)
res = chan->tech->answer(chan);
void ast_deactivate_generator(struct ast_channel *chan)
if (chan->generator && chan->generator->release)
chan->generator->release(chan, chan->generatordata);
ast_clear_flag(chan, AST_FLAG_WRITE_INT);
ast_settimeout(chan, 0, NULL, NULL);
static int generator_force(void *data)
{
/* Called if generator doesn't have data */
void *tmp;
int res;
int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
struct ast_channel *chan = data;
tmp = chan->generatordata;
chan->generatordata = NULL;
generate = chan->generator->generate;
res = generate(chan, tmp, 0, 160);
chan->generatordata = tmp;
if (res) {
ast_log(LOG_DEBUG, "Auto-deactivating generator\n");
ast_deactivate_generator(chan);
}
return 0;
}
int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
{
int res = 0;
ast_mutex_lock(&chan->lock);
if (chan->generator && chan->generator->release)
chan->generator->release(chan, chan->generatordata);
if ((chan->generatordata = gen->alloc(chan, params))) {
ast_settimeout(chan, 160, generator_force, chan);
ast_mutex_unlock(&chan->lock);
return res;
int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
{
/* Wait for x amount of time on a file descriptor to have input. */
struct timeval start, now;
int spoint;
struct pollfd *pfds;
pfds = alloca(sizeof(struct pollfd) * n);
if (!pfds) {
ast_log(LOG_ERROR, "Out of memory\n");
return -1;
}
if (*ms > 0)
gettimeofday(&start, NULL);
y = 0;
pfds[y].fd = fds[x];
pfds[y].events = POLLIN | POLLPRI;
y++;
res = poll(pfds, y, *ms);
if (res < 0) {
/* Simulate a timeout if we were interrupted */
if (errno != EINTR)
*ms = -1;
else
*ms = 0;
return -1;
}
if (fds[x] > -1) {
if ((res = ast_fdisset(pfds, fds[x], y, &spoint))) {
winner = fds[x];
if (exception) {
if (res & POLLPRI)
*exception = -1;
else
*exception = 0;
}
}
if (*ms > 0) {
long passed;
gettimeofday(&now, NULL);
passed = (now.tv_sec - start.tv_sec) * 1000;
passed += (now.tv_usec - start.tv_usec) / 1000;
if (passed <= *ms)
*ms -= passed;
else
*ms = 0;
}
struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
int *exception, int *outfd, int *ms)
{
/* Wait for x amount of time on a file descriptor to have input. */
struct timeval start, end;
struct pollfd *pfds;
long rms;
int x, y, max;
int spoint;
time_t now = 0;
long whentohangup = 0, havewhen = 0, diff;
pfds = alloca(sizeof(struct pollfd) * (n * AST_MAX_FDS + nfds));
if (!pfds) {
ast_log(LOG_ERROR, "Out of memory\n");
*outfd = -1;
return NULL;
}
/* Perform any pending masquerades */
for (x=0;x<n;x++) {
ast_mutex_lock(&c[x]->lock);
if (!havewhen)
time(&now);
diff = c[x]->whentohangup - now;
if (!havewhen || (diff < whentohangup)) {
havewhen++;
whentohangup = diff;
}
}
Mark Spencer
committed
if (ast_do_masquerade(c[x])) {
ast_log(LOG_WARNING, "Masquerade failed\n");
*ms = -1;
ast_mutex_unlock(&c[x]->lock);
ast_mutex_unlock(&c[x]->lock);
if (havewhen) {
if ((*ms < 0) || (whentohangup * 1000 < *ms)) {
rms = whentohangup * 1000;
pfds[max].fd = c[x]->fds[y];
pfds[max].events = POLLIN | POLLPRI;
max++;
if (fds[x] > -1) {
pfds[max].fd = fds[x];
pfds[max].events = POLLIN | POLLPRI;
max++;
}
if (*ms > 0)
gettimeofday(&start, NULL);
res = poll(pfds, max, rms);
ast_clear_flag(c[x], AST_FLAG_BLOCKING);
/* Simulate a timeout if we were interrupted */
if (errno != EINTR)
*ms = -1;
if (havewhen)
time(&now);
ast_clear_flag(c[x], AST_FLAG_BLOCKING);
if (havewhen && c[x]->whentohangup && (now > c[x]->whentohangup)) {
c[x]->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
if (!winner)
winner = c[x];
}
for (y=0;y<AST_MAX_FDS;y++) {
if (c[x]->fds[y] > -1) {
if ((res = ast_fdisset(pfds, c[x]->fds[y], max, &spoint))) {
if (res & POLLPRI)
ast_clear_flag(c[x], AST_FLAG_EXCEPTION);
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
if (fds[x] > -1) {
if ((res = ast_fdisset(pfds, fds[x], max, &spoint))) {
if (outfd)
*outfd = fds[x];
if (exception) {
if (res & POLLPRI)
*exception = -1;
else
*exception = 0;
}
winner = NULL;
}
}
}
if (*ms > 0) {
long diff;
gettimeofday(&end, NULL);
diff = (end.tv_sec - start.tv_sec) * 1000;
diff += (end.tv_usec - start.tv_usec) / 1000;
if (diff < *ms)
*ms -= diff;
else
*ms = 0;
struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
{
return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
}
int ast_waitfor(struct ast_channel *c, int ms)
{
if (ms < 0) {
if (oldms < 0)
return 0;
else
return -1;
}
}
char ast_waitfordigit(struct ast_channel *c, int ms)
{
/* XXX Should I be merged with waitfordigit_full XXX */
/* Stop if we're a zombie or need a soft hangup */
if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
/* Wait for a digit, no more than ms milliseconds total. */
while(ms && !result) {
ms = ast_waitfor(c, ms);
if (ms < 0) /* Error */
result = -1;
else if (ms > 0) {
/* Read something */
f = ast_read(c);
if (f) {
if (f->frametype == AST_FRAME_DTMF)
result = f->subclass;
ast_frfree(f);
} else
result = -1;
}
}
return result;
}
int ast_settimeout(struct ast_channel *c, int samples, int (*func)(void *data), void *data)
{
int res = -1;
#ifdef ZAPTEL_OPTIMIZATIONS
if (c->timingfd > -1) {
if (!func) {
samples = 0;
data = 0;
}
ast_log(LOG_DEBUG, "Scheduling timer at %d sample intervals\n", samples);
res = ioctl(c->timingfd, ZT_TIMERCONFIG, &samples);
c->timingfunc = func;
c->timingdata = data;
}
#endif
return res;
}
char ast_waitfordigit_full(struct ast_channel *c, int ms, int audiofd, int cmdfd)
{
struct ast_frame *f;
struct ast_channel *rchan;
int outfd;
/* Stop if we're a zombie or need a soft hangup */
if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
return -1;
/* Wait for a digit, no more than ms milliseconds total. */
while(ms) {
rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
if ((!rchan) && (outfd < 0) && (ms)) {
if (errno == EINTR)
continue;
ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
return -1;
} else if (outfd > -1) {
/* The FD we were watching has something waiting */
return 1;
} else if (rchan) {
f = ast_read(c);
if(!f) {
return -1;
}
switch(f->frametype) {
case AST_FRAME_DTMF:
res = f->subclass;
return res;
case AST_FRAME_CONTROL:
switch(f->subclass) {
case AST_CONTROL_HANGUP:
ast_frfree(f);
return -1;
case AST_CONTROL_RINGING:
case AST_CONTROL_ANSWER:
/* Unimportant */
break;
default:
ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass);
}
case AST_FRAME_VOICE:
/* Write audio if appropriate */
if (audiofd > -1)
write(audiofd, f->data, f->datalen);
}
/* Ignore */
ast_frfree(f);
return 0; /* Time is up */
struct ast_frame *ast_read(struct ast_channel *chan)
{
struct ast_frame *f = NULL;
#ifdef ZAPTEL_OPTIMIZATIONS
int (*func)(void *);
void *data;
int res;
static struct ast_frame null_frame =
{
AST_FRAME_NULL,
};
ast_mutex_lock(&chan->lock);
Mark Spencer
committed
if (ast_do_masquerade(chan)) {
ast_log(LOG_WARNING, "Failed to perform masquerade\n");
f = NULL;
} else
ast_mutex_unlock(&chan->lock);
return f;
}
/* Stop if we're a zombie or need a soft hangup */
if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
ast_mutex_unlock(&chan->lock);
if (!ast_test_flag(chan, AST_FLAG_DEFER_DTMF) && !ast_strlen_zero(chan->dtmfq)) {
/* We have DTMF that has been deferred. Return it now */
chan->dtmff.frametype = AST_FRAME_DTMF;
chan->dtmff.subclass = chan->dtmfq[0];
/* Drop first digit */
memmove(chan->dtmfq, chan->dtmfq + 1, sizeof(chan->dtmfq) - 1);
ast_mutex_unlock(&chan->lock);
/* Read and ignore anything on the alertpipe, but read only
one sizeof(blah) per frame that we send from it */
if (chan->alertpipe[0] > -1) {
read(chan->alertpipe[0], &blah, sizeof(blah));
if ((chan->timingfd > -1) && (chan->fdno == AST_MAX_FDS - 2) && ast_test_flag(chan, AST_FLAG_EXCEPTION)) {
ast_clear_flag(chan, AST_FLAG_EXCEPTION);
/* IF we can't get event, assume it's an expired as-per the old interface */
res = ioctl(chan->timingfd, ZT_GETEVENT, &blah);
if (res)
blah = ZT_EVENT_TIMER_EXPIRED;
if (blah == ZT_EVENT_TIMER_PING) {
ast_log(LOG_NOTICE, "Oooh, there's a PING!\n");
if (!chan->readq || !chan->readq->next) {
/* Acknowledge PONG unless we need it again */
#if 0
ast_log(LOG_NOTICE, "Sending a PONG!\n");
#endif
if (ioctl(chan->timingfd, ZT_TIMERPONG, &blah)) {
ast_log(LOG_WARNING, "Failed to pong timer on '%s': %s\n", chan->name, strerror(errno));
}
}
} else if (blah == ZT_EVENT_TIMER_EXPIRED) {
ioctl(chan->timingfd, ZT_TIMERACK, &blah);
func = chan->timingfunc;
data = chan->timingdata;
ast_mutex_unlock(&chan->lock);
if (func) {
#if 0
ast_log(LOG_DEBUG, "Calling private function\n");
#endif
func(data);
} else {
blah = 0;
ast_mutex_lock(&chan->lock);
ioctl(chan->timingfd, ZT_TIMERCONFIG, &blah);
chan->timingdata = NULL;
ast_mutex_unlock(&chan->lock);
}
return f;
} else
ast_log(LOG_NOTICE, "No/unknown event '%d' on timer for '%s'?\n", blah, chan->name);
if (chan->readq) {
f = chan->readq;
chan->readq = f->next;
if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) {
ast_frfree(f);
if (ast_test_flag(chan, AST_FLAG_EXCEPTION)) {
if (chan->tech->exception)
f = chan->tech->exception(chan);
else {
ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", chan->name);
f = &null_frame;
}
ast_clear_flag(chan, AST_FLAG_EXCEPTION);
if (chan->tech->read)
f = chan->tech->read(chan);
ast_log(LOG_WARNING, "No read routine on channel %s\n", chan->name);
}
if (!(f->subclass & chan->nativeformats)) {
/* This frame can't be from the current native formats -- drop it on the
floor */
ast_log(LOG_NOTICE, "Dropping incompatible voice frame on %s of format %s since our native format has changed to %s\n", chan->name, ast_getformatname(f->subclass), ast_getformatname(chan->nativeformats));
} else {
if (chan->monitor && chan->monitor->read_stream ) {
#ifndef MONITOR_CONSTANT_DELAY
int jump = chan->outsmpl - chan->insmpl - 2 * f->samples;
if (jump >= 0) {
if (ast_seekstream(chan->monitor->read_stream, jump + f->samples, SEEK_FORCECUR) == -1)
ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
chan->insmpl += jump + 2 * f->samples;
} else
chan->insmpl+= f->samples;
#else
int jump = chan->outsmpl - chan->insmpl;
if (jump - MONITOR_DELAY >= 0) {
if (ast_seekstream(chan->monitor->read_stream, jump - f->samples, SEEK_FORCECUR) == -1)
ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
chan->insmpl += jump;
} else
chan->insmpl += f->samples;
#endif
if (ast_writestream(chan->monitor->read_stream, f) < 0)
ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n");
}
if (chan->readtrans) {
f = ast_translate(chan->readtrans, f, 1);
if (!f)
f = &null_frame;
}
/* Make sure we always return NULL in the future */
chan->_softhangup |= AST_SOFTHANGUP_DEV;
if (chan->generator)
/* End the CDR if appropriate */
if (chan->cdr)
ast_cdr_end(chan->cdr);
} else if (ast_test_flag(chan, AST_FLAG_DEFER_DTMF) && f->frametype == AST_FRAME_DTMF) {
if (strlen(chan->dtmfq) < sizeof(chan->dtmfq) - 2)
chan->dtmfq[strlen(chan->dtmfq)] = f->subclass;
else
ast_log(LOG_WARNING, "Dropping deferred DTMF digits on %s\n", chan->name);
f = &null_frame;
} else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
if (chan->_state == AST_STATE_UP) {
ast_log(LOG_DEBUG, "Dropping duplicate answer!\n");
f = &null_frame;
}
Mark Spencer
committed
if (f && (f->frametype == AST_FRAME_VOICE) && chan->generatordata) {
/* Mask generator data temporarily and apply. If there is a timing function, it
will be calling the generator instead */
int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
Mark Spencer
committed
if (chan->timingfunc) {
ast_log(LOG_DEBUG, "Generator got voice, switching to phase locked mode\n");
ast_settimeout(chan, 0, NULL, NULL);
}
tmp = chan->generatordata;
chan->generatordata = NULL;
generate = chan->generator->generate;
res = generate(chan, tmp, f->datalen, f->samples);
chan->generatordata = tmp;
if (res) {
ast_log(LOG_DEBUG, "Auto-deactivating generator\n");
Mark Spencer
committed
} else if (f && (f->frametype == AST_FRAME_CNG)) {
if (chan->generator && !chan->timingfunc && (chan->timingfd > -1)) {
ast_log(LOG_DEBUG, "Generator got CNG, switching to zap timed mode\n");
ast_settimeout(chan, 160, generator_force, chan);
}
if (chan->fin & 0x80000000)
ast_frame_dump(chan->name, f, "<<");
if ((chan->fin & 0x7fffffff) == 0x7fffffff)
chan->fin &= 0x80000000;
else
chan->fin++;
int ast_indicate(struct ast_channel *chan, int condition)
{
int res = -1;
/* Stop if we're a zombie or need a soft hangup */
if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan))
if (chan->tech->indicate)
res = chan->tech->indicate(chan, condition);
ast_mutex_unlock(&chan->lock);
if (!chan->tech->indicate || res) {
/*
* Device does not support (that) indication, lets fake
* it by doing our own tone generation. (PM2002)
*/
if (condition >= 0) {
const struct tone_zone_sound *ts = NULL;
switch (condition) {
case AST_CONTROL_RINGING:
ts = ast_get_indication_tone(chan->zone, "ring");
break;
case AST_CONTROL_BUSY:
ts = ast_get_indication_tone(chan->zone, "busy");
break;
case AST_CONTROL_CONGESTION:
ts = ast_get_indication_tone(chan->zone, "congestion");
break;
}
if (ts && ts->data[0]) {
ast_log(LOG_DEBUG, "Driver for channel '%s' does not support indication %d, emulating it\n", chan->name, condition);
ast_playtones_start(chan,0,ts->data, 1);
} else if (condition == AST_CONTROL_PROGRESS) {
/* ast_playtones_stop(chan); */
} else if (condition == AST_CONTROL_PROCEEDING) {
/* Do nothing, really */
} else if (condition == AST_CONTROL_HOLD) {
/* Do nothing.... */
} else if (condition == AST_CONTROL_UNHOLD) {
/* Do nothing.... */
/* not handled */
ast_log(LOG_WARNING, "Unable to handle indication %d for '%s'\n", condition, chan->name);
}
}
else ast_playtones_stop(chan);
}
int ast_recvchar(struct ast_channel *chan, int timeout)
{
int res,ourto,c;
struct ast_frame *f;
ourto = timeout;
for(;;)
{
if (ast_check_hangup(chan)) return -1;
res = ast_waitfor(chan,ourto);
if (res <= 0) /* if timeout */
{
return 0;
}
ourto = res;
f = ast_read(chan);
if (f == NULL) return -1; /* if hangup */
if ((f->frametype == AST_FRAME_CONTROL) &&
(f->subclass == AST_CONTROL_HANGUP)) return -1; /* if hangup */
if (f->frametype == AST_FRAME_TEXT) /* if a text frame */
ast_frfree(f);
return(c);
}
ast_frfree(f);
}
}
int ast_sendtext(struct ast_channel *chan, char *text)
{
int res = 0;
/* Stop if we're a zombie or need a soft hangup */
if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan))
if (chan->tech->send_text)
res = chan->tech->send_text(chan, text);
ast_clear_flag(chan, AST_FLAG_BLOCKING);
static int do_senddigit(struct ast_channel *chan, char digit)
{
int res = -1;
if (chan->tech->send_digit)
res = chan->tech->send_digit(chan, digit);
if (!chan->tech->send_digit || res) {
/*
* Device does not support DTMF tones, lets fake
* it by doing our own generation. (PM2002)
*/
static const char* dtmf_tones[] = {
"!941+1336/100,!0/100", /* 0 */
"!697+1209/100,!0/100", /* 1 */
"!697+1336/100,!0/100", /* 2 */
"!697+1477/100,!0/100", /* 3 */
"!770+1209/100,!0/100", /* 4 */
"!770+1336/100,!0/100", /* 5 */
"!770+1477/100,!0/100", /* 6 */
"!852+1209/100,!0/100", /* 7 */
"!852+1336/100,!0/100", /* 8 */
"!852+1477/100,!0/100", /* 9 */
"!697+1633/100,!0/100", /* A */
"!770+1633/100,!0/100", /* B */
"!852+1633/100,!0/100", /* C */
"!941+1633/100,!0/100", /* D */
"!941+1209/100,!0/100", /* * */
"!941+1477/100,!0/100" }; /* # */
ast_playtones_start(chan,0,dtmf_tones[digit-'0'], 0);
ast_playtones_start(chan,0,dtmf_tones[digit-'A'+10], 0);
ast_playtones_start(chan,0,dtmf_tones[14], 0);
ast_playtones_start(chan,0,dtmf_tones[15], 0);
ast_log(LOG_DEBUG, "Unable to handle DTMF tone '%c' for '%s'\n", digit, chan->name);
int ast_senddigit(struct ast_channel *chan, char digit)
{
int ast_prod(struct ast_channel *chan)
{
struct ast_frame a = { AST_FRAME_VOICE };
char nothing[128];
/* Send an empty audio frame to get things moving */
if (chan->_state != AST_STATE_UP) {
ast_log(LOG_DEBUG, "Prodding channel '%s'\n", chan->name);
a.subclass = chan->rawwriteformat;
a.data = nothing + AST_FRIENDLY_OFFSET;
if (ast_write(chan, &a))
ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", chan->name);
int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
{
int res;
if (!chan->tech->write_video)
return 0;
res = ast_write(chan, fr);
if (!res)
res = 1;
return res;
}
int ast_write(struct ast_channel *chan, struct ast_frame *fr)
{
int res = -1;
/* Stop if we're a zombie or need a soft hangup */
ast_mutex_lock(&chan->lock);
if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
ast_mutex_unlock(&chan->lock);
/* Handle any pending masquerades */
if (chan->masq) {
Mark Spencer
committed
if (ast_do_masquerade(chan)) {
ast_log(LOG_WARNING, "Failed to perform masquerade\n");
ast_mutex_unlock(&chan->lock);
ast_mutex_unlock(&chan->lock);
if (ast_test_flag(chan, AST_FLAG_WRITE_INT))
ast_mutex_unlock(&chan->lock);
if (chan->fout & 0x80000000)
ast_frame_dump(chan->name, fr, ">>");
CHECK_BLOCKING(chan);
switch(fr->frametype) {
case AST_FRAME_CONTROL:
/* XXX Interpret control frames XXX */
ast_log(LOG_WARNING, "Don't know how to handle control frames yet\n");
break;
case AST_FRAME_DTMF:
ast_clear_flag(chan, AST_FLAG_BLOCKING);
if (chan->tech->send_text)
res = chan->tech->send_text(chan, (char *) fr->data);
else
res = 0;
break;
case AST_FRAME_HTML:
if (chan->tech->send_html)
res = chan->tech->send_html(chan, fr->subclass, (char *) fr->data, fr->datalen);
case AST_FRAME_VIDEO:
/* XXX Handle translation of video codecs one day XXX */
if (chan->tech->write_video)
res = chan->tech->write_video(chan, fr);
else
res = 0;
break;
if (chan->tech->write) {
if (chan->writetrans) {
f = ast_translate(chan->writetrans, fr, 0);
res = chan->tech->write(chan, f);
if( chan->monitor &&
chan->monitor->write_stream &&
f && ( f->frametype == AST_FRAME_VOICE ) ) {
#ifndef MONITOR_CONSTANT_DELAY
int jump = chan->insmpl - chan->outsmpl - 2 * f->samples;
if (jump >= 0) {
if (ast_seekstream(chan->monitor->write_stream, jump + f->samples, SEEK_FORCECUR) == -1)
ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
chan->outsmpl += jump + 2 * f->samples;
} else
chan->outsmpl += f->samples;
#else
int jump = chan->insmpl - chan->outsmpl;
if (jump - MONITOR_DELAY >= 0) {
if (ast_seekstream(chan->monitor->write_stream, jump - f->samples, SEEK_FORCECUR) == -1)
ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
chan->outsmpl += jump;
} else
chan->outsmpl += f->samples;
#endif
if (ast_writestream(chan->monitor->write_stream, f) < 0)
ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n");
}
if (f && (f != fr))
ast_frfree(f);
ast_clear_flag(chan, AST_FLAG_BLOCKING);
/* Consider a write failure to force a soft hangup */
if (res < 0)
else {
if ((chan->fout & 0x7fffffff) == 0x7fffffff)
chan->fout &= 0x80000000;
else
chan->fout++;
ast_mutex_unlock(&chan->lock);
Mark Spencer
committed
int ast_set_write_format(struct ast_channel *chan, int fmts)
{
int fmt;
int native;
int res;
Mark Spencer
committed
ast_mutex_lock(&chan->lock);
native = chan->nativeformats;
fmt = fmts;
res = ast_translator_best_choice(&native, &fmt);
if (res < 0) {
ast_log(LOG_NOTICE, "Unable to find a path from %s to %s\n",
ast_getformatname(fmts), ast_getformatname(chan->nativeformats));
Mark Spencer
committed
ast_mutex_unlock(&chan->lock);
/* Now we have a good choice for both. We'll write using our native format. */
chan->rawwriteformat = native;
/* User perspective is fmt */
chan->writeformat = fmt;
/* Free any write translation we have right now */
if (chan->writetrans)
ast_translator_free_path(chan->writetrans);
/* Build a translation path from the user write format to the raw writing format */
chan->writetrans = ast_translator_build_path(chan->rawwriteformat, chan->writeformat);
ast_log(LOG_DEBUG, "Set channel %s to write format %s\n", chan->name, ast_getformatname(chan->writeformat));
Mark Spencer
committed
ast_mutex_unlock(&chan->lock);
Mark Spencer
committed
int ast_set_read_format(struct ast_channel *chan, int fmts)
{
int fmt;
int native;
int res;
Mark Spencer
committed
ast_mutex_lock(&chan->lock);
native = chan->nativeformats;
fmt = fmts;
/* Find a translation path from the native read format to one of the user's read formats */
res = ast_translator_best_choice(&fmt, &native);
if (res < 0) {
ast_log(LOG_NOTICE, "Unable to find a path from %s to %s\n",
ast_getformatname(chan->nativeformats), ast_getformatname(fmts));
Mark Spencer
committed
ast_mutex_unlock(&chan->lock);
/* Now we have a good choice for both. We'll write using our native format. */
chan->rawreadformat = native;
/* User perspective is fmt */
chan->readformat = fmt;
/* Free any read translation we have right now */
if (chan->readtrans)
ast_translator_free_path(chan->readtrans);
/* Build a translation path from the raw read format to the user reading format */
chan->readtrans = ast_translator_build_path(chan->readformat, chan->rawreadformat);
ast_log(LOG_DEBUG, "Set channel %s to read format %s\n",
chan->name, ast_getformatname(chan->readformat));
Mark Spencer
committed
ast_mutex_unlock(&chan->lock);
struct ast_channel *__ast_request_and_dial(const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, struct outgoing_helper *oh)
Mark Spencer
committed
int cause = 0;
struct ast_channel *chan;
struct ast_frame *f;
Mark Spencer
committed
chan = ast_request(type, format, data, &cause);
Martin Pycko
committed
if (oh) {
char *tmp, *var;
/* JDG chanvar */
if (oh->variable)
variable = ast_strdupa(oh->variable);
else
variable = NULL;
tmp = variable;
/* FIXME replace this call with strsep NOT*/
Martin Pycko
committed
while( (var = strtok_r(NULL, "|", &tmp)) ) {
pbx_builtin_setvar( chan, var );
} /* /JDG */
ast_set_callerid(chan, oh->cid_num, oh->cid_name, oh->cid_num);
Martin Pycko
committed
if (oh->account && *oh->account)
ast_cdr_setaccount(chan, oh->account);
Martin Pycko
committed
}
ast_set_callerid(chan, cid_num, cid_name, cid_num);
Martin Pycko
committed
if (!ast_call(chan, data, 0)) {
while(timeout && (chan->_state != AST_STATE_UP)) {
res = ast_waitfor(chan, timeout);
if (res < 0) {
/* Something not cool, or timed out */
break;
}
/* If done, break out */
if (!res)
break;
if (timeout > -1)
timeout = res;
f = ast_read(chan);
if (!f) {
state = AST_CONTROL_HANGUP;
break;
}
if (f->frametype == AST_FRAME_CONTROL) {
if (f->subclass == AST_CONTROL_RINGING)
state = AST_CONTROL_RINGING;
else if ((f->subclass == AST_CONTROL_BUSY) || (f->subclass == AST_CONTROL_CONGESTION)) {
state = f->subclass;
break;
} else if (f->subclass == AST_CONTROL_ANSWER) {
state = f->subclass;
} else if (f->subclass == AST_CONTROL_PROGRESS) {
/* Ignore */
} else if (f->subclass == -1) {
/* Ignore -- just stopping indications */
} else {
ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass);
}
}
ast_frfree(f);
}
Mark Spencer
committed
ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
} else {
ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
Mark Spencer
committed
switch(cause) {
case AST_CAUSE_BUSY:
state = AST_CONTROL_BUSY;
break;
case AST_CAUSE_CONGESTION:
state = AST_CONTROL_CONGESTION;
break;
}
}
if (chan) {
/* Final fixups */
if (oh) {
if (oh->context && *oh->context)
strncpy(chan->context, oh->context, sizeof(chan->context) - 1);
if (oh->exten && *oh->exten)
strncpy(chan->exten, oh->exten, sizeof(chan->exten) - 1);
chan->priority = oh->priority;
}
if (chan->_state == AST_STATE_UP)
state = AST_CONTROL_ANSWER;
}
if (chan && res <= 0) {
if (!chan->cdr) {
chan->cdr = ast_cdr_alloc();
if (chan->cdr)
ast_cdr_init(chan->cdr, chan);
}
if (chan->cdr) {
char tmp[256];
snprintf(tmp, 256, "%s/%s", type, (char *)data);
ast_cdr_setapp(chan->cdr,"Dial",tmp);
ast_cdr_update(chan);
ast_cdr_start(chan->cdr);
ast_cdr_end(chan->cdr);
/* If the cause wasn't handled properly */
if (ast_cdr_disposition(chan->cdr,chan->hangupcause))
ast_cdr_failed(chan->cdr);
} else
ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
ast_hangup(chan);
chan = NULL;
}
struct ast_channel *ast_request_and_dial(const char *type, int format, void *data, int timeout, int *outstate, const char *cidnum, const char *cidname)
Martin Pycko
committed
{
return __ast_request_and_dial(type, format, data, timeout, outstate, cidnum, cidname, NULL);
Martin Pycko
committed
}
Mark Spencer
committed
struct ast_channel *ast_request(const char *type, int format, void *data, int *cause)
{
struct chanlist *chan;
struct ast_channel *c = NULL;
Mark Spencer
committed
int foo;
if (!cause)
cause = &foo;
*cause = AST_CAUSE_NOTDEFINED;
if (ast_mutex_lock(&chlock)) {
ast_log(LOG_WARNING, "Unable to lock channel list\n");
return NULL;
}
chan = backends;
while(chan) {
if (!strcasecmp(type, chan->tech->type)) {
capabilities = chan->tech->capabilities;
fmt = format;
res = ast_translator_best_choice(&fmt, &capabilities);
if (res < 0) {
ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %d) to %d\n", type, chan->tech->capabilities, format);
ast_mutex_unlock(&chlock);
ast_mutex_unlock(&chlock);
if (chan->tech->requester)
c = chan->tech->requester(type, capabilities, data, cause);
if (c->_state == AST_STATE_DOWN) {
manager_event(EVENT_FLAG_CALL, "Newchannel",
"Channel: %s\r\n"
"State: %s\r\n"
"CallerID: %s\r\n"
"CallerIDName: %s\r\n"
c->name, ast_state2str(c->_state), c->cid.cid_num ? c->cid.cid_num : "<unknown>", c->cid.cid_name ? c->cid.cid_name : "<unknown>",c->uniqueid);
Mark Spencer
committed
if (!chan) {
ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
Mark Spencer
committed
*cause = AST_CAUSE_NOSUCHDRIVER;
}
ast_mutex_unlock(&chlock);
int ast_parse_device_state(char *device)
{
char name[AST_CHANNEL_NAME] = "";
char *cut;
struct ast_channel *chan;
chan = ast_channel_walk_locked(NULL);
while (chan) {
strncpy(name, chan->name, sizeof(name)-1);
ast_mutex_unlock(&chan->lock);
cut = strchr(name,'-');
if (cut)
if (!strcmp(name, device))
chan = ast_channel_walk_locked(chan);
}
return AST_DEVICE_UNKNOWN;
}
int ast_device_state(char *device)
{
char tech[AST_MAX_EXTENSION] = "";
char *number;
struct chanlist *chanls;
int res = 0;
strncpy(tech, device, sizeof(tech)-1);
number = strchr(tech, '/');
if (!number) {
}
*number = 0;
number++;
if (ast_mutex_lock(&chlock)) {
ast_log(LOG_WARNING, "Unable to lock channel list\n");
return -1;
}
chanls = backends;
while(chanls) {
if (!strcasecmp(tech, chanls->tech->type)) {
ast_mutex_unlock(&chlock);
if (!chanls->tech->devicestate)
return ast_parse_device_state(device);
else {
res = chanls->tech->devicestate(number);
if (res == AST_DEVICE_UNKNOWN)
return ast_parse_device_state(device);
else
return res;
}
}
chanls = chanls->next;
}
ast_mutex_unlock(&chlock);
return AST_DEVICE_INVALID;
}
int ast_call(struct ast_channel *chan, char *addr, int timeout)
{
/* Place an outgoing call, but don't wait any longer than timeout ms before returning.
If the remote end does not answer within the timeout, then do NOT hang up, but
return anyway. */
int res = -1;
/* Stop if we're a zombie or need a soft hangup */
ast_mutex_lock(&chan->lock);
if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan))
if (chan->tech->call)
res = chan->tech->call(chan, addr, timeout);
ast_mutex_unlock(&chan->lock);
int ast_transfer(struct ast_channel *chan, char *dest)
{
/* Place an outgoing call, but don't wait any longer than timeout ms before returning.
If the remote end does not answer within the timeout, then do NOT hang up, but
return anyway. */
int res = -1;
/* Stop if we're a zombie or need a soft hangup */
ast_mutex_lock(&chan->lock);
if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) {
if (chan->tech->transfer) {
res = chan->tech->transfer(chan, dest);
if (!res)
res = 1;
} else
res = 0;
}
ast_mutex_unlock(&chan->lock);
int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
{
int pos=0;
int to = ftimeout;
char d;
/* XXX Merge with full version? XXX */
/* Stop if we're a zombie or need a soft hangup */
if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
d = ast_waitstream(c, AST_DIGIT_ANY);
ast_stopstream(c);
if (!d)
d = ast_waitfordigit(c, to);
} else {
d = ast_waitfordigit(c, to);
}
if (d < 0)
return -1;
if (d == 0) {
s[pos]='\0';
return 1;
}
return 0;
}
to = timeout;
} while(1);
/* Never reached */
return 0;
}
int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
{
int pos=0;
int to = ftimeout;
char d;
/* Stop if we're a zombie or need a soft hangup */
if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
return -1;
if (!len)
return -1;
do {
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
ast_stopstream(c);
usleep(1000);
if (!d)
d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
} else {
d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
}
if (d < 0)
return -1;
if (d == 0) {
s[pos]='\0';
return 1;
}
if (d == 1) {
s[pos]='\0';
return 2;
}
if (!strchr(enders, d))
s[pos++] = d;
if (strchr(enders, d) || (pos >= len)) {
s[pos]='\0';
return 0;
}
to = timeout;
} while(1);
/* Never reached */
return 0;
}
int ast_channel_supports_html(struct ast_channel *chan)
{
if (chan->tech->send_html)
return 1;
return 0;
}
int ast_channel_sendhtml(struct ast_channel *chan, int subclass, char *data, int datalen)
{
if (chan->tech->send_html)
return chan->tech->send_html(chan, subclass, data, datalen);
return -1;
}
int ast_channel_sendurl(struct ast_channel *chan, char *url)
{
if (chan->tech->send_html)
return chan->tech->send_html(chan, AST_HTML_URL, url, strlen(url) + 1);
int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
{
int peerf;
int chanf;
int res;
ast_mutex_lock(&peer->lock);
ast_mutex_unlock(&peer->lock);
ast_mutex_lock(&chan->lock);
ast_mutex_unlock(&chan->lock);
res = ast_translator_best_choice(&peerf, &chanf);
if (res < 0) {
ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", chan->name, chan->nativeformats, peer->name, peer->nativeformats);
return -1;
}
/* Set read format on channel */
Mark Spencer
committed
res = ast_set_read_format(chan, peerf);
ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", chan->name, chanf);
return -1;
}
/* Set write format on peer channel */
Mark Spencer
committed
res = ast_set_write_format(peer, peerf);
ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", peer->name, peerf);
return -1;
}
/* Now we go the other way */
peerf = peer->nativeformats;
chanf = chan->nativeformats;
res = ast_translator_best_choice(&chanf, &peerf);
if (res < 0) {
ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", peer->name, peer->nativeformats, chan->name, chan->nativeformats);
return -1;
}
/* Set writeformat on channel */
Mark Spencer
committed
res = ast_set_write_format(chan, chanf);
ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", chan->name, chanf);
Mark Spencer
committed
res = ast_set_read_format(peer, chanf);
ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", peer->name, peerf);
int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone)
{
struct ast_frame null = { AST_FRAME_NULL, };
int res = -1;
if (original == clone) {
ast_log(LOG_WARNING, "Can't masquerade channel '%s' into itself!\n", original->name);
return -1;
}
ast_mutex_lock(&original->lock);
while(ast_mutex_trylock(&clone->lock)) {
ast_mutex_unlock(&original->lock);
usleep(1);
ast_mutex_lock(&original->lock);
}
ast_log(LOG_DEBUG, "Planning to masquerade %s into the structure of %s\n",
clone->name, original->name);
if (original->masq) {
ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
original->masq->name, original->name);
} else if (clone->masqr) {
ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
clone->name, clone->masqr->name);
} else {
original->masq = clone;
clone->masqr = original;
ast_queue_frame(original, &null);
ast_queue_frame(clone, &null);
ast_log(LOG_DEBUG, "Done planning to masquerade %s into the structure of %s\n", original->name, clone->name);
res = 0;
}
ast_mutex_unlock(&clone->lock);
ast_mutex_unlock(&original->lock);
return res;
void ast_change_name(struct ast_channel *chan, char *newname)
{
char tmp[256];
strncpy(tmp, chan->name, sizeof(tmp) - 1);
strncpy(chan->name, newname, sizeof(chan->name) - 1);
manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", tmp, chan->name, chan->uniqueid);
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child)
{
struct ast_var_t *current, *newvar;
char *varname;
AST_LIST_TRAVERSE(&parent->varshead, current, entries) {
int vartype = 0;
varname = ast_var_full_name(current);
if (!varname)
continue;
if (varname[0] == '_') {
vartype = 1;
if (varname[1] == '_')
vartype = 2;
}
switch (vartype) {
case 1:
newvar = ast_var_assign(&varname[1], ast_var_value(current));
if (newvar) {
AST_LIST_INSERT_HEAD(&child->varshead, newvar, entries);
if (option_debug)
ast_log(LOG_DEBUG, "Copying soft-transferable variable %s.\n", ast_var_name(newvar));
}
break;
case 2:
newvar = ast_var_assign(ast_var_full_name(current), ast_var_value(current));
if (newvar) {
AST_LIST_INSERT_HEAD(&child->varshead, newvar, entries);
if (option_debug)
ast_log(LOG_DEBUG, "Copying hard-transferable variable %s.\n", ast_var_name(newvar));
}
break;
default:
if (option_debug)
ast_log(LOG_DEBUG, "Not copying variable %s.\n", ast_var_name(current));
break;
}
}
}
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
/* Clone channel variables from 'clone' channel into 'original' channel
All variables except those related to app_groupcount are cloned
Variables are actually _removed_ from 'clone' channel, presumably
because it will subsequently be destroyed.
Assumes locks will be in place on both channels when called.
*/
static void clone_variables(struct ast_channel *original, struct ast_channel *clone)
{
struct ast_var_t *varptr;
/* we need to remove all app_groupcount related variables from the original
channel before merging in the clone's variables; any groups assigned to the
original channel should be released, only those assigned to the clone
should remain
*/
AST_LIST_TRAVERSE_SAFE_BEGIN(&original->varshead, varptr, entries) {
if (!strncmp(ast_var_name(varptr), GROUP_CATEGORY_PREFIX, strlen(GROUP_CATEGORY_PREFIX))) {
AST_LIST_REMOVE(&original->varshead, varptr, entries);
ast_var_delete(varptr);
}
}
AST_LIST_TRAVERSE_SAFE_END;
/* Append variables from clone channel into original channel */
/* XXX Is this always correct? We have to in order to keep MACROS working XXX */
if (AST_LIST_FIRST(&clone->varshead))
AST_LIST_INSERT_TAIL(&original->varshead, AST_LIST_FIRST(&clone->varshead), entries);
}
/* Assumes channel will be locked when called */
Mark Spencer
committed
int ast_do_masquerade(struct ast_channel *original)
const struct ast_channel_tech *t;
void *t_pvt;
struct ast_callerid tmpcid;
int rformat = original->readformat;
int wformat = original->writeformat;
char newn[100];
char orig[100];
char masqn[100];
char zombn[100];
ast_log(LOG_DEBUG, "Actually Masquerading %s(%d) into the structure of %s(%d)\n",
clone->name, clone->_state, original->name, original->_state);
/* XXX This is a seriously wacked out operation. We're essentially putting the guts of
the clone channel into the original channel. Start by killing off the original
channel's backend. I'm not sure we're going to keep this function, because
while the features are nice, the cost is very high in terms of pure nastiness. XXX */
Mark Spencer
committed
/* We need the clone's lock, too */
ast_mutex_lock(&clone->lock);
Mark Spencer
committed
ast_log(LOG_DEBUG, "Got clone lock on '%s' at %p\n", clone->name, &clone->lock);
/* Having remembered the original read/write formats, we turn off any translation on either
one */
free_translation(clone);
free_translation(original);
/* Unlink the masquerade */
original->masq = NULL;
clone->masqr = NULL;
/* Save the original name */
strncpy(orig, original->name, sizeof(orig) - 1);
/* Save the new name */
strncpy(newn, clone->name, sizeof(newn) - 1);
/* Create the masq name */
snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn);
strncpy(original->name, newn, sizeof(original->name)-1);
strncpy(clone->name, masqn, sizeof(clone->name) - 1);
/* Notify any managers of the change, first the masq then the other */
manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", newn, masqn, clone->uniqueid);
manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", orig, newn, original->uniqueid);
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
/* Swap the technlogies */
t = original->tech;
original->tech = clone->tech;
clone->tech = t;
t_pvt = original->tech_pvt;
original->tech_pvt = clone->tech_pvt;
clone->tech_pvt = t_pvt;
/* Swap the readq's */
cur = original->readq;
original->readq = clone->readq;
clone->readq = cur;
/* Swap the alertpipes */
for (i = 0; i < 2; i++) {
x = original->alertpipe[i];
original->alertpipe[i] = clone->alertpipe[i];
clone->alertpipe[i] = x;
}
/* Swap the raw formats */
x = original->rawreadformat;
original->rawreadformat = clone->rawreadformat;
clone->rawreadformat = x;
x = original->rawwriteformat;
original->rawwriteformat = clone->rawwriteformat;
clone->rawwriteformat = x;
/* Save any pending frames on both sides. Start by counting
* how many we're going to need... */
prev = NULL;
cur = clone->readq;
x = 0;
while(cur) {
x++;
prev = cur;
cur = cur->next;
}
/* If we had any, prepend them to the ones already in the queue, and
* load up the alertpipe */
if (prev) {
prev->next = original->readq;
original->readq = clone->readq;
clone->readq = NULL;
if (original->alertpipe[1] > -1) {
write(original->alertpipe[1], &x, sizeof(x));
/* And of course, so does our current state. Note we need not
call ast_setstate since the event manager doesn't really consider
these separate. We do this early so that the clone has the proper
state of the original channel. */
origstate = original->_state;
original->_state = clone->_state;
clone->_state = origstate;
if (clone->tech->fixup){
res = clone->tech->fixup(original, clone);
if (res)
ast_log(LOG_WARNING, "Fixup failed on channel %s, strange things may happen.\n", clone->name);
}
/* Start by disconnecting the original's physical side */
if (clone->tech->hangup)
res = clone->tech->hangup(clone);
if (res) {
ast_log(LOG_WARNING, "Hangup failed! Strange things may happen!\n");
Mark Spencer
committed
ast_mutex_unlock(&clone->lock);
snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig);
strncpy(clone->name, zombn, sizeof(clone->name) - 1);
manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", masqn, zombn, clone->uniqueid);
/* Update the type. */
original->type = clone->type;
/* Keep the same language. */
strncpy(original->language, clone->language, sizeof(original->language));
clone_variables(original, clone);
/* Presense of ADSI capable CPE follows clone */
original->adsicpe = clone->adsicpe;
/* Bridge remains the same */
/* CDR fields remain the same */
/* XXX What about blocking, softhangup, blocker, and lock and blockproc? XXX */
/* Application and data remain the same */
/* Clone exception becomes real one, as with fdno */
ast_copy_flags(original, clone, AST_FLAG_EXCEPTION);
original->fdno = clone->fdno;
/* Schedule context remains the same */
/* Stream stuff stays the same */
/* Keep the original state. The fixup code will need to work with it most likely */
/* Just swap the whole structures, nevermind the allocations, they'll work themselves
out. */
tmpcid = original->cid;
original->cid = clone->cid;
clone->cid = tmpcid;
/* Restore original timing file descriptor */
original->fds[AST_MAX_FDS - 2] = original->timingfd;
/* Our native formats are different now */
original->nativeformats = clone->nativeformats;
/* Context, extension, priority, app data, jump table, remain the same */
/* pvt switches. pbx stays the same, as does next */
/* Set the write format */
Mark Spencer
committed
ast_set_write_format(original, wformat);
Mark Spencer
committed
ast_set_read_format(original, rformat);
/* Copy the music class */
strncpy(original->musicclass, clone->musicclass, sizeof(original->musicclass) - 1);
ast_log(LOG_DEBUG, "Putting channel %s in %d/%d formats\n", original->name, wformat, rformat);
/* Okay. Last thing is to let the channel driver know about all this mess, so he
can fix up everything as best as possible */
if (original->tech->fixup) {
res = original->tech->fixup(clone, original);
if (res) {
ast_log(LOG_WARNING, "Driver for '%s' could not fixup channel %s\n",
original->type, original->name);
Mark Spencer
committed
ast_mutex_unlock(&clone->lock);
return -1;
}
} else
ast_log(LOG_WARNING, "Driver '%s' does not have a fixup routine (for %s)! Bad things may happen.\n",
original->type, original->name);
/* Now, at this point, the "clone" channel is totally F'd up. We mark it as
a zombie so nothing tries to touch it. If it's already been marked as a
zombie, then free it now (since it already is considered invalid). */
if (ast_test_flag(clone, AST_FLAG_ZOMBIE)) {
ast_log(LOG_DEBUG, "Destroying clone '%s'\n", clone->name);
Mark Spencer
committed
ast_mutex_unlock(&clone->lock);
ast_channel_free(clone);
manager_event(EVENT_FLAG_CALL, "Hangup", "Channel: %s\r\n", zombn);
} else {
struct ast_frame null_frame = { AST_FRAME_NULL, };
ast_log(LOG_DEBUG, "Released clone lock on '%s'\n", clone->name);
ast_queue_frame(clone, &null_frame);
Mark Spencer
committed
ast_mutex_unlock(&clone->lock);
if (ast_test_flag(original, AST_FLAG_BLOCKING))
ast_log(LOG_DEBUG, "Done Masquerading %s (%d)\n",
original->name, original->_state);
void ast_set_callerid(struct ast_channel *chan, const char *callerid, const char *calleridname, const char *ani)
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
if (chan->cid.cid_num)
free(chan->cid.cid_num);
if (ast_strlen_zero(callerid))
chan->cid.cid_num = NULL;
else
chan->cid.cid_num = strdup(callerid);
}
if (calleridname) {
if (chan->cid.cid_name)
free(chan->cid.cid_name);
if (ast_strlen_zero(calleridname))
chan->cid.cid_name = NULL;
else
chan->cid.cid_name = strdup(calleridname);
}
if (ani) {
if (chan->cid.cid_ani)
free(chan->cid.cid_ani);
if (ast_strlen_zero(ani))
chan->cid.cid_ani = NULL;
else
chan->cid.cid_ani = strdup(ani);
}
if (chan->cdr)
ast_cdr_setcid(chan->cdr, chan);
manager_event(EVENT_FLAG_CALL, "Newcallerid",
"Channel: %s\r\n"
"CallerID: %s\r\n"
"CallerIDName: %s\r\n"
chan->name, chan->cid.cid_num ?
chan->cid.cid_num : "<Unknown>",
chan->cid.cid_name ?
chan->cid.cid_name : "<Unknown>",
}
int ast_setstate(struct ast_channel *chan, int state)
{
if (chan->_state != state) {
int oldstate = chan->_state;
chan->_state = state;
if (oldstate == AST_STATE_DOWN) {
ast_device_state_changed(chan->name);
manager_event(EVENT_FLAG_CALL, "Newchannel",
"Channel: %s\r\n"
"State: %s\r\n"
"CallerID: %s\r\n"
"CallerIDName: %s\r\n"
chan->name, ast_state2str(chan->_state),
chan->cid.cid_num ? chan->cid.cid_num : "<unknown>",
chan->cid.cid_name ? chan->cid.cid_name : "<unknown>",
chan->uniqueid);
} else {
manager_event(EVENT_FLAG_CALL, "Newstate",
"Channel: %s\r\n"
"State: %s\r\n"
"CallerID: %s\r\n"
"CallerIDName: %s\r\n"
chan->name, ast_state2str(chan->_state),
chan->cid.cid_num ? chan->cid.cid_num : "<unknown>",
chan->cid.cid_name ? chan->cid.cid_name : "<unknown>",
chan->uniqueid);
static long tvdiff(struct timeval *now, struct timeval *then)
{
return (((now->tv_sec * 1000) + now->tv_usec / 1000) - ((then->tv_sec * 1000) + then->tv_usec / 1000));
#else
return (now->tv_sec - then->tv_sec) * 1000 + (now->tv_usec - then->tv_usec) / 1000;
#endif
Mark Spencer
committed
struct ast_channel *ast_bridged_channel(struct ast_channel *chan)
{
struct ast_channel *bridged;
bridged = chan->_bridge;
if (bridged && bridged->tech->bridged_channel)
bridged = bridged->tech->bridged_channel(chan, bridged);
Mark Spencer
committed
return bridged;
}
Anthony Minessale II
committed
static void bridge_playfile(struct ast_channel *chan, struct ast_channel *peer, char *sound, int remain)
Anthony Minessale II
committed
int res=0, min=0, sec=0,check=0;
check = ast_autoservice_start(peer);
if(check)
return;
if (remain > 0) {
if (remain / 60 > 1) {
min = remain / 60;
sec = remain % 60;
sec = remain;
}
}
if (!strcmp(sound,"timeleft")) {
res = ast_streamfile(chan, "vm-youhave", chan->language);
res = ast_waitstream(chan, "");
if (min) {
res = ast_say_number(chan, min, AST_DIGIT_ANY, chan->language, (char *) NULL);
res = ast_streamfile(chan, "queue-minutes", chan->language);
res = ast_waitstream(chan, "");
}
if (sec) {
res = ast_say_number(chan, sec, AST_DIGIT_ANY, chan->language, (char *) NULL);
res = ast_streamfile(chan, "queue-seconds", chan->language);
res = ast_waitstream(chan, "");
}
} else {
res = ast_streamfile(chan, sound, chan->language);
res = ast_waitstream(chan, "");
}
Anthony Minessale II
committed
check = ast_autoservice_stop(peer);
int ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1, struct ast_bridge_config *config, struct ast_frame **fo, struct ast_channel **rc)
{
/* Copy voice back and forth between the two channels. Give the peer
the ability to transfer calls with '#<extension' syntax. */
struct ast_channel *cs[3];
int to = -1;
struct ast_frame *f;
int res=0;
int o0nativeformats;
int o1nativeformats;
struct timeval start_time,precise_now;
long elapsed_ms=0, time_left_ms=0;
int playit=0, playitagain=1, first_time=1;
firstpass = config->firstpass;
config->firstpass = 0;
/* timestamp */
gettimeofday(&start_time,NULL);
time_left_ms = config->timelimit;
if ((ast_test_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING)) && config->start_sound && firstpass)
Anthony Minessale II
committed
bridge_playfile(c0,c1,config->start_sound,time_left_ms / 1000);
if ((ast_test_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING)) && config->start_sound && firstpass)
Anthony Minessale II
committed
bridge_playfile(c1,c0,config->start_sound,time_left_ms / 1000);
/* Stop if we're a zombie or need a soft hangup */
if (ast_test_flag(c0, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c0) || ast_test_flag(c1, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c1))
Mark Spencer
committed
if (c0->_bridge) {
ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
Mark Spencer
committed
c0->name, c0->_bridge->name);
Mark Spencer
committed
if (c1->_bridge) {
ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
Mark Spencer
committed
c1->name, c1->_bridge->name);
return -1;
}
/* Keep track of bridge */
Mark Spencer
committed
c0->_bridge = c1;
c1->_bridge = c0;
manager_event(EVENT_FLAG_CALL, "Link",
"Channel1: %s\r\n"
James Golovich
committed
"Channel2: %s\r\n"
"Uniqueid1: %s\r\n"
"Uniqueid2: %s\r\n",
c0->name, c1->name, c0->uniqueid, c1->uniqueid);
o1nativeformats = c1->nativeformats;
o0nativeformats = c0->nativeformats;
/* timestamp */
gettimeofday(&precise_now,NULL);
elapsed_ms = tvdiff(&precise_now,&start_time);
time_left_ms = config->timelimit - elapsed_ms;
if (playitagain && ((ast_test_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING)) || (ast_test_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING))) && (config->play_warning && time_left_ms <= config->play_warning)) {
/* narrowing down to the end */
playit = 1;
first_time=0;
playitagain=0;
playit = 1;
first_time=0;
} else {
if ((time_left_ms % config->warning_freq) <= 50) {
playit = 1;
}
}
}
if ((ast_test_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING)) && config->end_sound)
Anthony Minessale II
committed
bridge_playfile(c0,c1,config->end_sound,0);
if ((ast_test_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING)) && config->end_sound)
Anthony Minessale II
committed
bridge_playfile(c1,c0,config->end_sound,0);
break;
}
if (time_left_ms >= 5000 && playit) {
if ((ast_test_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING)) && config->warning_sound && config->play_warning)
Anthony Minessale II
committed
bridge_playfile(c0,c1,config->warning_sound,time_left_ms / 1000);
if ((ast_test_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING)) && config->warning_sound && config->play_warning)
Anthony Minessale II
committed
bridge_playfile(c1,c0,config->warning_sound,time_left_ms / 1000);
playit = 0;
}
}
/* Stop if we're a zombie or need a soft hangup */
if (ast_test_flag(c0, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c0) || ast_test_flag(c1, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c1)) {
*fo = NULL;
if (who) *rc = who;
res = 0;
ast_log(LOG_DEBUG, "Bridge stops because we're zombie or need a soft hangup: c0=%s, c1=%s, flags: %s,%s,%s,%s\n",c0->name,c1->name,ast_test_flag(c0, AST_FLAG_ZOMBIE)?"Yes":"No",ast_check_hangup(c0)?"Yes":"No",ast_test_flag(c1, AST_FLAG_ZOMBIE)?"Yes":"No",ast_check_hangup(c1)?"Yes":"No");
if (c0->tech->bridge && config->timelimit==0 &&
(c0->tech->bridge == c1->tech->bridge) && !nativefailed && !c0->monitor && !c1->monitor) {
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Attempting native bridge of %s and %s\n", c0->name, c1->name);
if (!(res = c0->tech->bridge(c0, c1, config->flags, fo, rc))) {
Mark Spencer
committed
c0->_bridge = NULL;
c1->_bridge = NULL;
manager_event(EVENT_FLAG_CALL, "Unlink",
"Channel1: %s\r\n"
James Golovich
committed
"Channel2: %s\r\n"
"Uniqueid1: %s\r\n"
"Uniqueid2: %s\r\n",
c0->name, c1->name, c0->uniqueid, c1->uniqueid);
ast_log(LOG_DEBUG, "Returning from native bridge, channels: %s, %s\n",c0->name ,c1->name);
return 0;
}
/* If they return non-zero then continue on normally. Let "-2" mean don't worry about
my not wanting to bridge */
if ((res != -2) && (res != -3))
ast_log(LOG_WARNING, "Private bridge between %s and %s failed\n", c0->name, c1->name);
if (res != -3) nativefailed++;
}
if (((c0->writeformat != c1->readformat) || (c0->readformat != c1->writeformat) || (c0->nativeformats != o0nativeformats) || (c1->nativeformats != o1nativeformats)) &&
if (ast_channel_make_compatible(c0, c1)) {
ast_log(LOG_WARNING, "Can't make %s and %s compatible\n", c0->name, c1->name);
manager_event(EVENT_FLAG_CALL, "Unlink",
"Channel1: %s\r\n"
James Golovich
committed
"Channel2: %s\r\n"
"Uniqueid1: %s\r\n"
"Uniqueid2: %s\r\n",
c0->name, c1->name, c0->uniqueid, c1->uniqueid);
o0nativeformats = c0->nativeformats;
o1nativeformats = c1->nativeformats;
who = ast_waitfor_n(cs, 2, &to);
if (!who) {
ast_log(LOG_DEBUG, "Nobody there, continuing...\n");
continue;
}
f = ast_read(who);
if (!f) {
*fo = NULL;
*rc = who;
ast_log(LOG_DEBUG, "Didn't get a frame from channel: %s\n",who->name);
if ((f->frametype == AST_FRAME_CONTROL) && !(config->flags & AST_BRIDGE_IGNORE_SIGS)) {
if ((f->subclass == AST_CONTROL_HOLD) || (f->subclass == AST_CONTROL_UNHOLD)) {
ast_indicate(who == c0 ? c1 : c0, f->subclass);
} else {
*fo = f;
*rc = who;
res = 0;
ast_log(LOG_DEBUG, "Got a FRAME_CONTROL (%d) frame on channel %s\n", f->subclass, who->name);
break;
}
}
if ((f->frametype == AST_FRAME_VOICE) ||
(f->frametype == AST_FRAME_TEXT) ||
(f->frametype == AST_FRAME_VIDEO) ||
(f->frametype == AST_FRAME_IMAGE) ||
(config->flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))) {
if ((config->flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
*rc = c0;
*fo = f;
/* Take out of conference mode */
res = 0;
ast_log(LOG_DEBUG, "Got AST_BRIDGE_DTMF_CHANNEL_0 on c0 (%s)\n",c0->name);
if (config->flags & AST_BRIDGE_DTMF_CHANNEL_1) {
ast_log(LOG_DEBUG, "Got AST_BRIDGE_DTMF_CHANNEL_1 on c1 (%s)\n",c1->name);
}
} else {
#if 0
ast_log(LOG_DEBUG, "Read from %s\n", who->name);
if (who == last)
ast_log(LOG_DEBUG, "Servicing channel %s twice in a row?\n", last->name);
last = who;
#endif
/* Don't copy packets if there is a generator on either one, since they're
not supposed to be listening anyway */
if (who == c0)
ast_write(c1, f);
else
ast_write(c0, f);
/* Swap who gets priority */
cs[2] = cs[0];
cs[0] = cs[1];
cs[1] = cs[2];
}
Mark Spencer
committed
c0->_bridge = NULL;
c1->_bridge = NULL;
manager_event(EVENT_FLAG_CALL, "Unlink",
"Channel1: %s\r\n"
James Golovich
committed
"Channel2: %s\r\n"
"Uniqueid1: %s\r\n"
"Uniqueid2: %s\r\n",
c0->name, c1->name, c0->uniqueid, c1->uniqueid);
ast_log(LOG_DEBUG, "Bridge stops bridging channels %s and %s\n",c0->name,c1->name);
return res;
}
int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
{
int res;
if (chan->tech->setoption) {
res = chan->tech->setoption(chan, option, data, datalen);
if (res < 0)
return res;
} else {
errno = ENOSYS;
return -1;
}
if (block) {
/* XXX Implement blocking -- just wait for our option frame reply, discarding
intermediate packets. XXX */
ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
return -1;
}
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
struct tonepair_def {
int freq1;
int freq2;
int duration;
int vol;
};
struct tonepair_state {
float freq1;
float freq2;
float vol;
int duration;
int pos;
int origwfmt;
struct ast_frame f;
unsigned char offset[AST_FRIENDLY_OFFSET];
short data[4000];
};
static void tonepair_release(struct ast_channel *chan, void *params)
{
struct tonepair_state *ts = params;
if (chan) {
Mark Spencer
committed
ast_set_write_format(chan, ts->origwfmt);
}
free(ts);
}
static void * tonepair_alloc(struct ast_channel *chan, void *params)
{
struct tonepair_state *ts;
struct tonepair_def *td = params;
ts = malloc(sizeof(struct tonepair_state));
if (!ts)
return NULL;
memset(ts, 0, sizeof(struct tonepair_state));
ts->origwfmt = chan->writeformat;
Mark Spencer
committed
if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
tonepair_release(NULL, ts);
ts = NULL;
} else {
ts->freq1 = td->freq1;
ts->freq2 = td->freq2;
ts->duration = td->duration;
ts->vol = td->vol;
}
/* Let interrupts interrupt :) */
static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
{
struct tonepair_state *ts = data;
int x;
/* we need to prepare a frame with 16 * timelen samples as we're
* generating SLIN audio
*/
len = samples * 2;
if (len > sizeof(ts->data) / 2 - 1) {
ast_log(LOG_WARNING, "Can't generate that much data!\n");
return -1;
}
memset(&ts->f, 0, sizeof(ts->f));
for (x=0;x<len/2;x++) {
ts->data[x] = ts->vol * (
sin((ts->freq1 * 2.0 * M_PI / 8000.0) * (ts->pos + x)) +
sin((ts->freq2 * 2.0 * M_PI / 8000.0) * (ts->pos + x))
);
}
ts->f.frametype = AST_FRAME_VOICE;
ts->f.subclass = AST_FORMAT_SLINEAR;
ts->f.datalen = len;
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
ts->f.offset = AST_FRIENDLY_OFFSET;
ts->f.data = ts->data;
ast_write(chan, &ts->f);
ts->pos += x;
if (ts->duration > 0) {
if (ts->pos >= ts->duration * 8)
return -1;
}
return 0;
}
static struct ast_generator tonepair = {
alloc: tonepair_alloc,
release: tonepair_release,
generate: tonepair_generator,
};
int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
{
struct tonepair_def d = { 0, };
d.freq1 = freq1;
d.freq2 = freq2;
d.duration = duration;
if (vol < 1)
d.vol = 8192;
else
d.vol = vol;
if (ast_activate_generator(chan, &tonepair, &d))
return -1;
return 0;
}
void ast_tonepair_stop(struct ast_channel *chan)
{
ast_deactivate_generator(chan);
}
int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
{
struct ast_frame *f;
int res;
if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
return res;
/* Give us some wiggle room */
while(chan->generatordata && (ast_waitfor(chan, 100) >= 0)) {
f = ast_read(chan);
if (f)
ast_frfree(f);
else
return -1;
}
return 0;
}
ast_group_t ast_get_group(char *s)
{
char *copy;
char *piece;
char *c=NULL;
int start=0, finish=0,x;
copy = ast_strdupa(s);
if (!copy) {
ast_log(LOG_ERROR, "Out of memory\n");
return 0;
}
c = copy;
while((piece = strsep(&c, ","))) {
if (sscanf(piece, "%d-%d", &start, &finish) == 2) {
/* Range */
} else if (sscanf(piece, "%d", &start)) {
/* Just one */
finish = start;
} else {
ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'.\n", s, piece);
continue;
}
for (x=start;x<=finish;x++) {
if ((x > 63) || (x < 0)) {
ast_log(LOG_WARNING, "Ignoring invalid group %d (maximum group is 63)\n", x);
} else
group |= (1 << x);
}
}
return group;
}
static int (*ast_moh_start_ptr)(struct ast_channel *, char *) = NULL;
static void (*ast_moh_stop_ptr)(struct ast_channel *) = NULL;
Mark Spencer
committed
static void (*ast_moh_cleanup_ptr)(struct ast_channel *) = NULL;
void ast_install_music_functions(int (*start_ptr)(struct ast_channel *, char *),
Mark Spencer
committed
void (*stop_ptr)(struct ast_channel *),
void (*cleanup_ptr)(struct ast_channel *)
)
{
ast_moh_start_ptr = start_ptr;
ast_moh_stop_ptr = stop_ptr;
Mark Spencer
committed
ast_moh_cleanup_ptr = cleanup_ptr;
}
void ast_uninstall_music_functions(void)
{
ast_moh_start_ptr = NULL;
ast_moh_stop_ptr = NULL;
Mark Spencer
committed
ast_moh_cleanup_ptr = NULL;
}
/*! Turn on/off music on hold on a given channel */
int ast_moh_start(struct ast_channel *chan, char *mclass)
{
if(ast_moh_start_ptr)
return ast_moh_start_ptr(chan, mclass);
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Music class %s requested but no musiconhold loaded.\n", mclass ? mclass : "default");
return 0;
}
void ast_moh_stop(struct ast_channel *chan)
{
if(ast_moh_stop_ptr)
ast_moh_stop_ptr(chan);
}
Mark Spencer
committed
void ast_moh_cleanup(struct ast_channel *chan)
{
if(ast_moh_cleanup_ptr)
ast_moh_cleanup_ptr(chan);
}
void ast_channels_init(void)
{
ast_cli_register(&cli_show_channeltypes);
}
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
/*--- ast_print_group: Print call group and pickup group ---*/
char *ast_print_group(char *buf, int buflen, ast_group_t group)
{
unsigned int i;
int first=1;
char num[3];
buf[0] = '\0';
if (!group) /* Return empty string if no group */
return(buf);
for (i=0; i<=63; i++) { /* Max group is 63 */
if (group & (1 << i)) {
if (!first) {
strncat(buf, ", ", buflen);
} else {
first=0;
}
snprintf(num, sizeof(num), "%u", i);
strncat(buf, num, buflen);
}
}
return(buf);
}