12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564 |
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <string.h>
- #include <sys/time.h>
- #include <signal.h>
- #include <errno.h>
- #include <unistd.h>
- #include <math.h>
- #include <asterisk/pbx.h>
- #include <asterisk/frame.h>
- #include <asterisk/sched.h>
- #include <asterisk/options.h>
- #include <asterisk/channel.h>
- #include <asterisk/channel_pvt.h>
- #include <asterisk/logger.h>
- #include <asterisk/file.h>
- #include <asterisk/translate.h>
- #include <asterisk/manager.h>
- #include <asterisk/chanvars.h>
- #include <asterisk/linkedlists.h>
- #include <asterisk/indications.h>
- #include <asterisk/monitor.h>
- #include <asterisk/causes.h>
- #ifdef ZAPTEL_OPTIMIZATIONS
- #include <sys/ioctl.h>
- #include <linux/zaptel.h>
- #endif
- #if 0
- #define MONITOR_CONSTANT_DELAY
- #define MONITOR_DELAY 150 * 8
- #endif
- static int shutting_down = 0;
- static int uniqueint = 0;
- struct chanlist {
- char type[80];
- char description[80];
- int capabilities;
- struct ast_channel * (*requester)(char *type, int format, void *data);
- int (*devicestate)(void *data);
- struct chanlist *next;
- } *backends = NULL;
- struct ast_channel *channels = NULL;
-
- static ast_mutex_t chlock = AST_MUTEX_INITIALIZER;
- int ast_check_hangup(struct ast_channel *chan)
- {
- time_t myt;
-
- if (chan->_softhangup) return 1;
-
- if (!chan->pvt->pvt) return 1;
-
- if (!chan->whentohangup) return 0;
- time(&myt);
-
- if (chan->whentohangup > myt) return 0;
- chan->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
- return 1;
- }
- static int ast_check_hangup_locked(struct ast_channel *chan)
- {
- int res;
- ast_mutex_lock(&chan->lock);
- res = ast_check_hangup(chan);
- ast_mutex_unlock(&chan->lock);
- return res;
- }
- void ast_begin_shutdown(int hangup)
- {
- struct ast_channel *c;
- shutting_down = 1;
- if (hangup) {
- ast_mutex_lock(&chlock);
- c = channels;
- while(c) {
- ast_softhangup(c, AST_SOFTHANGUP_SHUTDOWN);
- c = c->next;
- }
- ast_mutex_unlock(&chlock);
- }
- }
- int ast_active_channels(void)
- {
- struct ast_channel *c;
- int cnt = 0;
- ast_mutex_lock(&chlock);
- c = channels;
- while(c) {
- cnt++;
- c = c->next;
- }
- ast_mutex_unlock(&chlock);
- return cnt;
- }
- void ast_cancel_shutdown(void)
- {
- shutting_down = 0;
- }
- int ast_shutting_down(void)
- {
- return shutting_down;
- }
- void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
- {
- time_t myt;
- time(&myt);
- if (offset)
- chan->whentohangup = myt + offset;
- else
- chan->whentohangup = 0;
- return;
- }
- int ast_channel_register(char *type, char *description, int capabilities,
- struct ast_channel *(*requester)(char *type, int format, void *data))
- {
- return ast_channel_register_ex(type, description, capabilities, requester, NULL);
- }
- int ast_channel_register_ex(char *type, char *description, int capabilities,
- struct ast_channel *(*requester)(char *type, int format, void *data),
- int (*devicestate)(void *data))
- {
- struct chanlist *chan, *last=NULL;
- if (ast_mutex_lock(&chlock)) {
- ast_log(LOG_WARNING, "Unable to lock channel list\n");
- return -1;
- }
- chan = backends;
- while(chan) {
- if (!strcasecmp(type, chan->type)) {
- ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", type);
- ast_mutex_unlock(&chlock);
- return -1;
- }
- last = chan;
- chan = chan->next;
- }
- chan = malloc(sizeof(struct chanlist));
- if (!chan) {
- ast_log(LOG_WARNING, "Out of memory\n");
- ast_mutex_unlock(&chlock);
- return -1;
- }
- strncpy(chan->type, type, sizeof(chan->type)-1);
- strncpy(chan->description, description, sizeof(chan->description)-1);
- chan->capabilities = capabilities;
- chan->requester = requester;
- chan->devicestate = devicestate;
- chan->next = NULL;
- if (last)
- last->next = chan;
- else
- backends = chan;
- if (option_debug)
- ast_log(LOG_DEBUG, "Registered handler for '%s' (%s)\n", chan->type, chan->description);
- else if (option_verbose > 1)
- ast_verbose( VERBOSE_PREFIX_2 "Registered channel type '%s' (%s)\n", chan->type, chan->description);
- ast_mutex_unlock(&chlock);
- return 0;
- }
- char *ast_state2str(int state)
- {
-
- static char localtmp[256];
- switch(state) {
- case AST_STATE_DOWN:
- return "Down";
- case AST_STATE_RESERVED:
- return "Rsrvd";
- case AST_STATE_OFFHOOK:
- return "OffHook";
- case AST_STATE_DIALING:
- return "Dialing";
- case AST_STATE_RING:
- return "Ring";
- case AST_STATE_RINGING:
- return "Ringing";
- case AST_STATE_UP:
- return "Up";
- case AST_STATE_BUSY:
- return "Busy";
- default:
- snprintf(localtmp, sizeof(localtmp), "Unknown (%d)\n", state);
- return localtmp;
- }
- }
- int ast_best_codec(int fmts)
- {
-
- int x;
- static int prefs[] =
- {
-
- AST_FORMAT_ULAW,
-
- AST_FORMAT_ALAW,
-
- AST_FORMAT_SLINEAR,
-
- AST_FORMAT_G726,
-
- AST_FORMAT_ADPCM,
-
- AST_FORMAT_GSM,
-
- AST_FORMAT_ILBC,
-
- AST_FORMAT_SPEEX,
-
- AST_FORMAT_LPC10,
-
- AST_FORMAT_G729A,
-
- AST_FORMAT_G723_1,
- };
-
-
- for (x=0;x<sizeof(prefs) / sizeof(prefs[0]); x++)
- if (fmts & prefs[x])
- return prefs[x];
- ast_log(LOG_WARNING, "Don't know any of 0x%x formats\n", fmts);
- return 0;
- }
- struct ast_channel *ast_channel_alloc(int needqueue)
- {
- struct ast_channel *tmp;
- struct ast_channel_pvt *pvt;
- int x;
- int flags;
- struct varshead *headp;
-
-
-
- if (shutting_down)
- return NULL;
- ast_mutex_lock(&chlock);
- tmp = malloc(sizeof(struct ast_channel));
- if (tmp) {
- memset(tmp, 0, sizeof(struct ast_channel));
- pvt = malloc(sizeof(struct ast_channel_pvt));
- if (pvt) {
- memset(pvt, 0, sizeof(struct ast_channel_pvt));
- tmp->sched = sched_context_create();
- if (tmp->sched) {
- for (x=0;x<AST_MAX_FDS - 1;x++)
- tmp->fds[x] = -1;
- if (needqueue &&
- pipe(pvt->alertpipe)) {
- ast_log(LOG_WARNING, "Alert pipe creation failed!\n");
- free(pvt);
- free(tmp);
- tmp = NULL;
- pvt = NULL;
- } else {
-
- if (needqueue) {
- flags = fcntl(pvt->alertpipe[0], F_GETFL);
- fcntl(pvt->alertpipe[0], F_SETFL, flags | O_NONBLOCK);
- flags = fcntl(pvt->alertpipe[1], F_GETFL);
- fcntl(pvt->alertpipe[1], F_SETFL, flags | O_NONBLOCK);
- } else
- pvt->alertpipe[0] = pvt->alertpipe[1] = -1;
- #ifdef ZAPTEL_OPTIMIZATIONS
- tmp->timingfd = open("/dev/zap/timer", O_RDWR);
- #else
- tmp->timingfd = -1;
- #endif
-
- tmp->fds[AST_MAX_FDS-1] = pvt->alertpipe[0];
-
- tmp->fds[AST_MAX_FDS-2] = tmp->timingfd;
- strncpy(tmp->name, "**Unknown**", sizeof(tmp->name)-1);
- tmp->pvt = pvt;
-
- tmp->_state = AST_STATE_DOWN;
- tmp->stack = -1;
- tmp->streamid = -1;
- tmp->appl = NULL;
- tmp->data = NULL;
- tmp->fin = 0;
- tmp->fout = 0;
- snprintf(tmp->uniqueid, sizeof(tmp->uniqueid), "%li.%d", (long)time(NULL), uniqueint++);
- headp=&tmp->varshead;
- ast_mutex_init(&tmp->lock);
- AST_LIST_HEAD_INIT(headp);
- tmp->vars=ast_var_assign("tempvar","tempval");
- AST_LIST_INSERT_HEAD(headp,tmp->vars,entries);
- strncpy(tmp->context, "default", sizeof(tmp->context)-1);
- strncpy(tmp->language, defaultlanguage, sizeof(tmp->language)-1);
- strncpy(tmp->exten, "s", sizeof(tmp->exten)-1);
- tmp->priority=1;
- tmp->amaflags = ast_default_amaflags;
- strncpy(tmp->accountcode, ast_default_accountcode, sizeof(tmp->accountcode)-1);
- tmp->next = channels;
- channels= tmp;
- }
- } else {
- ast_log(LOG_WARNING, "Unable to create schedule context\n");
- free(tmp);
- tmp = NULL;
- }
- } else {
- ast_log(LOG_WARNING, "Out of memory\n");
- free(tmp);
- tmp = NULL;
- }
- } else
- ast_log(LOG_WARNING, "Out of memory\n");
- ast_mutex_unlock(&chlock);
- return tmp;
- }
- int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int lock)
- {
- struct ast_frame *f;
- struct ast_frame *prev, *cur;
- int blah = 1;
- int qlen = 0;
-
- f = ast_frdup(fin);
- if (!f) {
- ast_log(LOG_WARNING, "Unable to duplicate frame\n");
- return -1;
- }
- if (lock)
- ast_mutex_lock(&chan->lock);
- prev = NULL;
- cur = chan->pvt->readq;
- while(cur) {
- prev = cur;
- cur = cur->next;
- qlen++;
- }
-
- if (((fin->frametype == AST_FRAME_VOICE) && (qlen > 96)) || (qlen > 128)) {
- if (fin->frametype != AST_FRAME_VOICE) {
- ast_log(LOG_WARNING, "Exceptionally long queue length queuing to %s\n", chan->name);
- CRASH;
- } else {
- ast_log(LOG_DEBUG, "Dropping voice to exceptionally long queue on %s\n", chan->name);
- ast_frfree(f);
- if (lock)
- ast_mutex_unlock(&chan->lock);
- return 0;
- }
- }
- if (prev)
- prev->next = f;
- else
- chan->pvt->readq = f;
- if (chan->pvt->alertpipe[1] > -1) {
- if (write(chan->pvt->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah))
- ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d (qlen = %d): %s!\n",
- chan->name, f->frametype, f->subclass, qlen, strerror(errno));
- } else if (chan->blocking) {
- pthread_kill(chan->blocker, SIGURG);
- }
- if (lock)
- ast_mutex_unlock(&chan->lock);
- return 0;
- }
- int ast_queue_hangup(struct ast_channel *chan, int lock)
- {
- struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
- chan->_softhangup |= AST_SOFTHANGUP_DEV;
- return ast_queue_frame(chan, &f, lock);
- }
- int ast_queue_control(struct ast_channel *chan, int control, int lock)
- {
- struct ast_frame f = { AST_FRAME_CONTROL, };
- f.subclass = control;
- return ast_queue_frame(chan, &f, lock);
- }
- int ast_channel_defer_dtmf(struct ast_channel *chan)
- {
- int pre = 0;
- if (chan) {
- pre = chan->deferdtmf;
- chan->deferdtmf = 1;
- }
- return pre;
- }
- void ast_channel_undefer_dtmf(struct ast_channel *chan)
- {
- if (chan)
- chan->deferdtmf = 0;
- }
- struct ast_channel *ast_channel_walk(struct ast_channel *prev)
- {
- struct ast_channel *l, *ret=NULL;
- ast_mutex_lock(&chlock);
- l = channels;
- if (!prev) {
- ast_mutex_unlock(&chlock);
- return l;
- }
- while(l) {
- if (l == prev)
- ret = l->next;
- l = l->next;
- }
- ast_mutex_unlock(&chlock);
- return ret;
-
- }
- int ast_safe_sleep_conditional( struct ast_channel *chan, int ms,
- int (*cond)(void*), void *data )
- {
- struct ast_frame *f;
- while(ms > 0) {
- if( cond && ((*cond)(data) == 0 ) )
- return 0;
- ms = ast_waitfor(chan, ms);
- if (ms <0)
- return -1;
- if (ms > 0) {
- f = ast_read(chan);
- if (!f)
- return -1;
- ast_frfree(f);
- }
- }
- return 0;
- }
- int ast_safe_sleep(struct ast_channel *chan, int ms)
- {
- struct ast_frame *f;
- while(ms > 0) {
- ms = ast_waitfor(chan, ms);
- if (ms <0)
- return -1;
- if (ms > 0) {
- f = ast_read(chan);
- if (!f)
- return -1;
- ast_frfree(f);
- }
- }
- return 0;
- }
- void ast_channel_free(struct ast_channel *chan)
- {
- struct ast_channel *last=NULL, *cur;
- int fd;
- struct ast_var_t *vardata;
- struct ast_frame *f, *fp;
- struct varshead *headp;
- char name[AST_CHANNEL_NAME];
-
- headp=&chan->varshead;
-
- ast_mutex_lock(&chlock);
- cur = channels;
- while(cur) {
- if (cur == chan) {
- if (last)
- last->next = cur->next;
- else
- channels = cur->next;
- break;
- }
- last = cur;
- cur = cur->next;
- }
- if (!cur)
- ast_log(LOG_WARNING, "Unable to find channel in list\n");
- if (chan->pvt->pvt)
- ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
- strncpy(name, chan->name, sizeof(name)-1);
-
-
- if (chan->monitor) {
- chan->monitor->stop( chan, 0 );
- }
-
- if (chan->pvt->readtrans)
- ast_translator_free_path(chan->pvt->readtrans);
- if (chan->pvt->writetrans)
- ast_translator_free_path(chan->pvt->writetrans);
- if (chan->pbx)
- ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
- if (chan->dnid)
- free(chan->dnid);
- if (chan->callerid)
- free(chan->callerid);
- if (chan->ani)
- free(chan->ani);
- if (chan->rdnis)
- free(chan->rdnis);
- ast_mutex_destroy(&chan->lock);
-
- if ((fd = chan->pvt->alertpipe[0]) > -1)
- close(fd);
- if ((fd = chan->pvt->alertpipe[1]) > -1)
- close(fd);
- if ((fd = chan->timingfd) > -1)
- close(fd);
- f = chan->pvt->readq;
- chan->pvt->readq = NULL;
- while(f) {
- fp = f;
- f = f->next;
- ast_frfree(fp);
- }
-
-
-
-
- while (!AST_LIST_EMPTY(headp)) {
- vardata = AST_LIST_FIRST(headp);
- AST_LIST_REMOVE_HEAD(headp, entries);
- ast_var_delete(vardata);
- }
-
- free(chan->pvt);
- chan->pvt = NULL;
- free(chan);
- ast_mutex_unlock(&chlock);
- ast_device_state_changed(name);
- }
- int ast_softhangup_nolock(struct ast_channel *chan, int cause)
- {
- int res = 0;
- struct ast_frame f = { AST_FRAME_NULL };
- if (option_debug)
- ast_log(LOG_DEBUG, "Soft-Hanging up channel '%s'\n", chan->name);
-
- chan->_softhangup |= cause;
- ast_queue_frame(chan, &f, 0);
-
- if (chan->blocking)
- pthread_kill(chan->blocker, SIGURG);
- return res;
- }
- int ast_softhangup(struct ast_channel *chan, int cause)
- {
- int res;
- ast_mutex_lock(&chan->lock);
- res = ast_softhangup_nolock(chan, cause);
- ast_mutex_unlock(&chan->lock);
- return res;
- }
- static int ast_do_masquerade(struct ast_channel *original);
- static void free_translation(struct ast_channel *clone)
- {
- if (clone->pvt->writetrans)
- ast_translator_free_path(clone->pvt->writetrans);
- if (clone->pvt->readtrans)
- ast_translator_free_path(clone->pvt->readtrans);
- clone->pvt->writetrans = NULL;
- clone->pvt->readtrans = NULL;
- clone->pvt->rawwriteformat = clone->nativeformats;
- clone->pvt->rawreadformat = clone->nativeformats;
- }
- int ast_hangup(struct ast_channel *chan)
- {
- int res = 0;
-
- ast_mutex_lock(&chan->lock);
- if (chan->masq) {
- if (ast_do_masquerade(chan))
- ast_log(LOG_WARNING, "Failed to perform masquerade\n");
- }
- if (chan->masq) {
- ast_log(LOG_WARNING, "%s getting hung up, but someone is trying to masq into us?!?\n", chan->name);
- ast_mutex_unlock(&chan->lock);
- return 0;
- }
-
- if (chan->masqr) {
- chan->zombie=1;
- ast_mutex_unlock(&chan->lock);
- return 0;
- }
- free_translation(chan);
- if (chan->stream)
- ast_stopstream(chan);
- if (chan->sched)
- sched_context_destroy(chan->sched);
-
- if (chan->generatordata)
- chan->generator->release(chan, chan->generatordata);
- chan->generatordata = NULL;
- chan->generator = NULL;
- if (chan->cdr) {
-
- ast_cdr_end(chan->cdr);
-
- ast_cdr_post(chan->cdr);
- ast_cdr_free(chan->cdr);
- }
- if (chan->blocking) {
- ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
- "is blocked by thread %ld in procedure %s! Expect a failure\n",
- (long)pthread_self(), chan->name, (long)chan->blocker, chan->blockproc);
- CRASH;
- }
- if (!chan->zombie) {
- if (option_debug)
- ast_log(LOG_DEBUG, "Hanging up channel '%s'\n", chan->name);
- if (chan->pvt->hangup)
- res = chan->pvt->hangup(chan);
- } else
- if (option_debug)
- ast_log(LOG_DEBUG, "Hanging up zombie '%s'\n", chan->name);
-
- ast_mutex_unlock(&chan->lock);
- manager_event(EVENT_FLAG_CALL, "Hangup",
- "Channel: %s\r\n"
- "Uniqueid: %s\r\n",
- chan->name, chan->uniqueid);
- ast_channel_free(chan);
- return res;
- }
- void ast_channel_unregister(char *type)
- {
- struct chanlist *chan, *last=NULL;
- if (option_debug)
- ast_log(LOG_DEBUG, "Unregistering channel type '%s'\n", type);
- if (ast_mutex_lock(&chlock)) {
- ast_log(LOG_WARNING, "Unable to lock channel list\n");
- return;
- }
- if (option_verbose > 1)
- ast_verbose( VERBOSE_PREFIX_2 "Unregistered channel type '%s'\n", type);
- chan = backends;
- while(chan) {
- if (!strcasecmp(chan->type, type)) {
- if (last)
- last->next = chan->next;
- else
- backends = backends->next;
- free(chan);
- ast_mutex_unlock(&chlock);
- return;
- }
- last = chan;
- chan = chan->next;
- }
- ast_mutex_unlock(&chlock);
- }
- int ast_answer(struct ast_channel *chan)
- {
- int res = 0;
-
- if (chan->zombie || ast_check_hangup(chan))
- return -1;
- switch(chan->_state) {
- case AST_STATE_RINGING:
- case AST_STATE_RING:
- ast_mutex_lock(&chan->lock);
- if (chan->pvt->answer)
- res = chan->pvt->answer(chan);
- ast_mutex_unlock(&chan->lock);
- ast_setstate(chan, AST_STATE_UP);
- if (chan->cdr)
- ast_cdr_answer(chan->cdr);
- return res;
- break;
- case AST_STATE_UP:
- if (chan->cdr)
- ast_cdr_answer(chan->cdr);
- break;
- }
- return 0;
- }
- void ast_deactivate_generator(struct ast_channel *chan)
- {
- if (chan->generatordata) {
- if (chan->generator && chan->generator->release)
- chan->generator->release(chan, chan->generatordata);
- chan->generatordata = NULL;
- chan->generator = NULL;
- chan->writeinterrupt = 0;
- }
- }
- int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
- {
- if (chan->generatordata) {
- if (chan->generator && chan->generator->release)
- chan->generator->release(chan, chan->generatordata);
- chan->generatordata = NULL;
- }
- ast_prod(chan);
- if ((chan->generatordata = gen->alloc(chan, params))) {
- chan->generator = gen;
- } else {
- return -1;
- }
- return 0;
- }
- int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
- {
-
- struct timeval tv;
- fd_set rfds, efds;
- int res;
- int x, max=-1;
- int winner = -1;
-
- tv.tv_sec = *ms / 1000;
- tv.tv_usec = (*ms % 1000) * 1000;
- FD_ZERO(&rfds);
- FD_ZERO(&efds);
- for (x=0;x<n;x++) {
- if (fds[x] > -1) {
- FD_SET(fds[x], &rfds);
- FD_SET(fds[x], &efds);
- if (fds[x] > max)
- max = fds[x];
- }
- }
- if (*ms >= 0)
- res = ast_select(max + 1, &rfds, NULL, &efds, &tv);
- else
- res = ast_select(max + 1, &rfds, NULL, &efds, NULL);
- if (res < 0) {
-
- if (errno != EINTR)
- *ms = -1;
- else
- *ms = 0;
- return -1;
- }
- for (x=0;x<n;x++) {
- if ((fds[x] > -1) && (FD_ISSET(fds[x], &rfds) || FD_ISSET(fds[x], &efds)) && (winner < 0)) {
- if (exception)
- *exception = FD_ISSET(fds[x], &efds);
- winner = fds[x];
- }
- }
- *ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
- return winner;
- }
- struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
- int *exception, int *outfd, int *ms)
- {
-
- struct timeval tv;
- fd_set rfds, efds;
- int res;
- int x, y, max=-1;
- time_t now = 0;
- long whentohangup = 0, havewhen = 0, diff;
- struct ast_channel *winner = NULL;
- if (outfd)
- *outfd = -99999;
- if (exception)
- *exception = 0;
-
-
- for (x=0;x<n;x++) {
- ast_mutex_lock(&c[x]->lock);
- if (c[x]->whentohangup) {
- if (!havewhen)
- time(&now);
- diff = c[x]->whentohangup - now;
- if (!havewhen || (diff < whentohangup)) {
- havewhen++;
- whentohangup = diff;
- }
- }
- if (c[x]->masq) {
- if (ast_do_masquerade(c[x])) {
- ast_log(LOG_WARNING, "Masquerade failed\n");
- *ms = -1;
- ast_mutex_unlock(&c[x]->lock);
- return NULL;
- }
- }
- ast_mutex_unlock(&c[x]->lock);
- }
-
- tv.tv_sec = *ms / 1000;
- tv.tv_usec = (*ms % 1000) * 1000;
-
- if (havewhen) {
- if ((*ms < 0) || (whentohangup * 1000 < *ms)) {
- tv.tv_sec = whentohangup;
- tv.tv_usec = 0;
- }
- }
- FD_ZERO(&rfds);
- FD_ZERO(&efds);
- for (x=0;x<n;x++) {
- for (y=0;y<AST_MAX_FDS;y++) {
- if (c[x]->fds[y] > -1) {
- FD_SET(c[x]->fds[y], &rfds);
- FD_SET(c[x]->fds[y], &efds);
- if (c[x]->fds[y] > max)
- max = c[x]->fds[y];
- }
- }
- CHECK_BLOCKING(c[x]);
- }
- for (x=0;x<nfds; x++) {
- FD_SET(fds[x], &rfds);
- FD_SET(fds[x], &efds);
- if (fds[x] > max)
- max = fds[x];
- }
- if ((*ms >= 0) || (havewhen))
- res = ast_select(max + 1, &rfds, NULL, &efds, &tv);
- else
- res = ast_select(max + 1, &rfds, NULL, &efds, NULL);
- if (res < 0) {
- for (x=0;x<n;x++)
- c[x]->blocking = 0;
-
- if (errno != EINTR)
- *ms = -1;
- else {
-
- #if 0
- *ms = 0;
- #endif
- }
- return NULL;
- }
- if (havewhen)
- time(&now);
- for (x=0;x<n;x++) {
- c[x]->blocking = 0;
- if (havewhen && c[x]->whentohangup && (now > c[x]->whentohangup)) {
- c[x]->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
- if (!winner)
- winner = c[x];
- }
- for (y=0;y<AST_MAX_FDS;y++) {
- if (c[x]->fds[y] > -1) {
- if ((FD_ISSET(c[x]->fds[y], &rfds) || FD_ISSET(c[x]->fds[y], &efds)) && !winner) {
-
- if (FD_ISSET(c[x]->fds[y], &efds))
- c[x]->exception = 1;
- c[x]->fdno = y;
- winner = c[x];
- }
- }
- }
- }
- for (x=0;x<nfds;x++) {
- if ((FD_ISSET(fds[x], &rfds) || FD_ISSET(fds[x], &efds)) && !winner) {
- if (outfd)
- *outfd = fds[x];
- if (FD_ISSET(fds[x], &efds) && exception)
- *exception = 1;
- winner = NULL;
- }
- }
- *ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
- return winner;
- }
- struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
- {
- return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
- }
- int ast_waitfor(struct ast_channel *c, int ms)
- {
- struct ast_channel *chan;
- int oldms = ms;
- chan = ast_waitfor_n(&c, 1, &ms);
- if (ms < 0) {
- if (oldms < 0)
- return 0;
- else
- return -1;
- }
- return ms;
- }
- char ast_waitfordigit(struct ast_channel *c, int ms)
- {
-
- struct ast_frame *f;
- char result = 0;
-
- if (c->zombie || ast_check_hangup(c))
- return -1;
-
- while(ms && !result) {
- ms = ast_waitfor(c, ms);
- if (ms < 0)
- result = -1;
- else if (ms > 0) {
-
- f = ast_read(c);
- if (f) {
- if (f->frametype == AST_FRAME_DTMF)
- result = f->subclass;
- ast_frfree(f);
- } else
- result = -1;
- }
- }
- return result;
- }
- int ast_settimeout(struct ast_channel *c, int samples, int (*func)(void *data), void *data)
- {
- int res = -1;
- #ifdef ZAPTEL_OPTIMIZATIONS
- if (c->timingfd > -1) {
- if (!func) {
- samples = 0;
- data = 0;
- }
- ast_log(LOG_DEBUG, "Scheduling timer at %d sample intervals\n", samples);
- res = ioctl(c->timingfd, ZT_TIMERCONFIG, &samples);
- c->timingfunc = func;
- c->timingdata = data;
- }
- #endif
- return res;
- }
- char ast_waitfordigit_full(struct ast_channel *c, int ms, int audio, int ctrl)
- {
- struct ast_frame *f;
- char result = 0;
- struct ast_channel *rchan;
- int outfd;
-
- if (c->zombie || ast_check_hangup(c))
- return -1;
-
- while(ms && !result) {
- rchan = ast_waitfor_nandfds(&c, 1, &audio, (audio > -1) ? 1 : 0, NULL, &outfd, &ms);
- if ((!rchan) && (outfd < 0) && (ms))
- result = -1;
- else if (outfd > -1) {
- result = 1;
- } else if (rchan) {
-
- f = ast_read(c);
- if (f) {
- if (f->frametype == AST_FRAME_DTMF)
- result = f->subclass;
- ast_frfree(f);
- } else
- result = -1;
- }
- }
- return result;
- }
- struct ast_frame *ast_read(struct ast_channel *chan)
- {
- struct ast_frame *f = NULL;
- int blah;
- #ifdef ZAPTEL_OPTIMIZATIONS
- int (*func)(void *);
- void *data;
- #endif
- static struct ast_frame null_frame =
- {
- AST_FRAME_NULL,
- };
-
- ast_mutex_lock(&chan->lock);
- if (chan->masq) {
- if (ast_do_masquerade(chan)) {
- ast_log(LOG_WARNING, "Failed to perform masquerade\n");
- f = NULL;
- } else
- f = &null_frame;
- ast_mutex_unlock(&chan->lock);
- return f;
- }
-
- if (chan->zombie || ast_check_hangup(chan)) {
- if (chan->generator)
- ast_deactivate_generator(chan);
- ast_mutex_unlock(&chan->lock);
- return NULL;
- }
- if (!chan->deferdtmf && strlen(chan->dtmfq)) {
-
- chan->dtmff.frametype = AST_FRAME_DTMF;
- chan->dtmff.subclass = chan->dtmfq[0];
-
- memmove(chan->dtmfq, chan->dtmfq + 1, sizeof(chan->dtmfq) - 1);
- ast_mutex_unlock(&chan->lock);
- return &chan->dtmff;
- }
-
-
- if (chan->pvt->alertpipe[0] > -1) {
- read(chan->pvt->alertpipe[0], &blah, sizeof(blah));
- }
- #ifdef ZAPTEL_OPTIMIZATIONS
- if ((chan->timingfd > -1) && (chan->fdno == AST_MAX_FDS - 2) && chan->exception) {
- chan->exception = 0;
- blah = -1;
- ioctl(chan->timingfd, ZT_TIMERACK, &blah);
- func = chan->timingfunc;
- data = chan->timingdata;
- ast_mutex_unlock(&chan->lock);
- if (func) {
- #if 0
- ast_log(LOG_DEBUG, "Calling private function\n");
- #endif
- func(data);
- } else {
- blah = 0;
- ast_mutex_lock(&chan->lock);
- ioctl(chan->timingfd, ZT_TIMERCONFIG, &blah);
- chan->timingdata = NULL;
- ast_mutex_unlock(&chan->lock);
- }
- f = &null_frame;
- return f;
- }
- #endif
-
- if (chan->pvt->readq) {
- f = chan->pvt->readq;
- chan->pvt->readq = f->next;
-
- if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) {
- ast_frfree(f);
- f = NULL;
- }
- } else {
- chan->blocker = pthread_self();
- if (chan->exception) {
- if (chan->pvt->exception)
- f = chan->pvt->exception(chan);
- else {
- ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", chan->name);
- f = &null_frame;
- }
-
- chan->exception = 0;
- } else
- if (chan->pvt->read)
- f = chan->pvt->read(chan);
- else
- ast_log(LOG_WARNING, "No read routine on channel %s\n", chan->name);
- }
- if (f && (f->frametype == AST_FRAME_VOICE)) {
- if (!(f->subclass & chan->nativeformats)) {
-
- ast_log(LOG_NOTICE, "Dropping incompatible voice frame on %s of format %s since our native format has changed to %s\n", chan->name, ast_getformatname(f->subclass), ast_getformatname(chan->nativeformats));
- ast_frfree(f);
- f = &null_frame;
- } else {
- if (chan->monitor && chan->monitor->read_stream ) {
- #ifndef MONITOR_CONSTANT_DELAY
- int jump = chan->outsmpl - chan->insmpl - 2 * f->samples;
- if (jump >= 0) {
- if (ast_seekstream(chan->monitor->read_stream, jump + f->samples, SEEK_FORCECUR) == -1)
- ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
- chan->insmpl += jump + 2 * f->samples;
- } else
- chan->insmpl+= f->samples;
- #else
- int jump = chan->outsmpl - chan->insmpl;
- if (jump - MONITOR_DELAY >= 0) {
- if (ast_seekstream(chan->monitor->read_stream, jump - f->samples, SEEK_FORCECUR) == -1)
- ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
- chan->insmpl += jump;
- } else
- chan->insmpl += f->samples;
- #endif
- if (ast_writestream(chan->monitor->read_stream, f) < 0)
- ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n");
- }
- if (chan->pvt->readtrans) {
- f = ast_translate(chan->pvt->readtrans, f, 1);
- if (!f)
- f = &null_frame;
- }
- }
- }
-
- if (!f) {
- chan->_softhangup |= AST_SOFTHANGUP_DEV;
- if (chan->generator)
- ast_deactivate_generator(chan);
-
- if (chan->cdr)
- ast_cdr_end(chan->cdr);
- } else if (chan->deferdtmf && f->frametype == AST_FRAME_DTMF) {
- if (strlen(chan->dtmfq) < sizeof(chan->dtmfq) - 2)
- chan->dtmfq[strlen(chan->dtmfq)] = f->subclass;
- else
- ast_log(LOG_WARNING, "Dropping deferred DTMF digits on %s\n", chan->name);
- f = &null_frame;
- } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
-
- ast_setstate(chan, AST_STATE_UP);
- ast_cdr_answer(chan->cdr);
- }
- ast_mutex_unlock(&chan->lock);
-
- if (f && (f->frametype == AST_FRAME_VOICE) && chan->generatordata) {
-
- void *tmp;
- int res;
- tmp = chan->generatordata;
- chan->generatordata = NULL;
- res = chan->generator->generate(chan, tmp, f->datalen, f->samples);
- chan->generatordata = tmp;
- if (res) {
- ast_log(LOG_DEBUG, "Auto-deactivating generator\n");
- ast_deactivate_generator(chan);
- }
- }
- if (chan->fin & 0x80000000)
- ast_frame_dump(chan->name, f, "<<");
- if ((chan->fin & 0x7fffffff) == 0x7fffffff)
- chan->fin &= 0x80000000;
- else
- chan->fin++;
- return f;
- }
- int ast_indicate(struct ast_channel *chan, int condition)
- {
- int res = -1;
-
- if (chan->zombie || ast_check_hangup(chan))
- return -1;
- ast_mutex_lock(&chan->lock);
- if (chan->pvt->indicate)
- res = chan->pvt->indicate(chan, condition);
- ast_mutex_unlock(&chan->lock);
- if (!chan->pvt->indicate || res) {
-
- if (condition >= 0) {
- const struct tone_zone_sound *ts = NULL;
- switch (condition) {
- case AST_CONTROL_RINGING:
- ts = ast_get_indication_tone(chan->zone, "ring");
- break;
- case AST_CONTROL_BUSY:
- ts = ast_get_indication_tone(chan->zone, "busy");
- break;
- case AST_CONTROL_CONGESTION:
- ts = ast_get_indication_tone(chan->zone, "congestion");
- break;
- }
- if (ts && ts->data[0]) {
- ast_log(LOG_DEBUG, "Driver for channel '%s' does not support indication %d, emulating it\n", chan->name, condition);
- ast_playtones_start(chan,0,ts->data, 1);
- res = 0;
- } else if (condition == AST_CONTROL_PROGRESS) {
-
- } else {
-
- ast_log(LOG_WARNING, "Unable to handle indication %d for '%s'\n", condition, chan->name);
- res = -1;
- }
- }
- else ast_playtones_stop(chan);
- }
- return res;
- }
- int ast_recvchar(struct ast_channel *chan, int timeout)
- {
- int res,ourto,c;
- struct ast_frame *f;
-
- ourto = timeout;
- for(;;)
- {
- if (ast_check_hangup(chan)) return -1;
- res = ast_waitfor(chan,ourto);
- if (res <= 0)
- {
- return 0;
- }
- ourto = res;
- f = ast_read(chan);
- if (f == NULL) return -1;
- if ((f->frametype == AST_FRAME_CONTROL) &&
- (f->subclass == AST_CONTROL_HANGUP)) return -1;
- if (f->frametype == AST_FRAME_TEXT)
- {
- c = *((char *)f->data);
- ast_frfree(f);
- return(c);
- }
- ast_frfree(f);
- }
- }
- int ast_sendtext(struct ast_channel *chan, char *text)
- {
- int res = 0;
-
- if (chan->zombie || ast_check_hangup(chan))
- return -1;
- CHECK_BLOCKING(chan);
- if (chan->pvt->send_text)
- res = chan->pvt->send_text(chan, text);
- chan->blocking = 0;
- return res;
- }
- static int do_senddigit(struct ast_channel *chan, char digit)
- {
- int res = -1;
- if (chan->pvt->send_digit)
- res = chan->pvt->send_digit(chan, digit);
- if (!chan->pvt->send_digit || res) {
-
- static const char* dtmf_tones[] = {
- "!941+1336/50,!0/50",
- "!697+1209/50,!0/50",
- "!697+1336/50,!0/50",
- "!697+1477/50,!0/50",
- "!770+1209/50,!0/50",
- "!770+1336/50,!0/50",
- "!770+1477/50,!0/50",
- "!852+1209/50,!0/50",
- "!852+1336/50,!0/50",
- "!852+1477/50,!0/50",
- "!697+1633/50,!0/50",
- "!770+1633/50,!0/50",
- "!852+1633/50,!0/50",
- "!941+1633/50,!0/50",
- "!941+1209/50,!0/50",
- "!941+1477/50,!0/50" };
- if (digit >= '0' && digit <='9')
- ast_playtones_start(chan,0,dtmf_tones[digit-'0'], 0);
- else if (digit >= 'A' && digit <= 'D')
- ast_playtones_start(chan,0,dtmf_tones[digit-'A'+10], 0);
- else if (digit == '*')
- ast_playtones_start(chan,0,dtmf_tones[14], 0);
- else if (digit == '#')
- ast_playtones_start(chan,0,dtmf_tones[15], 0);
- else {
-
- ast_log(LOG_WARNING, "Unable to handle DTMF tone '%c' for '%s'\n", digit, chan->name);
- return -1;
- }
- }
- return 0;
- }
- int ast_prod(struct ast_channel *chan)
- {
- struct ast_frame a = { AST_FRAME_VOICE };
- char nothing[128];
-
- if (chan->_state != AST_STATE_UP) {
- ast_log(LOG_DEBUG, "Prodding channel '%s'\n", chan->name);
- a.subclass = chan->pvt->rawwriteformat;
- a.data = nothing + AST_FRIENDLY_OFFSET;
- if (ast_write(chan, &a))
- ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", chan->name);
- }
- return 0;
- }
- int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
- {
- int res;
- if (!chan->pvt->write_video)
- return 0;
- res = ast_write(chan, fr);
- if (!res)
- res = 1;
- return res;
- }
- int ast_write(struct ast_channel *chan, struct ast_frame *fr)
- {
- int res = -1;
- struct ast_frame *f = NULL;
-
- ast_mutex_lock(&chan->lock);
- if (chan->zombie || ast_check_hangup(chan)) {
- ast_mutex_unlock(&chan->lock);
- return -1;
- }
-
- if (chan->masq) {
- if (ast_do_masquerade(chan)) {
- ast_log(LOG_WARNING, "Failed to perform masquerade\n");
- ast_mutex_unlock(&chan->lock);
- return -1;
- }
- }
- if (chan->masqr) {
- ast_mutex_unlock(&chan->lock);
- return 0;
- }
- if (chan->generatordata) {
- if (chan->writeinterrupt)
- ast_deactivate_generator(chan);
- else {
- ast_mutex_unlock(&chan->lock);
- return 0;
- }
- }
- if (chan->fout & 0x80000000)
- ast_frame_dump(chan->name, fr, ">>");
- CHECK_BLOCKING(chan);
- switch(fr->frametype) {
- case AST_FRAME_CONTROL:
-
- ast_log(LOG_WARNING, "Don't know how to handle control frames yet\n");
- break;
- case AST_FRAME_DTMF:
- chan->blocking = 0;
- ast_mutex_unlock(&chan->lock);
- res = do_senddigit(chan,fr->subclass);
- ast_mutex_lock(&chan->lock);
- CHECK_BLOCKING(chan);
- break;
- case AST_FRAME_TEXT:
- if (chan->pvt->send_text)
- res = chan->pvt->send_text(chan, (char *) fr->data);
- break;
- case AST_FRAME_VIDEO:
-
- if (chan->pvt->write_video)
- res = chan->pvt->write_video(chan, fr);
- else
- res = 0;
- break;
- default:
- if (chan->pvt->write) {
- if (chan->pvt->writetrans) {
- f = ast_translate(chan->pvt->writetrans, fr, 0);
- } else
- f = fr;
- if (f) {
- res = chan->pvt->write(chan, f);
- if( chan->monitor &&
- chan->monitor->write_stream &&
- f && ( f->frametype == AST_FRAME_VOICE ) ) {
- #ifndef MONITOR_CONSTANT_DELAY
- int jump = chan->insmpl - chan->outsmpl - 2 * f->samples;
- if (jump >= 0) {
- if (ast_seekstream(chan->monitor->write_stream, jump + f->samples, SEEK_FORCECUR) == -1)
- ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
- chan->outsmpl += jump + 2 * f->samples;
- } else
- chan->outsmpl += f->samples;
- #else
- int jump = chan->insmpl - chan->outsmpl;
- if (jump - MONITOR_DELAY >= 0) {
- if (ast_seekstream(chan->monitor->write_stream, jump - f->samples, SEEK_FORCECUR) == -1)
- ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
- chan->outsmpl += jump;
- } else
- chan->outsmpl += f->samples;
- #endif
- if (ast_writestream(chan->monitor->write_stream, f) < 0)
- ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n");
- }
- } else
- res = 0;
- }
- }
- if (f && (f != fr))
- ast_frfree(f);
- chan->blocking = 0;
-
- if (res < 0)
- chan->_softhangup |= AST_SOFTHANGUP_DEV;
- else {
- if ((chan->fout & 0x7fffffff) == 0x7fffffff)
- chan->fout &= 0x80000000;
- else
- chan->fout++;
- chan->fout++;
- }
- ast_mutex_unlock(&chan->lock);
- return res;
- }
- int ast_set_write_format(struct ast_channel *chan, int fmts)
- {
- int fmt;
- int native;
- int res;
-
- native = chan->nativeformats;
- fmt = fmts;
-
- res = ast_translator_best_choice(&native, &fmt);
- if (res < 0) {
- ast_log(LOG_NOTICE, "Unable to find a path from %s to %s\n",
- ast_getformatname(fmts), ast_getformatname(chan->nativeformats));
- return -1;
- }
-
-
- chan->pvt->rawwriteformat = native;
-
- chan->writeformat = fmt;
-
- if (chan->pvt->writetrans)
- ast_translator_free_path(chan->pvt->writetrans);
-
- chan->pvt->writetrans = ast_translator_build_path(chan->pvt->rawwriteformat, chan->writeformat);
- if (option_debug)
- ast_log(LOG_DEBUG, "Set channel %s to write format %s\n", chan->name, ast_getformatname(chan->writeformat));
- return 0;
- }
- int ast_set_read_format(struct ast_channel *chan, int fmts)
- {
- int fmt;
- int native;
- int res;
-
- native = chan->nativeformats;
- fmt = fmts;
-
- res = ast_translator_best_choice(&fmt, &native);
- if (res < 0) {
- ast_log(LOG_NOTICE, "Unable to find a path from %s to %s\n",
- ast_getformatname(chan->nativeformats), ast_getformatname(fmts));
- return -1;
- }
-
-
- chan->pvt->rawreadformat = native;
-
- chan->readformat = fmt;
-
- if (chan->pvt->readtrans)
- ast_translator_free_path(chan->pvt->readtrans);
-
- chan->pvt->readtrans = ast_translator_build_path(chan->readformat, chan->pvt->rawreadformat);
- if (option_debug)
- ast_log(LOG_DEBUG, "Set channel %s to read format %s\n",
- chan->name, ast_getformatname(chan->readformat));
- return 0;
- }
- struct ast_channel *__ast_request_and_dial(char *type, int format, void *data, int timeout, int *outstate, char *callerid, struct outgoing_helper *oh)
- {
- int state = 0;
- struct ast_channel *chan;
- struct ast_frame *f;
- int res = 0;
- char *variable;
- chan = ast_request(type, format, data);
- if (chan) {
- if (oh) {
- char *tmp, *var;
-
- if (oh->variable)
- variable = ast_strdupa(oh->variable);
- else
- variable = NULL;
- tmp = variable;
-
- while( (var = strtok_r(NULL, "|", &tmp)) ) {
- pbx_builtin_setvar( chan, var );
- }
- if (oh->callerid && *oh->callerid)
- ast_set_callerid(chan, oh->callerid, 1);
- if (oh->account && *oh->account)
- ast_cdr_setaccount(chan, oh->account);
- }
- if (callerid && strlen(callerid))
- ast_set_callerid(chan, callerid, 1);
- if (!ast_call(chan, data, 0)) {
- while(timeout && (chan->_state != AST_STATE_UP)) {
- res = ast_waitfor(chan, timeout);
- if (res < 0) {
-
- break;
- }
-
- if (!res)
- break;
- if (timeout > -1)
- timeout = res;
- f = ast_read(chan);
- if (!f) {
- state = AST_CONTROL_HANGUP;
- res = 0;
- break;
- }
- if (f->frametype == AST_FRAME_CONTROL) {
- if (f->subclass == AST_CONTROL_RINGING)
- state = AST_CONTROL_RINGING;
- else if ((f->subclass == AST_CONTROL_BUSY) || (f->subclass == AST_CONTROL_CONGESTION)) {
- state = f->subclass;
- ast_frfree(f);
- break;
- } else if (f->subclass == AST_CONTROL_ANSWER) {
- state = f->subclass;
- ast_frfree(f);
- break;
- } else if (f->subclass == -1) {
-
- } else {
- ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass);
- }
- }
- ast_frfree(f);
- }
- } else
- ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
- } else
- ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
- if (chan) {
-
- if (oh) {
- if (oh->context && *oh->context)
- strncpy(chan->context, oh->context, sizeof(chan->context) - 1);
- if (oh->exten && *oh->exten)
- strncpy(chan->exten, oh->exten, sizeof(chan->exten) - 1);
- chan->priority = oh->priority;
- }
- if (chan->_state == AST_STATE_UP)
- state = AST_CONTROL_ANSWER;
- }
- if (outstate)
- *outstate = state;
- if (chan && res <= 0) {
- if (!chan->cdr) {
- chan->cdr = ast_cdr_alloc();
- if (chan->cdr)
- ast_cdr_init(chan->cdr, chan);
- }
- if (chan->cdr) {
- char tmp[256];
- snprintf(tmp, 256, "%s/%s", type, (char *)data);
- ast_cdr_setapp(chan->cdr,"Dial",tmp);
- ast_cdr_update(chan);
- ast_cdr_start(chan->cdr);
- ast_cdr_end(chan->cdr);
-
- if (ast_cdr_disposition(chan->cdr,chan->hangupcause))
- ast_cdr_failed(chan->cdr);
- } else
- ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
- ast_hangup(chan);
- chan = NULL;
- }
- return chan;
- }
- struct ast_channel *ast_request_and_dial(char *type, int format, void *data, int timeout, int *outstate, char *callerid)
- {
- return __ast_request_and_dial(type, format, data, timeout, outstate, callerid, NULL);
- }
- struct ast_channel *ast_request(char *type, int format, void *data)
- {
- struct chanlist *chan;
- struct ast_channel *c = NULL;
- int capabilities;
- int fmt;
- int res;
- if (ast_mutex_lock(&chlock)) {
- ast_log(LOG_WARNING, "Unable to lock channel list\n");
- return NULL;
- }
- chan = backends;
- while(chan) {
- if (!strcasecmp(type, chan->type)) {
- capabilities = chan->capabilities;
- fmt = format;
- res = ast_translator_best_choice(&fmt, &capabilities);
- if (res < 0) {
- ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %d) to %d\n", type, chan->capabilities, format);
- ast_mutex_unlock(&chlock);
- return NULL;
- }
- ast_mutex_unlock(&chlock);
- if (chan->requester)
- c = chan->requester(type, capabilities, data);
- if (c) {
- if (c->_state == AST_STATE_DOWN) {
- manager_event(EVENT_FLAG_CALL, "Newchannel",
- "Channel: %s\r\n"
- "State: %s\r\n"
- "Callerid: %s\r\n"
- "Uniqueid: %s\r\n",
- c->name, ast_state2str(c->_state), c->callerid ? c->callerid : "<unknown>", c->uniqueid);
- }
- }
- return c;
- }
- chan = chan->next;
- }
- if (!chan)
- ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
- ast_mutex_unlock(&chlock);
- return c;
- }
- int ast_parse_device_state(char *device)
- {
- char name[AST_CHANNEL_NAME] = "";
- char *cut;
- struct ast_channel *chan;
- chan = ast_channel_walk(NULL);
- while (chan) {
- strncpy(name, chan->name, sizeof(name)-1);
- cut = strchr(name,'-');
- if (cut)
- *cut = 0;
- if (!strcmp(name, device))
- return AST_DEVICE_INUSE;
- chan = ast_channel_walk(chan);
- }
- return AST_DEVICE_UNKNOWN;
- }
- int ast_device_state(char *device)
- {
- char tech[AST_MAX_EXTENSION] = "";
- char *number;
- struct chanlist *chanls;
- int res = 0;
-
- strncpy(tech, device, sizeof(tech)-1);
- number = strchr(tech, '/');
- if (!number) {
- return AST_DEVICE_INVALID;
- }
- *number = 0;
- number++;
-
- if (ast_mutex_lock(&chlock)) {
- ast_log(LOG_WARNING, "Unable to lock channel list\n");
- return -1;
- }
- chanls = backends;
- while(chanls) {
- if (!strcasecmp(tech, chanls->type)) {
- ast_mutex_unlock(&chlock);
- if (!chanls->devicestate)
- return ast_parse_device_state(device);
- else {
- res = chanls->devicestate(number);
- if (res == AST_DEVICE_UNKNOWN)
- return ast_parse_device_state(device);
- else
- return res;
- }
- }
- chanls = chanls->next;
- }
- ast_mutex_unlock(&chlock);
- return AST_DEVICE_INVALID;
- }
- int ast_call(struct ast_channel *chan, char *addr, int timeout)
- {
-
- int res = -1;
-
- ast_mutex_lock(&chan->lock);
- if (!chan->zombie && !ast_check_hangup(chan))
- if (chan->pvt->call)
- res = chan->pvt->call(chan, addr, timeout);
- ast_mutex_unlock(&chan->lock);
- return res;
- }
- int ast_transfer(struct ast_channel *chan, char *dest)
- {
-
- int res = -1;
-
- ast_mutex_lock(&chan->lock);
- if (!chan->zombie && !ast_check_hangup(chan)) {
- if (chan->pvt->transfer) {
- res = chan->pvt->transfer(chan, dest);
- if (!res)
- res = 1;
- } else
- res = 0;
- }
- ast_mutex_unlock(&chan->lock);
- return res;
- }
- int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
- {
- int pos=0;
- int to = ftimeout;
- char d;
-
-
- if (c->zombie || ast_check_hangup(c))
- return -1;
- if (!len)
- return -1;
- do {
- if (c->stream) {
- d = ast_waitstream(c, AST_DIGIT_ANY);
- ast_stopstream(c);
- usleep(1000);
- if (!d)
- d = ast_waitfordigit(c, to);
- } else {
- d = ast_waitfordigit(c, to);
- }
- if (d < 0)
- return -1;
- if (d == 0) {
- s[pos]='\0';
- return 1;
- }
- if (!strchr(enders, d))
- s[pos++] = d;
- if (strchr(enders, d) || (pos >= len)) {
- s[pos]='\0';
- return 0;
- }
- to = timeout;
- } while(1);
-
- return 0;
- }
- int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
- {
- int pos=0;
- int to = ftimeout;
- char d;
-
- if (c->zombie || ast_check_hangup(c))
- return -1;
- if (!len)
- return -1;
- do {
- if (c->stream) {
- d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
- ast_stopstream(c);
- usleep(1000);
- if (!d)
- d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
- } else {
- d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
- }
- if (d < 0)
- return -1;
- if (d == 0) {
- s[pos]='\0';
- return 1;
- }
- if (d == 1) {
- s[pos]='\0';
- return 2;
- }
- if (!strchr(enders, d))
- s[pos++] = d;
- if (strchr(enders, d) || (pos >= len)) {
- s[pos]='\0';
- return 0;
- }
- to = timeout;
- } while(1);
-
- return 0;
- }
- int ast_channel_supports_html(struct ast_channel *chan)
- {
- if (chan->pvt->send_html)
- return 1;
- return 0;
- }
- int ast_channel_sendhtml(struct ast_channel *chan, int subclass, char *data, int datalen)
- {
- if (chan->pvt->send_html)
- return chan->pvt->send_html(chan, subclass, data, datalen);
- return -1;
- }
- int ast_channel_sendurl(struct ast_channel *chan, char *url)
- {
- if (chan->pvt->send_html)
- return chan->pvt->send_html(chan, AST_HTML_URL, url, strlen(url) + 1);
- return -1;
- }
- int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
- {
- int peerf;
- int chanf;
- int res;
- peerf = peer->nativeformats;
- chanf = chan->nativeformats;
- res = ast_translator_best_choice(&peerf, &chanf);
- if (res < 0) {
- ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", chan->name, chan->nativeformats, peer->name, peer->nativeformats);
- return -1;
- }
-
- res = ast_set_read_format(chan, peerf);
- if (res < 0) {
- ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", chan->name, chanf);
- return -1;
- }
-
- res = ast_set_write_format(peer, peerf);
- if (res < 0) {
- ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", peer->name, peerf);
- return -1;
- }
-
- peerf = peer->nativeformats;
- chanf = chan->nativeformats;
- res = ast_translator_best_choice(&chanf, &peerf);
- if (res < 0) {
- ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", peer->name, peer->nativeformats, chan->name, chan->nativeformats);
- return -1;
- }
-
- res = ast_set_write_format(chan, chanf);
- if (res < 0) {
- ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", chan->name, chanf);
- return -1;
- }
-
- res = ast_set_read_format(peer, chanf);
- if (res < 0) {
- ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", peer->name, peerf);
- return -1;
- }
- return 0;
- }
- int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone)
- {
- struct ast_frame null = { AST_FRAME_NULL, };
- ast_log(LOG_DEBUG, "Planning to masquerade %s into the structure of %s\n",
- clone->name, original->name);
- if (original->masq) {
- ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
- original->masq->name, original->name);
- return -1;
- }
- if (clone->masqr) {
- ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
- clone->name, clone->masqr->name);
- return -1;
- }
- original->masq = clone;
- clone->masqr = original;
-
- ast_queue_frame(original, &null, 0);
- ast_queue_frame(clone, &null, 0);
- ast_log(LOG_DEBUG, "Done planning to masquerade %s into the structure of %s\n", original->name, clone->name);
- return 0;
- }
- void ast_change_name(struct ast_channel *chan, char *newname)
- {
- char tmp[256];
- strncpy(tmp, chan->name, 256);
- strncpy(chan->name, newname, sizeof(chan->name) - 1);
- manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", tmp, chan->name, chan->uniqueid);
- }
- static int ast_do_masquerade(struct ast_channel *original)
- {
- int x,i;
- int res=0;
- char *tmp;
- struct ast_var_t *varptr;
- struct ast_frame *cur, *prev;
- struct ast_channel_pvt *p;
- struct ast_channel *clone = original->masq;
- int rformat = original->readformat;
- int wformat = original->writeformat;
- char newn[100];
- char orig[100];
- char masqn[100];
- char zombn[100];
-
- #if 1
- ast_log(LOG_DEBUG, "Actually Masquerading %s(%d) into the structure of %s(%d)\n",
- clone->name, clone->_state, original->name, original->_state);
- #endif
-
-
- ast_mutex_lock(&clone->lock);
- ast_log(LOG_DEBUG, "Got clone lock on '%s' at %p\n", clone->name, &clone->lock);
-
- free_translation(clone);
- free_translation(original);
-
- original->masq = NULL;
- clone->masqr = NULL;
-
-
- strncpy(orig, original->name, sizeof(orig) - 1);
-
- strncpy(newn, clone->name, sizeof(newn) - 1);
-
- snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn);
-
-
- strncpy(original->name, newn, sizeof(original->name)-1);
-
- strncpy(clone->name, masqn, sizeof(clone->name) - 1);
-
-
- manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\n", newn, masqn);
- manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\n", orig, newn);
-
- p = original->pvt;
- original->pvt = clone->pvt;
- clone->pvt = p;
-
- prev = NULL;
- cur = clone->pvt->readq;
- x = 0;
- while(cur) {
- x++;
- prev = cur;
- cur = cur->next;
- }
-
- if (prev) {
- prev->next = original->pvt->readq;
- original->pvt->readq = clone->pvt->readq;
- clone->pvt->readq = NULL;
- if (original->pvt->alertpipe[1] > -1) {
- for (i=0;i<x;i++)
- write(original->pvt->alertpipe[1], &x, sizeof(x));
- }
- }
- clone->_softhangup = AST_SOFTHANGUP_DEV;
- if (clone->pvt->fixup){
- res = clone->pvt->fixup(original, clone);
- if (res)
- ast_log(LOG_WARNING, "Fixup failed on channel %s, strange things may happen.\n", clone->name);
- }
-
- if (clone->pvt->hangup)
- res = clone->pvt->hangup(clone);
- if (res) {
- ast_log(LOG_WARNING, "Hangup failed! Strange things may happen!\n");
- ast_mutex_unlock(&clone->lock);
- return -1;
- }
-
- snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig);
-
- strncpy(clone->name, zombn, sizeof(clone->name) - 1);
- manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\n", masqn, zombn);
-
-
- original->type = clone->type;
-
- for (x=0;x<AST_MAX_FDS;x++) {
- original->fds[x] = clone->fds[x];
- }
-
-
- varptr = original->varshead.first;
- if (varptr) {
- while(varptr->entries.next) {
- varptr = varptr->entries.next;
- }
- varptr->entries.next = clone->varshead.first;
- } else {
- original->varshead.first = clone->varshead.first;
- }
- clone->varshead.first = NULL;
-
- original->adsicpe = clone->adsicpe;
-
-
-
-
-
- original->exception = clone->exception;
- original->fdno = clone->fdno;
-
-
-
-
- tmp = original->dnid;
- original->dnid = clone->dnid;
- clone->dnid = tmp;
-
- tmp = original->callerid;
- original->callerid = clone->callerid;
- clone->callerid = tmp;
-
-
- original->fds[AST_MAX_FDS - 2] = original->timingfd;
-
-
- original->nativeformats = clone->nativeformats;
-
- original->_state = clone->_state;
-
-
-
-
-
- ast_set_write_format(original, wformat);
-
- ast_set_read_format(original, rformat);
- ast_log(LOG_DEBUG, "Putting channel %s in %d/%d formats\n", original->name, wformat, rformat);
-
- if (original->pvt->fixup) {
- res = original->pvt->fixup(clone, original);
- if (res) {
- ast_log(LOG_WARNING, "Driver for '%s' could not fixup channel %s\n",
- original->type, original->name);
- return -1;
- }
- } else
- ast_log(LOG_WARNING, "Driver '%s' does not have a fixup routine (for %s)! Bad things may happen.\n",
- original->type, original->name);
-
-
- if (clone->zombie) {
- ast_log(LOG_DEBUG, "Destroying clone '%s'\n", clone->name);
- ast_mutex_unlock(&clone->lock);
- ast_channel_free(clone);
- manager_event(EVENT_FLAG_CALL, "Hangup", "Channel: %s\r\n", zombn);
- } else {
- ast_log(LOG_DEBUG, "Released clone lock on '%s'\n", clone->name);
- clone->zombie=1;
- ast_mutex_unlock(&clone->lock);
- }
-
-
- if (original->blocking)
- pthread_kill(original->blocker, SIGURG);
- ast_log(LOG_DEBUG, "Done Masquerading %s (%d)\n",
- original->name, original->_state);
- return 0;
- }
- void ast_set_callerid(struct ast_channel *chan, char *callerid, int anitoo)
- {
- if (chan->callerid)
- free(chan->callerid);
- if (anitoo && chan->ani)
- free(chan->ani);
- if (callerid) {
- chan->callerid = strdup(callerid);
- if (anitoo)
- chan->ani = strdup(callerid);
- } else {
- chan->callerid = NULL;
- if (anitoo)
- chan->ani = NULL;
- }
- if (chan->cdr)
- ast_cdr_setcid(chan->cdr, chan);
- manager_event(EVENT_FLAG_CALL, "Newcallerid",
- "Channel: %s\r\n"
- "Callerid: %s\r\n"
- "Uniqueid: %s\r\n",
- chan->name, chan->callerid ?
- chan->callerid : "<Unknown>",
- chan->uniqueid);
- }
- int ast_setstate(struct ast_channel *chan, int state)
- {
- if (chan->_state != state) {
- int oldstate = chan->_state;
- chan->_state = state;
- if (oldstate == AST_STATE_DOWN) {
- ast_device_state_changed(chan->name);
- manager_event(EVENT_FLAG_CALL, "Newchannel",
- "Channel: %s\r\n"
- "State: %s\r\n"
- "Callerid: %s\r\n"
- "Uniqueid: %s\r\n",
- chan->name, ast_state2str(chan->_state), chan->callerid ? chan->callerid : "<unknown>", chan->uniqueid);
- } else {
- manager_event(EVENT_FLAG_CALL, "Newstate",
- "Channel: %s\r\n"
- "State: %s\r\n"
- "Callerid: %s\r\n"
- "Uniqueid: %s\r\n",
- chan->name, ast_state2str(chan->_state), chan->callerid ? chan->callerid : "<unknown>", chan->uniqueid);
- }
- }
- return 0;
- }
- int ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
- {
-
- struct ast_channel *cs[3];
- int to = -1;
- struct ast_frame *f;
- struct ast_channel *who = NULL;
- int res;
- int nativefailed=0;
-
- if (c0->zombie || ast_check_hangup_locked(c0) || c1->zombie || ast_check_hangup_locked(c1))
- return -1;
- if (c0->bridge) {
- ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
- c0->name, c0->bridge->name);
- return -1;
- }
- if (c1->bridge) {
- ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
- c1->name, c1->bridge->name);
- return -1;
- }
-
-
- c0->bridge = c1;
- c1->bridge = c0;
- cs[0] = c0;
- cs[1] = c1;
-
- manager_event(EVENT_FLAG_CALL, "Link",
- "Channel1: %s\r\n"
- "Channel2: %s\r\n",
- c0->name, c1->name);
- for (;;) {
-
- if (c0->zombie || ast_check_hangup_locked(c0) || c1->zombie || ast_check_hangup_locked(c1)) {
- *fo = NULL;
- if (who) *rc = who;
- res = 0;
- ast_log(LOG_DEBUG, "Bridge stops because we're zombie or need a soft hangup: c0=%s, c1=%s, flags: %s,%s,%s,%s\n",c0->name,c1->name,c0->zombie?"Yes":"No",ast_check_hangup(c0)?"Yes":"No",c1->zombie?"Yes":"No",ast_check_hangup(c1)?"Yes":"No");
- break;
- }
- if (c0->pvt->bridge &&
- (c0->pvt->bridge == c1->pvt->bridge) && !nativefailed && !c0->monitor && !c1->monitor) {
-
- if (option_verbose > 2)
- ast_verbose(VERBOSE_PREFIX_3 "Attempting native bridge of %s and %s\n", c0->name, c1->name);
- if (!(res = c0->pvt->bridge(c0, c1, flags, fo, rc))) {
- c0->bridge = NULL;
- c1->bridge = NULL;
- manager_event(EVENT_FLAG_CALL, "Unlink",
- "Channel1: %s\r\n"
- "Channel2: %s\r\n",
- c0->name, c1->name);
- ast_log(LOG_DEBUG, "Returning from native bridge, channels: %s, %s\n",c0->name ,c1->name);
- return 0;
- }
-
- if ((res != -2) && (res != -3))
- ast_log(LOG_WARNING, "Private bridge between %s and %s failed\n", c0->name, c1->name);
- if (res != -3) nativefailed++;
- }
-
-
- if (((c0->writeformat != c1->readformat) || (c0->readformat != c1->writeformat)) &&
- !(c0->generator || c1->generator)) {
- if (ast_channel_make_compatible(c0, c1)) {
- ast_log(LOG_WARNING, "Can't make %s and %s compatible\n", c0->name, c1->name);
- manager_event(EVENT_FLAG_CALL, "Unlink",
- "Channel1: %s\r\n"
- "Channel2: %s\r\n",
- c0->name, c1->name);
- return -1;
- }
- }
- who = ast_waitfor_n(cs, 2, &to);
- if (!who) {
- ast_log(LOG_DEBUG, "Nobody there, continuing...\n");
- continue;
- }
- f = ast_read(who);
- if (!f) {
- *fo = NULL;
- *rc = who;
- res = 0;
- ast_log(LOG_DEBUG, "Didn't get a frame from channel: %s\n",who->name);
- break;
- }
- if ((f->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
- *fo = f;
- *rc = who;
- res = 0;
- ast_log(LOG_DEBUG, "Got a FRAME_CONTROL (%d) frame on channel %s\n", f->subclass, who->name);
- break;
- }
- if ((f->frametype == AST_FRAME_VOICE) ||
- (f->frametype == AST_FRAME_TEXT) ||
- (f->frametype == AST_FRAME_VIDEO) ||
- (f->frametype == AST_FRAME_IMAGE) ||
- (f->frametype == AST_FRAME_DTMF)) {
- if ((f->frametype == AST_FRAME_DTMF) &&
- (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))) {
- if ((who == c0)) {
- if ((flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
- *rc = c0;
- *fo = f;
-
- res = 0;
- ast_log(LOG_DEBUG, "Got AST_BRIDGE_DTMF_CHANNEL_0 on c0 (%s)\n",c0->name);
- break;
- } else
- goto tackygoto;
- } else
- if ((who == c1)) {
- if (flags & AST_BRIDGE_DTMF_CHANNEL_1) {
- *rc = c1;
- *fo = f;
- res = 0;
- ast_log(LOG_DEBUG, "Got AST_BRIDGE_DTMF_CHANNEL_1 on c1 (%s)\n",c1->name);
- break;
- } else
- goto tackygoto;
- }
- } else {
- #if 0
- ast_log(LOG_DEBUG, "Read from %s\n", who->name);
- if (who == last)
- ast_log(LOG_DEBUG, "Servicing channel %s twice in a row?\n", last->name);
- last = who;
- #endif
- tackygoto:
-
- if (who == c0)
- ast_write(c1, f);
- else
- ast_write(c0, f);
- }
- ast_frfree(f);
- } else
- ast_frfree(f);
-
- cs[2] = cs[0];
- cs[0] = cs[1];
- cs[1] = cs[2];
- }
- c0->bridge = NULL;
- c1->bridge = NULL;
- manager_event(EVENT_FLAG_CALL, "Unlink",
- "Channel1: %s\r\n"
- "Channel2: %s\r\n",
- c0->name, c1->name);
- ast_log(LOG_DEBUG, "Bridge stops bridging channels %s and %s\n",c0->name,c1->name);
- return res;
- }
- int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
- {
- int res;
- if (chan->pvt->setoption) {
- res = chan->pvt->setoption(chan, option, data, datalen);
- if (res < 0)
- return res;
- } else {
- errno = ENOSYS;
- return -1;
- }
- if (block) {
-
- ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
- return -1;
- }
- return 0;
- }
- struct tonepair_def {
- int freq1;
- int freq2;
- int duration;
- int vol;
- };
- struct tonepair_state {
- float freq1;
- float freq2;
- float vol;
- int duration;
- int pos;
- int origwfmt;
- struct ast_frame f;
- unsigned char offset[AST_FRIENDLY_OFFSET];
- short data[4000];
- };
- static void tonepair_release(struct ast_channel *chan, void *params)
- {
- struct tonepair_state *ts = params;
- if (chan) {
- ast_set_write_format(chan, ts->origwfmt);
- }
- free(ts);
- }
- static void * tonepair_alloc(struct ast_channel *chan, void *params)
- {
- struct tonepair_state *ts;
- struct tonepair_def *td = params;
- ts = malloc(sizeof(struct tonepair_state));
- if (!ts)
- return NULL;
- memset(ts, 0, sizeof(struct tonepair_state));
- ts->origwfmt = chan->writeformat;
- if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
- ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
- tonepair_release(NULL, ts);
- ts = NULL;
- } else {
- ts->freq1 = td->freq1;
- ts->freq2 = td->freq2;
- ts->duration = td->duration;
- ts->vol = td->vol;
- }
-
- chan->writeinterrupt = 1;
- return ts;
- }
- static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
- {
- struct tonepair_state *ts = data;
- int x;
-
- len = samples * 2;
- if (len > sizeof(ts->data) / 2 - 1) {
- ast_log(LOG_WARNING, "Can't generate that much data!\n");
- return -1;
- }
- memset(&ts->f, 0, sizeof(ts->f));
- for (x=0;x<len/2;x++) {
- ts->data[x] = ts->vol * (
- sin((ts->freq1 * 2.0 * M_PI / 8000.0) * (ts->pos + x)) +
- sin((ts->freq2 * 2.0 * M_PI / 8000.0) * (ts->pos + x))
- );
- }
- ts->f.frametype = AST_FRAME_VOICE;
- ts->f.subclass = AST_FORMAT_SLINEAR;
- ts->f.datalen = len;
- ts->f.samples = samples;
- ts->f.offset = AST_FRIENDLY_OFFSET;
- ts->f.data = ts->data;
- ast_write(chan, &ts->f);
- ts->pos += x;
- if (ts->duration > 0) {
- if (ts->pos >= ts->duration * 8)
- return -1;
- }
- return 0;
- }
- static struct ast_generator tonepair = {
- alloc: tonepair_alloc,
- release: tonepair_release,
- generate: tonepair_generator,
- };
- int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
- {
- struct tonepair_def d = { 0, };
- d.freq1 = freq1;
- d.freq2 = freq2;
- d.duration = duration;
- if (vol < 1)
- d.vol = 8192;
- else
- d.vol = vol;
- if (ast_activate_generator(chan, &tonepair, &d))
- return -1;
- return 0;
- }
- void ast_tonepair_stop(struct ast_channel *chan)
- {
- ast_deactivate_generator(chan);
- }
- int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
- {
- struct ast_frame *f;
- int res;
- if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
- return res;
-
- while(chan->generatordata && (ast_waitfor(chan, 100) >= 0)) {
- f = ast_read(chan);
- if (f)
- ast_frfree(f);
- else
- return -1;
- }
- return 0;
- }
- unsigned int ast_get_group(char *s)
- {
- char *copy;
- char *piece;
- char *c=NULL;
- int start=0, finish=0,x;
- unsigned int group = 0;
- copy = ast_strdupa(s);
- if (!copy) {
- ast_log(LOG_ERROR, "Out of memory\n");
- return 0;
- }
- c = copy;
-
- while((piece = strsep(&c, ","))) {
- if (sscanf(piece, "%d-%d", &start, &finish) == 2) {
-
- } else if (sscanf(piece, "%d", &start)) {
-
- finish = start;
- } else {
- ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'. Using '0'\n", s,piece);
- return 0;
- }
- for (x=start;x<=finish;x++) {
- if ((x > 31) || (x < 0)) {
- ast_log(LOG_WARNING, "Ignoring invalid group %d (maximum group is 31)\n", x);
- } else
- group |= (1 << x);
- }
- }
- return group;
- }
|