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;
1496 } else if (f->subclass == AST_CONTROL_ANSWER) {
1497 state = f->subclass;
1500 ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass);
1508 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
1511 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
1512 if (chan && (chan->_state == AST_STATE_UP))
1513 state = AST_CONTROL_ANSWER;
1519 struct ast_channel *ast_request(char *type, int format, void *data)
1521 struct chanlist *chan;
1522 struct ast_channel *c = NULL;
1526 if (PTHREAD_MUTEX_LOCK(&chlock)) {
1527 ast_log(LOG_WARNING, "Unable to lock channel list\n");
1532 if (!strcasecmp(type, chan->type)) {
1533 capabilities = chan->capabilities;
1535 res = ast_translator_best_choice(&fmt, &capabilities);
1537 ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %d) to %d\n", type, chan->capabilities, format);
1538 PTHREAD_MUTEX_UNLOCK(&chlock);
1541 PTHREAD_MUTEX_UNLOCK(&chlock);
1542 if (chan->requester)
1543 c = chan->requester(type, capabilities, data);
1545 // ast_device_state_changed(c->name);
1546 manager_event(EVENT_FLAG_CALL, "Newchannel",
1551 c->name, ast_state2str(c->_state), c->callerid ? c->callerid : "<unknown>", c->uniqueid);
1558 ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
1559 PTHREAD_MUTEX_UNLOCK(&chlock);
1563 int ast_parse_device_state(char *device)
1565 char name[AST_CHANNEL_NAME] = "";
1567 struct ast_channel *chan;
1569 chan = ast_channel_walk(NULL);
1571 strncpy(name, chan->name, sizeof(name)-1);
1572 cut = strchr(name,'-');
1575 if (!strcmp(name, device))
1576 return AST_DEVICE_INUSE;
1577 chan = ast_channel_walk(chan);
1579 return AST_DEVICE_UNKNOWN;
1582 int ast_device_state(char *device)
1584 char tech[AST_MAX_EXTENSION] = "";
1586 struct chanlist *chanls;
1589 strncpy(tech, device, sizeof(tech)-1);
1590 number = strchr(tech, '/');
1592 return AST_DEVICE_INVALID;
1597 if (PTHREAD_MUTEX_LOCK(&chlock)) {
1598 ast_log(LOG_WARNING, "Unable to lock channel list\n");
1603 if (!strcasecmp(tech, chanls->type)) {
1604 PTHREAD_MUTEX_UNLOCK(&chlock);
1605 if (!chanls->devicestate)
1606 return ast_parse_device_state(device);
1608 res = chanls->devicestate(number);
1609 if (res == AST_DEVICE_UNKNOWN)
1610 return ast_parse_device_state(device);
1615 chanls = chanls->next;
1617 PTHREAD_MUTEX_UNLOCK(&chlock);
1618 return AST_DEVICE_INVALID;
1621 int ast_call(struct ast_channel *chan, char *addr, int timeout)
1623 /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
1624 If the remote end does not answer within the timeout, then do NOT hang up, but
1627 /* Stop if we're a zombie or need a soft hangup */
1628 ast_pthread_mutex_lock(&chan->lock);
1629 if (!chan->zombie && !ast_check_hangup(chan))
1630 if (chan->pvt->call)
1631 res = chan->pvt->call(chan, addr, timeout);
1632 pthread_mutex_unlock(&chan->lock);
1636 int ast_transfer(struct ast_channel *chan, char *dest)
1638 /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
1639 If the remote end does not answer within the timeout, then do NOT hang up, but
1642 /* Stop if we're a zombie or need a soft hangup */
1643 ast_pthread_mutex_lock(&chan->lock);
1644 if (!chan->zombie && !ast_check_hangup(chan)) {
1645 if (chan->pvt->transfer) {
1646 res = chan->pvt->transfer(chan, dest);
1652 pthread_mutex_unlock(&chan->lock);
1656 int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
1661 /* XXX Merge with full version? XXX */
1662 /* Stop if we're a zombie or need a soft hangup */
1663 if (c->zombie || ast_check_hangup(c))
1669 d = ast_waitstream(c, AST_DIGIT_ANY);
1673 d = ast_waitfordigit(c, to);
1675 d = ast_waitfordigit(c, to);
1683 if (!strchr(enders, d))
1685 if (strchr(enders, d) || (pos >= len)) {
1695 int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
1700 /* Stop if we're a zombie or need a soft hangup */
1701 if (c->zombie || ast_check_hangup(c))
1707 d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
1711 d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
1713 d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
1725 if (!strchr(enders, d))
1727 if (strchr(enders, d) || (pos >= len)) {
1737 int ast_channel_supports_html(struct ast_channel *chan)
1739 if (chan->pvt->send_html)
1744 int ast_channel_sendhtml(struct ast_channel *chan, int subclass, char *data, int datalen)
1746 if (chan->pvt->send_html)
1747 return chan->pvt->send_html(chan, subclass, data, datalen);
1751 int ast_channel_sendurl(struct ast_channel *chan, char *url)
1753 if (chan->pvt->send_html)
1754 return chan->pvt->send_html(chan, AST_HTML_URL, url, strlen(url) + 1);
1758 int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
1763 peerf = peer->nativeformats;
1764 chanf = chan->nativeformats;
1765 res = ast_translator_best_choice(&peerf, &chanf);
1767 ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", chan->name, chan->nativeformats, peer->name, peer->nativeformats);
1770 /* Set read format on channel */
1771 res = ast_set_read_format(chan, peerf);
1773 ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", chan->name, chanf);
1776 /* Set write format on peer channel */
1777 res = ast_set_write_format(peer, peerf);
1779 ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", peer->name, peerf);
1782 /* Now we go the other way */
1783 peerf = peer->nativeformats;
1784 chanf = chan->nativeformats;
1785 res = ast_translator_best_choice(&chanf, &peerf);
1787 ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", peer->name, peer->nativeformats, chan->name, chan->nativeformats);
1790 /* Set writeformat on channel */
1791 res = ast_set_write_format(chan, chanf);
1793 ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", chan->name, chanf);
1796 /* Set read format on peer channel */
1797 res = ast_set_read_format(peer, chanf);
1799 ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", peer->name, peerf);
1805 int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone)
1807 struct ast_frame null = { AST_FRAME_NULL, };
1808 ast_log(LOG_DEBUG, "Planning to masquerade %s into the structure of %s\n",
1809 clone->name, original->name);
1810 if (original->masq) {
1811 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
1812 original->masq->name, original->name);
1816 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
1817 clone->name, clone->masqr->name);
1820 original->masq = clone;
1821 clone->masqr = original;
1822 /* XXX can't really hold the lock here, but at the same time, it' s
1823 not really safe not to XXX */
1824 ast_queue_frame(original, &null, 0);
1825 ast_queue_frame(clone, &null, 0);
1826 ast_log(LOG_DEBUG, "Done planning to masquerade %s into the structure of %s\n", original->name, clone->name);
1830 void ast_change_name(struct ast_channel *chan, char *newname)
1833 strncpy(tmp, chan->name, 256);
1834 strncpy(chan->name, newname, sizeof(chan->name) - 1);
1835 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", tmp, chan->name, chan->uniqueid);
1838 static int ast_do_masquerade(struct ast_channel *original)
1844 struct ast_channel_pvt *p;
1845 struct ast_channel *clone = original->masq;
1846 int rformat = original->readformat;
1847 int wformat = original->writeformat;
1854 ast_log(LOG_DEBUG, "Actually Masquerading %s(%d) into the structure of %s(%d)\n",
1855 clone->name, clone->_state, original->name, original->_state);
1857 /* XXX This is a seriously wacked out operation. We're essentially putting the guts of
1858 the clone channel into the original channel. Start by killing off the original
1859 channel's backend. I'm not sure we're going to keep this function, because
1860 while the features are nice, the cost is very high in terms of pure nastiness. XXX */
1862 /* Having remembered the original read/write formats, we turn off any translation on either
1864 free_translation(clone);
1865 free_translation(original);
1867 /* We need the clone's lock, too */
1868 ast_pthread_mutex_lock(&clone->lock);
1870 /* Unlink the masquerade */
1871 original->masq = NULL;
1872 clone->masqr = NULL;
1874 /* Save the original name */
1875 strncpy(orig, original->name, sizeof(orig) - 1);
1876 /* Save the new name */
1877 strncpy(newn, clone->name, sizeof(newn) - 1);
1878 /* Create the masq name */
1879 snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn);
1881 /* Copy the name from the clone channel */
1882 strncpy(original->name, newn, sizeof(original->name)-1);
1884 /* Mangle the name of the clone channel */
1885 strncpy(clone->name, masqn, sizeof(clone->name) - 1);
1887 /* Notify any managers of the change, first the masq then the other */
1888 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\n", newn, masqn);
1889 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\n", orig, newn);
1893 original->pvt = clone->pvt;
1896 clone->_softhangup = AST_SOFTHANGUP_DEV;
1899 if (clone->pvt->fixup){
1900 res = clone->pvt->fixup(original, clone);
1902 ast_log(LOG_WARNING, "Fixup failed on channel %s, strange things may happen.\n", clone->name);
1905 /* Start by disconnecting the original's physical side */
1906 if (clone->pvt->hangup)
1907 res = clone->pvt->hangup(clone);
1909 ast_log(LOG_WARNING, "Hangup failed! Strange things may happen!\n");
1910 pthread_mutex_unlock(&clone->lock);
1914 snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig);
1915 /* Mangle the name of the clone channel */
1916 strncpy(clone->name, zombn, sizeof(clone->name) - 1);
1917 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\n", masqn, zombn);
1919 /* Keep the same language. */
1920 /* Update the type. */
1921 original->type = clone->type;
1923 for (x=0;x<AST_MAX_FDS;x++) {
1924 original->fds[x] = clone->fds[x];
1926 /* Move the variables */
1927 tmpv = original->varshead.first;
1928 original->varshead.first = clone->varshead.first;
1929 clone->varshead.first = tmpv;
1930 /* Presense of ADSI capable CPE follows clone */
1931 original->adsicpe = clone->adsicpe;
1932 /* Bridge remains the same */
1933 /* CDR fields remain the same */
1934 /* XXX What about blocking, softhangup, blocker, and lock and blockproc? XXX */
1935 /* Application and data remain the same */
1936 /* Clone exception becomes real one, as with fdno */
1937 original->exception = clone->exception;
1938 original->fdno = clone->fdno;
1939 /* Schedule context remains the same */
1940 /* Stream stuff stays the same */
1941 /* Keep the original state. The fixup code will need to work with it most likely */
1943 /* dnid and callerid change to become the new, HOWEVER, we also link the original's
1944 fields back into the defunct 'clone' so that they will be freed when
1945 ast_frfree is eventually called */
1946 tmp = original->dnid;
1947 original->dnid = clone->dnid;
1950 tmp = original->callerid;
1951 original->callerid = clone->callerid;
1952 clone->callerid = tmp;
1954 /* Restore original timing file descriptor */
1955 original->fds[AST_MAX_FDS - 2] = original->timingfd;
1957 /* Our native formats are different now */
1958 original->nativeformats = clone->nativeformats;
1960 /* And of course, so does our current state. Note we need not
1961 call ast_setstate since the event manager doesn't really consider
1963 original->_state = clone->_state;
1965 /* Context, extension, priority, app data, jump table, remain the same */
1966 /* pvt switches. pbx stays the same, as does next */
1968 /* Now, at this point, the "clone" channel is totally F'd up. We mark it as
1969 a zombie so nothing tries to touch it. If it's already been marked as a
1970 zombie, then free it now (since it already is considered invalid). */
1971 if (clone->zombie) {
1972 pthread_mutex_unlock(&clone->lock);
1973 ast_channel_free(clone);
1974 manager_event(EVENT_FLAG_CALL, "Hangup", "Channel: %s\r\n", zombn);
1977 pthread_mutex_unlock(&clone->lock);
1979 /* Set the write format */
1980 ast_set_write_format(original, wformat);
1982 /* Set the read format */
1983 ast_set_read_format(original, rformat);
1985 ast_log(LOG_DEBUG, "Putting channel %s in %d/%d formats\n", original->name, wformat, rformat);
1987 /* Okay. Last thing is to let the channel driver know about all this mess, so he
1988 can fix up everything as best as possible */
1989 if (original->pvt->fixup) {
1990 res = original->pvt->fixup(clone, original);
1992 ast_log(LOG_WARNING, "Driver for '%s' could not fixup channel %s\n",
1993 original->type, original->name);
1997 ast_log(LOG_WARNING, "Driver '%s' does not have a fixup routine (for %s)! Bad things may happen.\n",
1998 original->type, original->name);
1999 /* Signal any blocker */
2000 if (original->blocking)
2001 pthread_kill(original->blocker, SIGURG);
2005 void ast_set_callerid(struct ast_channel *chan, char *callerid, int anitoo)
2008 free(chan->callerid);
2009 if (anitoo && chan->ani)
2012 chan->callerid = strdup(callerid);
2014 chan->ani = strdup(callerid);
2016 chan->callerid = NULL;
2021 ast_cdr_setcid(chan->cdr, chan);
2022 manager_event(EVENT_FLAG_CALL, "Newcallerid",
2026 chan->name, chan->callerid ?
2027 chan->callerid : "<Unknown>",
2031 int ast_setstate(struct ast_channel *chan, int state)
2033 if (chan->_state != state) {
2034 int oldstate = chan->_state;
2035 chan->_state = state;
2036 if (oldstate == AST_STATE_DOWN) {
2037 ast_device_state_changed(chan->name);
2038 manager_event(EVENT_FLAG_CALL, "Newchannel",
2043 chan->name, ast_state2str(chan->_state), chan->callerid ? chan->callerid : "<unknown>", chan->uniqueid);
2045 manager_event(EVENT_FLAG_CALL, "Newstate",
2050 chan->name, ast_state2str(chan->_state), chan->callerid ? chan->callerid : "<unknown>", chan->uniqueid);
2056 int ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2058 /* Copy voice back and forth between the two channels. Give the peer
2059 the ability to transfer calls with '#<extension' syntax. */
2060 struct ast_channel *cs[3];
2062 struct ast_frame *f;
2063 struct ast_channel *who = NULL;
2067 /* Stop if we're a zombie or need a soft hangup */
2068 if (c0->zombie || ast_check_hangup(c0) || c1->zombie || ast_check_hangup(c1))
2071 ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
2072 c0->name, c0->bridge->name);
2076 ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
2077 c1->name, c1->bridge->name);
2081 /* Keep track of bridge */
2087 manager_event(EVENT_FLAG_CALL, "Link",
2090 c0->name, c1->name);
2092 for (/* ever */;;) {
2093 /* Stop if we're a zombie or need a soft hangup */
2094 if (c0->zombie || ast_check_hangup(c0) || c1->zombie || ast_check_hangup(c1)) {
2098 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");
2101 if (c0->pvt->bridge &&
2102 (c0->pvt->bridge == c1->pvt->bridge) && !nativefailed && !c0->monitor && !c1->monitor) {
2103 /* Looks like they share a bridge code */
2104 if (option_verbose > 2)
2105 ast_verbose(VERBOSE_PREFIX_3 "Attempting native bridge of %s and %s\n", c0->name, c1->name);
2106 if (!(res = c0->pvt->bridge(c0, c1, flags, fo, rc))) {
2109 manager_event(EVENT_FLAG_CALL, "Unlink",
2112 c0->name, c1->name);
2113 ast_log(LOG_DEBUG, "Returning from native bridge, channels: %s, %s\n",c0->name ,c1->name);
2116 /* If they return non-zero then continue on normally. Let "-2" mean don't worry about
2117 my not wanting to bridge */
2118 if ((res != -2) && (res != -3))
2119 ast_log(LOG_WARNING, "Private bridge between %s and %s failed\n", c0->name, c1->name);
2120 if (res != -3) nativefailed++;
2124 if (((c0->writeformat != c1->readformat) || (c0->readformat != c1->writeformat)) &&
2125 !(c0->generator || c1->generator)) {
2126 if (ast_channel_make_compatible(c0, c1)) {
2127 ast_log(LOG_WARNING, "Can't make %s and %s compatible\n", c0->name, c1->name);
2128 manager_event(EVENT_FLAG_CALL, "Unlink",
2131 c0->name, c1->name);
2135 who = ast_waitfor_n(cs, 2, &to);
2137 ast_log(LOG_DEBUG, "Nobody there, continuing...\n");
2145 ast_log(LOG_DEBUG, "Didn't get a frame from channel: %s\n",who->name);
2149 if ((f->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
2153 ast_log(LOG_DEBUG, "Got a FRAME_CONTROL frame on channel %s\n",who->name);
2156 if ((f->frametype == AST_FRAME_VOICE) ||
2157 (f->frametype == AST_FRAME_TEXT) ||
2158 (f->frametype == AST_FRAME_VIDEO) ||
2159 (f->frametype == AST_FRAME_IMAGE) ||
2160 (f->frametype == AST_FRAME_DTMF)) {
2161 if ((f->frametype == AST_FRAME_DTMF) &&
2162 (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))) {
2164 if ((flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
2167 /* Take out of conference mode */
2169 ast_log(LOG_DEBUG, "Got AST_BRIDGE_DTMF_CHANNEL_0 on c0 (%s)\n",c0->name);
2175 if (flags & AST_BRIDGE_DTMF_CHANNEL_1) {
2179 ast_log(LOG_DEBUG, "Got AST_BRIDGE_DTMF_CHANNEL_1 on c1 (%s)\n",c1->name);
2186 ast_log(LOG_DEBUG, "Read from %s\n", who->name);
2188 ast_log(LOG_DEBUG, "Servicing channel %s twice in a row?\n", last->name);
2192 /* Don't copy packets if there is a generator on either one, since they're
2193 not supposed to be listening anyway */
2202 /* Swap who gets priority */
2209 manager_event(EVENT_FLAG_CALL, "Unlink",
2212 c0->name, c1->name);
2213 ast_log(LOG_DEBUG, "Bridge stops bridging channels %s and %s\n",c0->name,c1->name);
2217 int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
2220 if (chan->pvt->setoption) {
2221 res = chan->pvt->setoption(chan, option, data, datalen);
2229 /* XXX Implement blocking -- just wait for our option frame reply, discarding
2230 intermediate packets. XXX */
2231 ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
2237 struct tonepair_def {
2244 struct tonepair_state {
2252 unsigned char offset[AST_FRIENDLY_OFFSET];
2256 static void tonepair_release(struct ast_channel *chan, void *params)
2258 struct tonepair_state *ts = params;
2260 ast_set_write_format(chan, ts->origwfmt);
2265 static void * tonepair_alloc(struct ast_channel *chan, void *params)
2267 struct tonepair_state *ts;
2268 struct tonepair_def *td = params;
2269 ts = malloc(sizeof(struct tonepair_state));
2272 memset(ts, 0, sizeof(struct tonepair_state));
2273 ts->origwfmt = chan->writeformat;
2274 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
2275 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
2276 tonepair_release(NULL, ts);
2279 ts->freq1 = td->freq1;
2280 ts->freq2 = td->freq2;
2281 ts->duration = td->duration;
2284 /* Let interrupts interrupt :) */
2285 chan->writeinterrupt = 1;
2289 static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
2291 struct tonepair_state *ts = data;
2294 /* we need to prepare a frame with 16 * timelen samples as we're
2295 * generating SLIN audio
2299 if (len > sizeof(ts->data) / 2 - 1) {
2300 ast_log(LOG_WARNING, "Can't generate that much data!\n");
2303 memset(&ts->f, 0, sizeof(ts->f));
2304 for (x=0;x<len/2;x++) {
2305 ts->data[x] = ts->vol * (
2306 sin((ts->freq1 * 2.0 * M_PI / 8000.0) * (ts->pos + x)) +
2307 sin((ts->freq2 * 2.0 * M_PI / 8000.0) * (ts->pos + x))
2310 ts->f.frametype = AST_FRAME_VOICE;
2311 ts->f.subclass = AST_FORMAT_SLINEAR;
2312 ts->f.datalen = len;
2313 ts->f.samples = samples;
2314 ts->f.offset = AST_FRIENDLY_OFFSET;
2315 ts->f.data = ts->data;
2316 ast_write(chan, &ts->f);
2318 if (ts->duration > 0) {
2319 if (ts->pos >= ts->duration * 8)
2325 static struct ast_generator tonepair = {
2326 alloc: tonepair_alloc,
2327 release: tonepair_release,
2328 generate: tonepair_generator,
2331 int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
2333 struct tonepair_def d = { 0, };
2336 d.duration = duration;
2341 if (ast_activate_generator(chan, &tonepair, &d))
2346 void ast_tonepair_stop(struct ast_channel *chan)
2348 ast_deactivate_generator(chan);
2351 int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
2353 struct ast_frame *f;
2355 if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
2358 /* Give us some wiggle room */
2359 while(chan->generatordata && (ast_waitfor(chan, 100) >= 0)) {