chan_misdn.c 157 KB

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