chan_misdn.c 150 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2004 - 2006, Christian Richter
  5. *
  6. * Christian Richter <crich@beronet.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. *
  18. */
  19. /*!
  20. * \file
  21. *
  22. * \brief the chan_misdn channel driver for Asterisk
  23. *
  24. * \author Christian Richter <crich@beronet.com>
  25. *
  26. * \extref MISDN http://www.misdn.org/
  27. *
  28. * \ingroup channel_drivers
  29. */
  30. /*** MODULEINFO
  31. <depend>isdnnet</depend>
  32. <depend>misdn</depend>
  33. <depend>suppserv</depend>
  34. ***/
  35. #include "asterisk.h"
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include <stdio.h>
  38. #include <pthread.h>
  39. #include <string.h>
  40. #include <sys/socket.h>
  41. #include <sys/time.h>
  42. #include <errno.h>
  43. #include <unistd.h>
  44. #include <stdlib.h>
  45. #include <arpa/inet.h>
  46. #include <fcntl.h>
  47. #include <sys/ioctl.h>
  48. #include <signal.h>
  49. #include <sys/file.h>
  50. #include <semaphore.h>
  51. #include "asterisk/channel.h"
  52. #include "asterisk/config.h"
  53. #include "asterisk/logger.h"
  54. #include "asterisk/module.h"
  55. #include "asterisk/pbx.h"
  56. #include "asterisk/options.h"
  57. #include "asterisk/io.h"
  58. #include "asterisk/frame.h"
  59. #include "asterisk/translate.h"
  60. #include "asterisk/cli.h"
  61. #include "asterisk/musiconhold.h"
  62. #include "asterisk/dsp.h"
  63. #include "asterisk/translate.h"
  64. #include "asterisk/config.h"
  65. #include "asterisk/file.h"
  66. #include "asterisk/callerid.h"
  67. #include "asterisk/indications.h"
  68. #include "asterisk/app.h"
  69. #include "asterisk/features.h"
  70. #include "asterisk/term.h"
  71. #include "asterisk/sched.h"
  72. #include "asterisk/stringfields.h"
  73. #include "asterisk/causes.h"
  74. #include "chan_misdn_config.h"
  75. #include "isdn_lib.h"
  76. char global_tracefile[BUFFERSIZE + 1];
  77. static int g_config_initialized = 0;
  78. struct misdn_jb{
  79. int size;
  80. int upper_threshold;
  81. char *samples, *ok;
  82. int wp,rp;
  83. int state_empty;
  84. int state_full;
  85. int state_buffer;
  86. int bytes_wrote;
  87. ast_mutex_t mutexjb;
  88. };
  89. /*! \brief allocates the jb-structure and initialize the elements */
  90. struct misdn_jb *misdn_jb_init(int size, int upper_threshold);
  91. /*! \brief frees the data and destroys the given jitterbuffer struct */
  92. void misdn_jb_destroy(struct misdn_jb *jb);
  93. /*! \brief fills the jitterbuffer with len data returns < 0 if there was an
  94. error (buffer overrun). */
  95. int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len);
  96. /*! \brief gets len bytes out of the jitterbuffer if available, else only the
  97. available data is returned and the return value indicates the number
  98. of data. */
  99. int misdn_jb_empty(struct misdn_jb *jb, char *data, int len);
  100. /* BEGIN: chan_misdn.h */
  101. ast_mutex_t release_lock;
  102. enum misdn_chan_state {
  103. MISDN_NOTHING=0, /*!< at beginning */
  104. MISDN_WAITING4DIGS, /*!< when waiting for infos */
  105. MISDN_EXTCANTMATCH, /*!< when asterisk couldn't match our ext */
  106. MISDN_INCOMING_SETUP, /*!< for incoming setups*/
  107. MISDN_DIALING, /*!< when pbx_start */
  108. MISDN_PROGRESS, /*!< we got a progress */
  109. MISDN_PROCEEDING, /*!< we got a progress */
  110. MISDN_CALLING, /*!< when misdn_call is called */
  111. MISDN_CALLING_ACKNOWLEDGE, /*!< when we get SETUP_ACK */
  112. MISDN_ALERTING, /*!< when Alerting */
  113. MISDN_BUSY, /*!< when BUSY */
  114. MISDN_CONNECTED, /*!< when connected */
  115. MISDN_DISCONNECTED, /*!< when connected */
  116. MISDN_CLEANING, /*!< when hangup from * but we were connected before */
  117. };
  118. #define ORG_AST 1
  119. #define ORG_MISDN 2
  120. enum misdn_hold_state {
  121. MISDN_HOLD_IDLE, /*!< HOLD not active */
  122. MISDN_HOLD_ACTIVE, /*!< Call is held */
  123. MISDN_HOLD_TRANSFER, /*!< Held call is being transferred */
  124. MISDN_HOLD_DISCONNECT, /*!< Held call is being disconnected */
  125. };
  126. struct hold_info {
  127. /*!
  128. * \brief Call HOLD state.
  129. */
  130. enum misdn_hold_state state;
  131. /*!
  132. * \brief Logical port the channel call record is HELD on
  133. * because the B channel is no longer associated.
  134. */
  135. int port;
  136. /*!
  137. * \brief Original B channel number the HELD call was using.
  138. * \note Used only for debug display messages.
  139. */
  140. int channel;
  141. };
  142. /*!
  143. * \brief Channel call record structure
  144. */
  145. struct chan_list {
  146. /*!
  147. * \brief The "allowed_bearers" string read in from /etc/asterisk/misdn.conf
  148. */
  149. char allowed_bearers[BUFFERSIZE + 1];
  150. /*!
  151. * \brief State of the channel
  152. */
  153. enum misdn_chan_state state;
  154. /*!
  155. * \brief TRUE if a hangup needs to be queued
  156. * \note This is a debug flag only used to catch calls to hangup_chan() that are already hungup.
  157. */
  158. int need_queue_hangup;
  159. /*!
  160. * \brief TRUE if a channel can be hung up by calling asterisk directly when done.
  161. */
  162. int need_hangup;
  163. /*!
  164. * \brief TRUE if we could send an AST_CONTROL_BUSY if needed.
  165. */
  166. int need_busy;
  167. /*!
  168. * \brief Who originally created this channel. ORG_AST or ORG_MISDN
  169. */
  170. int originator;
  171. /*!
  172. * \brief TRUE of we are not to respond immediately to a SETUP message. Check the dialplan first.
  173. * \note The "noautorespond_on_setup" boolean read in from /etc/asterisk/misdn.conf
  174. */
  175. int noautorespond_on_setup;
  176. int norxtone; /* Boolean assigned values but the value is not used. */
  177. /*!
  178. * \brief TRUE if we are not to generate tones (Playtones)
  179. */
  180. int notxtone;
  181. /*!
  182. * \brief TRUE if echo canceller is enabled. Value is toggled.
  183. */
  184. int toggle_ec;
  185. /*!
  186. * \brief TRUE if you want to send Tone Indications to an incoming
  187. * ISDN channel on a TE Port.
  188. * \note The "incoming_early_audio" boolean read in from /etc/asterisk/misdn.conf
  189. */
  190. int incoming_early_audio;
  191. /*!
  192. * \brief TRUE if DTMF digits are to be passed inband only.
  193. * \note It is settable by the misdn_set_opt() application.
  194. */
  195. int ignore_dtmf;
  196. /*!
  197. * \brief Pipe file descriptor handles array.
  198. * Read from pipe[0], write to pipe[1]
  199. */
  200. int pipe[2];
  201. /*!
  202. * \brief Read buffer for inbound audio from pipe[0]
  203. */
  204. char ast_rd_buf[4096];
  205. /*!
  206. * \brief Inbound audio frame returned by misdn_read().
  207. */
  208. struct ast_frame frame;
  209. /*!
  210. * \brief Fax detection option. (0:no 1:yes 2:yes+nojump)
  211. * \note The "faxdetect" option string read in from /etc/asterisk/misdn.conf
  212. * \note It is settable by the misdn_set_opt() application.
  213. */
  214. int faxdetect;
  215. /*!
  216. * \brief Number of seconds to detect a Fax machine when detection enabled.
  217. * \note 0 disables the timeout.
  218. * \note The "faxdetect_timeout" value read in from /etc/asterisk/misdn.conf
  219. */
  220. int faxdetect_timeout;
  221. /*!
  222. * \brief Starting time of fax detection with timeout when nonzero.
  223. */
  224. struct timeval faxdetect_tv;
  225. /*!
  226. * \brief TRUE if a fax has been detected.
  227. */
  228. int faxhandled;
  229. /*!
  230. * \brief TRUE if we will use the Asterisk DSP to detect DTMF/Fax
  231. * \note The "astdtmf" boolean read in from /etc/asterisk/misdn.conf
  232. */
  233. int ast_dsp;
  234. /*!
  235. * \brief Jitterbuffer length
  236. * \note The "jitterbuffer" value read in from /etc/asterisk/misdn.conf
  237. */
  238. int jb_len;
  239. /*!
  240. * \brief Jitterbuffer upper threshold
  241. * \note The "jitterbuffer_upper_threshold" value read in from /etc/asterisk/misdn.conf
  242. */
  243. int jb_upper_threshold;
  244. /*!
  245. * \brief Allocated jitterbuffer controller
  246. * \note misdn_jb_init() creates the jitterbuffer.
  247. * \note Must use misdn_jb_destroy() to clean up.
  248. */
  249. struct misdn_jb *jb;
  250. /*!
  251. * \brief Allocated DSP controller
  252. * \note ast_dsp_new() creates the DSP controller.
  253. * \note Must use ast_dsp_free() to clean up.
  254. */
  255. struct ast_dsp *dsp;
  256. /*!
  257. * \brief Associated Asterisk channel structure.
  258. */
  259. struct ast_channel * ast;
  260. //int dummy; /* Not used */
  261. /*!
  262. * \brief Associated B channel structure.
  263. */
  264. struct misdn_bchannel *bc;
  265. /*!
  266. * \brief HELD channel call information
  267. */
  268. struct hold_info hold;
  269. /*!
  270. * \brief From associated B channel: Layer 3 process ID
  271. * \note Used to find the HELD channel call record when retrieving a call.
  272. */
  273. unsigned int l3id;
  274. /*!
  275. * \brief From associated B channel: B Channel mISDN driver layer ID from mISDN_get_layerid()
  276. * \note Used only for debug display messages.
  277. */
  278. int addr;
  279. /*!
  280. * \brief Incoming call dialplan context identifier.
  281. * \note The "context" string read in from /etc/asterisk/misdn.conf
  282. */
  283. char context[AST_MAX_CONTEXT];
  284. /*!
  285. * \brief The configured music-on-hold class to use for this call.
  286. * \note The "musicclass" string read in from /etc/asterisk/misdn.conf
  287. */
  288. char mohinterpret[MAX_MUSICCLASS];
  289. //int zero_read_cnt; /* Not used */
  290. /*!
  291. * \brief Number of outgoing audio frames dropped since last debug gripe message.
  292. */
  293. int dropped_frame_cnt;
  294. /*!
  295. * \brief TRUE if we must do the ringback tones.
  296. * \note The "far_alerting" boolean read in from /etc/asterisk/misdn.conf
  297. */
  298. int far_alerting;
  299. /*!
  300. * \brief TRUE if NT should disconnect an overlap dialing call when a timeout occurs.
  301. * \note The "nttimeout" boolean read in from /etc/asterisk/misdn.conf
  302. */
  303. int nttimeout;
  304. /*!
  305. * \brief Other channel call record PID
  306. * \note Value imported from Asterisk environment variable MISDN_PID
  307. */
  308. int other_pid;
  309. /*!
  310. * \brief Bridged other channel call record
  311. * \note Pointer set when other_pid imported from Asterisk environment
  312. * variable MISDN_PID by either side.
  313. */
  314. struct chan_list *other_ch;
  315. /*!
  316. * \brief Tone zone sound used for dialtone generation.
  317. * \note Used as a boolean. Non-NULL to prod generation if enabled.
  318. */
  319. const struct tone_zone_sound *ts;
  320. /*!
  321. * \brief Enables overlap dialing for the set amount of seconds. (0 = Disabled)
  322. * \note The "overlapdial" value read in from /etc/asterisk/misdn.conf
  323. */
  324. int overlap_dial;
  325. /*!
  326. * \brief Overlap dialing timeout Task ID. -1 if not running.
  327. */
  328. int overlap_dial_task;
  329. /*!
  330. * \brief overlap_tv access lock.
  331. */
  332. ast_mutex_t overlap_tv_lock;
  333. /*!
  334. * \brief Overlap timer start time. Timer restarted for every digit received.
  335. */
  336. struct timeval overlap_tv;
  337. //struct chan_list *peer; /* Not used */
  338. /*!
  339. * \brief Next channel call record in the list.
  340. */
  341. struct chan_list *next;
  342. //struct chan_list *prev; /* Not used */
  343. //struct chan_list *first; /* Not used */
  344. };
  345. void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
  346. void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
  347. struct robin_list {
  348. char *group;
  349. int port;
  350. int channel;
  351. struct robin_list *next;
  352. struct robin_list *prev;
  353. };
  354. static struct robin_list *robin = NULL;
  355. static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame);
  356. static void free_robin_list(void)
  357. {
  358. struct robin_list *r;
  359. struct robin_list *next;
  360. for (r = robin, robin = NULL; r; r = next) {
  361. next = r->next;
  362. free(r->group);
  363. free(r);
  364. }
  365. }
  366. static struct robin_list* get_robin_position (char *group)
  367. {
  368. struct robin_list *new;
  369. struct robin_list *iter = robin;
  370. for (; iter; iter = iter->next) {
  371. if (!strcasecmp(iter->group, group)) {
  372. return iter;
  373. }
  374. }
  375. new = calloc(1, sizeof(*new));
  376. if (!new) {
  377. return NULL;
  378. }
  379. new->group = strdup(group);
  380. if (!new->group) {
  381. free(new);
  382. return NULL;
  383. }
  384. new->port = 0;
  385. new->channel = 0;
  386. if (robin) {
  387. new->next = robin;
  388. robin->prev = new;
  389. }
  390. robin = new;
  391. return robin;
  392. }
  393. /*! \brief the main schedule context for stuff like l1 watcher, overlap dial, ... */
  394. static struct sched_context *misdn_tasks = NULL;
  395. static pthread_t misdn_tasks_thread;
  396. static int *misdn_ports;
  397. static void chan_misdn_log(int level, int port, char *tmpl, ...)
  398. __attribute__((format(printf, 3, 4)));
  399. static struct ast_channel *misdn_new(struct chan_list *cl, int state, char *exten, char *callerid, int format, int port, int c);
  400. static void send_digit_to_chan(struct chan_list *cl, char digit );
  401. static int pbx_start_chan(struct chan_list *ch);
  402. #define MISDN_ASTERISK_TECH_PVT(ast) ast->tech_pvt
  403. #define MISDN_ASTERISK_PVT(ast) 1
  404. #include "asterisk/strings.h"
  405. /* #define MISDN_DEBUG 1 */
  406. static const char misdn_type[] = "mISDN";
  407. static int tracing = 0 ;
  408. /*! \brief Only alaw and mulaw is allowed for now */
  409. static int prefformat = AST_FORMAT_ALAW ; /* AST_FORMAT_SLINEAR ; AST_FORMAT_ULAW | */
  410. static int *misdn_debug;
  411. static int *misdn_debug_only;
  412. static int max_ports;
  413. static int *misdn_in_calls;
  414. static int *misdn_out_calls;
  415. struct chan_list dummy_cl;
  416. /*!
  417. * \brief Global channel call record list head.
  418. */
  419. struct chan_list *cl_te=NULL;
  420. ast_mutex_t cl_te_lock;
  421. static enum event_response_e
  422. cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data);
  423. static void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel*bc, struct chan_list *ch);
  424. static void cl_queue_chan(struct chan_list **list, struct chan_list *chan);
  425. static void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan);
  426. static struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc);
  427. static struct chan_list *find_chan_by_pid(struct chan_list *list, int pid);
  428. static int dialtone_indicate(struct chan_list *cl);
  429. static void hanguptone_indicate(struct chan_list *cl);
  430. static int stop_indicate(struct chan_list *cl);
  431. static int start_bc_tones(struct chan_list *cl);
  432. static int stop_bc_tones(struct chan_list *cl);
  433. static void release_chan_early(struct chan_list *ch);
  434. static void release_chan(struct chan_list *ch, struct misdn_bchannel *bc);
  435. static int misdn_check_l2l1(struct ast_channel *chan, void *data);
  436. static int misdn_set_opt_exec(struct ast_channel *chan, void *data);
  437. static int misdn_facility_exec(struct ast_channel *chan, void *data);
  438. int chan_misdn_jb_empty(struct misdn_bchannel *bc, char *buf, int len);
  439. void debug_numplan(int port, int numplan, char *type);
  440. int add_out_calls(int port);
  441. int add_in_calls(int port);
  442. #ifdef MISDN_1_2
  443. static int update_pipeline_config(struct misdn_bchannel *bc);
  444. #else
  445. static int update_ec_config(struct misdn_bchannel *bc);
  446. #endif
  447. /*************** Helpers *****************/
  448. static struct chan_list * get_chan_by_ast(struct ast_channel *ast)
  449. {
  450. struct chan_list *tmp;
  451. for (tmp=cl_te; tmp; tmp = tmp->next) {
  452. if ( tmp->ast == ast ) return tmp;
  453. }
  454. return NULL;
  455. }
  456. static struct chan_list * get_chan_by_ast_name(char *name)
  457. {
  458. struct chan_list *tmp;
  459. for (tmp=cl_te; tmp; tmp = tmp->next) {
  460. if ( tmp->ast && strcmp(tmp->ast->name,name) == 0) return tmp;
  461. }
  462. return NULL;
  463. }
  464. struct allowed_bearers {
  465. char *name; /*!< Bearer capability name string used in /etc/misdn.conf allowed_bearers */
  466. char *display; /*!< Bearer capability displayable name */
  467. int cap; /*!< SETUP message bearer capability field code value */
  468. int deprecated; /*!< TRUE if this entry is deprecated. (Misspelled or bad name to use) */
  469. };
  470. /* *INDENT-OFF* */
  471. static const struct allowed_bearers allowed_bearers_array[]= {
  472. /* Name, Displayable Name Bearer Capability, Deprecated */
  473. { "speech", "Speech", INFO_CAPABILITY_SPEECH, 0 },
  474. { "3_1khz", "3.1KHz Audio", INFO_CAPABILITY_AUDIO_3_1K, 0 },
  475. { "digital_unrestricted", "Unrestricted Digital", INFO_CAPABILITY_DIGITAL_UNRESTRICTED, 0 },
  476. { "digital_restricted", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 0 },
  477. { "digital_restriced", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 1 }, /* Allow misspelling for backwards compatibility */
  478. { "video", "Video", INFO_CAPABILITY_VIDEO, 0 }
  479. };
  480. /* *INDENT-ON* */
  481. static const char *bearer2str(int cap)
  482. {
  483. unsigned index;
  484. for (index = 0; index < ARRAY_LEN(allowed_bearers_array); ++index) {
  485. if (allowed_bearers_array[index].cap == cap) {
  486. return allowed_bearers_array[index].display;
  487. }
  488. } /* end for */
  489. return "Unknown Bearer";
  490. }
  491. static void print_facility(struct FacParm *fac, struct misdn_bchannel *bc)
  492. {
  493. switch (fac->Function) {
  494. case Fac_CD:
  495. chan_misdn_log(1,bc->port," --> calldeflect to: %s, presentable: %s\n", fac->u.CDeflection.DeflectedToNumber,
  496. fac->u.CDeflection.PresentationAllowed ? "yes" : "no");
  497. break;
  498. case Fac_AOCDCurrency:
  499. if (fac->u.AOCDcur.chargeNotAvailable)
  500. chan_misdn_log(1,bc->port," --> AOCD currency: charge not available\n");
  501. else if (fac->u.AOCDcur.freeOfCharge)
  502. chan_misdn_log(1,bc->port," --> AOCD currency: free of charge\n");
  503. else if (fac->u.AOCDchu.billingId >= 0)
  504. chan_misdn_log(1,bc->port," --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s billingId:%d\n",
  505. fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
  506. (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDcur.billingId);
  507. else
  508. chan_misdn_log(1,bc->port," --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s\n",
  509. fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
  510. (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total");
  511. break;
  512. case Fac_AOCDChargingUnit:
  513. if (fac->u.AOCDchu.chargeNotAvailable)
  514. chan_misdn_log(1,bc->port," --> AOCD charging unit: charge not available\n");
  515. else if (fac->u.AOCDchu.freeOfCharge)
  516. chan_misdn_log(1,bc->port," --> AOCD charging unit: free of charge\n");
  517. else if (fac->u.AOCDchu.billingId >= 0)
  518. chan_misdn_log(1,bc->port," --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s billingId:%d\n",
  519. fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDchu.billingId);
  520. else
  521. chan_misdn_log(1,bc->port," --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s\n",
  522. fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total");
  523. break;
  524. default:
  525. chan_misdn_log(1,bc->port," --> unknown facility\n");
  526. break;
  527. }
  528. }
  529. static void print_bearer(struct misdn_bchannel *bc)
  530. {
  531. chan_misdn_log(2, bc->port, " --> Bearer: %s\n",bearer2str(bc->capability));
  532. switch(bc->law) {
  533. case INFO_CODEC_ALAW:
  534. chan_misdn_log(2, bc->port, " --> Codec: Alaw\n");
  535. break;
  536. case INFO_CODEC_ULAW:
  537. chan_misdn_log(2, bc->port, " --> Codec: Ulaw\n");
  538. break;
  539. }
  540. }
  541. static void export_aoc_vars(int originator, struct ast_channel *ast, struct misdn_bchannel *bc)
  542. {
  543. char buf[128];
  544. if (!ast)
  545. return;
  546. if (originator == ORG_AST) {
  547. ast = ast_bridged_channel(ast);
  548. if (!ast)
  549. return;
  550. }
  551. switch (bc->AOCDtype) {
  552. case Fac_AOCDCurrency:
  553. pbx_builtin_setvar_helper(ast, "AOCD_Type", "currency");
  554. if (bc->AOCD.currency.chargeNotAvailable)
  555. pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "no");
  556. else {
  557. pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "yes");
  558. if (bc->AOCD.currency.freeOfCharge)
  559. pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "yes");
  560. else {
  561. pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "no");
  562. if (snprintf(buf, sizeof(buf), "%d %s", bc->AOCD.currency.currencyAmount * bc->AOCD.currency.multiplier, bc->AOCD.currency.currency) < sizeof(buf)) {
  563. pbx_builtin_setvar_helper(ast, "AOCD_Amount", buf);
  564. if (bc->AOCD.currency.billingId >= 0 && snprintf(buf, sizeof(buf), "%d", bc->AOCD.currency.billingId) < sizeof(buf))
  565. pbx_builtin_setvar_helper(ast, "AOCD_BillingId", buf);
  566. }
  567. }
  568. }
  569. break;
  570. case Fac_AOCDChargingUnit:
  571. pbx_builtin_setvar_helper(ast, "AOCD_Type", "charging_unit");
  572. if (bc->AOCD.chargingUnit.chargeNotAvailable)
  573. pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "no");
  574. else {
  575. pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "yes");
  576. if (bc->AOCD.chargingUnit.freeOfCharge)
  577. pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "yes");
  578. else {
  579. pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "no");
  580. if (snprintf(buf, sizeof(buf), "%d", bc->AOCD.chargingUnit.recordedUnits) < sizeof(buf)) {
  581. pbx_builtin_setvar_helper(ast, "AOCD_RecordedUnits", buf);
  582. if (bc->AOCD.chargingUnit.billingId >= 0 && snprintf(buf, sizeof(buf), "%d", bc->AOCD.chargingUnit.billingId) < sizeof(buf))
  583. pbx_builtin_setvar_helper(ast, "AOCD_BillingId", buf);
  584. }
  585. }
  586. }
  587. break;
  588. default:
  589. break;
  590. }
  591. }
  592. /*************** Helpers END *************/
  593. static void sighandler(int sig)
  594. {}
  595. static void* misdn_tasks_thread_func (void *data)
  596. {
  597. int wait;
  598. struct sigaction sa;
  599. sa.sa_handler = sighandler;
  600. sa.sa_flags = SA_NODEFER;
  601. sigemptyset(&sa.sa_mask);
  602. sigaddset(&sa.sa_mask, SIGUSR1);
  603. sigaction(SIGUSR1, &sa, NULL);
  604. sem_post((sem_t *)data);
  605. while (1) {
  606. wait = ast_sched_wait(misdn_tasks);
  607. if (wait < 0)
  608. wait = 8000;
  609. if (poll(NULL, 0, wait) < 0)
  610. chan_misdn_log(4, 0, "Waking up misdn_tasks thread\n");
  611. ast_sched_runq(misdn_tasks);
  612. }
  613. return NULL;
  614. }
  615. static void misdn_tasks_init (void)
  616. {
  617. sem_t blocker;
  618. int i = 5;
  619. if (sem_init(&blocker, 0, 0)) {
  620. perror("chan_misdn: Failed to initialize semaphore!");
  621. exit(1);
  622. }
  623. chan_misdn_log(4, 0, "Starting misdn_tasks thread\n");
  624. misdn_tasks = sched_context_create();
  625. pthread_create(&misdn_tasks_thread, NULL, misdn_tasks_thread_func, &blocker);
  626. while (sem_wait(&blocker) && --i);
  627. sem_destroy(&blocker);
  628. }
  629. static void misdn_tasks_destroy (void)
  630. {
  631. if (misdn_tasks) {
  632. chan_misdn_log(4, 0, "Killing misdn_tasks thread\n");
  633. if ( pthread_cancel(misdn_tasks_thread) == 0 ) {
  634. cb_log(4, 0, "Joining misdn_tasks thread\n");
  635. pthread_join(misdn_tasks_thread, NULL);
  636. }
  637. sched_context_destroy(misdn_tasks);
  638. }
  639. }
  640. static inline void misdn_tasks_wakeup (void)
  641. {
  642. pthread_kill(misdn_tasks_thread, SIGUSR1);
  643. }
  644. static inline int _misdn_tasks_add_variable (int timeout, ast_sched_cb callback, const void *data, int variable)
  645. {
  646. int task_id;
  647. if (!misdn_tasks) {
  648. misdn_tasks_init();
  649. }
  650. task_id = ast_sched_add_variable(misdn_tasks, timeout, callback, data, variable);
  651. misdn_tasks_wakeup();
  652. return task_id;
  653. }
  654. static int misdn_tasks_add (int timeout, ast_sched_cb callback, const void *data)
  655. {
  656. return _misdn_tasks_add_variable(timeout, callback, data, 0);
  657. }
  658. static int misdn_tasks_add_variable (int timeout, ast_sched_cb callback, const void *data)
  659. {
  660. return _misdn_tasks_add_variable(timeout, callback, data, 1);
  661. }
  662. static void misdn_tasks_remove (int task_id)
  663. {
  664. AST_SCHED_DEL(misdn_tasks, task_id);
  665. }
  666. static int misdn_l1_task (const void *data)
  667. {
  668. misdn_lib_isdn_l1watcher(*(int *)data);
  669. chan_misdn_log(5, *(int *)data, "L1watcher timeout\n");
  670. return 1;
  671. }
  672. static int misdn_overlap_dial_task (const void *data)
  673. {
  674. struct timeval tv_end, tv_now;
  675. int diff;
  676. struct chan_list *ch = (struct chan_list *)data;
  677. chan_misdn_log(4, ch->bc->port, "overlap dial task, chan_state: %d\n", ch->state);
  678. if (ch->state != MISDN_WAITING4DIGS) {
  679. ch->overlap_dial_task = -1;
  680. return 0;
  681. }
  682. ast_mutex_lock(&ch->overlap_tv_lock);
  683. tv_end = ch->overlap_tv;
  684. ast_mutex_unlock(&ch->overlap_tv_lock);
  685. tv_end.tv_sec += ch->overlap_dial;
  686. tv_now = ast_tvnow();
  687. diff = ast_tvdiff_ms(tv_end, tv_now);
  688. if (diff <= 100) {
  689. char *dad=ch->bc->dad, sexten[]="s";
  690. /* if we are 100ms near the timeout, we are satisfied.. */
  691. stop_indicate(ch);
  692. if (ast_strlen_zero(ch->bc->dad)) {
  693. dad=sexten;
  694. strcpy(ch->ast->exten, sexten);
  695. }
  696. if (ast_exists_extension(ch->ast, ch->context, dad, 1, ch->bc->oad)) {
  697. ch->state=MISDN_DIALING;
  698. if (pbx_start_chan(ch) < 0) {
  699. chan_misdn_log(-1, ch->bc->port, "ast_pbx_start returned < 0 in misdn_overlap_dial_task\n");
  700. goto misdn_overlap_dial_task_disconnect;
  701. }
  702. } else {
  703. misdn_overlap_dial_task_disconnect:
  704. hanguptone_indicate(ch);
  705. ch->bc->out_cause = AST_CAUSE_UNALLOCATED;
  706. ch->state=MISDN_CLEANING;
  707. misdn_lib_send_event(ch->bc, EVENT_DISCONNECT);
  708. }
  709. ch->overlap_dial_task = -1;
  710. return 0;
  711. } else
  712. return diff;
  713. }
  714. static void send_digit_to_chan(struct chan_list *cl, char digit )
  715. {
  716. static const char* dtmf_tones[] = {
  717. "!941+1336/100,!0/100", /* 0 */
  718. "!697+1209/100,!0/100", /* 1 */
  719. "!697+1336/100,!0/100", /* 2 */
  720. "!697+1477/100,!0/100", /* 3 */
  721. "!770+1209/100,!0/100", /* 4 */
  722. "!770+1336/100,!0/100", /* 5 */
  723. "!770+1477/100,!0/100", /* 6 */
  724. "!852+1209/100,!0/100", /* 7 */
  725. "!852+1336/100,!0/100", /* 8 */
  726. "!852+1477/100,!0/100", /* 9 */
  727. "!697+1633/100,!0/100", /* A */
  728. "!770+1633/100,!0/100", /* B */
  729. "!852+1633/100,!0/100", /* C */
  730. "!941+1633/100,!0/100", /* D */
  731. "!941+1209/100,!0/100", /* * */
  732. "!941+1477/100,!0/100" }; /* # */
  733. struct ast_channel *chan=cl->ast;
  734. if (digit >= '0' && digit <='9')
  735. ast_playtones_start(chan,0,dtmf_tones[digit-'0'], 0);
  736. else if (digit >= 'A' && digit <= 'D')
  737. ast_playtones_start(chan,0,dtmf_tones[digit-'A'+10], 0);
  738. else if (digit == '*')
  739. ast_playtones_start(chan,0,dtmf_tones[14], 0);
  740. else if (digit == '#')
  741. ast_playtones_start(chan,0,dtmf_tones[15], 0);
  742. else {
  743. /* not handled */
  744. ast_log(LOG_DEBUG, "Unable to handle DTMF tone '%c' for '%s'\n", digit, chan->name);
  745. }
  746. }
  747. /*** CLI HANDLING ***/
  748. static int misdn_set_debug(int fd, int argc, char *argv[])
  749. {
  750. int level;
  751. if (argc != 4 && argc != 5 && argc != 6 && argc != 7)
  752. return RESULT_SHOWUSAGE;
  753. level = atoi(argv[3]);
  754. switch (argc) {
  755. case 4:
  756. case 5:
  757. {
  758. int i;
  759. int only = 0;
  760. if (argc == 5) {
  761. if (strncasecmp(argv[4], "only", strlen(argv[4])))
  762. return RESULT_SHOWUSAGE;
  763. else
  764. only = 1;
  765. }
  766. for (i = 0; i <= max_ports; i++) {
  767. misdn_debug[i] = level;
  768. misdn_debug_only[i] = only;
  769. }
  770. ast_cli(fd, "changing debug level for all ports to %d%s\n",misdn_debug[0], only?" (only)":"");
  771. }
  772. break;
  773. case 6:
  774. case 7:
  775. {
  776. int port;
  777. if (strncasecmp(argv[4], "port", strlen(argv[4])))
  778. return RESULT_SHOWUSAGE;
  779. port = atoi(argv[5]);
  780. if (port <= 0 || port > max_ports) {
  781. switch (max_ports) {
  782. case 0:
  783. ast_cli(fd, "port number not valid! no ports available so you won't get lucky with any number here...\n");
  784. break;
  785. case 1:
  786. ast_cli(fd, "port number not valid! only port 1 is available.\n");
  787. break;
  788. default:
  789. ast_cli(fd, "port number not valid! only ports 1 to %d are available.\n", max_ports);
  790. }
  791. return 0;
  792. }
  793. if (argc == 7) {
  794. if (strncasecmp(argv[6], "only", strlen(argv[6])))
  795. return RESULT_SHOWUSAGE;
  796. else
  797. misdn_debug_only[port] = 1;
  798. } else
  799. misdn_debug_only[port] = 0;
  800. misdn_debug[port] = level;
  801. ast_cli(fd, "changing debug level to %d%s for port %d\n", misdn_debug[port], misdn_debug_only[port]?" (only)":"", port);
  802. }
  803. }
  804. return 0;
  805. }
  806. static int misdn_set_crypt_debug(int fd, int argc, char *argv[])
  807. {
  808. if (argc != 5) return RESULT_SHOWUSAGE;
  809. return 0;
  810. }
  811. static int misdn_port_block(int fd, int argc, char *argv[])
  812. {
  813. int port;
  814. if (argc != 4)
  815. return RESULT_SHOWUSAGE;
  816. port = atoi(argv[3]);
  817. misdn_lib_port_block(port);
  818. return 0;
  819. }
  820. static int misdn_port_unblock(int fd, int argc, char *argv[])
  821. {
  822. int port;
  823. if (argc != 4)
  824. return RESULT_SHOWUSAGE;
  825. port = atoi(argv[3]);
  826. misdn_lib_port_unblock(port);
  827. return 0;
  828. }
  829. static int misdn_restart_port (int fd, int argc, char *argv[])
  830. {
  831. int port;
  832. if (argc != 4)
  833. return RESULT_SHOWUSAGE;
  834. port = atoi(argv[3]);
  835. misdn_lib_port_restart(port);
  836. return 0;
  837. }
  838. static int misdn_restart_pid (int fd, int argc, char *argv[])
  839. {
  840. int pid;
  841. if (argc != 4)
  842. return RESULT_SHOWUSAGE;
  843. pid = atoi(argv[3]);
  844. misdn_lib_pid_restart(pid);
  845. return 0;
  846. }
  847. static int misdn_port_up (int fd, int argc, char *argv[])
  848. {
  849. int port;
  850. if (argc != 4)
  851. return RESULT_SHOWUSAGE;
  852. port = atoi(argv[3]);
  853. misdn_lib_get_port_up(port);
  854. return 0;
  855. }
  856. static int misdn_port_down (int fd, int argc, char *argv[])
  857. {
  858. int port;
  859. if (argc != 4)
  860. return RESULT_SHOWUSAGE;
  861. port = atoi(argv[3]);
  862. misdn_lib_get_port_down(port);
  863. return 0;
  864. }
  865. static inline void show_config_description (int fd, enum misdn_cfg_elements elem)
  866. {
  867. char section[BUFFERSIZE];
  868. char name[BUFFERSIZE];
  869. char desc[BUFFERSIZE];
  870. char def[BUFFERSIZE];
  871. char tmp[BUFFERSIZE];
  872. misdn_cfg_get_name(elem, tmp, sizeof(tmp));
  873. term_color(name, tmp, COLOR_BRWHITE, 0, sizeof(tmp));
  874. misdn_cfg_get_desc(elem, desc, sizeof(desc), def, sizeof(def));
  875. if (elem < MISDN_CFG_LAST)
  876. term_color(section, "PORTS SECTION", COLOR_YELLOW, 0, sizeof(section));
  877. else
  878. term_color(section, "GENERAL SECTION", COLOR_YELLOW, 0, sizeof(section));
  879. if (*def)
  880. ast_cli(fd, "[%s] %s (Default: %s)\n\t%s\n", section, name, def, desc);
  881. else
  882. ast_cli(fd, "[%s] %s\n\t%s\n", section, name, desc);
  883. }
  884. static int misdn_show_config (int fd, int argc, char *argv[])
  885. {
  886. char buffer[BUFFERSIZE];
  887. enum misdn_cfg_elements elem;
  888. int linebreak;
  889. int onlyport = -1;
  890. int ok = 0;
  891. if (argc >= 4) {
  892. if (!strcmp(argv[3], "description")) {
  893. if (argc == 5) {
  894. enum misdn_cfg_elements elem = misdn_cfg_get_elem(argv[4]);
  895. if (elem == MISDN_CFG_FIRST)
  896. ast_cli(fd, "Unknown element: %s\n", argv[4]);
  897. else
  898. show_config_description(fd, elem);
  899. return 0;
  900. }
  901. return RESULT_SHOWUSAGE;
  902. }
  903. if (!strcmp(argv[3], "descriptions")) {
  904. if ((argc == 4) || ((argc == 5) && !strcmp(argv[4], "general"))) {
  905. for (elem = MISDN_GEN_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
  906. show_config_description(fd, elem);
  907. ast_cli(fd, "\n");
  908. }
  909. ok = 1;
  910. }
  911. if ((argc == 4) || ((argc == 5) && !strcmp(argv[4], "ports"))) {
  912. for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_CFG_LAST - 1 /* the ptp hack, remove the -1 when ptp is gone */; ++elem) {
  913. show_config_description(fd, elem);
  914. ast_cli(fd, "\n");
  915. }
  916. ok = 1;
  917. }
  918. return ok ? 0 : RESULT_SHOWUSAGE;
  919. }
  920. if (!sscanf(argv[3], "%30d", &onlyport) || onlyport < 0) {
  921. ast_cli(fd, "Unknown option: %s\n", argv[3]);
  922. return RESULT_SHOWUSAGE;
  923. }
  924. }
  925. if (argc == 3 || onlyport == 0) {
  926. ast_cli(fd, "Misdn General-Config:\n");
  927. for (elem = MISDN_GEN_FIRST + 1, linebreak = 1; elem < MISDN_GEN_LAST; elem++, linebreak++) {
  928. misdn_cfg_get_config_string(0, elem, buffer, BUFFERSIZE);
  929. ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  930. }
  931. ast_cli(fd, "\n");
  932. }
  933. if (onlyport < 0) {
  934. int port = misdn_cfg_get_next_port(0);
  935. for (; port > 0; port = misdn_cfg_get_next_port(port)) {
  936. ast_cli(fd, "\n[PORT %d]\n", port);
  937. for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
  938. misdn_cfg_get_config_string(port, elem, buffer, BUFFERSIZE);
  939. ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  940. }
  941. ast_cli(fd, "\n");
  942. }
  943. }
  944. if (onlyport > 0) {
  945. if (misdn_cfg_is_port_valid(onlyport)) {
  946. ast_cli(fd, "[PORT %d]\n", onlyport);
  947. for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
  948. misdn_cfg_get_config_string(onlyport, elem, buffer, BUFFERSIZE);
  949. ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  950. }
  951. ast_cli(fd, "\n");
  952. } else {
  953. ast_cli(fd, "Port %d is not active!\n", onlyport);
  954. }
  955. }
  956. return 0;
  957. }
  958. struct state_struct {
  959. enum misdn_chan_state state;
  960. char txt[255];
  961. };
  962. static struct state_struct state_array[] = {
  963. {MISDN_NOTHING,"NOTHING"}, /* at beginning */
  964. {MISDN_WAITING4DIGS,"WAITING4DIGS"}, /* when waiting for infos */
  965. {MISDN_EXTCANTMATCH,"EXTCANTMATCH"}, /* when asterisk couldn't match our ext */
  966. {MISDN_INCOMING_SETUP,"INCOMING SETUP"}, /* when pbx_start */
  967. {MISDN_DIALING,"DIALING"}, /* when pbx_start */
  968. {MISDN_PROGRESS,"PROGRESS"}, /* when pbx_start */
  969. {MISDN_PROCEEDING,"PROCEEDING"}, /* when pbx_start */
  970. {MISDN_CALLING,"CALLING"}, /* when misdn_call is called */
  971. {MISDN_CALLING_ACKNOWLEDGE,"CALLING_ACKNOWLEDGE"}, /* when misdn_call is called */
  972. {MISDN_ALERTING,"ALERTING"}, /* when Alerting */
  973. {MISDN_BUSY,"BUSY"}, /* when BUSY */
  974. {MISDN_CONNECTED,"CONNECTED"}, /* when connected */
  975. {MISDN_DISCONNECTED,"DISCONNECTED"}, /* when connected */
  976. {MISDN_CLEANING,"CLEANING"}, /* when hangup from * but we were connected before */
  977. };
  978. static const char *misdn_get_ch_state(struct chan_list *p)
  979. {
  980. int i;
  981. static char state[8];
  982. if( !p) return NULL;
  983. for (i = 0; i < sizeof(state_array) / sizeof(struct state_struct); i++) {
  984. if (state_array[i].state == p->state)
  985. return state_array[i].txt;
  986. }
  987. snprintf(state, sizeof(state), "%d", p->state) ;
  988. return state;
  989. }
  990. static void reload_config(void)
  991. {
  992. int i, cfg_debug;
  993. if (!g_config_initialized) {
  994. ast_log(LOG_WARNING, "chan_misdn is not initialized properly, still reloading ?\n");
  995. return ;
  996. }
  997. free_robin_list();
  998. misdn_cfg_reload();
  999. misdn_cfg_update_ptp();
  1000. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
  1001. misdn_cfg_get(0, MISDN_GEN_DEBUG, &cfg_debug, sizeof(int));
  1002. for (i = 0; i <= max_ports; i++) {
  1003. misdn_debug[i] = cfg_debug;
  1004. misdn_debug_only[i] = 0;
  1005. }
  1006. }
  1007. static int misdn_reload (int fd, int argc, char *argv[])
  1008. {
  1009. ast_cli(fd, "Reloading mISDN configuration\n");
  1010. reload_config();
  1011. return 0;
  1012. }
  1013. static void print_bc_info (int fd, struct chan_list *help, struct misdn_bchannel *bc)
  1014. {
  1015. struct ast_channel *ast = help->ast;
  1016. ast_cli(fd,
  1017. "* Pid:%d Prt:%d Ch:%d Mode:%s Org:%s dad:%s oad:%s rad:%s ctx:%s state:%s\n",
  1018. bc->pid, bc->port, bc->channel,
  1019. bc->nt ? "NT" : "TE",
  1020. help->originator == ORG_AST ? "*" : "I",
  1021. ast ? ast->exten : NULL,
  1022. ast ? ast->cid.cid_num : NULL,
  1023. bc->rad,
  1024. ast ? ast->context : NULL,
  1025. misdn_get_ch_state(help)
  1026. );
  1027. if (misdn_debug[bc->port] > 0)
  1028. ast_cli(fd,
  1029. " --> astname: %s\n"
  1030. " --> ch_l3id: %x\n"
  1031. " --> ch_addr: %x\n"
  1032. " --> bc_addr: %x\n"
  1033. " --> bc_l3id: %x\n"
  1034. " --> display: %s\n"
  1035. " --> activated: %d\n"
  1036. " --> state: %s\n"
  1037. " --> capability: %s\n"
  1038. #ifdef MISDN_1_2
  1039. " --> pipeline: %s\n"
  1040. #else
  1041. " --> echo_cancel: %d\n"
  1042. #endif
  1043. " --> notone : rx %d tx:%d\n"
  1044. " --> bc_hold: %d\n",
  1045. help->ast->name,
  1046. help->l3id,
  1047. help->addr,
  1048. bc->addr,
  1049. bc ? bc->l3_id : -1,
  1050. bc->display,
  1051. bc->active,
  1052. bc_state2str(bc->bc_state),
  1053. bearer2str(bc->capability),
  1054. #ifdef MISDN_1_2
  1055. bc->pipeline,
  1056. #else
  1057. bc->ec_enable,
  1058. #endif
  1059. help->norxtone, help->notxtone,
  1060. bc->holded
  1061. );
  1062. }
  1063. static int misdn_show_cls (int fd, int argc, char *argv[])
  1064. {
  1065. struct chan_list *help;
  1066. help = cl_te;
  1067. ast_cli(fd, "Channel List: %p\n", cl_te);
  1068. for (; help; help = help->next) {
  1069. struct misdn_bchannel *bc = help->bc;
  1070. struct ast_channel *ast = help->ast;
  1071. if (!ast) {
  1072. if (!bc) {
  1073. ast_cli(fd, "chan_list obj. with l3id:%x has no bc and no ast Leg\n", help->l3id);
  1074. continue;
  1075. }
  1076. ast_cli(fd, "bc with pid:%d has no Ast Leg\n", bc->pid);
  1077. continue;
  1078. }
  1079. if (misdn_debug[0] > 2)
  1080. ast_cli(fd, "Bc:%p Ast:%p\n", bc, ast);
  1081. if (bc) {
  1082. print_bc_info(fd, help, bc);
  1083. } else {
  1084. if (help->hold.state != MISDN_HOLD_IDLE) {
  1085. ast_cli(fd, "ITS A HELD CALL BC:\n");
  1086. ast_cli(fd, " --> l3_id: %x\n"
  1087. " --> dad:%s oad:%s\n"
  1088. " --> hold_port: %d\n"
  1089. " --> hold_channel: %d\n",
  1090. help->l3id,
  1091. ast->exten,
  1092. ast->cid.cid_num,
  1093. help->hold.port,
  1094. help->hold.channel
  1095. );
  1096. } else {
  1097. ast_cli(fd, "* Channel in unknown STATE !!! Exten:%s, Callerid:%s\n", ast->exten, ast->cid.cid_num);
  1098. }
  1099. }
  1100. }
  1101. misdn_dump_chanlist();
  1102. return 0;
  1103. }
  1104. static int misdn_show_cl (int fd, int argc, char *argv[])
  1105. {
  1106. struct chan_list *help;
  1107. if (argc != 4)
  1108. return RESULT_SHOWUSAGE;
  1109. help = cl_te;
  1110. for (; help; help = help->next) {
  1111. struct misdn_bchannel *bc = help->bc;
  1112. struct ast_channel *ast = help->ast;
  1113. if (bc && ast) {
  1114. if (!strcasecmp(ast->name,argv[3])) {
  1115. print_bc_info(fd, help, bc);
  1116. break;
  1117. }
  1118. }
  1119. }
  1120. return 0;
  1121. }
  1122. ast_mutex_t lock;
  1123. int MAXTICS = 8;
  1124. static int misdn_set_tics (int fd, int argc, char *argv[])
  1125. {
  1126. if (argc != 4)
  1127. return RESULT_SHOWUSAGE;
  1128. MAXTICS = atoi(argv[3]);
  1129. return 0;
  1130. }
  1131. static int misdn_show_stacks (int fd, int argc, char *argv[])
  1132. {
  1133. int port;
  1134. ast_cli(fd, "BEGIN STACK_LIST:\n");
  1135. for (port = misdn_cfg_get_next_port(0); port > 0;
  1136. port = misdn_cfg_get_next_port(port)) {
  1137. char buf[128];
  1138. get_show_stack_details(port, buf);
  1139. ast_cli(fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : "");
  1140. }
  1141. return 0;
  1142. }
  1143. static int misdn_show_ports_stats (int fd, int argc, char *argv[])
  1144. {
  1145. int port;
  1146. ast_cli(fd, "Port\tin_calls\tout_calls\n");
  1147. for (port = misdn_cfg_get_next_port(0); port > 0;
  1148. port = misdn_cfg_get_next_port(port)) {
  1149. ast_cli(fd, "%d\t%d\t\t%d\n", port, misdn_in_calls[port], misdn_out_calls[port]);
  1150. }
  1151. ast_cli(fd, "\n");
  1152. return 0;
  1153. }
  1154. static int misdn_show_port (int fd, int argc, char *argv[])
  1155. {
  1156. int port;
  1157. char buf[128];
  1158. if (argc != 4)
  1159. return RESULT_SHOWUSAGE;
  1160. port = atoi(argv[3]);
  1161. ast_cli(fd, "BEGIN STACK_LIST:\n");
  1162. get_show_stack_details(port, buf);
  1163. ast_cli(fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : "");
  1164. return 0;
  1165. }
  1166. static int misdn_send_cd (int fd, int argc, char *argv[])
  1167. {
  1168. char *channame;
  1169. char *nr;
  1170. struct chan_list *tmp;
  1171. if (argc != 5)
  1172. return RESULT_SHOWUSAGE;
  1173. {
  1174. channame = argv[3];
  1175. nr = argv[4];
  1176. ast_cli(fd, "Sending Calldeflection (%s) to %s\n", nr, channame);
  1177. tmp = get_chan_by_ast_name(channame);
  1178. if (!tmp) {
  1179. ast_cli(fd, "Sending CD with nr %s to %s failed: Channel does not exist.\n",nr, channame);
  1180. return 0;
  1181. }
  1182. if (strlen(nr) >= 15) {
  1183. ast_cli(fd, "Sending CD with nr %s to %s failed: Number too long (up to 15 digits are allowed).\n",nr, channame);
  1184. return 0;
  1185. }
  1186. tmp->bc->fac_out.Function = Fac_CD;
  1187. ast_copy_string((char *)tmp->bc->fac_out.u.CDeflection.DeflectedToNumber, nr, sizeof(tmp->bc->fac_out.u.CDeflection.DeflectedToNumber));
  1188. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  1189. }
  1190. return 0;
  1191. }
  1192. static int misdn_send_restart(int fd, int argc, char *argv[])
  1193. {
  1194. int port;
  1195. int channel;
  1196. if (argc < 4 || argc > 5)
  1197. return RESULT_SHOWUSAGE;
  1198. port = atoi(argv[3]);
  1199. if (argc == 5) {
  1200. channel = atoi(argv[4]);
  1201. misdn_lib_send_restart(port, channel);
  1202. } else {
  1203. misdn_lib_send_restart(port, -1);
  1204. }
  1205. return 0;
  1206. }
  1207. static int misdn_send_digit (int fd, int argc, char *argv[])
  1208. {
  1209. char *channame;
  1210. char *msg;
  1211. struct chan_list *tmp;
  1212. int i, msglen;
  1213. if (argc != 5)
  1214. return RESULT_SHOWUSAGE;
  1215. channame = argv[3];
  1216. msg = argv[4];
  1217. msglen = strlen(msg);
  1218. ast_cli(fd, "Sending %s to %s\n", msg, channame);
  1219. tmp = get_chan_by_ast_name(channame);
  1220. if (!tmp) {
  1221. ast_cli(fd, "Sending %s to %s failed Channel does not exist\n", msg, channame);
  1222. return 0;
  1223. }
  1224. #if 1
  1225. for (i = 0; i < msglen; i++) {
  1226. ast_cli(fd, "Sending: %c\n", msg[i]);
  1227. send_digit_to_chan(tmp, msg[i]);
  1228. /* res = ast_safe_sleep(tmp->ast, 250); */
  1229. usleep(250000);
  1230. /* res = ast_waitfor(tmp->ast,100); */
  1231. }
  1232. #else
  1233. ast_dtmf_stream(tmp->ast, NULL, msg, 250);
  1234. #endif
  1235. return 0;
  1236. }
  1237. static int misdn_toggle_echocancel (int fd, int argc, char *argv[])
  1238. {
  1239. char *channame;
  1240. struct chan_list *tmp;
  1241. if (argc != 4)
  1242. return RESULT_SHOWUSAGE;
  1243. channame = argv[3];
  1244. ast_cli(fd, "Toggling EchoCancel on %s\n", channame);
  1245. tmp = get_chan_by_ast_name(channame);
  1246. if (!tmp) {
  1247. ast_cli(fd, "Toggling EchoCancel %s failed Channel does not exist\n", channame);
  1248. return 0;
  1249. }
  1250. tmp->toggle_ec = tmp->toggle_ec ? 0 : 1;
  1251. if (tmp->toggle_ec) {
  1252. #ifdef MISDN_1_2
  1253. update_pipeline_config(tmp->bc);
  1254. #else
  1255. update_ec_config(tmp->bc);
  1256. #endif
  1257. manager_ec_enable(tmp->bc);
  1258. } else {
  1259. manager_ec_disable(tmp->bc);
  1260. }
  1261. return 0;
  1262. }
  1263. static int misdn_send_display (int fd, int argc, char *argv[])
  1264. {
  1265. char *channame;
  1266. char *msg;
  1267. struct chan_list *tmp;
  1268. if (argc != 5)
  1269. return RESULT_SHOWUSAGE;
  1270. channame = argv[3];
  1271. msg = argv[4];
  1272. ast_cli(fd, "Sending %s to %s\n", msg, channame);
  1273. tmp = get_chan_by_ast_name(channame);
  1274. if (tmp && tmp->bc) {
  1275. ast_copy_string(tmp->bc->display, msg, sizeof(tmp->bc->display));
  1276. misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
  1277. } else {
  1278. ast_cli(fd, "No such channel %s\n", channame);
  1279. return RESULT_FAILURE;
  1280. }
  1281. return RESULT_SUCCESS;
  1282. }
  1283. static char *complete_ch_helper(const char *line, const char *word, int pos, int state, int rpos)
  1284. {
  1285. struct ast_channel *c;
  1286. int which=0;
  1287. char *ret;
  1288. if (pos != rpos)
  1289. return NULL;
  1290. c = ast_channel_walk_locked(NULL);
  1291. while(c) {
  1292. if (!strncasecmp(word, c->name, strlen(word))) {
  1293. if (++which > state)
  1294. break;
  1295. }
  1296. ast_mutex_unlock(&c->lock);
  1297. c = ast_channel_walk_locked(c);
  1298. }
  1299. if (c) {
  1300. ret = strdup(c->name);
  1301. ast_mutex_unlock(&c->lock);
  1302. } else
  1303. ret = NULL;
  1304. return ret;
  1305. }
  1306. static char *complete_ch(const char *line, const char *word, int pos, int state)
  1307. {
  1308. return complete_ch_helper(line, word, pos, state, 3);
  1309. }
  1310. static char *complete_debug_port (const char *line, const char *word, int pos, int state)
  1311. {
  1312. if (state)
  1313. return NULL;
  1314. switch (pos) {
  1315. case 4:
  1316. if (*word == 'p')
  1317. return strdup("port");
  1318. else if (*word == 'o')
  1319. return strdup("only");
  1320. break;
  1321. case 6:
  1322. if (*word == 'o')
  1323. return strdup("only");
  1324. break;
  1325. }
  1326. return NULL;
  1327. }
  1328. static char *complete_show_config (const char *line, const char *word, int pos, int state)
  1329. {
  1330. char buffer[BUFFERSIZE];
  1331. enum misdn_cfg_elements elem;
  1332. int wordlen = strlen(word);
  1333. int which = 0;
  1334. int port = 0;
  1335. switch (pos) {
  1336. case 3:
  1337. if ((!strncmp(word, "description", wordlen)) && (++which > state))
  1338. return strdup("description");
  1339. if ((!strncmp(word, "descriptions", wordlen)) && (++which > state))
  1340. return strdup("descriptions");
  1341. if ((!strncmp(word, "0", wordlen)) && (++which > state))
  1342. return strdup("0");
  1343. while ((port = misdn_cfg_get_next_port(port)) != -1) {
  1344. snprintf(buffer, sizeof(buffer), "%d", port);
  1345. if ((!strncmp(word, buffer, wordlen)) && (++which > state)) {
  1346. return strdup(buffer);
  1347. }
  1348. }
  1349. break;
  1350. case 4:
  1351. if (strstr(line, "description ")) {
  1352. for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
  1353. if ((elem == MISDN_CFG_LAST) || (elem == MISDN_GEN_FIRST))
  1354. continue;
  1355. misdn_cfg_get_name(elem, buffer, BUFFERSIZE);
  1356. if (!wordlen || !strncmp(word, buffer, wordlen)) {
  1357. if (++which > state)
  1358. return strdup(buffer);
  1359. }
  1360. }
  1361. } else if (strstr(line, "descriptions ")) {
  1362. if ((!wordlen || !strncmp(word, "general", wordlen)) && (++which > state))
  1363. return strdup("general");
  1364. if ((!wordlen || !strncmp(word, "ports", wordlen)) && (++which > state))
  1365. return strdup("ports");
  1366. }
  1367. break;
  1368. }
  1369. return NULL;
  1370. }
  1371. static struct ast_cli_entry chan_misdn_clis[] = {
  1372. { {"misdn","send","calldeflect", NULL}, misdn_send_cd, "Sends CallDeflection to mISDN Channel",
  1373. "Usage: misdn send calldeflect <channel> \"<nr>\" \n", complete_ch },
  1374. { {"misdn","send","digit", NULL}, misdn_send_digit, "Sends DTMF Digit to mISDN Channel",
  1375. "Usage: misdn send digit <channel> \"<msg>\" \n"
  1376. " Send <digit> to <channel> as DTMF Tone\n"
  1377. " when channel is a mISDN channel\n", complete_ch },
  1378. { {"misdn","toggle","echocancel", NULL}, misdn_toggle_echocancel, "Toggles EchoCancel on mISDN Channel",
  1379. "Usage: misdn toggle echocancel <channel>\n", complete_ch },
  1380. { {"misdn","send","display", NULL}, misdn_send_display, "Sends Text to mISDN Channel",
  1381. "Usage: misdn send display <channel> \"<msg>\" \n"
  1382. " Send <msg> to <channel> as Display Message\n"
  1383. " when channel is a mISDN channel\n", complete_ch },
  1384. { {"misdn","show","config", NULL}, misdn_show_config, "Shows internal mISDN config, read from cfg-file",
  1385. "Usage: misdn show config [<port> | description <config element> | descriptions [general|ports]]\n"
  1386. " Use 0 for <port> to only print the general config.\n", complete_show_config },
  1387. { {"misdn","reload", NULL}, misdn_reload, "Reloads internal mISDN config, read from cfg-file",
  1388. "Usage: misdn reload\n" },
  1389. { {"misdn","set","tics", NULL}, misdn_set_tics, "",
  1390. "\n" },
  1391. { {"misdn","show","channels", NULL}, misdn_show_cls, "Shows internal mISDN chan_list",
  1392. "Usage: misdn show channels\n" },
  1393. { {"misdn","show","channel", NULL}, misdn_show_cl, "Shows internal mISDN chan_list",
  1394. "Usage: misdn show channels\n", complete_ch },
  1395. { {"misdn","port","block", NULL}, misdn_port_block, "Blocks the given port",
  1396. "Usage: misdn port block\n" },
  1397. { {"misdn","port","unblock", NULL}, misdn_port_unblock, "Unblocks the given port",
  1398. "Usage: misdn port unblock\n" },
  1399. { {"misdn","restart","port", NULL}, misdn_restart_port, "Restarts the given port",
  1400. "Usage: misdn restart port\n" },
  1401. { {"misdn","restart","pid", NULL}, misdn_restart_pid, "Restarts the given pid",
  1402. "Usage: misdn restart pid\n" },
  1403. { {"misdn","send","restart", NULL}, misdn_send_restart,
  1404. "Sends a restart for every bchannel on the given port",
  1405. "Usage: misdn send restart <port>\n"},
  1406. { {"misdn","port","up", NULL}, misdn_port_up, "Tries to establish L1 on the given port",
  1407. "Usage: misdn port up <port>\n" },
  1408. { {"misdn","port","down", NULL}, misdn_port_down, "Tries to deactivate the L1 on the given port",
  1409. "Usage: misdn port down <port>\n" },
  1410. { {"misdn","show","stacks", NULL}, misdn_show_stacks, "Shows internal mISDN stack_list",
  1411. "Usage: misdn show stacks\n" },
  1412. { {"misdn","show","ports","stats", NULL}, misdn_show_ports_stats, "Shows chan_misdns call statistics per port",
  1413. "Usage: misdn show port stats\n" },
  1414. { {"misdn","show","port", NULL}, misdn_show_port, "Shows detailed information for given port",
  1415. "Usage: misdn show port <port>\n" },
  1416. { {"misdn","set","debug", NULL}, misdn_set_debug, "Sets Debuglevel of chan_misdn",
  1417. "Usage: misdn set debug <level> [only] | [port <port> [only]]\n", complete_debug_port },
  1418. { {"misdn","set","crypt","debug", NULL}, misdn_set_crypt_debug, "Sets CryptDebuglevel of chan_misdn, at the moment, level={1,2}",
  1419. "Usage: misdn set crypt debug <level>\n" }
  1420. };
  1421. /*! \brief Updates caller ID information from config */
  1422. static int update_config(struct chan_list *ch, int orig)
  1423. {
  1424. struct ast_channel *ast;
  1425. struct misdn_bchannel *bc;
  1426. int port, hdlc = 0;
  1427. int pres, screen;
  1428. if (!ch) {
  1429. ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
  1430. return -1;
  1431. }
  1432. ast = ch->ast;
  1433. bc = ch->bc;
  1434. if (! ast || ! bc) {
  1435. ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
  1436. return -1;
  1437. }
  1438. port = bc->port;
  1439. chan_misdn_log(7, port, "update_config: Getting Config\n");
  1440. misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(int));
  1441. if (hdlc) {
  1442. switch (bc->capability) {
  1443. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  1444. case INFO_CAPABILITY_DIGITAL_RESTRICTED:
  1445. chan_misdn_log(1, bc->port, " --> CONF HDLC\n");
  1446. bc->hdlc = 1;
  1447. break;
  1448. }
  1449. }
  1450. misdn_cfg_get(port, MISDN_CFG_PRES, &pres, sizeof(pres));
  1451. misdn_cfg_get(port, MISDN_CFG_SCREEN, &screen, sizeof(screen));
  1452. chan_misdn_log(2, port, " --> pres: %d screen: %d\n", pres, screen);
  1453. if (pres < 0 || screen < 0) {
  1454. chan_misdn_log(2, port, " --> pres: %x\n", ast->cid.cid_pres);
  1455. switch (ast->cid.cid_pres & 0x60) {
  1456. case AST_PRES_RESTRICTED:
  1457. bc->pres = 1;
  1458. chan_misdn_log(2, port, " --> PRES: Restricted (1)\n");
  1459. break;
  1460. case AST_PRES_UNAVAILABLE:
  1461. bc->pres = 2;
  1462. chan_misdn_log(2, port, " --> PRES: Unavailable (2)\n");
  1463. break;
  1464. default:
  1465. bc->pres = 0;
  1466. chan_misdn_log(2, port, " --> PRES: Allowed (0)\n");
  1467. break;
  1468. }
  1469. switch (ast->cid.cid_pres & 0x3) {
  1470. default:
  1471. case AST_PRES_USER_NUMBER_UNSCREENED:
  1472. bc->screen = 0;
  1473. chan_misdn_log(2, port, " --> SCREEN: Unscreened (0)\n");
  1474. break;
  1475. case AST_PRES_USER_NUMBER_PASSED_SCREEN:
  1476. bc->screen = 1;
  1477. chan_misdn_log(2, port, " --> SCREEN: Passed Screen (1)\n");
  1478. break;
  1479. case AST_PRES_USER_NUMBER_FAILED_SCREEN:
  1480. bc->screen = 2;
  1481. chan_misdn_log(2, port, " --> SCREEN: Failed Screen (2)\n");
  1482. break;
  1483. case AST_PRES_NETWORK_NUMBER:
  1484. bc->screen = 3;
  1485. chan_misdn_log(2, port, " --> SCREEN: Network Nr. (3)\n");
  1486. break;
  1487. }
  1488. } else {
  1489. bc->screen = screen;
  1490. bc->pres = pres;
  1491. }
  1492. return 0;
  1493. }
  1494. static void config_jitterbuffer(struct chan_list *ch)
  1495. {
  1496. struct misdn_bchannel *bc = ch->bc;
  1497. int len = ch->jb_len, threshold = ch->jb_upper_threshold;
  1498. chan_misdn_log(5, bc->port, "config_jb: Called\n");
  1499. if (! len) {
  1500. chan_misdn_log(1, bc->port, "config_jb: Deactivating Jitterbuffer\n");
  1501. bc->nojitter=1;
  1502. } else {
  1503. if (len <= 100 || len > 8000) {
  1504. chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer out of Bounds, setting to 1000\n");
  1505. len = 1000;
  1506. }
  1507. if ( threshold > len ) {
  1508. chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer Threshold > Jitterbuffer setting to Jitterbuffer -1\n");
  1509. }
  1510. if ( ch->jb) {
  1511. cb_log(0, bc->port, "config_jb: We've got a Jitterbuffer Already on this port.\n");
  1512. misdn_jb_destroy(ch->jb);
  1513. ch->jb = NULL;
  1514. }
  1515. ch->jb=misdn_jb_init(len, threshold);
  1516. if (!ch->jb )
  1517. bc->nojitter = 1;
  1518. }
  1519. }
  1520. void debug_numplan(int port, int numplan, char *type)
  1521. {
  1522. switch (numplan) {
  1523. case NUMPLAN_INTERNATIONAL:
  1524. chan_misdn_log(2, port, " --> %s: International\n", type);
  1525. break;
  1526. case NUMPLAN_NATIONAL:
  1527. chan_misdn_log(2, port, " --> %s: National\n", type);
  1528. break;
  1529. case NUMPLAN_SUBSCRIBER:
  1530. chan_misdn_log(2, port, " --> %s: Subscriber\n", type);
  1531. break;
  1532. case NUMPLAN_UNKNOWN:
  1533. chan_misdn_log(2, port, " --> %s: Unknown\n", type);
  1534. break;
  1535. /* Maybe we should cut off the prefix if present ? */
  1536. default:
  1537. chan_misdn_log(0, port, " --> !!!! Wrong dialplan setting, please see the misdn.conf sample file\n ");
  1538. break;
  1539. }
  1540. }
  1541. #ifdef MISDN_1_2
  1542. static int update_pipeline_config(struct misdn_bchannel *bc)
  1543. {
  1544. int ec;
  1545. misdn_cfg_get(bc->port, MISDN_CFG_PIPELINE, bc->pipeline, sizeof(bc->pipeline));
  1546. if (*bc->pipeline)
  1547. return 0;
  1548. misdn_cfg_get(bc->port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int));
  1549. if (ec == 1)
  1550. ast_copy_string(bc->pipeline, "mg2ec", sizeof(bc->pipeline));
  1551. else if (ec > 1)
  1552. snprintf(bc->pipeline, sizeof(bc->pipeline), "mg2ec(deftaps=%d)", ec);
  1553. return 0;
  1554. }
  1555. #else
  1556. static int update_ec_config(struct misdn_bchannel *bc)
  1557. {
  1558. int ec;
  1559. int port = bc->port;
  1560. misdn_cfg_get(port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int));
  1561. if (ec == 1) {
  1562. bc->ec_enable = 1;
  1563. } else if (ec > 1) {
  1564. bc->ec_enable = 1;
  1565. bc->ec_deftaps = ec;
  1566. }
  1567. return 0;
  1568. }
  1569. #endif
  1570. static int read_config(struct chan_list *ch, int orig)
  1571. {
  1572. struct ast_channel *ast;
  1573. struct misdn_bchannel *bc;
  1574. int port;
  1575. int hdlc = 0;
  1576. char lang[BUFFERSIZE + 1];
  1577. char faxdetect[BUFFERSIZE + 1];
  1578. char buf[256];
  1579. char buf2[256];
  1580. ast_group_t pg;
  1581. ast_group_t cg;
  1582. if (!ch) {
  1583. ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
  1584. return -1;
  1585. }
  1586. ast = ch->ast;
  1587. bc = ch->bc;
  1588. if (! ast || ! bc) {
  1589. ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
  1590. return -1;
  1591. }
  1592. port = bc->port;
  1593. chan_misdn_log(1, port, "read_config: Getting Config\n");
  1594. misdn_cfg_get(port, MISDN_CFG_LANGUAGE, lang, BUFFERSIZE);
  1595. ast_string_field_set(ast, language, lang);
  1596. misdn_cfg_get(port, MISDN_CFG_MUSICCLASS, ch->mohinterpret, sizeof(ch->mohinterpret));
  1597. misdn_cfg_get(port, MISDN_CFG_TXGAIN, &bc->txgain, sizeof(int));
  1598. misdn_cfg_get(port, MISDN_CFG_RXGAIN, &bc->rxgain, sizeof(int));
  1599. misdn_cfg_get(port, MISDN_CFG_INCOMING_EARLY_AUDIO, &ch->incoming_early_audio, sizeof(int));
  1600. misdn_cfg_get(port, MISDN_CFG_SENDDTMF, &bc->send_dtmf, sizeof(int));
  1601. misdn_cfg_get(port, MISDN_CFG_ASTDTMF, &ch->ast_dsp, sizeof(int));
  1602. if (ch->ast_dsp) {
  1603. ch->ignore_dtmf = 1;
  1604. }
  1605. misdn_cfg_get(port, MISDN_CFG_NEED_MORE_INFOS, &bc->need_more_infos, sizeof(int));
  1606. misdn_cfg_get(port, MISDN_CFG_NTTIMEOUT, &ch->nttimeout, sizeof(int));
  1607. misdn_cfg_get(port, MISDN_CFG_NOAUTORESPOND_ON_SETUP, &ch->noautorespond_on_setup, sizeof(int));
  1608. misdn_cfg_get(port, MISDN_CFG_FAR_ALERTING, &ch->far_alerting, sizeof(int));
  1609. misdn_cfg_get(port, MISDN_CFG_ALLOWED_BEARERS, &ch->allowed_bearers, BUFFERSIZE);
  1610. misdn_cfg_get(port, MISDN_CFG_FAXDETECT, faxdetect, BUFFERSIZE);
  1611. misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(int));
  1612. if (hdlc) {
  1613. switch (bc->capability) {
  1614. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  1615. case INFO_CAPABILITY_DIGITAL_RESTRICTED:
  1616. chan_misdn_log(1, bc->port, " --> CONF HDLC\n");
  1617. bc->hdlc = 1;
  1618. break;
  1619. }
  1620. }
  1621. /*Initialize new Jitterbuffer*/
  1622. misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER, &ch->jb_len, sizeof(int));
  1623. misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, &ch->jb_upper_threshold, sizeof(int));
  1624. config_jitterbuffer(ch);
  1625. misdn_cfg_get(bc->port, MISDN_CFG_CONTEXT, ch->context, sizeof(ch->context));
  1626. ast_copy_string(ast->context, ch->context, sizeof(ast->context));
  1627. #ifdef MISDN_1_2
  1628. update_pipeline_config(bc);
  1629. #else
  1630. update_ec_config(bc);
  1631. #endif
  1632. {
  1633. int eb3;
  1634. misdn_cfg_get( bc->port, MISDN_CFG_EARLY_BCONNECT, &eb3, sizeof(int));
  1635. bc->early_bconnect=eb3;
  1636. }
  1637. misdn_cfg_get(port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg));
  1638. misdn_cfg_get(port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg));
  1639. chan_misdn_log(5, port, " --> * CallGrp:%s PickupGrp:%s\n", ast_print_group(buf, sizeof(buf), cg), ast_print_group(buf2, sizeof(buf2), pg));
  1640. ast->pickupgroup = pg;
  1641. ast->callgroup = cg;
  1642. if (orig == ORG_AST) {
  1643. char callerid[BUFFERSIZE + 1];
  1644. /* ORIGINATOR Asterisk (outgoing call) */
  1645. misdn_cfg_get(port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(bc->te_choose_channel), sizeof(int));
  1646. if (strstr(faxdetect, "outgoing") || strstr(faxdetect, "both")) {
  1647. if (strstr(faxdetect, "nojump"))
  1648. ch->faxdetect = 2;
  1649. else
  1650. ch->faxdetect = 1;
  1651. }
  1652. misdn_cfg_get(port, MISDN_CFG_CALLERID, callerid, BUFFERSIZE);
  1653. if ( ! ast_strlen_zero(callerid) ) {
  1654. chan_misdn_log(1, port, " --> * Setting Cid to %s\n", callerid);
  1655. ast_copy_string(bc->oad, callerid, sizeof(bc->oad));
  1656. }
  1657. misdn_cfg_get(port, MISDN_CFG_DIALPLAN, &bc->dnumplan, sizeof(int));
  1658. misdn_cfg_get(port, MISDN_CFG_LOCALDIALPLAN, &bc->onumplan, sizeof(int));
  1659. misdn_cfg_get(port, MISDN_CFG_CPNDIALPLAN, &bc->cpnnumplan, sizeof(int));
  1660. debug_numplan(port, bc->dnumplan, "TON");
  1661. debug_numplan(port, bc->onumplan, "LTON");
  1662. debug_numplan(port, bc->cpnnumplan, "CTON");
  1663. ch->overlap_dial = 0;
  1664. } else {
  1665. /* ORIGINATOR MISDN (incoming call) */
  1666. char prefix[BUFFERSIZE + 1] = "";
  1667. if (strstr(faxdetect, "incoming") || strstr(faxdetect, "both")) {
  1668. if (strstr(faxdetect, "nojump"))
  1669. ch->faxdetect = 2;
  1670. else
  1671. ch->faxdetect = 1;
  1672. }
  1673. misdn_cfg_get(port, MISDN_CFG_CPNDIALPLAN, &bc->cpnnumplan, sizeof(int));
  1674. debug_numplan(port, bc->cpnnumplan, "CTON");
  1675. switch (bc->onumplan) {
  1676. case NUMPLAN_INTERNATIONAL:
  1677. misdn_cfg_get(bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
  1678. break;
  1679. case NUMPLAN_NATIONAL:
  1680. misdn_cfg_get(bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
  1681. break;
  1682. default:
  1683. break;
  1684. }
  1685. ast_copy_string(buf, bc->oad, sizeof(buf));
  1686. snprintf(bc->oad, sizeof(bc->oad), "%s%s", prefix, buf);
  1687. if (!ast_strlen_zero(bc->dad)) {
  1688. ast_copy_string(bc->orig_dad, bc->dad, sizeof(bc->orig_dad));
  1689. }
  1690. if ( ast_strlen_zero(bc->dad) && !ast_strlen_zero(bc->keypad)) {
  1691. ast_copy_string(bc->dad, bc->keypad, sizeof(bc->dad));
  1692. }
  1693. prefix[0] = 0;
  1694. switch (bc->dnumplan) {
  1695. case NUMPLAN_INTERNATIONAL:
  1696. misdn_cfg_get(bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
  1697. break;
  1698. case NUMPLAN_NATIONAL:
  1699. misdn_cfg_get(bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
  1700. break;
  1701. default:
  1702. break;
  1703. }
  1704. ast_copy_string(buf, bc->dad, sizeof(buf));
  1705. snprintf(bc->dad, sizeof(bc->dad), "%s%s", prefix, buf);
  1706. if (strcmp(bc->dad, ast->exten)) {
  1707. ast_copy_string(ast->exten, bc->dad, sizeof(ast->exten));
  1708. }
  1709. ast_set_callerid(ast, bc->oad, NULL, bc->oad);
  1710. if ( !ast_strlen_zero(bc->rad) ) {
  1711. if (ast->cid.cid_rdnis)
  1712. free(ast->cid.cid_rdnis);
  1713. ast->cid.cid_rdnis = strdup(bc->rad);
  1714. }
  1715. misdn_cfg_get(bc->port, MISDN_CFG_OVERLAP_DIAL, &ch->overlap_dial, sizeof(ch->overlap_dial));
  1716. ast_mutex_init(&ch->overlap_tv_lock);
  1717. } /* ORIG MISDN END */
  1718. ch->overlap_dial_task = -1;
  1719. if (ch->faxdetect || ch->ast_dsp) {
  1720. misdn_cfg_get(port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout));
  1721. if (!ch->dsp)
  1722. ch->dsp = ast_dsp_new();
  1723. if (ch->dsp) {
  1724. if (ch->faxdetect)
  1725. ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT | DSP_FEATURE_FAX_DETECT);
  1726. else
  1727. ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT );
  1728. }
  1729. }
  1730. /* AOCD initialization */
  1731. bc->AOCDtype = Fac_None;
  1732. return 0;
  1733. }
  1734. /*****************************/
  1735. /*** AST Indications Start ***/
  1736. /*****************************/
  1737. static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
  1738. {
  1739. int port = 0;
  1740. int r;
  1741. int exceed;
  1742. int bridging;
  1743. struct chan_list *ch;
  1744. struct misdn_bchannel *newbc;
  1745. char *opts, *ext;
  1746. char *dest_cp;
  1747. if (!ast) {
  1748. ast_log(LOG_WARNING, " --> ! misdn_call called on ast_channel *ast where ast == NULL\n");
  1749. return -1;
  1750. }
  1751. if (((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) || !dest ) {
  1752. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
  1753. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  1754. ast_setstate(ast, AST_STATE_DOWN);
  1755. return -1;
  1756. }
  1757. ch = MISDN_ASTERISK_TECH_PVT(ast);
  1758. if (!ch) {
  1759. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
  1760. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  1761. ast_setstate(ast, AST_STATE_DOWN);
  1762. return -1;
  1763. }
  1764. newbc = ch->bc;
  1765. if (!newbc) {
  1766. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
  1767. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  1768. ast_setstate(ast, AST_STATE_DOWN);
  1769. return -1;
  1770. }
  1771. /*
  1772. * dest is ---v
  1773. * Dial(mISDN/g:group_name[/extension[/options]])
  1774. * Dial(mISDN/port[:preselected_channel][/extension[/options]])
  1775. *
  1776. * The dial extension could be empty if you are using MISDN_KEYPAD
  1777. * to control ISDN provider features.
  1778. */
  1779. dest_cp = ast_strdupa(dest);
  1780. strsep(&dest_cp, "/");/* Discard port/group token */
  1781. ext = strsep(&dest_cp, "/");
  1782. if (!ext) {
  1783. ext = "";
  1784. }
  1785. opts = dest_cp;
  1786. port = newbc->port;
  1787. if ((exceed = add_out_calls(port))) {
  1788. char tmp[16];
  1789. snprintf(tmp, sizeof(tmp), "%d", exceed);
  1790. pbx_builtin_setvar_helper(ast, "MAX_OVERFLOW", tmp);
  1791. return -1;
  1792. }
  1793. chan_misdn_log(1, port, "* CALL: %s\n", dest);
  1794. chan_misdn_log(2, port, " --> * dad:%s tech:%s ctx:%s\n", ast->exten, ast->name, ast->context);
  1795. chan_misdn_log(3, port, " --> * adding2newbc ext %s\n", ast->exten);
  1796. if (ast->exten) {
  1797. ast_copy_string(ast->exten, ext, sizeof(ast->exten));
  1798. ast_copy_string(newbc->dad, ext, sizeof(newbc->dad));
  1799. }
  1800. ast_copy_string(newbc->rad, S_OR(ast->cid.cid_rdnis, ""), sizeof(newbc->rad));
  1801. chan_misdn_log(3, port, " --> * adding2newbc callerid %s\n", ast->cid.cid_num);
  1802. if (ast_strlen_zero(newbc->oad) && !ast_strlen_zero(ast->cid.cid_num)) {
  1803. ast_copy_string(newbc->oad, ast->cid.cid_num, sizeof(newbc->oad));
  1804. }
  1805. newbc->capability = ast->transfercapability;
  1806. pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY", ast_transfercapability2str(newbc->capability));
  1807. if ( ast->transfercapability == INFO_CAPABILITY_DIGITAL_UNRESTRICTED) {
  1808. chan_misdn_log(2, port, " --> * Call with flag Digital\n");
  1809. }
  1810. /* update screening and presentation */
  1811. update_config(ch, ORG_AST);
  1812. /* fill in some ies from channel vary*/
  1813. import_ch(ast, newbc, ch);
  1814. /* Finally The Options Override Everything */
  1815. if (opts)
  1816. misdn_set_opt_exec(ast, opts);
  1817. else
  1818. chan_misdn_log(2, port, "NO OPTS GIVEN\n");
  1819. /*check for bridging*/
  1820. misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(bridging));
  1821. if (bridging && ch->other_ch) {
  1822. #ifdef MISDN_1_2
  1823. chan_misdn_log(1, port, "Disabling EC (aka Pipeline) on both Sides\n");
  1824. *ch->bc->pipeline = 0;
  1825. *ch->other_ch->bc->pipeline = 0;
  1826. #else
  1827. chan_misdn_log(1, port, "Disabling EC on both Sides\n");
  1828. ch->bc->ec_enable = 0;
  1829. ch->other_ch->bc->ec_enable = 0;
  1830. #endif
  1831. }
  1832. r = misdn_lib_send_event( newbc, EVENT_SETUP );
  1833. /** we should have l3id after sending setup **/
  1834. ch->l3id = newbc->l3_id;
  1835. if ( r == -ENOCHAN ) {
  1836. chan_misdn_log(0, port, " --> * Theres no Channel at the moment .. !\n");
  1837. chan_misdn_log(1, port, " --> * SEND: State Down pid:%d\n", newbc ? newbc->pid : -1);
  1838. ast->hangupcause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
  1839. ast_setstate(ast, AST_STATE_DOWN);
  1840. return -1;
  1841. }
  1842. chan_misdn_log(2, port, " --> * SEND: State Dialing pid:%d\n", newbc ? newbc->pid : 1);
  1843. ast_setstate(ast, AST_STATE_DIALING);
  1844. ast->hangupcause = AST_CAUSE_NORMAL_CLEARING;
  1845. if (newbc->nt)
  1846. stop_bc_tones(ch);
  1847. ch->state = MISDN_CALLING;
  1848. return 0;
  1849. }
  1850. static int misdn_answer(struct ast_channel *ast)
  1851. {
  1852. struct chan_list *p;
  1853. const char *tmp;
  1854. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) return -1;
  1855. chan_misdn_log(1, p ? (p->bc ? p->bc->port : 0) : 0, "* ANSWER:\n");
  1856. if (!p) {
  1857. ast_log(LOG_WARNING, " --> Channel not connected ??\n");
  1858. ast_queue_hangup(ast);
  1859. }
  1860. if (!p->bc) {
  1861. chan_misdn_log(1, 0, " --> Got Answer, but there is no bc obj ??\n");
  1862. ast_queue_hangup(ast);
  1863. }
  1864. tmp = pbx_builtin_getvar_helper(p->ast, "CRYPT_KEY");
  1865. if (!ast_strlen_zero(tmp)) {
  1866. chan_misdn_log(1, p->bc->port, " --> Connection will be BF crypted\n");
  1867. ast_copy_string(p->bc->crypt_key, tmp, sizeof(p->bc->crypt_key));
  1868. } else {
  1869. chan_misdn_log(3, p->bc->port, " --> Connection is without BF encryption\n");
  1870. }
  1871. tmp = pbx_builtin_getvar_helper(ast, "MISDN_DIGITAL_TRANS");
  1872. if (!ast_strlen_zero(tmp) && ast_true(tmp)) {
  1873. chan_misdn_log(1, p->bc->port, " --> Connection is transparent digital\n");
  1874. p->bc->nodsp = 1;
  1875. p->bc->hdlc = 0;
  1876. p->bc->nojitter = 1;
  1877. }
  1878. p->state = MISDN_CONNECTED;
  1879. stop_indicate(p);
  1880. if ( ast_strlen_zero(p->bc->cad) ) {
  1881. chan_misdn_log(2,p->bc->port," --> empty cad using dad\n");
  1882. ast_copy_string(p->bc->cad, p->bc->dad, sizeof(p->bc->cad));
  1883. }
  1884. misdn_lib_send_event( p->bc, EVENT_CONNECT);
  1885. start_bc_tones(p);
  1886. return 0;
  1887. }
  1888. static int misdn_digit_begin(struct ast_channel *chan, char digit)
  1889. {
  1890. /* XXX Modify this callback to support Asterisk controlling the length of DTMF */
  1891. return 0;
  1892. }
  1893. static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  1894. {
  1895. struct chan_list *p;
  1896. struct misdn_bchannel *bc;
  1897. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) return -1;
  1898. bc = p->bc;
  1899. chan_misdn_log(1, bc ? bc->port : 0, "* IND : Digit %c\n", digit);
  1900. if (!bc) {
  1901. ast_log(LOG_WARNING, " --> !! Got Digit Event without having bchannel Object\n");
  1902. return -1;
  1903. }
  1904. switch (p->state ) {
  1905. case MISDN_CALLING:
  1906. {
  1907. int l;
  1908. char buf[8];
  1909. buf[0]=digit;
  1910. buf[1]=0;
  1911. l = sizeof(bc->infos_pending);
  1912. strncat(bc->infos_pending, buf, l - strlen(bc->infos_pending) - 1);
  1913. }
  1914. break;
  1915. case MISDN_CALLING_ACKNOWLEDGE:
  1916. {
  1917. bc->info_dad[0]=digit;
  1918. bc->info_dad[1]=0;
  1919. {
  1920. int l = sizeof(bc->dad);
  1921. strncat(bc->dad, bc->info_dad, l - strlen(bc->dad) - 1);
  1922. }
  1923. {
  1924. int l = sizeof(p->ast->exten);
  1925. strncpy(p->ast->exten, bc->dad, l);
  1926. p->ast->exten[l-1] = 0;
  1927. }
  1928. misdn_lib_send_event( bc, EVENT_INFORMATION);
  1929. }
  1930. break;
  1931. default:
  1932. if ( bc->send_dtmf )
  1933. send_digit_to_chan(p,digit);
  1934. break;
  1935. }
  1936. return 0;
  1937. }
  1938. static int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast)
  1939. {
  1940. struct chan_list *p;
  1941. if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast) )) return -1;
  1942. chan_misdn_log(1, p->bc ? p->bc->port : 0, "* IND: Got Fixup State:%s L3id:%x\n", misdn_get_ch_state(p), p->l3id);
  1943. p->ast = ast;
  1944. return 0;
  1945. }
  1946. static int misdn_indication(struct ast_channel *ast, int cond, const void *data, size_t datalen)
  1947. {
  1948. struct chan_list *p;
  1949. if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast))) {
  1950. ast_log(LOG_WARNING, "Returned -1 in misdn_indication\n");
  1951. return -1;
  1952. }
  1953. if (!p->bc) {
  1954. if (p->hold.state == MISDN_HOLD_IDLE) {
  1955. chan_misdn_log(1, 0, "* IND : Indication [%d] ignored on %s\n", cond,
  1956. ast->name);
  1957. ast_log(LOG_WARNING, "Private Pointer but no bc ?\n");
  1958. } else {
  1959. chan_misdn_log(1, 0, "* IND : Indication [%d] ignored on hold %s\n",
  1960. cond, ast->name);
  1961. }
  1962. return -1;
  1963. }
  1964. chan_misdn_log(5, p->bc->port, "* IND : Indication [%d] on %s\n\n", cond, ast->name);
  1965. switch (cond) {
  1966. case AST_CONTROL_BUSY:
  1967. chan_misdn_log(1, p->bc->port, "* IND :\tbusy pid:%d\n", p->bc ? p->bc->pid : -1);
  1968. ast_setstate(ast, AST_STATE_BUSY);
  1969. p->bc->out_cause = AST_CAUSE_USER_BUSY;
  1970. if (p->state != MISDN_CONNECTED) {
  1971. start_bc_tones(p);
  1972. misdn_lib_send_event( p->bc, EVENT_DISCONNECT);
  1973. }
  1974. return -1;
  1975. case AST_CONTROL_RING:
  1976. chan_misdn_log(1, p->bc->port, "* IND :\tring pid:%d\n", p->bc ? p->bc->pid : -1);
  1977. return -1;
  1978. case AST_CONTROL_RINGING:
  1979. chan_misdn_log(1, p->bc->port, "* IND :\tringing pid:%d\n", p->bc ? p->bc->pid : -1);
  1980. switch (p->state) {
  1981. case MISDN_ALERTING:
  1982. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but I was Ringing before, so ignoring it\n", p->bc ? p->bc->pid : -1);
  1983. break;
  1984. case MISDN_CONNECTED:
  1985. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but Connected, so just send TONE_ALERTING without state changes \n", p->bc ? p->bc->pid : -1);
  1986. return -1;
  1987. default:
  1988. p->state = MISDN_ALERTING;
  1989. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d\n", p->bc ? p->bc->pid : -1);
  1990. misdn_lib_send_event( p->bc, EVENT_ALERTING);
  1991. if (p->other_ch && p->other_ch->bc) {
  1992. if (misdn_inband_avail(p->other_ch->bc)) {
  1993. chan_misdn_log(2, p->bc->port, " --> other End is mISDN and has inband info available\n");
  1994. break;
  1995. }
  1996. if (!p->other_ch->bc->nt) {
  1997. chan_misdn_log(2, p->bc->port, " --> other End is mISDN TE so it has inband info for sure (?)\n");
  1998. break;
  1999. }
  2000. }
  2001. chan_misdn_log(3, p->bc->port, " --> * SEND: State Ring pid:%d\n", p->bc ? p->bc->pid : -1);
  2002. ast_setstate(ast, AST_STATE_RING);
  2003. if (!p->bc->nt && (p->originator == ORG_MISDN) && !p->incoming_early_audio)
  2004. chan_misdn_log(2, p->bc->port, " --> incoming_early_audio off\n");
  2005. else
  2006. return -1;
  2007. }
  2008. break;
  2009. case AST_CONTROL_ANSWER:
  2010. chan_misdn_log(1, p->bc->port, " --> * IND :\tanswer pid:%d\n", p->bc ? p->bc->pid : -1);
  2011. start_bc_tones(p);
  2012. break;
  2013. case AST_CONTROL_TAKEOFFHOOK:
  2014. chan_misdn_log(1, p->bc->port, " --> *\ttakeoffhook pid:%d\n", p->bc ? p->bc->pid : -1);
  2015. return -1;
  2016. case AST_CONTROL_OFFHOOK:
  2017. chan_misdn_log(1, p->bc->port, " --> *\toffhook pid:%d\n", p->bc ? p->bc->pid : -1);
  2018. return -1;
  2019. case AST_CONTROL_FLASH:
  2020. chan_misdn_log(1, p->bc->port, " --> *\tflash pid:%d\n", p->bc ? p->bc->pid : -1);
  2021. break;
  2022. case AST_CONTROL_PROGRESS:
  2023. chan_misdn_log(1, p->bc->port, " --> * IND :\tprogress pid:%d\n", p->bc ? p->bc->pid : -1);
  2024. misdn_lib_send_event( p->bc, EVENT_PROGRESS);
  2025. break;
  2026. case AST_CONTROL_PROCEEDING:
  2027. chan_misdn_log(1, p->bc->port, " --> * IND :\tproceeding pid:%d\n", p->bc ? p->bc->pid : -1);
  2028. misdn_lib_send_event( p->bc, EVENT_PROCEEDING);
  2029. break;
  2030. case AST_CONTROL_CONGESTION:
  2031. chan_misdn_log(1, p->bc->port, " --> * IND :\tcongestion pid:%d\n", p->bc ? p->bc->pid : -1);
  2032. p->bc->out_cause = AST_CAUSE_SWITCH_CONGESTION;
  2033. start_bc_tones(p);
  2034. misdn_lib_send_event( p->bc, EVENT_DISCONNECT);
  2035. if (p->bc->nt) {
  2036. hanguptone_indicate(p);
  2037. }
  2038. break;
  2039. case -1 :
  2040. chan_misdn_log(1, p->bc->port, " --> * IND :\t-1! (stop indication) pid:%d\n", p->bc ? p->bc->pid : -1);
  2041. stop_indicate(p);
  2042. if (p->state == MISDN_CONNECTED)
  2043. start_bc_tones(p);
  2044. break;
  2045. case AST_CONTROL_HOLD:
  2046. ast_moh_start(ast, data, p->mohinterpret);
  2047. chan_misdn_log(1, p->bc->port, " --> *\tHOLD pid:%d\n", p->bc ? p->bc->pid : -1);
  2048. break;
  2049. case AST_CONTROL_UNHOLD:
  2050. ast_moh_stop(ast);
  2051. chan_misdn_log(1, p->bc->port, " --> *\tUNHOLD pid:%d\n", p->bc ? p->bc->pid : -1);
  2052. break;
  2053. default:
  2054. chan_misdn_log(1, p->bc->port, " --> * Unknown Indication:%d pid:%d\n", cond, p->bc ? p->bc->pid : -1);
  2055. return -1;
  2056. }
  2057. return 0;
  2058. }
  2059. static int misdn_hangup(struct ast_channel *ast)
  2060. {
  2061. struct chan_list *p;
  2062. struct misdn_bchannel *bc;
  2063. const char *var;
  2064. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  2065. return -1;
  2066. }
  2067. MISDN_ASTERISK_TECH_PVT(ast) = NULL;
  2068. ast_log(LOG_DEBUG, "misdn_hangup(%s)\n", ast->name);
  2069. if (p->hold.state == MISDN_HOLD_IDLE) {
  2070. bc = p->bc;
  2071. } else {
  2072. p->hold.state = MISDN_HOLD_DISCONNECT;
  2073. bc = misdn_lib_find_held_bc(p->hold.port, p->l3id);
  2074. if (!bc) {
  2075. chan_misdn_log(4, p->hold.port,
  2076. "misdn_hangup: Could not find held bc for (%s)\n", ast->name);
  2077. release_chan_early(p);
  2078. return 0;
  2079. }
  2080. }
  2081. if (ast->_state == AST_STATE_RESERVED || p->state == MISDN_NOTHING) {
  2082. /* between request and call */
  2083. ast_log(LOG_DEBUG, "State Reserved (or nothing) => chanIsAvail\n");
  2084. release_chan_early(p);
  2085. if (bc) {
  2086. misdn_lib_release(bc);
  2087. }
  2088. return 0;
  2089. }
  2090. if (!bc) {
  2091. ast_log(LOG_WARNING, "Hangup with private but no bc ? state:%s l3id:%x\n",
  2092. misdn_get_ch_state(p), p->l3id);
  2093. release_chan_early(p);
  2094. return 0;
  2095. }
  2096. p->ast = NULL;
  2097. p->need_hangup = 0;
  2098. p->need_queue_hangup = 0;
  2099. p->need_busy = 0;
  2100. if (!bc->nt) {
  2101. stop_bc_tones(p);
  2102. }
  2103. bc->out_cause = ast->hangupcause ? ast->hangupcause : AST_CAUSE_NORMAL_CLEARING;
  2104. var = pbx_builtin_getvar_helper(ast, "HANGUPCAUSE");
  2105. if (!var) {
  2106. var = pbx_builtin_getvar_helper(ast, "PRI_CAUSE");
  2107. }
  2108. if (var) {
  2109. int tmpcause;
  2110. tmpcause = atoi(var);
  2111. bc->out_cause = tmpcause ? tmpcause : AST_CAUSE_NORMAL_CLEARING;
  2112. }
  2113. var = pbx_builtin_getvar_helper(ast, "MISDN_USERUSER");
  2114. if (var) {
  2115. ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", var);
  2116. ast_copy_string(bc->uu, var, sizeof(bc->uu));
  2117. bc->uulen = strlen(bc->uu);
  2118. }
  2119. chan_misdn_log(1, bc->port,
  2120. "* IND : HANGUP\tpid:%d ctx:%s dad:%s oad:%s State:%s\n",
  2121. bc->pid,
  2122. ast->context,
  2123. ast->exten,
  2124. ast->cid.cid_num,
  2125. misdn_get_ch_state(p));
  2126. chan_misdn_log(3, bc->port, " --> l3id:%x\n", p->l3id);
  2127. chan_misdn_log(3, bc->port, " --> cause:%d\n", bc->cause);
  2128. chan_misdn_log(2, bc->port, " --> out_cause:%d\n", bc->out_cause);
  2129. switch (p->state) {
  2130. case MISDN_INCOMING_SETUP:
  2131. /*
  2132. * This is the only place in misdn_hangup, where we
  2133. * can call release_chan, else it might create a lot of trouble.
  2134. */
  2135. ast_log(LOG_NOTICE, "release channel, in INCOMING_SETUP state.. no other events happened\n");
  2136. release_chan(p, bc);
  2137. if (bc->need_release_complete) {
  2138. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  2139. }
  2140. return 0;
  2141. case MISDN_DIALING:
  2142. if (p->hold.state == MISDN_HOLD_IDLE) {
  2143. start_bc_tones(p);
  2144. hanguptone_indicate(p);
  2145. }
  2146. if (bc->need_disconnect) {
  2147. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  2148. }
  2149. break;
  2150. case MISDN_CALLING_ACKNOWLEDGE:
  2151. if (p->hold.state == MISDN_HOLD_IDLE) {
  2152. start_bc_tones(p);
  2153. hanguptone_indicate(p);
  2154. }
  2155. if (bc->need_disconnect) {
  2156. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  2157. }
  2158. break;
  2159. case MISDN_CALLING:
  2160. case MISDN_ALERTING:
  2161. case MISDN_PROGRESS:
  2162. case MISDN_PROCEEDING:
  2163. if (p->originator != ORG_AST && p->hold.state == MISDN_HOLD_IDLE) {
  2164. hanguptone_indicate(p);
  2165. }
  2166. if (bc->need_disconnect) {
  2167. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  2168. }
  2169. break;
  2170. case MISDN_CONNECTED:
  2171. /* Alerting or Disconnect */
  2172. if (bc->nt && p->hold.state == MISDN_HOLD_IDLE) {
  2173. start_bc_tones(p);
  2174. hanguptone_indicate(p);
  2175. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  2176. }
  2177. if (bc->need_disconnect) {
  2178. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  2179. }
  2180. break;
  2181. case MISDN_DISCONNECTED:
  2182. if (bc->need_release) {
  2183. misdn_lib_send_event(bc, EVENT_RELEASE);
  2184. }
  2185. break;
  2186. case MISDN_CLEANING:
  2187. return 0;
  2188. case MISDN_BUSY:
  2189. break;
  2190. default:
  2191. if (bc->nt) {
  2192. bc->out_cause = -1;
  2193. if (bc->need_release) {
  2194. misdn_lib_send_event(bc, EVENT_RELEASE);
  2195. }
  2196. } else {
  2197. if (bc->need_disconnect) {
  2198. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  2199. }
  2200. }
  2201. break;
  2202. }
  2203. p->state = MISDN_CLEANING;
  2204. chan_misdn_log(3, bc->port, " --> Channel: %s hungup new state:%s\n", ast->name,
  2205. misdn_get_ch_state(p));
  2206. return 0;
  2207. }
  2208. static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame)
  2209. {
  2210. struct ast_frame *f;
  2211. if (tmp->dsp) {
  2212. f = ast_dsp_process(tmp->ast, tmp->dsp, frame);
  2213. } else {
  2214. chan_misdn_log(0, tmp->bc->port, "No DSP-Path found\n");
  2215. return NULL;
  2216. }
  2217. if (!f || (f->frametype != AST_FRAME_DTMF)) {
  2218. return f;
  2219. }
  2220. ast_log(LOG_DEBUG, "Detected inband DTMF digit: %c\n", f->subclass);
  2221. if (tmp->faxdetect && (f->subclass == 'f')) {
  2222. /* Fax tone -- Handle and return NULL */
  2223. if (!tmp->faxhandled) {
  2224. struct ast_channel *ast = tmp->ast;
  2225. tmp->faxhandled++;
  2226. chan_misdn_log(0, tmp->bc->port, "Fax detected, preparing %s for fax transfer.\n", ast->name);
  2227. tmp->bc->rxgain = 0;
  2228. isdn_lib_update_rxgain(tmp->bc);
  2229. tmp->bc->txgain = 0;
  2230. isdn_lib_update_txgain(tmp->bc);
  2231. #ifdef MISDN_1_2
  2232. *tmp->bc->pipeline = 0;
  2233. #else
  2234. tmp->bc->ec_enable = 0;
  2235. #endif
  2236. isdn_lib_update_ec(tmp->bc);
  2237. isdn_lib_stop_dtmf(tmp->bc);
  2238. switch (tmp->faxdetect) {
  2239. case 1:
  2240. if (strcmp(ast->exten, "fax")) {
  2241. char *context;
  2242. char context_tmp[BUFFERSIZE];
  2243. misdn_cfg_get(tmp->bc->port, MISDN_CFG_FAXDETECT_CONTEXT, &context_tmp, sizeof(context_tmp));
  2244. context = ast_strlen_zero(context_tmp) ? (ast_strlen_zero(ast->macrocontext) ? ast->context : ast->macrocontext) : context_tmp;
  2245. if (ast_exists_extension(ast, context, "fax", 1, ast->cid.cid_num)) {
  2246. if (option_verbose > 2)
  2247. ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension (context:%s)\n", ast->name, context);
  2248. /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
  2249. pbx_builtin_setvar_helper(ast,"FAXEXTEN",ast->exten);
  2250. if (ast_async_goto(ast, context, "fax", 1))
  2251. ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, context);
  2252. } else
  2253. ast_log(LOG_NOTICE, "Fax detected, but no fax extension ctx:%s exten:%s\n", context, ast->exten);
  2254. } else {
  2255. ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
  2256. }
  2257. break;
  2258. case 2:
  2259. ast_verbose(VERBOSE_PREFIX_3 "Not redirecting %s to fax extension, nojump is set.\n", ast->name);
  2260. break;
  2261. }
  2262. } else {
  2263. ast_log(LOG_DEBUG, "Fax already handled\n");
  2264. }
  2265. }
  2266. if (tmp->ast_dsp && (f->subclass != 'f')) {
  2267. chan_misdn_log(2, tmp->bc->port, " --> * SEND: DTMF (AST_DSP) :%c\n", f->subclass);
  2268. }
  2269. return f;
  2270. }
  2271. static struct ast_frame *misdn_read(struct ast_channel *ast)
  2272. {
  2273. struct chan_list *tmp;
  2274. int len, t;
  2275. struct pollfd pfd = { .fd = -1, .events = POLLIN };
  2276. if (!ast) {
  2277. chan_misdn_log(1, 0, "misdn_read called without ast\n");
  2278. return NULL;
  2279. }
  2280. if (!(tmp = MISDN_ASTERISK_TECH_PVT(ast))) {
  2281. chan_misdn_log(1, 0, "misdn_read called without ast->pvt\n");
  2282. return NULL;
  2283. }
  2284. if (!tmp->bc && tmp->hold.state == MISDN_HOLD_IDLE) {
  2285. chan_misdn_log(1, 0, "misdn_read called without bc\n");
  2286. return NULL;
  2287. }
  2288. pfd.fd = tmp->pipe[0];
  2289. t = ast_poll(&pfd, 1, 20);
  2290. if (t < 0) {
  2291. chan_misdn_log(-1, tmp->bc->port, "poll() error (err=%s)\n", strerror(errno));
  2292. return NULL;
  2293. }
  2294. if (!t) {
  2295. chan_misdn_log(3, tmp->bc->port, "poll() timed out\n");
  2296. len = 160;
  2297. } else if (pfd.revents & POLLIN) {
  2298. len = read(tmp->pipe[0], tmp->ast_rd_buf, sizeof(tmp->ast_rd_buf));
  2299. if (len <= 0) {
  2300. /* we hangup here, since our pipe is closed */
  2301. chan_misdn_log(2, tmp->bc->port, "misdn_read: Pipe closed, hanging up\n");
  2302. return NULL;
  2303. }
  2304. } else {
  2305. return NULL;
  2306. }
  2307. tmp->frame.frametype = AST_FRAME_VOICE;
  2308. tmp->frame.subclass = AST_FORMAT_ALAW;
  2309. tmp->frame.datalen = len;
  2310. tmp->frame.samples = len;
  2311. tmp->frame.mallocd = 0;
  2312. tmp->frame.offset = 0;
  2313. tmp->frame.delivery = ast_tv(0,0);
  2314. tmp->frame.src = NULL;
  2315. tmp->frame.data = tmp->ast_rd_buf;
  2316. if (tmp->faxdetect && !tmp->faxhandled) {
  2317. if (tmp->faxdetect_timeout) {
  2318. if (ast_tvzero(tmp->faxdetect_tv)) {
  2319. tmp->faxdetect_tv = ast_tvnow();
  2320. chan_misdn_log(2, tmp->bc->port, "faxdetect: starting detection with timeout: %ds ...\n", tmp->faxdetect_timeout);
  2321. return process_ast_dsp(tmp, &tmp->frame);
  2322. } else {
  2323. struct timeval tv_now = ast_tvnow();
  2324. int diff = ast_tvdiff_ms(tv_now, tmp->faxdetect_tv);
  2325. if (diff <= (tmp->faxdetect_timeout * 1000)) {
  2326. chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ...\n");
  2327. return process_ast_dsp(tmp, &tmp->frame);
  2328. } else {
  2329. chan_misdn_log(2, tmp->bc->port, "faxdetect: stopping detection (time ran out) ...\n");
  2330. tmp->faxdetect = 0;
  2331. return &tmp->frame;
  2332. }
  2333. }
  2334. } else {
  2335. chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ... (no timeout)\n");
  2336. return process_ast_dsp(tmp, &tmp->frame);
  2337. }
  2338. } else {
  2339. if (tmp->ast_dsp)
  2340. return process_ast_dsp(tmp, &tmp->frame);
  2341. else
  2342. return &tmp->frame;
  2343. }
  2344. }
  2345. static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
  2346. {
  2347. struct chan_list *ch;
  2348. int i = 0;
  2349. if (!ast || ! (ch = MISDN_ASTERISK_TECH_PVT(ast)) ) return -1;
  2350. if (ch->hold.state != MISDN_HOLD_IDLE) {
  2351. chan_misdn_log(7, 0, "misdn_write: Returning because hold active\n");
  2352. return 0;
  2353. }
  2354. if (!ch->bc ) {
  2355. ast_log(LOG_WARNING, "private but no bc\n");
  2356. return -1;
  2357. }
  2358. if (ch->notxtone) {
  2359. chan_misdn_log(7, ch->bc->port, "misdn_write: Returning because notxtone\n");
  2360. return 0;
  2361. }
  2362. if (!frame->subclass) {
  2363. chan_misdn_log(4, ch->bc->port, "misdn_write: * prods us\n");
  2364. return 0;
  2365. }
  2366. if (!(frame->subclass & prefformat)) {
  2367. chan_misdn_log(-1, ch->bc->port, "Got Unsupported Frame with Format:%d\n", frame->subclass);
  2368. return 0;
  2369. }
  2370. if (!frame->samples ) {
  2371. chan_misdn_log(4, ch->bc->port, "misdn_write: zero write\n");
  2372. if (!strcmp(frame->src,"ast_prod")) {
  2373. chan_misdn_log(1, ch->bc->port, "misdn_write: state (%s) prodded.\n", misdn_get_ch_state(ch));
  2374. if (ch->ts) {
  2375. chan_misdn_log(4, ch->bc->port, "Starting Playtones\n");
  2376. misdn_lib_tone_generator_start(ch->bc);
  2377. }
  2378. return 0;
  2379. }
  2380. return -1;
  2381. }
  2382. if ( ! ch->bc->addr ) {
  2383. chan_misdn_log(8, ch->bc->port, "misdn_write: no addr for bc dropping:%d\n", frame->samples);
  2384. return 0;
  2385. }
  2386. #ifdef MISDN_DEBUG
  2387. {
  2388. int i, max = 5 > frame->samples ? frame->samples : 5;
  2389. printf("write2mISDN %p %d bytes: ", p, frame->samples);
  2390. for (i = 0; i < max; i++)
  2391. printf("%2.2x ", ((char*) frame->data)[i]);
  2392. printf ("\n");
  2393. }
  2394. #endif
  2395. switch (ch->bc->bc_state) {
  2396. case BCHAN_ACTIVATED:
  2397. case BCHAN_BRIDGED:
  2398. break;
  2399. default:
  2400. if (!ch->dropped_frame_cnt)
  2401. chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) dropping: %d frames addr:%x exten:%s cid:%s ch->state:%s bc_state:%d l3id:%x\n", frame->samples, ch->bc->addr, ast->exten, ast->cid.cid_num, misdn_get_ch_state( ch), ch->bc->bc_state, ch->bc->l3_id);
  2402. ch->dropped_frame_cnt++;
  2403. if (ch->dropped_frame_cnt > 100) {
  2404. ch->dropped_frame_cnt = 0;
  2405. chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) dropping: %d frames addr:%x dropped > 100 frames!\n", frame->samples, ch->bc->addr);
  2406. }
  2407. return 0;
  2408. }
  2409. chan_misdn_log(9, ch->bc->port, "Sending :%d bytes to MISDN\n", frame->samples);
  2410. if ( !ch->bc->nojitter && misdn_cap_is_speech(ch->bc->capability) ) {
  2411. /* Buffered Transmit (triggered by read from isdn side)*/
  2412. if (misdn_jb_fill(ch->jb, frame->data, frame->samples) < 0) {
  2413. if (ch->bc->active)
  2414. cb_log(0, ch->bc->port, "Misdn Jitterbuffer Overflow.\n");
  2415. }
  2416. } else {
  2417. /*transmit without jitterbuffer*/
  2418. i = misdn_lib_tx2misdn_frm(ch->bc, frame->data, frame->samples);
  2419. }
  2420. return 0;
  2421. }
  2422. static enum ast_bridge_result misdn_bridge (struct ast_channel *c0,
  2423. struct ast_channel *c1, int flags,
  2424. struct ast_frame **fo,
  2425. struct ast_channel **rc,
  2426. int timeoutms)
  2427. {
  2428. struct chan_list *ch1, *ch2;
  2429. struct ast_channel *carr[2], *who;
  2430. int to = -1;
  2431. struct ast_frame *f;
  2432. int p1_b, p2_b;
  2433. int bridging;
  2434. ch1 = get_chan_by_ast(c0);
  2435. ch2 = get_chan_by_ast(c1);
  2436. carr[0] = c0;
  2437. carr[1] = c1;
  2438. if (!(ch1 && ch2))
  2439. return -1;
  2440. misdn_cfg_get(ch1->bc->port, MISDN_CFG_BRIDGING, &p1_b, sizeof(int));
  2441. misdn_cfg_get(ch2->bc->port, MISDN_CFG_BRIDGING, &p2_b, sizeof(int));
  2442. if (! p1_b || ! p2_b) {
  2443. ast_log(LOG_NOTICE, "Falling back to Asterisk bridging\n");
  2444. return AST_BRIDGE_FAILED;
  2445. }
  2446. misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(int));
  2447. if (bridging) {
  2448. /* trying to make a mISDN_dsp conference */
  2449. chan_misdn_log(1, ch1->bc->port, "I SEND: Making conference with Number:%d\n", ch1->bc->pid + 1);
  2450. misdn_lib_bridge(ch1->bc, ch2->bc);
  2451. }
  2452. if (option_verbose > 2)
  2453. ast_verbose(VERBOSE_PREFIX_3 "Native bridging %s and %s\n", c0->name, c1->name);
  2454. chan_misdn_log(1, ch1->bc->port, "* Making Native Bridge between %s and %s\n", ch1->bc->oad, ch2->bc->oad);
  2455. if (! (flags & AST_BRIDGE_DTMF_CHANNEL_0) )
  2456. ch1->ignore_dtmf = 1;
  2457. if (! (flags & AST_BRIDGE_DTMF_CHANNEL_1) )
  2458. ch2->ignore_dtmf = 1;
  2459. for (;/*ever*/;) {
  2460. to = -1;
  2461. who = ast_waitfor_n(carr, 2, &to);
  2462. if (!who) {
  2463. ast_log(LOG_NOTICE, "misdn_bridge: empty read, breaking out\n");
  2464. break;
  2465. }
  2466. f = ast_read(who);
  2467. if (!f || f->frametype == AST_FRAME_CONTROL) {
  2468. /* got hangup .. */
  2469. if (!f)
  2470. chan_misdn_log(4, ch1->bc->port, "Read Null Frame\n");
  2471. else
  2472. chan_misdn_log(4, ch1->bc->port, "Read Frame Control class:%d\n", f->subclass);
  2473. *fo = f;
  2474. *rc = who;
  2475. break;
  2476. }
  2477. if ( f->frametype == AST_FRAME_DTMF ) {
  2478. chan_misdn_log(1, 0, "Read DTMF %d from %s\n", f->subclass, who->exten);
  2479. *fo = f;
  2480. *rc = who;
  2481. break;
  2482. }
  2483. #if 0
  2484. if (f->frametype == AST_FRAME_VOICE) {
  2485. chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid +1);
  2486. continue;
  2487. }
  2488. #endif
  2489. if (who == c0) {
  2490. ast_write(c1, f);
  2491. }
  2492. else {
  2493. ast_write(c0, f);
  2494. }
  2495. }
  2496. chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid + 1);
  2497. misdn_lib_split_bridge(ch1->bc, ch2->bc);
  2498. return AST_BRIDGE_COMPLETE;
  2499. }
  2500. /** AST INDICATIONS END **/
  2501. static int dialtone_indicate(struct chan_list *cl)
  2502. {
  2503. const struct tone_zone_sound *ts = NULL;
  2504. struct ast_channel *ast = cl->ast;
  2505. int nd = 0;
  2506. if (!ast) {
  2507. chan_misdn_log(0, cl->bc->port, "No Ast in dialtone_indicate\n");
  2508. return -1;
  2509. }
  2510. misdn_cfg_get(cl->bc->port, MISDN_CFG_NODIALTONE, &nd, sizeof(nd));
  2511. if (nd) {
  2512. chan_misdn_log(1, cl->bc->port, "Not sending Dialtone, because config wants it\n");
  2513. return 0;
  2514. }
  2515. chan_misdn_log(3, cl->bc->port, " --> Dial\n");
  2516. ts = ast_get_indication_tone(ast->zone, "dial");
  2517. cl->ts = ts;
  2518. if (ts) {
  2519. cl->notxtone = 0;
  2520. cl->norxtone = 0;
  2521. /* This prods us in misdn_write */
  2522. ast_playtones_start(ast, 0, ts->data, 0);
  2523. }
  2524. return 0;
  2525. }
  2526. static void hanguptone_indicate(struct chan_list *cl)
  2527. {
  2528. misdn_lib_send_tone(cl->bc, TONE_HANGUP);
  2529. }
  2530. static int stop_indicate(struct chan_list *cl)
  2531. {
  2532. struct ast_channel *ast = cl->ast;
  2533. if (!ast) {
  2534. chan_misdn_log(0, cl->bc->port, "No Ast in stop_indicate\n");
  2535. return -1;
  2536. }
  2537. chan_misdn_log(3, cl->bc->port, " --> None\n");
  2538. misdn_lib_tone_generator_stop(cl->bc);
  2539. ast_playtones_stop(ast);
  2540. cl->ts = NULL;
  2541. /*ast_deactivate_generator(ast);*/
  2542. return 0;
  2543. }
  2544. static int start_bc_tones(struct chan_list* cl)
  2545. {
  2546. misdn_lib_tone_generator_stop(cl->bc);
  2547. cl->notxtone = 0;
  2548. cl->norxtone = 0;
  2549. return 0;
  2550. }
  2551. static int stop_bc_tones(struct chan_list *cl)
  2552. {
  2553. if (!cl) return -1;
  2554. cl->notxtone = 1;
  2555. cl->norxtone = 1;
  2556. return 0;
  2557. }
  2558. static struct chan_list *init_chan_list(int orig)
  2559. {
  2560. struct chan_list *cl;
  2561. cl = calloc(1, sizeof(struct chan_list));
  2562. if (!cl) {
  2563. chan_misdn_log(-1, 0, "misdn_request: malloc failed!");
  2564. return NULL;
  2565. }
  2566. cl->originator = orig;
  2567. cl->need_queue_hangup = 1;
  2568. cl->need_hangup = 1;
  2569. cl->need_busy = 1;
  2570. cl->overlap_dial_task = -1;
  2571. return cl;
  2572. }
  2573. static struct ast_channel *misdn_request(const char *type, int format, void *data, int *cause)
  2574. {
  2575. struct ast_channel *tmp = NULL;
  2576. char group[BUFFERSIZE + 1] = "";
  2577. char dial_str[128];
  2578. char *buf2 = ast_strdupa(data);
  2579. char *ext;
  2580. char *port_str;
  2581. char *p = NULL;
  2582. int channel = 0;
  2583. int port = 0;
  2584. struct misdn_bchannel *newbc = NULL;
  2585. int dec = 0;
  2586. struct chan_list *cl;
  2587. snprintf(dial_str, sizeof(dial_str), "%s/%s", misdn_type, (char *) data);
  2588. /*
  2589. * data is ---v
  2590. * Dial(mISDN/g:group_name[/extension[/options]])
  2591. * Dial(mISDN/port[:preselected_channel][/extension[/options]])
  2592. *
  2593. * The dial extension could be empty if you are using MISDN_KEYPAD
  2594. * to control ISDN provider features.
  2595. */
  2596. port_str = strsep(&buf2, "/");
  2597. if (!ast_strlen_zero(port_str)) {
  2598. if (port_str[0] == 'g' && port_str[1] == ':' ) {
  2599. /* We make a group call lets checkout which ports are in my group */
  2600. port_str += 2;
  2601. ast_copy_string(group, port_str, sizeof(group));
  2602. chan_misdn_log(2, 0, " --> Group Call group: %s\n", group);
  2603. } else if ((p = strchr(port_str, ':'))) {
  2604. /* we have a preselected channel */
  2605. *p = 0;
  2606. channel = atoi(++p);
  2607. port = atoi(port_str);
  2608. chan_misdn_log(2, port, " --> Call on preselected Channel (%d).\n", channel);
  2609. } else {
  2610. port = atoi(port_str);
  2611. }
  2612. } else {
  2613. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) WITHOUT Port or Group, check extensions.conf\n", dial_str);
  2614. return NULL;
  2615. }
  2616. ext = strsep(&buf2, "/");
  2617. if (!ext) {
  2618. ext = "";
  2619. }
  2620. if (misdn_cfg_is_group_method(group, METHOD_STANDARD_DEC)) {
  2621. chan_misdn_log(4, port, " --> STARTING STANDARD DEC...\n");
  2622. dec = 1;
  2623. }
  2624. if (!ast_strlen_zero(group)) {
  2625. char cfg_group[BUFFERSIZE + 1];
  2626. struct robin_list *rr = NULL;
  2627. /* Group dial */
  2628. if (misdn_cfg_is_group_method(group, METHOD_ROUND_ROBIN)) {
  2629. chan_misdn_log(4, port, " --> STARTING ROUND ROBIN...\n");
  2630. rr = get_robin_position(group);
  2631. }
  2632. if (rr) {
  2633. int port_start = 0;
  2634. int port_bak = rr->port;
  2635. int chan_bak = rr->channel;
  2636. if (!rr->port)
  2637. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  2638. for (; rr->port > 0; rr->port = misdn_cfg_get_next_port_spin(rr->port)) {
  2639. int port_up;
  2640. int check;
  2641. int max_chan;
  2642. int last_chance = 0;
  2643. misdn_cfg_get(rr->port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE);
  2644. if (strcasecmp(cfg_group, group))
  2645. continue;
  2646. misdn_cfg_get(rr->port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(int));
  2647. port_up = misdn_lib_port_up(rr->port, check);
  2648. if (check && !port_up)
  2649. chan_misdn_log(1, rr->port, "L1 is not Up on this Port\n");
  2650. if (check && port_up < 0)
  2651. ast_log(LOG_WARNING,"This port (%d) is blocked\n", rr->port);
  2652. if ((port_start == rr->port) && (port_up <= 0))
  2653. break;
  2654. if (!port_start)
  2655. port_start = rr->port;
  2656. if (port_up <= 0)
  2657. continue;
  2658. max_chan = misdn_lib_get_maxchans(rr->port);
  2659. for (++rr->channel; !last_chance && rr->channel <= max_chan; ++rr->channel) {
  2660. if (rr->port == port_bak && rr->channel == chan_bak)
  2661. last_chance = 1;
  2662. chan_misdn_log(1, 0, "trying port:%d channel:%d\n", rr->port, rr->channel);
  2663. newbc = misdn_lib_get_free_bc(rr->port, rr->channel, 0, 0);
  2664. if (newbc) {
  2665. chan_misdn_log(4, rr->port, " Success! Found port:%d channel:%d\n", newbc->port, newbc->channel);
  2666. if (port_up)
  2667. chan_misdn_log(4, rr->port, "portup:%d\n", port_up);
  2668. port = rr->port;
  2669. break;
  2670. }
  2671. }
  2672. if (newbc || last_chance)
  2673. break;
  2674. rr->channel = 0;
  2675. }
  2676. if (!newbc) {
  2677. rr->port = port_bak;
  2678. rr->channel = chan_bak;
  2679. }
  2680. } else {
  2681. for (port = misdn_cfg_get_next_port(0); port > 0;
  2682. port = misdn_cfg_get_next_port(port)) {
  2683. misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE);
  2684. chan_misdn_log(3, port, "Group [%s] Port [%d]\n", group, port);
  2685. if (!strcasecmp(cfg_group, group)) {
  2686. int port_up;
  2687. int check;
  2688. misdn_cfg_get(port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(int));
  2689. port_up = misdn_lib_port_up(port, check);
  2690. chan_misdn_log(4, port, "portup:%d\n", port_up);
  2691. if (port_up > 0) {
  2692. newbc = misdn_lib_get_free_bc(port, 0, 0, dec);
  2693. if (newbc)
  2694. break;
  2695. }
  2696. }
  2697. }
  2698. }
  2699. /* Group dial failed ?*/
  2700. if (!newbc) {
  2701. ast_log(LOG_WARNING,
  2702. "Could not Dial out on group '%s'.\n"
  2703. "\tEither the L2 and L1 on all of these ports where DOWN (see 'show application misdn_check_l2l1')\n"
  2704. "\tOr there was no free channel on none of the ports\n\n"
  2705. , group);
  2706. return NULL;
  2707. }
  2708. } else {
  2709. /* 'Normal' Port dial * Port dial */
  2710. if (channel)
  2711. chan_misdn_log(1, port, " --> preselected_channel: %d\n", channel);
  2712. newbc = misdn_lib_get_free_bc(port, channel, 0, dec);
  2713. if (!newbc) {
  2714. ast_log(LOG_WARNING, "Could not create channel on port:%d with extensions:%s\n", port, ext);
  2715. return NULL;
  2716. }
  2717. }
  2718. /* create ast_channel and link all the objects together */
  2719. cl = init_chan_list(ORG_AST);
  2720. if (!cl) {
  2721. ast_log(LOG_ERROR, "Could not create call record for Dial(%s)\n", dial_str);
  2722. return NULL;
  2723. }
  2724. cl->bc = newbc;
  2725. tmp = misdn_new(cl, AST_STATE_RESERVED, ext, NULL, format, port, channel);
  2726. if (!tmp) {
  2727. free(cl);
  2728. ast_log(LOG_ERROR,"Could not create Asterisk object\n");
  2729. return NULL;
  2730. }
  2731. cl->ast=tmp;
  2732. /* register chan in local list */
  2733. cl_queue_chan(&cl_te, cl) ;
  2734. /* fill in the config into the objects */
  2735. read_config(cl, ORG_AST);
  2736. /* important */
  2737. cl->need_hangup = 0;
  2738. return tmp;
  2739. }
  2740. static int misdn_send_text(struct ast_channel *chan, const char *text)
  2741. {
  2742. struct chan_list *tmp = chan->tech_pvt;
  2743. if (tmp && tmp->bc) {
  2744. ast_copy_string(tmp->bc->display, text, sizeof(tmp->bc->display));
  2745. misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
  2746. } else {
  2747. ast_log(LOG_WARNING, "No chan_list but send_text request?\n");
  2748. return -1;
  2749. }
  2750. return 0;
  2751. }
  2752. static struct ast_channel_tech misdn_tech = {
  2753. .type = "mISDN",
  2754. .description = "Channel driver for mISDN Support (Bri/Pri)",
  2755. .capabilities = AST_FORMAT_ALAW ,
  2756. .requester = misdn_request,
  2757. .send_digit_begin = misdn_digit_begin,
  2758. .send_digit_end = misdn_digit_end,
  2759. .call = misdn_call,
  2760. .bridge = misdn_bridge,
  2761. .hangup = misdn_hangup,
  2762. .answer = misdn_answer,
  2763. .read = misdn_read,
  2764. .write = misdn_write,
  2765. .indicate = misdn_indication,
  2766. .fixup = misdn_fixup,
  2767. .send_text = misdn_send_text,
  2768. .properties = 0
  2769. };
  2770. static struct ast_channel_tech misdn_tech_wo_bridge = {
  2771. .type = "mISDN",
  2772. .description = "Channel driver for mISDN Support (Bri/Pri)",
  2773. .capabilities = AST_FORMAT_ALAW ,
  2774. .requester = misdn_request,
  2775. .send_digit_begin = misdn_digit_begin,
  2776. .send_digit_end = misdn_digit_end,
  2777. .call = misdn_call,
  2778. .hangup = misdn_hangup,
  2779. .answer = misdn_answer,
  2780. .read = misdn_read,
  2781. .write = misdn_write,
  2782. .indicate = misdn_indication,
  2783. .fixup = misdn_fixup,
  2784. .send_text = misdn_send_text,
  2785. .properties = 0
  2786. };
  2787. static int glob_channel = 0;
  2788. static void update_name(struct ast_channel *tmp, int port, int c)
  2789. {
  2790. int chan_offset = 0;
  2791. int tmp_port = misdn_cfg_get_next_port(0);
  2792. for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
  2793. if (tmp_port == port)
  2794. break;
  2795. chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
  2796. }
  2797. if (c < 0)
  2798. c = 0;
  2799. ast_string_field_build(tmp, name, "%s/%d-u%d",
  2800. misdn_type, chan_offset + c, glob_channel++);
  2801. chan_misdn_log(3, port, " --> updating channel name to [%s]\n", tmp->name);
  2802. }
  2803. static struct ast_channel *misdn_new(struct chan_list *chlist, int state, char *exten, char *callerid, int format, int port, int c)
  2804. {
  2805. struct ast_channel *tmp;
  2806. char *cid_name = 0, *cid_num = 0;
  2807. int chan_offset = 0;
  2808. int tmp_port = misdn_cfg_get_next_port(0);
  2809. int bridging;
  2810. for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
  2811. if (tmp_port == port)
  2812. break;
  2813. chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
  2814. }
  2815. if (c < 0)
  2816. c = 0;
  2817. if (callerid) {
  2818. ast_callerid_parse(callerid, &cid_name, &cid_num);
  2819. }
  2820. tmp = ast_channel_alloc(1, state, cid_num, cid_name, "", exten, "", 0, "%s/%d-u%d", misdn_type, chan_offset + c, glob_channel++);
  2821. if (tmp) {
  2822. chan_misdn_log(2, 0, " --> * NEW CHANNEL dad:%s oad:%s\n", exten, callerid);
  2823. tmp->nativeformats = prefformat;
  2824. tmp->readformat = format;
  2825. tmp->rawreadformat = format;
  2826. tmp->writeformat = format;
  2827. tmp->rawwriteformat = format;
  2828. tmp->tech_pvt = chlist;
  2829. misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(int));
  2830. if (bridging)
  2831. tmp->tech = &misdn_tech;
  2832. else
  2833. tmp->tech = &misdn_tech_wo_bridge;
  2834. tmp->writeformat = format;
  2835. tmp->readformat = format;
  2836. tmp->priority=1;
  2837. if (exten)
  2838. ast_copy_string(tmp->exten, exten, sizeof(tmp->exten));
  2839. else
  2840. chan_misdn_log(1, 0, "misdn_new: no exten given.\n");
  2841. if (callerid)
  2842. /* Don't use ast_set_callerid() here because it will
  2843. * generate a needless NewCallerID event */
  2844. tmp->cid.cid_ani = ast_strdup(cid_num);
  2845. if (pipe(chlist->pipe) < 0)
  2846. perror("Pipe failed\n");
  2847. tmp->fds[0] = chlist->pipe[0];
  2848. if (state == AST_STATE_RING)
  2849. tmp->rings = 1;
  2850. else
  2851. tmp->rings = 0;
  2852. } else {
  2853. chan_misdn_log(-1, 0, "Unable to allocate channel structure\n");
  2854. }
  2855. return tmp;
  2856. }
  2857. static struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc)
  2858. {
  2859. struct chan_list *help = list;
  2860. for (; help; help = help->next) {
  2861. if (help->bc == bc) return help;
  2862. }
  2863. chan_misdn_log(6, bc->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n", bc->oad, bc->dad);
  2864. return NULL;
  2865. }
  2866. static struct chan_list *find_chan_by_pid(struct chan_list *list, int pid)
  2867. {
  2868. struct chan_list *help = list;
  2869. for (; help; help = help->next) {
  2870. if ( help->bc && (help->bc->pid == pid) ) return help;
  2871. }
  2872. chan_misdn_log(6, 0, "$$$ find_chan: No channel found for pid:%d\n", pid);
  2873. return NULL;
  2874. }
  2875. static struct chan_list *find_hold_call(struct chan_list *list, struct misdn_bchannel *bc)
  2876. {
  2877. struct chan_list *help = list;
  2878. if (bc->pri) return NULL;
  2879. chan_misdn_log(6, bc->port, "$$$ find_hold_call: channel:%d oad:%s dad:%s\n", bc->channel, bc->oad, bc->dad);
  2880. for (;help; help = help->next) {
  2881. chan_misdn_log(4, bc->port, "$$$ find_hold_call: --> hold:%d channel:%d\n", help->hold.state, help->hold.channel);
  2882. if (help->hold.state == MISDN_HOLD_ACTIVE && help->hold.port == bc->port)
  2883. return help;
  2884. }
  2885. chan_misdn_log(6, bc->port, "$$$ find_hold_call: No channel found for oad:%s dad:%s\n", bc->oad, bc->dad);
  2886. return NULL;
  2887. }
  2888. static struct chan_list *find_hold_call_l3(struct chan_list *list, unsigned long l3_id)
  2889. {
  2890. struct chan_list *help = list;
  2891. for (; help; help = help->next) {
  2892. if (help->hold.state != MISDN_HOLD_IDLE && help->l3id == l3_id)
  2893. return help;
  2894. }
  2895. return NULL;
  2896. }
  2897. #define TRANSFER_ON_HELD_CALL_HANGUP 1
  2898. #if defined(TRANSFER_ON_HELD_CALL_HANGUP)
  2899. /*!
  2900. * \internal
  2901. * \brief Find a suitable active call to go with a held call so we could try a transfer.
  2902. *
  2903. * \param list Channel list.
  2904. * \param bc B channel record.
  2905. *
  2906. * \return Found call record or NULL.
  2907. *
  2908. * \note There could be a possibility where we find the wrong active call to transfer.
  2909. * This concern is mitigated by the fact that there could be at most one other call
  2910. * on a PTMP BRI link to another device. Maybe the l3_id could help in locating an
  2911. * active call on the same TEI?
  2912. */
  2913. static struct chan_list *find_hold_active_call(struct chan_list *list, struct misdn_bchannel *bc)
  2914. {
  2915. for (; list; list = list->next) {
  2916. if (list->hold.state == MISDN_HOLD_IDLE && list->bc && list->bc->port == bc->port
  2917. && list->ast) {
  2918. switch (list->state) {
  2919. case MISDN_PROCEEDING:
  2920. case MISDN_PROGRESS:
  2921. case MISDN_ALERTING:
  2922. case MISDN_CONNECTED:
  2923. return list;
  2924. default:
  2925. break;
  2926. }
  2927. }
  2928. }
  2929. return NULL;
  2930. }
  2931. #endif /* defined(TRANSFER_ON_HELD_CALL_HANGUP) */
  2932. static void cl_queue_chan(struct chan_list **list, struct chan_list *chan)
  2933. {
  2934. chan_misdn_log(4, chan->bc ? chan->bc->port : 0, "* Queuing chan %p\n", chan);
  2935. ast_mutex_lock(&cl_te_lock);
  2936. if (!*list) {
  2937. *list = chan;
  2938. } else {
  2939. struct chan_list *help = *list;
  2940. for (; help->next; help = help->next);
  2941. help->next = chan;
  2942. }
  2943. chan->next = NULL;
  2944. ast_mutex_unlock(&cl_te_lock);
  2945. }
  2946. static void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan)
  2947. {
  2948. struct chan_list *help;
  2949. if (chan->dsp)
  2950. ast_dsp_free(chan->dsp);
  2951. ast_mutex_lock(&cl_te_lock);
  2952. if (!*list) {
  2953. ast_mutex_unlock(&cl_te_lock);
  2954. return;
  2955. }
  2956. if (*list == chan) {
  2957. *list = (*list)->next;
  2958. ast_mutex_unlock(&cl_te_lock);
  2959. return;
  2960. }
  2961. for (help = *list; help->next; help = help->next) {
  2962. if (help->next == chan) {
  2963. help->next = help->next->next;
  2964. ast_mutex_unlock(&cl_te_lock);
  2965. return;
  2966. }
  2967. }
  2968. ast_mutex_unlock(&cl_te_lock);
  2969. }
  2970. /** Channel Queue End **/
  2971. static int pbx_start_chan(struct chan_list *ch)
  2972. {
  2973. int ret = ast_pbx_start(ch->ast);
  2974. if (ret >= 0)
  2975. ch->need_hangup = 0;
  2976. else
  2977. ch->need_hangup = 1;
  2978. return ret;
  2979. }
  2980. static void hangup_chan(struct chan_list *ch, struct misdn_bchannel *bc)
  2981. {
  2982. int port;
  2983. if (!ch) {
  2984. cb_log(1, 0, "Cannot hangup chan, no ch\n");
  2985. return;
  2986. }
  2987. port = bc->port;
  2988. cb_log(5, port, "hangup_chan called\n");
  2989. if (ch->need_hangup) {
  2990. cb_log(2, port, " --> hangup\n");
  2991. ch->need_hangup = 0;
  2992. ch->need_queue_hangup = 0;
  2993. if (ch->ast) {
  2994. send_cause2ast(ch->ast, bc, ch);
  2995. ast_hangup(ch->ast);
  2996. }
  2997. return;
  2998. }
  2999. if (!ch->need_queue_hangup) {
  3000. cb_log(2, port, " --> No need to queue hangup\n");
  3001. }
  3002. ch->need_queue_hangup = 0;
  3003. if (ch->ast) {
  3004. send_cause2ast(ch->ast, bc, ch);
  3005. ast_queue_hangup(ch->ast);
  3006. cb_log(2, port, " --> queue_hangup\n");
  3007. } else {
  3008. cb_log(1, port, "Cannot hangup chan, no ast\n");
  3009. }
  3010. }
  3011. /*!
  3012. * \internal
  3013. * \brief ISDN asked us to release channel, pendant to misdn_hangup.
  3014. *
  3015. * \param ch Call channel record to release.
  3016. * \param bc Current B channel record associated with ch.
  3017. *
  3018. * \return Nothing
  3019. *
  3020. * \note ch must not be referenced after calling.
  3021. */
  3022. static void release_chan(struct chan_list *ch, struct misdn_bchannel *bc)
  3023. {
  3024. struct ast_channel *ast;
  3025. ch->state = MISDN_CLEANING;
  3026. ast_mutex_lock(&release_lock);
  3027. cl_dequeue_chan(&cl_te, ch);
  3028. chan_misdn_log(5, bc->port, "release_chan: bc with pid:%d l3id: %x\n", bc->pid, bc->l3_id);
  3029. /* releasing jitterbuffer */
  3030. if (ch->jb) {
  3031. misdn_jb_destroy(ch->jb);
  3032. ch->jb = NULL;
  3033. } else {
  3034. if (!bc->nojitter) {
  3035. chan_misdn_log(5, bc->port, "Jitterbuffer already destroyed.\n");
  3036. }
  3037. }
  3038. if (ch->overlap_dial) {
  3039. if (ch->overlap_dial_task != -1) {
  3040. misdn_tasks_remove(ch->overlap_dial_task);
  3041. ch->overlap_dial_task = -1;
  3042. }
  3043. ast_mutex_destroy(&ch->overlap_tv_lock);
  3044. }
  3045. if (ch->originator == ORG_AST) {
  3046. --misdn_out_calls[bc->port];
  3047. } else {
  3048. --misdn_in_calls[bc->port];
  3049. }
  3050. close(ch->pipe[0]);
  3051. close(ch->pipe[1]);
  3052. ast = ch->ast;
  3053. if (ast) {
  3054. MISDN_ASTERISK_TECH_PVT(ast) = NULL;
  3055. chan_misdn_log(1, bc->port,
  3056. "* RELEASING CHANNEL pid:%d ctx:%s dad:%s oad:%s\n",
  3057. bc->pid,
  3058. ast->context,
  3059. ast->exten,
  3060. ast->cid.cid_num);
  3061. if (ast->_state != AST_STATE_RESERVED) {
  3062. chan_misdn_log(3, bc->port, " --> Setting AST State to down\n");
  3063. ast_setstate(ast, AST_STATE_DOWN);
  3064. }
  3065. }
  3066. free(ch);
  3067. ast_mutex_unlock(&release_lock);
  3068. }
  3069. /*!
  3070. * \internal
  3071. * \brief Do everything in release_chan() that makes sense without a bc.
  3072. *
  3073. * \param ch Call channel record to release.
  3074. *
  3075. * \return Nothing
  3076. *
  3077. * \note ch must not be referenced after calling.
  3078. */
  3079. static void release_chan_early(struct chan_list *ch)
  3080. {
  3081. struct ast_channel *ast;
  3082. ch->state = MISDN_CLEANING;
  3083. ast_mutex_lock(&release_lock);
  3084. cl_dequeue_chan(&cl_te, ch);
  3085. /* releasing jitterbuffer */
  3086. if (ch->jb) {
  3087. misdn_jb_destroy(ch->jb);
  3088. ch->jb = NULL;
  3089. }
  3090. if (ch->overlap_dial) {
  3091. if (ch->overlap_dial_task != -1) {
  3092. misdn_tasks_remove(ch->overlap_dial_task);
  3093. ch->overlap_dial_task = -1;
  3094. }
  3095. ast_mutex_destroy(&ch->overlap_tv_lock);
  3096. }
  3097. if (ch->hold.state != MISDN_HOLD_IDLE) {
  3098. if (ch->originator == ORG_AST) {
  3099. --misdn_out_calls[ch->hold.port];
  3100. } else {
  3101. --misdn_in_calls[ch->hold.port];
  3102. }
  3103. }
  3104. close(ch->pipe[0]);
  3105. close(ch->pipe[1]);
  3106. ast = ch->ast;
  3107. if (ast) {
  3108. MISDN_ASTERISK_TECH_PVT(ast) = NULL;
  3109. if (ast->_state != AST_STATE_RESERVED) {
  3110. ast_setstate(ast, AST_STATE_DOWN);
  3111. }
  3112. }
  3113. free(ch);
  3114. ast_mutex_unlock(&release_lock);
  3115. }
  3116. /*!
  3117. * \internal
  3118. * \brief Attempt to transfer the active channel party to the held channel party.
  3119. *
  3120. * \param active_ch Channel currently connected.
  3121. * \param held_ch Channel currently on hold.
  3122. *
  3123. * \retval 0 on success.
  3124. * \retval -1 on error.
  3125. */
  3126. static int misdn_attempt_transfer(struct chan_list *active_ch, struct chan_list *held_ch)
  3127. {
  3128. int retval;
  3129. struct ast_channel *bridged;
  3130. switch (active_ch->state) {
  3131. case MISDN_PROCEEDING:
  3132. case MISDN_PROGRESS:
  3133. case MISDN_ALERTING:
  3134. case MISDN_CONNECTED:
  3135. break;
  3136. default:
  3137. return -1;
  3138. }
  3139. bridged = ast_bridged_channel(held_ch->ast);
  3140. if (bridged) {
  3141. ast_queue_control(held_ch->ast, AST_CONTROL_UNHOLD);
  3142. held_ch->hold.state = MISDN_HOLD_TRANSFER;
  3143. chan_misdn_log(1, held_ch->hold.port, "TRANSFERRING %s to %s\n",
  3144. held_ch->ast->name, active_ch->ast->name);
  3145. retval = ast_channel_masquerade(active_ch->ast, bridged);
  3146. } else {
  3147. /*
  3148. * Could not transfer. Held channel is not bridged anymore.
  3149. * Held party probably got tired of waiting and hung up.
  3150. */
  3151. retval = -1;
  3152. }
  3153. return retval;
  3154. }
  3155. static void do_immediate_setup(struct misdn_bchannel *bc, struct chan_list *ch, struct ast_channel *ast)
  3156. {
  3157. char predial[256]="";
  3158. char *p = predial;
  3159. struct ast_frame fr;
  3160. strncpy(predial, ast->exten, sizeof(predial) -1 );
  3161. ch->state = MISDN_DIALING;
  3162. if (!ch->noautorespond_on_setup) {
  3163. if (bc->nt) {
  3164. int ret;
  3165. ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
  3166. } else {
  3167. int ret;
  3168. if ( misdn_lib_is_ptp(bc->port)) {
  3169. ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
  3170. } else {
  3171. ret = misdn_lib_send_event(bc, EVENT_PROCEEDING );
  3172. }
  3173. }
  3174. } else {
  3175. ch->state = MISDN_INCOMING_SETUP;
  3176. }
  3177. chan_misdn_log(1, bc->port, "* Starting Ast ctx:%s dad:%s oad:%s with 's' extension\n", ast->context, ast->exten, ast->cid.cid_num);
  3178. strcpy(ast->exten, "s");
  3179. if (!ast_canmatch_extension(ast, ast->context, ast->exten, 1, bc->oad) || pbx_start_chan(ch) < 0) {
  3180. ast = NULL;
  3181. bc->out_cause = AST_CAUSE_UNALLOCATED;
  3182. hangup_chan(ch, bc);
  3183. hanguptone_indicate(ch);
  3184. if (bc->nt)
  3185. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
  3186. else
  3187. misdn_lib_send_event(bc, EVENT_DISCONNECT );
  3188. }
  3189. while (!ast_strlen_zero(p) ) {
  3190. fr.frametype = AST_FRAME_DTMF;
  3191. fr.subclass = *p;
  3192. fr.src = NULL;
  3193. fr.data = NULL;
  3194. fr.datalen = 0;
  3195. fr.samples = 0;
  3196. fr.mallocd = 0;
  3197. fr.offset = 0;
  3198. fr.delivery = ast_tv(0,0);
  3199. if (ch->ast && MISDN_ASTERISK_PVT(ch->ast) && MISDN_ASTERISK_TECH_PVT(ch->ast)) {
  3200. ast_queue_frame(ch->ast, &fr);
  3201. }
  3202. p++;
  3203. }
  3204. }
  3205. static void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, struct chan_list *ch) {
  3206. if (!ast) {
  3207. chan_misdn_log(1, 0, "send_cause2ast: No Ast\n");
  3208. return;
  3209. }
  3210. if (!bc) {
  3211. chan_misdn_log(1, 0, "send_cause2ast: No BC\n");
  3212. return;
  3213. }
  3214. if (!ch) {
  3215. chan_misdn_log(1, 0, "send_cause2ast: No Ch\n");
  3216. return;
  3217. }
  3218. ast->hangupcause = bc->cause;
  3219. switch (bc->cause) {
  3220. case AST_CAUSE_UNALLOCATED:
  3221. case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
  3222. case AST_CAUSE_NO_ROUTE_DESTINATION:
  3223. case 4: /* Send special information tone */
  3224. case AST_CAUSE_NUMBER_CHANGED:
  3225. case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
  3226. /* Congestion Cases */
  3227. /*
  3228. * Not Queueing the Congestion anymore, since we want to hear
  3229. * the inband message
  3230. *
  3231. chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Congestion pid:%d\n", bc ? bc->pid : -1);
  3232. ch->state = MISDN_BUSY;
  3233. ast_queue_control(ast, AST_CONTROL_CONGESTION);
  3234. */
  3235. break;
  3236. case AST_CAUSE_CALL_REJECTED:
  3237. case AST_CAUSE_USER_BUSY:
  3238. ch->state = MISDN_BUSY;
  3239. if (!ch->need_busy) {
  3240. chan_misdn_log(1, bc ? bc->port : 0, "Queued busy already\n");
  3241. break;
  3242. }
  3243. chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Busy pid:%d\n", bc ? bc->pid : -1);
  3244. ast_queue_control(ast, AST_CONTROL_BUSY);
  3245. ch->need_busy = 0;
  3246. break;
  3247. }
  3248. }
  3249. /*! \brief Import parameters from the dialplan environment variables */
  3250. void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch)
  3251. {
  3252. const char *tmp;
  3253. tmp = pbx_builtin_getvar_helper(chan, "MISDN_PID");
  3254. if (tmp) {
  3255. ch->other_pid = atoi(tmp);
  3256. chan_misdn_log(3, bc->port, " --> IMPORT_PID: importing pid:%s\n", tmp);
  3257. if (ch->other_pid > 0) {
  3258. ch->other_ch = find_chan_by_pid(cl_te, ch->other_pid);
  3259. if (ch->other_ch)
  3260. ch->other_ch->other_ch = ch;
  3261. }
  3262. }
  3263. tmp = pbx_builtin_getvar_helper(chan, "MISDN_ADDRESS_COMPLETE");
  3264. if (tmp && (atoi(tmp) == 1)) {
  3265. bc->sending_complete = 1;
  3266. }
  3267. tmp = pbx_builtin_getvar_helper(chan, "MISDN_USERUSER");
  3268. if (tmp) {
  3269. ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", tmp);
  3270. ast_copy_string(bc->uu, tmp, sizeof(bc->uu));
  3271. bc->uulen = strlen(bc->uu);
  3272. }
  3273. tmp = pbx_builtin_getvar_helper(chan, "MISDN_KEYPAD");
  3274. if (tmp) {
  3275. ast_copy_string(bc->keypad, tmp, sizeof(bc->keypad));
  3276. }
  3277. }
  3278. /*! \brief Export parameters to the dialplan environment variables */
  3279. void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch)
  3280. {
  3281. char tmp[32];
  3282. chan_misdn_log(3, bc->port, " --> EXPORT_PID: pid:%d\n", bc->pid);
  3283. snprintf(tmp, sizeof(tmp), "%d", bc->pid);
  3284. pbx_builtin_setvar_helper(chan, "_MISDN_PID", tmp);
  3285. if (bc->sending_complete) {
  3286. snprintf(tmp, sizeof(tmp), "%d", bc->sending_complete);
  3287. pbx_builtin_setvar_helper(chan, "MISDN_ADDRESS_COMPLETE", tmp);
  3288. }
  3289. if (bc->urate) {
  3290. snprintf(tmp, sizeof(tmp), "%d", bc->urate);
  3291. pbx_builtin_setvar_helper(chan, "MISDN_URATE", tmp);
  3292. }
  3293. if (bc->uulen && (bc->uulen < sizeof(bc->uu))) {
  3294. bc->uu[bc->uulen] = 0;
  3295. pbx_builtin_setvar_helper(chan, "MISDN_USERUSER", bc->uu);
  3296. }
  3297. if (!ast_strlen_zero(bc->keypad))
  3298. pbx_builtin_setvar_helper(chan, "MISDN_KEYPAD", bc->keypad);
  3299. }
  3300. int add_in_calls(int port)
  3301. {
  3302. int max_in_calls;
  3303. misdn_cfg_get(port, MISDN_CFG_MAX_IN, &max_in_calls, sizeof(max_in_calls));
  3304. misdn_in_calls[port]++;
  3305. if (max_in_calls >= 0 && max_in_calls < misdn_in_calls[port]) {
  3306. ast_log(LOG_NOTICE, "Marking Incoming Call on port[%d]\n", port);
  3307. return misdn_in_calls[port] - max_in_calls;
  3308. }
  3309. return 0;
  3310. }
  3311. int add_out_calls(int port)
  3312. {
  3313. int max_out_calls;
  3314. misdn_cfg_get(port, MISDN_CFG_MAX_OUT, &max_out_calls, sizeof(max_out_calls));
  3315. if (max_out_calls >= 0 && max_out_calls <= misdn_out_calls[port]) {
  3316. ast_log(LOG_NOTICE, "Rejecting Outgoing Call on port[%d]\n", port);
  3317. return (misdn_out_calls[port] + 1) - max_out_calls;
  3318. }
  3319. misdn_out_calls[port]++;
  3320. return 0;
  3321. }
  3322. static void start_pbx(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan) {
  3323. if (pbx_start_chan(ch) < 0) {
  3324. hangup_chan(ch, bc);
  3325. chan_misdn_log(-1, bc->port, "ast_pbx_start returned <0 in SETUP\n");
  3326. if (bc->nt) {
  3327. hanguptone_indicate(ch);
  3328. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  3329. } else
  3330. misdn_lib_send_event(bc, EVENT_RELEASE);
  3331. }
  3332. }
  3333. static void wait_for_digits(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan) {
  3334. ch->state=MISDN_WAITING4DIGS;
  3335. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
  3336. if (bc->nt && !bc->dad[0])
  3337. dialtone_indicate(ch);
  3338. }
  3339. /************************************************************/
  3340. /* Receive Events from isdn_lib here */
  3341. /************************************************************/
  3342. static enum event_response_e
  3343. cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
  3344. {
  3345. int msn_valid;
  3346. struct chan_list *held_ch;
  3347. struct chan_list *ch = find_chan_by_bc(cl_te, bc);
  3348. if (event != EVENT_BCHAN_DATA && event != EVENT_TONE_GENERATE) { /* Debug Only Non-Bchan */
  3349. int debuglevel = 1;
  3350. if ( event == EVENT_CLEANUP && !user_data)
  3351. debuglevel = 5;
  3352. chan_misdn_log(debuglevel, bc->port, "I IND :%s oad:%s dad:%s pid:%d state:%s\n", manager_isdn_get_info(event), bc->oad, bc->dad, bc->pid, ch ? misdn_get_ch_state(ch) : "none");
  3353. if (debuglevel == 1) {
  3354. misdn_lib_log_ies(bc);
  3355. chan_misdn_log(4, bc->port, " --> bc_state:%s\n", bc_state2str(bc->bc_state));
  3356. }
  3357. }
  3358. if (!ch) {
  3359. switch(event) {
  3360. case EVENT_SETUP:
  3361. case EVENT_DISCONNECT:
  3362. case EVENT_RELEASE:
  3363. case EVENT_RELEASE_COMPLETE:
  3364. case EVENT_PORT_ALARM:
  3365. case EVENT_RETRIEVE:
  3366. case EVENT_NEW_BC:
  3367. case EVENT_FACILITY:
  3368. break;
  3369. case EVENT_CLEANUP:
  3370. case EVENT_TONE_GENERATE:
  3371. case EVENT_BCHAN_DATA:
  3372. return -1;
  3373. default:
  3374. chan_misdn_log(1, bc->port, "Chan not existing at the moment bc->l3id:%x bc:%p event:%s port:%d channel:%d\n", bc->l3_id, bc, manager_isdn_get_info(event), bc->port, bc->channel);
  3375. return -1;
  3376. }
  3377. }
  3378. if (ch) {
  3379. switch (event) {
  3380. case EVENT_TONE_GENERATE:
  3381. break;
  3382. case EVENT_DISCONNECT:
  3383. case EVENT_RELEASE:
  3384. case EVENT_RELEASE_COMPLETE:
  3385. case EVENT_CLEANUP:
  3386. case EVENT_TIMEOUT:
  3387. if (!ch->ast)
  3388. chan_misdn_log(3, bc->port, "ast_hangup already called, so we have no ast ptr anymore in event(%s)\n", manager_isdn_get_info(event));
  3389. break;
  3390. default:
  3391. if (!ch->ast || !MISDN_ASTERISK_PVT(ch->ast) || !MISDN_ASTERISK_TECH_PVT(ch->ast)) {
  3392. if (event != EVENT_BCHAN_DATA)
  3393. ast_log(LOG_NOTICE, "No Ast or No private Pointer in Event (%d:%s)\n", event, manager_isdn_get_info(event));
  3394. return -1;
  3395. }
  3396. }
  3397. }
  3398. switch (event) {
  3399. case EVENT_PORT_ALARM:
  3400. {
  3401. int boa = 0;
  3402. misdn_cfg_get(bc->port, MISDN_CFG_ALARM_BLOCK, &boa, sizeof(int));
  3403. if (boa) {
  3404. cb_log(1, bc->port, " --> blocking\n");
  3405. misdn_lib_port_block(bc->port);
  3406. }
  3407. }
  3408. break;
  3409. case EVENT_BCHAN_ACTIVATED:
  3410. break;
  3411. case EVENT_NEW_CHANNEL:
  3412. update_name(ch->ast,bc->port,bc->channel);
  3413. break;
  3414. case EVENT_NEW_L3ID:
  3415. ch->l3id=bc->l3_id;
  3416. ch->addr=bc->addr;
  3417. break;
  3418. case EVENT_NEW_BC:
  3419. if (!ch) {
  3420. ch = find_hold_call(cl_te,bc);
  3421. }
  3422. if (!ch) {
  3423. ast_log(LOG_WARNING, "NEW_BC without chan_list?\n");
  3424. break;
  3425. }
  3426. if (bc)
  3427. ch->bc = (struct misdn_bchannel *)user_data;
  3428. break;
  3429. case EVENT_DTMF_TONE:
  3430. {
  3431. /* sending INFOS as DTMF-Frames :) */
  3432. struct ast_frame fr;
  3433. memset(&fr, 0, sizeof(fr));
  3434. fr.frametype = AST_FRAME_DTMF;
  3435. fr.subclass = bc->dtmf ;
  3436. fr.src = NULL;
  3437. fr.data = NULL;
  3438. fr.datalen = 0;
  3439. fr.samples = 0;
  3440. fr.mallocd = 0;
  3441. fr.offset = 0;
  3442. fr.delivery = ast_tv(0,0);
  3443. if (!ch->ignore_dtmf) {
  3444. chan_misdn_log(2, bc->port, " --> DTMF:%c\n", bc->dtmf);
  3445. ast_queue_frame(ch->ast, &fr);
  3446. } else {
  3447. chan_misdn_log(2, bc->port, " --> Ignoring DTMF:%c due to bridge flags\n", bc->dtmf);
  3448. }
  3449. }
  3450. break;
  3451. case EVENT_STATUS:
  3452. break;
  3453. case EVENT_INFORMATION:
  3454. {
  3455. if ( ch->state != MISDN_CONNECTED )
  3456. stop_indicate(ch);
  3457. if (!ch->ast)
  3458. break;
  3459. if (ch->state == MISDN_WAITING4DIGS ) {
  3460. /* Ok, incomplete Setup, waiting till extension exists */
  3461. if (ast_strlen_zero(bc->info_dad) && ! ast_strlen_zero(bc->keypad)) {
  3462. chan_misdn_log(1, bc->port, " --> using keypad as info\n");
  3463. ast_copy_string(bc->info_dad, bc->keypad, sizeof(bc->info_dad));
  3464. }
  3465. strncat(bc->dad, bc->info_dad, sizeof(bc->dad) - strlen(bc->dad) - 1);
  3466. ast_copy_string(ch->ast->exten, bc->dad, sizeof(ch->ast->exten));
  3467. /* Check for Pickup Request first */
  3468. if (!strcmp(ch->ast->exten, ast_pickup_ext())) {
  3469. if (ast_pickup_call(ch->ast)) {
  3470. hangup_chan(ch, bc);
  3471. } else {
  3472. struct ast_channel *chan = ch->ast;
  3473. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  3474. ast_setstate(chan, AST_STATE_DOWN);
  3475. hangup_chan(ch, bc);
  3476. ch->ast = NULL;
  3477. break;
  3478. }
  3479. }
  3480. if (!ast_canmatch_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
  3481. if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->oad)) {
  3482. ast_log(LOG_WARNING,
  3483. "Extension '%s@%s' can never match. Jumping to 'i' extension. port:%d\n",
  3484. bc->dad, ch->context, bc->port);
  3485. strcpy(ch->ast->exten, "i");
  3486. ch->state = MISDN_DIALING;
  3487. start_pbx(ch, bc, ch->ast);
  3488. break;
  3489. }
  3490. ast_log(LOG_WARNING,
  3491. "Extension '%s@%s' can never match. Disconnecting. port:%d\n"
  3492. "\tMaybe you want to add an 'i' extension to catch this case.\n",
  3493. bc->dad, ch->context, bc->port);
  3494. if (bc->nt)
  3495. hanguptone_indicate(ch);
  3496. ch->state = MISDN_EXTCANTMATCH;
  3497. bc->out_cause = AST_CAUSE_UNALLOCATED;
  3498. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  3499. break;
  3500. }
  3501. if (ch->overlap_dial) {
  3502. ast_mutex_lock(&ch->overlap_tv_lock);
  3503. ch->overlap_tv = ast_tvnow();
  3504. ast_mutex_unlock(&ch->overlap_tv_lock);
  3505. if (ch->overlap_dial_task == -1) {
  3506. ch->overlap_dial_task =
  3507. misdn_tasks_add_variable(ch->overlap_dial, misdn_overlap_dial_task, ch);
  3508. }
  3509. break;
  3510. }
  3511. if (ast_exists_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
  3512. ch->state = MISDN_DIALING;
  3513. start_pbx(ch, bc, ch->ast);
  3514. }
  3515. } else {
  3516. /* sending INFOS as DTMF-Frames :) */
  3517. struct ast_frame fr;
  3518. int digits;
  3519. memset(&fr, 0, sizeof(fr));
  3520. fr.frametype = AST_FRAME_DTMF;
  3521. fr.subclass = bc->info_dad[0] ;
  3522. fr.src = NULL;
  3523. fr.data = NULL;
  3524. fr.datalen = 0;
  3525. fr.samples = 0;
  3526. fr.mallocd = 0;
  3527. fr.offset = 0;
  3528. fr.delivery = ast_tv(0,0);
  3529. misdn_cfg_get(0, MISDN_GEN_APPEND_DIGITS2EXTEN, &digits, sizeof(int));
  3530. if (ch->state != MISDN_CONNECTED ) {
  3531. if (digits) {
  3532. strncat(bc->dad, bc->info_dad, sizeof(bc->dad) - strlen(bc->dad) - 1);
  3533. ast_copy_string(ch->ast->exten, bc->dad, sizeof(ch->ast->exten));
  3534. ast_cdr_update(ch->ast);
  3535. }
  3536. ast_queue_frame(ch->ast, &fr);
  3537. }
  3538. }
  3539. }
  3540. break;
  3541. case EVENT_SETUP:
  3542. {
  3543. struct chan_list *ch = find_chan_by_bc(cl_te, bc);
  3544. struct ast_channel *chan;
  3545. int exceed;
  3546. int pres, screen;
  3547. int ai;
  3548. int im;
  3549. if (ch) {
  3550. switch (ch->state) {
  3551. case MISDN_NOTHING:
  3552. ch = NULL;
  3553. break;
  3554. default:
  3555. chan_misdn_log(1, bc->port, " --> Ignoring Call we have already one\n");
  3556. return RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE; /* Ignore MSNs which are not in our List */
  3557. }
  3558. }
  3559. msn_valid = misdn_cfg_is_msn_valid(bc->port, bc->dad);
  3560. if (!bc->nt && ! msn_valid) {
  3561. chan_misdn_log(1, bc->port, " --> Ignoring Call, its not in our MSN List\n");
  3562. return RESPONSE_IGNORE_SETUP; /* Ignore MSNs which are not in our List */
  3563. }
  3564. if (bc->cw) {
  3565. int cause;
  3566. chan_misdn_log(0, bc->port, " --> Call Waiting on PMP sending RELEASE_COMPLETE\n");
  3567. misdn_cfg_get(bc->port, MISDN_CFG_REJECT_CAUSE, &cause, sizeof(cause));
  3568. bc->out_cause = cause ? cause : AST_CAUSE_NORMAL_CLEARING;
  3569. return RESPONSE_RELEASE_SETUP;
  3570. }
  3571. print_bearer(bc);
  3572. ch = init_chan_list(ORG_MISDN);
  3573. if (!ch) {
  3574. chan_misdn_log(-1, bc->port, "cb_events: malloc for chan_list failed!\n");
  3575. return 0;
  3576. }
  3577. ch->bc = bc;
  3578. ch->l3id = bc->l3_id;
  3579. ch->addr = bc->addr;
  3580. ch->originator = ORG_MISDN;
  3581. chan = misdn_new(ch, AST_STATE_RESERVED, bc->dad, bc->oad, AST_FORMAT_ALAW, bc->port, bc->channel);
  3582. if (!chan) {
  3583. free(ch);
  3584. misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
  3585. ast_log(LOG_ERROR, "cb_events: misdn_new failed !\n");
  3586. return 0;
  3587. }
  3588. ch->ast = chan;
  3589. if ((exceed = add_in_calls(bc->port))) {
  3590. char tmp[16];
  3591. snprintf(tmp, sizeof(tmp), "%d", exceed);
  3592. pbx_builtin_setvar_helper(chan, "MAX_OVERFLOW", tmp);
  3593. }
  3594. read_config(ch, ORG_MISDN);
  3595. export_ch(chan, bc, ch);
  3596. ch->ast->rings = 1;
  3597. ast_setstate(ch->ast, AST_STATE_RINGING);
  3598. switch (bc->pres) {
  3599. case 1:
  3600. pres = AST_PRES_RESTRICTED;
  3601. chan_misdn_log(2, bc->port, " --> PRES: Restricted (1)\n");
  3602. break;
  3603. case 2:
  3604. pres = AST_PRES_UNAVAILABLE;
  3605. chan_misdn_log(2, bc->port, " --> PRES: Unavailable (2)\n");
  3606. break;
  3607. default:
  3608. pres = AST_PRES_ALLOWED;
  3609. chan_misdn_log(2, bc->port, " --> PRES: Allowed (%d)\n", bc->pres);
  3610. break;
  3611. }
  3612. switch (bc->screen) {
  3613. default:
  3614. case 0:
  3615. screen = AST_PRES_USER_NUMBER_UNSCREENED;
  3616. chan_misdn_log(2, bc->port, " --> SCREEN: Unscreened (%d)\n", bc->screen);
  3617. break;
  3618. case 1:
  3619. screen = AST_PRES_USER_NUMBER_PASSED_SCREEN;
  3620. chan_misdn_log(2, bc->port, " --> SCREEN: Passed screen (1)\n");
  3621. break;
  3622. case 2:
  3623. screen = AST_PRES_USER_NUMBER_FAILED_SCREEN;
  3624. chan_misdn_log(2, bc->port, " --> SCREEN: failed screen (2)\n");
  3625. break;
  3626. case 3:
  3627. screen = AST_PRES_NETWORK_NUMBER;
  3628. chan_misdn_log(2, bc->port, " --> SCREEN: Network Number (3)\n");
  3629. break;
  3630. }
  3631. chan->cid.cid_pres = pres | screen;
  3632. pbx_builtin_setvar_helper(chan, "TRANSFERCAPABILITY", ast_transfercapability2str(bc->capability));
  3633. chan->transfercapability = bc->capability;
  3634. switch (bc->capability) {
  3635. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  3636. pbx_builtin_setvar_helper(chan, "CALLTYPE", "DIGITAL");
  3637. break;
  3638. default:
  3639. pbx_builtin_setvar_helper(chan, "CALLTYPE", "SPEECH");
  3640. }
  3641. /** queue new chan **/
  3642. cl_queue_chan(&cl_te, ch);
  3643. if (!strstr(ch->allowed_bearers, "all")) {
  3644. int i;
  3645. for (i = 0; i < ARRAY_LEN(allowed_bearers_array); ++i) {
  3646. if (allowed_bearers_array[i].cap == bc->capability) {
  3647. if (strstr(ch->allowed_bearers, allowed_bearers_array[i].name)) {
  3648. /* The bearer capability is allowed */
  3649. if (allowed_bearers_array[i].deprecated) {
  3650. chan_misdn_log(0, bc->port, "%s in allowed_bearers list is deprecated\n",
  3651. allowed_bearers_array[i].name);
  3652. }
  3653. break;
  3654. }
  3655. }
  3656. } /* end for */
  3657. if (i == ARRAY_LEN(allowed_bearers_array)) {
  3658. /* We did not find the bearer capability */
  3659. chan_misdn_log(0, bc->port, "Bearer capability not allowed: %s(%d)\n",
  3660. bearer2str(bc->capability), bc->capability);
  3661. bc->out_cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
  3662. ch->state = MISDN_EXTCANTMATCH;
  3663. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  3664. return RESPONSE_OK;
  3665. }
  3666. }
  3667. /* Check for Pickup Request first */
  3668. if (!strcmp(chan->exten, ast_pickup_ext())) {
  3669. if (!ch->noautorespond_on_setup) {
  3670. int ret;/** Sending SETUP_ACK**/
  3671. ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
  3672. } else {
  3673. ch->state = MISDN_INCOMING_SETUP;
  3674. }
  3675. if (ast_pickup_call(chan)) {
  3676. hangup_chan(ch, bc);
  3677. } else {
  3678. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  3679. ast_setstate(chan, AST_STATE_DOWN);
  3680. hangup_chan(ch, bc);
  3681. ch->ast = NULL;
  3682. break;
  3683. }
  3684. }
  3685. /*
  3686. * added support for s extension hope it will help those poor cretains
  3687. * which haven't overlap dial.
  3688. */
  3689. misdn_cfg_get(bc->port, MISDN_CFG_ALWAYS_IMMEDIATE, &ai, sizeof(ai));
  3690. if (ai) {
  3691. do_immediate_setup(bc, ch, chan);
  3692. break;
  3693. }
  3694. /* check if we should jump into s when we have no dad */
  3695. misdn_cfg_get(bc->port, MISDN_CFG_IMMEDIATE, &im, sizeof(im));
  3696. if (im && ast_strlen_zero(bc->dad)) {
  3697. do_immediate_setup(bc, ch, chan);
  3698. break;
  3699. }
  3700. chan_misdn_log(5, bc->port, "CONTEXT:%s\n", ch->context);
  3701. if(!ast_canmatch_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
  3702. if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->oad)) {
  3703. ast_log(LOG_WARNING,
  3704. "Extension '%s@%s' can never match. Jumping to 'i' extension. port:%d\n",
  3705. bc->dad, ch->context, bc->port);
  3706. strcpy(ch->ast->exten, "i");
  3707. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  3708. ch->state = MISDN_DIALING;
  3709. start_pbx(ch, bc, chan);
  3710. break;
  3711. }
  3712. ast_log(LOG_WARNING,
  3713. "Extension '%s@%s' can never match. Disconnecting. port:%d\n"
  3714. "\tMaybe you want to add an 'i' extension to catch this case.\n",
  3715. bc->dad, ch->context, bc->port);
  3716. if (bc->nt)
  3717. hanguptone_indicate(ch);
  3718. ch->state = MISDN_EXTCANTMATCH;
  3719. bc->out_cause = AST_CAUSE_UNALLOCATED;
  3720. if (bc->nt)
  3721. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
  3722. else
  3723. misdn_lib_send_event(bc, EVENT_RELEASE );
  3724. break;
  3725. }
  3726. /* Whatever happens, when sending_complete is set or we are PTMP TE, we will definitely
  3727. * jump into the dialplan, when the dialed extension does not exist, the 's' extension
  3728. * will be used by Asterisk automatically. */
  3729. if (bc->sending_complete || (!bc->nt && !misdn_lib_is_ptp(bc->port))) {
  3730. if (!ch->noautorespond_on_setup) {
  3731. ch->state=MISDN_DIALING;
  3732. misdn_lib_send_event(bc, EVENT_PROCEEDING );
  3733. } else {
  3734. ch->state = MISDN_INCOMING_SETUP;
  3735. }
  3736. start_pbx(ch, bc, chan);
  3737. break;
  3738. }
  3739. /*
  3740. * When we are NT and overlapdial is set and if
  3741. * the number is empty, we wait for the ISDN timeout
  3742. * instead of our own timer.
  3743. */
  3744. if (ch->overlap_dial && bc->nt && !bc->dad[0] ) {
  3745. wait_for_digits(ch, bc, chan);
  3746. break;
  3747. }
  3748. /*
  3749. * If overlapdial we will definitely send a SETUP_ACKNOWLEDGE and wait for more
  3750. * Infos with a Interdigit Timeout.
  3751. * */
  3752. if (ch->overlap_dial) {
  3753. ast_mutex_lock(&ch->overlap_tv_lock);
  3754. ch->overlap_tv = ast_tvnow();
  3755. ast_mutex_unlock(&ch->overlap_tv_lock);
  3756. wait_for_digits(ch, bc, chan);
  3757. if (ch->overlap_dial_task == -1)
  3758. ch->overlap_dial_task =
  3759. misdn_tasks_add_variable(ch->overlap_dial, misdn_overlap_dial_task, ch);
  3760. break;
  3761. }
  3762. /* If the extension does not exist and we're not TE_PTMP we wait for more digits
  3763. * without interdigit timeout.
  3764. * */
  3765. if (!ast_exists_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
  3766. wait_for_digits(ch, bc, chan);
  3767. break;
  3768. }
  3769. /*
  3770. * If the extension exists let's just jump into it.
  3771. * */
  3772. if (ast_exists_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
  3773. if (bc->need_more_infos)
  3774. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
  3775. else
  3776. misdn_lib_send_event(bc, EVENT_PROCEEDING);
  3777. ch->state = MISDN_DIALING;
  3778. start_pbx(ch, bc, chan);
  3779. break;
  3780. }
  3781. }
  3782. break;
  3783. case EVENT_SETUP_ACKNOWLEDGE:
  3784. {
  3785. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  3786. if (bc->channel)
  3787. update_name(ch->ast,bc->port,bc->channel);
  3788. if (!ast_strlen_zero(bc->infos_pending)) {
  3789. /* TX Pending Infos */
  3790. strncat(bc->dad, bc->infos_pending, sizeof(bc->dad) - strlen(bc->dad) - 1);
  3791. if (!ch->ast)
  3792. break;
  3793. ast_copy_string(ch->ast->exten, bc->dad, sizeof(ch->ast->exten));
  3794. ast_copy_string(bc->info_dad, bc->infos_pending, sizeof(bc->info_dad));
  3795. ast_copy_string(bc->infos_pending, "", sizeof(bc->infos_pending));
  3796. misdn_lib_send_event(bc, EVENT_INFORMATION);
  3797. }
  3798. }
  3799. break;
  3800. case EVENT_PROCEEDING:
  3801. {
  3802. if (bc->channel)
  3803. update_name(ch->ast, bc->port, bc->channel);
  3804. if (misdn_cap_is_speech(bc->capability) &&
  3805. misdn_inband_avail(bc) ) {
  3806. start_bc_tones(ch);
  3807. }
  3808. ch->state = MISDN_PROCEEDING;
  3809. if (!ch->ast)
  3810. break;
  3811. ast_queue_control(ch->ast, AST_CONTROL_PROCEEDING);
  3812. }
  3813. break;
  3814. case EVENT_PROGRESS:
  3815. if (bc->channel)
  3816. update_name(ch->ast, bc->port, bc->channel);
  3817. if (!bc->nt ) {
  3818. if ( misdn_cap_is_speech(bc->capability) &&
  3819. misdn_inband_avail(bc)
  3820. ) {
  3821. start_bc_tones(ch);
  3822. }
  3823. ch->state = MISDN_PROGRESS;
  3824. if (!ch->ast)
  3825. break;
  3826. ast_queue_control(ch->ast, AST_CONTROL_PROGRESS);
  3827. }
  3828. break;
  3829. case EVENT_ALERTING:
  3830. {
  3831. if (bc->channel)
  3832. update_name(ch->ast, bc->port, bc->channel);
  3833. ch->state = MISDN_ALERTING;
  3834. if (!ch->ast)
  3835. break;
  3836. ast_queue_control(ch->ast, AST_CONTROL_RINGING);
  3837. ast_setstate(ch->ast, AST_STATE_RINGING);
  3838. cb_log(7, bc->port, " --> Set State Ringing\n");
  3839. if (misdn_cap_is_speech(bc->capability) && misdn_inband_avail(bc)) {
  3840. cb_log(1, bc->port, "Starting Tones, we have inband Data\n");
  3841. start_bc_tones(ch);
  3842. } else {
  3843. cb_log(3, bc->port, " --> We have no inband Data, the other end must create ringing\n");
  3844. if (ch->far_alerting) {
  3845. cb_log(1, bc->port, " --> The other end can not do ringing eh ?.. we must do all ourself..");
  3846. start_bc_tones(ch);
  3847. /*tone_indicate(ch, TONE_FAR_ALERTING);*/
  3848. }
  3849. }
  3850. }
  3851. break;
  3852. case EVENT_CONNECT:
  3853. {
  3854. struct ast_channel *bridged;
  3855. /*we answer when we've got our very new L3 ID from the NT stack */
  3856. misdn_lib_send_event(bc, EVENT_CONNECT_ACKNOWLEDGE);
  3857. if (!ch->ast)
  3858. break;
  3859. bridged = ast_bridged_channel(ch->ast);
  3860. stop_indicate(ch);
  3861. if (bridged && !strcasecmp(bridged->tech->type, "mISDN")) {
  3862. struct chan_list *bridged_ch = MISDN_ASTERISK_TECH_PVT(bridged);
  3863. chan_misdn_log(1, bc->port, " --> copying cpndialplan:%d and cad:%s to the A-Channel\n", bc->cpnnumplan, bc->cad);
  3864. if (bridged_ch) {
  3865. bridged_ch->bc->cpnnumplan = bc->cpnnumplan;
  3866. ast_copy_string(bridged_ch->bc->cad, bc->cad, sizeof(bridged_ch->bc->cad));
  3867. }
  3868. }
  3869. }
  3870. ch->l3id=bc->l3_id;
  3871. ch->addr=bc->addr;
  3872. start_bc_tones(ch);
  3873. ch->state = MISDN_CONNECTED;
  3874. ast_queue_control(ch->ast, AST_CONTROL_ANSWER);
  3875. break;
  3876. case EVENT_CONNECT_ACKNOWLEDGE:
  3877. {
  3878. ch->l3id = bc->l3_id;
  3879. ch->addr = bc->addr;
  3880. start_bc_tones(ch);
  3881. ch->state = MISDN_CONNECTED;
  3882. }
  3883. break;
  3884. case EVENT_DISCONNECT:
  3885. /*we might not have an ch->ast ptr here anymore*/
  3886. if (ch) {
  3887. chan_misdn_log(3, bc->port, " --> org:%d nt:%d, inbandavail:%d state:%d\n", ch->originator, bc->nt, misdn_inband_avail(bc), ch->state);
  3888. if (ch->originator == ORG_AST && !bc->nt && misdn_inband_avail(bc) && ch->state != MISDN_CONNECTED) {
  3889. /* If there's inband information available (e.g. a
  3890. recorded message saying what was wrong with the
  3891. dialled number, or perhaps even giving an
  3892. alternative number, then play it instead of
  3893. immediately releasing the call */
  3894. chan_misdn_log(1, bc->port, " --> Inband Info Avail, not sending RELEASE\n");
  3895. ch->state = MISDN_DISCONNECTED;
  3896. start_bc_tones(ch);
  3897. if (ch->ast) {
  3898. ch->ast->hangupcause = bc->cause;
  3899. if (bc->cause == AST_CAUSE_USER_BUSY)
  3900. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  3901. }
  3902. ch->need_busy = 0;
  3903. break;
  3904. }
  3905. bc->need_disconnect = 0;
  3906. stop_bc_tones(ch);
  3907. /* Check for held channel, to implement transfer */
  3908. held_ch = find_hold_call(cl_te, bc);
  3909. if (!held_ch || !ch->ast || misdn_attempt_transfer(ch, held_ch)) {
  3910. hangup_chan(ch, bc);
  3911. }
  3912. } else {
  3913. held_ch = find_hold_call_l3(cl_te, bc->l3_id);
  3914. if (held_ch && held_ch->hold.state == MISDN_HOLD_ACTIVE) {
  3915. bc->need_disconnect = 0;
  3916. #if defined(TRANSFER_ON_HELD_CALL_HANGUP)
  3917. /*
  3918. * Some phones disconnect the held call and the active call at the
  3919. * same time to do the transfer. Unfortunately, either call could
  3920. * be disconnected first.
  3921. */
  3922. ch = find_hold_active_call(cl_te, bc);
  3923. if (!ch || misdn_attempt_transfer(ch, held_ch)) {
  3924. held_ch->hold.state = MISDN_HOLD_DISCONNECT;
  3925. hangup_chan(held_ch, bc);
  3926. }
  3927. #else
  3928. hangup_chan(held_ch, bc);
  3929. #endif /* defined(TRANSFER_ON_HELD_CALL_HANGUP) */
  3930. }
  3931. }
  3932. bc->out_cause = -1;
  3933. if (bc->need_release)
  3934. misdn_lib_send_event(bc, EVENT_RELEASE);
  3935. break;
  3936. case EVENT_RELEASE:
  3937. if (!ch) {
  3938. ch = find_hold_call_l3(cl_te, bc->l3_id);
  3939. if (!ch) {
  3940. chan_misdn_log(1, bc->port,
  3941. " --> no Ch, so we've already released. (%s)\n",
  3942. manager_isdn_get_info(event));
  3943. return -1;
  3944. }
  3945. }
  3946. bc->need_disconnect = 0;
  3947. bc->need_release = 0;
  3948. hangup_chan(ch, bc);
  3949. release_chan(ch, bc);
  3950. break;
  3951. case EVENT_RELEASE_COMPLETE:
  3952. if (!ch) {
  3953. ch = find_hold_call_l3(cl_te, bc->l3_id);
  3954. if (!ch) {
  3955. chan_misdn_log(1, bc->port,
  3956. " --> no Ch, so we've already released. (%s)\n",
  3957. manager_isdn_get_info(event));
  3958. break;
  3959. }
  3960. }
  3961. bc->need_disconnect = 0;
  3962. bc->need_release = 0;
  3963. bc->need_release_complete = 0;
  3964. stop_bc_tones(ch);
  3965. hangup_chan(ch, bc);
  3966. release_chan(ch, bc);
  3967. break;
  3968. case EVENT_BCHAN_ERROR:
  3969. case EVENT_CLEANUP:
  3970. {
  3971. stop_bc_tones(ch);
  3972. switch (ch->state) {
  3973. case MISDN_CALLING:
  3974. bc->cause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
  3975. break;
  3976. default:
  3977. break;
  3978. }
  3979. hangup_chan(ch, bc);
  3980. release_chan(ch, bc);
  3981. }
  3982. break;
  3983. case EVENT_TONE_GENERATE:
  3984. {
  3985. int tone_len = bc->tone_cnt;
  3986. struct ast_channel *ast = ch->ast;
  3987. void *tmp;
  3988. int res;
  3989. int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
  3990. chan_misdn_log(9, bc->port, "TONE_GEN: len:%d\n", tone_len);
  3991. if (!ast)
  3992. break;
  3993. if (!ast->generator)
  3994. break;
  3995. tmp = ast->generatordata;
  3996. ast->generatordata = NULL;
  3997. generate = ast->generator->generate;
  3998. if (tone_len < 0 || tone_len > 512 ) {
  3999. ast_log(LOG_NOTICE, "TONE_GEN: len was %d, set to 128\n", tone_len);
  4000. tone_len = 128;
  4001. }
  4002. res = generate(ast, tmp, tone_len, tone_len);
  4003. ast->generatordata = tmp;
  4004. if (res) {
  4005. ast_log(LOG_WARNING, "Auto-deactivating generator\n");
  4006. ast_deactivate_generator(ast);
  4007. } else {
  4008. bc->tone_cnt = 0;
  4009. }
  4010. }
  4011. break;
  4012. case EVENT_BCHAN_DATA:
  4013. {
  4014. if (!misdn_cap_is_speech(ch->bc->capability)) {
  4015. struct ast_frame frame;
  4016. /*In Data Modes we queue frames*/
  4017. memset(&frame, 0, sizeof(frame));
  4018. frame.frametype = AST_FRAME_VOICE; /*we have no data frames yet*/
  4019. frame.subclass = AST_FORMAT_ALAW;
  4020. frame.datalen = bc->bframe_len;
  4021. frame.samples = bc->bframe_len;
  4022. frame.mallocd = 0;
  4023. frame.offset = 0;
  4024. frame.delivery = ast_tv(0,0);
  4025. frame.src = NULL;
  4026. frame.data = bc->bframe;
  4027. if (ch->ast)
  4028. ast_queue_frame(ch->ast, &frame);
  4029. } else {
  4030. struct pollfd pfd = { .fd = ch->pipe[1], .events = POLLOUT };
  4031. int t;
  4032. t = ast_poll(&pfd, 1, 0);
  4033. if (t < 0) {
  4034. chan_misdn_log(-1, bc->port, "poll() error (err=%s)\n", strerror(errno));
  4035. break;
  4036. }
  4037. if (!t) {
  4038. chan_misdn_log(9, bc->port, "poll() timed out\n");
  4039. break;
  4040. }
  4041. if (pfd.revents & POLLOUT) {
  4042. chan_misdn_log(9, bc->port, "writing %d bytes to asterisk\n", bc->bframe_len);
  4043. if (write(ch->pipe[1], bc->bframe, bc->bframe_len) <= 0) {
  4044. chan_misdn_log(0, bc->port, "Write returned <=0 (err=%s) --> hanging up channel\n", strerror(errno));
  4045. stop_bc_tones(ch);
  4046. hangup_chan(ch, bc);
  4047. release_chan(ch, bc);
  4048. }
  4049. } else {
  4050. chan_misdn_log(1, bc->port, "Write Pipe full!\n");
  4051. }
  4052. }
  4053. }
  4054. break;
  4055. case EVENT_TIMEOUT:
  4056. {
  4057. if (ch && bc)
  4058. chan_misdn_log(1, bc->port, "--> state: %s\n", misdn_get_ch_state(ch));
  4059. switch (ch->state) {
  4060. case MISDN_DIALING:
  4061. case MISDN_PROGRESS:
  4062. if (bc->nt && !ch->nttimeout)
  4063. break;
  4064. case MISDN_CALLING:
  4065. case MISDN_ALERTING:
  4066. case MISDN_PROCEEDING:
  4067. case MISDN_CALLING_ACKNOWLEDGE:
  4068. if (bc->nt) {
  4069. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  4070. hanguptone_indicate(ch);
  4071. }
  4072. bc->out_cause = AST_CAUSE_UNALLOCATED;
  4073. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  4074. break;
  4075. case MISDN_WAITING4DIGS:
  4076. if (bc->nt) {
  4077. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  4078. bc->out_cause = AST_CAUSE_UNALLOCATED;
  4079. hanguptone_indicate(ch);
  4080. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  4081. } else {
  4082. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  4083. misdn_lib_send_event(bc, EVENT_RELEASE);
  4084. }
  4085. break;
  4086. case MISDN_CLEANING:
  4087. chan_misdn_log(1,bc->port," --> in state cleaning .. so ignoring, the stack should clean it for us\n");
  4088. break;
  4089. default:
  4090. misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
  4091. }
  4092. }
  4093. break;
  4094. /****************************/
  4095. /** Supplementary Services **/
  4096. /****************************/
  4097. case EVENT_RETRIEVE:
  4098. if (!ch) {
  4099. chan_misdn_log(4, bc->port, " --> no CH, searching for held call\n");
  4100. ch = find_hold_call_l3(cl_te, bc->l3_id);
  4101. if (!ch || ch->hold.state != MISDN_HOLD_ACTIVE) {
  4102. ast_log(LOG_WARNING, "No held call found, cannot Retrieve\n");
  4103. misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT);
  4104. break;
  4105. }
  4106. }
  4107. /* remember the channel again */
  4108. ch->bc = bc;
  4109. ch->hold.state = MISDN_HOLD_IDLE;
  4110. ch->hold.port = 0;
  4111. ch->hold.channel = 0;
  4112. ast_queue_control(ch->ast, AST_CONTROL_UNHOLD);
  4113. if (misdn_lib_send_event(bc, EVENT_RETRIEVE_ACKNOWLEDGE) < 0) {
  4114. chan_misdn_log(4, bc->port, " --> RETRIEVE_ACK failed\n");
  4115. misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT);
  4116. }
  4117. break;
  4118. case EVENT_HOLD:
  4119. {
  4120. int hold_allowed;
  4121. struct ast_channel *bridged;
  4122. misdn_cfg_get(bc->port, MISDN_CFG_HOLD_ALLOWED, &hold_allowed, sizeof(int));
  4123. if (!hold_allowed) {
  4124. chan_misdn_log(-1, bc->port, "Hold not allowed this port.\n");
  4125. misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
  4126. break;
  4127. }
  4128. bridged = ast_bridged_channel(ch->ast);
  4129. if (bridged) {
  4130. chan_misdn_log(2, bc->port, "Bridge Partner is of type: %s\n", bridged->tech->type);
  4131. ch->l3id = bc->l3_id;
  4132. /* forget the channel now */
  4133. ch->bc = NULL;
  4134. ch->hold.state = MISDN_HOLD_ACTIVE;
  4135. ch->hold.port = bc->port;
  4136. ch->hold.channel = bc->channel;
  4137. ast_queue_control(ch->ast, AST_CONTROL_HOLD);
  4138. misdn_lib_send_event(bc, EVENT_HOLD_ACKNOWLEDGE);
  4139. } else {
  4140. misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
  4141. chan_misdn_log(0, bc->port, "We aren't bridged to anybody\n");
  4142. }
  4143. }
  4144. break;
  4145. case EVENT_FACILITY:
  4146. if (!ch) {
  4147. /* This may come from a call we don't know nothing about, so we ignore it. */
  4148. chan_misdn_log(-1, bc->port, "Got EVENT_FACILITY but we don't have a ch!\n");
  4149. break;
  4150. }
  4151. print_facility(&(bc->fac_in), bc);
  4152. switch (bc->fac_in.Function) {
  4153. case Fac_CD:
  4154. {
  4155. struct ast_channel *bridged = ast_bridged_channel(ch->ast);
  4156. struct chan_list *ch_br;
  4157. if (bridged && MISDN_ASTERISK_TECH_PVT(bridged)) {
  4158. ch_br = MISDN_ASTERISK_TECH_PVT(bridged);
  4159. /*ch->state = MISDN_FACILITY_DEFLECTED;*/
  4160. if (ch_br->bc) {
  4161. if (ast_exists_extension(bridged, ch->context, (char *)bc->fac_in.u.CDeflection.DeflectedToNumber, 1, bc->oad)) {
  4162. ch_br->state = MISDN_DIALING;
  4163. if (pbx_start_chan(ch_br) < 0) {
  4164. chan_misdn_log(-1, ch_br->bc->port, "ast_pbx_start returned < 0 in misdn_overlap_dial_task\n");
  4165. }
  4166. }
  4167. }
  4168. }
  4169. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  4170. }
  4171. break;
  4172. case Fac_AOCDCurrency:
  4173. {
  4174. bc->AOCDtype = Fac_AOCDCurrency;
  4175. memcpy(&(bc->AOCD.currency), &(bc->fac_in.u.AOCDcur), sizeof(struct FacAOCDCurrency));
  4176. export_aoc_vars(ch->originator, ch->ast, bc);
  4177. }
  4178. break;
  4179. case Fac_AOCDChargingUnit:
  4180. {
  4181. bc->AOCDtype = Fac_AOCDChargingUnit;
  4182. memcpy(&(bc->AOCD.chargingUnit), &(bc->fac_in.u.AOCDchu), sizeof(struct FacAOCDChargingUnit));
  4183. export_aoc_vars(ch->originator, ch->ast, bc);
  4184. }
  4185. break;
  4186. default:
  4187. chan_misdn_log(0, bc->port," --> not yet handled: facility type:%d\n", bc->fac_in.Function);
  4188. }
  4189. break;
  4190. case EVENT_RESTART:
  4191. if (!bc->dummy) {
  4192. stop_bc_tones(ch);
  4193. release_chan(ch, bc);
  4194. }
  4195. break;
  4196. default:
  4197. chan_misdn_log(1, 0, "Got Unknown Event\n");
  4198. break;
  4199. }
  4200. return RESPONSE_OK;
  4201. }
  4202. /** TE STUFF END **/
  4203. /******************************************
  4204. *
  4205. * Asterisk Channel Endpoint END
  4206. *
  4207. *
  4208. *******************************************/
  4209. static int unload_module(void)
  4210. {
  4211. /* First, take us out of the channel loop */
  4212. ast_log(LOG_VERBOSE, "-- Unregistering mISDN Channel Driver --\n");
  4213. misdn_tasks_destroy();
  4214. if (!g_config_initialized)
  4215. return 0;
  4216. ast_cli_unregister_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
  4217. /* ast_unregister_application("misdn_crypt"); */
  4218. ast_unregister_application("misdn_set_opt");
  4219. ast_unregister_application("misdn_facility");
  4220. ast_unregister_application("misdn_check_l2l1");
  4221. ast_channel_unregister(&misdn_tech);
  4222. free_robin_list();
  4223. misdn_cfg_destroy();
  4224. misdn_lib_destroy();
  4225. free(misdn_out_calls);
  4226. free(misdn_in_calls);
  4227. free(misdn_debug_only);
  4228. free(misdn_ports);
  4229. free(misdn_debug);
  4230. return 0;
  4231. }
  4232. static int load_module(void)
  4233. {
  4234. int i, port;
  4235. int ntflags = 0, ntkc = 0;
  4236. char ports[256] = "";
  4237. char tempbuf[BUFFERSIZE + 1];
  4238. char ntfile[BUFFERSIZE + 1];
  4239. struct misdn_lib_iface iface = {
  4240. .cb_event = cb_events,
  4241. .cb_log = chan_misdn_log,
  4242. .cb_jb_empty = chan_misdn_jb_empty,
  4243. };
  4244. max_ports = misdn_lib_maxports_get();
  4245. if (max_ports <= 0) {
  4246. ast_log(LOG_ERROR, "Unable to initialize mISDN\n");
  4247. return AST_MODULE_LOAD_DECLINE;
  4248. }
  4249. if (misdn_cfg_init(max_ports)) {
  4250. ast_log(LOG_ERROR, "Unable to initialize misdn_config.\n");
  4251. return AST_MODULE_LOAD_DECLINE;
  4252. }
  4253. g_config_initialized = 1;
  4254. misdn_debug = (int *) malloc(sizeof(int) * (max_ports + 1));
  4255. if (!misdn_debug) {
  4256. ast_log(LOG_ERROR, "Out of memory for misdn_debug\n");
  4257. return AST_MODULE_LOAD_DECLINE;
  4258. }
  4259. misdn_ports = (int *) malloc(sizeof(int) * (max_ports + 1));
  4260. if (!misdn_ports) {
  4261. free(misdn_debug);
  4262. ast_log(LOG_ERROR, "Out of memory for misdn_ports\n");
  4263. return AST_MODULE_LOAD_DECLINE;
  4264. }
  4265. misdn_cfg_get(0, MISDN_GEN_DEBUG, &misdn_debug[0], sizeof(int));
  4266. for (i = 1; i <= max_ports; i++) {
  4267. misdn_debug[i] = misdn_debug[0];
  4268. misdn_ports[i] = i;
  4269. }
  4270. *misdn_ports = 0;
  4271. misdn_debug_only = (int *) calloc(max_ports + 1, sizeof(int));
  4272. if (!misdn_debug_only) {
  4273. free(misdn_ports);
  4274. free(misdn_debug);
  4275. ast_log(LOG_ERROR, "Out of memory for misdn_debug_only\n");
  4276. return AST_MODULE_LOAD_DECLINE;
  4277. }
  4278. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, tempbuf, BUFFERSIZE);
  4279. if (!ast_strlen_zero(tempbuf))
  4280. tracing = 1;
  4281. misdn_in_calls = (int *) malloc(sizeof(int) * (max_ports + 1));
  4282. if (!misdn_in_calls) {
  4283. free(misdn_debug_only);
  4284. free(misdn_ports);
  4285. free(misdn_debug);
  4286. ast_log(LOG_ERROR, "Out of memory for misdn_in_calls\n");
  4287. return AST_MODULE_LOAD_DECLINE;
  4288. }
  4289. misdn_out_calls = (int *) malloc(sizeof(int) * (max_ports + 1));
  4290. if (!misdn_out_calls) {
  4291. free(misdn_in_calls);
  4292. free(misdn_debug_only);
  4293. free(misdn_ports);
  4294. free(misdn_debug);
  4295. ast_log(LOG_ERROR, "Out of memory for misdn_out_calls\n");
  4296. return AST_MODULE_LOAD_DECLINE;
  4297. }
  4298. for (i = 1; i <= max_ports; i++) {
  4299. misdn_in_calls[i] = 0;
  4300. misdn_out_calls[i] = 0;
  4301. }
  4302. ast_mutex_init(&cl_te_lock);
  4303. ast_mutex_init(&release_lock);
  4304. misdn_cfg_update_ptp();
  4305. misdn_cfg_get_ports_string(ports);
  4306. if (!ast_strlen_zero(ports))
  4307. chan_misdn_log(0, 0, "Got: %s from get_ports\n", ports);
  4308. if (misdn_lib_init(ports, &iface, NULL))
  4309. chan_misdn_log(0, 0, "No te ports initialized\n");
  4310. misdn_cfg_get(0, MISDN_GEN_NTDEBUGFLAGS, &ntflags, sizeof(int));
  4311. misdn_cfg_get(0, MISDN_GEN_NTDEBUGFILE, &ntfile, BUFFERSIZE);
  4312. misdn_lib_nt_debug_init(ntflags, ntfile);
  4313. misdn_cfg_get( 0, MISDN_GEN_NTKEEPCALLS, &ntkc, sizeof(int));
  4314. misdn_lib_nt_keepcalls(ntkc);
  4315. if (ast_channel_register(&misdn_tech)) {
  4316. ast_log(LOG_ERROR, "Unable to register channel class %s\n", misdn_type);
  4317. unload_module();
  4318. return -1;
  4319. }
  4320. ast_cli_register_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
  4321. ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_opt",
  4322. "misdn_set_opt(:<opt><optarg>:<opt><optarg>...):\n"
  4323. "Sets mISDN opts. and optargs\n"
  4324. "\n"
  4325. "The available options are:\n"
  4326. " a - Have Asterisk detect DTMF tones on called channel\n"
  4327. " c - Make crypted outgoing call, optarg is keyindex\n"
  4328. " d - Send display text to called phone, text is the optarg\n"
  4329. " e - Perform echo cancellation on this channel,\n"
  4330. " takes taps as optarg (32,64,128,256)\n"
  4331. " e! - Disable echo cancellation on this channel\n"
  4332. " f - Enable fax detection\n"
  4333. " h - Make digital outgoing call\n"
  4334. " h1 - Make HDLC mode digital outgoing call\n"
  4335. " i - Ignore detected DTMF tones, don't signal them to Asterisk,\n"
  4336. " they will be transported inband.\n"
  4337. " jb - Set jitter buffer length, optarg is length\n"
  4338. " jt - Set jitter buffer upper threshold, optarg is threshold\n"
  4339. " jn - Disable jitter buffer\n"
  4340. " n - Disable mISDN DSP on channel.\n"
  4341. " Disables: echo cancel, DTMF detection, and volume control.\n"
  4342. " p - Caller ID presentation,\n"
  4343. " optarg is either 'allowed' or 'restricted'\n"
  4344. " s - Send Non-inband DTMF as inband\n"
  4345. " vr - Rx gain control, optarg is gain\n"
  4346. " vt - Tx gain control, optarg is gain\n"
  4347. );
  4348. ast_register_application("misdn_facility", misdn_facility_exec, "misdn_facility",
  4349. "misdn_facility(<FACILITY_TYPE>|<ARG1>|..)\n"
  4350. "Sends the Facility Message FACILITY_TYPE with \n"
  4351. "the given Arguments to the current ISDN Channel\n"
  4352. "Supported Facilities are:\n"
  4353. "\n"
  4354. "type=calldeflect args=Nr where to deflect\n"
  4355. );
  4356. ast_register_application("misdn_check_l2l1", misdn_check_l2l1, "misdn_check_l2l1",
  4357. "misdn_check_l2l1(<port>||g:<groupname>,timeout)"
  4358. "Checks if the L2 and L1 are up on either the given <port> or\n"
  4359. "on the ports in the group with <groupname>\n"
  4360. "If the L1/L2 are down, check_l2l1 gets up the L1/L2 and waits\n"
  4361. "for <timeout> seconds that this happens. Otherwise, nothing happens\n"
  4362. "\n"
  4363. "This application, ensures the L1/L2 state of the Ports in a group\n"
  4364. "it is intended to make the pmp_l1_check option redundant and to\n"
  4365. "fix a buggy switch config from your provider\n"
  4366. "\n"
  4367. "a sample dialplan would look like:\n\n"
  4368. "exten => _X.,1,misdn_check_l2l1(g:out|2)\n"
  4369. "exten => _X.,n,dial(mISDN/g:out/${EXTEN})\n"
  4370. "\n"
  4371. );
  4372. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
  4373. /* start the l1 watchers */
  4374. for (port = misdn_cfg_get_next_port(0); port >= 0; port = misdn_cfg_get_next_port(port)) {
  4375. int l1timeout;
  4376. misdn_cfg_get(port, MISDN_CFG_L1_TIMEOUT, &l1timeout, sizeof(l1timeout));
  4377. if (l1timeout) {
  4378. chan_misdn_log(4, 0, "Adding L1watcher task: port:%d timeout:%ds\n", port, l1timeout);
  4379. misdn_tasks_add(l1timeout * 1000, misdn_l1_task, &misdn_ports[port]);
  4380. }
  4381. }
  4382. chan_misdn_log(0, 0, "-- mISDN Channel Driver Registered --\n");
  4383. return 0;
  4384. }
  4385. static int reload(void)
  4386. {
  4387. reload_config();
  4388. return 0;
  4389. }
  4390. /*** SOME APPS ;)***/
  4391. static int misdn_facility_exec(struct ast_channel *chan, void *data)
  4392. {
  4393. struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
  4394. char *tok, *tokb;
  4395. chan_misdn_log(0, 0, "TYPE: %s\n", chan->tech->type);
  4396. if (strcasecmp(chan->tech->type, "mISDN")) {
  4397. ast_log(LOG_WARNING, "misdn_facility makes only sense with chan_misdn channels!\n");
  4398. return -1;
  4399. }
  4400. if (ast_strlen_zero((char *)data)) {
  4401. ast_log(LOG_WARNING, "misdn_facility Requires arguments\n");
  4402. return -1;
  4403. }
  4404. tok = strtok_r((char*) data, "|", &tokb) ;
  4405. if (!tok) {
  4406. ast_log(LOG_WARNING, "misdn_facility Requires arguments\n");
  4407. return -1;
  4408. }
  4409. if (!strcasecmp(tok, "calldeflect")) {
  4410. tok = strtok_r(NULL, "|", &tokb) ;
  4411. if (!tok) {
  4412. ast_log(LOG_WARNING, "Facility: Call Defl Requires arguments\n");
  4413. }
  4414. if (strlen(tok) >= sizeof(ch->bc->fac_out.u.CDeflection.DeflectedToNumber)) {
  4415. ast_log(LOG_WARNING, "Facility: Number argument too long (up to 15 digits are allowed). Ignoring.\n");
  4416. return 0;
  4417. }
  4418. ch->bc->fac_out.Function = Fac_CD;
  4419. ast_copy_string((char *)ch->bc->fac_out.u.CDeflection.DeflectedToNumber, tok, sizeof(ch->bc->fac_out.u.CDeflection.DeflectedToNumber));
  4420. misdn_lib_send_event(ch->bc, EVENT_FACILITY);
  4421. } else {
  4422. chan_misdn_log(1, ch->bc->port, "Unknown Facility: %s\n", tok);
  4423. }
  4424. return 0;
  4425. }
  4426. static int misdn_check_l2l1(struct ast_channel *chan, void *data)
  4427. {
  4428. char group[BUFFERSIZE + 1];
  4429. char *port_str;
  4430. int port = 0;
  4431. int timeout;
  4432. int dowait = 0;
  4433. int port_up;
  4434. AST_DECLARE_APP_ARGS(args,
  4435. AST_APP_ARG(grouppar);
  4436. AST_APP_ARG(timeout);
  4437. );
  4438. if (ast_strlen_zero((char *)data)) {
  4439. ast_log(LOG_WARNING, "misdn_check_l2l1 Requires arguments\n");
  4440. return -1;
  4441. }
  4442. AST_STANDARD_APP_ARGS(args, data);
  4443. if (args.argc != 2) {
  4444. ast_log(LOG_WARNING, "Wrong argument count\n");
  4445. return 0;
  4446. }
  4447. /*ast_log(LOG_NOTICE, "Arguments: group/port '%s' timeout '%s'\n", args.grouppar, args.timeout);*/
  4448. timeout = atoi(args.timeout);
  4449. port_str = args.grouppar;
  4450. if (port_str[0] == 'g' && port_str[1] == ':' ) {
  4451. /* We make a group call lets checkout which ports are in my group */
  4452. port_str += 2;
  4453. ast_copy_string(group, port_str, sizeof(group));
  4454. chan_misdn_log(2, 0, "Checking Ports in group: %s\n", group);
  4455. for ( port = misdn_cfg_get_next_port(port);
  4456. port > 0;
  4457. port = misdn_cfg_get_next_port(port)) {
  4458. char cfg_group[BUFFERSIZE + 1];
  4459. chan_misdn_log(2, 0, "trying port %d\n", port);
  4460. misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE);
  4461. if (!strcasecmp(cfg_group, group)) {
  4462. port_up = misdn_lib_port_up(port, 1);
  4463. if (!port_up) {
  4464. chan_misdn_log(2, 0, " --> port '%d'\n", port);
  4465. misdn_lib_get_port_up(port);
  4466. dowait = 1;
  4467. }
  4468. }
  4469. }
  4470. } else {
  4471. port = atoi(port_str);
  4472. chan_misdn_log(2, 0, "Checking Port: %d\n",port);
  4473. port_up = misdn_lib_port_up(port, 1);
  4474. if (!port_up) {
  4475. misdn_lib_get_port_up(port);
  4476. dowait = 1;
  4477. }
  4478. }
  4479. if (dowait) {
  4480. chan_misdn_log(2, 0, "Waiting for '%d' seconds\n", timeout);
  4481. sleep(timeout);
  4482. }
  4483. return 0;
  4484. }
  4485. static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
  4486. {
  4487. struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
  4488. char *tok, *tokb;
  4489. int keyidx = 0;
  4490. int rxgain = 0;
  4491. int txgain = 0;
  4492. int change_jitter = 0;
  4493. if (strcasecmp(chan->tech->type, "mISDN")) {
  4494. ast_log(LOG_WARNING, "misdn_set_opt makes only sense with chan_misdn channels!\n");
  4495. return -1;
  4496. }
  4497. if (ast_strlen_zero((char *)data)) {
  4498. ast_log(LOG_WARNING, "misdn_set_opt Requires arguments\n");
  4499. return -1;
  4500. }
  4501. for (tok = strtok_r((char*) data, ":", &tokb);
  4502. tok;
  4503. tok = strtok_r(NULL, ":", &tokb) ) {
  4504. int neglect = 0;
  4505. if (tok[0] == '!' ) {
  4506. neglect = 1;
  4507. tok++;
  4508. }
  4509. switch(tok[0]) {
  4510. case 'd' :
  4511. ast_copy_string(ch->bc->display, ++tok, sizeof(ch->bc->display));
  4512. chan_misdn_log(1, ch->bc->port, "SETOPT: Display:%s\n", ch->bc->display);
  4513. break;
  4514. case 'n':
  4515. chan_misdn_log(1, ch->bc->port, "SETOPT: No DSP\n");
  4516. ch->bc->nodsp = 1;
  4517. break;
  4518. case 'j':
  4519. chan_misdn_log(1, ch->bc->port, "SETOPT: jitter\n");
  4520. tok++;
  4521. change_jitter = 1;
  4522. switch ( tok[0] ) {
  4523. case 'b':
  4524. ch->jb_len = atoi(++tok);
  4525. chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d\n", ch->jb_len);
  4526. break;
  4527. case 't' :
  4528. ch->jb_upper_threshold = atoi(++tok);
  4529. chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d\n", ch->jb_upper_threshold);
  4530. break;
  4531. case 'n':
  4532. ch->bc->nojitter = 1;
  4533. chan_misdn_log(1, ch->bc->port, " --> nojitter\n");
  4534. break;
  4535. default:
  4536. ch->jb_len = 4000;
  4537. ch->jb_upper_threshold = 0;
  4538. chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d (default)\n", ch->jb_len);
  4539. chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d (default)\n", ch->jb_upper_threshold);
  4540. }
  4541. break;
  4542. case 'v':
  4543. tok++;
  4544. switch (tok[0]) {
  4545. case 'r' :
  4546. rxgain = atoi(++tok);
  4547. if (rxgain < -8)
  4548. rxgain = -8;
  4549. if (rxgain > 8)
  4550. rxgain = 8;
  4551. ch->bc->rxgain = rxgain;
  4552. chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", rxgain);
  4553. break;
  4554. case 't':
  4555. txgain = atoi(++tok);
  4556. if (txgain < -8)
  4557. txgain = -8;
  4558. if (txgain > 8)
  4559. txgain = 8;
  4560. ch->bc->txgain = txgain;
  4561. chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", txgain);
  4562. break;
  4563. }
  4564. break;
  4565. case 'c':
  4566. keyidx = atoi(++tok);
  4567. {
  4568. char keys[4096];
  4569. char *key = NULL, *tmp = keys;
  4570. int i;
  4571. misdn_cfg_get(0, MISDN_GEN_CRYPT_KEYS, keys, sizeof(keys));
  4572. for (i = 0; i < keyidx; i++) {
  4573. key = strsep(&tmp, ",");
  4574. }
  4575. if (key) {
  4576. ast_copy_string(ch->bc->crypt_key, key, sizeof(ch->bc->crypt_key));
  4577. }
  4578. chan_misdn_log(0, ch->bc->port, "SETOPT: crypt with key:%s\n", ch->bc->crypt_key);
  4579. break;
  4580. }
  4581. case 'e':
  4582. chan_misdn_log(1, ch->bc->port, "SETOPT: EchoCancel\n");
  4583. if (neglect) {
  4584. chan_misdn_log(1, ch->bc->port, " --> disabled\n");
  4585. #ifdef MISDN_1_2
  4586. *ch->bc->pipeline = 0;
  4587. #else
  4588. ch->bc->ec_enable = 0;
  4589. #endif
  4590. } else {
  4591. #ifdef MISDN_1_2
  4592. update_pipeline_config(ch->bc);
  4593. #else
  4594. ch->bc->ec_enable = 1;
  4595. ch->bc->orig = ch->originator;
  4596. tok++;
  4597. if (*tok) {
  4598. ch->bc->ec_deftaps = atoi(tok);
  4599. }
  4600. #endif
  4601. }
  4602. break;
  4603. case 'h':
  4604. chan_misdn_log(1, ch->bc->port, "SETOPT: Digital\n");
  4605. if (strlen(tok) > 1 && tok[1] == '1') {
  4606. chan_misdn_log(1, ch->bc->port, "SETOPT: HDLC \n");
  4607. if (!ch->bc->hdlc) {
  4608. ch->bc->hdlc = 1;
  4609. }
  4610. }
  4611. ch->bc->capability = INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
  4612. break;
  4613. case 's':
  4614. chan_misdn_log(1, ch->bc->port, "SETOPT: Send DTMF\n");
  4615. ch->bc->send_dtmf = 1;
  4616. break;
  4617. case 'f':
  4618. chan_misdn_log(1, ch->bc->port, "SETOPT: Faxdetect\n");
  4619. ch->faxdetect = 1;
  4620. misdn_cfg_get(ch->bc->port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout));
  4621. break;
  4622. case 'a':
  4623. chan_misdn_log(1, ch->bc->port, "SETOPT: AST_DSP (for DTMF)\n");
  4624. ch->ast_dsp = 1;
  4625. break;
  4626. case 'p':
  4627. chan_misdn_log(1, ch->bc->port, "SETOPT: callerpres: %s\n", &tok[1]);
  4628. /* CRICH: callingpres!!! */
  4629. if (strstr(tok,"allowed")) {
  4630. ch->bc->pres = 0;
  4631. } else if (strstr(tok, "restricted")) {
  4632. ch->bc->pres = 1;
  4633. } else if (strstr(tok, "not_screened")) {
  4634. chan_misdn_log(0, ch->bc->port, "SETOPT: callerpres: not_screened is deprecated\n");
  4635. ch->bc->pres = 1;
  4636. }
  4637. break;
  4638. case 'i' :
  4639. chan_misdn_log(1, ch->bc->port, "Ignoring dtmf tones, just use them inband\n");
  4640. ch->ignore_dtmf=1;
  4641. break;
  4642. default:
  4643. break;
  4644. }
  4645. }
  4646. if (change_jitter)
  4647. config_jitterbuffer(ch);
  4648. if (ch->faxdetect || ch->ast_dsp) {
  4649. if (!ch->dsp)
  4650. ch->dsp = ast_dsp_new();
  4651. if (ch->dsp)
  4652. ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
  4653. }
  4654. if (ch->ast_dsp) {
  4655. chan_misdn_log(1, ch->bc->port, "SETOPT: with AST_DSP we deactivate mISDN_dsp\n");
  4656. ch->bc->nodsp = 1;
  4657. }
  4658. return 0;
  4659. }
  4660. int chan_misdn_jb_empty ( struct misdn_bchannel *bc, char *buf, int len)
  4661. {
  4662. struct chan_list *ch = find_chan_by_bc(cl_te, bc);
  4663. if (ch && ch->jb) {
  4664. return misdn_jb_empty(ch->jb, buf, len);
  4665. }
  4666. return -1;
  4667. }
  4668. /*******************************************************/
  4669. /***************** JITTERBUFFER ************************/
  4670. /*******************************************************/
  4671. /* allocates the jb-structure and initialize the elements*/
  4672. struct misdn_jb *misdn_jb_init(int size, int upper_threshold)
  4673. {
  4674. int i;
  4675. struct misdn_jb *jb;
  4676. jb = malloc(sizeof(struct misdn_jb));
  4677. if (!jb) {
  4678. chan_misdn_log(-1, 0, "No free Mem for jb\n");
  4679. return NULL;
  4680. }
  4681. jb->size = size;
  4682. jb->upper_threshold = upper_threshold;
  4683. jb->wp = 0;
  4684. jb->rp = 0;
  4685. jb->state_full = 0;
  4686. jb->state_empty = 0;
  4687. jb->bytes_wrote = 0;
  4688. jb->samples = malloc(size * sizeof(char));
  4689. if (!jb->samples) {
  4690. free(jb);
  4691. chan_misdn_log(-1, 0, "No free Mem for jb->samples\n");
  4692. return NULL;
  4693. }
  4694. jb->ok = malloc(size * sizeof(char));
  4695. if (!jb->ok) {
  4696. free(jb->samples);
  4697. free(jb);
  4698. chan_misdn_log(-1, 0, "No free Mem for jb->ok\n");
  4699. return NULL;
  4700. }
  4701. for (i = 0; i < size; i++)
  4702. jb->ok[i] = 0;
  4703. ast_mutex_init(&jb->mutexjb);
  4704. return jb;
  4705. }
  4706. /* frees the data and destroys the given jitterbuffer struct */
  4707. void misdn_jb_destroy(struct misdn_jb *jb)
  4708. {
  4709. ast_mutex_destroy(&jb->mutexjb);
  4710. free(jb->ok);
  4711. free(jb->samples);
  4712. free(jb);
  4713. }
  4714. /* fills the jitterbuffer with len data returns < 0 if there was an
  4715. error (buffer overflow). */
  4716. int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len)
  4717. {
  4718. int i, j, rp, wp;
  4719. if (!jb || ! data)
  4720. return 0;
  4721. ast_mutex_lock(&jb->mutexjb);
  4722. wp = jb->wp;
  4723. rp = jb->rp;
  4724. for (i = 0; i < len; i++) {
  4725. jb->samples[wp] = data[i];
  4726. jb->ok[wp] = 1;
  4727. wp = (wp != jb->size - 1) ? wp + 1 : 0;
  4728. if (wp == jb->rp)
  4729. jb->state_full = 1;
  4730. }
  4731. if (wp >= rp)
  4732. jb->state_buffer = wp - rp;
  4733. else
  4734. jb->state_buffer = jb->size - rp + wp;
  4735. chan_misdn_log(9, 0, "misdn_jb_fill: written:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
  4736. if (jb->state_full) {
  4737. jb->wp = wp;
  4738. rp = wp;
  4739. for (j = 0; j < jb->upper_threshold; j++)
  4740. rp = (rp != 0) ? rp - 1 : jb->size - 1;
  4741. jb->rp = rp;
  4742. jb->state_full = 0;
  4743. jb->state_empty = 1;
  4744. ast_mutex_unlock(&jb->mutexjb);
  4745. return -1;
  4746. }
  4747. if (!jb->state_empty) {
  4748. jb->bytes_wrote += len;
  4749. if (jb->bytes_wrote >= jb->upper_threshold) {
  4750. jb->state_empty = 1;
  4751. jb->bytes_wrote = 0;
  4752. }
  4753. }
  4754. jb->wp = wp;
  4755. ast_mutex_unlock(&jb->mutexjb);
  4756. return 0;
  4757. }
  4758. /* gets len bytes out of the jitterbuffer if available, else only the
  4759. available data is returned and the return value indicates the number
  4760. of data. */
  4761. int misdn_jb_empty(struct misdn_jb *jb, char *data, int len)
  4762. {
  4763. int i, wp, rp, read = 0;
  4764. ast_mutex_lock(&jb->mutexjb);
  4765. rp = jb->rp;
  4766. wp = jb->wp;
  4767. if (jb->state_empty) {
  4768. for (i = 0; i < len; i++) {
  4769. if (wp == rp) {
  4770. jb->rp = rp;
  4771. jb->state_empty = 0;
  4772. ast_mutex_unlock(&jb->mutexjb);
  4773. return read;
  4774. } else {
  4775. if (jb->ok[rp] == 1) {
  4776. data[i] = jb->samples[rp];
  4777. jb->ok[rp] = 0;
  4778. rp = (rp != jb->size - 1) ? rp + 1 : 0;
  4779. read += 1;
  4780. }
  4781. }
  4782. }
  4783. if (wp >= rp)
  4784. jb->state_buffer = wp - rp;
  4785. else
  4786. jb->state_buffer = jb->size - rp + wp;
  4787. chan_misdn_log(9, 0, "misdn_jb_empty: read:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
  4788. jb->rp = rp;
  4789. } else
  4790. chan_misdn_log(9, 0, "misdn_jb_empty: Wait...requested:%d p:%p\n", len, jb);
  4791. ast_mutex_unlock(&jb->mutexjb);
  4792. return read;
  4793. }
  4794. /*******************************************************/
  4795. /*************** JITTERBUFFER END *********************/
  4796. /*******************************************************/
  4797. static void chan_misdn_log(int level, int port, char *tmpl, ...)
  4798. {
  4799. va_list ap;
  4800. char buf[1024];
  4801. char port_buf[8];
  4802. if (! ((0 <= port) && (port <= max_ports))) {
  4803. ast_log(LOG_WARNING, "cb_log called with out-of-range port number! (%d)\n", port);
  4804. port = 0;
  4805. level = -1;
  4806. }
  4807. snprintf(port_buf, sizeof(port_buf), "P[%2d] ", port);
  4808. va_start(ap, tmpl);
  4809. vsnprintf(buf, sizeof(buf), tmpl, ap);
  4810. va_end(ap);
  4811. if (level == -1)
  4812. ast_log(LOG_WARNING, "%s", buf);
  4813. else if (misdn_debug_only[port] ?
  4814. (level == 1 && misdn_debug[port]) || (level == misdn_debug[port])
  4815. : level <= misdn_debug[port]) {
  4816. ast_console_puts(port_buf);
  4817. ast_console_puts(buf);
  4818. }
  4819. if ((level <= misdn_debug[0]) && !ast_strlen_zero(global_tracefile) ) {
  4820. time_t tm = time(NULL);
  4821. char *tmp = ctime(&tm), *p;
  4822. FILE *fp = fopen(global_tracefile, "a+");
  4823. p = strchr(tmp, '\n');
  4824. if (p)
  4825. *p = ':';
  4826. if (!fp) {
  4827. ast_console_puts("Error opening Tracefile: [ ");
  4828. ast_console_puts(global_tracefile);
  4829. ast_console_puts(" ] ");
  4830. ast_console_puts(strerror(errno));
  4831. ast_console_puts("\n");
  4832. return ;
  4833. }
  4834. fputs(tmp, fp);
  4835. fputs(" ", fp);
  4836. fputs(port_buf, fp);
  4837. fputs(" ", fp);
  4838. fputs(buf, fp);
  4839. fclose(fp);
  4840. }
  4841. }
  4842. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for mISDN Support (BRI/PRI)",
  4843. .load = load_module,
  4844. .unload = unload_module,
  4845. .reload = reload,
  4846. );