2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
22 #include <math.h> /* For PI */
23 #include <asterisk/pbx.h>
24 #include <asterisk/frame.h>
25 #include <asterisk/sched.h>
26 #include <asterisk/options.h>
27 #include <asterisk/channel.h>
28 #include <asterisk/channel_pvt.h>
29 #include <asterisk/logger.h>
30 #include <asterisk/file.h>
31 #include <asterisk/translate.h>
32 #include <asterisk/manager.h>
33 #include <asterisk/chanvars.h>
34 #include <asterisk/linkedlists.h>
35 #include <asterisk/indications.h>
36 #include <asterisk/monitor.h>
37 #ifdef ZAPTEL_OPTIMIZATIONS
38 #include <sys/ioctl.h>
39 #include <linux/zaptel.h>
43 static int shutting_down = 0;
44 static int uniqueint = 0;
46 /* XXX Lock appropriately in more functions XXX */
49 /* Convenient mutex debugging functions */
50 #define PTHREAD_MUTEX_LOCK(a) __PTHREAD_MUTEX_LOCK(__FUNCTION__, a)
51 #define PTHREAD_MUTEX_UNLOCK(a) __PTHREAD_MUTEX_UNLOCK(__FUNCTION__, a)
53 static int __PTHREAD_MUTEX_LOCK(char *f, pthread_mutex_t *a) {
54 ast_log(LOG_DEBUG, "Locking %p (%s)\n", a, f);
55 return ast_pthread_mutex_lock(a);
58 static int __PTHREAD_MUTEX_UNLOCK(char *f, pthread_mutex_t *a) {
59 ast_log(LOG_DEBUG, "Unlocking %p (%s)\n", a, f);
60 return ast_pthread_mutex_unlock(a);
63 #define PTHREAD_MUTEX_LOCK(a) ast_pthread_mutex_lock(a)
64 #define PTHREAD_MUTEX_UNLOCK(a) ast_pthread_mutex_unlock(a)
71 struct ast_channel * (*requester)(char *type, int format, void *data);
72 int (*devicestate)(void *data);
73 struct chanlist *next;
75 struct ast_channel *channels = NULL;
77 /* Protect the channel list (highly unlikely that two things would change
78 it at the same time, but still! */
80 static pthread_mutex_t chlock = AST_MUTEX_INITIALIZER;
82 int ast_check_hangup(struct ast_channel *chan)
86 /* if soft hangup flag, return true */
87 if (chan->_softhangup) return 1;
88 /* if no private structure, return true */
89 if (!chan->pvt->pvt) return 1;
90 /* if no hangup scheduled, just return here */
91 if (!chan->whentohangup) return 0;
92 time(&myt); /* get current time */
93 /* return, if not yet */
94 if (chan->whentohangup > myt) return 0;
95 chan->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
99 void ast_begin_shutdown(int hangup)
101 struct ast_channel *c;
104 PTHREAD_MUTEX_LOCK(&chlock);
107 ast_softhangup(c, AST_SOFTHANGUP_SHUTDOWN);
110 PTHREAD_MUTEX_UNLOCK(&chlock);
114 int ast_active_channels(void)
116 struct ast_channel *c;
118 PTHREAD_MUTEX_LOCK(&chlock);
124 PTHREAD_MUTEX_UNLOCK(&chlock);
128 void ast_cancel_shutdown(void)
133 int ast_shutting_down(void)
135 return shutting_down;
138 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
144 chan->whentohangup = myt + offset;
146 chan->whentohangup = 0;
150 int ast_channel_register(char *type, char *description, int capabilities,
151 struct ast_channel *(*requester)(char *type, int format, void *data))
153 return ast_channel_register_ex(type, description, capabilities, requester, NULL);
156 int ast_channel_register_ex(char *type, char *description, int capabilities,
157 struct ast_channel *(*requester)(char *type, int format, void *data),
158 int (*devicestate)(void *data))
160 struct chanlist *chan, *last=NULL;
161 if (PTHREAD_MUTEX_LOCK(&chlock)) {
162 ast_log(LOG_WARNING, "Unable to lock channel list\n");
167 if (!strcasecmp(type, chan->type)) {
168 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", type);
169 PTHREAD_MUTEX_UNLOCK(&chlock);
175 chan = malloc(sizeof(struct chanlist));
177 ast_log(LOG_WARNING, "Out of memory\n");
178 PTHREAD_MUTEX_UNLOCK(&chlock);
181 strncpy(chan->type, type, sizeof(chan->type)-1);
182 strncpy(chan->description, description, sizeof(chan->description)-1);
183 chan->capabilities = capabilities;
184 chan->requester = requester;
185 chan->devicestate = devicestate;
192 ast_log(LOG_DEBUG, "Registered handler for '%s' (%s)\n", chan->type, chan->description);
193 else if (option_verbose > 1)
194 ast_verbose( VERBOSE_PREFIX_2 "Registered channel type '%s' (%s)\n", chan->type, chan->description);
195 PTHREAD_MUTEX_UNLOCK(&chlock);
199 char *ast_state2str(int state)
201 /* XXX Not reentrant XXX */
202 static char localtmp[256];
206 case AST_STATE_RESERVED:
208 case AST_STATE_OFFHOOK:
210 case AST_STATE_DIALING:
214 case AST_STATE_RINGING:
221 snprintf(localtmp, sizeof(localtmp), "Unknown (%d)\n", state);
227 int ast_best_codec(int fmts)
229 /* This just our opinion, expressed in code. We are asked to choose
230 the best codec to use, given no information */
234 /* Okay, ulaw is used by all telephony equipment, so start with it */
236 /* Unless of course, you're a silly European, so then prefer ALAW */
238 /* Okay, well, signed linear is easy to translate into other stuff */
240 /* ADPCM has great sound quality and is still pretty easy to translate */
242 /* Okay, we're down to vocoders now, so pick GSM because it's small and easier to
243 translate and sounds pretty good */
245 /* iLBC is not too bad */
247 /* Speex is free, but computationally more expensive than GSM */
249 /* Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
252 /* G.729a is faster than 723 and slightly less expensive */
254 /* Down to G.723.1 which is proprietary but at least designed for voice */
256 /* Last and least, MP3 which was of course never designed for real-time voice */
261 for (x=0;x<sizeof(prefs) / sizeof(prefs[0]); x++)
264 ast_log(LOG_WARNING, "Don't know any of 0x%x formats\n", fmts);
268 struct ast_channel *ast_channel_alloc(int needqueue)
270 struct ast_channel *tmp;
271 struct ast_channel_pvt *pvt;
274 struct varshead *headp;
277 /* If shutting down, don't allocate any new channels */
280 PTHREAD_MUTEX_LOCK(&chlock);
281 tmp = malloc(sizeof(struct ast_channel));
283 memset(tmp, 0, sizeof(struct ast_channel));
284 pvt = malloc(sizeof(struct ast_channel_pvt));
286 memset(pvt, 0, sizeof(struct ast_channel_pvt));
287 tmp->sched = sched_context_create();
289 for (x=0;x<AST_MAX_FDS - 1;x++)
292 pipe(pvt->alertpipe)) {
293 ast_log(LOG_WARNING, "Alert pipe creation failed!\n");
299 /* Make sure we've got it done right if they don't */
301 flags = fcntl(pvt->alertpipe[0], F_GETFL);
302 fcntl(pvt->alertpipe[0], F_SETFL, flags | O_NONBLOCK);
303 flags = fcntl(pvt->alertpipe[1], F_GETFL);
304 fcntl(pvt->alertpipe[1], F_SETFL, flags | O_NONBLOCK);
306 pvt->alertpipe[0] = pvt->alertpipe[1] = -1;
307 #ifdef ZAPTEL_OPTIMIZATIONS
308 tmp->timingfd = open("/dev/zap/timer", O_RDWR);
312 /* Always watch the alertpipe */
313 tmp->fds[AST_MAX_FDS-1] = pvt->alertpipe[0];
314 /* And timing pipe */
315 tmp->fds[AST_MAX_FDS-2] = tmp->timingfd;
316 strncpy(tmp->name, "**Unknown**", sizeof(tmp->name)-1);
319 tmp->_state = AST_STATE_DOWN;
326 snprintf(tmp->uniqueid, sizeof(tmp->uniqueid), "%li.%d", time(NULL), uniqueint++);
327 headp=&tmp->varshead;
328 ast_pthread_mutex_init(&tmp->lock);
329 AST_LIST_HEAD_INIT(headp);
330 tmp->vars=ast_var_assign("tempvar","tempval");
331 AST_LIST_INSERT_HEAD(headp,tmp->vars,entries);
332 strncpy(tmp->context, "default", sizeof(tmp->context)-1);
333 strncpy(tmp->language, defaultlanguage, sizeof(tmp->language)-1);
334 strncpy(tmp->exten, "s", sizeof(tmp->exten)-1);
336 tmp->amaflags = ast_default_amaflags;
337 strncpy(tmp->accountcode, ast_default_accountcode, sizeof(tmp->accountcode)-1);
338 tmp->next = channels;
342 ast_log(LOG_WARNING, "Unable to create schedule context\n");
347 ast_log(LOG_WARNING, "Out of memory\n");
352 ast_log(LOG_WARNING, "Out of memory\n");
353 PTHREAD_MUTEX_UNLOCK(&chlock);
357 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int lock)
360 struct ast_frame *prev, *cur;
363 /* Build us a copy and free the original one */
366 ast_log(LOG_WARNING, "Unable to duplicate frame\n");
370 ast_pthread_mutex_lock(&chan->lock);
372 cur = chan->pvt->readq;
378 /* Allow up to 96 voice frames outstanding, and up to 128 total frames */
379 if (((fin->frametype == AST_FRAME_VOICE) && (qlen > 96)) || (qlen > 128)) {
380 if (fin->frametype != AST_FRAME_VOICE) {
381 ast_log(LOG_WARNING, "Exceptionally long queue length queuing to %s\n", chan->name);
384 ast_log(LOG_DEBUG, "Dropping voice to exceptionally long queue on %s\n", chan->name);
387 ast_pthread_mutex_unlock(&chan->lock);
394 chan->pvt->readq = f;
395 if (chan->pvt->alertpipe[1] > -1) {
396 if (write(chan->pvt->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah))
397 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d (qlen = %d): %s!\n",
398 chan->name, f->frametype, f->subclass, qlen, strerror(errno));
399 } else if (chan->blocking) {
400 pthread_kill(chan->blocker, SIGURG);
403 ast_pthread_mutex_unlock(&chan->lock);
407 int ast_queue_hangup(struct ast_channel *chan, int lock)
409 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
410 chan->_softhangup |= AST_SOFTHANGUP_DEV;
411 return ast_queue_frame(chan, &f, lock);
414 int ast_queue_control(struct ast_channel *chan, int control, int lock)
416 struct ast_frame f = { AST_FRAME_CONTROL, };
417 f.subclass = control;
418 return ast_queue_frame(chan, &f, lock);
421 int ast_channel_defer_dtmf(struct ast_channel *chan)
425 pre = chan->deferdtmf;
431 void ast_channel_undefer_dtmf(struct ast_channel *chan)
437 struct ast_channel *ast_channel_walk(struct ast_channel *prev)
439 struct ast_channel *l, *ret=NULL;
440 PTHREAD_MUTEX_LOCK(&chlock);
443 PTHREAD_MUTEX_UNLOCK(&chlock);
451 PTHREAD_MUTEX_UNLOCK(&chlock);
456 int ast_safe_sleep_conditional( struct ast_channel *chan, int ms,
457 int (*cond)(void*), void *data )
462 if( cond && ((*cond)(data) == 0 ) )
464 ms = ast_waitfor(chan, ms);
477 int ast_safe_sleep(struct ast_channel *chan, int ms)
481 ms = ast_waitfor(chan, ms);
494 void ast_channel_free(struct ast_channel *chan)
496 struct ast_channel *last=NULL, *cur;
498 struct ast_var_t *vardata;
499 struct ast_frame *f, *fp;
500 struct varshead *headp;
501 char name[AST_CHANNEL_NAME];
503 headp=&chan->varshead;
505 PTHREAD_MUTEX_LOCK(&chlock);
510 last->next = cur->next;
512 channels = cur->next;
519 ast_log(LOG_WARNING, "Unable to find channel in list\n");
521 ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
523 strncpy(name, chan->name, sizeof(name)-1);
525 /* Stop monitoring */
527 chan->monitor->stop( chan, 0 );
530 /* Free translatosr */
531 if (chan->pvt->readtrans)
532 ast_translator_free_path(chan->pvt->readtrans);
533 if (chan->pvt->writetrans)
534 ast_translator_free_path(chan->pvt->writetrans);
536 ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
540 free(chan->callerid);
545 pthread_mutex_destroy(&chan->lock);
546 /* Close pipes if appropriate */
547 if ((fd = chan->pvt->alertpipe[0]) > -1)
549 if ((fd = chan->pvt->alertpipe[1]) > -1)
551 if ((fd = chan->timingfd) > -1)
553 f = chan->pvt->readq;
554 chan->pvt->readq = NULL;
561 /* loop over the variables list, freeing all data and deleting list items */
562 /* no need to lock the list, as the channel is already locked */
564 while (!AST_LIST_EMPTY(headp)) { /* List Deletion. */
565 vardata = AST_LIST_FIRST(headp);
566 AST_LIST_REMOVE_HEAD(headp, entries);
567 // printf("deleting var %s=%s\n",ast_var_name(vardata),ast_var_value(vardata));
568 ast_var_delete(vardata);
575 PTHREAD_MUTEX_UNLOCK(&chlock);
577 ast_device_state_changed(name);
580 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
583 struct ast_frame f = { AST_FRAME_NULL };
585 ast_log(LOG_DEBUG, "Soft-Hanging up channel '%s'\n", chan->name);
586 /* Inform channel driver that we need to be hung up, if it cares */
587 chan->_softhangup |= cause;
588 ast_queue_frame(chan, &f, 0);
589 /* Interrupt any select call or such */
591 pthread_kill(chan->blocker, SIGURG);
595 int ast_softhangup(struct ast_channel *chan, int cause)
598 ast_pthread_mutex_lock(&chan->lock);
599 res = ast_softhangup_nolock(chan, cause);
600 ast_pthread_mutex_unlock(&chan->lock);
604 static int ast_do_masquerade(struct ast_channel *original);
606 static void free_translation(struct ast_channel *clone)
608 if (clone->pvt->writetrans)
609 ast_translator_free_path(clone->pvt->writetrans);
610 if (clone->pvt->readtrans)
611 ast_translator_free_path(clone->pvt->readtrans);
612 clone->pvt->writetrans = NULL;
613 clone->pvt->readtrans = NULL;
614 clone->pvt->rawwriteformat = clone->nativeformats;
615 clone->pvt->rawreadformat = clone->nativeformats;
618 int ast_hangup(struct ast_channel *chan)
621 /* Don't actually hang up a channel that will masquerade as someone else, or
622 if someone is going to masquerade as us */
623 ast_pthread_mutex_lock(&chan->lock);
625 if (ast_do_masquerade(chan))
626 ast_log(LOG_WARNING, "Failed to perform masquerade\n");
630 ast_log(LOG_WARNING, "%s getting hung up, but someone is trying to masq into us?!?\n", chan->name);
631 ast_pthread_mutex_unlock(&chan->lock);
634 /* If this channel is one which will be masqueraded into something,
635 mark it as a zombie already, so we know to free it later */
637 ast_pthread_mutex_unlock(&chan->lock);
641 free_translation(chan);
643 ast_stopstream(chan);
645 sched_context_destroy(chan->sched);
646 /* Clear any tone stuff remaining */
647 if (chan->generatordata)
648 chan->generator->release(chan, chan->generatordata);
649 chan->generatordata = NULL;
650 chan->generator = NULL;
652 /* End the CDR if it hasn't already */
653 ast_cdr_end(chan->cdr);
654 /* Post and Free the CDR */
655 ast_cdr_post(chan->cdr);
656 ast_cdr_free(chan->cdr);
658 if (chan->blocking) {
659 ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
660 "is blocked by thread %ld in procedure %s! Expect a failure\n",
661 pthread_self(), chan->name, chan->blocker, chan->blockproc);
666 ast_log(LOG_DEBUG, "Hanging up channel '%s'\n", chan->name);
667 if (chan->pvt->hangup)
668 res = chan->pvt->hangup(chan);
671 ast_log(LOG_DEBUG, "Hanging up zombie '%s'\n", chan->name);
673 ast_pthread_mutex_unlock(&chan->lock);
674 manager_event(EVENT_FLAG_CALL, "Hangup",
677 chan->name, chan->uniqueid);
678 ast_channel_free(chan);
682 void ast_channel_unregister(char *type)
684 struct chanlist *chan, *last=NULL;
686 ast_log(LOG_DEBUG, "Unregistering channel type '%s'\n", type);
687 if (PTHREAD_MUTEX_LOCK(&chlock)) {
688 ast_log(LOG_WARNING, "Unable to lock channel list\n");
693 if (!strcasecmp(chan->type, type)) {
695 last->next = chan->next;
697 backends = backends->next;
699 PTHREAD_MUTEX_UNLOCK(&chlock);
705 PTHREAD_MUTEX_UNLOCK(&chlock);
708 int ast_answer(struct ast_channel *chan)
711 /* Stop if we're a zombie or need a soft hangup */
712 if (chan->zombie || ast_check_hangup(chan))
714 switch(chan->_state) {
715 case AST_STATE_RINGING:
717 if (chan->pvt->answer)
718 res = chan->pvt->answer(chan);
719 ast_setstate(chan, AST_STATE_UP);
721 ast_cdr_answer(chan->cdr);
726 ast_cdr_answer(chan->cdr);
732 void ast_deactivate_generator(struct ast_channel *chan)
734 if (chan->generatordata) {
735 chan->generator->release(chan, chan->generatordata);
736 chan->generatordata = NULL;
737 chan->generator = NULL;
738 chan->writeinterrupt = 0;
742 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
744 if (chan->generatordata) {
745 chan->generator->release(chan, chan->generatordata);
746 chan->generatordata = NULL;
749 if ((chan->generatordata = gen->alloc(chan, params))) {
750 chan->generator = gen;
757 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
759 /* Wait for x amount of time on a file descriptor to have input. */
766 tv.tv_sec = *ms / 1000;
767 tv.tv_usec = (*ms % 1000) * 1000;
772 FD_SET(fds[x], &rfds);
773 FD_SET(fds[x], &efds);
779 res = ast_select(max + 1, &rfds, NULL, &efds, &tv);
781 res = ast_select(max + 1, &rfds, NULL, &efds, NULL);
784 /* Simulate a timeout if we were interrupted */
793 if ((fds[x] > -1) && (FD_ISSET(fds[x], &rfds) || FD_ISSET(fds[x], &efds)) && (winner < 0)) {
795 *exception = FD_ISSET(fds[x], &efds);
799 *ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
803 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
804 int *exception, int *outfd, int *ms)
806 /* Wait for x amount of time on a file descriptor to have input. */
811 struct ast_channel *winner = NULL;
817 /* Perform any pending masquerades */
819 ast_pthread_mutex_lock(&c[x]->lock);
821 if (ast_do_masquerade(c[x])) {
822 ast_log(LOG_WARNING, "Masquerade failed\n");
824 ast_pthread_mutex_unlock(&c[x]->lock);
828 ast_pthread_mutex_unlock(&c[x]->lock);
831 tv.tv_sec = *ms / 1000;
832 tv.tv_usec = (*ms % 1000) * 1000;
837 for (y=0;y<AST_MAX_FDS;y++) {
838 if (c[x]->fds[y] > -1) {
839 FD_SET(c[x]->fds[y], &rfds);
840 FD_SET(c[x]->fds[y], &efds);
841 if (c[x]->fds[y] > max)
845 CHECK_BLOCKING(c[x]);
847 for (x=0;x<nfds; x++) {
848 FD_SET(fds[x], &rfds);
849 FD_SET(fds[x], &efds);
854 res = ast_select(max + 1, &rfds, NULL, &efds, &tv);
856 res = ast_select(max + 1, &rfds, NULL, &efds, NULL);
861 /* Simulate a timeout if we were interrupted */
865 /* Just an interrupt */
875 for (y=0;y<AST_MAX_FDS;y++) {
876 if (c[x]->fds[y] > -1) {
877 if ((FD_ISSET(c[x]->fds[y], &rfds) || FD_ISSET(c[x]->fds[y], &efds)) && !winner) {
878 /* Set exception flag if appropriate */
879 if (FD_ISSET(c[x]->fds[y], &efds))
887 for (x=0;x<nfds;x++) {
888 if ((FD_ISSET(fds[x], &rfds) || FD_ISSET(fds[x], &efds)) && !winner) {
891 if (FD_ISSET(fds[x], &efds) && exception)
896 *ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
900 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
902 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
905 int ast_waitfor(struct ast_channel *c, int ms)
907 struct ast_channel *chan;
909 chan = ast_waitfor_n(&c, 1, &ms);
919 char ast_waitfordigit(struct ast_channel *c, int ms)
921 /* XXX Should I be merged with waitfordigit_full XXX */
924 /* Stop if we're a zombie or need a soft hangup */
925 if (c->zombie || ast_check_hangup(c))
927 /* Wait for a digit, no more than ms milliseconds total. */
928 while(ms && !result) {
929 ms = ast_waitfor(c, ms);
930 if (ms < 0) /* Error */
936 if (f->frametype == AST_FRAME_DTMF)
937 result = f->subclass;
946 int ast_settimeout(struct ast_channel *c, int samples, int (*func)(void *data), void *data)
949 #ifdef ZAPTEL_OPTIMIZATIONS
950 if (c->timingfd > -1) {
955 ast_log(LOG_DEBUG, "Scheduling timer at %d sample intervals\n", samples);
956 res = ioctl(c->timingfd, ZT_TIMERCONFIG, &samples);
957 c->timingfunc = func;
958 c->timingdata = data;
963 char ast_waitfordigit_full(struct ast_channel *c, int ms, int audio, int ctrl)
967 struct ast_channel *rchan;
969 /* Stop if we're a zombie or need a soft hangup */
970 if (c->zombie || ast_check_hangup(c))
972 /* Wait for a digit, no more than ms milliseconds total. */
973 while(ms && !result) {
974 rchan = ast_waitfor_nandfds(&c, 1, &audio, (audio > -1) ? 1 : 0, NULL, &outfd, &ms);
975 if ((!rchan) && (outfd < 0) && (ms)) /* Error */
977 else if (outfd > -1) {
983 if (f->frametype == AST_FRAME_DTMF)
984 result = f->subclass;
993 struct ast_frame *ast_read(struct ast_channel *chan)
995 struct ast_frame *f = NULL;
997 #ifdef ZAPTEL_OPTIMIZATIONS
1001 static struct ast_frame null_frame =
1006 ast_pthread_mutex_lock(&chan->lock);
1008 if (ast_do_masquerade(chan)) {
1009 ast_log(LOG_WARNING, "Failed to perform masquerade\n");
1013 pthread_mutex_unlock(&chan->lock);
1017 /* Stop if we're a zombie or need a soft hangup */
1018 if (chan->zombie || ast_check_hangup(chan)) {
1019 if (chan->generator)
1020 ast_deactivate_generator(chan);
1021 pthread_mutex_unlock(&chan->lock);
1025 if (!chan->deferdtmf && strlen(chan->dtmfq)) {
1026 /* We have DTMF that has been deferred. Return it now */
1027 chan->dtmff.frametype = AST_FRAME_DTMF;
1028 chan->dtmff.subclass = chan->dtmfq[0];
1029 /* Drop first digit */
1030 memmove(chan->dtmfq, chan->dtmfq + 1, sizeof(chan->dtmfq) - 1);
1031 pthread_mutex_unlock(&chan->lock);
1032 return &chan->dtmff;
1035 /* Read and ignore anything on the alertpipe, but read only
1036 one sizeof(blah) per frame that we send from it */
1037 if (chan->pvt->alertpipe[0] > -1) {
1038 read(chan->pvt->alertpipe[0], &blah, sizeof(blah));
1040 #ifdef ZAPTEL_OPTIMIZATIONS
1041 if ((chan->timingfd > -1) && (chan->fdno == AST_MAX_FDS - 2) && chan->exception) {
1042 chan->exception = 0;
1044 ioctl(chan->timingfd, ZT_TIMERACK, &blah);
1045 func = chan->timingfunc;
1046 data = chan->timingdata;
1047 pthread_mutex_unlock(&chan->lock);
1050 ast_log(LOG_DEBUG, "Calling private function\n");
1055 pthread_mutex_lock(&chan->lock);
1056 ioctl(chan->timingfd, ZT_TIMERCONFIG, &blah);
1057 chan->timingdata = NULL;
1058 pthread_mutex_unlock(&chan->lock);
1064 /* Check for pending read queue */
1065 if (chan->pvt->readq) {
1066 f = chan->pvt->readq;
1067 chan->pvt->readq = f->next;
1068 /* Interpret hangup and return NULL */
1069 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))
1072 chan->blocker = pthread_self();
1073 if (chan->exception) {
1074 if (chan->pvt->exception)
1075 f = chan->pvt->exception(chan);
1077 ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", chan->name);
1080 /* Clear the exception flag */
1081 chan->exception = 0;
1083 if (chan->pvt->read)
1084 f = chan->pvt->read(chan);
1086 ast_log(LOG_WARNING, "No read routine on channel %s\n", chan->name);
1090 if (f && (f->frametype == AST_FRAME_VOICE)) {
1091 if (!(f->subclass & chan->nativeformats)) {
1092 /* This frame can't be from the current native formats -- drop it on the
1094 ast_log(LOG_NOTICE, "Dropping incompatible voice frame on %s of format %d since our native format has changed to %d\n", chan->name, f->subclass, chan->nativeformats);
1098 if (chan->monitor && chan->monitor->read_stream ) {
1099 if( ast_writestream( chan->monitor->read_stream, f ) < 0 ) {
1100 ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n");
1103 if (chan->pvt->readtrans) {
1104 f = ast_translate(chan->pvt->readtrans, f, 1);
1111 /* Make sure we always return NULL in the future */
1113 chan->_softhangup |= AST_SOFTHANGUP_DEV;
1114 if (chan->generator)
1115 ast_deactivate_generator(chan);
1116 /* End the CDR if appropriate */
1118 ast_cdr_end(chan->cdr);
1119 } else if (chan->deferdtmf && f->frametype == AST_FRAME_DTMF) {
1120 if (strlen(chan->dtmfq) < sizeof(chan->dtmfq) - 2)
1121 chan->dtmfq[strlen(chan->dtmfq)] = f->subclass;
1123 ast_log(LOG_WARNING, "Dropping deferred DTMF digits on %s\n", chan->name);
1125 } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
1126 /* Answer the CDR */
1127 ast_setstate(chan, AST_STATE_UP);
1128 ast_cdr_answer(chan->cdr);
1130 pthread_mutex_unlock(&chan->lock);
1132 /* Run any generator sitting on the line */
1133 if (f && (f->frametype == AST_FRAME_VOICE) && chan->generatordata) {
1134 /* Mask generator data temporarily */
1137 tmp = chan->generatordata;
1138 chan->generatordata = NULL;
1139 res = chan->generator->generate(chan, tmp, f->datalen, f->samples);
1140 chan->generatordata = tmp;
1142 ast_log(LOG_DEBUG, "Auto-deactivating generator\n");
1143 ast_deactivate_generator(chan);
1146 if (chan->fin & 0x80000000)
1147 ast_frame_dump(chan->name, f, "<<");
1148 if ((chan->fin & 0x7fffffff) == 0x7fffffff)
1149 chan->fin &= 0x80000000;
1155 int ast_indicate(struct ast_channel *chan, int condition)
1158 /* Stop if we're a zombie or need a soft hangup */
1159 if (chan->zombie || ast_check_hangup(chan))
1161 if (chan->pvt->indicate)
1162 res = chan->pvt->indicate(chan, condition);
1163 if (!chan->pvt->indicate || res) {
1165 * Device does not support (that) indication, lets fake
1166 * it by doing our own tone generation. (PM2002)
1168 if (condition >= 0) {
1169 const struct tone_zone_sound *ts = NULL;
1170 switch (condition) {
1171 case AST_CONTROL_RINGING:
1172 ts = ast_get_indication_tone(chan->zone, "ring");
1174 case AST_CONTROL_BUSY:
1175 ts = ast_get_indication_tone(chan->zone, "busy");
1177 case AST_CONTROL_CONGESTION:
1178 ts = ast_get_indication_tone(chan->zone, "congestion");
1181 if (ts && ts->data[0]) {
1182 ast_log(LOG_DEBUG, "Driver for channel '%s' does not support indication %d, emulating it\n", chan->name, condition);
1183 ast_playtones_start(chan,0,ts->data, 1);
1184 } else if (condition == AST_CONTROL_PROGRESS) {
1185 /* ast_playtones_stop(chan); */
1188 ast_log(LOG_WARNING, "Unable to handle indication %d for '%s'\n", condition, chan->name);
1192 else ast_playtones_stop(chan);
1197 int ast_recvchar(struct ast_channel *chan, int timeout)
1200 struct ast_frame *f;
1205 if (ast_check_hangup(chan)) return -1;
1206 res = ast_waitfor(chan,ourto);
1207 if (res <= 0) /* if timeout */
1213 if (f == NULL) return -1; /* if hangup */
1214 if ((f->frametype == AST_FRAME_CONTROL) &&
1215 (f->subclass == AST_CONTROL_HANGUP)) return -1; /* if hangup */
1216 if (f->frametype == AST_FRAME_TEXT) /* if a text frame */
1218 c = *((char *)f->data); /* get the data */
1226 int ast_sendtext(struct ast_channel *chan, char *text)
1229 /* Stop if we're a zombie or need a soft hangup */
1230 if (chan->zombie || ast_check_hangup(chan))
1232 CHECK_BLOCKING(chan);
1233 if (chan->pvt->send_text)
1234 res = chan->pvt->send_text(chan, text);
1239 static int do_senddigit(struct ast_channel *chan, char digit)
1243 if (chan->pvt->send_digit)
1244 res = chan->pvt->send_digit(chan, digit);
1245 if (!chan->pvt->send_digit || res) {
1247 * Device does not support DTMF tones, lets fake
1248 * it by doing our own generation. (PM2002)
1250 static const char* dtmf_tones[] = {
1251 "!941+1336/50,!0/50", /* 0 */
1252 "!697+1209/50,!0/50", /* 1 */
1253 "!697+1336/50,!0/50", /* 2 */
1254 "!697+1477/50,!0/50", /* 3 */
1255 "!770+1209/50,!0/50", /* 4 */
1256 "!770+1336/50,!0/50", /* 5 */
1257 "!770+1477/50,!0/50", /* 6 */
1258 "!852+1209/50,!0/50", /* 7 */
1259 "!852+1336/50,!0/50", /* 8 */
1260 "!852+1477/50,!0/50", /* 9 */
1261 "!697+1633/50,!0/50", /* A */
1262 "!770+1633/50,!0/50", /* B */
1263 "!852+1633/50,!0/50", /* C */
1264 "!941+1633/50,!0/50", /* D */
1265 "!941+1209/50,!0/50", /* * */
1266 "!941+1477/50,!0/50" }; /* # */
1267 if (digit >= '0' && digit <='9')
1268 ast_playtones_start(chan,0,dtmf_tones[digit-'0'], 0);
1269 else if (digit >= 'A' && digit <= 'D')
1270 ast_playtones_start(chan,0,dtmf_tones[digit-'A'+10], 0);
1271 else if (digit == '*')
1272 ast_playtones_start(chan,0,dtmf_tones[14], 0);
1273 else if (digit == '#')
1274 ast_playtones_start(chan,0,dtmf_tones[15], 0);
1277 ast_log(LOG_WARNING, "Unable to handle DTMF tone '%c' for '%s'\n", digit, chan->name);
1284 int ast_prod(struct ast_channel *chan)
1286 struct ast_frame a = { AST_FRAME_VOICE };
1288 /* Send an empty audio frame to get things moving */
1289 if (chan->_state != AST_STATE_UP) {
1290 ast_log(LOG_DEBUG, "Prodding channel '%s'\n", chan->name);
1291 a.subclass = chan->pvt->rawwriteformat;
1292 a.data = nothing + AST_FRIENDLY_OFFSET;
1293 if (ast_write(chan, &a))
1294 ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", chan->name);
1299 int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
1302 if (!chan->pvt->write_video)
1304 res = ast_write(chan, fr);
1310 int ast_write(struct ast_channel *chan, struct ast_frame *fr)
1313 struct ast_frame *f = NULL;
1314 /* Stop if we're a zombie or need a soft hangup */
1315 ast_pthread_mutex_lock(&chan->lock);
1316 if (chan->zombie || ast_check_hangup(chan)) {
1317 ast_pthread_mutex_unlock(&chan->lock);
1320 /* Handle any pending masquerades */
1322 if (ast_do_masquerade(chan)) {
1323 ast_log(LOG_WARNING, "Failed to perform masquerade\n");
1324 ast_pthread_mutex_unlock(&chan->lock);
1329 ast_pthread_mutex_unlock(&chan->lock);
1332 if (chan->generatordata) {
1333 if (chan->writeinterrupt)
1334 ast_deactivate_generator(chan);
1336 ast_pthread_mutex_unlock(&chan->lock);
1340 if (chan->fout & 0x80000000)
1341 ast_frame_dump(chan->name, fr, ">>");
1342 CHECK_BLOCKING(chan);
1343 switch(fr->frametype) {
1344 case AST_FRAME_CONTROL:
1345 /* XXX Interpret control frames XXX */
1346 ast_log(LOG_WARNING, "Don't know how to handle control frames yet\n");
1348 case AST_FRAME_DTMF:
1349 res = do_senddigit(chan,fr->subclass);
1351 case AST_FRAME_TEXT:
1352 if (chan->pvt->send_text)
1353 res = chan->pvt->send_text(chan, (char *) fr->data);
1355 case AST_FRAME_VIDEO:
1356 /* XXX Handle translation of video codecs one day XXX */
1357 if (chan->pvt->write_video)
1358 res = chan->pvt->write_video(chan, fr);
1363 if (chan->pvt->write) {
1364 if (chan->pvt->writetrans) {
1365 f = ast_translate(chan->pvt->writetrans, fr, 0);
1370 res = chan->pvt->write(chan, f);
1371 if( chan->monitor &&
1372 chan->monitor->write_stream &&
1373 f && ( f->frametype == AST_FRAME_VOICE ) ) {
1374 if( ast_writestream( chan->monitor->write_stream, f ) < 0 ) {
1375 ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n");
1386 /* Consider a write failure to force a soft hangup */
1388 chan->_softhangup |= AST_SOFTHANGUP_DEV;
1390 if ((chan->fout & 0x7fffffff) == 0x7fffffff)
1391 chan->fout &= 0x80000000;
1396 ast_pthread_mutex_unlock(&chan->lock);
1400 int ast_set_write_format(struct ast_channel *chan, int fmts)
1406 native = chan->nativeformats;
1409 res = ast_translator_best_choice(&native, &fmt);
1411 ast_log(LOG_NOTICE, "Unable to find a path from %d to %d\n", fmts, chan->nativeformats);
1415 /* Now we have a good choice for both. We'll write using our native format. */
1416 chan->pvt->rawwriteformat = native;
1417 /* User perspective is fmt */
1418 chan->writeformat = fmt;
1419 /* Free any write translation we have right now */
1420 if (chan->pvt->writetrans)
1421 ast_translator_free_path(chan->pvt->writetrans);
1422 /* Build a translation path from the user write format to the raw writing format */
1423 chan->pvt->writetrans = ast_translator_build_path(chan->pvt->rawwriteformat, chan->writeformat);
1425 ast_log(LOG_DEBUG, "Set channel %s to write format %d\n", chan->name, chan->writeformat);
1429 int ast_set_read_format(struct ast_channel *chan, int fmts)
1435 native = chan->nativeformats;
1437 /* Find a translation path from the native read format to one of the user's read formats */
1438 res = ast_translator_best_choice(&fmt, &native);
1440 ast_log(LOG_NOTICE, "Unable to find a path from %d to %d\n", chan->nativeformats, fmts);
1444 /* Now we have a good choice for both. We'll write using our native format. */
1445 chan->pvt->rawreadformat = native;
1446 /* User perspective is fmt */
1447 chan->readformat = fmt;
1448 /* Free any read translation we have right now */
1449 if (chan->pvt->readtrans)
1450 ast_translator_free_path(chan->pvt->readtrans);
1451 /* Build a translation path from the raw read format to the user reading format */
1452 chan->pvt->readtrans = ast_translator_build_path(chan->readformat, chan->pvt->rawreadformat);
1454 ast_log(LOG_DEBUG, "Set channel %s to read format %d\n", chan->name, chan->readformat);
1458 struct ast_channel *ast_request_and_dial(char *type, int format, void *data, int timeout, int *outstate, char *callerid)
1461 struct ast_channel *chan;
1462 struct ast_frame *f;
1465 chan = ast_request(type, format, data);
1468 ast_set_callerid(chan, callerid, 1);
1469 if (!ast_call(chan, data, 0)) {
1470 while(timeout && (chan->_state != AST_STATE_UP)) {
1471 res = ast_waitfor(chan, timeout);
1473 /* Something not cool, or timed out */
1478 /* If done, break out */
1485 state = AST_CONTROL_HANGUP;
1490 if (f->frametype == AST_FRAME_CONTROL) {
1491 if (f->subclass == AST_CONTROL_RINGING)
1492 state = AST_CONTROL_RINGING;
1493 else if ((f->subclass == AST_CONTROL_BUSY) || (f->subclass == AST_CONTROL_CONGESTION)) {
1494 state = f->subclass;
1497 } else if (f->subclass == AST_CONTROL_ANSWER) {
1498 state = f->subclass;
1502 ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass);
1510 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
1513 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
1514 if (chan && (chan->_state == AST_STATE_UP))
1515 state = AST_CONTROL_ANSWER;
1521 struct ast_channel *ast_request(char *type, int format, void *data)
1523 struct chanlist *chan;
1524 struct ast_channel *c = NULL;
1528 if (PTHREAD_MUTEX_LOCK(&chlock)) {
1529 ast_log(LOG_WARNING, "Unable to lock channel list\n");
1534 if (!strcasecmp(type, chan->type)) {
1535 capabilities = chan->capabilities;
1537 res = ast_translator_best_choice(&fmt, &capabilities);
1539 ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %d) to %d\n", type, chan->capabilities, format);
1540 PTHREAD_MUTEX_UNLOCK(&chlock);
1543 PTHREAD_MUTEX_UNLOCK(&chlock);
1544 if (chan->requester)
1545 c = chan->requester(type, capabilities, data);
1547 // ast_device_state_changed(c->name);
1548 manager_event(EVENT_FLAG_CALL, "Newchannel",
1553 c->name, ast_state2str(c->_state), c->callerid ? c->callerid : "<unknown>", c->uniqueid);
1560 ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
1561 PTHREAD_MUTEX_UNLOCK(&chlock);
1565 int ast_parse_device_state(char *device)
1567 char name[AST_CHANNEL_NAME] = "";
1569 struct ast_channel *chan;
1571 chan = ast_channel_walk(NULL);
1573 strncpy(name, chan->name, sizeof(name)-1);
1574 cut = strchr(name,'-');
1577 if (!strcmp(name, device))
1578 return AST_DEVICE_INUSE;
1579 chan = ast_channel_walk(chan);
1581 return AST_DEVICE_UNKNOWN;
1584 int ast_device_state(char *device)
1586 char tech[AST_MAX_EXTENSION] = "";
1588 struct chanlist *chanls;
1591 strncpy(tech, device, sizeof(tech)-1);
1592 number = strchr(tech, '/');
1594 return AST_DEVICE_INVALID;
1599 if (PTHREAD_MUTEX_LOCK(&chlock)) {
1600 ast_log(LOG_WARNING, "Unable to lock channel list\n");
1605 if (!strcasecmp(tech, chanls->type)) {
1606 PTHREAD_MUTEX_UNLOCK(&chlock);
1607 if (!chanls->devicestate)
1608 return ast_parse_device_state(device);
1610 res = chanls->devicestate(number);
1611 if (res == AST_DEVICE_UNKNOWN)
1612 return ast_parse_device_state(device);
1617 chanls = chanls->next;
1619 PTHREAD_MUTEX_UNLOCK(&chlock);
1620 return AST_DEVICE_INVALID;
1623 int ast_call(struct ast_channel *chan, char *addr, int timeout)
1625 /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
1626 If the remote end does not answer within the timeout, then do NOT hang up, but
1629 /* Stop if we're a zombie or need a soft hangup */
1630 ast_pthread_mutex_lock(&chan->lock);
1631 if (!chan->zombie && !ast_check_hangup(chan))
1632 if (chan->pvt->call)
1633 res = chan->pvt->call(chan, addr, timeout);
1634 pthread_mutex_unlock(&chan->lock);
1638 int ast_transfer(struct ast_channel *chan, char *dest)
1640 /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
1641 If the remote end does not answer within the timeout, then do NOT hang up, but
1644 /* Stop if we're a zombie or need a soft hangup */
1645 ast_pthread_mutex_lock(&chan->lock);
1646 if (!chan->zombie && !ast_check_hangup(chan)) {
1647 if (chan->pvt->transfer) {
1648 res = chan->pvt->transfer(chan, dest);
1654 pthread_mutex_unlock(&chan->lock);
1658 int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
1663 /* XXX Merge with full version? XXX */
1664 /* Stop if we're a zombie or need a soft hangup */
1665 if (c->zombie || ast_check_hangup(c))
1671 d = ast_waitstream(c, AST_DIGIT_ANY);
1675 d = ast_waitfordigit(c, to);
1677 d = ast_waitfordigit(c, to);
1685 if (!strchr(enders, d))
1687 if (strchr(enders, d) || (pos >= len)) {
1697 int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
1702 /* Stop if we're a zombie or need a soft hangup */
1703 if (c->zombie || ast_check_hangup(c))
1709 d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
1713 d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
1715 d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
1727 if (!strchr(enders, d))
1729 if (strchr(enders, d) || (pos >= len)) {
1739 int ast_channel_supports_html(struct ast_channel *chan)
1741 if (chan->pvt->send_html)
1746 int ast_channel_sendhtml(struct ast_channel *chan, int subclass, char *data, int datalen)
1748 if (chan->pvt->send_html)
1749 return chan->pvt->send_html(chan, subclass, data, datalen);
1753 int ast_channel_sendurl(struct ast_channel *chan, char *url)
1755 if (chan->pvt->send_html)
1756 return chan->pvt->send_html(chan, AST_HTML_URL, url, strlen(url) + 1);
1760 int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
1765 peerf = peer->nativeformats;
1766 chanf = chan->nativeformats;
1767 res = ast_translator_best_choice(&peerf, &chanf);
1769 ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", chan->name, chan->nativeformats, peer->name, peer->nativeformats);
1772 /* Set read format on channel */
1773 res = ast_set_read_format(chan, peerf);
1775 ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", chan->name, chanf);
1778 /* Set write format on peer channel */
1779 res = ast_set_write_format(peer, peerf);
1781 ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", peer->name, peerf);
1784 /* Now we go the other way */
1785 peerf = peer->nativeformats;
1786 chanf = chan->nativeformats;
1787 res = ast_translator_best_choice(&chanf, &peerf);
1789 ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", peer->name, peer->nativeformats, chan->name, chan->nativeformats);
1792 /* Set writeformat on channel */
1793 res = ast_set_write_format(chan, chanf);
1795 ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", chan->name, chanf);
1798 /* Set read format on peer channel */
1799 res = ast_set_read_format(peer, chanf);
1801 ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", peer->name, peerf);
1807 int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone)
1809 struct ast_frame null = { AST_FRAME_NULL, };
1810 ast_log(LOG_DEBUG, "Planning to masquerade %s into the structure of %s\n",
1811 clone->name, original->name);
1812 if (original->masq) {
1813 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
1814 original->masq->name, original->name);
1818 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
1819 clone->name, clone->masqr->name);
1822 original->masq = clone;
1823 clone->masqr = original;
1824 /* XXX can't really hold the lock here, but at the same time, it' s
1825 not really safe not to XXX */
1826 ast_queue_frame(original, &null, 0);
1827 ast_queue_frame(clone, &null, 0);
1828 ast_log(LOG_DEBUG, "Done planning to masquerade %s into the structure of %s\n", original->name, clone->name);
1832 void ast_change_name(struct ast_channel *chan, char *newname)
1835 strncpy(tmp, chan->name, 256);
1836 strncpy(chan->name, newname, sizeof(chan->name) - 1);
1837 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", tmp, chan->name, chan->uniqueid);
1840 static int ast_do_masquerade(struct ast_channel *original)
1846 struct ast_channel_pvt *p;
1847 struct ast_channel *clone = original->masq;
1848 int rformat = original->readformat;
1849 int wformat = original->writeformat;
1856 ast_log(LOG_DEBUG, "Actually Masquerading %s(%d) into the structure of %s(%d)\n",
1857 clone->name, clone->_state, original->name, original->_state);
1859 /* XXX This is a seriously wacked out operation. We're essentially putting the guts of
1860 the clone channel into the original channel. Start by killing off the original
1861 channel's backend. I'm not sure we're going to keep this function, because
1862 while the features are nice, the cost is very high in terms of pure nastiness. XXX */
1864 /* Having remembered the original read/write formats, we turn off any translation on either
1866 free_translation(clone);
1867 free_translation(original);
1869 /* We need the clone's lock, too */
1870 ast_pthread_mutex_lock(&clone->lock);
1872 /* Unlink the masquerade */
1873 original->masq = NULL;
1874 clone->masqr = NULL;
1876 /* Save the original name */
1877 strncpy(orig, original->name, sizeof(orig) - 1);
1878 /* Save the new name */
1879 strncpy(newn, clone->name, sizeof(newn) - 1);
1880 /* Create the masq name */
1881 snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn);
1883 /* Copy the name from the clone channel */
1884 strncpy(original->name, newn, sizeof(original->name)-1);
1886 /* Mangle the name of the clone channel */
1887 strncpy(clone->name, masqn, sizeof(clone->name) - 1);
1889 /* Notify any managers of the change, first the masq then the other */
1890 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\n", newn, masqn);
1891 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\n", orig, newn);
1895 original->pvt = clone->pvt;
1898 clone->_softhangup = AST_SOFTHANGUP_DEV;
1901 if (clone->pvt->fixup){
1902 res = clone->pvt->fixup(original, clone);
1904 ast_log(LOG_WARNING, "Fixup failed on channel %s, strange things may happen.\n", clone->name);
1907 /* Start by disconnecting the original's physical side */
1908 if (clone->pvt->hangup)
1909 res = clone->pvt->hangup(clone);
1911 ast_log(LOG_WARNING, "Hangup failed! Strange things may happen!\n");
1912 pthread_mutex_unlock(&clone->lock);
1916 snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig);
1917 /* Mangle the name of the clone channel */
1918 strncpy(clone->name, zombn, sizeof(clone->name) - 1);
1919 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\n", masqn, zombn);
1921 /* Keep the same language. */
1922 /* Update the type. */
1923 original->type = clone->type;
1925 for (x=0;x<AST_MAX_FDS;x++) {
1926 original->fds[x] = clone->fds[x];
1928 /* Move the variables */
1929 tmpv = original->varshead.first;
1930 original->varshead.first = clone->varshead.first;
1931 clone->varshead.first = tmpv;
1932 /* Presense of ADSI capable CPE follows clone */
1933 original->adsicpe = clone->adsicpe;
1934 /* Bridge remains the same */
1935 /* CDR fields remain the same */
1936 /* XXX What about blocking, softhangup, blocker, and lock and blockproc? XXX */
1937 /* Application and data remain the same */
1938 /* Clone exception becomes real one, as with fdno */
1939 original->exception = clone->exception;
1940 original->fdno = clone->fdno;
1941 /* Schedule context remains the same */
1942 /* Stream stuff stays the same */
1943 /* Keep the original state. The fixup code will need to work with it most likely */
1945 /* dnid and callerid change to become the new, HOWEVER, we also link the original's
1946 fields back into the defunct 'clone' so that they will be freed when
1947 ast_frfree is eventually called */
1948 tmp = original->dnid;
1949 original->dnid = clone->dnid;
1952 tmp = original->callerid;
1953 original->callerid = clone->callerid;
1954 clone->callerid = tmp;
1956 /* Restore original timing file descriptor */
1957 original->fds[AST_MAX_FDS - 2] = original->timingfd;
1959 /* Our native formats are different now */
1960 original->nativeformats = clone->nativeformats;
1962 /* And of course, so does our current state. Note we need not
1963 call ast_setstate since the event manager doesn't really consider
1965 original->_state = clone->_state;
1967 /* Context, extension, priority, app data, jump table, remain the same */
1968 /* pvt switches. pbx stays the same, as does next */
1970 /* Now, at this point, the "clone" channel is totally F'd up. We mark it as
1971 a zombie so nothing tries to touch it. If it's already been marked as a
1972 zombie, then free it now (since it already is considered invalid). */
1973 if (clone->zombie) {
1974 pthread_mutex_unlock(&clone->lock);
1975 ast_channel_free(clone);
1976 manager_event(EVENT_FLAG_CALL, "Hangup", "Channel: %s\r\n", zombn);
1979 pthread_mutex_unlock(&clone->lock);
1981 /* Set the write format */
1982 ast_set_write_format(original, wformat);
1984 /* Set the read format */
1985 ast_set_read_format(original, rformat);
1987 ast_log(LOG_DEBUG, "Putting channel %s in %d/%d formats\n", original->name, wformat, rformat);
1989 /* Okay. Last thing is to let the channel driver know about all this mess, so he
1990 can fix up everything as best as possible */
1991 if (original->pvt->fixup) {
1992 res = original->pvt->fixup(clone, original);
1994 ast_log(LOG_WARNING, "Driver for '%s' could not fixup channel %s\n",
1995 original->type, original->name);
1999 ast_log(LOG_WARNING, "Driver '%s' does not have a fixup routine (for %s)! Bad things may happen.\n",
2000 original->type, original->name);
2001 /* Signal any blocker */
2002 if (original->blocking)
2003 pthread_kill(original->blocker, SIGURG);
2007 void ast_set_callerid(struct ast_channel *chan, char *callerid, int anitoo)
2010 free(chan->callerid);
2011 if (anitoo && chan->ani)
2014 chan->callerid = strdup(callerid);
2016 chan->ani = strdup(callerid);
2018 chan->callerid = NULL;
2023 ast_cdr_setcid(chan->cdr, chan);
2024 manager_event(EVENT_FLAG_CALL, "Newcallerid",
2028 chan->name, chan->callerid ?
2029 chan->callerid : "<Unknown>",
2033 int ast_setstate(struct ast_channel *chan, int state)
2035 if (chan->_state != state) {
2036 int oldstate = chan->_state;
2037 chan->_state = state;
2038 if (oldstate == AST_STATE_DOWN) {
2039 ast_device_state_changed(chan->name);
2040 manager_event(EVENT_FLAG_CALL, "Newchannel",
2045 chan->name, ast_state2str(chan->_state), chan->callerid ? chan->callerid : "<unknown>", chan->uniqueid);
2047 manager_event(EVENT_FLAG_CALL, "Newstate",
2052 chan->name, ast_state2str(chan->_state), chan->callerid ? chan->callerid : "<unknown>", chan->uniqueid);
2058 int ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2060 /* Copy voice back and forth between the two channels. Give the peer
2061 the ability to transfer calls with '#<extension' syntax. */
2062 struct ast_channel *cs[3];
2064 struct ast_frame *f;
2065 struct ast_channel *who = NULL;
2069 /* Stop if we're a zombie or need a soft hangup */
2070 if (c0->zombie || ast_check_hangup(c0) || c1->zombie || ast_check_hangup(c1))
2073 ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
2074 c0->name, c0->bridge->name);
2078 ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
2079 c1->name, c1->bridge->name);
2083 /* Keep track of bridge */
2089 manager_event(EVENT_FLAG_CALL, "Link",
2092 c0->name, c1->name);
2094 for (/* ever */;;) {
2095 /* Stop if we're a zombie or need a soft hangup */
2096 if (c0->zombie || ast_check_hangup(c0) || c1->zombie || ast_check_hangup(c1)) {
2100 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,c0->zombie?"Yes":"No",ast_check_hangup(c0)?"Yes":"No",c1->zombie?"Yes":"No",ast_check_hangup(c1)?"Yes":"No");
2103 if (c0->pvt->bridge &&
2104 (c0->pvt->bridge == c1->pvt->bridge) && !nativefailed && !c0->monitor && !c1->monitor) {
2105 /* Looks like they share a bridge code */
2106 if (option_verbose > 2)
2107 ast_verbose(VERBOSE_PREFIX_3 "Attempting native bridge of %s and %s\n", c0->name, c1->name);
2108 if (!(res = c0->pvt->bridge(c0, c1, flags, fo, rc))) {
2111 manager_event(EVENT_FLAG_CALL, "Unlink",
2114 c0->name, c1->name);
2115 ast_log(LOG_DEBUG, "Returning from native bridge, channels: %s, %s\n",c0->name ,c1->name);
2118 /* If they return non-zero then continue on normally. Let "-2" mean don't worry about
2119 my not wanting to bridge */
2120 if ((res != -2) && (res != -3))
2121 ast_log(LOG_WARNING, "Private bridge between %s and %s failed\n", c0->name, c1->name);
2122 if (res != -3) nativefailed++;
2126 if (((c0->writeformat != c1->readformat) || (c0->readformat != c1->writeformat)) &&
2127 !(c0->generator || c1->generator)) {
2128 if (ast_channel_make_compatible(c0, c1)) {
2129 ast_log(LOG_WARNING, "Can't make %s and %s compatible\n", c0->name, c1->name);
2130 manager_event(EVENT_FLAG_CALL, "Unlink",
2133 c0->name, c1->name);
2137 who = ast_waitfor_n(cs, 2, &to);
2139 ast_log(LOG_DEBUG, "Nobody there, continuing...\n");
2147 ast_log(LOG_DEBUG, "Didn't get a frame from channel: %s\n",who->name);
2151 if ((f->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
2155 ast_log(LOG_DEBUG, "Got a FRAME_CONTROL frame on channel %s\n",who->name);
2158 if ((f->frametype == AST_FRAME_VOICE) ||
2159 (f->frametype == AST_FRAME_TEXT) ||
2160 (f->frametype == AST_FRAME_VIDEO) ||
2161 (f->frametype == AST_FRAME_IMAGE) ||
2162 (f->frametype == AST_FRAME_DTMF)) {
2163 if ((f->frametype == AST_FRAME_DTMF) &&
2164 (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))) {
2166 if ((flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
2169 /* Take out of conference mode */
2171 ast_log(LOG_DEBUG, "Got AST_BRIDGE_DTMF_CHANNEL_0 on c0 (%s)\n",c0->name);
2177 if (flags & AST_BRIDGE_DTMF_CHANNEL_1) {
2181 ast_log(LOG_DEBUG, "Got AST_BRIDGE_DTMF_CHANNEL_1 on c1 (%s)\n",c1->name);
2188 ast_log(LOG_DEBUG, "Read from %s\n", who->name);
2190 ast_log(LOG_DEBUG, "Servicing channel %s twice in a row?\n", last->name);
2194 /* Don't copy packets if there is a generator on either one, since they're
2195 not supposed to be listening anyway */
2204 /* Swap who gets priority */
2211 manager_event(EVENT_FLAG_CALL, "Unlink",
2214 c0->name, c1->name);
2215 ast_log(LOG_DEBUG, "Bridge stops bridging channels %s and %s\n",c0->name,c1->name);
2219 int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
2222 if (chan->pvt->setoption) {
2223 res = chan->pvt->setoption(chan, option, data, datalen);
2231 /* XXX Implement blocking -- just wait for our option frame reply, discarding
2232 intermediate packets. XXX */
2233 ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
2239 struct tonepair_def {
2246 struct tonepair_state {
2254 unsigned char offset[AST_FRIENDLY_OFFSET];
2258 static void tonepair_release(struct ast_channel *chan, void *params)
2260 struct tonepair_state *ts = params;
2262 ast_set_write_format(chan, ts->origwfmt);
2267 static void * tonepair_alloc(struct ast_channel *chan, void *params)
2269 struct tonepair_state *ts;
2270 struct tonepair_def *td = params;
2271 ts = malloc(sizeof(struct tonepair_state));
2274 memset(ts, 0, sizeof(struct tonepair_state));
2275 ts->origwfmt = chan->writeformat;
2276 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
2277 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
2278 tonepair_release(NULL, ts);
2281 ts->freq1 = td->freq1;
2282 ts->freq2 = td->freq2;
2283 ts->duration = td->duration;
2286 /* Let interrupts interrupt :) */
2287 chan->writeinterrupt = 1;
2291 static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
2293 struct tonepair_state *ts = data;
2296 /* we need to prepare a frame with 16 * timelen samples as we're
2297 * generating SLIN audio
2301 if (len > sizeof(ts->data) / 2 - 1) {
2302 ast_log(LOG_WARNING, "Can't generate that much data!\n");
2305 memset(&ts->f, 0, sizeof(ts->f));
2306 for (x=0;x<len/2;x++) {
2307 ts->data[x] = ts->vol * (
2308 sin((ts->freq1 * 2.0 * M_PI / 8000.0) * (ts->pos + x)) +
2309 sin((ts->freq2 * 2.0 * M_PI / 8000.0) * (ts->pos + x))
2312 ts->f.frametype = AST_FRAME_VOICE;
2313 ts->f.subclass = AST_FORMAT_SLINEAR;
2314 ts->f.datalen = len;
2315 ts->f.samples = samples;
2316 ts->f.offset = AST_FRIENDLY_OFFSET;
2317 ts->f.data = ts->data;
2318 ast_write(chan, &ts->f);
2320 if (ts->duration > 0) {
2321 if (ts->pos >= ts->duration * 8)
2327 static struct ast_generator tonepair = {
2328 alloc: tonepair_alloc,
2329 release: tonepair_release,
2330 generate: tonepair_generator,
2333 int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
2335 struct tonepair_def d = { 0, };
2338 d.duration = duration;
2343 if (ast_activate_generator(chan, &tonepair, &d))
2348 void ast_tonepair_stop(struct ast_channel *chan)
2350 ast_deactivate_generator(chan);
2353 int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
2355 struct ast_frame *f;
2357 if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
2360 /* Give us some wiggle room */
2361 while(chan->generatordata && (ast_waitfor(chan, 100) >= 0)) {