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 static int ast_check_hangup_locked(struct ast_channel *chan)
102 ast_pthread_mutex_lock(&chan->lock);
103 res = ast_check_hangup(chan);
104 ast_pthread_mutex_unlock(&chan->lock);
108 void ast_begin_shutdown(int hangup)
110 struct ast_channel *c;
113 PTHREAD_MUTEX_LOCK(&chlock);
116 ast_softhangup(c, AST_SOFTHANGUP_SHUTDOWN);
119 PTHREAD_MUTEX_UNLOCK(&chlock);
123 int ast_active_channels(void)
125 struct ast_channel *c;
127 PTHREAD_MUTEX_LOCK(&chlock);
133 PTHREAD_MUTEX_UNLOCK(&chlock);
137 void ast_cancel_shutdown(void)
142 int ast_shutting_down(void)
144 return shutting_down;
147 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
153 chan->whentohangup = myt + offset;
155 chan->whentohangup = 0;
159 int ast_channel_register(char *type, char *description, int capabilities,
160 struct ast_channel *(*requester)(char *type, int format, void *data))
162 return ast_channel_register_ex(type, description, capabilities, requester, NULL);
165 int ast_channel_register_ex(char *type, char *description, int capabilities,
166 struct ast_channel *(*requester)(char *type, int format, void *data),
167 int (*devicestate)(void *data))
169 struct chanlist *chan, *last=NULL;
170 if (PTHREAD_MUTEX_LOCK(&chlock)) {
171 ast_log(LOG_WARNING, "Unable to lock channel list\n");
176 if (!strcasecmp(type, chan->type)) {
177 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", type);
178 PTHREAD_MUTEX_UNLOCK(&chlock);
184 chan = malloc(sizeof(struct chanlist));
186 ast_log(LOG_WARNING, "Out of memory\n");
187 PTHREAD_MUTEX_UNLOCK(&chlock);
190 strncpy(chan->type, type, sizeof(chan->type)-1);
191 strncpy(chan->description, description, sizeof(chan->description)-1);
192 chan->capabilities = capabilities;
193 chan->requester = requester;
194 chan->devicestate = devicestate;
201 ast_log(LOG_DEBUG, "Registered handler for '%s' (%s)\n", chan->type, chan->description);
202 else if (option_verbose > 1)
203 ast_verbose( VERBOSE_PREFIX_2 "Registered channel type '%s' (%s)\n", chan->type, chan->description);
204 PTHREAD_MUTEX_UNLOCK(&chlock);
208 char *ast_state2str(int state)
210 /* XXX Not reentrant XXX */
211 static char localtmp[256];
215 case AST_STATE_RESERVED:
217 case AST_STATE_OFFHOOK:
219 case AST_STATE_DIALING:
223 case AST_STATE_RINGING:
230 snprintf(localtmp, sizeof(localtmp), "Unknown (%d)\n", state);
236 int ast_best_codec(int fmts)
238 /* This just our opinion, expressed in code. We are asked to choose
239 the best codec to use, given no information */
243 /* Okay, ulaw is used by all telephony equipment, so start with it */
245 /* Unless of course, you're a silly European, so then prefer ALAW */
247 /* Okay, well, signed linear is easy to translate into other stuff */
249 /* ADPCM has great sound quality and is still pretty easy to translate */
251 /* Okay, we're down to vocoders now, so pick GSM because it's small and easier to
252 translate and sounds pretty good */
254 /* iLBC is not too bad */
256 /* Speex is free, but computationally more expensive than GSM */
258 /* Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
261 /* G.729a is faster than 723 and slightly less expensive */
263 /* Down to G.723.1 which is proprietary but at least designed for voice */
265 /* Last and least, MP3 which was of course never designed for real-time voice */
270 for (x=0;x<sizeof(prefs) / sizeof(prefs[0]); x++)
273 ast_log(LOG_WARNING, "Don't know any of 0x%x formats\n", fmts);
277 struct ast_channel *ast_channel_alloc(int needqueue)
279 struct ast_channel *tmp;
280 struct ast_channel_pvt *pvt;
283 struct varshead *headp;
286 /* If shutting down, don't allocate any new channels */
289 PTHREAD_MUTEX_LOCK(&chlock);
290 tmp = malloc(sizeof(struct ast_channel));
292 memset(tmp, 0, sizeof(struct ast_channel));
293 pvt = malloc(sizeof(struct ast_channel_pvt));
295 memset(pvt, 0, sizeof(struct ast_channel_pvt));
296 tmp->sched = sched_context_create();
298 for (x=0;x<AST_MAX_FDS - 1;x++)
301 pipe(pvt->alertpipe)) {
302 ast_log(LOG_WARNING, "Alert pipe creation failed!\n");
308 /* Make sure we've got it done right if they don't */
310 flags = fcntl(pvt->alertpipe[0], F_GETFL);
311 fcntl(pvt->alertpipe[0], F_SETFL, flags | O_NONBLOCK);
312 flags = fcntl(pvt->alertpipe[1], F_GETFL);
313 fcntl(pvt->alertpipe[1], F_SETFL, flags | O_NONBLOCK);
315 pvt->alertpipe[0] = pvt->alertpipe[1] = -1;
316 #ifdef ZAPTEL_OPTIMIZATIONS
317 tmp->timingfd = open("/dev/zap/timer", O_RDWR);
321 /* Always watch the alertpipe */
322 tmp->fds[AST_MAX_FDS-1] = pvt->alertpipe[0];
323 /* And timing pipe */
324 tmp->fds[AST_MAX_FDS-2] = tmp->timingfd;
325 strncpy(tmp->name, "**Unknown**", sizeof(tmp->name)-1);
328 tmp->_state = AST_STATE_DOWN;
335 snprintf(tmp->uniqueid, sizeof(tmp->uniqueid), "%li.%d", time(NULL), uniqueint++);
336 headp=&tmp->varshead;
337 ast_pthread_mutex_init(&tmp->lock);
338 AST_LIST_HEAD_INIT(headp);
339 tmp->vars=ast_var_assign("tempvar","tempval");
340 AST_LIST_INSERT_HEAD(headp,tmp->vars,entries);
341 strncpy(tmp->context, "default", sizeof(tmp->context)-1);
342 strncpy(tmp->language, defaultlanguage, sizeof(tmp->language)-1);
343 strncpy(tmp->exten, "s", sizeof(tmp->exten)-1);
345 tmp->amaflags = ast_default_amaflags;
346 strncpy(tmp->accountcode, ast_default_accountcode, sizeof(tmp->accountcode)-1);
347 tmp->next = channels;
351 ast_log(LOG_WARNING, "Unable to create schedule context\n");
356 ast_log(LOG_WARNING, "Out of memory\n");
361 ast_log(LOG_WARNING, "Out of memory\n");
362 PTHREAD_MUTEX_UNLOCK(&chlock);
366 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int lock)
369 struct ast_frame *prev, *cur;
372 /* Build us a copy and free the original one */
375 ast_log(LOG_WARNING, "Unable to duplicate frame\n");
379 ast_pthread_mutex_lock(&chan->lock);
381 cur = chan->pvt->readq;
387 /* Allow up to 96 voice frames outstanding, and up to 128 total frames */
388 if (((fin->frametype == AST_FRAME_VOICE) && (qlen > 96)) || (qlen > 128)) {
389 if (fin->frametype != AST_FRAME_VOICE) {
390 ast_log(LOG_WARNING, "Exceptionally long queue length queuing to %s\n", chan->name);
393 ast_log(LOG_DEBUG, "Dropping voice to exceptionally long queue on %s\n", chan->name);
396 ast_pthread_mutex_unlock(&chan->lock);
403 chan->pvt->readq = f;
404 if (chan->pvt->alertpipe[1] > -1) {
405 if (write(chan->pvt->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah))
406 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d (qlen = %d): %s!\n",
407 chan->name, f->frametype, f->subclass, qlen, strerror(errno));
408 } else if (chan->blocking) {
409 pthread_kill(chan->blocker, SIGURG);
412 ast_pthread_mutex_unlock(&chan->lock);
416 int ast_queue_hangup(struct ast_channel *chan, int lock)
418 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
419 chan->_softhangup |= AST_SOFTHANGUP_DEV;
420 return ast_queue_frame(chan, &f, lock);
423 int ast_queue_control(struct ast_channel *chan, int control, int lock)
425 struct ast_frame f = { AST_FRAME_CONTROL, };
426 f.subclass = control;
427 return ast_queue_frame(chan, &f, lock);
430 int ast_channel_defer_dtmf(struct ast_channel *chan)
434 pre = chan->deferdtmf;
440 void ast_channel_undefer_dtmf(struct ast_channel *chan)
446 struct ast_channel *ast_channel_walk(struct ast_channel *prev)
448 struct ast_channel *l, *ret=NULL;
449 PTHREAD_MUTEX_LOCK(&chlock);
452 PTHREAD_MUTEX_UNLOCK(&chlock);
460 PTHREAD_MUTEX_UNLOCK(&chlock);
465 int ast_safe_sleep_conditional( struct ast_channel *chan, int ms,
466 int (*cond)(void*), void *data )
471 if( cond && ((*cond)(data) == 0 ) )
473 ms = ast_waitfor(chan, ms);
486 int ast_safe_sleep(struct ast_channel *chan, int ms)
490 ms = ast_waitfor(chan, ms);
503 void ast_channel_free(struct ast_channel *chan)
505 struct ast_channel *last=NULL, *cur;
507 struct ast_var_t *vardata;
508 struct ast_frame *f, *fp;
509 struct varshead *headp;
510 char name[AST_CHANNEL_NAME];
512 headp=&chan->varshead;
514 PTHREAD_MUTEX_LOCK(&chlock);
519 last->next = cur->next;
521 channels = cur->next;
528 ast_log(LOG_WARNING, "Unable to find channel in list\n");
530 ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
532 strncpy(name, chan->name, sizeof(name)-1);
534 /* Stop monitoring */
536 chan->monitor->stop( chan, 0 );
539 /* Free translatosr */
540 if (chan->pvt->readtrans)
541 ast_translator_free_path(chan->pvt->readtrans);
542 if (chan->pvt->writetrans)
543 ast_translator_free_path(chan->pvt->writetrans);
545 ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
549 free(chan->callerid);
554 pthread_mutex_destroy(&chan->lock);
555 /* Close pipes if appropriate */
556 if ((fd = chan->pvt->alertpipe[0]) > -1)
558 if ((fd = chan->pvt->alertpipe[1]) > -1)
560 if ((fd = chan->timingfd) > -1)
562 f = chan->pvt->readq;
563 chan->pvt->readq = NULL;
570 /* loop over the variables list, freeing all data and deleting list items */
571 /* no need to lock the list, as the channel is already locked */
573 while (!AST_LIST_EMPTY(headp)) { /* List Deletion. */
574 vardata = AST_LIST_FIRST(headp);
575 AST_LIST_REMOVE_HEAD(headp, entries);
576 // printf("deleting var %s=%s\n",ast_var_name(vardata),ast_var_value(vardata));
577 ast_var_delete(vardata);
584 PTHREAD_MUTEX_UNLOCK(&chlock);
586 ast_device_state_changed(name);
589 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
592 struct ast_frame f = { AST_FRAME_NULL };
594 ast_log(LOG_DEBUG, "Soft-Hanging up channel '%s'\n", chan->name);
595 /* Inform channel driver that we need to be hung up, if it cares */
596 chan->_softhangup |= cause;
597 ast_queue_frame(chan, &f, 0);
598 /* Interrupt any select call or such */
600 pthread_kill(chan->blocker, SIGURG);
604 int ast_softhangup(struct ast_channel *chan, int cause)
607 ast_pthread_mutex_lock(&chan->lock);
608 res = ast_softhangup_nolock(chan, cause);
609 ast_pthread_mutex_unlock(&chan->lock);
613 static int ast_do_masquerade(struct ast_channel *original);
615 static void free_translation(struct ast_channel *clone)
617 if (clone->pvt->writetrans)
618 ast_translator_free_path(clone->pvt->writetrans);
619 if (clone->pvt->readtrans)
620 ast_translator_free_path(clone->pvt->readtrans);
621 clone->pvt->writetrans = NULL;
622 clone->pvt->readtrans = NULL;
623 clone->pvt->rawwriteformat = clone->nativeformats;
624 clone->pvt->rawreadformat = clone->nativeformats;
627 int ast_hangup(struct ast_channel *chan)
630 /* Don't actually hang up a channel that will masquerade as someone else, or
631 if someone is going to masquerade as us */
632 ast_pthread_mutex_lock(&chan->lock);
634 if (ast_do_masquerade(chan))
635 ast_log(LOG_WARNING, "Failed to perform masquerade\n");
639 ast_log(LOG_WARNING, "%s getting hung up, but someone is trying to masq into us?!?\n", chan->name);
640 ast_pthread_mutex_unlock(&chan->lock);
643 /* If this channel is one which will be masqueraded into something,
644 mark it as a zombie already, so we know to free it later */
646 ast_pthread_mutex_unlock(&chan->lock);
650 free_translation(chan);
652 ast_stopstream(chan);
654 sched_context_destroy(chan->sched);
655 /* Clear any tone stuff remaining */
656 if (chan->generatordata)
657 chan->generator->release(chan, chan->generatordata);
658 chan->generatordata = NULL;
659 chan->generator = NULL;
661 /* End the CDR if it hasn't already */
662 ast_cdr_end(chan->cdr);
663 /* Post and Free the CDR */
664 ast_cdr_post(chan->cdr);
665 ast_cdr_free(chan->cdr);
667 if (chan->blocking) {
668 ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
669 "is blocked by thread %ld in procedure %s! Expect a failure\n",
670 pthread_self(), chan->name, chan->blocker, chan->blockproc);
675 ast_log(LOG_DEBUG, "Hanging up channel '%s'\n", chan->name);
676 if (chan->pvt->hangup)
677 res = chan->pvt->hangup(chan);
680 ast_log(LOG_DEBUG, "Hanging up zombie '%s'\n", chan->name);
682 ast_pthread_mutex_unlock(&chan->lock);
683 manager_event(EVENT_FLAG_CALL, "Hangup",
686 chan->name, chan->uniqueid);
687 ast_channel_free(chan);
691 void ast_channel_unregister(char *type)
693 struct chanlist *chan, *last=NULL;
695 ast_log(LOG_DEBUG, "Unregistering channel type '%s'\n", type);
696 if (PTHREAD_MUTEX_LOCK(&chlock)) {
697 ast_log(LOG_WARNING, "Unable to lock channel list\n");
702 if (!strcasecmp(chan->type, type)) {
704 last->next = chan->next;
706 backends = backends->next;
708 PTHREAD_MUTEX_UNLOCK(&chlock);
714 PTHREAD_MUTEX_UNLOCK(&chlock);
717 int ast_answer(struct ast_channel *chan)
720 /* Stop if we're a zombie or need a soft hangup */
721 if (chan->zombie || ast_check_hangup(chan))
723 switch(chan->_state) {
724 case AST_STATE_RINGING:
726 if (chan->pvt->answer)
727 res = chan->pvt->answer(chan);
728 ast_setstate(chan, AST_STATE_UP);
730 ast_cdr_answer(chan->cdr);
735 ast_cdr_answer(chan->cdr);
741 void ast_deactivate_generator(struct ast_channel *chan)
743 if (chan->generatordata) {
744 chan->generator->release(chan, chan->generatordata);
745 chan->generatordata = NULL;
746 chan->generator = NULL;
747 chan->writeinterrupt = 0;
751 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
753 if (chan->generatordata) {
754 chan->generator->release(chan, chan->generatordata);
755 chan->generatordata = NULL;
758 if ((chan->generatordata = gen->alloc(chan, params))) {
759 chan->generator = gen;
766 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
768 /* Wait for x amount of time on a file descriptor to have input. */
775 tv.tv_sec = *ms / 1000;
776 tv.tv_usec = (*ms % 1000) * 1000;
781 FD_SET(fds[x], &rfds);
782 FD_SET(fds[x], &efds);
788 res = ast_select(max + 1, &rfds, NULL, &efds, &tv);
790 res = ast_select(max + 1, &rfds, NULL, &efds, NULL);
793 /* Simulate a timeout if we were interrupted */
802 if ((fds[x] > -1) && (FD_ISSET(fds[x], &rfds) || FD_ISSET(fds[x], &efds)) && (winner < 0)) {
804 *exception = FD_ISSET(fds[x], &efds);
808 *ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
812 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
813 int *exception, int *outfd, int *ms)
815 /* Wait for x amount of time on a file descriptor to have input. */
820 struct ast_channel *winner = NULL;
826 /* Perform any pending masquerades */
828 ast_pthread_mutex_lock(&c[x]->lock);
830 if (ast_do_masquerade(c[x])) {
831 ast_log(LOG_WARNING, "Masquerade failed\n");
833 ast_pthread_mutex_unlock(&c[x]->lock);
837 ast_pthread_mutex_unlock(&c[x]->lock);
840 tv.tv_sec = *ms / 1000;
841 tv.tv_usec = (*ms % 1000) * 1000;
846 for (y=0;y<AST_MAX_FDS;y++) {
847 if (c[x]->fds[y] > -1) {
848 FD_SET(c[x]->fds[y], &rfds);
849 FD_SET(c[x]->fds[y], &efds);
850 if (c[x]->fds[y] > max)
854 CHECK_BLOCKING(c[x]);
856 for (x=0;x<nfds; x++) {
857 FD_SET(fds[x], &rfds);
858 FD_SET(fds[x], &efds);
863 res = ast_select(max + 1, &rfds, NULL, &efds, &tv);
865 res = ast_select(max + 1, &rfds, NULL, &efds, NULL);
870 /* Simulate a timeout if we were interrupted */
874 /* Just an interrupt */
884 for (y=0;y<AST_MAX_FDS;y++) {
885 if (c[x]->fds[y] > -1) {
886 if ((FD_ISSET(c[x]->fds[y], &rfds) || FD_ISSET(c[x]->fds[y], &efds)) && !winner) {
887 /* Set exception flag if appropriate */
888 if (FD_ISSET(c[x]->fds[y], &efds))
896 for (x=0;x<nfds;x++) {
897 if ((FD_ISSET(fds[x], &rfds) || FD_ISSET(fds[x], &efds)) && !winner) {
900 if (FD_ISSET(fds[x], &efds) && exception)
905 *ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
909 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
911 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
914 int ast_waitfor(struct ast_channel *c, int ms)
916 struct ast_channel *chan;
918 chan = ast_waitfor_n(&c, 1, &ms);
928 char ast_waitfordigit(struct ast_channel *c, int ms)
930 /* XXX Should I be merged with waitfordigit_full XXX */
933 /* Stop if we're a zombie or need a soft hangup */
934 if (c->zombie || ast_check_hangup(c))
936 /* Wait for a digit, no more than ms milliseconds total. */
937 while(ms && !result) {
938 ms = ast_waitfor(c, ms);
939 if (ms < 0) /* Error */
945 if (f->frametype == AST_FRAME_DTMF)
946 result = f->subclass;
955 int ast_settimeout(struct ast_channel *c, int samples, int (*func)(void *data), void *data)
958 #ifdef ZAPTEL_OPTIMIZATIONS
959 if (c->timingfd > -1) {
964 ast_log(LOG_DEBUG, "Scheduling timer at %d sample intervals\n", samples);
965 res = ioctl(c->timingfd, ZT_TIMERCONFIG, &samples);
966 c->timingfunc = func;
967 c->timingdata = data;
972 char ast_waitfordigit_full(struct ast_channel *c, int ms, int audio, int ctrl)
976 struct ast_channel *rchan;
978 /* Stop if we're a zombie or need a soft hangup */
979 if (c->zombie || ast_check_hangup(c))
981 /* Wait for a digit, no more than ms milliseconds total. */
982 while(ms && !result) {
983 rchan = ast_waitfor_nandfds(&c, 1, &audio, (audio > -1) ? 1 : 0, NULL, &outfd, &ms);
984 if ((!rchan) && (outfd < 0) && (ms)) /* Error */
986 else if (outfd > -1) {
992 if (f->frametype == AST_FRAME_DTMF)
993 result = f->subclass;
1002 struct ast_frame *ast_read(struct ast_channel *chan)
1004 struct ast_frame *f = NULL;
1006 #ifdef ZAPTEL_OPTIMIZATIONS
1007 int (*func)(void *);
1010 static struct ast_frame null_frame =
1015 ast_pthread_mutex_lock(&chan->lock);
1017 if (ast_do_masquerade(chan)) {
1018 ast_log(LOG_WARNING, "Failed to perform masquerade\n");
1022 pthread_mutex_unlock(&chan->lock);
1026 /* Stop if we're a zombie or need a soft hangup */
1027 if (chan->zombie || ast_check_hangup(chan)) {
1028 if (chan->generator)
1029 ast_deactivate_generator(chan);
1030 pthread_mutex_unlock(&chan->lock);
1034 if (!chan->deferdtmf && strlen(chan->dtmfq)) {
1035 /* We have DTMF that has been deferred. Return it now */
1036 chan->dtmff.frametype = AST_FRAME_DTMF;
1037 chan->dtmff.subclass = chan->dtmfq[0];
1038 /* Drop first digit */
1039 memmove(chan->dtmfq, chan->dtmfq + 1, sizeof(chan->dtmfq) - 1);
1040 pthread_mutex_unlock(&chan->lock);
1041 return &chan->dtmff;
1044 /* Read and ignore anything on the alertpipe, but read only
1045 one sizeof(blah) per frame that we send from it */
1046 if (chan->pvt->alertpipe[0] > -1) {
1047 read(chan->pvt->alertpipe[0], &blah, sizeof(blah));
1049 #ifdef ZAPTEL_OPTIMIZATIONS
1050 if ((chan->timingfd > -1) && (chan->fdno == AST_MAX_FDS - 2) && chan->exception) {
1051 chan->exception = 0;
1053 ioctl(chan->timingfd, ZT_TIMERACK, &blah);
1054 func = chan->timingfunc;
1055 data = chan->timingdata;
1056 pthread_mutex_unlock(&chan->lock);
1059 ast_log(LOG_DEBUG, "Calling private function\n");
1064 pthread_mutex_lock(&chan->lock);
1065 ioctl(chan->timingfd, ZT_TIMERCONFIG, &blah);
1066 chan->timingdata = NULL;
1067 pthread_mutex_unlock(&chan->lock);
1073 /* Check for pending read queue */
1074 if (chan->pvt->readq) {
1075 f = chan->pvt->readq;
1076 chan->pvt->readq = f->next;
1077 /* Interpret hangup and return NULL */
1078 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))
1081 chan->blocker = pthread_self();
1082 if (chan->exception) {
1083 if (chan->pvt->exception)
1084 f = chan->pvt->exception(chan);
1086 ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", chan->name);
1089 /* Clear the exception flag */
1090 chan->exception = 0;
1092 if (chan->pvt->read)
1093 f = chan->pvt->read(chan);
1095 ast_log(LOG_WARNING, "No read routine on channel %s\n", chan->name);
1099 if (f && (f->frametype == AST_FRAME_VOICE)) {
1100 if (!(f->subclass & chan->nativeformats)) {
1101 /* This frame can't be from the current native formats -- drop it on the
1103 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);
1107 if (chan->monitor && chan->monitor->read_stream ) {
1108 if( ast_writestream( chan->monitor->read_stream, f ) < 0 ) {
1109 ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n");
1112 if (chan->pvt->readtrans) {
1113 f = ast_translate(chan->pvt->readtrans, f, 1);
1120 /* Make sure we always return NULL in the future */
1122 chan->_softhangup |= AST_SOFTHANGUP_DEV;
1123 if (chan->generator)
1124 ast_deactivate_generator(chan);
1125 /* End the CDR if appropriate */
1127 ast_cdr_end(chan->cdr);
1128 } else if (chan->deferdtmf && f->frametype == AST_FRAME_DTMF) {
1129 if (strlen(chan->dtmfq) < sizeof(chan->dtmfq) - 2)
1130 chan->dtmfq[strlen(chan->dtmfq)] = f->subclass;
1132 ast_log(LOG_WARNING, "Dropping deferred DTMF digits on %s\n", chan->name);
1134 } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
1135 /* Answer the CDR */
1136 ast_setstate(chan, AST_STATE_UP);
1137 ast_cdr_answer(chan->cdr);
1139 pthread_mutex_unlock(&chan->lock);
1141 /* Run any generator sitting on the line */
1142 if (f && (f->frametype == AST_FRAME_VOICE) && chan->generatordata) {
1143 /* Mask generator data temporarily */
1146 tmp = chan->generatordata;
1147 chan->generatordata = NULL;
1148 res = chan->generator->generate(chan, tmp, f->datalen, f->samples);
1149 chan->generatordata = tmp;
1151 ast_log(LOG_DEBUG, "Auto-deactivating generator\n");
1152 ast_deactivate_generator(chan);
1155 if (chan->fin & 0x80000000)
1156 ast_frame_dump(chan->name, f, "<<");
1157 if ((chan->fin & 0x7fffffff) == 0x7fffffff)
1158 chan->fin &= 0x80000000;
1164 int ast_indicate(struct ast_channel *chan, int condition)
1167 /* Stop if we're a zombie or need a soft hangup */
1168 if (chan->zombie || ast_check_hangup(chan))
1170 if (chan->pvt->indicate)
1171 res = chan->pvt->indicate(chan, condition);
1172 if (!chan->pvt->indicate || res) {
1174 * Device does not support (that) indication, lets fake
1175 * it by doing our own tone generation. (PM2002)
1177 if (condition >= 0) {
1178 const struct tone_zone_sound *ts = NULL;
1179 switch (condition) {
1180 case AST_CONTROL_RINGING:
1181 ts = ast_get_indication_tone(chan->zone, "ring");
1183 case AST_CONTROL_BUSY:
1184 ts = ast_get_indication_tone(chan->zone, "busy");
1186 case AST_CONTROL_CONGESTION:
1187 ts = ast_get_indication_tone(chan->zone, "congestion");
1190 if (ts && ts->data[0]) {
1191 ast_log(LOG_DEBUG, "Driver for channel '%s' does not support indication %d, emulating it\n", chan->name, condition);
1192 ast_playtones_start(chan,0,ts->data, 1);
1193 } else if (condition == AST_CONTROL_PROGRESS) {
1194 /* ast_playtones_stop(chan); */
1197 ast_log(LOG_WARNING, "Unable to handle indication %d for '%s'\n", condition, chan->name);
1201 else ast_playtones_stop(chan);
1206 int ast_recvchar(struct ast_channel *chan, int timeout)
1209 struct ast_frame *f;
1214 if (ast_check_hangup(chan)) return -1;
1215 res = ast_waitfor(chan,ourto);
1216 if (res <= 0) /* if timeout */
1222 if (f == NULL) return -1; /* if hangup */
1223 if ((f->frametype == AST_FRAME_CONTROL) &&
1224 (f->subclass == AST_CONTROL_HANGUP)) return -1; /* if hangup */
1225 if (f->frametype == AST_FRAME_TEXT) /* if a text frame */
1227 c = *((char *)f->data); /* get the data */
1235 int ast_sendtext(struct ast_channel *chan, char *text)
1238 /* Stop if we're a zombie or need a soft hangup */
1239 if (chan->zombie || ast_check_hangup(chan))
1241 CHECK_BLOCKING(chan);
1242 if (chan->pvt->send_text)
1243 res = chan->pvt->send_text(chan, text);
1248 static int do_senddigit(struct ast_channel *chan, char digit)
1252 if (chan->pvt->send_digit)
1253 res = chan->pvt->send_digit(chan, digit);
1254 if (!chan->pvt->send_digit || res) {
1256 * Device does not support DTMF tones, lets fake
1257 * it by doing our own generation. (PM2002)
1259 static const char* dtmf_tones[] = {
1260 "!941+1336/50,!0/50", /* 0 */
1261 "!697+1209/50,!0/50", /* 1 */
1262 "!697+1336/50,!0/50", /* 2 */
1263 "!697+1477/50,!0/50", /* 3 */
1264 "!770+1209/50,!0/50", /* 4 */
1265 "!770+1336/50,!0/50", /* 5 */
1266 "!770+1477/50,!0/50", /* 6 */
1267 "!852+1209/50,!0/50", /* 7 */
1268 "!852+1336/50,!0/50", /* 8 */
1269 "!852+1477/50,!0/50", /* 9 */
1270 "!697+1633/50,!0/50", /* A */
1271 "!770+1633/50,!0/50", /* B */
1272 "!852+1633/50,!0/50", /* C */
1273 "!941+1633/50,!0/50", /* D */
1274 "!941+1209/50,!0/50", /* * */
1275 "!941+1477/50,!0/50" }; /* # */
1276 if (digit >= '0' && digit <='9')
1277 ast_playtones_start(chan,0,dtmf_tones[digit-'0'], 0);
1278 else if (digit >= 'A' && digit <= 'D')
1279 ast_playtones_start(chan,0,dtmf_tones[digit-'A'+10], 0);
1280 else if (digit == '*')
1281 ast_playtones_start(chan,0,dtmf_tones[14], 0);
1282 else if (digit == '#')
1283 ast_playtones_start(chan,0,dtmf_tones[15], 0);
1286 ast_log(LOG_WARNING, "Unable to handle DTMF tone '%c' for '%s'\n", digit, chan->name);
1293 int ast_prod(struct ast_channel *chan)
1295 struct ast_frame a = { AST_FRAME_VOICE };
1297 /* Send an empty audio frame to get things moving */
1298 if (chan->_state != AST_STATE_UP) {
1299 ast_log(LOG_DEBUG, "Prodding channel '%s'\n", chan->name);
1300 a.subclass = chan->pvt->rawwriteformat;
1301 a.data = nothing + AST_FRIENDLY_OFFSET;
1302 if (ast_write(chan, &a))
1303 ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", chan->name);
1308 int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
1311 if (!chan->pvt->write_video)
1313 res = ast_write(chan, fr);
1319 int ast_write(struct ast_channel *chan, struct ast_frame *fr)
1322 struct ast_frame *f = NULL;
1323 /* Stop if we're a zombie or need a soft hangup */
1324 ast_pthread_mutex_lock(&chan->lock);
1325 if (chan->zombie || ast_check_hangup(chan)) {
1326 ast_pthread_mutex_unlock(&chan->lock);
1329 /* Handle any pending masquerades */
1331 if (ast_do_masquerade(chan)) {
1332 ast_log(LOG_WARNING, "Failed to perform masquerade\n");
1333 ast_pthread_mutex_unlock(&chan->lock);
1338 ast_pthread_mutex_unlock(&chan->lock);
1341 if (chan->generatordata) {
1342 if (chan->writeinterrupt)
1343 ast_deactivate_generator(chan);
1345 ast_pthread_mutex_unlock(&chan->lock);
1349 if (chan->fout & 0x80000000)
1350 ast_frame_dump(chan->name, fr, ">>");
1351 CHECK_BLOCKING(chan);
1352 switch(fr->frametype) {
1353 case AST_FRAME_CONTROL:
1354 /* XXX Interpret control frames XXX */
1355 ast_log(LOG_WARNING, "Don't know how to handle control frames yet\n");
1357 case AST_FRAME_DTMF:
1358 res = do_senddigit(chan,fr->subclass);
1360 case AST_FRAME_TEXT:
1361 if (chan->pvt->send_text)
1362 res = chan->pvt->send_text(chan, (char *) fr->data);
1364 case AST_FRAME_VIDEO:
1365 /* XXX Handle translation of video codecs one day XXX */
1366 if (chan->pvt->write_video)
1367 res = chan->pvt->write_video(chan, fr);
1372 if (chan->pvt->write) {
1373 if (chan->pvt->writetrans) {
1374 f = ast_translate(chan->pvt->writetrans, fr, 0);
1379 res = chan->pvt->write(chan, f);
1380 if( chan->monitor &&
1381 chan->monitor->write_stream &&
1382 f && ( f->frametype == AST_FRAME_VOICE ) ) {
1383 if( ast_writestream( chan->monitor->write_stream, f ) < 0 ) {
1384 ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n");
1395 /* Consider a write failure to force a soft hangup */
1397 chan->_softhangup |= AST_SOFTHANGUP_DEV;
1399 if ((chan->fout & 0x7fffffff) == 0x7fffffff)
1400 chan->fout &= 0x80000000;
1405 ast_pthread_mutex_unlock(&chan->lock);
1409 int ast_set_write_format(struct ast_channel *chan, int fmts)
1415 native = chan->nativeformats;
1418 res = ast_translator_best_choice(&native, &fmt);
1420 ast_log(LOG_NOTICE, "Unable to find a path from %d to %d\n", fmts, chan->nativeformats);
1424 /* Now we have a good choice for both. We'll write using our native format. */
1425 chan->pvt->rawwriteformat = native;
1426 /* User perspective is fmt */
1427 chan->writeformat = fmt;
1428 /* Free any write translation we have right now */
1429 if (chan->pvt->writetrans)
1430 ast_translator_free_path(chan->pvt->writetrans);
1431 /* Build a translation path from the user write format to the raw writing format */
1432 chan->pvt->writetrans = ast_translator_build_path(chan->pvt->rawwriteformat, chan->writeformat);
1434 ast_log(LOG_DEBUG, "Set channel %s to write format %d\n", chan->name, chan->writeformat);
1438 int ast_set_read_format(struct ast_channel *chan, int fmts)
1444 native = chan->nativeformats;
1446 /* Find a translation path from the native read format to one of the user's read formats */
1447 res = ast_translator_best_choice(&fmt, &native);
1449 ast_log(LOG_NOTICE, "Unable to find a path from %d to %d\n", chan->nativeformats, fmts);
1453 /* Now we have a good choice for both. We'll write using our native format. */
1454 chan->pvt->rawreadformat = native;
1455 /* User perspective is fmt */
1456 chan->readformat = fmt;
1457 /* Free any read translation we have right now */
1458 if (chan->pvt->readtrans)
1459 ast_translator_free_path(chan->pvt->readtrans);
1460 /* Build a translation path from the raw read format to the user reading format */
1461 chan->pvt->readtrans = ast_translator_build_path(chan->readformat, chan->pvt->rawreadformat);
1463 ast_log(LOG_DEBUG, "Set channel %s to read format %d\n", chan->name, chan->readformat);
1467 struct ast_channel *ast_request_and_dial(char *type, int format, void *data, int timeout, int *outstate, char *callerid)
1470 struct ast_channel *chan;
1471 struct ast_frame *f;
1474 chan = ast_request(type, format, data);
1477 ast_set_callerid(chan, callerid, 1);
1478 if (!ast_call(chan, data, 0)) {
1479 while(timeout && (chan->_state != AST_STATE_UP)) {
1480 res = ast_waitfor(chan, timeout);
1482 /* Something not cool, or timed out */
1487 /* If done, break out */
1494 state = AST_CONTROL_HANGUP;
1499 if (f->frametype == AST_FRAME_CONTROL) {
1500 if (f->subclass == AST_CONTROL_RINGING)
1501 state = AST_CONTROL_RINGING;
1502 else if ((f->subclass == AST_CONTROL_BUSY) || (f->subclass == AST_CONTROL_CONGESTION)) {
1503 state = f->subclass;
1506 } else if (f->subclass == AST_CONTROL_ANSWER) {
1507 state = f->subclass;
1511 ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass);
1519 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
1522 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
1523 if (chan && (chan->_state == AST_STATE_UP))
1524 state = AST_CONTROL_ANSWER;
1530 struct ast_channel *ast_request(char *type, int format, void *data)
1532 struct chanlist *chan;
1533 struct ast_channel *c = NULL;
1537 if (PTHREAD_MUTEX_LOCK(&chlock)) {
1538 ast_log(LOG_WARNING, "Unable to lock channel list\n");
1543 if (!strcasecmp(type, chan->type)) {
1544 capabilities = chan->capabilities;
1546 res = ast_translator_best_choice(&fmt, &capabilities);
1548 ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %d) to %d\n", type, chan->capabilities, format);
1549 PTHREAD_MUTEX_UNLOCK(&chlock);
1552 PTHREAD_MUTEX_UNLOCK(&chlock);
1553 if (chan->requester)
1554 c = chan->requester(type, capabilities, data);
1556 // ast_device_state_changed(c->name);
1557 manager_event(EVENT_FLAG_CALL, "Newchannel",
1562 c->name, ast_state2str(c->_state), c->callerid ? c->callerid : "<unknown>", c->uniqueid);
1569 ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
1570 PTHREAD_MUTEX_UNLOCK(&chlock);
1574 int ast_parse_device_state(char *device)
1576 char name[AST_CHANNEL_NAME] = "";
1578 struct ast_channel *chan;
1580 chan = ast_channel_walk(NULL);
1582 strncpy(name, chan->name, sizeof(name)-1);
1583 cut = strchr(name,'-');
1586 if (!strcmp(name, device))
1587 return AST_DEVICE_INUSE;
1588 chan = ast_channel_walk(chan);
1590 return AST_DEVICE_UNKNOWN;
1593 int ast_device_state(char *device)
1595 char tech[AST_MAX_EXTENSION] = "";
1597 struct chanlist *chanls;
1600 strncpy(tech, device, sizeof(tech)-1);
1601 number = strchr(tech, '/');
1603 return AST_DEVICE_INVALID;
1608 if (PTHREAD_MUTEX_LOCK(&chlock)) {
1609 ast_log(LOG_WARNING, "Unable to lock channel list\n");
1614 if (!strcasecmp(tech, chanls->type)) {
1615 PTHREAD_MUTEX_UNLOCK(&chlock);
1616 if (!chanls->devicestate)
1617 return ast_parse_device_state(device);
1619 res = chanls->devicestate(number);
1620 if (res == AST_DEVICE_UNKNOWN)
1621 return ast_parse_device_state(device);
1626 chanls = chanls->next;
1628 PTHREAD_MUTEX_UNLOCK(&chlock);
1629 return AST_DEVICE_INVALID;
1632 int ast_call(struct ast_channel *chan, char *addr, int timeout)
1634 /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
1635 If the remote end does not answer within the timeout, then do NOT hang up, but
1638 /* Stop if we're a zombie or need a soft hangup */
1639 ast_pthread_mutex_lock(&chan->lock);
1640 if (!chan->zombie && !ast_check_hangup(chan))
1641 if (chan->pvt->call)
1642 res = chan->pvt->call(chan, addr, timeout);
1643 pthread_mutex_unlock(&chan->lock);
1647 int ast_transfer(struct ast_channel *chan, char *dest)
1649 /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
1650 If the remote end does not answer within the timeout, then do NOT hang up, but
1653 /* Stop if we're a zombie or need a soft hangup */
1654 ast_pthread_mutex_lock(&chan->lock);
1655 if (!chan->zombie && !ast_check_hangup(chan)) {
1656 if (chan->pvt->transfer) {
1657 res = chan->pvt->transfer(chan, dest);
1663 pthread_mutex_unlock(&chan->lock);
1667 int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
1672 /* XXX Merge with full version? XXX */
1673 /* Stop if we're a zombie or need a soft hangup */
1674 if (c->zombie || ast_check_hangup(c))
1680 d = ast_waitstream(c, AST_DIGIT_ANY);
1684 d = ast_waitfordigit(c, to);
1686 d = ast_waitfordigit(c, to);
1694 if (!strchr(enders, d))
1696 if (strchr(enders, d) || (pos >= len)) {
1706 int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
1711 /* Stop if we're a zombie or need a soft hangup */
1712 if (c->zombie || ast_check_hangup(c))
1718 d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
1722 d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
1724 d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
1736 if (!strchr(enders, d))
1738 if (strchr(enders, d) || (pos >= len)) {
1748 int ast_channel_supports_html(struct ast_channel *chan)
1750 if (chan->pvt->send_html)
1755 int ast_channel_sendhtml(struct ast_channel *chan, int subclass, char *data, int datalen)
1757 if (chan->pvt->send_html)
1758 return chan->pvt->send_html(chan, subclass, data, datalen);
1762 int ast_channel_sendurl(struct ast_channel *chan, char *url)
1764 if (chan->pvt->send_html)
1765 return chan->pvt->send_html(chan, AST_HTML_URL, url, strlen(url) + 1);
1769 int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
1774 peerf = peer->nativeformats;
1775 chanf = chan->nativeformats;
1776 res = ast_translator_best_choice(&peerf, &chanf);
1778 ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", chan->name, chan->nativeformats, peer->name, peer->nativeformats);
1781 /* Set read format on channel */
1782 res = ast_set_read_format(chan, peerf);
1784 ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", chan->name, chanf);
1787 /* Set write format on peer channel */
1788 res = ast_set_write_format(peer, peerf);
1790 ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", peer->name, peerf);
1793 /* Now we go the other way */
1794 peerf = peer->nativeformats;
1795 chanf = chan->nativeformats;
1796 res = ast_translator_best_choice(&chanf, &peerf);
1798 ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", peer->name, peer->nativeformats, chan->name, chan->nativeformats);
1801 /* Set writeformat on channel */
1802 res = ast_set_write_format(chan, chanf);
1804 ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", chan->name, chanf);
1807 /* Set read format on peer channel */
1808 res = ast_set_read_format(peer, chanf);
1810 ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", peer->name, peerf);
1816 int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone)
1818 struct ast_frame null = { AST_FRAME_NULL, };
1819 ast_log(LOG_DEBUG, "Planning to masquerade %s into the structure of %s\n",
1820 clone->name, original->name);
1821 if (original->masq) {
1822 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
1823 original->masq->name, original->name);
1827 ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
1828 clone->name, clone->masqr->name);
1831 original->masq = clone;
1832 clone->masqr = original;
1833 /* XXX can't really hold the lock here, but at the same time, it' s
1834 not really safe not to XXX */
1835 ast_queue_frame(original, &null, 0);
1836 ast_queue_frame(clone, &null, 0);
1837 ast_log(LOG_DEBUG, "Done planning to masquerade %s into the structure of %s\n", original->name, clone->name);
1841 void ast_change_name(struct ast_channel *chan, char *newname)
1844 strncpy(tmp, chan->name, 256);
1845 strncpy(chan->name, newname, sizeof(chan->name) - 1);
1846 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", tmp, chan->name, chan->uniqueid);
1849 static int ast_do_masquerade(struct ast_channel *original)
1855 struct ast_channel_pvt *p;
1856 struct ast_channel *clone = original->masq;
1857 int rformat = original->readformat;
1858 int wformat = original->writeformat;
1865 ast_log(LOG_DEBUG, "Actually Masquerading %s(%d) into the structure of %s(%d)\n",
1866 clone->name, clone->_state, original->name, original->_state);
1868 /* XXX This is a seriously wacked out operation. We're essentially putting the guts of
1869 the clone channel into the original channel. Start by killing off the original
1870 channel's backend. I'm not sure we're going to keep this function, because
1871 while the features are nice, the cost is very high in terms of pure nastiness. XXX */
1873 /* We need the clone's lock, too */
1874 ast_pthread_mutex_lock(&clone->lock);
1876 ast_log(LOG_DEBUG, "Got clone lock on '%s'\n", clone->name);
1878 /* Having remembered the original read/write formats, we turn off any translation on either
1880 free_translation(clone);
1881 free_translation(original);
1884 /* Unlink the masquerade */
1885 original->masq = NULL;
1886 clone->masqr = NULL;
1888 /* Save the original name */
1889 strncpy(orig, original->name, sizeof(orig) - 1);
1890 /* Save the new name */
1891 strncpy(newn, clone->name, sizeof(newn) - 1);
1892 /* Create the masq name */
1893 snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn);
1895 /* Copy the name from the clone channel */
1896 strncpy(original->name, newn, sizeof(original->name)-1);
1898 /* Mangle the name of the clone channel */
1899 strncpy(clone->name, masqn, sizeof(clone->name) - 1);
1901 /* Notify any managers of the change, first the masq then the other */
1902 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\n", newn, masqn);
1903 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\n", orig, newn);
1907 original->pvt = clone->pvt;
1910 clone->_softhangup = AST_SOFTHANGUP_DEV;
1913 if (clone->pvt->fixup){
1914 res = clone->pvt->fixup(original, clone);
1916 ast_log(LOG_WARNING, "Fixup failed on channel %s, strange things may happen.\n", clone->name);
1919 /* Start by disconnecting the original's physical side */
1920 if (clone->pvt->hangup)
1921 res = clone->pvt->hangup(clone);
1923 ast_log(LOG_WARNING, "Hangup failed! Strange things may happen!\n");
1924 pthread_mutex_unlock(&clone->lock);
1928 snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig);
1929 /* Mangle the name of the clone channel */
1930 strncpy(clone->name, zombn, sizeof(clone->name) - 1);
1931 manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\n", masqn, zombn);
1933 /* Keep the same language. */
1934 /* Update the type. */
1935 original->type = clone->type;
1937 for (x=0;x<AST_MAX_FDS;x++) {
1938 original->fds[x] = clone->fds[x];
1940 /* Move the variables */
1941 tmpv = original->varshead.first;
1942 original->varshead.first = clone->varshead.first;
1943 clone->varshead.first = tmpv;
1944 /* Presense of ADSI capable CPE follows clone */
1945 original->adsicpe = clone->adsicpe;
1946 /* Bridge remains the same */
1947 /* CDR fields remain the same */
1948 /* XXX What about blocking, softhangup, blocker, and lock and blockproc? XXX */
1949 /* Application and data remain the same */
1950 /* Clone exception becomes real one, as with fdno */
1951 original->exception = clone->exception;
1952 original->fdno = clone->fdno;
1953 /* Schedule context remains the same */
1954 /* Stream stuff stays the same */
1955 /* Keep the original state. The fixup code will need to work with it most likely */
1957 /* dnid and callerid change to become the new, HOWEVER, we also link the original's
1958 fields back into the defunct 'clone' so that they will be freed when
1959 ast_frfree is eventually called */
1960 tmp = original->dnid;
1961 original->dnid = clone->dnid;
1964 tmp = original->callerid;
1965 original->callerid = clone->callerid;
1966 clone->callerid = tmp;
1968 /* Restore original timing file descriptor */
1969 original->fds[AST_MAX_FDS - 2] = original->timingfd;
1971 /* Our native formats are different now */
1972 original->nativeformats = clone->nativeformats;
1974 /* And of course, so does our current state. Note we need not
1975 call ast_setstate since the event manager doesn't really consider
1977 original->_state = clone->_state;
1979 /* Context, extension, priority, app data, jump table, remain the same */
1980 /* pvt switches. pbx stays the same, as does next */
1982 /* Now, at this point, the "clone" channel is totally F'd up. We mark it as
1983 a zombie so nothing tries to touch it. If it's already been marked as a
1984 zombie, then free it now (since it already is considered invalid). */
1985 if (clone->zombie) {
1986 ast_log(LOG_DEBUG, "Destroying clone '%s'\n", clone->name);
1987 pthread_mutex_unlock(&clone->lock);
1988 ast_channel_free(clone);
1989 manager_event(EVENT_FLAG_CALL, "Hangup", "Channel: %s\r\n", zombn);
1991 ast_log(LOG_DEBUG, "Released clone lock on '%s'\n", clone->name);
1993 pthread_mutex_unlock(&clone->lock);
1995 /* Set the write format */
1996 ast_set_write_format(original, wformat);
1998 /* Set the read format */
1999 ast_set_read_format(original, rformat);
2001 ast_log(LOG_DEBUG, "Putting channel %s in %d/%d formats\n", original->name, wformat, rformat);
2003 /* Okay. Last thing is to let the channel driver know about all this mess, so he
2004 can fix up everything as best as possible */
2005 if (original->pvt->fixup) {
2006 res = original->pvt->fixup(clone, original);
2008 ast_log(LOG_WARNING, "Driver for '%s' could not fixup channel %s\n",
2009 original->type, original->name);
2013 ast_log(LOG_WARNING, "Driver '%s' does not have a fixup routine (for %s)! Bad things may happen.\n",
2014 original->type, original->name);
2015 /* Signal any blocker */
2016 if (original->blocking)
2017 pthread_kill(original->blocker, SIGURG);
2018 ast_log(LOG_DEBUG, "Done Masquerading %s (%d)\n",
2019 original->name, original->_state);
2023 void ast_set_callerid(struct ast_channel *chan, char *callerid, int anitoo)
2026 free(chan->callerid);
2027 if (anitoo && chan->ani)
2030 chan->callerid = strdup(callerid);
2032 chan->ani = strdup(callerid);
2034 chan->callerid = NULL;
2039 ast_cdr_setcid(chan->cdr, chan);
2040 manager_event(EVENT_FLAG_CALL, "Newcallerid",
2044 chan->name, chan->callerid ?
2045 chan->callerid : "<Unknown>",
2049 int ast_setstate(struct ast_channel *chan, int state)
2051 if (chan->_state != state) {
2052 int oldstate = chan->_state;
2053 chan->_state = state;
2054 if (oldstate == AST_STATE_DOWN) {
2055 ast_device_state_changed(chan->name);
2056 manager_event(EVENT_FLAG_CALL, "Newchannel",
2061 chan->name, ast_state2str(chan->_state), chan->callerid ? chan->callerid : "<unknown>", chan->uniqueid);
2063 manager_event(EVENT_FLAG_CALL, "Newstate",
2068 chan->name, ast_state2str(chan->_state), chan->callerid ? chan->callerid : "<unknown>", chan->uniqueid);
2074 int ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2076 /* Copy voice back and forth between the two channels. Give the peer
2077 the ability to transfer calls with '#<extension' syntax. */
2078 struct ast_channel *cs[3];
2080 struct ast_frame *f;
2081 struct ast_channel *who = NULL;
2085 /* Stop if we're a zombie or need a soft hangup */
2086 if (c0->zombie || ast_check_hangup_locked(c0) || c1->zombie || ast_check_hangup_locked(c1))
2089 ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
2090 c0->name, c0->bridge->name);
2094 ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
2095 c1->name, c1->bridge->name);
2099 /* Keep track of bridge */
2105 manager_event(EVENT_FLAG_CALL, "Link",
2108 c0->name, c1->name);
2110 for (/* ever */;;) {
2111 /* Stop if we're a zombie or need a soft hangup */
2112 if (c0->zombie || ast_check_hangup_locked(c0) || c1->zombie || ast_check_hangup_locked(c1)) {
2116 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");
2119 if (c0->pvt->bridge &&
2120 (c0->pvt->bridge == c1->pvt->bridge) && !nativefailed && !c0->monitor && !c1->monitor) {
2121 /* Looks like they share a bridge code */
2122 if (option_verbose > 2)
2123 ast_verbose(VERBOSE_PREFIX_3 "Attempting native bridge of %s and %s\n", c0->name, c1->name);
2124 if (!(res = c0->pvt->bridge(c0, c1, flags, fo, rc))) {
2127 manager_event(EVENT_FLAG_CALL, "Unlink",
2130 c0->name, c1->name);
2131 ast_log(LOG_DEBUG, "Returning from native bridge, channels: %s, %s\n",c0->name ,c1->name);
2134 /* If they return non-zero then continue on normally. Let "-2" mean don't worry about
2135 my not wanting to bridge */
2136 if ((res != -2) && (res != -3))
2137 ast_log(LOG_WARNING, "Private bridge between %s and %s failed\n", c0->name, c1->name);
2138 if (res != -3) nativefailed++;
2142 if (((c0->writeformat != c1->readformat) || (c0->readformat != c1->writeformat)) &&
2143 !(c0->generator || c1->generator)) {
2144 if (ast_channel_make_compatible(c0, c1)) {
2145 ast_log(LOG_WARNING, "Can't make %s and %s compatible\n", c0->name, c1->name);
2146 manager_event(EVENT_FLAG_CALL, "Unlink",
2149 c0->name, c1->name);
2153 who = ast_waitfor_n(cs, 2, &to);
2155 ast_log(LOG_DEBUG, "Nobody there, continuing...\n");
2163 ast_log(LOG_DEBUG, "Didn't get a frame from channel: %s\n",who->name);
2167 if ((f->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
2171 ast_log(LOG_DEBUG, "Got a FRAME_CONTROL frame on channel %s\n",who->name);
2174 if ((f->frametype == AST_FRAME_VOICE) ||
2175 (f->frametype == AST_FRAME_TEXT) ||
2176 (f->frametype == AST_FRAME_VIDEO) ||
2177 (f->frametype == AST_FRAME_IMAGE) ||
2178 (f->frametype == AST_FRAME_DTMF)) {
2179 if ((f->frametype == AST_FRAME_DTMF) &&
2180 (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))) {
2182 if ((flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
2185 /* Take out of conference mode */
2187 ast_log(LOG_DEBUG, "Got AST_BRIDGE_DTMF_CHANNEL_0 on c0 (%s)\n",c0->name);
2193 if (flags & AST_BRIDGE_DTMF_CHANNEL_1) {
2197 ast_log(LOG_DEBUG, "Got AST_BRIDGE_DTMF_CHANNEL_1 on c1 (%s)\n",c1->name);
2204 ast_log(LOG_DEBUG, "Read from %s\n", who->name);
2206 ast_log(LOG_DEBUG, "Servicing channel %s twice in a row?\n", last->name);
2210 /* Don't copy packets if there is a generator on either one, since they're
2211 not supposed to be listening anyway */
2220 /* Swap who gets priority */
2227 manager_event(EVENT_FLAG_CALL, "Unlink",
2230 c0->name, c1->name);
2231 ast_log(LOG_DEBUG, "Bridge stops bridging channels %s and %s\n",c0->name,c1->name);
2235 int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
2238 if (chan->pvt->setoption) {
2239 res = chan->pvt->setoption(chan, option, data, datalen);
2247 /* XXX Implement blocking -- just wait for our option frame reply, discarding
2248 intermediate packets. XXX */
2249 ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
2255 struct tonepair_def {
2262 struct tonepair_state {
2270 unsigned char offset[AST_FRIENDLY_OFFSET];
2274 static void tonepair_release(struct ast_channel *chan, void *params)
2276 struct tonepair_state *ts = params;
2278 ast_set_write_format(chan, ts->origwfmt);
2283 static void * tonepair_alloc(struct ast_channel *chan, void *params)
2285 struct tonepair_state *ts;
2286 struct tonepair_def *td = params;
2287 ts = malloc(sizeof(struct tonepair_state));
2290 memset(ts, 0, sizeof(struct tonepair_state));
2291 ts->origwfmt = chan->writeformat;
2292 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
2293 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
2294 tonepair_release(NULL, ts);
2297 ts->freq1 = td->freq1;
2298 ts->freq2 = td->freq2;
2299 ts->duration = td->duration;
2302 /* Let interrupts interrupt :) */
2303 chan->writeinterrupt = 1;
2307 static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
2309 struct tonepair_state *ts = data;
2312 /* we need to prepare a frame with 16 * timelen samples as we're
2313 * generating SLIN audio
2317 if (len > sizeof(ts->data) / 2 - 1) {
2318 ast_log(LOG_WARNING, "Can't generate that much data!\n");
2321 memset(&ts->f, 0, sizeof(ts->f));
2322 for (x=0;x<len/2;x++) {
2323 ts->data[x] = ts->vol * (
2324 sin((ts->freq1 * 2.0 * M_PI / 8000.0) * (ts->pos + x)) +
2325 sin((ts->freq2 * 2.0 * M_PI / 8000.0) * (ts->pos + x))
2328 ts->f.frametype = AST_FRAME_VOICE;
2329 ts->f.subclass = AST_FORMAT_SLINEAR;
2330 ts->f.datalen = len;
2331 ts->f.samples = samples;
2332 ts->f.offset = AST_FRIENDLY_OFFSET;
2333 ts->f.data = ts->data;
2334 ast_write(chan, &ts->f);
2336 if (ts->duration > 0) {
2337 if (ts->pos >= ts->duration * 8)
2343 static struct ast_generator tonepair = {
2344 alloc: tonepair_alloc,
2345 release: tonepair_release,
2346 generate: tonepair_generator,
2349 int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
2351 struct tonepair_def d = { 0, };
2354 d.duration = duration;
2359 if (ast_activate_generator(chan, &tonepair, &d))
2364 void ast_tonepair_stop(struct ast_channel *chan)
2366 ast_deactivate_generator(chan);
2369 int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
2371 struct ast_frame *f;
2373 if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
2376 /* Give us some wiggle room */
2377 while(chan->generatordata && (ast_waitfor(chan, 100) >= 0)) {