chan_misdn.c 378 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696
  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. /*!
  31. * \note
  32. * To use the CCBS/CCNR supplementary service feature and other
  33. * supplementary services using FACILITY messages requires a
  34. * modified version of mISDN.
  35. *
  36. * \note
  37. * The latest modified mISDN v1.1.x based version is available at:
  38. * http://svn.digium.com/svn/thirdparty/mISDN/trunk
  39. * http://svn.digium.com/svn/thirdparty/mISDNuser/trunk
  40. *
  41. * \note
  42. * Taged versions of the modified mISDN code are available under:
  43. * http://svn.digium.com/svn/thirdparty/mISDN/tags
  44. * http://svn.digium.com/svn/thirdparty/mISDNuser/tags
  45. */
  46. /* Define to enable cli commands to generate canned CCBS messages. */
  47. // #define CCBS_TEST_MESSAGES 1
  48. /*** MODULEINFO
  49. <depend>isdnnet</depend>
  50. <depend>misdn</depend>
  51. <depend>suppserv</depend>
  52. <support_level>extended</support_level>
  53. ***/
  54. #include "asterisk.h"
  55. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  56. #include <pthread.h>
  57. #include <sys/socket.h>
  58. #include <sys/time.h>
  59. #include <arpa/inet.h>
  60. #include <fcntl.h>
  61. #include <sys/ioctl.h>
  62. #include <signal.h>
  63. #include <sys/file.h>
  64. #include <semaphore.h>
  65. #include <ctype.h>
  66. #include <time.h>
  67. #include "asterisk/channel.h"
  68. #include "asterisk/config.h"
  69. #include "asterisk/module.h"
  70. #include "asterisk/pbx.h"
  71. #include "asterisk/io.h"
  72. #include "asterisk/frame.h"
  73. #include "asterisk/translate.h"
  74. #include "asterisk/cli.h"
  75. #include "asterisk/musiconhold.h"
  76. #include "asterisk/dsp.h"
  77. #include "asterisk/file.h"
  78. #include "asterisk/callerid.h"
  79. #include "asterisk/indications.h"
  80. #include "asterisk/app.h"
  81. #include "asterisk/features.h"
  82. #include "asterisk/term.h"
  83. #include "asterisk/sched.h"
  84. #include "asterisk/stringfields.h"
  85. #include "asterisk/abstract_jb.h"
  86. #include "asterisk/causes.h"
  87. #include "chan_misdn_config.h"
  88. #include "isdn_lib.h"
  89. static char global_tracefile[BUFFERSIZE + 1];
  90. static int g_config_initialized = 0;
  91. struct misdn_jb{
  92. int size;
  93. int upper_threshold;
  94. char *samples, *ok;
  95. int wp,rp;
  96. int state_empty;
  97. int state_full;
  98. int state_buffer;
  99. int bytes_wrote;
  100. ast_mutex_t mutexjb;
  101. };
  102. /*! \brief allocates the jb-structure and initialize the elements */
  103. struct misdn_jb *misdn_jb_init(int size, int upper_threshold);
  104. /*! \brief frees the data and destroys the given jitterbuffer struct */
  105. void misdn_jb_destroy(struct misdn_jb *jb);
  106. /*! \brief fills the jitterbuffer with len data returns < 0 if there was an
  107. error (buffer overrun). */
  108. int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len);
  109. /*! \brief gets len bytes out of the jitterbuffer if available, else only the
  110. available data is returned and the return value indicates the number
  111. of data. */
  112. int misdn_jb_empty(struct misdn_jb *jb, char *data, int len);
  113. static char *complete_ch(struct ast_cli_args *a);
  114. static char *complete_debug_port(struct ast_cli_args *a);
  115. static char *complete_show_config(struct ast_cli_args *a);
  116. /* BEGIN: chan_misdn.h */
  117. #if defined(AST_MISDN_ENHANCEMENTS)
  118. /*
  119. * This timeout duration is to clean up any call completion records that
  120. * are forgotten about by the switch.
  121. */
  122. #define MISDN_CC_RECORD_AGE_MAX (6UL * 60 * 60) /* seconds */
  123. #define MISDN_CC_REQUEST_WAIT_MAX 5 /* seconds */
  124. /*!
  125. * \brief Caller that initialized call completion services
  126. *
  127. * \details
  128. * This data is the payload for a datastore that is put on the channel that
  129. * initializes call completion services. This datastore is set to be inherited
  130. * by the outbound mISDN channel. When one of these channels hangs up, the
  131. * channel pointer will be set to NULL. That way, we can ensure that we do not
  132. * touch this channel after it gets destroyed.
  133. */
  134. struct misdn_cc_caller {
  135. /*! \brief The channel that initialized call completion services */
  136. struct ast_channel *chan;
  137. };
  138. struct misdn_cc_notify {
  139. /*! \brief Dialplan: Notify extension priority */
  140. int priority;
  141. /*! \brief Dialplan: Notify extension context */
  142. char context[AST_MAX_CONTEXT];
  143. /*! \brief Dialplan: Notify extension number (User-A) */
  144. char exten[AST_MAX_EXTENSION];
  145. };
  146. /*! \brief mISDN call completion record */
  147. struct misdn_cc_record {
  148. /*! \brief Call completion record linked list */
  149. AST_LIST_ENTRY(misdn_cc_record) list;
  150. /*! \brief Time the record was created. */
  151. time_t time_created;
  152. /*! \brief MISDN_CC_RECORD_ID value */
  153. long record_id;
  154. /*!
  155. * \brief Logical Layer 1 port associated with this
  156. * call completion record
  157. */
  158. int port;
  159. /*! \brief TRUE if point-to-point mode (CCBS-T/CCNR-T mode) */
  160. int ptp;
  161. /*! \brief Mode specific parameters */
  162. union {
  163. /*! \brief point-to-point specific parameters. */
  164. struct {
  165. /*!
  166. * \brief Call-completion signaling link.
  167. * NULL if signaling link not established.
  168. */
  169. struct misdn_bchannel *bc;
  170. /*!
  171. * \brief TRUE if we requested the request retention option
  172. * to be enabled.
  173. */
  174. int requested_retention;
  175. /*!
  176. * \brief TRUE if the request retention option is enabled.
  177. */
  178. int retention_enabled;
  179. } ptp;
  180. /*! \brief point-to-multi-point specific parameters. */
  181. struct {
  182. /*! \brief CallLinkageID (valid when port determined) */
  183. int linkage_id;
  184. /*! \breif CCBSReference (valid when activated is TRUE) */
  185. int reference_id;
  186. /*! \brief globalRecall(0), specificRecall(1) */
  187. int recall_mode;
  188. } ptmp;
  189. } mode;
  190. /*! \brief TRUE if call completion activated */
  191. int activated;
  192. /*! \brief Outstanding message ID (valid when outstanding_message) */
  193. int invoke_id;
  194. /*! \brief TRUE if waiting for a response from a message (invoke_id is valid) */
  195. int outstanding_message;
  196. /*! \brief TRUE if activation has been requested */
  197. int activation_requested;
  198. /*!
  199. * \brief TRUE if User-A is free
  200. * \note PTMP - Used to answer CCBSStatusRequest.
  201. * PTP - Determines how to respond to CCBS_T_RemoteUserFree.
  202. */
  203. int party_a_free;
  204. /*! \brief Error code received from last outstanding message. */
  205. enum FacErrorCode error_code;
  206. /*! \brief Reject code received from last outstanding message. */
  207. enum FacRejectCode reject_code;
  208. /*!
  209. * \brief Saved struct misdn_bchannel call information when
  210. * attempted to call User-B
  211. */
  212. struct {
  213. /*! \brief User-A caller id information */
  214. struct misdn_party_id caller;
  215. /*! \brief User-B number information */
  216. struct misdn_party_dialing dialed;
  217. /*! \brief The BC, HLC (optional) and LLC (optional) contents from the SETUP message. */
  218. struct Q931_Bc_Hlc_Llc setup_bc_hlc_llc;
  219. /*! \brief SETUP message bearer capability field code value */
  220. int capability;
  221. /*! \brief TRUE if call made in digital HDLC mode */
  222. int hdlc;
  223. } redial;
  224. /*! \brief Dialplan location to indicate User-B free and User-A is free */
  225. struct misdn_cc_notify remote_user_free;
  226. /*! \brief Dialplan location to indicate User-B free and User-A is busy */
  227. struct misdn_cc_notify b_free;
  228. };
  229. /*! \brief mISDN call completion record database */
  230. static AST_LIST_HEAD_STATIC(misdn_cc_records_db, misdn_cc_record);
  231. /*! \brief Next call completion record ID to use */
  232. static __u16 misdn_cc_record_id;
  233. /*! \brief Next invoke ID to use */
  234. static __s16 misdn_invoke_id;
  235. static const char misdn_no_response_from_network[] = "No response from network";
  236. static const char misdn_cc_record_not_found[] = "Call completion record not found";
  237. /* mISDN channel variable names */
  238. #define MISDN_CC_RECORD_ID "MISDN_CC_RECORD_ID"
  239. #define MISDN_CC_STATUS "MISDN_CC_STATUS"
  240. #define MISDN_ERROR_MSG "MISDN_ERROR_MSG"
  241. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  242. static ast_mutex_t release_lock;
  243. enum misdn_chan_state {
  244. MISDN_NOTHING = 0, /*!< at beginning */
  245. MISDN_WAITING4DIGS, /*!< when waiting for info */
  246. MISDN_EXTCANTMATCH, /*!< when asterisk couldn't match our ext */
  247. MISDN_INCOMING_SETUP, /*!< for incoming setup */
  248. MISDN_DIALING, /*!< when pbx_start */
  249. MISDN_PROGRESS, /*!< we have progress */
  250. MISDN_PROCEEDING, /*!< we have progress */
  251. MISDN_CALLING, /*!< when misdn_call is called */
  252. MISDN_CALLING_ACKNOWLEDGE, /*!< when we get SETUP_ACK */
  253. MISDN_ALERTING, /*!< when Alerting */
  254. MISDN_BUSY, /*!< when BUSY */
  255. MISDN_CONNECTED, /*!< when connected */
  256. MISDN_DISCONNECTED, /*!< when connected */
  257. MISDN_CLEANING, /*!< when hangup from * but we were connected before */
  258. };
  259. /*! Asterisk created the channel (outgoing call) */
  260. #define ORG_AST 1
  261. /*! mISDN created the channel (incoming call) */
  262. #define ORG_MISDN 2
  263. enum misdn_hold_state {
  264. MISDN_HOLD_IDLE, /*!< HOLD not active */
  265. MISDN_HOLD_ACTIVE, /*!< Call is held */
  266. MISDN_HOLD_TRANSFER, /*!< Held call is being transferred */
  267. MISDN_HOLD_DISCONNECT, /*!< Held call is being disconnected */
  268. };
  269. struct hold_info {
  270. /*!
  271. * \brief Call HOLD state.
  272. */
  273. enum misdn_hold_state state;
  274. /*!
  275. * \brief Logical port the channel call record is HELD on
  276. * because the B channel is no longer associated.
  277. */
  278. int port;
  279. /*!
  280. * \brief Original B channel number the HELD call was using.
  281. * \note Used only for debug display messages.
  282. */
  283. int channel;
  284. };
  285. #define chan_list_ref(obj, debug) (ao2_t_ref((obj), +1, (debug)), (obj))
  286. #define chan_list_unref(obj, debug) (ao2_t_ref((obj), -1, (debug)), NULL)
  287. /*!
  288. * \brief Channel call record structure
  289. */
  290. struct chan_list {
  291. /*!
  292. * \brief The "allowed_bearers" string read in from /etc/asterisk/misdn.conf
  293. */
  294. char allowed_bearers[BUFFERSIZE + 1];
  295. /*!
  296. * \brief State of the channel
  297. */
  298. enum misdn_chan_state state;
  299. /*!
  300. * \brief TRUE if a hangup needs to be queued
  301. * \note This is a debug flag only used to catch calls to hangup_chan() that are already hungup.
  302. */
  303. int need_queue_hangup;
  304. /*!
  305. * \brief TRUE if a channel can be hung up by calling asterisk directly when done.
  306. */
  307. int need_hangup;
  308. /*!
  309. * \brief TRUE if we could send an AST_CONTROL_BUSY if needed.
  310. */
  311. int need_busy;
  312. /*!
  313. * \brief Who originally created this channel. ORG_AST or ORG_MISDN
  314. */
  315. int originator;
  316. /*!
  317. * \brief TRUE of we are not to respond immediately to a SETUP message. Check the dialplan first.
  318. * \note The "noautorespond_on_setup" boolean read in from /etc/asterisk/misdn.conf
  319. */
  320. int noautorespond_on_setup;
  321. int norxtone; /*!< Boolean assigned values but the value is not used. */
  322. /*!
  323. * \brief TRUE if we are not to generate tones (Playtones)
  324. */
  325. int notxtone;
  326. /*!
  327. * \brief TRUE if echo canceller is enabled. Value is toggled.
  328. */
  329. int toggle_ec;
  330. /*!
  331. * \brief TRUE if you want to send Tone Indications to an incoming
  332. * ISDN channel on a TE Port.
  333. * \note The "incoming_early_audio" boolean read in from /etc/asterisk/misdn.conf
  334. */
  335. int incoming_early_audio;
  336. /*!
  337. * \brief TRUE if DTMF digits are to be passed inband only.
  338. * \note It is settable by the misdn_set_opt() application.
  339. */
  340. int ignore_dtmf;
  341. /*!
  342. * \brief Pipe file descriptor handles array.
  343. * Read from pipe[0], write to pipe[1]
  344. */
  345. int pipe[2];
  346. /*!
  347. * \brief Read buffer for inbound audio from pipe[0]
  348. */
  349. char ast_rd_buf[4096];
  350. /*!
  351. * \brief Inbound audio frame returned by misdn_read().
  352. */
  353. struct ast_frame frame;
  354. /*!
  355. * \brief Fax detection option. (0:no 1:yes 2:yes+nojump)
  356. * \note The "faxdetect" option string read in from /etc/asterisk/misdn.conf
  357. * \note It is settable by the misdn_set_opt() application.
  358. */
  359. int faxdetect;
  360. /*!
  361. * \brief Number of seconds to detect a Fax machine when detection enabled.
  362. * \note 0 disables the timeout.
  363. * \note The "faxdetect_timeout" value read in from /etc/asterisk/misdn.conf
  364. */
  365. int faxdetect_timeout;
  366. /*!
  367. * \brief Starting time of fax detection with timeout when nonzero.
  368. */
  369. struct timeval faxdetect_tv;
  370. /*!
  371. * \brief TRUE if a fax has been detected.
  372. */
  373. int faxhandled;
  374. /*!
  375. * \brief TRUE if we will use the Asterisk DSP to detect DTMF/Fax
  376. * \note The "astdtmf" boolean read in from /etc/asterisk/misdn.conf
  377. */
  378. int ast_dsp;
  379. /*!
  380. * \brief Jitterbuffer length
  381. * \note The "jitterbuffer" value read in from /etc/asterisk/misdn.conf
  382. */
  383. int jb_len;
  384. /*!
  385. * \brief Jitterbuffer upper threshold
  386. * \note The "jitterbuffer_upper_threshold" value read in from /etc/asterisk/misdn.conf
  387. */
  388. int jb_upper_threshold;
  389. /*!
  390. * \brief Allocated jitterbuffer controller
  391. * \note misdn_jb_init() creates the jitterbuffer.
  392. * \note Must use misdn_jb_destroy() to clean up.
  393. */
  394. struct misdn_jb *jb;
  395. /*!
  396. * \brief Allocated DSP controller
  397. * \note ast_dsp_new() creates the DSP controller.
  398. * \note Must use ast_dsp_free() to clean up.
  399. */
  400. struct ast_dsp *dsp;
  401. /*!
  402. * \brief Associated Asterisk channel structure.
  403. */
  404. struct ast_channel * ast;
  405. /*!
  406. * \brief Associated B channel structure.
  407. */
  408. struct misdn_bchannel *bc;
  409. #if defined(AST_MISDN_ENHANCEMENTS)
  410. /*!
  411. * \brief Peer channel for which call completion was initialized.
  412. */
  413. struct misdn_cc_caller *peer;
  414. /*! \brief Associated call completion record ID (-1 if not associated) */
  415. long record_id;
  416. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  417. /*!
  418. * \brief HELD channel call information
  419. */
  420. struct hold_info hold;
  421. /*!
  422. * \brief From associated B channel: Layer 3 process ID
  423. * \note Used to find the HELD channel call record when retrieving a call.
  424. */
  425. unsigned int l3id;
  426. /*!
  427. * \brief From associated B channel: B Channel mISDN driver layer ID from mISDN_get_layerid()
  428. * \note Used only for debug display messages.
  429. */
  430. int addr;
  431. /*!
  432. * \brief Incoming call dialplan context identifier.
  433. * \note The "context" string read in from /etc/asterisk/misdn.conf
  434. */
  435. char context[AST_MAX_CONTEXT];
  436. /*!
  437. * \brief The configured music-on-hold class to use for this call.
  438. * \note The "musicclass" string read in from /etc/asterisk/misdn.conf
  439. */
  440. char mohinterpret[MAX_MUSICCLASS];
  441. /*!
  442. * \brief Number of outgoing audio frames dropped since last debug gripe message.
  443. */
  444. int dropped_frame_cnt;
  445. /*!
  446. * \brief TRUE if we must do the ringback tones.
  447. * \note The "far_alerting" boolean read in from /etc/asterisk/misdn.conf
  448. */
  449. int far_alerting;
  450. /*!
  451. * \brief TRUE if NT should disconnect an overlap dialing call when a timeout occurs.
  452. * \note The "nttimeout" boolean read in from /etc/asterisk/misdn.conf
  453. */
  454. int nttimeout;
  455. /*!
  456. * \brief Tone zone sound used for dialtone generation.
  457. * \note Used as a boolean. Non-NULL to prod generation if enabled.
  458. */
  459. struct ast_tone_zone_sound *ts;
  460. /*!
  461. * \brief Enables overlap dialing for the set amount of seconds. (0 = Disabled)
  462. * \note The "overlapdial" value read in from /etc/asterisk/misdn.conf
  463. */
  464. int overlap_dial;
  465. /*!
  466. * \brief Overlap dialing timeout Task ID. -1 if not running.
  467. */
  468. int overlap_dial_task;
  469. /*!
  470. * \brief overlap_tv access lock.
  471. */
  472. ast_mutex_t overlap_tv_lock;
  473. /*!
  474. * \brief Overlap timer start time. Timer restarted for every digit received.
  475. */
  476. struct timeval overlap_tv;
  477. /*!
  478. * \brief Next channel call record in the list.
  479. */
  480. struct chan_list *next;
  481. };
  482. int MAXTICS = 8;
  483. void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
  484. void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
  485. static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame);
  486. struct robin_list {
  487. char *group;
  488. int port;
  489. int channel;
  490. struct robin_list *next;
  491. struct robin_list *prev;
  492. };
  493. static struct robin_list *robin = NULL;
  494. static void free_robin_list(void)
  495. {
  496. struct robin_list *r;
  497. struct robin_list *next;
  498. for (r = robin, robin = NULL; r; r = next) {
  499. next = r->next;
  500. ast_free(r->group);
  501. ast_free(r);
  502. }
  503. }
  504. static struct robin_list *get_robin_position(char *group)
  505. {
  506. struct robin_list *new;
  507. struct robin_list *iter = robin;
  508. for (; iter; iter = iter->next) {
  509. if (!strcasecmp(iter->group, group)) {
  510. return iter;
  511. }
  512. }
  513. new = ast_calloc(1, sizeof(*new));
  514. if (!new) {
  515. return NULL;
  516. }
  517. new->group = ast_strdup(group);
  518. if (!new->group) {
  519. ast_free(new);
  520. return NULL;
  521. }
  522. new->channel = 1;
  523. if (robin) {
  524. new->next = robin;
  525. robin->prev = new;
  526. }
  527. robin = new;
  528. return robin;
  529. }
  530. /*! \brief the main schedule context for stuff like l1 watcher, overlap dial, ... */
  531. static struct sched_context *misdn_tasks = NULL;
  532. static pthread_t misdn_tasks_thread;
  533. static int *misdn_ports;
  534. static void chan_misdn_log(int level, int port, char *tmpl, ...)
  535. __attribute__((format(printf, 3, 4)));
  536. static struct ast_channel *misdn_new(struct chan_list *cl, int state, char *exten, char *callerid, int format, const char *linkedid, int port, int c);
  537. static void send_digit_to_chan(struct chan_list *cl, char digit);
  538. static int pbx_start_chan(struct chan_list *ch);
  539. #define MISDN_ASTERISK_TECH_PVT(ast) ast->tech_pvt
  540. #include "asterisk/strings.h"
  541. /* #define MISDN_DEBUG 1 */
  542. static const char misdn_type[] = "mISDN";
  543. static int tracing = 0;
  544. /*! \brief Only alaw and mulaw is allowed for now */
  545. static int prefformat = AST_FORMAT_ALAW ; /* AST_FORMAT_SLINEAR ; AST_FORMAT_ULAW | */
  546. static int *misdn_debug;
  547. static int *misdn_debug_only;
  548. static int max_ports;
  549. static int *misdn_in_calls;
  550. static int *misdn_out_calls;
  551. /*!
  552. * \brief Global channel call record list head.
  553. */
  554. static struct chan_list *cl_te=NULL;
  555. static ast_mutex_t cl_te_lock;
  556. static enum event_response_e
  557. cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data);
  558. static int send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, struct chan_list *ch);
  559. static void cl_queue_chan(struct chan_list *chan);
  560. static int dialtone_indicate(struct chan_list *cl);
  561. static void hanguptone_indicate(struct chan_list *cl);
  562. static int stop_indicate(struct chan_list *cl);
  563. static int start_bc_tones(struct chan_list *cl);
  564. static int stop_bc_tones(struct chan_list *cl);
  565. static void release_chan_early(struct chan_list *ch);
  566. static void release_chan(struct chan_list *ch, struct misdn_bchannel *bc);
  567. #if defined(AST_MISDN_ENHANCEMENTS)
  568. static const char misdn_command_name[] = "misdn_command";
  569. static int misdn_command_exec(struct ast_channel *chan, const char *data);
  570. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  571. static int misdn_check_l2l1(struct ast_channel *chan, const char *data);
  572. static int misdn_set_opt_exec(struct ast_channel *chan, const char *data);
  573. static int misdn_facility_exec(struct ast_channel *chan, const char *data);
  574. int chan_misdn_jb_empty(struct misdn_bchannel *bc, char *buf, int len);
  575. void debug_numtype(int port, int numtype, char *type);
  576. int add_out_calls(int port);
  577. int add_in_calls(int port);
  578. #ifdef MISDN_1_2
  579. static int update_pipeline_config(struct misdn_bchannel *bc);
  580. #else
  581. static int update_ec_config(struct misdn_bchannel *bc);
  582. #endif
  583. /*************** Helpers *****************/
  584. static int misdn_chan_is_valid(struct chan_list *ch)
  585. {
  586. struct chan_list *list;
  587. ast_mutex_lock(&cl_te_lock);
  588. for (list = cl_te; list; list = list->next) {
  589. if (list == ch) {
  590. ast_mutex_unlock(&cl_te_lock);
  591. return 1;
  592. }
  593. }
  594. ast_mutex_unlock(&cl_te_lock);
  595. return 0;
  596. }
  597. /*! Returns a reference to the found chan_list. */
  598. static struct chan_list *get_chan_by_ast(struct ast_channel *ast)
  599. {
  600. struct chan_list *tmp;
  601. ast_mutex_lock(&cl_te_lock);
  602. for (tmp = cl_te; tmp; tmp = tmp->next) {
  603. if (tmp->ast == ast) {
  604. chan_list_ref(tmp, "Found chan_list by ast");
  605. ast_mutex_unlock(&cl_te_lock);
  606. return tmp;
  607. }
  608. }
  609. ast_mutex_unlock(&cl_te_lock);
  610. return NULL;
  611. }
  612. /*! Returns a reference to the found chan_list. */
  613. static struct chan_list *get_chan_by_ast_name(const char *name)
  614. {
  615. struct chan_list *tmp;
  616. ast_mutex_lock(&cl_te_lock);
  617. for (tmp = cl_te; tmp; tmp = tmp->next) {
  618. if (tmp->ast && strcmp(tmp->ast->name, name) == 0) {
  619. chan_list_ref(tmp, "Found chan_list by ast name");
  620. ast_mutex_unlock(&cl_te_lock);
  621. return tmp;
  622. }
  623. }
  624. ast_mutex_unlock(&cl_te_lock);
  625. return NULL;
  626. }
  627. #if defined(AST_MISDN_ENHANCEMENTS)
  628. /*!
  629. * \internal
  630. * \brief Destroy the misdn_cc_ds_info datastore payload
  631. *
  632. * \param[in] data the datastore payload, a reference to an misdn_cc_caller
  633. *
  634. * \details
  635. * Since the payload is a reference to an astobj2 object, we just decrement its
  636. * reference count. Before doing so, we NULL out the channel pointer inside of
  637. * the misdn_cc_caller instance. This function will be called in one of two
  638. * cases. In both cases, we no longer need the channel pointer:
  639. *
  640. * - The original channel that initialized call completion services, the same
  641. * channel that is stored here, has been destroyed early. This could happen
  642. * if it transferred the mISDN channel, for example.
  643. *
  644. * - The mISDN channel that had this datastore inherited on to it is now being
  645. * destroyed. If this is the case, then the call completion events have
  646. * already occurred and the appropriate channel variables have already been
  647. * set on the original channel that requested call completion services.
  648. *
  649. * \return Nothing
  650. */
  651. static void misdn_cc_ds_destroy(void *data)
  652. {
  653. struct misdn_cc_caller *cc_caller = data;
  654. ao2_lock(cc_caller);
  655. cc_caller->chan = NULL;
  656. ao2_unlock(cc_caller);
  657. ao2_ref(cc_caller, -1);
  658. }
  659. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  660. #if defined(AST_MISDN_ENHANCEMENTS)
  661. /*!
  662. * \internal
  663. * \brief Duplicate the misdn_cc_ds_info datastore payload
  664. *
  665. * \param[in] data the datastore payload, a reference to an misdn_cc_caller
  666. *
  667. * \details
  668. * All we need to do is bump the reference count and return the same instance.
  669. *
  670. * \return A reference to an instance of a misdn_cc_caller
  671. */
  672. static void *misdn_cc_ds_duplicate(void *data)
  673. {
  674. struct misdn_cc_caller *cc_caller = data;
  675. ao2_ref(cc_caller, +1);
  676. return cc_caller;
  677. }
  678. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  679. #if defined(AST_MISDN_ENHANCEMENTS)
  680. static const struct ast_datastore_info misdn_cc_ds_info = {
  681. .type = "misdn_cc",
  682. .destroy = misdn_cc_ds_destroy,
  683. .duplicate = misdn_cc_ds_duplicate,
  684. };
  685. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  686. #if defined(AST_MISDN_ENHANCEMENTS)
  687. /*!
  688. * \internal
  689. * \brief Set a channel var on the peer channel for call completion services
  690. *
  691. * \param[in] peer The peer that initialized call completion services
  692. * \param[in] var The variable name to set
  693. * \param[in] value The variable value to set
  694. *
  695. * This function may be called from outside of the channel thread. It handles
  696. * the fact that the peer channel may be hung up and destroyed at any time.
  697. *
  698. * \return nothing
  699. */
  700. static void misdn_cc_set_peer_var(struct misdn_cc_caller *peer, const char *var,
  701. const char *value)
  702. {
  703. ao2_lock(peer);
  704. /*! \todo XXX This nastiness can go away once ast_channel is ref counted! */
  705. while (peer->chan && ast_channel_trylock(peer->chan)) {
  706. ao2_unlock(peer);
  707. sched_yield();
  708. ao2_lock(peer);
  709. }
  710. if (peer->chan) {
  711. pbx_builtin_setvar_helper(peer->chan, var, value);
  712. ast_channel_unlock(peer->chan);
  713. }
  714. ao2_unlock(peer);
  715. }
  716. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  717. #if defined(AST_MISDN_ENHANCEMENTS)
  718. /*!
  719. * \internal
  720. * \brief Get a reference to the CC caller if it exists
  721. */
  722. static struct misdn_cc_caller *misdn_cc_caller_get(struct ast_channel *chan)
  723. {
  724. struct ast_datastore *datastore;
  725. struct misdn_cc_caller *cc_caller;
  726. ast_channel_lock(chan);
  727. if (!(datastore = ast_channel_datastore_find(chan, &misdn_cc_ds_info, NULL))) {
  728. ast_channel_unlock(chan);
  729. return NULL;
  730. }
  731. ao2_ref(datastore->data, +1);
  732. cc_caller = datastore->data;
  733. ast_channel_unlock(chan);
  734. return cc_caller;
  735. }
  736. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  737. #if defined(AST_MISDN_ENHANCEMENTS)
  738. /*!
  739. * \internal
  740. * \brief Find the call completion record given the record id.
  741. *
  742. * \param record_id
  743. *
  744. * \retval pointer to found call completion record
  745. * \retval NULL if not found
  746. *
  747. * \note Assumes the misdn_cc_records_db lock is already obtained.
  748. */
  749. static struct misdn_cc_record *misdn_cc_find_by_id(long record_id)
  750. {
  751. struct misdn_cc_record *current;
  752. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  753. if (current->record_id == record_id) {
  754. /* Found the record */
  755. break;
  756. }
  757. }
  758. return current;
  759. }
  760. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  761. #if defined(AST_MISDN_ENHANCEMENTS)
  762. /*!
  763. * \internal
  764. * \brief Find the call completion record given the port and call linkage id.
  765. *
  766. * \param port Logical port number
  767. * \param linkage_id Call linkage ID number from switch.
  768. *
  769. * \retval pointer to found call completion record
  770. * \retval NULL if not found
  771. *
  772. * \note Assumes the misdn_cc_records_db lock is already obtained.
  773. */
  774. static struct misdn_cc_record *misdn_cc_find_by_linkage(int port, int linkage_id)
  775. {
  776. struct misdn_cc_record *current;
  777. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  778. if (current->port == port
  779. && !current->ptp
  780. && current->mode.ptmp.linkage_id == linkage_id) {
  781. /* Found the record */
  782. break;
  783. }
  784. }
  785. return current;
  786. }
  787. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  788. #if defined(AST_MISDN_ENHANCEMENTS)
  789. /*!
  790. * \internal
  791. * \brief Find the call completion record given the port and outstanding invocation id.
  792. *
  793. * \param port Logical port number
  794. * \param invoke_id Outstanding message invocation ID number.
  795. *
  796. * \retval pointer to found call completion record
  797. * \retval NULL if not found
  798. *
  799. * \note Assumes the misdn_cc_records_db lock is already obtained.
  800. */
  801. static struct misdn_cc_record *misdn_cc_find_by_invoke(int port, int invoke_id)
  802. {
  803. struct misdn_cc_record *current;
  804. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  805. if (current->outstanding_message
  806. && current->invoke_id == invoke_id
  807. && current->port == port) {
  808. /* Found the record */
  809. break;
  810. }
  811. }
  812. return current;
  813. }
  814. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  815. #if defined(AST_MISDN_ENHANCEMENTS)
  816. /*!
  817. * \internal
  818. * \brief Find the call completion record given the port and CCBS reference id.
  819. *
  820. * \param port Logical port number
  821. * \param reference_id CCBS reference ID number from switch.
  822. *
  823. * \retval pointer to found call completion record
  824. * \retval NULL if not found
  825. *
  826. * \note Assumes the misdn_cc_records_db lock is already obtained.
  827. */
  828. static struct misdn_cc_record *misdn_cc_find_by_reference(int port, int reference_id)
  829. {
  830. struct misdn_cc_record *current;
  831. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  832. if (current->activated
  833. && current->port == port
  834. && !current->ptp
  835. && current->mode.ptmp.reference_id == reference_id) {
  836. /* Found the record */
  837. break;
  838. }
  839. }
  840. return current;
  841. }
  842. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  843. #if defined(AST_MISDN_ENHANCEMENTS)
  844. /*!
  845. * \internal
  846. * \brief Find the call completion record given the B channel pointer
  847. *
  848. * \param bc B channel control structure pointer.
  849. *
  850. * \retval pointer to found call completion record
  851. * \retval NULL if not found
  852. *
  853. * \note Assumes the misdn_cc_records_db lock is already obtained.
  854. */
  855. static struct misdn_cc_record *misdn_cc_find_by_bc(const struct misdn_bchannel *bc)
  856. {
  857. struct misdn_cc_record *current;
  858. if (bc) {
  859. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  860. if (current->ptp
  861. && current->mode.ptp.bc == bc) {
  862. /* Found the record */
  863. break;
  864. }
  865. }
  866. } else {
  867. current = NULL;
  868. }
  869. return current;
  870. }
  871. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  872. #if defined(AST_MISDN_ENHANCEMENTS)
  873. /*!
  874. * \internal
  875. * \brief Delete the given call completion record
  876. *
  877. * \param doomed Call completion record to destroy
  878. *
  879. * \return Nothing
  880. *
  881. * \note Assumes the misdn_cc_records_db lock is already obtained.
  882. */
  883. static void misdn_cc_delete(struct misdn_cc_record *doomed)
  884. {
  885. struct misdn_cc_record *current;
  886. AST_LIST_TRAVERSE_SAFE_BEGIN(&misdn_cc_records_db, current, list) {
  887. if (current == doomed) {
  888. AST_LIST_REMOVE_CURRENT(list);
  889. ast_free(current);
  890. return;
  891. }
  892. }
  893. AST_LIST_TRAVERSE_SAFE_END;
  894. /* The doomed node is not in the call completion database */
  895. }
  896. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  897. #if defined(AST_MISDN_ENHANCEMENTS)
  898. /*!
  899. * \internal
  900. * \brief Delete all old call completion records
  901. *
  902. * \return Nothing
  903. *
  904. * \note Assumes the misdn_cc_records_db lock is already obtained.
  905. */
  906. static void misdn_cc_remove_old(void)
  907. {
  908. struct misdn_cc_record *current;
  909. time_t now;
  910. now = time(NULL);
  911. AST_LIST_TRAVERSE_SAFE_BEGIN(&misdn_cc_records_db, current, list) {
  912. if (MISDN_CC_RECORD_AGE_MAX < now - current->time_created) {
  913. if (current->ptp && current->mode.ptp.bc) {
  914. /* Close the old call-completion signaling link */
  915. current->mode.ptp.bc->fac_out.Function = Fac_None;
  916. current->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  917. misdn_lib_send_event(current->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  918. }
  919. /* Remove the old call completion record */
  920. AST_LIST_REMOVE_CURRENT(list);
  921. ast_free(current);
  922. }
  923. }
  924. AST_LIST_TRAVERSE_SAFE_END;
  925. }
  926. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  927. #if defined(AST_MISDN_ENHANCEMENTS)
  928. /*!
  929. * \internal
  930. * \brief Allocate the next record id.
  931. *
  932. * \retval New record id on success.
  933. * \retval -1 on error.
  934. *
  935. * \note Assumes the misdn_cc_records_db lock is already obtained.
  936. */
  937. static long misdn_cc_record_id_new(void)
  938. {
  939. long record_id;
  940. long first_id;
  941. record_id = ++misdn_cc_record_id;
  942. first_id = record_id;
  943. while (misdn_cc_find_by_id(record_id)) {
  944. record_id = ++misdn_cc_record_id;
  945. if (record_id == first_id) {
  946. /*
  947. * We have a resource leak.
  948. * We should never need to allocate 64k records.
  949. */
  950. chan_misdn_log(0, 0, " --> ERROR Too many call completion records!\n");
  951. record_id = -1;
  952. break;
  953. }
  954. }
  955. return record_id;
  956. }
  957. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  958. #if defined(AST_MISDN_ENHANCEMENTS)
  959. /*!
  960. * \internal
  961. * \brief Create a new call completion record
  962. *
  963. * \retval pointer to new call completion record
  964. * \retval NULL if failed
  965. *
  966. * \note Assumes the misdn_cc_records_db lock is already obtained.
  967. */
  968. static struct misdn_cc_record *misdn_cc_new(void)
  969. {
  970. struct misdn_cc_record *cc_record;
  971. long record_id;
  972. misdn_cc_remove_old();
  973. cc_record = ast_calloc(1, sizeof(*cc_record));
  974. if (cc_record) {
  975. record_id = misdn_cc_record_id_new();
  976. if (record_id < 0) {
  977. ast_free(cc_record);
  978. return NULL;
  979. }
  980. /* Initialize the new record */
  981. cc_record->record_id = record_id;
  982. cc_record->port = -1;/* Invalid port so it will never be found this way */
  983. cc_record->invoke_id = ++misdn_invoke_id;
  984. cc_record->party_a_free = 1;/* Default User-A as free */
  985. cc_record->error_code = FacError_None;
  986. cc_record->reject_code = FacReject_None;
  987. cc_record->time_created = time(NULL);
  988. /* Insert the new record into the database */
  989. AST_LIST_INSERT_HEAD(&misdn_cc_records_db, cc_record, list);
  990. }
  991. return cc_record;
  992. }
  993. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  994. #if defined(AST_MISDN_ENHANCEMENTS)
  995. /*!
  996. * \internal
  997. * \brief Destroy the call completion record database
  998. *
  999. * \return Nothing
  1000. */
  1001. static void misdn_cc_destroy(void)
  1002. {
  1003. struct misdn_cc_record *current;
  1004. while ((current = AST_LIST_REMOVE_HEAD(&misdn_cc_records_db, list))) {
  1005. /* Do a misdn_cc_delete(current) inline */
  1006. ast_free(current);
  1007. }
  1008. }
  1009. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1010. #if defined(AST_MISDN_ENHANCEMENTS)
  1011. /*!
  1012. * \internal
  1013. * \brief Initialize the call completion record database
  1014. *
  1015. * \return Nothing
  1016. */
  1017. static void misdn_cc_init(void)
  1018. {
  1019. misdn_cc_record_id = 0;
  1020. }
  1021. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1022. #if defined(AST_MISDN_ENHANCEMENTS)
  1023. /*!
  1024. * \internal
  1025. * \brief Check the status of an outstanding invocation request.
  1026. *
  1027. * \param data Points to an integer containing the call completion record id.
  1028. *
  1029. * \retval 0 if got a response.
  1030. * \retval -1 if no response yet.
  1031. */
  1032. static int misdn_cc_response_check(void *data)
  1033. {
  1034. int not_responded;
  1035. struct misdn_cc_record *cc_record;
  1036. AST_LIST_LOCK(&misdn_cc_records_db);
  1037. cc_record = misdn_cc_find_by_id(*(long *) data);
  1038. if (cc_record) {
  1039. if (cc_record->outstanding_message) {
  1040. not_responded = -1;
  1041. } else {
  1042. not_responded = 0;
  1043. }
  1044. } else {
  1045. /* No record so there is no response to check. */
  1046. not_responded = 0;
  1047. }
  1048. AST_LIST_UNLOCK(&misdn_cc_records_db);
  1049. return not_responded;
  1050. }
  1051. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1052. #if defined(AST_MISDN_ENHANCEMENTS)
  1053. /*!
  1054. * \internal
  1055. * \brief Wait for a response from the switch for an outstanding
  1056. * invocation request.
  1057. *
  1058. * \param chan Asterisk channel to operate upon.
  1059. * \param wait_seconds Number of seconds to wait
  1060. * \param record_id Call completion record ID.
  1061. *
  1062. * \return Nothing
  1063. */
  1064. static void misdn_cc_response_wait(struct ast_channel *chan, int wait_seconds, long record_id)
  1065. {
  1066. unsigned count;
  1067. for (count = 2 * MISDN_CC_REQUEST_WAIT_MAX; count--;) {
  1068. /* Sleep in 500 ms increments */
  1069. if (ast_safe_sleep_conditional(chan, 500, misdn_cc_response_check, &record_id) != 0) {
  1070. /* We got hung up or our response came in. */
  1071. break;
  1072. }
  1073. }
  1074. }
  1075. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1076. #if defined(AST_MISDN_ENHANCEMENTS)
  1077. /*!
  1078. * \internal
  1079. * \brief Convert the mISDN reject code to a string
  1080. *
  1081. * \param code mISDN reject code.
  1082. *
  1083. * \return The mISDN reject code as a string
  1084. */
  1085. static const char *misdn_to_str_reject_code(enum FacRejectCode code)
  1086. {
  1087. static const struct {
  1088. enum FacRejectCode code;
  1089. char *name;
  1090. } arr[] = {
  1091. /* *INDENT-OFF* */
  1092. { FacReject_None, "No reject occurred" },
  1093. { FacReject_Unknown, "Unknown reject code" },
  1094. { FacReject_Gen_UnrecognizedComponent, "General: Unrecognized Component" },
  1095. { FacReject_Gen_MistypedComponent, "General: Mistyped Component" },
  1096. { FacReject_Gen_BadlyStructuredComponent, "General: Badly Structured Component" },
  1097. { FacReject_Inv_DuplicateInvocation, "Invoke: Duplicate Invocation" },
  1098. { FacReject_Inv_UnrecognizedOperation, "Invoke: Unrecognized Operation" },
  1099. { FacReject_Inv_MistypedArgument, "Invoke: Mistyped Argument" },
  1100. { FacReject_Inv_ResourceLimitation, "Invoke: Resource Limitation" },
  1101. { FacReject_Inv_InitiatorReleasing, "Invoke: Initiator Releasing" },
  1102. { FacReject_Inv_UnrecognizedLinkedID, "Invoke: Unrecognized Linked ID" },
  1103. { FacReject_Inv_LinkedResponseUnexpected, "Invoke: Linked Response Unexpected" },
  1104. { FacReject_Inv_UnexpectedChildOperation, "Invoke: Unexpected Child Operation" },
  1105. { FacReject_Res_UnrecognizedInvocation, "Result: Unrecognized Invocation" },
  1106. { FacReject_Res_ResultResponseUnexpected, "Result: Result Response Unexpected" },
  1107. { FacReject_Res_MistypedResult, "Result: Mistyped Result" },
  1108. { FacReject_Err_UnrecognizedInvocation, "Error: Unrecognized Invocation" },
  1109. { FacReject_Err_ErrorResponseUnexpected, "Error: Error Response Unexpected" },
  1110. { FacReject_Err_UnrecognizedError, "Error: Unrecognized Error" },
  1111. { FacReject_Err_UnexpectedError, "Error: Unexpected Error" },
  1112. { FacReject_Err_MistypedParameter, "Error: Mistyped Parameter" },
  1113. /* *INDENT-ON* */
  1114. };
  1115. unsigned index;
  1116. for (index = 0; index < ARRAY_LEN(arr); ++index) {
  1117. if (arr[index].code == code) {
  1118. return arr[index].name;
  1119. }
  1120. }
  1121. return "unknown";
  1122. }
  1123. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1124. #if defined(AST_MISDN_ENHANCEMENTS)
  1125. /*!
  1126. * \internal
  1127. * \brief Convert the mISDN error code to a string
  1128. *
  1129. * \param code mISDN error code.
  1130. *
  1131. * \return The mISDN error code as a string
  1132. */
  1133. static const char *misdn_to_str_error_code(enum FacErrorCode code)
  1134. {
  1135. static const struct {
  1136. enum FacErrorCode code;
  1137. char *name;
  1138. } arr[] = {
  1139. /* *INDENT-OFF* */
  1140. { FacError_None, "No error occurred" },
  1141. { FacError_Unknown, "Unknown OID error code" },
  1142. { FacError_Gen_NotSubscribed, "General: Not Subscribed" },
  1143. { FacError_Gen_NotAvailable, "General: Not Available" },
  1144. { FacError_Gen_NotImplemented, "General: Not Implemented" },
  1145. { FacError_Gen_InvalidServedUserNr, "General: Invalid Served User Number" },
  1146. { FacError_Gen_InvalidCallState, "General: Invalid Call State" },
  1147. { FacError_Gen_BasicServiceNotProvided, "General: Basic Service Not Provided" },
  1148. { FacError_Gen_NotIncomingCall, "General: Not Incoming Call" },
  1149. { FacError_Gen_SupplementaryServiceInteractionNotAllowed,"General: Supplementary Service Interaction Not Allowed" },
  1150. { FacError_Gen_ResourceUnavailable, "General: Resource Unavailable" },
  1151. { FacError_Div_InvalidDivertedToNr, "Diversion: Invalid Diverted To Number" },
  1152. { FacError_Div_SpecialServiceNr, "Diversion: Special Service Number" },
  1153. { FacError_Div_DiversionToServedUserNr, "Diversion: Diversion To Served User Number" },
  1154. { FacError_Div_IncomingCallAccepted, "Diversion: Incoming Call Accepted" },
  1155. { FacError_Div_NumberOfDiversionsExceeded, "Diversion: Number Of Diversions Exceeded" },
  1156. { FacError_Div_NotActivated, "Diversion: Not Activated" },
  1157. { FacError_Div_RequestAlreadyAccepted, "Diversion: Request Already Accepted" },
  1158. { FacError_AOC_NoChargingInfoAvailable, "AOC: No Charging Info Available" },
  1159. { FacError_CCBS_InvalidCallLinkageID, "CCBS: Invalid Call Linkage ID" },
  1160. { FacError_CCBS_InvalidCCBSReference, "CCBS: Invalid CCBS Reference" },
  1161. { FacError_CCBS_LongTermDenial, "CCBS: Long Term Denial" },
  1162. { FacError_CCBS_ShortTermDenial, "CCBS: Short Term Denial" },
  1163. { FacError_CCBS_IsAlreadyActivated, "CCBS: Is Already Activated" },
  1164. { FacError_CCBS_AlreadyAccepted, "CCBS: Already Accepted" },
  1165. { FacError_CCBS_OutgoingCCBSQueueFull, "CCBS: Outgoing CCBS Queue Full" },
  1166. { FacError_CCBS_CallFailureReasonNotBusy, "CCBS: Call Failure Reason Not Busy" },
  1167. { FacError_CCBS_NotReadyForCall, "CCBS: Not Ready For Call" },
  1168. { FacError_CCBS_T_LongTermDenial, "CCBS-T: Long Term Denial" },
  1169. { FacError_CCBS_T_ShortTermDenial, "CCBS-T: Short Term Denial" },
  1170. { FacError_ECT_LinkIdNotAssignedByNetwork, "ECT: Link ID Not Assigned By Network" },
  1171. /* *INDENT-ON* */
  1172. };
  1173. unsigned index;
  1174. for (index = 0; index < ARRAY_LEN(arr); ++index) {
  1175. if (arr[index].code == code) {
  1176. return arr[index].name;
  1177. }
  1178. }
  1179. return "unknown";
  1180. }
  1181. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1182. #if defined(AST_MISDN_ENHANCEMENTS)
  1183. /*!
  1184. * \internal
  1185. * \brief Convert mISDN redirecting reason to diversion reason.
  1186. *
  1187. * \param reason mISDN redirecting reason code.
  1188. *
  1189. * \return Supported diversion reason code.
  1190. */
  1191. static unsigned misdn_to_diversion_reason(enum mISDN_REDIRECTING_REASON reason)
  1192. {
  1193. unsigned diversion_reason;
  1194. switch (reason) {
  1195. case mISDN_REDIRECTING_REASON_CALL_FWD:
  1196. diversion_reason = 1;/* cfu */
  1197. break;
  1198. case mISDN_REDIRECTING_REASON_CALL_FWD_BUSY:
  1199. diversion_reason = 2;/* cfb */
  1200. break;
  1201. case mISDN_REDIRECTING_REASON_NO_REPLY:
  1202. diversion_reason = 3;/* cfnr */
  1203. break;
  1204. default:
  1205. diversion_reason = 0;/* unknown */
  1206. break;
  1207. }
  1208. return diversion_reason;
  1209. }
  1210. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1211. #if defined(AST_MISDN_ENHANCEMENTS)
  1212. /*!
  1213. * \internal
  1214. * \brief Convert diversion reason to mISDN redirecting reason
  1215. *
  1216. * \param diversion_reason Diversion reason to convert
  1217. *
  1218. * \return Supported redirecting reason code.
  1219. */
  1220. static enum mISDN_REDIRECTING_REASON diversion_reason_to_misdn(unsigned diversion_reason)
  1221. {
  1222. enum mISDN_REDIRECTING_REASON reason;
  1223. switch (diversion_reason) {
  1224. case 1:/* cfu */
  1225. reason = mISDN_REDIRECTING_REASON_CALL_FWD;
  1226. break;
  1227. case 2:/* cfb */
  1228. reason = mISDN_REDIRECTING_REASON_CALL_FWD_BUSY;
  1229. break;
  1230. case 3:/* cfnr */
  1231. reason = mISDN_REDIRECTING_REASON_NO_REPLY;
  1232. break;
  1233. default:
  1234. reason = mISDN_REDIRECTING_REASON_UNKNOWN;
  1235. break;
  1236. }
  1237. return reason;
  1238. }
  1239. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1240. #if defined(AST_MISDN_ENHANCEMENTS)
  1241. /*!
  1242. * \internal
  1243. * \brief Convert the mISDN presentation to PresentedNumberUnscreened type
  1244. *
  1245. * \param presentation mISDN presentation to convert
  1246. * \param number_present TRUE if the number is present
  1247. *
  1248. * \return PresentedNumberUnscreened type
  1249. */
  1250. static unsigned misdn_to_PresentedNumberUnscreened_type(int presentation, int number_present)
  1251. {
  1252. unsigned type;
  1253. switch (presentation) {
  1254. case 0:/* allowed */
  1255. if (number_present) {
  1256. type = 0;/* presentationAllowedNumber */
  1257. } else {
  1258. type = 2;/* numberNotAvailableDueToInterworking */
  1259. }
  1260. break;
  1261. case 1:/* restricted */
  1262. if (number_present) {
  1263. type = 3;/* presentationRestrictedNumber */
  1264. } else {
  1265. type = 1;/* presentationRestricted */
  1266. }
  1267. break;
  1268. default:
  1269. type = 2;/* numberNotAvailableDueToInterworking */
  1270. break;
  1271. }
  1272. return type;
  1273. }
  1274. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1275. #if defined(AST_MISDN_ENHANCEMENTS)
  1276. /*!
  1277. * \internal
  1278. * \brief Convert the PresentedNumberUnscreened type to mISDN presentation
  1279. *
  1280. * \param type PresentedNumberUnscreened type
  1281. *
  1282. * \return mISDN presentation
  1283. */
  1284. static int PresentedNumberUnscreened_to_misdn_pres(unsigned type)
  1285. {
  1286. int presentation;
  1287. switch (type) {
  1288. default:
  1289. case 0:/* presentationAllowedNumber */
  1290. presentation = 0;/* allowed */
  1291. break;
  1292. case 1:/* presentationRestricted */
  1293. case 3:/* presentationRestrictedNumber */
  1294. presentation = 1;/* restricted */
  1295. break;
  1296. case 2:/* numberNotAvailableDueToInterworking */
  1297. presentation = 2;/* unavailable */
  1298. break;
  1299. }
  1300. return presentation;
  1301. }
  1302. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1303. #if defined(AST_MISDN_ENHANCEMENTS)
  1304. /*!
  1305. * \internal
  1306. * \brief Convert the mISDN numbering plan to PartyNumber numbering plan
  1307. *
  1308. * \param number_plan mISDN numbering plan
  1309. *
  1310. * \return PartyNumber numbering plan
  1311. */
  1312. static unsigned misdn_to_PartyNumber_plan(enum mISDN_NUMBER_PLAN number_plan)
  1313. {
  1314. unsigned party_plan;
  1315. switch (number_plan) {
  1316. default:
  1317. case NUMPLAN_UNKNOWN:
  1318. party_plan = 0;/* unknown */
  1319. break;
  1320. case NUMPLAN_ISDN:
  1321. party_plan = 1;/* public */
  1322. break;
  1323. case NUMPLAN_DATA:
  1324. party_plan = 3;/* data */
  1325. break;
  1326. case NUMPLAN_TELEX:
  1327. party_plan = 4;/* telex */
  1328. break;
  1329. case NUMPLAN_NATIONAL:
  1330. party_plan = 8;/* nationalStandard */
  1331. break;
  1332. case NUMPLAN_PRIVATE:
  1333. party_plan = 5;/* private */
  1334. break;
  1335. }
  1336. return party_plan;
  1337. }
  1338. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1339. #if defined(AST_MISDN_ENHANCEMENTS)
  1340. /*!
  1341. * \internal
  1342. * \brief Convert PartyNumber numbering plan to mISDN numbering plan
  1343. *
  1344. * \param party_plan PartyNumber numbering plan
  1345. *
  1346. * \return mISDN numbering plan
  1347. */
  1348. static enum mISDN_NUMBER_PLAN PartyNumber_to_misdn_plan(unsigned party_plan)
  1349. {
  1350. enum mISDN_NUMBER_PLAN number_plan;
  1351. switch (party_plan) {
  1352. default:
  1353. case 0:/* unknown */
  1354. number_plan = NUMPLAN_UNKNOWN;
  1355. break;
  1356. case 1:/* public */
  1357. number_plan = NUMPLAN_ISDN;
  1358. break;
  1359. case 3:/* data */
  1360. number_plan = NUMPLAN_DATA;
  1361. break;
  1362. case 4:/* telex */
  1363. number_plan = NUMPLAN_TELEX;
  1364. break;
  1365. case 8:/* nationalStandard */
  1366. number_plan = NUMPLAN_NATIONAL;
  1367. break;
  1368. case 5:/* private */
  1369. number_plan = NUMPLAN_PRIVATE;
  1370. break;
  1371. }
  1372. return number_plan;
  1373. }
  1374. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1375. #if defined(AST_MISDN_ENHANCEMENTS)
  1376. /*!
  1377. * \internal
  1378. * \brief Convert mISDN type-of-number to PartyNumber public type-of-number
  1379. *
  1380. * \param ton mISDN type-of-number
  1381. *
  1382. * \return PartyNumber public type-of-number
  1383. */
  1384. static unsigned misdn_to_PartyNumber_ton_public(enum mISDN_NUMBER_TYPE ton)
  1385. {
  1386. unsigned party_ton;
  1387. switch (ton) {
  1388. default:
  1389. case NUMTYPE_UNKNOWN:
  1390. party_ton = 0;/* unknown */
  1391. break;
  1392. case NUMTYPE_INTERNATIONAL:
  1393. party_ton = 1;/* internationalNumber */
  1394. break;
  1395. case NUMTYPE_NATIONAL:
  1396. party_ton = 2;/* nationalNumber */
  1397. break;
  1398. case NUMTYPE_NETWORK_SPECIFIC:
  1399. party_ton = 3;/* networkSpecificNumber */
  1400. break;
  1401. case NUMTYPE_SUBSCRIBER:
  1402. party_ton = 4;/* subscriberNumber */
  1403. break;
  1404. case NUMTYPE_ABBREVIATED:
  1405. party_ton = 6;/* abbreviatedNumber */
  1406. break;
  1407. }
  1408. return party_ton;
  1409. }
  1410. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1411. #if defined(AST_MISDN_ENHANCEMENTS)
  1412. /*!
  1413. * \internal
  1414. * \brief Convert the PartyNumber public type-of-number to mISDN type-of-number
  1415. *
  1416. * \param party_ton PartyNumber public type-of-number
  1417. *
  1418. * \return mISDN type-of-number
  1419. */
  1420. static enum mISDN_NUMBER_TYPE PartyNumber_to_misdn_ton_public(unsigned party_ton)
  1421. {
  1422. enum mISDN_NUMBER_TYPE ton;
  1423. switch (party_ton) {
  1424. default:
  1425. case 0:/* unknown */
  1426. ton = NUMTYPE_UNKNOWN;
  1427. break;
  1428. case 1:/* internationalNumber */
  1429. ton = NUMTYPE_INTERNATIONAL;
  1430. break;
  1431. case 2:/* nationalNumber */
  1432. ton = NUMTYPE_NATIONAL;
  1433. break;
  1434. case 3:/* networkSpecificNumber */
  1435. ton = NUMTYPE_NETWORK_SPECIFIC;
  1436. break;
  1437. case 4:/* subscriberNumber */
  1438. ton = NUMTYPE_SUBSCRIBER;
  1439. break;
  1440. case 6:/* abbreviatedNumber */
  1441. ton = NUMTYPE_ABBREVIATED;
  1442. break;
  1443. }
  1444. return ton;
  1445. }
  1446. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1447. #if defined(AST_MISDN_ENHANCEMENTS)
  1448. /*!
  1449. * \internal
  1450. * \brief Convert mISDN type-of-number to PartyNumber private type-of-number
  1451. *
  1452. * \param ton mISDN type-of-number
  1453. *
  1454. * \return PartyNumber private type-of-number
  1455. */
  1456. static unsigned misdn_to_PartyNumber_ton_private(enum mISDN_NUMBER_TYPE ton)
  1457. {
  1458. unsigned party_ton;
  1459. switch (ton) {
  1460. default:
  1461. case NUMTYPE_UNKNOWN:
  1462. party_ton = 0;/* unknown */
  1463. break;
  1464. case NUMTYPE_INTERNATIONAL:
  1465. party_ton = 1;/* level2RegionalNumber */
  1466. break;
  1467. case NUMTYPE_NATIONAL:
  1468. party_ton = 2;/* level1RegionalNumber */
  1469. break;
  1470. case NUMTYPE_NETWORK_SPECIFIC:
  1471. party_ton = 3;/* pTNSpecificNumber */
  1472. break;
  1473. case NUMTYPE_SUBSCRIBER:
  1474. party_ton = 4;/* localNumber */
  1475. break;
  1476. case NUMTYPE_ABBREVIATED:
  1477. party_ton = 6;/* abbreviatedNumber */
  1478. break;
  1479. }
  1480. return party_ton;
  1481. }
  1482. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1483. #if defined(AST_MISDN_ENHANCEMENTS)
  1484. /*!
  1485. * \internal
  1486. * \brief Convert the PartyNumber private type-of-number to mISDN type-of-number
  1487. *
  1488. * \param party_ton PartyNumber private type-of-number
  1489. *
  1490. * \return mISDN type-of-number
  1491. */
  1492. static enum mISDN_NUMBER_TYPE PartyNumber_to_misdn_ton_private(unsigned party_ton)
  1493. {
  1494. enum mISDN_NUMBER_TYPE ton;
  1495. switch (party_ton) {
  1496. default:
  1497. case 0:/* unknown */
  1498. ton = NUMTYPE_UNKNOWN;
  1499. break;
  1500. case 1:/* level2RegionalNumber */
  1501. ton = NUMTYPE_INTERNATIONAL;
  1502. break;
  1503. case 2:/* level1RegionalNumber */
  1504. ton = NUMTYPE_NATIONAL;
  1505. break;
  1506. case 3:/* pTNSpecificNumber */
  1507. ton = NUMTYPE_NETWORK_SPECIFIC;
  1508. break;
  1509. case 4:/* localNumber */
  1510. ton = NUMTYPE_SUBSCRIBER;
  1511. break;
  1512. case 6:/* abbreviatedNumber */
  1513. ton = NUMTYPE_ABBREVIATED;
  1514. break;
  1515. }
  1516. return ton;
  1517. }
  1518. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1519. /*!
  1520. * \internal
  1521. * \brief Convert the mISDN type of number code to a string
  1522. *
  1523. * \param number_type mISDN type of number code.
  1524. *
  1525. * \return The mISDN type of number code as a string
  1526. */
  1527. static const char *misdn_to_str_ton(enum mISDN_NUMBER_TYPE number_type)
  1528. {
  1529. const char *str;
  1530. switch (number_type) {
  1531. default:
  1532. case NUMTYPE_UNKNOWN:
  1533. str = "Unknown";
  1534. break;
  1535. case NUMTYPE_INTERNATIONAL:
  1536. str = "International";
  1537. break;
  1538. case NUMTYPE_NATIONAL:
  1539. str = "National";
  1540. break;
  1541. case NUMTYPE_NETWORK_SPECIFIC:
  1542. str = "Network Specific";
  1543. break;
  1544. case NUMTYPE_SUBSCRIBER:
  1545. str = "Subscriber";
  1546. break;
  1547. case NUMTYPE_ABBREVIATED:
  1548. str = "Abbreviated";
  1549. break;
  1550. }
  1551. return str;
  1552. }
  1553. /*!
  1554. * \internal
  1555. * \brief Convert the mISDN type of number code to Asterisk type of number code
  1556. *
  1557. * \param number_type mISDN type of number code.
  1558. *
  1559. * \return Asterisk type of number code
  1560. */
  1561. static int misdn_to_ast_ton(enum mISDN_NUMBER_TYPE number_type)
  1562. {
  1563. int ast_number_type;
  1564. switch (number_type) {
  1565. default:
  1566. case NUMTYPE_UNKNOWN:
  1567. ast_number_type = NUMTYPE_UNKNOWN << 4;
  1568. break;
  1569. case NUMTYPE_INTERNATIONAL:
  1570. ast_number_type = NUMTYPE_INTERNATIONAL << 4;
  1571. break;
  1572. case NUMTYPE_NATIONAL:
  1573. ast_number_type = NUMTYPE_NATIONAL << 4;
  1574. break;
  1575. case NUMTYPE_NETWORK_SPECIFIC:
  1576. ast_number_type = NUMTYPE_NETWORK_SPECIFIC << 4;
  1577. break;
  1578. case NUMTYPE_SUBSCRIBER:
  1579. ast_number_type = NUMTYPE_SUBSCRIBER << 4;
  1580. break;
  1581. case NUMTYPE_ABBREVIATED:
  1582. ast_number_type = NUMTYPE_ABBREVIATED << 4;
  1583. break;
  1584. }
  1585. return ast_number_type;
  1586. }
  1587. /*!
  1588. * \internal
  1589. * \brief Convert the Asterisk type of number code to mISDN type of number code
  1590. *
  1591. * \param ast_number_type Asterisk type of number code.
  1592. *
  1593. * \return mISDN type of number code
  1594. */
  1595. static enum mISDN_NUMBER_TYPE ast_to_misdn_ton(unsigned ast_number_type)
  1596. {
  1597. enum mISDN_NUMBER_TYPE number_type;
  1598. switch ((ast_number_type >> 4) & 0x07) {
  1599. default:
  1600. case NUMTYPE_UNKNOWN:
  1601. number_type = NUMTYPE_UNKNOWN;
  1602. break;
  1603. case NUMTYPE_INTERNATIONAL:
  1604. number_type = NUMTYPE_INTERNATIONAL;
  1605. break;
  1606. case NUMTYPE_NATIONAL:
  1607. number_type = NUMTYPE_NATIONAL;
  1608. break;
  1609. case NUMTYPE_NETWORK_SPECIFIC:
  1610. number_type = NUMTYPE_NETWORK_SPECIFIC;
  1611. break;
  1612. case NUMTYPE_SUBSCRIBER:
  1613. number_type = NUMTYPE_SUBSCRIBER;
  1614. break;
  1615. case NUMTYPE_ABBREVIATED:
  1616. number_type = NUMTYPE_ABBREVIATED;
  1617. break;
  1618. }
  1619. return number_type;
  1620. }
  1621. /*!
  1622. * \internal
  1623. * \brief Convert the mISDN numbering plan code to a string
  1624. *
  1625. * \param number_plan mISDN numbering plan code.
  1626. *
  1627. * \return The mISDN numbering plan code as a string
  1628. */
  1629. static const char *misdn_to_str_plan(enum mISDN_NUMBER_PLAN number_plan)
  1630. {
  1631. const char *str;
  1632. switch (number_plan) {
  1633. default:
  1634. case NUMPLAN_UNKNOWN:
  1635. str = "Unknown";
  1636. break;
  1637. case NUMPLAN_ISDN:
  1638. str = "ISDN";
  1639. break;
  1640. case NUMPLAN_DATA:
  1641. str = "Data";
  1642. break;
  1643. case NUMPLAN_TELEX:
  1644. str = "Telex";
  1645. break;
  1646. case NUMPLAN_NATIONAL:
  1647. str = "National";
  1648. break;
  1649. case NUMPLAN_PRIVATE:
  1650. str = "Private";
  1651. break;
  1652. }
  1653. return str;
  1654. }
  1655. /*!
  1656. * \internal
  1657. * \brief Convert the mISDN numbering plan code to Asterisk numbering plan code
  1658. *
  1659. * \param number_plan mISDN numbering plan code.
  1660. *
  1661. * \return Asterisk numbering plan code
  1662. */
  1663. static int misdn_to_ast_plan(enum mISDN_NUMBER_PLAN number_plan)
  1664. {
  1665. int ast_number_plan;
  1666. switch (number_plan) {
  1667. default:
  1668. case NUMPLAN_UNKNOWN:
  1669. ast_number_plan = NUMPLAN_UNKNOWN;
  1670. break;
  1671. case NUMPLAN_ISDN:
  1672. ast_number_plan = NUMPLAN_ISDN;
  1673. break;
  1674. case NUMPLAN_DATA:
  1675. ast_number_plan = NUMPLAN_DATA;
  1676. break;
  1677. case NUMPLAN_TELEX:
  1678. ast_number_plan = NUMPLAN_TELEX;
  1679. break;
  1680. case NUMPLAN_NATIONAL:
  1681. ast_number_plan = NUMPLAN_NATIONAL;
  1682. break;
  1683. case NUMPLAN_PRIVATE:
  1684. ast_number_plan = NUMPLAN_PRIVATE;
  1685. break;
  1686. }
  1687. return ast_number_plan;
  1688. }
  1689. /*!
  1690. * \internal
  1691. * \brief Convert the Asterisk numbering plan code to mISDN numbering plan code
  1692. *
  1693. * \param ast_number_plan Asterisk numbering plan code.
  1694. *
  1695. * \return mISDN numbering plan code
  1696. */
  1697. static enum mISDN_NUMBER_PLAN ast_to_misdn_plan(unsigned ast_number_plan)
  1698. {
  1699. enum mISDN_NUMBER_PLAN number_plan;
  1700. switch (ast_number_plan & 0x0F) {
  1701. default:
  1702. case NUMPLAN_UNKNOWN:
  1703. number_plan = NUMPLAN_UNKNOWN;
  1704. break;
  1705. case NUMPLAN_ISDN:
  1706. number_plan = NUMPLAN_ISDN;
  1707. break;
  1708. case NUMPLAN_DATA:
  1709. number_plan = NUMPLAN_DATA;
  1710. break;
  1711. case NUMPLAN_TELEX:
  1712. number_plan = NUMPLAN_TELEX;
  1713. break;
  1714. case NUMPLAN_NATIONAL:
  1715. number_plan = NUMPLAN_NATIONAL;
  1716. break;
  1717. case NUMPLAN_PRIVATE:
  1718. number_plan = NUMPLAN_PRIVATE;
  1719. break;
  1720. }
  1721. return number_plan;
  1722. }
  1723. /*!
  1724. * \internal
  1725. * \brief Convert the mISDN presentation code to a string
  1726. *
  1727. * \param presentation mISDN number presentation restriction code.
  1728. *
  1729. * \return The mISDN presentation code as a string
  1730. */
  1731. static const char *misdn_to_str_pres(int presentation)
  1732. {
  1733. const char *str;
  1734. switch (presentation) {
  1735. case 0:
  1736. str = "Allowed";
  1737. break;
  1738. case 1:
  1739. str = "Restricted";
  1740. break;
  1741. case 2:
  1742. str = "Unavailable";
  1743. break;
  1744. default:
  1745. str = "Unknown";
  1746. break;
  1747. }
  1748. return str;
  1749. }
  1750. /*!
  1751. * \internal
  1752. * \brief Convert the mISDN presentation code to Asterisk presentation code
  1753. *
  1754. * \param presentation mISDN number presentation restriction code.
  1755. *
  1756. * \return Asterisk presentation code
  1757. */
  1758. static int misdn_to_ast_pres(int presentation)
  1759. {
  1760. switch (presentation) {
  1761. default:
  1762. case 0:
  1763. presentation = AST_PRES_ALLOWED;
  1764. break;
  1765. case 1:
  1766. presentation = AST_PRES_RESTRICTED;
  1767. break;
  1768. case 2:
  1769. presentation = AST_PRES_UNAVAILABLE;
  1770. break;
  1771. }
  1772. return presentation;
  1773. }
  1774. /*!
  1775. * \internal
  1776. * \brief Convert the Asterisk presentation code to mISDN presentation code
  1777. *
  1778. * \param presentation Asterisk number presentation restriction code.
  1779. *
  1780. * \return mISDN presentation code
  1781. */
  1782. static int ast_to_misdn_pres(int presentation)
  1783. {
  1784. switch (presentation & AST_PRES_RESTRICTION) {
  1785. default:
  1786. case AST_PRES_ALLOWED:
  1787. presentation = 0;
  1788. break;
  1789. case AST_PRES_RESTRICTED:
  1790. presentation = 1;
  1791. break;
  1792. case AST_PRES_UNAVAILABLE:
  1793. presentation = 2;
  1794. break;
  1795. }
  1796. return presentation;
  1797. }
  1798. /*!
  1799. * \internal
  1800. * \brief Convert the mISDN screening code to a string
  1801. *
  1802. * \param screening mISDN number screening code.
  1803. *
  1804. * \return The mISDN screening code as a string
  1805. */
  1806. static const char *misdn_to_str_screen(int screening)
  1807. {
  1808. const char *str;
  1809. switch (screening) {
  1810. case 0:
  1811. str = "Unscreened";
  1812. break;
  1813. case 1:
  1814. str = "Passed Screen";
  1815. break;
  1816. case 2:
  1817. str = "Failed Screen";
  1818. break;
  1819. case 3:
  1820. str = "Network Number";
  1821. break;
  1822. default:
  1823. str = "Unknown";
  1824. break;
  1825. }
  1826. return str;
  1827. }
  1828. /*!
  1829. * \internal
  1830. * \brief Convert the mISDN screening code to Asterisk screening code
  1831. *
  1832. * \param screening mISDN number screening code.
  1833. *
  1834. * \return Asterisk screening code
  1835. */
  1836. static int misdn_to_ast_screen(int screening)
  1837. {
  1838. switch (screening) {
  1839. default:
  1840. case 0:
  1841. screening = AST_PRES_USER_NUMBER_UNSCREENED;
  1842. break;
  1843. case 1:
  1844. screening = AST_PRES_USER_NUMBER_PASSED_SCREEN;
  1845. break;
  1846. case 2:
  1847. screening = AST_PRES_USER_NUMBER_FAILED_SCREEN;
  1848. break;
  1849. case 3:
  1850. screening = AST_PRES_NETWORK_NUMBER;
  1851. break;
  1852. }
  1853. return screening;
  1854. }
  1855. /*!
  1856. * \internal
  1857. * \brief Convert the Asterisk screening code to mISDN screening code
  1858. *
  1859. * \param screening Asterisk number screening code.
  1860. *
  1861. * \return mISDN screening code
  1862. */
  1863. static int ast_to_misdn_screen(int screening)
  1864. {
  1865. switch (screening & AST_PRES_NUMBER_TYPE) {
  1866. default:
  1867. case AST_PRES_USER_NUMBER_UNSCREENED:
  1868. screening = 0;
  1869. break;
  1870. case AST_PRES_USER_NUMBER_PASSED_SCREEN:
  1871. screening = 1;
  1872. break;
  1873. case AST_PRES_USER_NUMBER_FAILED_SCREEN:
  1874. screening = 2;
  1875. break;
  1876. case AST_PRES_NETWORK_NUMBER:
  1877. screening = 3;
  1878. break;
  1879. }
  1880. return screening;
  1881. }
  1882. /*!
  1883. * \internal
  1884. * \brief Convert Asterisk redirecting reason to mISDN redirecting reason code.
  1885. *
  1886. * \param ast Asterisk redirecting reason code.
  1887. *
  1888. * \return mISDN reason code
  1889. */
  1890. static enum mISDN_REDIRECTING_REASON ast_to_misdn_reason(const enum AST_REDIRECTING_REASON ast)
  1891. {
  1892. unsigned index;
  1893. static const struct misdn_reasons {
  1894. enum AST_REDIRECTING_REASON ast;
  1895. enum mISDN_REDIRECTING_REASON q931;
  1896. } misdn_reason_table[] = {
  1897. /* *INDENT-OFF* */
  1898. { AST_REDIRECTING_REASON_UNKNOWN, mISDN_REDIRECTING_REASON_UNKNOWN },
  1899. { AST_REDIRECTING_REASON_USER_BUSY, mISDN_REDIRECTING_REASON_CALL_FWD_BUSY },
  1900. { AST_REDIRECTING_REASON_NO_ANSWER, mISDN_REDIRECTING_REASON_NO_REPLY },
  1901. { AST_REDIRECTING_REASON_UNAVAILABLE, mISDN_REDIRECTING_REASON_NO_REPLY },
  1902. { AST_REDIRECTING_REASON_UNCONDITIONAL, mISDN_REDIRECTING_REASON_CALL_FWD },
  1903. { AST_REDIRECTING_REASON_TIME_OF_DAY, mISDN_REDIRECTING_REASON_UNKNOWN },
  1904. { AST_REDIRECTING_REASON_DO_NOT_DISTURB, mISDN_REDIRECTING_REASON_UNKNOWN },
  1905. { AST_REDIRECTING_REASON_DEFLECTION, mISDN_REDIRECTING_REASON_DEFLECTION },
  1906. { AST_REDIRECTING_REASON_FOLLOW_ME, mISDN_REDIRECTING_REASON_UNKNOWN },
  1907. { AST_REDIRECTING_REASON_OUT_OF_ORDER, mISDN_REDIRECTING_REASON_OUT_OF_ORDER },
  1908. { AST_REDIRECTING_REASON_AWAY, mISDN_REDIRECTING_REASON_UNKNOWN },
  1909. { AST_REDIRECTING_REASON_CALL_FWD_DTE, mISDN_REDIRECTING_REASON_CALL_FWD_DTE }
  1910. /* *INDENT-ON* */
  1911. };
  1912. for (index = 0; index < ARRAY_LEN(misdn_reason_table); ++index) {
  1913. if (misdn_reason_table[index].ast == ast) {
  1914. return misdn_reason_table[index].q931;
  1915. }
  1916. }
  1917. return mISDN_REDIRECTING_REASON_UNKNOWN;
  1918. }
  1919. /*!
  1920. * \internal
  1921. * \brief Convert the mISDN redirecting reason to Asterisk redirecting reason code
  1922. *
  1923. * \param q931 mISDN redirecting reason code.
  1924. *
  1925. * \return Asterisk redirecting reason code
  1926. */
  1927. static enum AST_REDIRECTING_REASON misdn_to_ast_reason(const enum mISDN_REDIRECTING_REASON q931)
  1928. {
  1929. enum AST_REDIRECTING_REASON ast;
  1930. switch (q931) {
  1931. default:
  1932. case mISDN_REDIRECTING_REASON_UNKNOWN:
  1933. ast = AST_REDIRECTING_REASON_UNKNOWN;
  1934. break;
  1935. case mISDN_REDIRECTING_REASON_CALL_FWD_BUSY:
  1936. ast = AST_REDIRECTING_REASON_USER_BUSY;
  1937. break;
  1938. case mISDN_REDIRECTING_REASON_NO_REPLY:
  1939. ast = AST_REDIRECTING_REASON_NO_ANSWER;
  1940. break;
  1941. case mISDN_REDIRECTING_REASON_DEFLECTION:
  1942. ast = AST_REDIRECTING_REASON_DEFLECTION;
  1943. break;
  1944. case mISDN_REDIRECTING_REASON_OUT_OF_ORDER:
  1945. ast = AST_REDIRECTING_REASON_OUT_OF_ORDER;
  1946. break;
  1947. case mISDN_REDIRECTING_REASON_CALL_FWD_DTE:
  1948. ast = AST_REDIRECTING_REASON_CALL_FWD_DTE;
  1949. break;
  1950. case mISDN_REDIRECTING_REASON_CALL_FWD:
  1951. ast = AST_REDIRECTING_REASON_UNCONDITIONAL;
  1952. break;
  1953. }
  1954. return ast;
  1955. }
  1956. struct allowed_bearers {
  1957. char *name; /*!< Bearer capability name string used in /etc/misdn.conf allowed_bearers */
  1958. char *display; /*!< Bearer capability displayable name */
  1959. int cap; /*!< SETUP message bearer capability field code value */
  1960. int deprecated; /*!< TRUE if this entry is deprecated. (Misspelled or bad name to use) */
  1961. };
  1962. /* *INDENT-OFF* */
  1963. static const struct allowed_bearers allowed_bearers_array[] = {
  1964. /* Name, Displayable Name Bearer Capability, Deprecated */
  1965. { "speech", "Speech", INFO_CAPABILITY_SPEECH, 0 },
  1966. { "3_1khz", "3.1KHz Audio", INFO_CAPABILITY_AUDIO_3_1K, 0 },
  1967. { "digital_unrestricted", "Unrestricted Digital", INFO_CAPABILITY_DIGITAL_UNRESTRICTED, 0 },
  1968. { "digital_restricted", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 0 },
  1969. { "digital_restriced", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 1 }, /* Allow misspelling for backwards compatibility */
  1970. { "video", "Video", INFO_CAPABILITY_VIDEO, 0 }
  1971. };
  1972. /* *INDENT-ON* */
  1973. static const char *bearer2str(int cap)
  1974. {
  1975. unsigned index;
  1976. for (index = 0; index < ARRAY_LEN(allowed_bearers_array); ++index) {
  1977. if (allowed_bearers_array[index].cap == cap) {
  1978. return allowed_bearers_array[index].display;
  1979. }
  1980. }
  1981. return "Unknown Bearer";
  1982. }
  1983. #if defined(AST_MISDN_ENHANCEMENTS)
  1984. /*!
  1985. * \internal
  1986. * \brief Fill in facility PartyNumber information
  1987. *
  1988. * \param party PartyNumber structure to fill in.
  1989. * \param id Information to put in PartyNumber structure.
  1990. *
  1991. * \return Nothing
  1992. */
  1993. static void misdn_PartyNumber_fill(struct FacPartyNumber *party, const struct misdn_party_id *id)
  1994. {
  1995. ast_copy_string((char *) party->Number, id->number, sizeof(party->Number));
  1996. party->LengthOfNumber = strlen((char *) party->Number);
  1997. party->Type = misdn_to_PartyNumber_plan(id->number_plan);
  1998. switch (party->Type) {
  1999. case 1:/* public */
  2000. party->TypeOfNumber = misdn_to_PartyNumber_ton_public(id->number_type);
  2001. break;
  2002. case 5:/* private */
  2003. party->TypeOfNumber = misdn_to_PartyNumber_ton_private(id->number_type);
  2004. break;
  2005. default:
  2006. party->TypeOfNumber = 0;/* Don't care */
  2007. break;
  2008. }
  2009. }
  2010. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2011. #if defined(AST_MISDN_ENHANCEMENTS)
  2012. /*!
  2013. * \internal
  2014. * \brief Extract the information from PartyNumber
  2015. *
  2016. * \param id Where to put extracted PartyNumber information
  2017. * \param party PartyNumber information to extract
  2018. *
  2019. * \return Nothing
  2020. */
  2021. static void misdn_PartyNumber_extract(struct misdn_party_id *id, const struct FacPartyNumber *party)
  2022. {
  2023. if (party->LengthOfNumber) {
  2024. ast_copy_string(id->number, (char *) party->Number, sizeof(id->number));
  2025. id->number_plan = PartyNumber_to_misdn_plan(party->Type);
  2026. switch (party->Type) {
  2027. case 1:/* public */
  2028. id->number_type = PartyNumber_to_misdn_ton_public(party->TypeOfNumber);
  2029. break;
  2030. case 5:/* private */
  2031. id->number_type = PartyNumber_to_misdn_ton_private(party->TypeOfNumber);
  2032. break;
  2033. default:
  2034. id->number_type = NUMTYPE_UNKNOWN;
  2035. break;
  2036. }
  2037. } else {
  2038. /* Number not present */
  2039. id->number_type = NUMTYPE_UNKNOWN;
  2040. id->number_plan = NUMPLAN_ISDN;
  2041. id->number[0] = 0;
  2042. }
  2043. }
  2044. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2045. #if defined(AST_MISDN_ENHANCEMENTS)
  2046. /*!
  2047. * \internal
  2048. * \brief Fill in facility Address information
  2049. *
  2050. * \param Address Address structure to fill in.
  2051. * \param id Information to put in Address structure.
  2052. *
  2053. * \return Nothing
  2054. */
  2055. static void misdn_Address_fill(struct FacAddress *Address, const struct misdn_party_id *id)
  2056. {
  2057. misdn_PartyNumber_fill(&Address->Party, id);
  2058. /* Subaddresses are not supported yet */
  2059. Address->Subaddress.Length = 0;
  2060. }
  2061. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2062. #if defined(AST_MISDN_ENHANCEMENTS)
  2063. /*!
  2064. * \internal
  2065. * \brief Fill in facility PresentedNumberUnscreened information
  2066. *
  2067. * \param presented PresentedNumberUnscreened structure to fill in.
  2068. * \param id Information to put in PresentedNumberUnscreened structure.
  2069. *
  2070. * \return Nothing
  2071. */
  2072. static void misdn_PresentedNumberUnscreened_fill(struct FacPresentedNumberUnscreened *presented, const struct misdn_party_id *id)
  2073. {
  2074. presented->Type = misdn_to_PresentedNumberUnscreened_type(id->presentation, id->number[0] ? 1 : 0);
  2075. misdn_PartyNumber_fill(&presented->Unscreened, id);
  2076. }
  2077. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2078. #if defined(AST_MISDN_ENHANCEMENTS)
  2079. /*!
  2080. * \internal
  2081. * \brief Extract the information from PartyNumber
  2082. *
  2083. * \param id Where to put extracted PresentedNumberUnscreened information
  2084. * \param presented PresentedNumberUnscreened information to extract
  2085. *
  2086. * \return Nothing
  2087. */
  2088. static void misdn_PresentedNumberUnscreened_extract(struct misdn_party_id *id, const struct FacPresentedNumberUnscreened *presented)
  2089. {
  2090. id->presentation = PresentedNumberUnscreened_to_misdn_pres(presented->Type);
  2091. id->screening = 0;/* unscreened */
  2092. switch (presented->Type) {
  2093. case 0:/* presentationAllowedNumber */
  2094. case 3:/* presentationRestrictedNumber */
  2095. misdn_PartyNumber_extract(id, &presented->Unscreened);
  2096. break;
  2097. case 1:/* presentationRestricted */
  2098. case 2:/* numberNotAvailableDueToInterworking */
  2099. default:
  2100. /* Number not present (And uninitialized so do not even look at it!) */
  2101. id->number_type = NUMTYPE_UNKNOWN;
  2102. id->number_plan = NUMPLAN_ISDN;
  2103. id->number[0] = 0;
  2104. break;
  2105. }
  2106. }
  2107. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2108. #if defined(AST_MISDN_ENHANCEMENTS)
  2109. static const char Level_Spacing[] = " ";/* Work for up to 10 levels */
  2110. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2111. #if defined(AST_MISDN_ENHANCEMENTS)
  2112. static void print_facility_PartyNumber(unsigned Level, const struct FacPartyNumber *Party, const struct misdn_bchannel *bc)
  2113. {
  2114. if (Party->LengthOfNumber) {
  2115. const char *Spacing;
  2116. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2117. chan_misdn_log(1, bc->port, " -->%s PartyNumber: Type:%d\n",
  2118. Spacing, Party->Type);
  2119. switch (Party->Type) {
  2120. case 0: /* Unknown PartyNumber */
  2121. chan_misdn_log(1, bc->port, " -->%s Unknown: %s\n",
  2122. Spacing, Party->Number);
  2123. break;
  2124. case 1: /* Public PartyNumber */
  2125. chan_misdn_log(1, bc->port, " -->%s Public TON:%d %s\n",
  2126. Spacing, Party->TypeOfNumber, Party->Number);
  2127. break;
  2128. case 2: /* NSAP encoded PartyNumber */
  2129. chan_misdn_log(1, bc->port, " -->%s NSAP: %s\n",
  2130. Spacing, Party->Number);
  2131. break;
  2132. case 3: /* Data PartyNumber (Not used) */
  2133. chan_misdn_log(1, bc->port, " -->%s Data: %s\n",
  2134. Spacing, Party->Number);
  2135. break;
  2136. case 4: /* Telex PartyNumber (Not used) */
  2137. chan_misdn_log(1, bc->port, " -->%s Telex: %s\n",
  2138. Spacing, Party->Number);
  2139. break;
  2140. case 5: /* Private PartyNumber */
  2141. chan_misdn_log(1, bc->port, " -->%s Private TON:%d %s\n",
  2142. Spacing, Party->TypeOfNumber, Party->Number);
  2143. break;
  2144. case 8: /* National Standard PartyNumber (Not used) */
  2145. chan_misdn_log(1, bc->port, " -->%s National: %s\n",
  2146. Spacing, Party->Number);
  2147. break;
  2148. default:
  2149. break;
  2150. }
  2151. }
  2152. }
  2153. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2154. #if defined(AST_MISDN_ENHANCEMENTS)
  2155. static void print_facility_Subaddress(unsigned Level, const struct FacPartySubaddress *Subaddress, const struct misdn_bchannel *bc)
  2156. {
  2157. if (Subaddress->Length) {
  2158. const char *Spacing;
  2159. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2160. chan_misdn_log(1, bc->port, " -->%s Subaddress: Type:%d\n",
  2161. Spacing, Subaddress->Type);
  2162. switch (Subaddress->Type) {
  2163. case 0: /* UserSpecified */
  2164. if (Subaddress->u.UserSpecified.OddCountPresent) {
  2165. chan_misdn_log(1, bc->port, " -->%s User BCD OddCount:%d NumOctets:%d\n",
  2166. Spacing, Subaddress->u.UserSpecified.OddCount, Subaddress->Length);
  2167. } else {
  2168. chan_misdn_log(1, bc->port, " -->%s User: %s\n",
  2169. Spacing, Subaddress->u.UserSpecified.Information);
  2170. }
  2171. break;
  2172. case 1: /* NSAP */
  2173. chan_misdn_log(1, bc->port, " -->%s NSAP: %s\n",
  2174. Spacing, Subaddress->u.Nsap);
  2175. break;
  2176. default:
  2177. break;
  2178. }
  2179. }
  2180. }
  2181. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2182. #if defined(AST_MISDN_ENHANCEMENTS)
  2183. static void print_facility_Address(unsigned Level, const struct FacAddress *Address, const struct misdn_bchannel *bc)
  2184. {
  2185. print_facility_PartyNumber(Level, &Address->Party, bc);
  2186. print_facility_Subaddress(Level, &Address->Subaddress, bc);
  2187. }
  2188. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2189. #if defined(AST_MISDN_ENHANCEMENTS)
  2190. static void print_facility_PresentedNumberUnscreened(unsigned Level, const struct FacPresentedNumberUnscreened *Presented, const struct misdn_bchannel *bc)
  2191. {
  2192. const char *Spacing;
  2193. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2194. chan_misdn_log(1, bc->port, " -->%s Unscreened Type:%d\n", Spacing, Presented->Type);
  2195. switch (Presented->Type) {
  2196. case 0: /* presentationAllowedNumber */
  2197. chan_misdn_log(1, bc->port, " -->%s Allowed:\n", Spacing);
  2198. print_facility_PartyNumber(Level + 2, &Presented->Unscreened, bc);
  2199. break;
  2200. case 1: /* presentationRestricted */
  2201. chan_misdn_log(1, bc->port, " -->%s Restricted\n", Spacing);
  2202. break;
  2203. case 2: /* numberNotAvailableDueToInterworking */
  2204. chan_misdn_log(1, bc->port, " -->%s Not Available\n", Spacing);
  2205. break;
  2206. case 3: /* presentationRestrictedNumber */
  2207. chan_misdn_log(1, bc->port, " -->%s Restricted:\n", Spacing);
  2208. print_facility_PartyNumber(Level + 2, &Presented->Unscreened, bc);
  2209. break;
  2210. default:
  2211. break;
  2212. }
  2213. }
  2214. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2215. #if defined(AST_MISDN_ENHANCEMENTS)
  2216. static void print_facility_AddressScreened(unsigned Level, const struct FacAddressScreened *Address, const struct misdn_bchannel *bc)
  2217. {
  2218. const char *Spacing;
  2219. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2220. chan_misdn_log(1, bc->port, " -->%s ScreeningIndicator:%d\n", Spacing, Address->ScreeningIndicator);
  2221. print_facility_PartyNumber(Level, &Address->Party, bc);
  2222. print_facility_Subaddress(Level, &Address->Subaddress, bc);
  2223. }
  2224. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2225. #if defined(AST_MISDN_ENHANCEMENTS)
  2226. static void print_facility_PresentedAddressScreened(unsigned Level, const struct FacPresentedAddressScreened *Presented, const struct misdn_bchannel *bc)
  2227. {
  2228. const char *Spacing;
  2229. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2230. chan_misdn_log(1, bc->port, " -->%s Screened Type:%d\n", Spacing, Presented->Type);
  2231. switch (Presented->Type) {
  2232. case 0: /* presentationAllowedAddress */
  2233. chan_misdn_log(1, bc->port, " -->%s Allowed:\n", Spacing);
  2234. print_facility_AddressScreened(Level + 2, &Presented->Address, bc);
  2235. break;
  2236. case 1: /* presentationRestricted */
  2237. chan_misdn_log(1, bc->port, " -->%s Restricted\n", Spacing);
  2238. break;
  2239. case 2: /* numberNotAvailableDueToInterworking */
  2240. chan_misdn_log(1, bc->port, " -->%s Not Available\n", Spacing);
  2241. break;
  2242. case 3: /* presentationRestrictedAddress */
  2243. chan_misdn_log(1, bc->port, " -->%s Restricted:\n", Spacing);
  2244. print_facility_AddressScreened(Level + 2, &Presented->Address, bc);
  2245. break;
  2246. default:
  2247. break;
  2248. }
  2249. }
  2250. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2251. #if defined(AST_MISDN_ENHANCEMENTS)
  2252. static void print_facility_Q931_Bc_Hlc_Llc(unsigned Level, const struct Q931_Bc_Hlc_Llc *Q931ie, const struct misdn_bchannel *bc)
  2253. {
  2254. const char *Spacing;
  2255. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2256. chan_misdn_log(1, bc->port, " -->%s Q931ie:\n", Spacing);
  2257. if (Q931ie->Bc.Length) {
  2258. chan_misdn_log(1, bc->port, " -->%s Bc Len:%d\n", Spacing, Q931ie->Bc.Length);
  2259. }
  2260. if (Q931ie->Hlc.Length) {
  2261. chan_misdn_log(1, bc->port, " -->%s Hlc Len:%d\n", Spacing, Q931ie->Hlc.Length);
  2262. }
  2263. if (Q931ie->Llc.Length) {
  2264. chan_misdn_log(1, bc->port, " -->%s Llc Len:%d\n", Spacing, Q931ie->Llc.Length);
  2265. }
  2266. }
  2267. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2268. #if defined(AST_MISDN_ENHANCEMENTS)
  2269. static void print_facility_Q931_Bc_Hlc_Llc_Uu(unsigned Level, const struct Q931_Bc_Hlc_Llc_Uu *Q931ie, const struct misdn_bchannel *bc)
  2270. {
  2271. const char *Spacing;
  2272. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2273. chan_misdn_log(1, bc->port, " -->%s Q931ie:\n", Spacing);
  2274. if (Q931ie->Bc.Length) {
  2275. chan_misdn_log(1, bc->port, " -->%s Bc Len:%d\n", Spacing, Q931ie->Bc.Length);
  2276. }
  2277. if (Q931ie->Hlc.Length) {
  2278. chan_misdn_log(1, bc->port, " -->%s Hlc Len:%d\n", Spacing, Q931ie->Hlc.Length);
  2279. }
  2280. if (Q931ie->Llc.Length) {
  2281. chan_misdn_log(1, bc->port, " -->%s Llc Len:%d\n", Spacing, Q931ie->Llc.Length);
  2282. }
  2283. if (Q931ie->UserInfo.Length) {
  2284. chan_misdn_log(1, bc->port, " -->%s UserInfo Len:%d\n", Spacing, Q931ie->UserInfo.Length);
  2285. }
  2286. }
  2287. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2288. #if defined(AST_MISDN_ENHANCEMENTS)
  2289. static void print_facility_CallInformation(unsigned Level, const struct FacCallInformation *CallInfo, const struct misdn_bchannel *bc)
  2290. {
  2291. const char *Spacing;
  2292. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2293. chan_misdn_log(1, bc->port, " -->%s CCBSReference:%d\n",
  2294. Spacing, CallInfo->CCBSReference);
  2295. chan_misdn_log(1, bc->port, " -->%s AddressOfB:\n", Spacing);
  2296. print_facility_Address(Level + 1, &CallInfo->AddressOfB, bc);
  2297. print_facility_Q931_Bc_Hlc_Llc(Level, &CallInfo->Q931ie, bc);
  2298. if (CallInfo->SubaddressOfA.Length) {
  2299. chan_misdn_log(1, bc->port, " -->%s SubaddressOfA:\n", Spacing);
  2300. print_facility_Subaddress(Level + 1, &CallInfo->SubaddressOfA, bc);
  2301. }
  2302. }
  2303. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2304. #if defined(AST_MISDN_ENHANCEMENTS)
  2305. static void print_facility_ServedUserNr(unsigned Level, const struct FacPartyNumber *Party, const struct misdn_bchannel *bc)
  2306. {
  2307. const char *Spacing;
  2308. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2309. if (Party->LengthOfNumber) {
  2310. print_facility_PartyNumber(Level, Party, bc);
  2311. } else {
  2312. chan_misdn_log(1, bc->port, " -->%s All Numbers\n", Spacing);
  2313. }
  2314. }
  2315. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2316. #if defined(AST_MISDN_ENHANCEMENTS)
  2317. static void print_facility_IntResult(unsigned Level, const struct FacForwardingRecord *ForwardingRecord, const struct misdn_bchannel *bc)
  2318. {
  2319. const char *Spacing;
  2320. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2321. chan_misdn_log(1, bc->port, " -->%s Procedure:%d BasicService:%d\n",
  2322. Spacing,
  2323. ForwardingRecord->Procedure,
  2324. ForwardingRecord->BasicService);
  2325. chan_misdn_log(1, bc->port, " -->%s ForwardedTo:\n", Spacing);
  2326. print_facility_Address(Level + 1, &ForwardingRecord->ForwardedTo, bc);
  2327. chan_misdn_log(1, bc->port, " -->%s ServedUserNr:\n", Spacing);
  2328. print_facility_ServedUserNr(Level + 1, &ForwardingRecord->ServedUser, bc);
  2329. }
  2330. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2331. static void print_facility(const struct FacParm *fac, const struct misdn_bchannel *bc)
  2332. {
  2333. #if defined(AST_MISDN_ENHANCEMENTS)
  2334. unsigned Index;
  2335. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2336. switch (fac->Function) {
  2337. #if defined(AST_MISDN_ENHANCEMENTS)
  2338. case Fac_ActivationDiversion:
  2339. chan_misdn_log(1, bc->port, " --> ActivationDiversion: InvokeID:%d\n",
  2340. fac->u.ActivationDiversion.InvokeID);
  2341. switch (fac->u.ActivationDiversion.ComponentType) {
  2342. case FacComponent_Invoke:
  2343. chan_misdn_log(1, bc->port, " --> Invoke: Procedure:%d BasicService:%d\n",
  2344. fac->u.ActivationDiversion.Component.Invoke.Procedure,
  2345. fac->u.ActivationDiversion.Component.Invoke.BasicService);
  2346. chan_misdn_log(1, bc->port, " --> ForwardedTo:\n");
  2347. print_facility_Address(3, &fac->u.ActivationDiversion.Component.Invoke.ForwardedTo, bc);
  2348. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2349. print_facility_ServedUserNr(3, &fac->u.ActivationDiversion.Component.Invoke.ServedUser, bc);
  2350. break;
  2351. case FacComponent_Result:
  2352. chan_misdn_log(1, bc->port, " --> Result\n");
  2353. break;
  2354. default:
  2355. break;
  2356. }
  2357. break;
  2358. case Fac_DeactivationDiversion:
  2359. chan_misdn_log(1, bc->port, " --> DeactivationDiversion: InvokeID:%d\n",
  2360. fac->u.DeactivationDiversion.InvokeID);
  2361. switch (fac->u.DeactivationDiversion.ComponentType) {
  2362. case FacComponent_Invoke:
  2363. chan_misdn_log(1, bc->port, " --> Invoke: Procedure:%d BasicService:%d\n",
  2364. fac->u.DeactivationDiversion.Component.Invoke.Procedure,
  2365. fac->u.DeactivationDiversion.Component.Invoke.BasicService);
  2366. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2367. print_facility_ServedUserNr(3, &fac->u.DeactivationDiversion.Component.Invoke.ServedUser, bc);
  2368. break;
  2369. case FacComponent_Result:
  2370. chan_misdn_log(1, bc->port, " --> Result\n");
  2371. break;
  2372. default:
  2373. break;
  2374. }
  2375. break;
  2376. case Fac_ActivationStatusNotificationDiv:
  2377. chan_misdn_log(1, bc->port, " --> ActivationStatusNotificationDiv: InvokeID:%d Procedure:%d BasicService:%d\n",
  2378. fac->u.ActivationStatusNotificationDiv.InvokeID,
  2379. fac->u.ActivationStatusNotificationDiv.Procedure,
  2380. fac->u.ActivationStatusNotificationDiv.BasicService);
  2381. chan_misdn_log(1, bc->port, " --> ForwardedTo:\n");
  2382. print_facility_Address(2, &fac->u.ActivationStatusNotificationDiv.ForwardedTo, bc);
  2383. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2384. print_facility_ServedUserNr(2, &fac->u.ActivationStatusNotificationDiv.ServedUser, bc);
  2385. break;
  2386. case Fac_DeactivationStatusNotificationDiv:
  2387. chan_misdn_log(1, bc->port, " --> DeactivationStatusNotificationDiv: InvokeID:%d Procedure:%d BasicService:%d\n",
  2388. fac->u.DeactivationStatusNotificationDiv.InvokeID,
  2389. fac->u.DeactivationStatusNotificationDiv.Procedure,
  2390. fac->u.DeactivationStatusNotificationDiv.BasicService);
  2391. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2392. print_facility_ServedUserNr(2, &fac->u.DeactivationStatusNotificationDiv.ServedUser, bc);
  2393. break;
  2394. case Fac_InterrogationDiversion:
  2395. chan_misdn_log(1, bc->port, " --> InterrogationDiversion: InvokeID:%d\n",
  2396. fac->u.InterrogationDiversion.InvokeID);
  2397. switch (fac->u.InterrogationDiversion.ComponentType) {
  2398. case FacComponent_Invoke:
  2399. chan_misdn_log(1, bc->port, " --> Invoke: Procedure:%d BasicService:%d\n",
  2400. fac->u.InterrogationDiversion.Component.Invoke.Procedure,
  2401. fac->u.InterrogationDiversion.Component.Invoke.BasicService);
  2402. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2403. print_facility_ServedUserNr(3, &fac->u.InterrogationDiversion.Component.Invoke.ServedUser, bc);
  2404. break;
  2405. case FacComponent_Result:
  2406. chan_misdn_log(1, bc->port, " --> Result:\n");
  2407. if (fac->u.InterrogationDiversion.Component.Result.NumRecords) {
  2408. for (Index = 0; Index < fac->u.InterrogationDiversion.Component.Result.NumRecords; ++Index) {
  2409. chan_misdn_log(1, bc->port, " --> IntResult[%d]:\n", Index);
  2410. print_facility_IntResult(3, &fac->u.InterrogationDiversion.Component.Result.List[Index], bc);
  2411. }
  2412. }
  2413. break;
  2414. default:
  2415. break;
  2416. }
  2417. break;
  2418. case Fac_DiversionInformation:
  2419. chan_misdn_log(1, bc->port, " --> DiversionInformation: InvokeID:%d Reason:%d BasicService:%d\n",
  2420. fac->u.DiversionInformation.InvokeID,
  2421. fac->u.DiversionInformation.DiversionReason,
  2422. fac->u.DiversionInformation.BasicService);
  2423. if (fac->u.DiversionInformation.ServedUserSubaddress.Length) {
  2424. chan_misdn_log(1, bc->port, " --> ServedUserSubaddress:\n");
  2425. print_facility_Subaddress(2, &fac->u.DiversionInformation.ServedUserSubaddress, bc);
  2426. }
  2427. if (fac->u.DiversionInformation.CallingAddressPresent) {
  2428. chan_misdn_log(1, bc->port, " --> CallingAddress:\n");
  2429. print_facility_PresentedAddressScreened(2, &fac->u.DiversionInformation.CallingAddress, bc);
  2430. }
  2431. if (fac->u.DiversionInformation.OriginalCalledPresent) {
  2432. chan_misdn_log(1, bc->port, " --> OriginalCalledNr:\n");
  2433. print_facility_PresentedNumberUnscreened(2, &fac->u.DiversionInformation.OriginalCalled, bc);
  2434. }
  2435. if (fac->u.DiversionInformation.LastDivertingPresent) {
  2436. chan_misdn_log(1, bc->port, " --> LastDivertingNr:\n");
  2437. print_facility_PresentedNumberUnscreened(2, &fac->u.DiversionInformation.LastDiverting, bc);
  2438. }
  2439. if (fac->u.DiversionInformation.LastDivertingReasonPresent) {
  2440. chan_misdn_log(1, bc->port, " --> LastDivertingReason:%d\n", fac->u.DiversionInformation.LastDivertingReason);
  2441. }
  2442. if (fac->u.DiversionInformation.UserInfo.Length) {
  2443. chan_misdn_log(1, bc->port, " --> UserInfo Length:%d\n", fac->u.DiversionInformation.UserInfo.Length);
  2444. }
  2445. break;
  2446. case Fac_CallDeflection:
  2447. chan_misdn_log(1, bc->port, " --> CallDeflection: InvokeID:%d\n",
  2448. fac->u.CallDeflection.InvokeID);
  2449. switch (fac->u.CallDeflection.ComponentType) {
  2450. case FacComponent_Invoke:
  2451. chan_misdn_log(1, bc->port, " --> Invoke:\n");
  2452. if (fac->u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent) {
  2453. chan_misdn_log(1, bc->port, " --> PresentationAllowed:%d\n",
  2454. fac->u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser);
  2455. }
  2456. chan_misdn_log(1, bc->port, " --> DeflectionAddress:\n");
  2457. print_facility_Address(3, &fac->u.CallDeflection.Component.Invoke.Deflection, bc);
  2458. break;
  2459. case FacComponent_Result:
  2460. chan_misdn_log(1, bc->port, " --> Result\n");
  2461. break;
  2462. default:
  2463. break;
  2464. }
  2465. break;
  2466. case Fac_CallRerouteing:
  2467. chan_misdn_log(1, bc->port, " --> CallRerouteing: InvokeID:%d\n",
  2468. fac->u.CallRerouteing.InvokeID);
  2469. switch (fac->u.CallRerouteing.ComponentType) {
  2470. case FacComponent_Invoke:
  2471. chan_misdn_log(1, bc->port, " --> Invoke: Reason:%d Counter:%d\n",
  2472. fac->u.CallRerouteing.Component.Invoke.ReroutingReason,
  2473. fac->u.CallRerouteing.Component.Invoke.ReroutingCounter);
  2474. chan_misdn_log(1, bc->port, " --> CalledAddress:\n");
  2475. print_facility_Address(3, &fac->u.CallRerouteing.Component.Invoke.CalledAddress, bc);
  2476. print_facility_Q931_Bc_Hlc_Llc_Uu(2, &fac->u.CallRerouteing.Component.Invoke.Q931ie, bc);
  2477. chan_misdn_log(1, bc->port, " --> LastReroutingNr:\n");
  2478. print_facility_PresentedNumberUnscreened(3, &fac->u.CallRerouteing.Component.Invoke.LastRerouting, bc);
  2479. chan_misdn_log(1, bc->port, " --> SubscriptionOption:%d\n",
  2480. fac->u.CallRerouteing.Component.Invoke.SubscriptionOption);
  2481. if (fac->u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length) {
  2482. chan_misdn_log(1, bc->port, " --> CallingParty:\n");
  2483. print_facility_Subaddress(3, &fac->u.CallRerouteing.Component.Invoke.CallingPartySubaddress, bc);
  2484. }
  2485. break;
  2486. case FacComponent_Result:
  2487. chan_misdn_log(1, bc->port, " --> Result\n");
  2488. break;
  2489. default:
  2490. break;
  2491. }
  2492. break;
  2493. case Fac_InterrogateServedUserNumbers:
  2494. chan_misdn_log(1, bc->port, " --> InterrogateServedUserNumbers: InvokeID:%d\n",
  2495. fac->u.InterrogateServedUserNumbers.InvokeID);
  2496. switch (fac->u.InterrogateServedUserNumbers.ComponentType) {
  2497. case FacComponent_Invoke:
  2498. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2499. break;
  2500. case FacComponent_Result:
  2501. chan_misdn_log(1, bc->port, " --> Result:\n");
  2502. if (fac->u.InterrogateServedUserNumbers.Component.Result.NumRecords) {
  2503. for (Index = 0; Index < fac->u.InterrogateServedUserNumbers.Component.Result.NumRecords; ++Index) {
  2504. chan_misdn_log(1, bc->port, " --> ServedUserNr[%d]:\n", Index);
  2505. print_facility_PartyNumber(3, &fac->u.InterrogateServedUserNumbers.Component.Result.List[Index], bc);
  2506. }
  2507. }
  2508. break;
  2509. default:
  2510. break;
  2511. }
  2512. break;
  2513. case Fac_DivertingLegInformation1:
  2514. chan_misdn_log(1, bc->port, " --> DivertingLegInformation1: InvokeID:%d Reason:%d SubscriptionOption:%d\n",
  2515. fac->u.DivertingLegInformation1.InvokeID,
  2516. fac->u.DivertingLegInformation1.DiversionReason,
  2517. fac->u.DivertingLegInformation1.SubscriptionOption);
  2518. if (fac->u.DivertingLegInformation1.DivertedToPresent) {
  2519. chan_misdn_log(1, bc->port, " --> DivertedToNr:\n");
  2520. print_facility_PresentedNumberUnscreened(2, &fac->u.DivertingLegInformation1.DivertedTo, bc);
  2521. }
  2522. break;
  2523. case Fac_DivertingLegInformation2:
  2524. chan_misdn_log(1, bc->port, " --> DivertingLegInformation2: InvokeID:%d Reason:%d Count:%d\n",
  2525. fac->u.DivertingLegInformation2.InvokeID,
  2526. fac->u.DivertingLegInformation2.DiversionReason,
  2527. fac->u.DivertingLegInformation2.DiversionCounter);
  2528. if (fac->u.DivertingLegInformation2.DivertingPresent) {
  2529. chan_misdn_log(1, bc->port, " --> DivertingNr:\n");
  2530. print_facility_PresentedNumberUnscreened(2, &fac->u.DivertingLegInformation2.Diverting, bc);
  2531. }
  2532. if (fac->u.DivertingLegInformation2.OriginalCalledPresent) {
  2533. chan_misdn_log(1, bc->port, " --> OriginalCalledNr:\n");
  2534. print_facility_PresentedNumberUnscreened(2, &fac->u.DivertingLegInformation2.OriginalCalled, bc);
  2535. }
  2536. break;
  2537. case Fac_DivertingLegInformation3:
  2538. chan_misdn_log(1, bc->port, " --> DivertingLegInformation3: InvokeID:%d PresentationAllowed:%d\n",
  2539. fac->u.DivertingLegInformation3.InvokeID,
  2540. fac->u.DivertingLegInformation3.PresentationAllowedIndicator);
  2541. break;
  2542. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  2543. case Fac_CD:
  2544. chan_misdn_log(1, bc->port, " --> calldeflect to: %s, presentable: %s\n", fac->u.CDeflection.DeflectedToNumber,
  2545. fac->u.CDeflection.PresentationAllowed ? "yes" : "no");
  2546. break;
  2547. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  2548. case Fac_AOCDCurrency:
  2549. if (fac->u.AOCDcur.chargeNotAvailable) {
  2550. chan_misdn_log(1, bc->port, " --> AOCD currency: charge not available\n");
  2551. } else if (fac->u.AOCDcur.freeOfCharge) {
  2552. chan_misdn_log(1, bc->port, " --> AOCD currency: free of charge\n");
  2553. } else if (fac->u.AOCDchu.billingId >= 0) {
  2554. chan_misdn_log(1, bc->port, " --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s billingId:%d\n",
  2555. fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
  2556. (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDcur.billingId);
  2557. } else {
  2558. chan_misdn_log(1, bc->port, " --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s\n",
  2559. fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
  2560. (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total");
  2561. }
  2562. break;
  2563. case Fac_AOCDChargingUnit:
  2564. if (fac->u.AOCDchu.chargeNotAvailable) {
  2565. chan_misdn_log(1, bc->port, " --> AOCD charging unit: charge not available\n");
  2566. } else if (fac->u.AOCDchu.freeOfCharge) {
  2567. chan_misdn_log(1, bc->port, " --> AOCD charging unit: free of charge\n");
  2568. } else if (fac->u.AOCDchu.billingId >= 0) {
  2569. chan_misdn_log(1, bc->port, " --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s billingId:%d\n",
  2570. fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDchu.billingId);
  2571. } else {
  2572. chan_misdn_log(1, bc->port, " --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s\n",
  2573. fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total");
  2574. }
  2575. break;
  2576. #if defined(AST_MISDN_ENHANCEMENTS)
  2577. case Fac_ERROR:
  2578. chan_misdn_log(1, bc->port, " --> ERROR: InvokeID:%d, Code:0x%02x\n",
  2579. fac->u.ERROR.invokeId, fac->u.ERROR.errorValue);
  2580. break;
  2581. case Fac_RESULT:
  2582. chan_misdn_log(1, bc->port, " --> RESULT: InvokeID:%d\n",
  2583. fac->u.RESULT.InvokeID);
  2584. break;
  2585. case Fac_REJECT:
  2586. if (fac->u.REJECT.InvokeIDPresent) {
  2587. chan_misdn_log(1, bc->port, " --> REJECT: InvokeID:%d, Code:0x%02x\n",
  2588. fac->u.REJECT.InvokeID, fac->u.REJECT.Code);
  2589. } else {
  2590. chan_misdn_log(1, bc->port, " --> REJECT: Code:0x%02x\n",
  2591. fac->u.REJECT.Code);
  2592. }
  2593. break;
  2594. case Fac_EctExecute:
  2595. chan_misdn_log(1, bc->port, " --> EctExecute: InvokeID:%d\n",
  2596. fac->u.EctExecute.InvokeID);
  2597. break;
  2598. case Fac_ExplicitEctExecute:
  2599. chan_misdn_log(1, bc->port, " --> ExplicitEctExecute: InvokeID:%d LinkID:%d\n",
  2600. fac->u.ExplicitEctExecute.InvokeID,
  2601. fac->u.ExplicitEctExecute.LinkID);
  2602. break;
  2603. case Fac_RequestSubaddress:
  2604. chan_misdn_log(1, bc->port, " --> RequestSubaddress: InvokeID:%d\n",
  2605. fac->u.RequestSubaddress.InvokeID);
  2606. break;
  2607. case Fac_SubaddressTransfer:
  2608. chan_misdn_log(1, bc->port, " --> SubaddressTransfer: InvokeID:%d\n",
  2609. fac->u.SubaddressTransfer.InvokeID);
  2610. print_facility_Subaddress(1, &fac->u.SubaddressTransfer.Subaddress, bc);
  2611. break;
  2612. case Fac_EctLinkIdRequest:
  2613. chan_misdn_log(1, bc->port, " --> EctLinkIdRequest: InvokeID:%d\n",
  2614. fac->u.EctLinkIdRequest.InvokeID);
  2615. switch (fac->u.EctLinkIdRequest.ComponentType) {
  2616. case FacComponent_Invoke:
  2617. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2618. break;
  2619. case FacComponent_Result:
  2620. chan_misdn_log(1, bc->port, " --> Result: LinkID:%d\n",
  2621. fac->u.EctLinkIdRequest.Component.Result.LinkID);
  2622. break;
  2623. default:
  2624. break;
  2625. }
  2626. break;
  2627. case Fac_EctInform:
  2628. chan_misdn_log(1, bc->port, " --> EctInform: InvokeID:%d Status:%d\n",
  2629. fac->u.EctInform.InvokeID,
  2630. fac->u.EctInform.Status);
  2631. if (fac->u.EctInform.RedirectionPresent) {
  2632. chan_misdn_log(1, bc->port, " --> Redirection Number\n");
  2633. print_facility_PresentedNumberUnscreened(2, &fac->u.EctInform.Redirection, bc);
  2634. }
  2635. break;
  2636. case Fac_EctLoopTest:
  2637. chan_misdn_log(1, bc->port, " --> EctLoopTest: InvokeID:%d\n",
  2638. fac->u.EctLoopTest.InvokeID);
  2639. switch (fac->u.EctLoopTest.ComponentType) {
  2640. case FacComponent_Invoke:
  2641. chan_misdn_log(1, bc->port, " --> Invoke: CallTransferID:%d\n",
  2642. fac->u.EctLoopTest.Component.Invoke.CallTransferID);
  2643. break;
  2644. case FacComponent_Result:
  2645. chan_misdn_log(1, bc->port, " --> Result: LoopResult:%d\n",
  2646. fac->u.EctLoopTest.Component.Result.LoopResult);
  2647. break;
  2648. default:
  2649. break;
  2650. }
  2651. break;
  2652. case Fac_StatusRequest:
  2653. chan_misdn_log(1, bc->port, " --> StatusRequest: InvokeID:%d\n",
  2654. fac->u.StatusRequest.InvokeID);
  2655. switch (fac->u.StatusRequest.ComponentType) {
  2656. case FacComponent_Invoke:
  2657. chan_misdn_log(1, bc->port, " --> Invoke: Compatibility:%d\n",
  2658. fac->u.StatusRequest.Component.Invoke.CompatibilityMode);
  2659. break;
  2660. case FacComponent_Result:
  2661. chan_misdn_log(1, bc->port, " --> Result: Status:%d\n",
  2662. fac->u.StatusRequest.Component.Result.Status);
  2663. break;
  2664. default:
  2665. break;
  2666. }
  2667. break;
  2668. case Fac_CallInfoRetain:
  2669. chan_misdn_log(1, bc->port, " --> CallInfoRetain: InvokeID:%d, LinkageID:%d\n",
  2670. fac->u.CallInfoRetain.InvokeID, fac->u.CallInfoRetain.CallLinkageID);
  2671. break;
  2672. case Fac_CCBSDeactivate:
  2673. chan_misdn_log(1, bc->port, " --> CCBSDeactivate: InvokeID:%d\n",
  2674. fac->u.CCBSDeactivate.InvokeID);
  2675. switch (fac->u.CCBSDeactivate.ComponentType) {
  2676. case FacComponent_Invoke:
  2677. chan_misdn_log(1, bc->port, " --> Invoke: CCBSReference:%d\n",
  2678. fac->u.CCBSDeactivate.Component.Invoke.CCBSReference);
  2679. break;
  2680. case FacComponent_Result:
  2681. chan_misdn_log(1, bc->port, " --> Result\n");
  2682. break;
  2683. default:
  2684. break;
  2685. }
  2686. break;
  2687. case Fac_CCBSErase:
  2688. chan_misdn_log(1, bc->port, " --> CCBSErase: InvokeID:%d, CCBSReference:%d RecallMode:%d, Reason:%d\n",
  2689. fac->u.CCBSErase.InvokeID, fac->u.CCBSErase.CCBSReference,
  2690. fac->u.CCBSErase.RecallMode, fac->u.CCBSErase.Reason);
  2691. chan_misdn_log(1, bc->port, " --> AddressOfB\n");
  2692. print_facility_Address(2, &fac->u.CCBSErase.AddressOfB, bc);
  2693. print_facility_Q931_Bc_Hlc_Llc(1, &fac->u.CCBSErase.Q931ie, bc);
  2694. break;
  2695. case Fac_CCBSRemoteUserFree:
  2696. chan_misdn_log(1, bc->port, " --> CCBSRemoteUserFree: InvokeID:%d, CCBSReference:%d RecallMode:%d\n",
  2697. fac->u.CCBSRemoteUserFree.InvokeID, fac->u.CCBSRemoteUserFree.CCBSReference,
  2698. fac->u.CCBSRemoteUserFree.RecallMode);
  2699. chan_misdn_log(1, bc->port, " --> AddressOfB\n");
  2700. print_facility_Address(2, &fac->u.CCBSRemoteUserFree.AddressOfB, bc);
  2701. print_facility_Q931_Bc_Hlc_Llc(1, &fac->u.CCBSRemoteUserFree.Q931ie, bc);
  2702. break;
  2703. case Fac_CCBSCall:
  2704. chan_misdn_log(1, bc->port, " --> CCBSCall: InvokeID:%d, CCBSReference:%d\n",
  2705. fac->u.CCBSCall.InvokeID, fac->u.CCBSCall.CCBSReference);
  2706. break;
  2707. case Fac_CCBSStatusRequest:
  2708. chan_misdn_log(1, bc->port, " --> CCBSStatusRequest: InvokeID:%d\n",
  2709. fac->u.CCBSStatusRequest.InvokeID);
  2710. switch (fac->u.CCBSStatusRequest.ComponentType) {
  2711. case FacComponent_Invoke:
  2712. chan_misdn_log(1, bc->port, " --> Invoke: CCBSReference:%d RecallMode:%d\n",
  2713. fac->u.CCBSStatusRequest.Component.Invoke.CCBSReference,
  2714. fac->u.CCBSStatusRequest.Component.Invoke.RecallMode);
  2715. print_facility_Q931_Bc_Hlc_Llc(2, &fac->u.CCBSStatusRequest.Component.Invoke.Q931ie, bc);
  2716. break;
  2717. case FacComponent_Result:
  2718. chan_misdn_log(1, bc->port, " --> Result: Free:%d\n",
  2719. fac->u.CCBSStatusRequest.Component.Result.Free);
  2720. break;
  2721. default:
  2722. break;
  2723. }
  2724. break;
  2725. case Fac_CCBSBFree:
  2726. chan_misdn_log(1, bc->port, " --> CCBSBFree: InvokeID:%d, CCBSReference:%d RecallMode:%d\n",
  2727. fac->u.CCBSBFree.InvokeID, fac->u.CCBSBFree.CCBSReference,
  2728. fac->u.CCBSBFree.RecallMode);
  2729. chan_misdn_log(1, bc->port, " --> AddressOfB\n");
  2730. print_facility_Address(2, &fac->u.CCBSBFree.AddressOfB, bc);
  2731. print_facility_Q931_Bc_Hlc_Llc(1, &fac->u.CCBSBFree.Q931ie, bc);
  2732. break;
  2733. case Fac_EraseCallLinkageID:
  2734. chan_misdn_log(1, bc->port, " --> EraseCallLinkageID: InvokeID:%d, LinkageID:%d\n",
  2735. fac->u.EraseCallLinkageID.InvokeID, fac->u.EraseCallLinkageID.CallLinkageID);
  2736. break;
  2737. case Fac_CCBSStopAlerting:
  2738. chan_misdn_log(1, bc->port, " --> CCBSStopAlerting: InvokeID:%d, CCBSReference:%d\n",
  2739. fac->u.CCBSStopAlerting.InvokeID, fac->u.CCBSStopAlerting.CCBSReference);
  2740. break;
  2741. case Fac_CCBSRequest:
  2742. chan_misdn_log(1, bc->port, " --> CCBSRequest: InvokeID:%d\n",
  2743. fac->u.CCBSRequest.InvokeID);
  2744. switch (fac->u.CCBSRequest.ComponentType) {
  2745. case FacComponent_Invoke:
  2746. chan_misdn_log(1, bc->port, " --> Invoke: LinkageID:%d\n",
  2747. fac->u.CCBSRequest.Component.Invoke.CallLinkageID);
  2748. break;
  2749. case FacComponent_Result:
  2750. chan_misdn_log(1, bc->port, " --> Result: CCBSReference:%d RecallMode:%d\n",
  2751. fac->u.CCBSRequest.Component.Result.CCBSReference,
  2752. fac->u.CCBSRequest.Component.Result.RecallMode);
  2753. break;
  2754. default:
  2755. break;
  2756. }
  2757. break;
  2758. case Fac_CCBSInterrogate:
  2759. chan_misdn_log(1, bc->port, " --> CCBSInterrogate: InvokeID:%d\n",
  2760. fac->u.CCBSInterrogate.InvokeID);
  2761. switch (fac->u.CCBSInterrogate.ComponentType) {
  2762. case FacComponent_Invoke:
  2763. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2764. if (fac->u.CCBSInterrogate.Component.Invoke.CCBSReferencePresent) {
  2765. chan_misdn_log(1, bc->port, " --> CCBSReference:%d\n",
  2766. fac->u.CCBSInterrogate.Component.Invoke.CCBSReference);
  2767. }
  2768. if (fac->u.CCBSInterrogate.Component.Invoke.AParty.LengthOfNumber) {
  2769. chan_misdn_log(1, bc->port, " --> AParty\n");
  2770. print_facility_PartyNumber(3, &fac->u.CCBSInterrogate.Component.Invoke.AParty, bc);
  2771. }
  2772. break;
  2773. case FacComponent_Result:
  2774. chan_misdn_log(1, bc->port, " --> Result: RecallMode:%d\n",
  2775. fac->u.CCBSInterrogate.Component.Result.RecallMode);
  2776. if (fac->u.CCBSInterrogate.Component.Result.NumRecords) {
  2777. for (Index = 0; Index < fac->u.CCBSInterrogate.Component.Result.NumRecords; ++Index) {
  2778. chan_misdn_log(1, bc->port, " --> CallDetails[%d]:\n", Index);
  2779. print_facility_CallInformation(3, &fac->u.CCBSInterrogate.Component.Result.CallDetails[Index], bc);
  2780. }
  2781. }
  2782. break;
  2783. default:
  2784. break;
  2785. }
  2786. break;
  2787. case Fac_CCNRRequest:
  2788. chan_misdn_log(1, bc->port, " --> CCNRRequest: InvokeID:%d\n",
  2789. fac->u.CCNRRequest.InvokeID);
  2790. switch (fac->u.CCNRRequest.ComponentType) {
  2791. case FacComponent_Invoke:
  2792. chan_misdn_log(1, bc->port, " --> Invoke: LinkageID:%d\n",
  2793. fac->u.CCNRRequest.Component.Invoke.CallLinkageID);
  2794. break;
  2795. case FacComponent_Result:
  2796. chan_misdn_log(1, bc->port, " --> Result: CCBSReference:%d RecallMode:%d\n",
  2797. fac->u.CCNRRequest.Component.Result.CCBSReference,
  2798. fac->u.CCNRRequest.Component.Result.RecallMode);
  2799. break;
  2800. default:
  2801. break;
  2802. }
  2803. break;
  2804. case Fac_CCNRInterrogate:
  2805. chan_misdn_log(1, bc->port, " --> CCNRInterrogate: InvokeID:%d\n",
  2806. fac->u.CCNRInterrogate.InvokeID);
  2807. switch (fac->u.CCNRInterrogate.ComponentType) {
  2808. case FacComponent_Invoke:
  2809. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2810. if (fac->u.CCNRInterrogate.Component.Invoke.CCBSReferencePresent) {
  2811. chan_misdn_log(1, bc->port, " --> CCBSReference:%d\n",
  2812. fac->u.CCNRInterrogate.Component.Invoke.CCBSReference);
  2813. }
  2814. if (fac->u.CCNRInterrogate.Component.Invoke.AParty.LengthOfNumber) {
  2815. chan_misdn_log(1, bc->port, " --> AParty\n");
  2816. print_facility_PartyNumber(3, &fac->u.CCNRInterrogate.Component.Invoke.AParty, bc);
  2817. }
  2818. break;
  2819. case FacComponent_Result:
  2820. chan_misdn_log(1, bc->port, " --> Result: RecallMode:%d\n",
  2821. fac->u.CCNRInterrogate.Component.Result.RecallMode);
  2822. if (fac->u.CCNRInterrogate.Component.Result.NumRecords) {
  2823. for (Index = 0; Index < fac->u.CCNRInterrogate.Component.Result.NumRecords; ++Index) {
  2824. chan_misdn_log(1, bc->port, " --> CallDetails[%d]:\n", Index);
  2825. print_facility_CallInformation(3, &fac->u.CCNRInterrogate.Component.Result.CallDetails[Index], bc);
  2826. }
  2827. }
  2828. break;
  2829. default:
  2830. break;
  2831. }
  2832. break;
  2833. case Fac_CCBS_T_Call:
  2834. chan_misdn_log(1, bc->port, " --> CCBS_T_Call: InvokeID:%d\n",
  2835. fac->u.CCBS_T_Call.InvokeID);
  2836. break;
  2837. case Fac_CCBS_T_Suspend:
  2838. chan_misdn_log(1, bc->port, " --> CCBS_T_Suspend: InvokeID:%d\n",
  2839. fac->u.CCBS_T_Suspend.InvokeID);
  2840. break;
  2841. case Fac_CCBS_T_Resume:
  2842. chan_misdn_log(1, bc->port, " --> CCBS_T_Resume: InvokeID:%d\n",
  2843. fac->u.CCBS_T_Resume.InvokeID);
  2844. break;
  2845. case Fac_CCBS_T_RemoteUserFree:
  2846. chan_misdn_log(1, bc->port, " --> CCBS_T_RemoteUserFree: InvokeID:%d\n",
  2847. fac->u.CCBS_T_RemoteUserFree.InvokeID);
  2848. break;
  2849. case Fac_CCBS_T_Available:
  2850. chan_misdn_log(1, bc->port, " --> CCBS_T_Available: InvokeID:%d\n",
  2851. fac->u.CCBS_T_Available.InvokeID);
  2852. break;
  2853. case Fac_CCBS_T_Request:
  2854. chan_misdn_log(1, bc->port, " --> CCBS_T_Request: InvokeID:%d\n",
  2855. fac->u.CCBS_T_Request.InvokeID);
  2856. switch (fac->u.CCBS_T_Request.ComponentType) {
  2857. case FacComponent_Invoke:
  2858. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2859. chan_misdn_log(1, bc->port, " --> DestinationAddress:\n");
  2860. print_facility_Address(3, &fac->u.CCBS_T_Request.Component.Invoke.Destination, bc);
  2861. print_facility_Q931_Bc_Hlc_Llc(2, &fac->u.CCBS_T_Request.Component.Invoke.Q931ie, bc);
  2862. if (fac->u.CCBS_T_Request.Component.Invoke.RetentionSupported) {
  2863. chan_misdn_log(1, bc->port, " --> RetentionSupported:1\n");
  2864. }
  2865. if (fac->u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent) {
  2866. chan_misdn_log(1, bc->port, " --> PresentationAllowed:%d\n",
  2867. fac->u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator);
  2868. }
  2869. if (fac->u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber) {
  2870. chan_misdn_log(1, bc->port, " --> OriginatingAddress:\n");
  2871. print_facility_Address(3, &fac->u.CCBS_T_Request.Component.Invoke.Originating, bc);
  2872. }
  2873. break;
  2874. case FacComponent_Result:
  2875. chan_misdn_log(1, bc->port, " --> Result: RetentionSupported:%d\n",
  2876. fac->u.CCBS_T_Request.Component.Result.RetentionSupported);
  2877. break;
  2878. default:
  2879. break;
  2880. }
  2881. break;
  2882. case Fac_CCNR_T_Request:
  2883. chan_misdn_log(1, bc->port, " --> CCNR_T_Request: InvokeID:%d\n",
  2884. fac->u.CCNR_T_Request.InvokeID);
  2885. switch (fac->u.CCNR_T_Request.ComponentType) {
  2886. case FacComponent_Invoke:
  2887. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2888. chan_misdn_log(1, bc->port, " --> DestinationAddress:\n");
  2889. print_facility_Address(3, &fac->u.CCNR_T_Request.Component.Invoke.Destination, bc);
  2890. print_facility_Q931_Bc_Hlc_Llc(2, &fac->u.CCNR_T_Request.Component.Invoke.Q931ie, bc);
  2891. if (fac->u.CCNR_T_Request.Component.Invoke.RetentionSupported) {
  2892. chan_misdn_log(1, bc->port, " --> RetentionSupported:1\n");
  2893. }
  2894. if (fac->u.CCNR_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent) {
  2895. chan_misdn_log(1, bc->port, " --> PresentationAllowed:%d\n",
  2896. fac->u.CCNR_T_Request.Component.Invoke.PresentationAllowedIndicator);
  2897. }
  2898. if (fac->u.CCNR_T_Request.Component.Invoke.Originating.Party.LengthOfNumber) {
  2899. chan_misdn_log(1, bc->port, " --> OriginatingAddress:\n");
  2900. print_facility_Address(3, &fac->u.CCNR_T_Request.Component.Invoke.Originating, bc);
  2901. }
  2902. break;
  2903. case FacComponent_Result:
  2904. chan_misdn_log(1, bc->port, " --> Result: RetentionSupported:%d\n",
  2905. fac->u.CCNR_T_Request.Component.Result.RetentionSupported);
  2906. break;
  2907. default:
  2908. break;
  2909. }
  2910. break;
  2911. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2912. case Fac_None:
  2913. /* No facility so print nothing */
  2914. break;
  2915. default:
  2916. chan_misdn_log(1, bc->port, " --> unknown facility\n");
  2917. break;
  2918. }
  2919. }
  2920. static void print_bearer(struct misdn_bchannel *bc)
  2921. {
  2922. chan_misdn_log(2, bc->port, " --> Bearer: %s\n", bearer2str(bc->capability));
  2923. switch(bc->law) {
  2924. case INFO_CODEC_ALAW:
  2925. chan_misdn_log(2, bc->port, " --> Codec: Alaw\n");
  2926. break;
  2927. case INFO_CODEC_ULAW:
  2928. chan_misdn_log(2, bc->port, " --> Codec: Ulaw\n");
  2929. break;
  2930. }
  2931. }
  2932. /*!
  2933. * \internal
  2934. * \brief Prefix a string to another string in place.
  2935. *
  2936. * \param str_prefix String to prefix to the main string.
  2937. * \param str_main String to get the prefix added to it.
  2938. * \param size Buffer size of the main string (Includes null terminator).
  2939. *
  2940. * \note The str_main buffer size must be greater than one.
  2941. *
  2942. * \return Nothing
  2943. */
  2944. static void misdn_prefix_string(const char *str_prefix, char *str_main, size_t size)
  2945. {
  2946. size_t len_over;
  2947. size_t len_total;
  2948. size_t len_main;
  2949. size_t len_prefix;
  2950. len_prefix = strlen(str_prefix);
  2951. if (!len_prefix) {
  2952. /* There is no prefix to prepend. */
  2953. return;
  2954. }
  2955. len_main = strlen(str_main);
  2956. len_total = len_prefix + len_main;
  2957. if (size <= len_total) {
  2958. /* We need to truncate since the buffer is too small. */
  2959. len_over = len_total + 1 - size;
  2960. if (len_over <= len_main) {
  2961. len_main -= len_over;
  2962. } else {
  2963. len_over -= len_main;
  2964. len_main = 0;
  2965. len_prefix -= len_over;
  2966. }
  2967. }
  2968. if (len_main) {
  2969. memmove(str_main + len_prefix, str_main, len_main);
  2970. }
  2971. memcpy(str_main, str_prefix, len_prefix);
  2972. str_main[len_prefix + len_main] = '\0';
  2973. }
  2974. /*!
  2975. * \internal
  2976. * \brief Add a configured prefix to the given number.
  2977. *
  2978. * \param port Logical port number
  2979. * \param number_type Type-of-number passed in.
  2980. * \param number Given number string to add prefix
  2981. * \param size Buffer size number string occupies.
  2982. *
  2983. * \return Nothing
  2984. */
  2985. static void misdn_add_number_prefix(int port, enum mISDN_NUMBER_TYPE number_type, char *number, size_t size)
  2986. {
  2987. enum misdn_cfg_elements type_prefix;
  2988. char num_prefix[MISDN_MAX_NUMBER_LEN];
  2989. /* Get prefix string. */
  2990. switch (number_type) {
  2991. case NUMTYPE_UNKNOWN:
  2992. type_prefix = MISDN_CFG_TON_PREFIX_UNKNOWN;
  2993. break;
  2994. case NUMTYPE_INTERNATIONAL:
  2995. type_prefix = MISDN_CFG_TON_PREFIX_INTERNATIONAL;
  2996. break;
  2997. case NUMTYPE_NATIONAL:
  2998. type_prefix = MISDN_CFG_TON_PREFIX_NATIONAL;
  2999. break;
  3000. case NUMTYPE_NETWORK_SPECIFIC:
  3001. type_prefix = MISDN_CFG_TON_PREFIX_NETWORK_SPECIFIC;
  3002. break;
  3003. case NUMTYPE_SUBSCRIBER:
  3004. type_prefix = MISDN_CFG_TON_PREFIX_SUBSCRIBER;
  3005. break;
  3006. case NUMTYPE_ABBREVIATED:
  3007. type_prefix = MISDN_CFG_TON_PREFIX_ABBREVIATED;
  3008. break;
  3009. default:
  3010. /* Type-of-number does not have a prefix that can be added. */
  3011. return;
  3012. }
  3013. misdn_cfg_get(port, type_prefix, num_prefix, sizeof(num_prefix));
  3014. misdn_prefix_string(num_prefix, number, size);
  3015. }
  3016. static void export_aoc_vars(int originator, struct ast_channel *ast, struct misdn_bchannel *bc)
  3017. {
  3018. char buf[128];
  3019. if (!bc->AOCD_need_export || !ast) {
  3020. return;
  3021. }
  3022. if (originator == ORG_AST) {
  3023. ast = ast_bridged_channel(ast);
  3024. if (!ast) {
  3025. return;
  3026. }
  3027. }
  3028. switch (bc->AOCDtype) {
  3029. case Fac_AOCDCurrency:
  3030. pbx_builtin_setvar_helper(ast, "AOCD_Type", "currency");
  3031. if (bc->AOCD.currency.chargeNotAvailable) {
  3032. pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "no");
  3033. } else {
  3034. pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "yes");
  3035. if (bc->AOCD.currency.freeOfCharge) {
  3036. pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "yes");
  3037. } else {
  3038. pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "no");
  3039. if (snprintf(buf, sizeof(buf), "%d %s", bc->AOCD.currency.currencyAmount * bc->AOCD.currency.multiplier, bc->AOCD.currency.currency) < sizeof(buf)) {
  3040. pbx_builtin_setvar_helper(ast, "AOCD_Amount", buf);
  3041. if (bc->AOCD.currency.billingId >= 0 && snprintf(buf, sizeof(buf), "%d", bc->AOCD.currency.billingId) < sizeof(buf)) {
  3042. pbx_builtin_setvar_helper(ast, "AOCD_BillingId", buf);
  3043. }
  3044. }
  3045. }
  3046. }
  3047. break;
  3048. case Fac_AOCDChargingUnit:
  3049. pbx_builtin_setvar_helper(ast, "AOCD_Type", "charging_unit");
  3050. if (bc->AOCD.chargingUnit.chargeNotAvailable) {
  3051. pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "no");
  3052. } else {
  3053. pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "yes");
  3054. if (bc->AOCD.chargingUnit.freeOfCharge) {
  3055. pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "yes");
  3056. } else {
  3057. pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "no");
  3058. if (snprintf(buf, sizeof(buf), "%d", bc->AOCD.chargingUnit.recordedUnits) < sizeof(buf)) {
  3059. pbx_builtin_setvar_helper(ast, "AOCD_RecordedUnits", buf);
  3060. if (bc->AOCD.chargingUnit.billingId >= 0 && snprintf(buf, sizeof(buf), "%d", bc->AOCD.chargingUnit.billingId) < sizeof(buf)) {
  3061. pbx_builtin_setvar_helper(ast, "AOCD_BillingId", buf);
  3062. }
  3063. }
  3064. }
  3065. }
  3066. break;
  3067. default:
  3068. break;
  3069. }
  3070. bc->AOCD_need_export = 0;
  3071. }
  3072. /*************** Helpers END *************/
  3073. static void sighandler(int sig)
  3074. {
  3075. }
  3076. static void *misdn_tasks_thread_func(void *data)
  3077. {
  3078. int wait;
  3079. struct sigaction sa;
  3080. sa.sa_handler = sighandler;
  3081. sa.sa_flags = SA_NODEFER;
  3082. sigemptyset(&sa.sa_mask);
  3083. sigaddset(&sa.sa_mask, SIGUSR1);
  3084. sigaction(SIGUSR1, &sa, NULL);
  3085. sem_post((sem_t *)data);
  3086. while (1) {
  3087. wait = ast_sched_wait(misdn_tasks);
  3088. if (wait < 0) {
  3089. wait = 8000;
  3090. }
  3091. if (poll(NULL, 0, wait) < 0) {
  3092. chan_misdn_log(4, 0, "Waking up misdn_tasks thread\n");
  3093. }
  3094. ast_sched_runq(misdn_tasks);
  3095. }
  3096. return NULL;
  3097. }
  3098. static void misdn_tasks_init(void)
  3099. {
  3100. sem_t blocker;
  3101. int i = 5;
  3102. if (sem_init(&blocker, 0, 0)) {
  3103. perror("chan_misdn: Failed to initialize semaphore!");
  3104. exit(1);
  3105. }
  3106. chan_misdn_log(4, 0, "Starting misdn_tasks thread\n");
  3107. misdn_tasks = sched_context_create();
  3108. pthread_create(&misdn_tasks_thread, NULL, misdn_tasks_thread_func, &blocker);
  3109. while (sem_wait(&blocker) && --i) {
  3110. }
  3111. sem_destroy(&blocker);
  3112. }
  3113. static void misdn_tasks_destroy(void)
  3114. {
  3115. if (misdn_tasks) {
  3116. chan_misdn_log(4, 0, "Killing misdn_tasks thread\n");
  3117. if (pthread_cancel(misdn_tasks_thread) == 0) {
  3118. cb_log(4, 0, "Joining misdn_tasks thread\n");
  3119. pthread_join(misdn_tasks_thread, NULL);
  3120. }
  3121. sched_context_destroy(misdn_tasks);
  3122. }
  3123. }
  3124. static inline void misdn_tasks_wakeup(void)
  3125. {
  3126. pthread_kill(misdn_tasks_thread, SIGUSR1);
  3127. }
  3128. static inline int _misdn_tasks_add_variable(int timeout, ast_sched_cb callback, const void *data, int variable)
  3129. {
  3130. int task_id;
  3131. if (!misdn_tasks) {
  3132. misdn_tasks_init();
  3133. }
  3134. task_id = ast_sched_add_variable(misdn_tasks, timeout, callback, data, variable);
  3135. misdn_tasks_wakeup();
  3136. return task_id;
  3137. }
  3138. static int misdn_tasks_add(int timeout, ast_sched_cb callback, const void *data)
  3139. {
  3140. return _misdn_tasks_add_variable(timeout, callback, data, 0);
  3141. }
  3142. static int misdn_tasks_add_variable(int timeout, ast_sched_cb callback, const void *data)
  3143. {
  3144. return _misdn_tasks_add_variable(timeout, callback, data, 1);
  3145. }
  3146. static void misdn_tasks_remove(int task_id)
  3147. {
  3148. AST_SCHED_DEL(misdn_tasks, task_id);
  3149. }
  3150. static int misdn_l1_task(const void *vdata)
  3151. {
  3152. const int *data = vdata;
  3153. misdn_lib_isdn_l1watcher(*data);
  3154. chan_misdn_log(5, *data, "L1watcher timeout\n");
  3155. return 1;
  3156. }
  3157. static int misdn_overlap_dial_task(const void *data)
  3158. {
  3159. struct timeval tv_end, tv_now;
  3160. int diff;
  3161. struct chan_list *ch = (struct chan_list *) data;
  3162. char *dad;
  3163. chan_misdn_log(4, ch->bc->port, "overlap dial task, chan_state: %d\n", ch->state);
  3164. if (ch->state != MISDN_WAITING4DIGS) {
  3165. ch->overlap_dial_task = -1;
  3166. return 0;
  3167. }
  3168. ast_mutex_lock(&ch->overlap_tv_lock);
  3169. tv_end = ch->overlap_tv;
  3170. ast_mutex_unlock(&ch->overlap_tv_lock);
  3171. tv_end.tv_sec += ch->overlap_dial;
  3172. tv_now = ast_tvnow();
  3173. diff = ast_tvdiff_ms(tv_end, tv_now);
  3174. if (100 < diff) {
  3175. return diff;
  3176. }
  3177. /* if we are 100ms near the timeout, we are satisfied.. */
  3178. stop_indicate(ch);
  3179. if (ast_strlen_zero(ch->bc->dialed.number)) {
  3180. dad = "s";
  3181. strcpy(ch->ast->exten, dad);
  3182. } else {
  3183. dad = ch->bc->dialed.number;
  3184. }
  3185. if (ast_exists_extension(ch->ast, ch->context, dad, 1, ch->bc->caller.number)) {
  3186. ch->state = MISDN_DIALING;
  3187. if (pbx_start_chan(ch) < 0) {
  3188. chan_misdn_log(-1, ch->bc->port, "ast_pbx_start returned < 0 in misdn_overlap_dial_task\n");
  3189. goto misdn_overlap_dial_task_disconnect;
  3190. }
  3191. } else {
  3192. misdn_overlap_dial_task_disconnect:
  3193. hanguptone_indicate(ch);
  3194. ch->bc->out_cause = AST_CAUSE_UNALLOCATED;
  3195. ch->state = MISDN_CLEANING;
  3196. misdn_lib_send_event(ch->bc, EVENT_DISCONNECT);
  3197. }
  3198. ch->overlap_dial_task = -1;
  3199. return 0;
  3200. }
  3201. static void send_digit_to_chan(struct chan_list *cl, char digit)
  3202. {
  3203. static const char * const dtmf_tones[] = {
  3204. /* *INDENT-OFF* */
  3205. "!941+1336/100,!0/100", /* 0 */
  3206. "!697+1209/100,!0/100", /* 1 */
  3207. "!697+1336/100,!0/100", /* 2 */
  3208. "!697+1477/100,!0/100", /* 3 */
  3209. "!770+1209/100,!0/100", /* 4 */
  3210. "!770+1336/100,!0/100", /* 5 */
  3211. "!770+1477/100,!0/100", /* 6 */
  3212. "!852+1209/100,!0/100", /* 7 */
  3213. "!852+1336/100,!0/100", /* 8 */
  3214. "!852+1477/100,!0/100", /* 9 */
  3215. "!697+1633/100,!0/100", /* A */
  3216. "!770+1633/100,!0/100", /* B */
  3217. "!852+1633/100,!0/100", /* C */
  3218. "!941+1633/100,!0/100", /* D */
  3219. "!941+1209/100,!0/100", /* * */
  3220. "!941+1477/100,!0/100", /* # */
  3221. /* *INDENT-ON* */
  3222. };
  3223. struct ast_channel *chan = cl->ast;
  3224. if (digit >= '0' && digit <='9') {
  3225. ast_playtones_start(chan, 0, dtmf_tones[digit - '0'], 0);
  3226. } else if (digit >= 'A' && digit <= 'D') {
  3227. ast_playtones_start(chan, 0, dtmf_tones[digit - 'A' + 10], 0);
  3228. } else if (digit == '*') {
  3229. ast_playtones_start(chan, 0, dtmf_tones[14], 0);
  3230. } else if (digit == '#') {
  3231. ast_playtones_start(chan, 0, dtmf_tones[15], 0);
  3232. } else {
  3233. /* not handled */
  3234. ast_debug(1, "Unable to handle DTMF tone '%c' for '%s'\n", digit, chan->name);
  3235. }
  3236. }
  3237. /*** CLI HANDLING ***/
  3238. static char *handle_cli_misdn_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3239. {
  3240. int level;
  3241. switch (cmd) {
  3242. case CLI_INIT:
  3243. e->command = "misdn set debug [on|off]";
  3244. e->usage =
  3245. "Usage: misdn set debug {on|off|<level>} [only] | [port <port> [only]]\n"
  3246. " Set the debug level of the mISDN channel.\n";
  3247. return NULL;
  3248. case CLI_GENERATE:
  3249. return complete_debug_port(a);
  3250. }
  3251. if (a->argc < 4 || a->argc > 7) {
  3252. return CLI_SHOWUSAGE;
  3253. }
  3254. if (!strcasecmp(a->argv[3], "on")) {
  3255. level = 1;
  3256. } else if (!strcasecmp(a->argv[3], "off")) {
  3257. level = 0;
  3258. } else if (isdigit(a->argv[3][0])) {
  3259. level = atoi(a->argv[3]);
  3260. } else {
  3261. return CLI_SHOWUSAGE;
  3262. }
  3263. switch (a->argc) {
  3264. case 4:
  3265. case 5:
  3266. {
  3267. int i;
  3268. int only = 0;
  3269. if (a->argc == 5) {
  3270. if (strncasecmp(a->argv[4], "only", strlen(a->argv[4]))) {
  3271. return CLI_SHOWUSAGE;
  3272. } else {
  3273. only = 1;
  3274. }
  3275. }
  3276. for (i = 0; i <= max_ports; i++) {
  3277. misdn_debug[i] = level;
  3278. misdn_debug_only[i] = only;
  3279. }
  3280. ast_cli(a->fd, "changing debug level for all ports to %d%s\n", misdn_debug[0], only ? " (only)" : "");
  3281. }
  3282. break;
  3283. case 6:
  3284. case 7:
  3285. {
  3286. int port;
  3287. if (strncasecmp(a->argv[4], "port", strlen(a->argv[4])))
  3288. return CLI_SHOWUSAGE;
  3289. port = atoi(a->argv[5]);
  3290. if (port <= 0 || port > max_ports) {
  3291. switch (max_ports) {
  3292. case 0:
  3293. ast_cli(a->fd, "port number not valid! no ports available so you won't get lucky with any number here...\n");
  3294. break;
  3295. case 1:
  3296. ast_cli(a->fd, "port number not valid! only port 1 is available.\n");
  3297. break;
  3298. default:
  3299. ast_cli(a->fd, "port number not valid! only ports 1 to %d are available.\n", max_ports);
  3300. }
  3301. return 0;
  3302. }
  3303. if (a->argc == 7) {
  3304. if (strncasecmp(a->argv[6], "only", strlen(a->argv[6]))) {
  3305. return CLI_SHOWUSAGE;
  3306. } else {
  3307. misdn_debug_only[port] = 1;
  3308. }
  3309. } else {
  3310. misdn_debug_only[port] = 0;
  3311. }
  3312. misdn_debug[port] = level;
  3313. ast_cli(a->fd, "changing debug level to %d%s for port %d\n", misdn_debug[port], misdn_debug_only[port] ? " (only)" : "", port);
  3314. }
  3315. }
  3316. return CLI_SUCCESS;
  3317. }
  3318. static char *handle_cli_misdn_set_crypt_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3319. {
  3320. switch (cmd) {
  3321. case CLI_INIT:
  3322. e->command = "misdn set crypt debug";
  3323. e->usage =
  3324. "Usage: misdn set crypt debug <level>\n"
  3325. " Set the crypt debug level of the mISDN channel. Level\n"
  3326. " must be 1 or 2.\n";
  3327. return NULL;
  3328. case CLI_GENERATE:
  3329. return NULL;
  3330. }
  3331. if (a->argc != 5) {
  3332. return CLI_SHOWUSAGE;
  3333. }
  3334. /* XXX Is this supposed to not do anything? XXX */
  3335. return CLI_SUCCESS;
  3336. }
  3337. static char *handle_cli_misdn_port_block(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3338. {
  3339. switch (cmd) {
  3340. case CLI_INIT:
  3341. e->command = "misdn port block";
  3342. e->usage =
  3343. "Usage: misdn port block <port>\n"
  3344. " Block the specified port by <port>.\n";
  3345. return NULL;
  3346. case CLI_GENERATE:
  3347. return NULL;
  3348. }
  3349. if (a->argc != 4) {
  3350. return CLI_SHOWUSAGE;
  3351. }
  3352. misdn_lib_port_block(atoi(a->argv[3]));
  3353. return CLI_SUCCESS;
  3354. }
  3355. static char *handle_cli_misdn_port_unblock(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3356. {
  3357. switch (cmd) {
  3358. case CLI_INIT:
  3359. e->command = "misdn port unblock";
  3360. e->usage =
  3361. "Usage: misdn port unblock <port>\n"
  3362. " Unblock the port specified by <port>.\n";
  3363. return NULL;
  3364. case CLI_GENERATE:
  3365. return NULL;
  3366. }
  3367. if (a->argc != 4) {
  3368. return CLI_SHOWUSAGE;
  3369. }
  3370. misdn_lib_port_unblock(atoi(a->argv[3]));
  3371. return CLI_SUCCESS;
  3372. }
  3373. static char *handle_cli_misdn_restart_port(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3374. {
  3375. switch (cmd) {
  3376. case CLI_INIT:
  3377. e->command = "misdn restart port";
  3378. e->usage =
  3379. "Usage: misdn restart port <port>\n"
  3380. " Restart the given port.\n";
  3381. return NULL;
  3382. case CLI_GENERATE:
  3383. return NULL;
  3384. }
  3385. if (a->argc != 4) {
  3386. return CLI_SHOWUSAGE;
  3387. }
  3388. misdn_lib_port_restart(atoi(a->argv[3]));
  3389. return CLI_SUCCESS;
  3390. }
  3391. static char *handle_cli_misdn_restart_pid(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3392. {
  3393. switch (cmd) {
  3394. case CLI_INIT:
  3395. e->command = "misdn restart pid";
  3396. e->usage =
  3397. "Usage: misdn restart pid <pid>\n"
  3398. " Restart the given pid\n";
  3399. return NULL;
  3400. case CLI_GENERATE:
  3401. return NULL;
  3402. }
  3403. if (a->argc != 4) {
  3404. return CLI_SHOWUSAGE;
  3405. }
  3406. misdn_lib_pid_restart(atoi(a->argv[3]));
  3407. return CLI_SUCCESS;
  3408. }
  3409. static char *handle_cli_misdn_port_up(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3410. {
  3411. switch (cmd) {
  3412. case CLI_INIT:
  3413. e->command = "misdn port up";
  3414. e->usage =
  3415. "Usage: misdn port up <port>\n"
  3416. " Try to establish L1 on the given port.\n";
  3417. return NULL;
  3418. case CLI_GENERATE:
  3419. return NULL;
  3420. }
  3421. if (a->argc != 4) {
  3422. return CLI_SHOWUSAGE;
  3423. }
  3424. misdn_lib_get_port_up(atoi(a->argv[3]));
  3425. return CLI_SUCCESS;
  3426. }
  3427. static char *handle_cli_misdn_port_down(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3428. {
  3429. switch (cmd) {
  3430. case CLI_INIT:
  3431. e->command = "misdn port down";
  3432. e->usage =
  3433. "Usage: misdn port down <port>\n"
  3434. " Try to deactivate the L1 on the given port.\n";
  3435. return NULL;
  3436. case CLI_GENERATE:
  3437. return NULL;
  3438. }
  3439. if (a->argc != 4) {
  3440. return CLI_SHOWUSAGE;
  3441. }
  3442. misdn_lib_get_port_down(atoi(a->argv[3]));
  3443. return CLI_SUCCESS;
  3444. }
  3445. static inline void show_config_description(int fd, enum misdn_cfg_elements elem)
  3446. {
  3447. char section[BUFFERSIZE];
  3448. char name[BUFFERSIZE];
  3449. char desc[BUFFERSIZE];
  3450. char def[BUFFERSIZE];
  3451. char tmp[BUFFERSIZE];
  3452. misdn_cfg_get_name(elem, tmp, sizeof(tmp));
  3453. term_color(name, tmp, COLOR_BRWHITE, 0, sizeof(tmp));
  3454. misdn_cfg_get_desc(elem, desc, sizeof(desc), def, sizeof(def));
  3455. if (elem < MISDN_CFG_LAST) {
  3456. term_color(section, "PORTS SECTION", COLOR_YELLOW, 0, sizeof(section));
  3457. } else {
  3458. term_color(section, "GENERAL SECTION", COLOR_YELLOW, 0, sizeof(section));
  3459. }
  3460. if (*def) {
  3461. ast_cli(fd, "[%s] %s (Default: %s)\n\t%s\n", section, name, def, desc);
  3462. } else {
  3463. ast_cli(fd, "[%s] %s\n\t%s\n", section, name, desc);
  3464. }
  3465. }
  3466. static char *handle_cli_misdn_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3467. {
  3468. char buffer[BUFFERSIZE];
  3469. enum misdn_cfg_elements elem;
  3470. int linebreak;
  3471. int onlyport = -1;
  3472. int ok = 0;
  3473. switch (cmd) {
  3474. case CLI_INIT:
  3475. e->command = "misdn show config";
  3476. e->usage =
  3477. "Usage: misdn show config [<port> | description <config element> | descriptions [general|ports]]\n"
  3478. " Use 0 for <port> to only print the general config.\n";
  3479. return NULL;
  3480. case CLI_GENERATE:
  3481. return complete_show_config(a);
  3482. }
  3483. if (a->argc >= 4) {
  3484. if (!strcmp(a->argv[3], "description")) {
  3485. if (a->argc == 5) {
  3486. enum misdn_cfg_elements elem = misdn_cfg_get_elem(a->argv[4]);
  3487. if (elem == MISDN_CFG_FIRST) {
  3488. ast_cli(a->fd, "Unknown element: %s\n", a->argv[4]);
  3489. } else {
  3490. show_config_description(a->fd, elem);
  3491. }
  3492. return CLI_SUCCESS;
  3493. }
  3494. return CLI_SHOWUSAGE;
  3495. } else if (!strcmp(a->argv[3], "descriptions")) {
  3496. if ((a->argc == 4) || ((a->argc == 5) && !strcmp(a->argv[4], "general"))) {
  3497. for (elem = MISDN_GEN_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
  3498. show_config_description(a->fd, elem);
  3499. ast_cli(a->fd, "\n");
  3500. }
  3501. ok = 1;
  3502. }
  3503. if ((a->argc == 4) || ((a->argc == 5) && !strcmp(a->argv[4], "ports"))) {
  3504. for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_CFG_LAST - 1 /* the ptp hack, remove the -1 when ptp is gone */; ++elem) {
  3505. show_config_description(a->fd, elem);
  3506. ast_cli(a->fd, "\n");
  3507. }
  3508. ok = 1;
  3509. }
  3510. return ok ? CLI_SUCCESS : CLI_SHOWUSAGE;
  3511. } else if (!sscanf(a->argv[3], "%5d", &onlyport) || onlyport < 0) {
  3512. ast_cli(a->fd, "Unknown option: %s\n", a->argv[3]);
  3513. return CLI_SHOWUSAGE;
  3514. }
  3515. }
  3516. if (a->argc == 3 || onlyport == 0) {
  3517. ast_cli(a->fd, "mISDN General-Config:\n");
  3518. for (elem = MISDN_GEN_FIRST + 1, linebreak = 1; elem < MISDN_GEN_LAST; elem++, linebreak++) {
  3519. misdn_cfg_get_config_string(0, elem, buffer, sizeof(buffer));
  3520. ast_cli(a->fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  3521. }
  3522. ast_cli(a->fd, "\n");
  3523. }
  3524. if (onlyport < 0) {
  3525. int port = misdn_cfg_get_next_port(0);
  3526. for (; port > 0; port = misdn_cfg_get_next_port(port)) {
  3527. ast_cli(a->fd, "\n[PORT %d]\n", port);
  3528. for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
  3529. misdn_cfg_get_config_string(port, elem, buffer, sizeof(buffer));
  3530. ast_cli(a->fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  3531. }
  3532. ast_cli(a->fd, "\n");
  3533. }
  3534. }
  3535. if (onlyport > 0) {
  3536. if (misdn_cfg_is_port_valid(onlyport)) {
  3537. ast_cli(a->fd, "[PORT %d]\n", onlyport);
  3538. for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
  3539. misdn_cfg_get_config_string(onlyport, elem, buffer, sizeof(buffer));
  3540. ast_cli(a->fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  3541. }
  3542. ast_cli(a->fd, "\n");
  3543. } else {
  3544. ast_cli(a->fd, "Port %d is not active!\n", onlyport);
  3545. }
  3546. }
  3547. return CLI_SUCCESS;
  3548. }
  3549. struct state_struct {
  3550. enum misdn_chan_state state;
  3551. char txt[255];
  3552. };
  3553. static const struct state_struct state_array[] = {
  3554. /* *INDENT-OFF* */
  3555. { MISDN_NOTHING, "NOTHING" }, /* at beginning */
  3556. { MISDN_WAITING4DIGS, "WAITING4DIGS" }, /* when waiting for infos */
  3557. { MISDN_EXTCANTMATCH, "EXTCANTMATCH" }, /* when asterisk couldn't match our ext */
  3558. { MISDN_INCOMING_SETUP, "INCOMING SETUP" }, /* when pbx_start */
  3559. { MISDN_DIALING, "DIALING" }, /* when pbx_start */
  3560. { MISDN_PROGRESS, "PROGRESS" }, /* when pbx_start */
  3561. { MISDN_PROCEEDING, "PROCEEDING" }, /* when pbx_start */
  3562. { MISDN_CALLING, "CALLING" }, /* when misdn_call is called */
  3563. { MISDN_CALLING_ACKNOWLEDGE, "CALLING_ACKNOWLEDGE" }, /* when misdn_call is called */
  3564. { MISDN_ALERTING, "ALERTING" }, /* when Alerting */
  3565. { MISDN_BUSY, "BUSY" }, /* when BUSY */
  3566. { MISDN_CONNECTED, "CONNECTED" }, /* when connected */
  3567. { MISDN_DISCONNECTED, "DISCONNECTED" }, /* when connected */
  3568. { MISDN_CLEANING, "CLEANING" }, /* when hangup from * but we were connected before */
  3569. /* *INDENT-ON* */
  3570. };
  3571. static const char *misdn_get_ch_state(struct chan_list *p)
  3572. {
  3573. int i;
  3574. static char state[8];
  3575. if (!p) {
  3576. return NULL;
  3577. }
  3578. for (i = 0; i < ARRAY_LEN(state_array); i++) {
  3579. if (state_array[i].state == p->state) {
  3580. return state_array[i].txt;
  3581. }
  3582. }
  3583. snprintf(state, sizeof(state), "%d", p->state) ;
  3584. return state;
  3585. }
  3586. static void reload_config(void)
  3587. {
  3588. int i, cfg_debug;
  3589. if (!g_config_initialized) {
  3590. ast_log(LOG_WARNING, "chan_misdn is not initialized properly, still reloading ?\n");
  3591. return ;
  3592. }
  3593. free_robin_list();
  3594. misdn_cfg_reload();
  3595. misdn_cfg_update_ptp();
  3596. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, sizeof(global_tracefile));
  3597. misdn_cfg_get(0, MISDN_GEN_DEBUG, &cfg_debug, sizeof(cfg_debug));
  3598. for (i = 0; i <= max_ports; i++) {
  3599. misdn_debug[i] = cfg_debug;
  3600. misdn_debug_only[i] = 0;
  3601. }
  3602. }
  3603. static char *handle_cli_misdn_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3604. {
  3605. switch (cmd) {
  3606. case CLI_INIT:
  3607. e->command = "misdn reload";
  3608. e->usage =
  3609. "Usage: misdn reload\n"
  3610. " Reload internal mISDN config, read from the config\n"
  3611. " file.\n";
  3612. return NULL;
  3613. case CLI_GENERATE:
  3614. return NULL;
  3615. }
  3616. if (a->argc != 2) {
  3617. return CLI_SHOWUSAGE;
  3618. }
  3619. ast_cli(a->fd, "Reloading mISDN configuration\n");
  3620. reload_config();
  3621. return CLI_SUCCESS;
  3622. }
  3623. static void print_bc_info(int fd, struct chan_list *help, struct misdn_bchannel *bc)
  3624. {
  3625. struct ast_channel *ast = help->ast;
  3626. ast_cli(fd,
  3627. "* Pid:%d Port:%d Ch:%d Mode:%s Orig:%s dialed:%s\n"
  3628. " --> caller:\"%s\" <%s>\n"
  3629. " --> redirecting-from:\"%s\" <%s>\n"
  3630. " --> redirecting-to:\"%s\" <%s>\n"
  3631. " --> context:%s state:%s\n",
  3632. bc->pid,
  3633. bc->port,
  3634. bc->channel,
  3635. bc->nt ? "NT" : "TE",
  3636. help->originator == ORG_AST ? "*" : "I",
  3637. ast ? ast->exten : "",
  3638. (ast && ast->caller.id.name.valid && ast->caller.id.name.str)
  3639. ? ast->caller.id.name.str : "",
  3640. (ast && ast->caller.id.number.valid && ast->caller.id.number.str)
  3641. ? ast->caller.id.number.str : "",
  3642. bc->redirecting.from.name,
  3643. bc->redirecting.from.number,
  3644. bc->redirecting.to.name,
  3645. bc->redirecting.to.number,
  3646. ast ? ast->context : "",
  3647. misdn_get_ch_state(help));
  3648. if (misdn_debug[bc->port] > 0) {
  3649. ast_cli(fd,
  3650. " --> astname: %s\n"
  3651. " --> ch_l3id: %x\n"
  3652. " --> ch_addr: %x\n"
  3653. " --> bc_addr: %x\n"
  3654. " --> bc_l3id: %x\n"
  3655. " --> display: %s\n"
  3656. " --> activated: %d\n"
  3657. " --> state: %s\n"
  3658. " --> capability: %s\n"
  3659. #ifdef MISDN_1_2
  3660. " --> pipeline: %s\n"
  3661. #else
  3662. " --> echo_cancel: %d\n"
  3663. #endif
  3664. " --> notone : rx %d tx:%d\n"
  3665. " --> bc_hold: %d\n",
  3666. ast ? ast->name : "",
  3667. help->l3id,
  3668. help->addr,
  3669. bc->addr,
  3670. bc->l3_id,
  3671. bc->display,
  3672. bc->active,
  3673. bc_state2str(bc->bc_state),
  3674. bearer2str(bc->capability),
  3675. #ifdef MISDN_1_2
  3676. bc->pipeline,
  3677. #else
  3678. bc->ec_enable,
  3679. #endif
  3680. help->norxtone, help->notxtone,
  3681. bc->holded);
  3682. }
  3683. }
  3684. static char *handle_cli_misdn_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3685. {
  3686. struct chan_list *help;
  3687. switch (cmd) {
  3688. case CLI_INIT:
  3689. e->command = "misdn show channels";
  3690. e->usage =
  3691. "Usage: misdn show channels\n"
  3692. " Show the internal mISDN channel list\n";
  3693. return NULL;
  3694. case CLI_GENERATE:
  3695. return NULL;
  3696. }
  3697. if (a->argc != 3) {
  3698. return CLI_SHOWUSAGE;
  3699. }
  3700. ast_cli(a->fd, "Channel List: %p\n", cl_te);
  3701. /*
  3702. * Walking the list and dumping the channel information could
  3703. * take awhile. With the list locked for the duration, the
  3704. * channel driver cannot process signaling messages. However,
  3705. * since this is a CLI command it should not be run very often.
  3706. */
  3707. ast_mutex_lock(&cl_te_lock);
  3708. for (help = cl_te; help; help = help->next) {
  3709. struct misdn_bchannel *bc = help->bc;
  3710. struct ast_channel *ast = help->ast;
  3711. if (!ast) {
  3712. if (!bc) {
  3713. ast_cli(a->fd, "chan_list obj. with l3id:%x has no bc and no ast Leg\n", help->l3id);
  3714. continue;
  3715. }
  3716. ast_cli(a->fd, "bc with pid:%d has no Ast Leg\n", bc->pid);
  3717. }
  3718. if (misdn_debug[0] > 2) {
  3719. ast_cli(a->fd, "Bc:%p Ast:%p\n", bc, ast);
  3720. }
  3721. if (bc) {
  3722. print_bc_info(a->fd, help, bc);
  3723. } else {
  3724. if (help->hold.state != MISDN_HOLD_IDLE) {
  3725. ast_cli(a->fd, "ITS A HELD CALL BC:\n");
  3726. ast_cli(a->fd, " --> l3_id: %x\n"
  3727. " --> dialed:%s\n"
  3728. " --> caller:\"%s\" <%s>\n"
  3729. " --> hold_port: %d\n"
  3730. " --> hold_channel: %d\n",
  3731. help->l3id,
  3732. ast->exten,
  3733. S_COR(ast->caller.id.name.valid, ast->caller.id.name.str, ""),
  3734. S_COR(ast->caller.id.number.valid, ast->caller.id.number.str, ""),
  3735. help->hold.port,
  3736. help->hold.channel
  3737. );
  3738. } else {
  3739. ast_cli(a->fd, "* Channel in unknown STATE !!! Exten:%s, Callerid:%s\n",
  3740. ast->exten,
  3741. S_COR(ast->caller.id.number.valid, ast->caller.id.number.str, ""));
  3742. }
  3743. }
  3744. }
  3745. ast_mutex_unlock(&cl_te_lock);
  3746. misdn_dump_chanlist();
  3747. return CLI_SUCCESS;
  3748. }
  3749. static char *handle_cli_misdn_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3750. {
  3751. struct chan_list *help;
  3752. switch (cmd) {
  3753. case CLI_INIT:
  3754. e->command = "misdn show channel";
  3755. e->usage =
  3756. "Usage: misdn show channel <channel>\n"
  3757. " Show an internal mISDN channel\n.";
  3758. return NULL;
  3759. case CLI_GENERATE:
  3760. return complete_ch(a);
  3761. }
  3762. if (a->argc != 4) {
  3763. return CLI_SHOWUSAGE;
  3764. }
  3765. ast_mutex_lock(&cl_te_lock);
  3766. for (help = cl_te; help; help = help->next) {
  3767. struct misdn_bchannel *bc = help->bc;
  3768. struct ast_channel *ast = help->ast;
  3769. if (bc && ast) {
  3770. if (!strcasecmp(ast->name, a->argv[3])) {
  3771. print_bc_info(a->fd, help, bc);
  3772. break;
  3773. }
  3774. }
  3775. }
  3776. ast_mutex_unlock(&cl_te_lock);
  3777. return CLI_SUCCESS;
  3778. }
  3779. static char *handle_cli_misdn_set_tics(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3780. {
  3781. switch (cmd) {
  3782. case CLI_INIT:
  3783. e->command = "misdn set tics";
  3784. e->usage =
  3785. "Usage: misdn set tics <value>\n";
  3786. return NULL;
  3787. case CLI_GENERATE:
  3788. return NULL;
  3789. }
  3790. if (a->argc != 4) {
  3791. return CLI_SHOWUSAGE;
  3792. }
  3793. /* XXX Wow, this does... a whole lot of nothing... XXX */
  3794. MAXTICS = atoi(a->argv[3]);
  3795. return CLI_SUCCESS;
  3796. }
  3797. static char *handle_cli_misdn_show_stacks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3798. {
  3799. int port;
  3800. switch (cmd) {
  3801. case CLI_INIT:
  3802. e->command = "misdn show stacks";
  3803. e->usage =
  3804. "Usage: misdn show stacks\n"
  3805. " Show internal mISDN stack_list.\n";
  3806. return NULL;
  3807. case CLI_GENERATE:
  3808. return NULL;
  3809. }
  3810. if (a->argc != 3) {
  3811. return CLI_SHOWUSAGE;
  3812. }
  3813. ast_cli(a->fd, "BEGIN STACK_LIST:\n");
  3814. for (port = misdn_cfg_get_next_port(0); port > 0;
  3815. port = misdn_cfg_get_next_port(port)) {
  3816. char buf[128];
  3817. get_show_stack_details(port, buf);
  3818. ast_cli(a->fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : "");
  3819. }
  3820. return CLI_SUCCESS;
  3821. }
  3822. static char *handle_cli_misdn_show_ports_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3823. {
  3824. int port;
  3825. switch (cmd) {
  3826. case CLI_INIT:
  3827. e->command = "misdn show ports stats";
  3828. e->usage =
  3829. "Usage: misdn show ports stats\n"
  3830. " Show mISDNs channel's call statistics per port.\n";
  3831. return NULL;
  3832. case CLI_GENERATE:
  3833. return NULL;
  3834. }
  3835. if (a->argc != 4) {
  3836. return CLI_SHOWUSAGE;
  3837. }
  3838. ast_cli(a->fd, "Port\tin_calls\tout_calls\n");
  3839. for (port = misdn_cfg_get_next_port(0); port > 0;
  3840. port = misdn_cfg_get_next_port(port)) {
  3841. ast_cli(a->fd, "%d\t%d\t\t%d\n", port, misdn_in_calls[port], misdn_out_calls[port]);
  3842. }
  3843. ast_cli(a->fd, "\n");
  3844. return CLI_SUCCESS;
  3845. }
  3846. static char *handle_cli_misdn_show_port(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3847. {
  3848. int port;
  3849. char buf[128];
  3850. switch (cmd) {
  3851. case CLI_INIT:
  3852. e->command = "misdn show port";
  3853. e->usage =
  3854. "Usage: misdn show port <port>\n"
  3855. " Show detailed information for given port.\n";
  3856. return NULL;
  3857. case CLI_GENERATE:
  3858. return NULL;
  3859. }
  3860. if (a->argc != 4) {
  3861. return CLI_SHOWUSAGE;
  3862. }
  3863. port = atoi(a->argv[3]);
  3864. ast_cli(a->fd, "BEGIN STACK_LIST:\n");
  3865. get_show_stack_details(port, buf);
  3866. ast_cli(a->fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : "");
  3867. return CLI_SUCCESS;
  3868. }
  3869. #if defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES)
  3870. static const struct FacParm Fac_Msgs[] = {
  3871. /* *INDENT-OFF* */
  3872. [0].Function = Fac_ERROR,
  3873. [0].u.ERROR.invokeId = 8,
  3874. [0].u.ERROR.errorValue = FacError_CCBS_AlreadyAccepted,
  3875. [1].Function = Fac_RESULT,
  3876. [1].u.RESULT.InvokeID = 9,
  3877. [2].Function = Fac_REJECT,
  3878. [2].u.REJECT.Code = FacReject_Gen_BadlyStructuredComponent,
  3879. [3].Function = Fac_REJECT,
  3880. [3].u.REJECT.InvokeIDPresent = 1,
  3881. [3].u.REJECT.InvokeID = 10,
  3882. [3].u.REJECT.Code = FacReject_Inv_InitiatorReleasing,
  3883. [4].Function = Fac_REJECT,
  3884. [4].u.REJECT.InvokeIDPresent = 1,
  3885. [4].u.REJECT.InvokeID = 11,
  3886. [4].u.REJECT.Code = FacReject_Res_MistypedResult,
  3887. [5].Function = Fac_REJECT,
  3888. [5].u.REJECT.InvokeIDPresent = 1,
  3889. [5].u.REJECT.InvokeID = 12,
  3890. [5].u.REJECT.Code = FacReject_Err_ErrorResponseUnexpected,
  3891. [6].Function = Fac_StatusRequest,
  3892. [6].u.StatusRequest.InvokeID = 13,
  3893. [6].u.StatusRequest.ComponentType = FacComponent_Invoke,
  3894. [6].u.StatusRequest.Component.Invoke.Q931ie.Bc.Length = 2,
  3895. [6].u.StatusRequest.Component.Invoke.Q931ie.Bc.Contents = "AB",
  3896. [6].u.StatusRequest.Component.Invoke.Q931ie.Llc.Length = 3,
  3897. [6].u.StatusRequest.Component.Invoke.Q931ie.Llc.Contents = "CDE",
  3898. [6].u.StatusRequest.Component.Invoke.Q931ie.Hlc.Length = 4,
  3899. [6].u.StatusRequest.Component.Invoke.Q931ie.Hlc.Contents = "FGHI",
  3900. [6].u.StatusRequest.Component.Invoke.CompatibilityMode = 1,
  3901. [7].Function = Fac_StatusRequest,
  3902. [7].u.StatusRequest.InvokeID = 14,
  3903. [7].u.StatusRequest.ComponentType = FacComponent_Result,
  3904. [7].u.StatusRequest.Component.Result.Status = 2,
  3905. [8].Function = Fac_CallInfoRetain,
  3906. [8].u.CallInfoRetain.InvokeID = 15,
  3907. [8].u.CallInfoRetain.CallLinkageID = 115,
  3908. [9].Function = Fac_EraseCallLinkageID,
  3909. [9].u.EraseCallLinkageID.InvokeID = 16,
  3910. [9].u.EraseCallLinkageID.CallLinkageID = 105,
  3911. [10].Function = Fac_CCBSDeactivate,
  3912. [10].u.CCBSDeactivate.InvokeID = 17,
  3913. [10].u.CCBSDeactivate.ComponentType = FacComponent_Invoke,
  3914. [10].u.CCBSDeactivate.Component.Invoke.CCBSReference = 2,
  3915. [11].Function = Fac_CCBSDeactivate,
  3916. [11].u.CCBSDeactivate.InvokeID = 18,
  3917. [11].u.CCBSDeactivate.ComponentType = FacComponent_Result,
  3918. [12].Function = Fac_CCBSErase,
  3919. [12].u.CCBSErase.InvokeID = 19,
  3920. [12].u.CCBSErase.Q931ie.Bc.Length = 2,
  3921. [12].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3922. [12].u.CCBSErase.AddressOfB.Party.Type = 0,
  3923. [12].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 5,
  3924. [12].u.CCBSErase.AddressOfB.Party.Number = "33403",
  3925. [12].u.CCBSErase.AddressOfB.Subaddress.Type = 0,
  3926. [12].u.CCBSErase.AddressOfB.Subaddress.Length = 4,
  3927. [12].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.Information = "3748",
  3928. [12].u.CCBSErase.RecallMode = 1,
  3929. [12].u.CCBSErase.CCBSReference = 102,
  3930. [12].u.CCBSErase.Reason = 3,
  3931. [13].Function = Fac_CCBSErase,
  3932. [13].u.CCBSErase.InvokeID = 20,
  3933. [13].u.CCBSErase.Q931ie.Bc.Length = 2,
  3934. [13].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3935. [13].u.CCBSErase.AddressOfB.Party.Type = 1,
  3936. [13].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 11,
  3937. [13].u.CCBSErase.AddressOfB.Party.TypeOfNumber = 1,
  3938. [13].u.CCBSErase.AddressOfB.Party.Number = "18003020102",
  3939. [13].u.CCBSErase.AddressOfB.Subaddress.Type = 0,
  3940. [13].u.CCBSErase.AddressOfB.Subaddress.Length = 4,
  3941. [13].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.OddCountPresent = 1,
  3942. [13].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.OddCount = 1,
  3943. [13].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.Information = "3748",
  3944. [13].u.CCBSErase.RecallMode = 1,
  3945. [13].u.CCBSErase.CCBSReference = 102,
  3946. [13].u.CCBSErase.Reason = 3,
  3947. [14].Function = Fac_CCBSErase,
  3948. [14].u.CCBSErase.InvokeID = 21,
  3949. [14].u.CCBSErase.Q931ie.Bc.Length = 2,
  3950. [14].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3951. [14].u.CCBSErase.AddressOfB.Party.Type = 2,
  3952. [14].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  3953. [14].u.CCBSErase.AddressOfB.Party.Number = "1803",
  3954. [14].u.CCBSErase.AddressOfB.Subaddress.Type = 1,
  3955. [14].u.CCBSErase.AddressOfB.Subaddress.Length = 4,
  3956. [14].u.CCBSErase.AddressOfB.Subaddress.u.Nsap = "6492",
  3957. [14].u.CCBSErase.RecallMode = 1,
  3958. [14].u.CCBSErase.CCBSReference = 102,
  3959. [14].u.CCBSErase.Reason = 3,
  3960. [15].Function = Fac_CCBSErase,
  3961. [15].u.CCBSErase.InvokeID = 22,
  3962. [15].u.CCBSErase.Q931ie.Bc.Length = 2,
  3963. [15].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3964. [15].u.CCBSErase.AddressOfB.Party.Type = 3,
  3965. [15].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  3966. [15].u.CCBSErase.AddressOfB.Party.Number = "1803",
  3967. [15].u.CCBSErase.RecallMode = 1,
  3968. [15].u.CCBSErase.CCBSReference = 102,
  3969. [15].u.CCBSErase.Reason = 3,
  3970. [16].Function = Fac_CCBSErase,
  3971. [16].u.CCBSErase.InvokeID = 23,
  3972. [16].u.CCBSErase.Q931ie.Bc.Length = 2,
  3973. [16].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3974. [16].u.CCBSErase.AddressOfB.Party.Type = 4,
  3975. [16].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  3976. [16].u.CCBSErase.AddressOfB.Party.Number = "1803",
  3977. [16].u.CCBSErase.RecallMode = 1,
  3978. [16].u.CCBSErase.CCBSReference = 102,
  3979. [16].u.CCBSErase.Reason = 3,
  3980. [17].Function = Fac_CCBSErase,
  3981. [17].u.CCBSErase.InvokeID = 24,
  3982. [17].u.CCBSErase.Q931ie.Bc.Length = 2,
  3983. [17].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3984. [17].u.CCBSErase.AddressOfB.Party.Type = 5,
  3985. [17].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 11,
  3986. [17].u.CCBSErase.AddressOfB.Party.TypeOfNumber = 4,
  3987. [17].u.CCBSErase.AddressOfB.Party.Number = "18003020102",
  3988. [17].u.CCBSErase.RecallMode = 1,
  3989. [17].u.CCBSErase.CCBSReference = 102,
  3990. [17].u.CCBSErase.Reason = 3,
  3991. [18].Function = Fac_CCBSErase,
  3992. [18].u.CCBSErase.InvokeID = 25,
  3993. [18].u.CCBSErase.Q931ie.Bc.Length = 2,
  3994. [18].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3995. [18].u.CCBSErase.AddressOfB.Party.Type = 8,
  3996. [18].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  3997. [18].u.CCBSErase.AddressOfB.Party.Number = "1803",
  3998. [18].u.CCBSErase.RecallMode = 1,
  3999. [18].u.CCBSErase.CCBSReference = 102,
  4000. [18].u.CCBSErase.Reason = 3,
  4001. [19].Function = Fac_CCBSRemoteUserFree,
  4002. [19].u.CCBSRemoteUserFree.InvokeID = 26,
  4003. [19].u.CCBSRemoteUserFree.Q931ie.Bc.Length = 2,
  4004. [19].u.CCBSRemoteUserFree.Q931ie.Bc.Contents = "JK",
  4005. [19].u.CCBSRemoteUserFree.AddressOfB.Party.Type = 8,
  4006. [19].u.CCBSRemoteUserFree.AddressOfB.Party.LengthOfNumber = 4,
  4007. [19].u.CCBSRemoteUserFree.AddressOfB.Party.Number = "1803",
  4008. [19].u.CCBSRemoteUserFree.RecallMode = 1,
  4009. [19].u.CCBSRemoteUserFree.CCBSReference = 102,
  4010. [20].Function = Fac_CCBSCall,
  4011. [20].u.CCBSCall.InvokeID = 27,
  4012. [20].u.CCBSCall.CCBSReference = 115,
  4013. [21].Function = Fac_CCBSStatusRequest,
  4014. [21].u.CCBSStatusRequest.InvokeID = 28,
  4015. [21].u.CCBSStatusRequest.ComponentType = FacComponent_Invoke,
  4016. [21].u.CCBSStatusRequest.Component.Invoke.Q931ie.Bc.Length = 2,
  4017. [21].u.CCBSStatusRequest.Component.Invoke.Q931ie.Bc.Contents = "JK",
  4018. [21].u.CCBSStatusRequest.Component.Invoke.RecallMode = 1,
  4019. [21].u.CCBSStatusRequest.Component.Invoke.CCBSReference = 102,
  4020. [22].Function = Fac_CCBSStatusRequest,
  4021. [22].u.CCBSStatusRequest.InvokeID = 29,
  4022. [22].u.CCBSStatusRequest.ComponentType = FacComponent_Result,
  4023. [22].u.CCBSStatusRequest.Component.Result.Free = 1,
  4024. [23].Function = Fac_CCBSBFree,
  4025. [23].u.CCBSBFree.InvokeID = 30,
  4026. [23].u.CCBSBFree.Q931ie.Bc.Length = 2,
  4027. [23].u.CCBSBFree.Q931ie.Bc.Contents = "JK",
  4028. [23].u.CCBSBFree.AddressOfB.Party.Type = 8,
  4029. [23].u.CCBSBFree.AddressOfB.Party.LengthOfNumber = 4,
  4030. [23].u.CCBSBFree.AddressOfB.Party.Number = "1803",
  4031. [23].u.CCBSBFree.RecallMode = 1,
  4032. [23].u.CCBSBFree.CCBSReference = 14,
  4033. [24].Function = Fac_CCBSStopAlerting,
  4034. [24].u.CCBSStopAlerting.InvokeID = 31,
  4035. [24].u.CCBSStopAlerting.CCBSReference = 37,
  4036. [25].Function = Fac_CCBSRequest,
  4037. [25].u.CCBSRequest.InvokeID = 32,
  4038. [25].u.CCBSRequest.ComponentType = FacComponent_Invoke,
  4039. [25].u.CCBSRequest.Component.Invoke.CallLinkageID = 57,
  4040. [26].Function = Fac_CCBSRequest,
  4041. [26].u.CCBSRequest.InvokeID = 33,
  4042. [26].u.CCBSRequest.ComponentType = FacComponent_Result,
  4043. [26].u.CCBSRequest.Component.Result.RecallMode = 1,
  4044. [26].u.CCBSRequest.Component.Result.CCBSReference = 102,
  4045. [27].Function = Fac_CCBSInterrogate,
  4046. [27].u.CCBSInterrogate.InvokeID = 34,
  4047. [27].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4048. [27].u.CCBSInterrogate.Component.Invoke.AParty.Type = 8,
  4049. [27].u.CCBSInterrogate.Component.Invoke.AParty.LengthOfNumber = 4,
  4050. [27].u.CCBSInterrogate.Component.Invoke.AParty.Number = "1803",
  4051. [27].u.CCBSInterrogate.Component.Invoke.CCBSReferencePresent = 1,
  4052. [27].u.CCBSInterrogate.Component.Invoke.CCBSReference = 76,
  4053. [28].Function = Fac_CCBSInterrogate,
  4054. [28].u.CCBSInterrogate.InvokeID = 35,
  4055. [28].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4056. [28].u.CCBSInterrogate.Component.Invoke.AParty.Type = 8,
  4057. [28].u.CCBSInterrogate.Component.Invoke.AParty.LengthOfNumber = 4,
  4058. [28].u.CCBSInterrogate.Component.Invoke.AParty.Number = "1803",
  4059. [29].Function = Fac_CCBSInterrogate,
  4060. [29].u.CCBSInterrogate.InvokeID = 36,
  4061. [29].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4062. [29].u.CCBSInterrogate.Component.Invoke.CCBSReferencePresent = 1,
  4063. [29].u.CCBSInterrogate.Component.Invoke.CCBSReference = 76,
  4064. [30].Function = Fac_CCBSInterrogate,
  4065. [30].u.CCBSInterrogate.InvokeID = 37,
  4066. [30].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4067. [31].Function = Fac_CCBSInterrogate,
  4068. [31].u.CCBSInterrogate.InvokeID = 38,
  4069. [31].u.CCBSInterrogate.ComponentType = FacComponent_Result,
  4070. [31].u.CCBSInterrogate.Component.Result.RecallMode = 1,
  4071. [32].Function = Fac_CCBSInterrogate,
  4072. [32].u.CCBSInterrogate.InvokeID = 39,
  4073. [32].u.CCBSInterrogate.ComponentType = FacComponent_Result,
  4074. [32].u.CCBSInterrogate.Component.Result.RecallMode = 1,
  4075. [32].u.CCBSInterrogate.Component.Result.NumRecords = 1,
  4076. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].CCBSReference = 12,
  4077. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Length = 2,
  4078. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Contents = "JK",
  4079. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Type = 8,
  4080. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.LengthOfNumber = 4,
  4081. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Number = "1803",
  4082. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].SubaddressOfA.Type = 1,
  4083. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].SubaddressOfA.Length = 4,
  4084. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].SubaddressOfA.u.Nsap = "6492",
  4085. [33].Function = Fac_CCBSInterrogate,
  4086. [33].u.CCBSInterrogate.InvokeID = 40,
  4087. [33].u.CCBSInterrogate.ComponentType = FacComponent_Result,
  4088. [33].u.CCBSInterrogate.Component.Result.RecallMode = 1,
  4089. [33].u.CCBSInterrogate.Component.Result.NumRecords = 2,
  4090. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].CCBSReference = 12,
  4091. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Length = 2,
  4092. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Contents = "JK",
  4093. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Type = 8,
  4094. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.LengthOfNumber = 4,
  4095. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Number = "1803",
  4096. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].CCBSReference = 102,
  4097. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].Q931ie.Bc.Length = 2,
  4098. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].Q931ie.Bc.Contents = "LM",
  4099. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Party.Type = 8,
  4100. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Party.LengthOfNumber = 4,
  4101. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Party.Number = "6229",
  4102. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Subaddress.Type = 1,
  4103. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Subaddress.Length = 4,
  4104. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Subaddress.u.Nsap = "8592",
  4105. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].SubaddressOfA.Type = 1,
  4106. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].SubaddressOfA.Length = 4,
  4107. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].SubaddressOfA.u.Nsap = "6492",
  4108. [34].Function = Fac_CCNRRequest,
  4109. [34].u.CCNRRequest.InvokeID = 512,
  4110. [34].u.CCNRRequest.ComponentType = FacComponent_Invoke,
  4111. [34].u.CCNRRequest.Component.Invoke.CallLinkageID = 57,
  4112. [35].Function = Fac_CCNRRequest,
  4113. [35].u.CCNRRequest.InvokeID = 150,
  4114. [35].u.CCNRRequest.ComponentType = FacComponent_Result,
  4115. [35].u.CCNRRequest.Component.Result.RecallMode = 1,
  4116. [35].u.CCNRRequest.Component.Result.CCBSReference = 102,
  4117. [36].Function = Fac_CCNRInterrogate,
  4118. [36].u.CCNRInterrogate.InvokeID = -129,
  4119. [36].u.CCNRInterrogate.ComponentType = FacComponent_Invoke,
  4120. [37].Function = Fac_CCNRInterrogate,
  4121. [37].u.CCNRInterrogate.InvokeID = -3,
  4122. [37].u.CCNRInterrogate.ComponentType = FacComponent_Result,
  4123. [37].u.CCNRInterrogate.Component.Result.RecallMode = 1,
  4124. [38].Function = Fac_CCBS_T_Call,
  4125. [38].u.EctExecute.InvokeID = 41,
  4126. [39].Function = Fac_CCBS_T_Suspend,
  4127. [39].u.EctExecute.InvokeID = 42,
  4128. [40].Function = Fac_CCBS_T_Resume,
  4129. [40].u.EctExecute.InvokeID = 43,
  4130. [41].Function = Fac_CCBS_T_RemoteUserFree,
  4131. [41].u.EctExecute.InvokeID = 44,
  4132. [42].Function = Fac_CCBS_T_Available,
  4133. [42].u.EctExecute.InvokeID = 45,
  4134. [43].Function = Fac_CCBS_T_Request,
  4135. [43].u.CCBS_T_Request.InvokeID = 46,
  4136. [43].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4137. [43].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4138. [43].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4139. [43].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4140. [43].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4141. [43].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4142. [43].u.CCBS_T_Request.Component.Invoke.RetentionSupported = 1,
  4143. [43].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1,
  4144. [43].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator = 1,
  4145. [43].u.CCBS_T_Request.Component.Invoke.Originating.Party.Type = 8,
  4146. [43].u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber = 4,
  4147. [43].u.CCBS_T_Request.Component.Invoke.Originating.Party.Number = "9864",
  4148. [44].Function = Fac_CCBS_T_Request,
  4149. [44].u.CCBS_T_Request.InvokeID = 47,
  4150. [44].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4151. [44].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4152. [44].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4153. [44].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4154. [44].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4155. [44].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4156. [44].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1,
  4157. [44].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator = 1,
  4158. [44].u.CCBS_T_Request.Component.Invoke.Originating.Party.Type = 8,
  4159. [44].u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber = 4,
  4160. [44].u.CCBS_T_Request.Component.Invoke.Originating.Party.Number = "9864",
  4161. [45].Function = Fac_CCBS_T_Request,
  4162. [45].u.CCBS_T_Request.InvokeID = 48,
  4163. [45].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4164. [45].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4165. [45].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4166. [45].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4167. [45].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4168. [45].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4169. [45].u.CCBS_T_Request.Component.Invoke.Originating.Party.Type = 8,
  4170. [45].u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber = 4,
  4171. [45].u.CCBS_T_Request.Component.Invoke.Originating.Party.Number = "9864",
  4172. [46].Function = Fac_CCBS_T_Request,
  4173. [46].u.CCBS_T_Request.InvokeID = 49,
  4174. [46].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4175. [46].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4176. [46].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4177. [46].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4178. [46].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4179. [46].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4180. [46].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1,
  4181. [46].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator = 1,
  4182. [47].Function = Fac_CCBS_T_Request,
  4183. [47].u.CCBS_T_Request.InvokeID = 50,
  4184. [47].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4185. [47].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4186. [47].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4187. [47].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4188. [47].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4189. [47].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4190. [48].Function = Fac_CCBS_T_Request,
  4191. [48].u.CCBS_T_Request.InvokeID = 51,
  4192. [48].u.CCBS_T_Request.ComponentType = FacComponent_Result,
  4193. [48].u.CCBS_T_Request.Component.Result.RetentionSupported = 1,
  4194. [49].Function = Fac_CCNR_T_Request,
  4195. [49].u.CCNR_T_Request.InvokeID = 52,
  4196. [49].u.CCNR_T_Request.ComponentType = FacComponent_Invoke,
  4197. [49].u.CCNR_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4198. [49].u.CCNR_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4199. [49].u.CCNR_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4200. [49].u.CCNR_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4201. [49].u.CCNR_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4202. [50].Function = Fac_CCNR_T_Request,
  4203. [50].u.CCNR_T_Request.InvokeID = 53,
  4204. [50].u.CCNR_T_Request.ComponentType = FacComponent_Result,
  4205. [50].u.CCNR_T_Request.Component.Result.RetentionSupported = 1,
  4206. [51].Function = Fac_EctExecute,
  4207. [51].u.EctExecute.InvokeID = 54,
  4208. [52].Function = Fac_ExplicitEctExecute,
  4209. [52].u.ExplicitEctExecute.InvokeID = 55,
  4210. [52].u.ExplicitEctExecute.LinkID = 23,
  4211. [53].Function = Fac_RequestSubaddress,
  4212. [53].u.RequestSubaddress.InvokeID = 56,
  4213. [54].Function = Fac_SubaddressTransfer,
  4214. [54].u.SubaddressTransfer.InvokeID = 57,
  4215. [54].u.SubaddressTransfer.Subaddress.Type = 1,
  4216. [54].u.SubaddressTransfer.Subaddress.Length = 4,
  4217. [54].u.SubaddressTransfer.Subaddress.u.Nsap = "6492",
  4218. [55].Function = Fac_EctLinkIdRequest,
  4219. [55].u.EctLinkIdRequest.InvokeID = 58,
  4220. [55].u.EctLinkIdRequest.ComponentType = FacComponent_Invoke,
  4221. [56].Function = Fac_EctLinkIdRequest,
  4222. [56].u.EctLinkIdRequest.InvokeID = 59,
  4223. [56].u.EctLinkIdRequest.ComponentType = FacComponent_Result,
  4224. [56].u.EctLinkIdRequest.Component.Result.LinkID = 76,
  4225. [57].Function = Fac_EctInform,
  4226. [57].u.EctInform.InvokeID = 60,
  4227. [57].u.EctInform.Status = 1,
  4228. [57].u.EctInform.RedirectionPresent = 1,
  4229. [57].u.EctInform.Redirection.Type = 0,
  4230. [57].u.EctInform.Redirection.Unscreened.Type = 8,
  4231. [57].u.EctInform.Redirection.Unscreened.LengthOfNumber = 4,
  4232. [57].u.EctInform.Redirection.Unscreened.Number = "6229",
  4233. [58].Function = Fac_EctInform,
  4234. [58].u.EctInform.InvokeID = 61,
  4235. [58].u.EctInform.Status = 1,
  4236. [58].u.EctInform.RedirectionPresent = 1,
  4237. [58].u.EctInform.Redirection.Type = 1,
  4238. [59].Function = Fac_EctInform,
  4239. [59].u.EctInform.InvokeID = 62,
  4240. [59].u.EctInform.Status = 1,
  4241. [59].u.EctInform.RedirectionPresent = 1,
  4242. [59].u.EctInform.Redirection.Type = 2,
  4243. [60].Function = Fac_EctInform,
  4244. [60].u.EctInform.InvokeID = 63,
  4245. [60].u.EctInform.Status = 1,
  4246. [60].u.EctInform.RedirectionPresent = 1,
  4247. [60].u.EctInform.Redirection.Type = 3,
  4248. [60].u.EctInform.Redirection.Unscreened.Type = 8,
  4249. [60].u.EctInform.Redirection.Unscreened.LengthOfNumber = 4,
  4250. [60].u.EctInform.Redirection.Unscreened.Number = "3340",
  4251. [61].Function = Fac_EctInform,
  4252. [61].u.EctInform.InvokeID = 64,
  4253. [61].u.EctInform.Status = 1,
  4254. [61].u.EctInform.RedirectionPresent = 0,
  4255. [62].Function = Fac_EctLoopTest,
  4256. [62].u.EctLoopTest.InvokeID = 65,
  4257. [62].u.EctLoopTest.ComponentType = FacComponent_Invoke,
  4258. [62].u.EctLoopTest.Component.Invoke.CallTransferID = 7,
  4259. [63].Function = Fac_EctLoopTest,
  4260. [63].u.EctLoopTest.InvokeID = 66,
  4261. [63].u.EctLoopTest.ComponentType = FacComponent_Result,
  4262. [63].u.EctLoopTest.Component.Result.LoopResult = 2,
  4263. [64].Function = Fac_ActivationDiversion,
  4264. [64].u.ActivationDiversion.InvokeID = 67,
  4265. [64].u.ActivationDiversion.ComponentType = FacComponent_Invoke,
  4266. [64].u.ActivationDiversion.Component.Invoke.Procedure = 2,
  4267. [64].u.ActivationDiversion.Component.Invoke.BasicService = 3,
  4268. [64].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Type = 4,
  4269. [64].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.LengthOfNumber = 4,
  4270. [64].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number = "1803",
  4271. [64].u.ActivationDiversion.Component.Invoke.ServedUser.Type = 4,
  4272. [64].u.ActivationDiversion.Component.Invoke.ServedUser.LengthOfNumber = 4,
  4273. [64].u.ActivationDiversion.Component.Invoke.ServedUser.Number = "5398",
  4274. [65].Function = Fac_ActivationDiversion,
  4275. [65].u.ActivationDiversion.InvokeID = 68,
  4276. [65].u.ActivationDiversion.ComponentType = FacComponent_Invoke,
  4277. [65].u.ActivationDiversion.Component.Invoke.Procedure = 1,
  4278. [65].u.ActivationDiversion.Component.Invoke.BasicService = 5,
  4279. [65].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Type = 4,
  4280. [65].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.LengthOfNumber = 4,
  4281. [65].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number = "1803",
  4282. [66].Function = Fac_ActivationDiversion,
  4283. [66].u.ActivationDiversion.InvokeID = 69,
  4284. [66].u.ActivationDiversion.ComponentType = FacComponent_Result,
  4285. [67].Function = Fac_DeactivationDiversion,
  4286. [67].u.DeactivationDiversion.InvokeID = 70,
  4287. [67].u.DeactivationDiversion.ComponentType = FacComponent_Invoke,
  4288. [67].u.DeactivationDiversion.Component.Invoke.Procedure = 1,
  4289. [67].u.DeactivationDiversion.Component.Invoke.BasicService = 5,
  4290. [68].Function = Fac_DeactivationDiversion,
  4291. [68].u.DeactivationDiversion.InvokeID = 71,
  4292. [68].u.DeactivationDiversion.ComponentType = FacComponent_Result,
  4293. [69].Function = Fac_ActivationStatusNotificationDiv,
  4294. [69].u.ActivationStatusNotificationDiv.InvokeID = 72,
  4295. [69].u.ActivationStatusNotificationDiv.Procedure = 1,
  4296. [69].u.ActivationStatusNotificationDiv.BasicService = 5,
  4297. [69].u.ActivationStatusNotificationDiv.ForwardedTo.Party.Type = 4,
  4298. [69].u.ActivationStatusNotificationDiv.ForwardedTo.Party.LengthOfNumber = 4,
  4299. [69].u.ActivationStatusNotificationDiv.ForwardedTo.Party.Number = "1803",
  4300. [70].Function = Fac_DeactivationStatusNotificationDiv,
  4301. [70].u.DeactivationStatusNotificationDiv.InvokeID = 73,
  4302. [70].u.DeactivationStatusNotificationDiv.Procedure = 1,
  4303. [70].u.DeactivationStatusNotificationDiv.BasicService = 5,
  4304. [71].Function = Fac_InterrogationDiversion,
  4305. [71].u.InterrogationDiversion.InvokeID = 74,
  4306. [71].u.InterrogationDiversion.ComponentType = FacComponent_Invoke,
  4307. [71].u.InterrogationDiversion.Component.Invoke.Procedure = 1,
  4308. [71].u.InterrogationDiversion.Component.Invoke.BasicService = 5,
  4309. [72].Function = Fac_InterrogationDiversion,
  4310. [72].u.InterrogationDiversion.InvokeID = 75,
  4311. [72].u.InterrogationDiversion.ComponentType = FacComponent_Invoke,
  4312. [72].u.InterrogationDiversion.Component.Invoke.Procedure = 1,
  4313. [73].Function = Fac_InterrogationDiversion,
  4314. [73].u.InterrogationDiversion.InvokeID = 76,
  4315. [73].u.InterrogationDiversion.ComponentType = FacComponent_Result,
  4316. [73].u.InterrogationDiversion.Component.Result.NumRecords = 2,
  4317. [73].u.InterrogationDiversion.Component.Result.List[0].Procedure = 2,
  4318. [73].u.InterrogationDiversion.Component.Result.List[0].BasicService = 5,
  4319. [73].u.InterrogationDiversion.Component.Result.List[0].ForwardedTo.Party.Type = 4,
  4320. [73].u.InterrogationDiversion.Component.Result.List[0].ForwardedTo.Party.LengthOfNumber = 4,
  4321. [73].u.InterrogationDiversion.Component.Result.List[0].ForwardedTo.Party.Number = "1803",
  4322. [73].u.InterrogationDiversion.Component.Result.List[1].Procedure = 1,
  4323. [73].u.InterrogationDiversion.Component.Result.List[1].BasicService = 3,
  4324. [73].u.InterrogationDiversion.Component.Result.List[1].ForwardedTo.Party.Type = 4,
  4325. [73].u.InterrogationDiversion.Component.Result.List[1].ForwardedTo.Party.LengthOfNumber = 4,
  4326. [73].u.InterrogationDiversion.Component.Result.List[1].ForwardedTo.Party.Number = "1903",
  4327. [73].u.InterrogationDiversion.Component.Result.List[1].ServedUser.Type = 4,
  4328. [73].u.InterrogationDiversion.Component.Result.List[1].ServedUser.LengthOfNumber = 4,
  4329. [73].u.InterrogationDiversion.Component.Result.List[1].ServedUser.Number = "5398",
  4330. [74].Function = Fac_DiversionInformation,
  4331. [74].u.DiversionInformation.InvokeID = 77,
  4332. [74].u.DiversionInformation.DiversionReason = 3,
  4333. [74].u.DiversionInformation.BasicService = 5,
  4334. [74].u.DiversionInformation.ServedUserSubaddress.Type = 1,
  4335. [74].u.DiversionInformation.ServedUserSubaddress.Length = 4,
  4336. [74].u.DiversionInformation.ServedUserSubaddress.u.Nsap = "6492",
  4337. [74].u.DiversionInformation.CallingAddressPresent = 1,
  4338. [74].u.DiversionInformation.CallingAddress.Type = 0,
  4339. [74].u.DiversionInformation.CallingAddress.Address.ScreeningIndicator = 3,
  4340. [74].u.DiversionInformation.CallingAddress.Address.Party.Type = 4,
  4341. [74].u.DiversionInformation.CallingAddress.Address.Party.LengthOfNumber = 4,
  4342. [74].u.DiversionInformation.CallingAddress.Address.Party.Number = "1803",
  4343. [74].u.DiversionInformation.OriginalCalledPresent = 1,
  4344. [74].u.DiversionInformation.OriginalCalled.Type = 1,
  4345. [74].u.DiversionInformation.LastDivertingPresent = 1,
  4346. [74].u.DiversionInformation.LastDiverting.Type = 2,
  4347. [74].u.DiversionInformation.LastDivertingReasonPresent = 1,
  4348. [74].u.DiversionInformation.LastDivertingReason = 3,
  4349. [74].u.DiversionInformation.UserInfo.Length = 5,
  4350. [74].u.DiversionInformation.UserInfo.Contents = "79828",
  4351. [75].Function = Fac_DiversionInformation,
  4352. [75].u.DiversionInformation.InvokeID = 78,
  4353. [75].u.DiversionInformation.DiversionReason = 3,
  4354. [75].u.DiversionInformation.BasicService = 5,
  4355. [75].u.DiversionInformation.CallingAddressPresent = 1,
  4356. [75].u.DiversionInformation.CallingAddress.Type = 1,
  4357. [75].u.DiversionInformation.OriginalCalledPresent = 1,
  4358. [75].u.DiversionInformation.OriginalCalled.Type = 2,
  4359. [75].u.DiversionInformation.LastDivertingPresent = 1,
  4360. [75].u.DiversionInformation.LastDiverting.Type = 1,
  4361. [76].Function = Fac_DiversionInformation,
  4362. [76].u.DiversionInformation.InvokeID = 79,
  4363. [76].u.DiversionInformation.DiversionReason = 2,
  4364. [76].u.DiversionInformation.BasicService = 3,
  4365. [76].u.DiversionInformation.CallingAddressPresent = 1,
  4366. [76].u.DiversionInformation.CallingAddress.Type = 2,
  4367. [77].Function = Fac_DiversionInformation,
  4368. [77].u.DiversionInformation.InvokeID = 80,
  4369. [77].u.DiversionInformation.DiversionReason = 3,
  4370. [77].u.DiversionInformation.BasicService = 5,
  4371. [77].u.DiversionInformation.CallingAddressPresent = 1,
  4372. [77].u.DiversionInformation.CallingAddress.Type = 3,
  4373. [77].u.DiversionInformation.CallingAddress.Address.ScreeningIndicator = 2,
  4374. [77].u.DiversionInformation.CallingAddress.Address.Party.Type = 4,
  4375. [77].u.DiversionInformation.CallingAddress.Address.Party.LengthOfNumber = 4,
  4376. [77].u.DiversionInformation.CallingAddress.Address.Party.Number = "1803",
  4377. [78].Function = Fac_DiversionInformation,
  4378. [78].u.DiversionInformation.InvokeID = 81,
  4379. [78].u.DiversionInformation.DiversionReason = 2,
  4380. [78].u.DiversionInformation.BasicService = 4,
  4381. [78].u.DiversionInformation.UserInfo.Length = 5,
  4382. [78].u.DiversionInformation.UserInfo.Contents = "79828",
  4383. [79].Function = Fac_DiversionInformation,
  4384. [79].u.DiversionInformation.InvokeID = 82,
  4385. [79].u.DiversionInformation.DiversionReason = 2,
  4386. [79].u.DiversionInformation.BasicService = 4,
  4387. [80].Function = Fac_CallDeflection,
  4388. [80].u.CallDeflection.InvokeID = 83,
  4389. [80].u.CallDeflection.ComponentType = FacComponent_Invoke,
  4390. [80].u.CallDeflection.Component.Invoke.Deflection.Party.Type = 4,
  4391. [80].u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = 4,
  4392. [80].u.CallDeflection.Component.Invoke.Deflection.Party.Number = "1803",
  4393. [80].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1,
  4394. [80].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 1,
  4395. [81].Function = Fac_CallDeflection,
  4396. [81].u.CallDeflection.InvokeID = 84,
  4397. [81].u.CallDeflection.ComponentType = FacComponent_Invoke,
  4398. [81].u.CallDeflection.Component.Invoke.Deflection.Party.Type = 4,
  4399. [81].u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = 4,
  4400. [81].u.CallDeflection.Component.Invoke.Deflection.Party.Number = "1803",
  4401. [81].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1,
  4402. [81].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 0,
  4403. [82].Function = Fac_CallDeflection,
  4404. [82].u.CallDeflection.InvokeID = 85,
  4405. [82].u.CallDeflection.ComponentType = FacComponent_Invoke,
  4406. [82].u.CallDeflection.Component.Invoke.Deflection.Party.Type = 4,
  4407. [82].u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = 4,
  4408. [82].u.CallDeflection.Component.Invoke.Deflection.Party.Number = "1803",
  4409. [83].Function = Fac_CallDeflection,
  4410. [83].u.CallDeflection.InvokeID = 86,
  4411. [83].u.CallDeflection.ComponentType = FacComponent_Result,
  4412. [84].Function = Fac_CallRerouteing,
  4413. [84].u.CallRerouteing.InvokeID = 87,
  4414. [84].u.CallRerouteing.ComponentType = FacComponent_Invoke,
  4415. [84].u.CallRerouteing.Component.Invoke.ReroutingReason = 3,
  4416. [84].u.CallRerouteing.Component.Invoke.ReroutingCounter = 2,
  4417. [84].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 4,
  4418. [84].u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = 4,
  4419. [84].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number = "1803",
  4420. [84].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 2,
  4421. [84].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents = "RT",
  4422. [84].u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Length = 3,
  4423. [84].u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Contents = "RTG",
  4424. [84].u.CallRerouteing.Component.Invoke.Q931ie.Llc.Length = 2,
  4425. [84].u.CallRerouteing.Component.Invoke.Q931ie.Llc.Contents = "MY",
  4426. [84].u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Length = 5,
  4427. [84].u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Contents = "YEHAW",
  4428. [84].u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1,
  4429. [84].u.CallRerouteing.Component.Invoke.SubscriptionOption = 2,
  4430. [84].u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Type = 1,
  4431. [84].u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length = 4,
  4432. [84].u.CallRerouteing.Component.Invoke.CallingPartySubaddress.u.Nsap = "6492",
  4433. [85].Function = Fac_CallRerouteing,
  4434. [85].u.CallRerouteing.InvokeID = 88,
  4435. [85].u.CallRerouteing.ComponentType = FacComponent_Invoke,
  4436. [85].u.CallRerouteing.Component.Invoke.ReroutingReason = 3,
  4437. [85].u.CallRerouteing.Component.Invoke.ReroutingCounter = 2,
  4438. [85].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 4,
  4439. [85].u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = 4,
  4440. [85].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number = "1803",
  4441. [85].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 2,
  4442. [85].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents = "RT",
  4443. [85].u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1,
  4444. [85].u.CallRerouteing.Component.Invoke.SubscriptionOption = 2,
  4445. [86].Function = Fac_CallRerouteing,
  4446. [86].u.CallRerouteing.InvokeID = 89,
  4447. [86].u.CallRerouteing.ComponentType = FacComponent_Invoke,
  4448. [86].u.CallRerouteing.Component.Invoke.ReroutingReason = 3,
  4449. [86].u.CallRerouteing.Component.Invoke.ReroutingCounter = 2,
  4450. [86].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 4,
  4451. [86].u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = 4,
  4452. [86].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number = "1803",
  4453. [86].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 2,
  4454. [86].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents = "RT",
  4455. [86].u.CallRerouteing.Component.Invoke.LastRerouting.Type = 2,
  4456. [87].Function = Fac_CallRerouteing,
  4457. [87].u.CallRerouteing.InvokeID = 90,
  4458. [87].u.CallRerouteing.ComponentType = FacComponent_Result,
  4459. [88].Function = Fac_InterrogateServedUserNumbers,
  4460. [88].u.InterrogateServedUserNumbers.InvokeID = 91,
  4461. [88].u.InterrogateServedUserNumbers.ComponentType = FacComponent_Invoke,
  4462. [89].Function = Fac_InterrogateServedUserNumbers,
  4463. [89].u.InterrogateServedUserNumbers.InvokeID = 92,
  4464. [89].u.InterrogateServedUserNumbers.ComponentType = FacComponent_Result,
  4465. [89].u.InterrogateServedUserNumbers.Component.Result.NumRecords = 2,
  4466. [89].u.InterrogateServedUserNumbers.Component.Result.List[0].Type = 4,
  4467. [89].u.InterrogateServedUserNumbers.Component.Result.List[0].LengthOfNumber = 4,
  4468. [89].u.InterrogateServedUserNumbers.Component.Result.List[0].Number = "1803",
  4469. [89].u.InterrogateServedUserNumbers.Component.Result.List[1].Type = 4,
  4470. [89].u.InterrogateServedUserNumbers.Component.Result.List[1].LengthOfNumber = 4,
  4471. [89].u.InterrogateServedUserNumbers.Component.Result.List[1].Number = "5786",
  4472. [90].Function = Fac_DivertingLegInformation1,
  4473. [90].u.DivertingLegInformation1.InvokeID = 93,
  4474. [90].u.DivertingLegInformation1.DiversionReason = 4,
  4475. [90].u.DivertingLegInformation1.SubscriptionOption = 1,
  4476. [90].u.DivertingLegInformation1.DivertedToPresent = 1,
  4477. [90].u.DivertingLegInformation1.DivertedTo.Type = 2,
  4478. [91].Function = Fac_DivertingLegInformation1,
  4479. [91].u.DivertingLegInformation1.InvokeID = 94,
  4480. [91].u.DivertingLegInformation1.DiversionReason = 4,
  4481. [91].u.DivertingLegInformation1.SubscriptionOption = 1,
  4482. [92].Function = Fac_DivertingLegInformation2,
  4483. [92].u.DivertingLegInformation2.InvokeID = 95,
  4484. [92].u.DivertingLegInformation2.DiversionCounter = 3,
  4485. [92].u.DivertingLegInformation2.DiversionReason = 2,
  4486. [92].u.DivertingLegInformation2.DivertingPresent = 1,
  4487. [92].u.DivertingLegInformation2.Diverting.Type = 2,
  4488. [92].u.DivertingLegInformation2.OriginalCalledPresent = 1,
  4489. [92].u.DivertingLegInformation2.OriginalCalled.Type = 1,
  4490. [93].Function = Fac_DivertingLegInformation2,
  4491. [93].u.DivertingLegInformation2.InvokeID = 96,
  4492. [93].u.DivertingLegInformation2.DiversionCounter = 3,
  4493. [93].u.DivertingLegInformation2.DiversionReason = 2,
  4494. [93].u.DivertingLegInformation2.OriginalCalledPresent = 1,
  4495. [93].u.DivertingLegInformation2.OriginalCalled.Type = 1,
  4496. [94].Function = Fac_DivertingLegInformation2,
  4497. [94].u.DivertingLegInformation2.InvokeID = 97,
  4498. [94].u.DivertingLegInformation2.DiversionCounter = 1,
  4499. [94].u.DivertingLegInformation2.DiversionReason = 2,
  4500. [95].Function = Fac_DivertingLegInformation3,
  4501. [95].u.DivertingLegInformation3.InvokeID = 98,
  4502. [95].u.DivertingLegInformation3.PresentationAllowedIndicator = 1,
  4503. /* *INDENT-ON* */
  4504. };
  4505. #endif /* defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES) */
  4506. static char *handle_cli_misdn_send_facility(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4507. {
  4508. const char *channame;
  4509. const char *nr;
  4510. struct chan_list *tmp;
  4511. int port;
  4512. const char *served_nr;
  4513. struct misdn_bchannel dummy, *bc=&dummy;
  4514. unsigned max_len;
  4515. switch (cmd) {
  4516. case CLI_INIT:
  4517. e->command = "misdn send facility";
  4518. e->usage = "Usage: misdn send facility <type> <channel|port> \"<args>\" \n"
  4519. "\t type is one of:\n"
  4520. "\t - calldeflect\n"
  4521. #if defined(AST_MISDN_ENHANCEMENTS)
  4522. "\t - callrerouting\n"
  4523. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  4524. "\t - CFActivate\n"
  4525. "\t - CFDeactivate\n";
  4526. return NULL;
  4527. case CLI_GENERATE:
  4528. return complete_ch(a);
  4529. }
  4530. if (a->argc < 5) {
  4531. return CLI_SHOWUSAGE;
  4532. }
  4533. if (strstr(a->argv[3], "calldeflect")) {
  4534. if (a->argc < 6) {
  4535. ast_verbose("calldeflect requires 1 arg: ToNumber\n\n");
  4536. return 0;
  4537. }
  4538. channame = a->argv[4];
  4539. nr = a->argv[5];
  4540. ast_verbose("Sending Calldeflection (%s) to %s\n", nr, channame);
  4541. tmp = get_chan_by_ast_name(channame);
  4542. if (!tmp) {
  4543. ast_verbose("Sending CD with nr %s to %s failed: Channel does not exist.\n", nr, channame);
  4544. return 0;
  4545. }
  4546. ao2_lock(tmp);
  4547. #if defined(AST_MISDN_ENHANCEMENTS)
  4548. max_len = sizeof(tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number) - 1;
  4549. if (max_len < strlen(nr)) {
  4550. ast_verbose("Sending CD with nr %s to %s failed: Number too long (up to %u digits are allowed).\n",
  4551. nr, channame, max_len);
  4552. ao2_unlock(tmp);
  4553. chan_list_unref(tmp, "Number too long");
  4554. return 0;
  4555. }
  4556. tmp->bc->fac_out.Function = Fac_CallDeflection;
  4557. tmp->bc->fac_out.u.CallDeflection.InvokeID = ++misdn_invoke_id;
  4558. tmp->bc->fac_out.u.CallDeflection.ComponentType = FacComponent_Invoke;
  4559. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1;
  4560. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 0;
  4561. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Type = 0;/* unknown */
  4562. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = strlen(nr);
  4563. strcpy((char *) tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number, nr);
  4564. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Subaddress.Length = 0;
  4565. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  4566. max_len = sizeof(tmp->bc->fac_out.u.CDeflection.DeflectedToNumber) - 1;
  4567. if (max_len < strlen(nr)) {
  4568. ast_verbose("Sending CD with nr %s to %s failed: Number too long (up to %u digits are allowed).\n",
  4569. nr, channame, max_len);
  4570. ao2_unlock(tmp);
  4571. chan_list_unref(tmp, "Number too long");
  4572. return 0;
  4573. }
  4574. tmp->bc->fac_out.Function = Fac_CD;
  4575. tmp->bc->fac_out.u.CDeflection.PresentationAllowed = 0;
  4576. //tmp->bc->fac_out.u.CDeflection.DeflectedToSubaddress[0] = 0;
  4577. strcpy((char *) tmp->bc->fac_out.u.CDeflection.DeflectedToNumber, nr);
  4578. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  4579. /* Send message */
  4580. print_facility(&tmp->bc->fac_out, tmp->bc);
  4581. ao2_unlock(tmp);
  4582. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  4583. chan_list_unref(tmp, "Send facility complete");
  4584. #if defined(AST_MISDN_ENHANCEMENTS)
  4585. } else if (strstr(a->argv[3], "callrerouteing") || strstr(a->argv[3], "callrerouting")) {
  4586. if (a->argc < 6) {
  4587. ast_verbose("callrerouting requires 1 arg: ToNumber\n\n");
  4588. return 0;
  4589. }
  4590. channame = a->argv[4];
  4591. nr = a->argv[5];
  4592. ast_verbose("Sending Callrerouting (%s) to %s\n", nr, channame);
  4593. tmp = get_chan_by_ast_name(channame);
  4594. if (!tmp) {
  4595. ast_verbose("Sending Call Rerouting with nr %s to %s failed: Channel does not exist.\n", nr, channame);
  4596. return 0;
  4597. }
  4598. ao2_lock(tmp);
  4599. max_len = sizeof(tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number) - 1;
  4600. if (max_len < strlen(nr)) {
  4601. ast_verbose("Sending Call Rerouting with nr %s to %s failed: Number too long (up to %u digits are allowed).\n",
  4602. nr, channame, max_len);
  4603. ao2_unlock(tmp);
  4604. chan_list_unref(tmp, "Number too long");
  4605. return 0;
  4606. }
  4607. tmp->bc->fac_out.Function = Fac_CallRerouteing;
  4608. tmp->bc->fac_out.u.CallRerouteing.InvokeID = ++misdn_invoke_id;
  4609. tmp->bc->fac_out.u.CallRerouteing.ComponentType = FacComponent_Invoke;
  4610. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingReason = 0;/* unknown */
  4611. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingCounter = 1;
  4612. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 0;/* unknown */
  4613. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = strlen(nr);
  4614. strcpy((char *) tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number, nr);
  4615. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Subaddress.Length = 0;
  4616. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length = 0;
  4617. /* 0x90 0x90 0xa3 3.1 kHz audio, circuit mode, 64kbit/sec, level1/a-Law */
  4618. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 3;
  4619. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[0] = 0x90;
  4620. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[1] = 0x90;
  4621. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[2] = 0xa3;
  4622. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Length = 0;
  4623. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Llc.Length = 0;
  4624. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Length = 0;
  4625. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1;/* presentationRestricted */
  4626. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.SubscriptionOption = 0;/* no notification to caller */
  4627. /* Send message */
  4628. print_facility(&tmp->bc->fac_out, tmp->bc);
  4629. ao2_unlock(tmp);
  4630. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  4631. chan_list_unref(tmp, "Send facility complete");
  4632. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  4633. } else if (strstr(a->argv[3], "CFActivate")) {
  4634. if (a->argc < 7) {
  4635. ast_verbose("CFActivate requires 2 args: 1.FromNumber, 2.ToNumber\n\n");
  4636. return 0;
  4637. }
  4638. port = atoi(a->argv[4]);
  4639. served_nr = a->argv[5];
  4640. nr = a->argv[6];
  4641. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4642. ast_verbose("Sending CFActivate Port:(%d) FromNr. (%s) to Nr. (%s)\n", port, served_nr, nr);
  4643. #if defined(AST_MISDN_ENHANCEMENTS)
  4644. bc->fac_out.Function = Fac_ActivationDiversion;
  4645. bc->fac_out.u.ActivationDiversion.InvokeID = ++misdn_invoke_id;
  4646. bc->fac_out.u.ActivationDiversion.ComponentType = FacComponent_Invoke;
  4647. bc->fac_out.u.ActivationDiversion.Component.Invoke.BasicService = 0;/* allServices */
  4648. bc->fac_out.u.ActivationDiversion.Component.Invoke.Procedure = 0;/* cfu (Call Forward Unconditional) */
  4649. ast_copy_string((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Number,
  4650. served_nr, sizeof(bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Number));
  4651. bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.LengthOfNumber =
  4652. strlen((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Number);
  4653. bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Type = 0;/* unknown */
  4654. ast_copy_string((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number,
  4655. nr, sizeof(bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number));
  4656. bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.LengthOfNumber =
  4657. strlen((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number);
  4658. bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Type = 0;/* unknown */
  4659. bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Subaddress.Length = 0;
  4660. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  4661. bc->fac_out.Function = Fac_CFActivate;
  4662. bc->fac_out.u.CFActivate.BasicService = 0; /* All Services */
  4663. bc->fac_out.u.CFActivate.Procedure = 0; /* Unconditional */
  4664. ast_copy_string((char *) bc->fac_out.u.CFActivate.ServedUserNumber, served_nr, sizeof(bc->fac_out.u.CFActivate.ServedUserNumber));
  4665. ast_copy_string((char *) bc->fac_out.u.CFActivate.ForwardedToNumber, nr, sizeof(bc->fac_out.u.CFActivate.ForwardedToNumber));
  4666. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  4667. /* Send message */
  4668. print_facility(&bc->fac_out, bc);
  4669. misdn_lib_send_event(bc, EVENT_FACILITY);
  4670. } else if (strstr(a->argv[3], "CFDeactivate")) {
  4671. if (a->argc < 6) {
  4672. ast_verbose("CFDeactivate requires 1 arg: FromNumber\n\n");
  4673. return 0;
  4674. }
  4675. port = atoi(a->argv[4]);
  4676. served_nr = a->argv[5];
  4677. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4678. ast_verbose("Sending CFDeactivate Port:(%d) FromNr. (%s)\n", port, served_nr);
  4679. #if defined(AST_MISDN_ENHANCEMENTS)
  4680. bc->fac_out.Function = Fac_DeactivationDiversion;
  4681. bc->fac_out.u.DeactivationDiversion.InvokeID = ++misdn_invoke_id;
  4682. bc->fac_out.u.DeactivationDiversion.ComponentType = FacComponent_Invoke;
  4683. bc->fac_out.u.DeactivationDiversion.Component.Invoke.BasicService = 0;/* allServices */
  4684. bc->fac_out.u.DeactivationDiversion.Component.Invoke.Procedure = 0;/* cfu (Call Forward Unconditional) */
  4685. ast_copy_string((char *) bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Number,
  4686. served_nr, sizeof(bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Number));
  4687. bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.LengthOfNumber =
  4688. strlen((char *) bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Number);
  4689. bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Type = 0;/* unknown */
  4690. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  4691. bc->fac_out.Function = Fac_CFDeactivate;
  4692. bc->fac_out.u.CFDeactivate.BasicService = 0; /* All Services */
  4693. bc->fac_out.u.CFDeactivate.Procedure = 0; /* Unconditional */
  4694. ast_copy_string((char *) bc->fac_out.u.CFActivate.ServedUserNumber, served_nr, sizeof(bc->fac_out.u.CFActivate.ServedUserNumber));
  4695. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  4696. /* Send message */
  4697. print_facility(&bc->fac_out, bc);
  4698. misdn_lib_send_event(bc, EVENT_FACILITY);
  4699. #if defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES)
  4700. } else if (strstr(a->argv[3], "test")) {
  4701. int msg_number;
  4702. if (a->argc < 5) {
  4703. ast_verbose("test (<port> [<msg#>]) | (<channel-name> <msg#>)\n\n");
  4704. return 0;
  4705. }
  4706. port = atoi(a->argv[4]);
  4707. channame = a->argv[4];
  4708. tmp = get_chan_by_ast_name(channame);
  4709. if (tmp) {
  4710. /* We are going to send this FACILITY message out on an existing connection */
  4711. msg_number = atoi(a->argv[5]);
  4712. if (msg_number < ARRAY_LEN(Fac_Msgs)) {
  4713. ao2_lock(tmp);
  4714. tmp->bc->fac_out = Fac_Msgs[msg_number];
  4715. /* Send message */
  4716. print_facility(&tmp->bc->fac_out, tmp->bc);
  4717. ao2_unlock(tmp);
  4718. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  4719. } else {
  4720. ast_verbose("test <channel-name> <msg#>\n\n");
  4721. }
  4722. chan_list_unref(tmp, "Facility test done");
  4723. } else if (a->argc < 6) {
  4724. for (msg_number = 0; msg_number < ARRAY_LEN(Fac_Msgs); ++msg_number) {
  4725. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4726. bc->fac_out = Fac_Msgs[msg_number];
  4727. /* Send message */
  4728. print_facility(&bc->fac_out, bc);
  4729. misdn_lib_send_event(bc, EVENT_FACILITY);
  4730. sleep(1);
  4731. }
  4732. } else {
  4733. msg_number = atoi(a->argv[5]);
  4734. if (msg_number < ARRAY_LEN(Fac_Msgs)) {
  4735. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4736. bc->fac_out = Fac_Msgs[msg_number];
  4737. /* Send message */
  4738. print_facility(&bc->fac_out, bc);
  4739. misdn_lib_send_event(bc, EVENT_FACILITY);
  4740. } else {
  4741. ast_verbose("test <port> [<msg#>]\n\n");
  4742. }
  4743. }
  4744. } else if (strstr(a->argv[3], "register")) {
  4745. if (a->argc < 5) {
  4746. ast_verbose("register <port>\n\n");
  4747. return 0;
  4748. }
  4749. port = atoi(a->argv[4]);
  4750. bc = misdn_lib_get_register_bc(port);
  4751. if (!bc) {
  4752. ast_verbose("Could not allocate REGISTER bc struct\n\n");
  4753. return 0;
  4754. }
  4755. bc->fac_out = Fac_Msgs[45];
  4756. /* Send message */
  4757. print_facility(&bc->fac_out, bc);
  4758. misdn_lib_send_event(bc, EVENT_REGISTER);
  4759. #endif /* defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES) */
  4760. }
  4761. return CLI_SUCCESS;
  4762. }
  4763. static char *handle_cli_misdn_send_restart(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4764. {
  4765. int port;
  4766. int channel;
  4767. switch (cmd) {
  4768. case CLI_INIT:
  4769. e->command = "misdn send restart";
  4770. e->usage =
  4771. "Usage: misdn send restart [port [channel]]\n"
  4772. " Send a restart for every bchannel on the given port.\n";
  4773. return NULL;
  4774. case CLI_GENERATE:
  4775. return NULL;
  4776. }
  4777. if (a->argc < 4 || a->argc > 5) {
  4778. return CLI_SHOWUSAGE;
  4779. }
  4780. port = atoi(a->argv[3]);
  4781. if (a->argc == 5) {
  4782. channel = atoi(a->argv[4]);
  4783. misdn_lib_send_restart(port, channel);
  4784. } else {
  4785. misdn_lib_send_restart(port, -1);
  4786. }
  4787. return CLI_SUCCESS;
  4788. }
  4789. static char *handle_cli_misdn_send_digit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4790. {
  4791. const char *channame;
  4792. const char *msg;
  4793. struct chan_list *tmp;
  4794. int i, msglen;
  4795. switch (cmd) {
  4796. case CLI_INIT:
  4797. e->command = "misdn send digit";
  4798. e->usage =
  4799. "Usage: misdn send digit <channel> \"<msg>\" \n"
  4800. " Send <digit> to <channel> as DTMF Tone\n"
  4801. " when channel is a mISDN channel\n";
  4802. return NULL;
  4803. case CLI_GENERATE:
  4804. return complete_ch(a);
  4805. }
  4806. if (a->argc != 5) {
  4807. return CLI_SHOWUSAGE;
  4808. }
  4809. channame = a->argv[3];
  4810. msg = a->argv[4];
  4811. msglen = strlen(msg);
  4812. ast_cli(a->fd, "Sending %s to %s\n", msg, channame);
  4813. tmp = get_chan_by_ast_name(channame);
  4814. if (!tmp) {
  4815. ast_cli(a->fd, "Sending %s to %s failed Channel does not exist\n", msg, channame);
  4816. return CLI_SUCCESS;
  4817. }
  4818. #if 1
  4819. for (i = 0; i < msglen; i++) {
  4820. if (!tmp->ast) {
  4821. break;
  4822. }
  4823. ast_cli(a->fd, "Sending: %c\n", msg[i]);
  4824. send_digit_to_chan(tmp, msg[i]);
  4825. /* res = ast_safe_sleep(tmp->ast, 250); */
  4826. usleep(250000);
  4827. /* res = ast_waitfor(tmp->ast,100); */
  4828. }
  4829. #else
  4830. if (tmp->ast) {
  4831. ast_dtmf_stream(tmp->ast, NULL, msg, 250);
  4832. }
  4833. #endif
  4834. chan_list_unref(tmp, "Digit(s) sent");
  4835. return CLI_SUCCESS;
  4836. }
  4837. static char *handle_cli_misdn_toggle_echocancel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4838. {
  4839. const char *channame;
  4840. struct chan_list *tmp;
  4841. switch (cmd) {
  4842. case CLI_INIT:
  4843. e->command = "misdn toggle echocancel";
  4844. e->usage =
  4845. "Usage: misdn toggle echocancel <channel>\n"
  4846. " Toggle EchoCancel on mISDN Channel.\n";
  4847. return NULL;
  4848. case CLI_GENERATE:
  4849. return complete_ch(a);
  4850. }
  4851. if (a->argc != 4) {
  4852. return CLI_SHOWUSAGE;
  4853. }
  4854. channame = a->argv[3];
  4855. ast_cli(a->fd, "Toggling EchoCancel on %s\n", channame);
  4856. tmp = get_chan_by_ast_name(channame);
  4857. if (!tmp) {
  4858. ast_cli(a->fd, "Toggling EchoCancel %s failed Channel does not exist\n", channame);
  4859. return CLI_SUCCESS;
  4860. }
  4861. tmp->toggle_ec = tmp->toggle_ec ? 0 : 1;
  4862. if (tmp->toggle_ec) {
  4863. #ifdef MISDN_1_2
  4864. update_pipeline_config(tmp->bc);
  4865. #else
  4866. update_ec_config(tmp->bc);
  4867. #endif
  4868. manager_ec_enable(tmp->bc);
  4869. } else {
  4870. manager_ec_disable(tmp->bc);
  4871. }
  4872. chan_list_unref(tmp, "Done toggling echo cancel");
  4873. return CLI_SUCCESS;
  4874. }
  4875. static char *handle_cli_misdn_send_display(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4876. {
  4877. const char *channame;
  4878. const char *msg;
  4879. struct chan_list *tmp;
  4880. switch (cmd) {
  4881. case CLI_INIT:
  4882. e->command = "misdn send display";
  4883. e->usage =
  4884. "Usage: misdn send display <channel> \"<msg>\" \n"
  4885. " Send <msg> to <channel> as Display Message\n"
  4886. " when channel is a mISDN channel\n";
  4887. return NULL;
  4888. case CLI_GENERATE:
  4889. return complete_ch(a);
  4890. }
  4891. if (a->argc != 5) {
  4892. return CLI_SHOWUSAGE;
  4893. }
  4894. channame = a->argv[3];
  4895. msg = a->argv[4];
  4896. ast_cli(a->fd, "Sending %s to %s\n", msg, channame);
  4897. tmp = get_chan_by_ast_name(channame);
  4898. if (tmp && tmp->bc) {
  4899. ast_copy_string(tmp->bc->display, msg, sizeof(tmp->bc->display));
  4900. misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
  4901. chan_list_unref(tmp, "Done sending display");
  4902. } else {
  4903. if (tmp) {
  4904. chan_list_unref(tmp, "Display failed");
  4905. }
  4906. ast_cli(a->fd, "No such channel %s\n", channame);
  4907. return CLI_SUCCESS;
  4908. }
  4909. return CLI_SUCCESS;
  4910. }
  4911. static char *complete_ch(struct ast_cli_args *a)
  4912. {
  4913. return ast_complete_channels(a->line, a->word, a->pos, a->n, 3);
  4914. }
  4915. static char *complete_debug_port(struct ast_cli_args *a)
  4916. {
  4917. if (a->n) {
  4918. return NULL;
  4919. }
  4920. switch (a->pos) {
  4921. case 4:
  4922. if (a->word[0] == 'p') {
  4923. return ast_strdup("port");
  4924. } else if (a->word[0] == 'o') {
  4925. return ast_strdup("only");
  4926. }
  4927. break;
  4928. case 6:
  4929. if (a->word[0] == 'o') {
  4930. return ast_strdup("only");
  4931. }
  4932. break;
  4933. }
  4934. return NULL;
  4935. }
  4936. static char *complete_show_config(struct ast_cli_args *a)
  4937. {
  4938. char buffer[BUFFERSIZE];
  4939. enum misdn_cfg_elements elem;
  4940. int wordlen = strlen(a->word);
  4941. int which = 0;
  4942. int port = 0;
  4943. switch (a->pos) {
  4944. case 3:
  4945. if ((!strncmp(a->word, "description", wordlen)) && (++which > a->n)) {
  4946. return ast_strdup("description");
  4947. }
  4948. if ((!strncmp(a->word, "descriptions", wordlen)) && (++which > a->n)) {
  4949. return ast_strdup("descriptions");
  4950. }
  4951. if ((!strncmp(a->word, "0", wordlen)) && (++which > a->n)) {
  4952. return ast_strdup("0");
  4953. }
  4954. while ((port = misdn_cfg_get_next_port(port)) != -1) {
  4955. snprintf(buffer, sizeof(buffer), "%d", port);
  4956. if ((!strncmp(a->word, buffer, wordlen)) && (++which > a->n)) {
  4957. return ast_strdup(buffer);
  4958. }
  4959. }
  4960. break;
  4961. case 4:
  4962. if (strstr(a->line, "description ")) {
  4963. for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
  4964. if ((elem == MISDN_CFG_LAST) || (elem == MISDN_GEN_FIRST)) {
  4965. continue;
  4966. }
  4967. misdn_cfg_get_name(elem, buffer, sizeof(buffer));
  4968. if (!wordlen || !strncmp(a->word, buffer, wordlen)) {
  4969. if (++which > a->n) {
  4970. return ast_strdup(buffer);
  4971. }
  4972. }
  4973. }
  4974. } else if (strstr(a->line, "descriptions ")) {
  4975. if ((!wordlen || !strncmp(a->word, "general", wordlen)) && (++which > a->n)) {
  4976. return ast_strdup("general");
  4977. }
  4978. if ((!wordlen || !strncmp(a->word, "ports", wordlen)) && (++which > a->n)) {
  4979. return ast_strdup("ports");
  4980. }
  4981. }
  4982. break;
  4983. }
  4984. return NULL;
  4985. }
  4986. static struct ast_cli_entry chan_misdn_clis[] = {
  4987. /* *INDENT-OFF* */
  4988. AST_CLI_DEFINE(handle_cli_misdn_port_block, "Block the given port"),
  4989. AST_CLI_DEFINE(handle_cli_misdn_port_down, "Try to deactivate the L1 on the given port"),
  4990. AST_CLI_DEFINE(handle_cli_misdn_port_unblock, "Unblock the given port"),
  4991. AST_CLI_DEFINE(handle_cli_misdn_port_up, "Try to establish L1 on the given port"),
  4992. AST_CLI_DEFINE(handle_cli_misdn_reload, "Reload internal mISDN config, read from the config file"),
  4993. AST_CLI_DEFINE(handle_cli_misdn_restart_pid, "Restart the given pid"),
  4994. AST_CLI_DEFINE(handle_cli_misdn_restart_port, "Restart the given port"),
  4995. AST_CLI_DEFINE(handle_cli_misdn_show_channel, "Show an internal mISDN channel"),
  4996. AST_CLI_DEFINE(handle_cli_misdn_show_channels, "Show the internal mISDN channel list"),
  4997. AST_CLI_DEFINE(handle_cli_misdn_show_config, "Show internal mISDN config, read from the config file"),
  4998. AST_CLI_DEFINE(handle_cli_misdn_show_port, "Show detailed information for given port"),
  4999. AST_CLI_DEFINE(handle_cli_misdn_show_ports_stats, "Show mISDNs channel's call statistics per port"),
  5000. AST_CLI_DEFINE(handle_cli_misdn_show_stacks, "Show internal mISDN stack_list"),
  5001. AST_CLI_DEFINE(handle_cli_misdn_send_facility, "Sends a Facility Message to the mISDN Channel"),
  5002. AST_CLI_DEFINE(handle_cli_misdn_send_digit, "Send DTMF digit to mISDN Channel"),
  5003. AST_CLI_DEFINE(handle_cli_misdn_send_display, "Send Text to mISDN Channel"),
  5004. AST_CLI_DEFINE(handle_cli_misdn_send_restart, "Send a restart for every bchannel on the given port"),
  5005. AST_CLI_DEFINE(handle_cli_misdn_set_crypt_debug, "Set CryptDebuglevel of chan_misdn, at the moment, level={1,2}"),
  5006. AST_CLI_DEFINE(handle_cli_misdn_set_debug, "Set Debuglevel of chan_misdn"),
  5007. AST_CLI_DEFINE(handle_cli_misdn_set_tics, "???"),
  5008. AST_CLI_DEFINE(handle_cli_misdn_toggle_echocancel, "Toggle EchoCancel on mISDN Channel"),
  5009. /* *INDENT-ON* */
  5010. };
  5011. /*! \brief Updates caller ID information from config */
  5012. static void update_config(struct chan_list *ch)
  5013. {
  5014. struct ast_channel *ast;
  5015. struct misdn_bchannel *bc;
  5016. int port;
  5017. int hdlc = 0;
  5018. int pres;
  5019. int screen;
  5020. if (!ch) {
  5021. ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
  5022. return;
  5023. }
  5024. ast = ch->ast;
  5025. bc = ch->bc;
  5026. if (! ast || ! bc) {
  5027. ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
  5028. return;
  5029. }
  5030. port = bc->port;
  5031. chan_misdn_log(7, port, "update_config: Getting Config\n");
  5032. misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(int));
  5033. if (hdlc) {
  5034. switch (bc->capability) {
  5035. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  5036. case INFO_CAPABILITY_DIGITAL_RESTRICTED:
  5037. chan_misdn_log(1, bc->port, " --> CONF HDLC\n");
  5038. bc->hdlc = 1;
  5039. break;
  5040. }
  5041. }
  5042. misdn_cfg_get(port, MISDN_CFG_PRES, &pres, sizeof(pres));
  5043. misdn_cfg_get(port, MISDN_CFG_SCREEN, &screen, sizeof(screen));
  5044. chan_misdn_log(2, port, " --> pres: %d screen: %d\n", pres, screen);
  5045. if (pres < 0 || screen < 0) {
  5046. chan_misdn_log(2, port, " --> pres: %x\n", ast->connected.id.number.presentation);
  5047. bc->caller.presentation = ast_to_misdn_pres(ast->connected.id.number.presentation);
  5048. chan_misdn_log(2, port, " --> PRES: %s(%d)\n", misdn_to_str_pres(bc->caller.presentation), bc->caller.presentation);
  5049. bc->caller.screening = ast_to_misdn_screen(ast->connected.id.number.presentation);
  5050. chan_misdn_log(2, port, " --> SCREEN: %s(%d)\n", misdn_to_str_screen(bc->caller.screening), bc->caller.screening);
  5051. } else {
  5052. bc->caller.screening = screen;
  5053. bc->caller.presentation = pres;
  5054. }
  5055. }
  5056. static void config_jitterbuffer(struct chan_list *ch)
  5057. {
  5058. struct misdn_bchannel *bc = ch->bc;
  5059. int len = ch->jb_len;
  5060. int threshold = ch->jb_upper_threshold;
  5061. chan_misdn_log(5, bc->port, "config_jb: Called\n");
  5062. if (!len) {
  5063. chan_misdn_log(1, bc->port, "config_jb: Deactivating Jitterbuffer\n");
  5064. bc->nojitter = 1;
  5065. } else {
  5066. if (len <= 100 || len > 8000) {
  5067. chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer out of Bounds, setting to 1000\n");
  5068. len = 1000;
  5069. }
  5070. if (threshold > len) {
  5071. chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer Threshold > Jitterbuffer setting to Jitterbuffer -1\n");
  5072. }
  5073. if (ch->jb) {
  5074. cb_log(0, bc->port, "config_jb: We've got a Jitterbuffer Already on this port.\n");
  5075. misdn_jb_destroy(ch->jb);
  5076. ch->jb = NULL;
  5077. }
  5078. ch->jb = misdn_jb_init(len, threshold);
  5079. if (!ch->jb) {
  5080. bc->nojitter = 1;
  5081. }
  5082. }
  5083. }
  5084. void debug_numtype(int port, int numtype, char *type)
  5085. {
  5086. switch (numtype) {
  5087. case NUMTYPE_UNKNOWN:
  5088. chan_misdn_log(2, port, " --> %s: Unknown\n", type);
  5089. break;
  5090. case NUMTYPE_INTERNATIONAL:
  5091. chan_misdn_log(2, port, " --> %s: International\n", type);
  5092. break;
  5093. case NUMTYPE_NATIONAL:
  5094. chan_misdn_log(2, port, " --> %s: National\n", type);
  5095. break;
  5096. case NUMTYPE_NETWORK_SPECIFIC:
  5097. chan_misdn_log(2, port, " --> %s: Network Specific\n", type);
  5098. break;
  5099. case NUMTYPE_SUBSCRIBER:
  5100. chan_misdn_log(2, port, " --> %s: Subscriber\n", type);
  5101. break;
  5102. case NUMTYPE_ABBREVIATED:
  5103. chan_misdn_log(2, port, " --> %s: Abbreviated\n", type);
  5104. break;
  5105. /* Maybe we should cut off the prefix if present ? */
  5106. default:
  5107. chan_misdn_log(0, port, " --> !!!! Wrong dialplan setting, please see the misdn.conf sample file\n ");
  5108. break;
  5109. }
  5110. }
  5111. #ifdef MISDN_1_2
  5112. static int update_pipeline_config(struct misdn_bchannel *bc)
  5113. {
  5114. int ec;
  5115. misdn_cfg_get(bc->port, MISDN_CFG_PIPELINE, bc->pipeline, sizeof(bc->pipeline));
  5116. if (*bc->pipeline) {
  5117. return 0;
  5118. }
  5119. misdn_cfg_get(bc->port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(ec));
  5120. if (ec == 1) {
  5121. ast_copy_string(bc->pipeline, "mg2ec", sizeof(bc->pipeline));
  5122. } else if (ec > 1) {
  5123. snprintf(bc->pipeline, sizeof(bc->pipeline), "mg2ec(deftaps=%d)", ec);
  5124. }
  5125. return 0;
  5126. }
  5127. #else
  5128. static int update_ec_config(struct misdn_bchannel *bc)
  5129. {
  5130. int ec;
  5131. int port = bc->port;
  5132. misdn_cfg_get(port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(ec));
  5133. if (ec == 1) {
  5134. bc->ec_enable = 1;
  5135. } else if (ec > 1) {
  5136. bc->ec_enable = 1;
  5137. bc->ec_deftaps = ec;
  5138. }
  5139. return 0;
  5140. }
  5141. #endif
  5142. static int read_config(struct chan_list *ch)
  5143. {
  5144. struct ast_channel *ast;
  5145. struct misdn_bchannel *bc;
  5146. int port;
  5147. int hdlc = 0;
  5148. char lang[BUFFERSIZE + 1];
  5149. char faxdetect[BUFFERSIZE + 1];
  5150. char buf[256];
  5151. char buf2[256];
  5152. ast_group_t pg;
  5153. ast_group_t cg;
  5154. if (!ch) {
  5155. ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
  5156. return -1;
  5157. }
  5158. ast = ch->ast;
  5159. bc = ch->bc;
  5160. if (! ast || ! bc) {
  5161. ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
  5162. return -1;
  5163. }
  5164. port = bc->port;
  5165. chan_misdn_log(1, port, "read_config: Getting Config\n");
  5166. misdn_cfg_get(port, MISDN_CFG_LANGUAGE, lang, sizeof(lang));
  5167. ast_string_field_set(ast, language, lang);
  5168. misdn_cfg_get(port, MISDN_CFG_MUSICCLASS, ch->mohinterpret, sizeof(ch->mohinterpret));
  5169. misdn_cfg_get(port, MISDN_CFG_TXGAIN, &bc->txgain, sizeof(bc->txgain));
  5170. misdn_cfg_get(port, MISDN_CFG_RXGAIN, &bc->rxgain, sizeof(bc->rxgain));
  5171. misdn_cfg_get(port, MISDN_CFG_INCOMING_EARLY_AUDIO, &ch->incoming_early_audio, sizeof(ch->incoming_early_audio));
  5172. misdn_cfg_get(port, MISDN_CFG_SENDDTMF, &bc->send_dtmf, sizeof(bc->send_dtmf));
  5173. misdn_cfg_get(port, MISDN_CFG_ASTDTMF, &ch->ast_dsp, sizeof(int));
  5174. if (ch->ast_dsp) {
  5175. ch->ignore_dtmf = 1;
  5176. }
  5177. misdn_cfg_get(port, MISDN_CFG_NEED_MORE_INFOS, &bc->need_more_infos, sizeof(bc->need_more_infos));
  5178. misdn_cfg_get(port, MISDN_CFG_NTTIMEOUT, &ch->nttimeout, sizeof(ch->nttimeout));
  5179. misdn_cfg_get(port, MISDN_CFG_NOAUTORESPOND_ON_SETUP, &ch->noautorespond_on_setup, sizeof(ch->noautorespond_on_setup));
  5180. misdn_cfg_get(port, MISDN_CFG_FAR_ALERTING, &ch->far_alerting, sizeof(ch->far_alerting));
  5181. misdn_cfg_get(port, MISDN_CFG_ALLOWED_BEARERS, &ch->allowed_bearers, sizeof(ch->allowed_bearers));
  5182. misdn_cfg_get(port, MISDN_CFG_FAXDETECT, faxdetect, sizeof(faxdetect));
  5183. misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(hdlc));
  5184. if (hdlc) {
  5185. switch (bc->capability) {
  5186. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  5187. case INFO_CAPABILITY_DIGITAL_RESTRICTED:
  5188. chan_misdn_log(1, bc->port, " --> CONF HDLC\n");
  5189. bc->hdlc = 1;
  5190. break;
  5191. }
  5192. }
  5193. /*Initialize new Jitterbuffer*/
  5194. misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER, &ch->jb_len, sizeof(ch->jb_len));
  5195. misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, &ch->jb_upper_threshold, sizeof(ch->jb_upper_threshold));
  5196. config_jitterbuffer(ch);
  5197. misdn_cfg_get(bc->port, MISDN_CFG_CONTEXT, ch->context, sizeof(ch->context));
  5198. ast_copy_string(ast->context, ch->context, sizeof(ast->context));
  5199. #ifdef MISDN_1_2
  5200. update_pipeline_config(bc);
  5201. #else
  5202. update_ec_config(bc);
  5203. #endif
  5204. misdn_cfg_get(bc->port, MISDN_CFG_EARLY_BCONNECT, &bc->early_bconnect, sizeof(bc->early_bconnect));
  5205. misdn_cfg_get(port, MISDN_CFG_DISPLAY_CONNECTED, &bc->display_connected, sizeof(bc->display_connected));
  5206. misdn_cfg_get(port, MISDN_CFG_DISPLAY_SETUP, &bc->display_setup, sizeof(bc->display_setup));
  5207. misdn_cfg_get(port, MISDN_CFG_OUTGOING_COLP, &bc->outgoing_colp, sizeof(bc->outgoing_colp));
  5208. misdn_cfg_get(port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg));
  5209. misdn_cfg_get(port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg));
  5210. chan_misdn_log(5, port, " --> * CallGrp:%s PickupGrp:%s\n", ast_print_group(buf, sizeof(buf), cg), ast_print_group(buf2, sizeof(buf2), pg));
  5211. ast->pickupgroup = pg;
  5212. ast->callgroup = cg;
  5213. if (ch->originator == ORG_AST) {
  5214. char callerid[BUFFERSIZE + 1];
  5215. /* ORIGINATOR Asterisk (outgoing call) */
  5216. misdn_cfg_get(port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(bc->te_choose_channel), sizeof(bc->te_choose_channel));
  5217. if (strstr(faxdetect, "outgoing") || strstr(faxdetect, "both")) {
  5218. ch->faxdetect = strstr(faxdetect, "nojump") ? 2 : 1;
  5219. }
  5220. misdn_cfg_get(port, MISDN_CFG_CALLERID, callerid, sizeof(callerid));
  5221. if (!ast_strlen_zero(callerid)) {
  5222. char *cid_name = NULL;
  5223. char *cid_num = NULL;
  5224. ast_callerid_parse(callerid, &cid_name, &cid_num);
  5225. if (cid_name) {
  5226. ast_copy_string(bc->caller.name, cid_name, sizeof(bc->caller.name));
  5227. } else {
  5228. bc->caller.name[0] = '\0';
  5229. }
  5230. if (cid_num) {
  5231. ast_copy_string(bc->caller.number, cid_num, sizeof(bc->caller.number));
  5232. } else {
  5233. bc->caller.number[0] = '\0';
  5234. }
  5235. chan_misdn_log(1, port, " --> * Setting caller to \"%s\" <%s>\n", bc->caller.name, bc->caller.number);
  5236. }
  5237. misdn_cfg_get(port, MISDN_CFG_DIALPLAN, &bc->dialed.number_type, sizeof(bc->dialed.number_type));
  5238. bc->dialed.number_plan = NUMPLAN_ISDN;
  5239. debug_numtype(port, bc->dialed.number_type, "TON");
  5240. ch->overlap_dial = 0;
  5241. } else {
  5242. /* ORIGINATOR MISDN (incoming call) */
  5243. if (strstr(faxdetect, "incoming") || strstr(faxdetect, "both")) {
  5244. ch->faxdetect = (strstr(faxdetect, "nojump")) ? 2 : 1;
  5245. }
  5246. /* Add configured prefix to caller.number */
  5247. misdn_add_number_prefix(bc->port, bc->caller.number_type, bc->caller.number, sizeof(bc->caller.number));
  5248. if (ast_strlen_zero(bc->dialed.number) && !ast_strlen_zero(bc->keypad)) {
  5249. ast_copy_string(bc->dialed.number, bc->keypad, sizeof(bc->dialed.number));
  5250. }
  5251. /* Add configured prefix to dialed.number */
  5252. misdn_add_number_prefix(bc->port, bc->dialed.number_type, bc->dialed.number, sizeof(bc->dialed.number));
  5253. ast_copy_string(ast->exten, bc->dialed.number, sizeof(ast->exten));
  5254. misdn_cfg_get(bc->port, MISDN_CFG_OVERLAP_DIAL, &ch->overlap_dial, sizeof(ch->overlap_dial));
  5255. ast_mutex_init(&ch->overlap_tv_lock);
  5256. } /* ORIG MISDN END */
  5257. misdn_cfg_get(port, MISDN_CFG_INCOMING_CALLERID_TAG, bc->incoming_cid_tag, sizeof(bc->incoming_cid_tag));
  5258. if (!ast_strlen_zero(bc->incoming_cid_tag)) {
  5259. chan_misdn_log(1, port, " --> * Setting incoming caller id tag to \"%s\"\n", bc->incoming_cid_tag);
  5260. }
  5261. ch->overlap_dial_task = -1;
  5262. if (ch->faxdetect || ch->ast_dsp) {
  5263. misdn_cfg_get(port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout));
  5264. if (!ch->dsp) {
  5265. ch->dsp = ast_dsp_new();
  5266. }
  5267. if (ch->dsp) {
  5268. ast_dsp_set_features(ch->dsp, DSP_FEATURE_DIGIT_DETECT | (ch->faxdetect ? DSP_FEATURE_FAX_DETECT : 0));
  5269. }
  5270. }
  5271. /* AOCD initialization */
  5272. bc->AOCDtype = Fac_None;
  5273. return 0;
  5274. }
  5275. /*!
  5276. * \internal
  5277. * \brief Send a connected line update to the other channel
  5278. *
  5279. * \param ast Current Asterisk channel
  5280. * \param id Party id information to send to the other side
  5281. * \param source Why are we sending this update
  5282. * \param cid_tag User tag to apply to the party id.
  5283. *
  5284. * \return Nothing
  5285. */
  5286. static void misdn_queue_connected_line_update(struct ast_channel *ast, const struct misdn_party_id *id, enum AST_CONNECTED_LINE_UPDATE_SOURCE source, char *cid_tag)
  5287. {
  5288. struct ast_party_connected_line connected;
  5289. struct ast_set_party_connected_line update_connected;
  5290. ast_party_connected_line_init(&connected);
  5291. memset(&update_connected, 0, sizeof(update_connected));
  5292. update_connected.id.number = 1;
  5293. connected.id.number.valid = 1;
  5294. connected.id.number.str = (char *) id->number;
  5295. connected.id.number.plan = misdn_to_ast_ton(id->number_type)
  5296. | misdn_to_ast_plan(id->number_plan);
  5297. connected.id.number.presentation = misdn_to_ast_pres(id->presentation)
  5298. | misdn_to_ast_screen(id->screening);
  5299. connected.id.tag = cid_tag;
  5300. connected.source = source;
  5301. ast_channel_queue_connected_line_update(ast, &connected, &update_connected);
  5302. }
  5303. /*!
  5304. * \internal
  5305. * \brief Update the caller id party on this channel.
  5306. *
  5307. * \param ast Current Asterisk channel
  5308. * \param id Remote party id information to update.
  5309. * \param cid_tag User tag to apply to the party id.
  5310. *
  5311. * \return Nothing
  5312. */
  5313. static void misdn_update_caller_id(struct ast_channel *ast, const struct misdn_party_id *id, char *cid_tag)
  5314. {
  5315. struct ast_party_caller caller;
  5316. struct ast_set_party_caller update_caller;
  5317. memset(&update_caller, 0, sizeof(update_caller));
  5318. update_caller.id.number = 1;
  5319. update_caller.ani.number = 1;
  5320. ast_channel_lock(ast);
  5321. ast_party_caller_set_init(&caller, &ast->caller);
  5322. caller.id.number.valid = 1;
  5323. caller.id.number.str = (char *) id->number;
  5324. caller.id.number.plan = misdn_to_ast_ton(id->number_type)
  5325. | misdn_to_ast_plan(id->number_plan);
  5326. caller.id.number.presentation = misdn_to_ast_pres(id->presentation)
  5327. | misdn_to_ast_screen(id->screening);
  5328. caller.ani.number = caller.id.number;
  5329. caller.id.tag = cid_tag;
  5330. caller.ani.tag = cid_tag;
  5331. ast_channel_set_caller_event(ast, &caller, &update_caller);
  5332. ast_channel_unlock(ast);
  5333. }
  5334. /*!
  5335. * \internal
  5336. * \brief Update the remote party id information.
  5337. *
  5338. * \param ast Current Asterisk channel
  5339. * \param id Remote party id information to update.
  5340. * \param source Why are we sending this update
  5341. * \param cid_tag User tag to apply to the party id.
  5342. *
  5343. * \return Nothing
  5344. */
  5345. static void misdn_update_remote_party(struct ast_channel *ast, const struct misdn_party_id *id, enum AST_CONNECTED_LINE_UPDATE_SOURCE source, char *cid_tag)
  5346. {
  5347. misdn_update_caller_id(ast, id, cid_tag);
  5348. misdn_queue_connected_line_update(ast, id, source, cid_tag);
  5349. }
  5350. /*!
  5351. * \internal
  5352. * \brief Get the connected line information out of the Asterisk channel.
  5353. *
  5354. * \param ast Current Asterisk channel
  5355. * \param bc Associated B channel
  5356. * \param originator Who originally created this channel. ORG_AST or ORG_MISDN
  5357. *
  5358. * \return Nothing
  5359. */
  5360. static void misdn_get_connected_line(struct ast_channel *ast, struct misdn_bchannel *bc, int originator)
  5361. {
  5362. int number_type;
  5363. if (originator == ORG_MISDN) {
  5364. /* ORIGINATOR MISDN (incoming call) */
  5365. ast_copy_string(bc->connected.name,
  5366. S_COR(ast->connected.id.name.valid, ast->connected.id.name.str, ""),
  5367. sizeof(bc->connected.name));
  5368. if (ast->connected.id.number.valid) {
  5369. ast_copy_string(bc->connected.number, S_OR(ast->connected.id.number.str, ""),
  5370. sizeof(bc->connected.number));
  5371. bc->connected.presentation = ast_to_misdn_pres(ast->connected.id.number.presentation);
  5372. bc->connected.screening = ast_to_misdn_screen(ast->connected.id.number.presentation);
  5373. bc->connected.number_type = ast_to_misdn_ton(ast->connected.id.number.plan);
  5374. bc->connected.number_plan = ast_to_misdn_plan(ast->connected.id.number.plan);
  5375. } else {
  5376. bc->connected.number[0] = '\0';
  5377. bc->connected.presentation = 0;/* Allowed */
  5378. bc->connected.screening = 0;/* Unscreened */
  5379. bc->connected.number_type = NUMTYPE_UNKNOWN;
  5380. bc->connected.number_plan = NUMPLAN_UNKNOWN;
  5381. }
  5382. misdn_cfg_get(bc->port, MISDN_CFG_CPNDIALPLAN, &number_type, sizeof(number_type));
  5383. if (0 <= number_type) {
  5384. /* Force us to send in CONNECT message */
  5385. bc->connected.number_type = number_type;
  5386. bc->connected.number_plan = NUMPLAN_ISDN;
  5387. }
  5388. debug_numtype(bc->port, bc->connected.number_type, "CTON");
  5389. } else {
  5390. /* ORIGINATOR Asterisk (outgoing call) */
  5391. ast_copy_string(bc->caller.name,
  5392. S_COR(ast->connected.id.name.valid, ast->connected.id.name.str, ""),
  5393. sizeof(bc->caller.name));
  5394. if (ast->connected.id.number.valid) {
  5395. ast_copy_string(bc->caller.number, S_OR(ast->connected.id.number.str, ""),
  5396. sizeof(bc->caller.number));
  5397. bc->caller.presentation = ast_to_misdn_pres(ast->connected.id.number.presentation);
  5398. bc->caller.screening = ast_to_misdn_screen(ast->connected.id.number.presentation);
  5399. bc->caller.number_type = ast_to_misdn_ton(ast->connected.id.number.plan);
  5400. bc->caller.number_plan = ast_to_misdn_plan(ast->connected.id.number.plan);
  5401. } else {
  5402. bc->caller.number[0] = '\0';
  5403. bc->caller.presentation = 0;/* Allowed */
  5404. bc->caller.screening = 0;/* Unscreened */
  5405. bc->caller.number_type = NUMTYPE_UNKNOWN;
  5406. bc->caller.number_plan = NUMPLAN_UNKNOWN;
  5407. }
  5408. misdn_cfg_get(bc->port, MISDN_CFG_LOCALDIALPLAN, &number_type, sizeof(number_type));
  5409. if (0 <= number_type) {
  5410. /* Force us to send in SETUP message */
  5411. bc->caller.number_type = number_type;
  5412. bc->caller.number_plan = NUMPLAN_ISDN;
  5413. }
  5414. debug_numtype(bc->port, bc->caller.number_type, "LTON");
  5415. }
  5416. }
  5417. /*!
  5418. * \internal
  5419. * \brief Notify peer that the connected line has changed.
  5420. *
  5421. * \param ast Current Asterisk channel
  5422. * \param bc Associated B channel
  5423. * \param originator Who originally created this channel. ORG_AST or ORG_MISDN
  5424. *
  5425. * \return Nothing
  5426. */
  5427. static void misdn_update_connected_line(struct ast_channel *ast, struct misdn_bchannel *bc, int originator)
  5428. {
  5429. struct chan_list *ch;
  5430. misdn_get_connected_line(ast, bc, originator);
  5431. if (originator == ORG_MISDN) {
  5432. bc->redirecting.to = bc->connected;
  5433. } else {
  5434. bc->redirecting.to = bc->caller;
  5435. }
  5436. switch (bc->outgoing_colp) {
  5437. case 1:/* restricted */
  5438. bc->redirecting.to.presentation = 1;/* restricted */
  5439. break;
  5440. case 2:/* blocked */
  5441. /* Don't tell the remote party that the call was transferred. */
  5442. return;
  5443. default:
  5444. break;
  5445. }
  5446. ch = MISDN_ASTERISK_TECH_PVT(ast);
  5447. if (ch->state == MISDN_CONNECTED
  5448. || originator != ORG_MISDN) {
  5449. int is_ptmp;
  5450. is_ptmp = !misdn_lib_is_ptp(bc->port);
  5451. if (is_ptmp) {
  5452. /*
  5453. * We should not send these messages to the network if we are
  5454. * the CPE side since phones do not transfer calls within
  5455. * themselves. Well... If you consider handing the handset to
  5456. * someone else a transfer then how is the network to know?
  5457. */
  5458. if (!misdn_lib_port_is_nt(bc->port)) {
  5459. return;
  5460. }
  5461. if (ch->state != MISDN_CONNECTED) {
  5462. /* Send NOTIFY(Nie(transfer-active), RDNie(redirecting.to data)) */
  5463. bc->redirecting.to_changed = 1;
  5464. bc->notify_description_code = mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE;
  5465. misdn_lib_send_event(bc, EVENT_NOTIFY);
  5466. #if defined(AST_MISDN_ENHANCEMENTS)
  5467. } else {
  5468. /* Send FACILITY(Fie(RequestSubaddress), Nie(transfer-active), RDNie(redirecting.to data)) */
  5469. bc->redirecting.to_changed = 1;
  5470. bc->notify_description_code = mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE;
  5471. bc->fac_out.Function = Fac_RequestSubaddress;
  5472. bc->fac_out.u.RequestSubaddress.InvokeID = ++misdn_invoke_id;
  5473. /* Send message */
  5474. print_facility(&bc->fac_out, bc);
  5475. misdn_lib_send_event(bc, EVENT_FACILITY);
  5476. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5477. }
  5478. #if defined(AST_MISDN_ENHANCEMENTS)
  5479. } else {
  5480. /* Send FACILITY(Fie(EctInform(transfer-active, redirecting.to data))) */
  5481. bc->fac_out.Function = Fac_EctInform;
  5482. bc->fac_out.u.EctInform.InvokeID = ++misdn_invoke_id;
  5483. bc->fac_out.u.EctInform.Status = 1;/* active */
  5484. bc->fac_out.u.EctInform.RedirectionPresent = 1;/* Must be present when status is active */
  5485. misdn_PresentedNumberUnscreened_fill(&bc->fac_out.u.EctInform.Redirection,
  5486. &bc->redirecting.to);
  5487. /* Send message */
  5488. print_facility(&bc->fac_out, bc);
  5489. misdn_lib_send_event(bc, EVENT_FACILITY);
  5490. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5491. }
  5492. }
  5493. }
  5494. /*!
  5495. * \internal
  5496. * \brief Copy the redirecting information out of the Asterisk channel
  5497. *
  5498. * \param bc Associated B channel
  5499. * \param ast Current Asterisk channel
  5500. *
  5501. * \return Nothing
  5502. */
  5503. static void misdn_copy_redirecting_from_ast(struct misdn_bchannel *bc, struct ast_channel *ast)
  5504. {
  5505. ast_copy_string(bc->redirecting.from.name,
  5506. S_COR(ast->redirecting.from.name.valid, ast->redirecting.from.name.str, ""),
  5507. sizeof(bc->redirecting.from.name));
  5508. if (ast->redirecting.from.number.valid) {
  5509. ast_copy_string(bc->redirecting.from.number, S_OR(ast->redirecting.from.number.str, ""),
  5510. sizeof(bc->redirecting.from.number));
  5511. bc->redirecting.from.presentation = ast_to_misdn_pres(ast->redirecting.from.number.presentation);
  5512. bc->redirecting.from.screening = ast_to_misdn_screen(ast->redirecting.from.number.presentation);
  5513. bc->redirecting.from.number_type = ast_to_misdn_ton(ast->redirecting.from.number.plan);
  5514. bc->redirecting.from.number_plan = ast_to_misdn_plan(ast->redirecting.from.number.plan);
  5515. } else {
  5516. bc->redirecting.from.number[0] = '\0';
  5517. bc->redirecting.from.presentation = 0;/* Allowed */
  5518. bc->redirecting.from.screening = 0;/* Unscreened */
  5519. bc->redirecting.from.number_type = NUMTYPE_UNKNOWN;
  5520. bc->redirecting.from.number_plan = NUMPLAN_UNKNOWN;
  5521. }
  5522. ast_copy_string(bc->redirecting.to.name,
  5523. S_COR(ast->redirecting.to.name.valid, ast->redirecting.to.name.str, ""),
  5524. sizeof(bc->redirecting.to.name));
  5525. if (ast->redirecting.to.number.valid) {
  5526. ast_copy_string(bc->redirecting.to.number, S_OR(ast->redirecting.to.number.str, ""),
  5527. sizeof(bc->redirecting.to.number));
  5528. bc->redirecting.to.presentation = ast_to_misdn_pres(ast->redirecting.to.number.presentation);
  5529. bc->redirecting.to.screening = ast_to_misdn_screen(ast->redirecting.to.number.presentation);
  5530. bc->redirecting.to.number_type = ast_to_misdn_ton(ast->redirecting.to.number.plan);
  5531. bc->redirecting.to.number_plan = ast_to_misdn_plan(ast->redirecting.to.number.plan);
  5532. } else {
  5533. bc->redirecting.to.number[0] = '\0';
  5534. bc->redirecting.to.presentation = 0;/* Allowed */
  5535. bc->redirecting.to.screening = 0;/* Unscreened */
  5536. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  5537. bc->redirecting.to.number_plan = NUMPLAN_UNKNOWN;
  5538. }
  5539. bc->redirecting.reason = ast_to_misdn_reason(ast->redirecting.reason);
  5540. bc->redirecting.count = ast->redirecting.count;
  5541. }
  5542. /*!
  5543. * \internal
  5544. * \brief Copy the redirecting info into the Asterisk channel
  5545. *
  5546. * \param ast Current Asterisk channel
  5547. * \param redirect Associated B channel redirecting info
  5548. * \param tag Caller ID tag to set in the redirecting party fields
  5549. *
  5550. * \return Nothing
  5551. */
  5552. static void misdn_copy_redirecting_to_ast(struct ast_channel *ast, const struct misdn_party_redirecting *redirect, char *tag)
  5553. {
  5554. struct ast_party_redirecting redirecting;
  5555. struct ast_set_party_redirecting update_redirecting;
  5556. ast_party_redirecting_set_init(&redirecting, &ast->redirecting);
  5557. memset(&update_redirecting, 0, sizeof(update_redirecting));
  5558. update_redirecting.from.number = 1;
  5559. redirecting.from.number.valid = 1;
  5560. redirecting.from.number.str = (char *) redirect->from.number;
  5561. redirecting.from.number.plan =
  5562. misdn_to_ast_ton(redirect->from.number_type)
  5563. | misdn_to_ast_plan(redirect->from.number_plan);
  5564. redirecting.from.number.presentation =
  5565. misdn_to_ast_pres(redirect->from.presentation)
  5566. | misdn_to_ast_screen(redirect->from.screening);
  5567. redirecting.from.tag = tag;
  5568. update_redirecting.to.number = 1;
  5569. redirecting.to.number.valid = 1;
  5570. redirecting.to.number.str = (char *) redirect->to.number;
  5571. redirecting.to.number.plan =
  5572. misdn_to_ast_ton(redirect->to.number_type)
  5573. | misdn_to_ast_plan(redirect->to.number_plan);
  5574. redirecting.to.number.presentation =
  5575. misdn_to_ast_pres(redirect->to.presentation)
  5576. | misdn_to_ast_screen(redirect->to.screening);
  5577. redirecting.to.tag = tag;
  5578. redirecting.reason = misdn_to_ast_reason(redirect->reason);
  5579. redirecting.count = redirect->count;
  5580. ast_channel_set_redirecting(ast, &redirecting, &update_redirecting);
  5581. }
  5582. /*!
  5583. * \internal
  5584. * \brief Notify peer that the redirecting information has changed.
  5585. *
  5586. * \param ast Current Asterisk channel
  5587. * \param bc Associated B channel
  5588. * \param originator Who originally created this channel. ORG_AST or ORG_MISDN
  5589. *
  5590. * \return Nothing
  5591. */
  5592. static void misdn_update_redirecting(struct ast_channel *ast, struct misdn_bchannel *bc, int originator)
  5593. {
  5594. int is_ptmp;
  5595. misdn_copy_redirecting_from_ast(bc, ast);
  5596. switch (bc->outgoing_colp) {
  5597. case 1:/* restricted */
  5598. bc->redirecting.to.presentation = 1;/* restricted */
  5599. break;
  5600. case 2:/* blocked */
  5601. /* Don't tell the remote party that the call was redirected. */
  5602. return;
  5603. default:
  5604. break;
  5605. }
  5606. if (originator != ORG_MISDN) {
  5607. return;
  5608. }
  5609. is_ptmp = !misdn_lib_is_ptp(bc->port);
  5610. if (is_ptmp) {
  5611. /*
  5612. * We should not send these messages to the network if we are
  5613. * the CPE side since phones do not redirect calls within
  5614. * themselves. Well... If you consider someone else picking up
  5615. * the handset a redirection then how is the network to know?
  5616. */
  5617. if (!misdn_lib_port_is_nt(bc->port)) {
  5618. return;
  5619. }
  5620. /* Send NOTIFY(call-is-diverting, redirecting.to data) */
  5621. bc->redirecting.to_changed = 1;
  5622. bc->notify_description_code = mISDN_NOTIFY_CODE_CALL_IS_DIVERTING;
  5623. misdn_lib_send_event(bc, EVENT_NOTIFY);
  5624. #if defined(AST_MISDN_ENHANCEMENTS)
  5625. } else {
  5626. int match; /* TRUE if the dialed number matches the redirecting to number */
  5627. match = (strcmp(ast->exten, bc->redirecting.to.number) == 0) ? 1 : 0;
  5628. if (!bc->div_leg_3_tx_pending
  5629. || !match) {
  5630. /* Send DivertingLegInformation1 */
  5631. bc->fac_out.Function = Fac_DivertingLegInformation1;
  5632. bc->fac_out.u.DivertingLegInformation1.InvokeID = ++misdn_invoke_id;
  5633. bc->fac_out.u.DivertingLegInformation1.DiversionReason =
  5634. misdn_to_diversion_reason(bc->redirecting.reason);
  5635. bc->fac_out.u.DivertingLegInformation1.SubscriptionOption = 2;/* notificationWithDivertedToNr */
  5636. bc->fac_out.u.DivertingLegInformation1.DivertedToPresent = 1;
  5637. misdn_PresentedNumberUnscreened_fill(&bc->fac_out.u.DivertingLegInformation1.DivertedTo, &bc->redirecting.to);
  5638. print_facility(&bc->fac_out, bc);
  5639. misdn_lib_send_event(bc, EVENT_FACILITY);
  5640. }
  5641. bc->div_leg_3_tx_pending = 0;
  5642. /* Send DivertingLegInformation3 */
  5643. bc->fac_out.Function = Fac_DivertingLegInformation3;
  5644. bc->fac_out.u.DivertingLegInformation3.InvokeID = ++misdn_invoke_id;
  5645. bc->fac_out.u.DivertingLegInformation3.PresentationAllowedIndicator =
  5646. bc->redirecting.to.presentation == 0 ? 1 : 0;
  5647. print_facility(&bc->fac_out, bc);
  5648. misdn_lib_send_event(bc, EVENT_FACILITY);
  5649. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5650. }
  5651. }
  5652. /*****************************/
  5653. /*** AST Indications Start ***/
  5654. /*****************************/
  5655. static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
  5656. {
  5657. int port = 0;
  5658. int r;
  5659. int exceed;
  5660. int number_type;
  5661. struct chan_list *ch;
  5662. struct misdn_bchannel *newbc;
  5663. char *dest_cp;
  5664. int append_msn = 0;
  5665. AST_DECLARE_APP_ARGS(args,
  5666. AST_APP_ARG(intf); /* The interface token is discarded. */
  5667. AST_APP_ARG(ext); /* extension token */
  5668. AST_APP_ARG(opts); /* options token */
  5669. );
  5670. if (!ast) {
  5671. ast_log(LOG_WARNING, " --> ! misdn_call called on ast_channel *ast where ast == NULL\n");
  5672. return -1;
  5673. }
  5674. if (((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) || !dest) {
  5675. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
  5676. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  5677. ast_setstate(ast, AST_STATE_DOWN);
  5678. return -1;
  5679. }
  5680. ch = MISDN_ASTERISK_TECH_PVT(ast);
  5681. if (!ch) {
  5682. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, chan_list *ch==NULL\n", ast->name);
  5683. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  5684. ast_setstate(ast, AST_STATE_DOWN);
  5685. return -1;
  5686. }
  5687. newbc = ch->bc;
  5688. if (!newbc) {
  5689. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, newbc==NULL\n", ast->name);
  5690. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  5691. ast_setstate(ast, AST_STATE_DOWN);
  5692. return -1;
  5693. }
  5694. port = newbc->port;
  5695. #if defined(AST_MISDN_ENHANCEMENTS)
  5696. if ((ch->peer = misdn_cc_caller_get(ast))) {
  5697. chan_misdn_log(3, port, " --> Found CC caller data, peer:%s\n",
  5698. ch->peer->chan ? "available" : "NULL");
  5699. }
  5700. if (ch->record_id != -1) {
  5701. struct misdn_cc_record *cc_record;
  5702. /* This is a call completion retry call */
  5703. AST_LIST_LOCK(&misdn_cc_records_db);
  5704. cc_record = misdn_cc_find_by_id(ch->record_id);
  5705. if (!cc_record) {
  5706. AST_LIST_UNLOCK(&misdn_cc_records_db);
  5707. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, cc_record==NULL\n", ast->name);
  5708. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  5709. ast_setstate(ast, AST_STATE_DOWN);
  5710. return -1;
  5711. }
  5712. /* Setup calling parameters to retry the call. */
  5713. newbc->dialed = cc_record->redial.dialed;
  5714. newbc->caller = cc_record->redial.caller;
  5715. memset(&newbc->redirecting, 0, sizeof(newbc->redirecting));
  5716. newbc->capability = cc_record->redial.capability;
  5717. newbc->hdlc = cc_record->redial.hdlc;
  5718. newbc->sending_complete = 1;
  5719. if (cc_record->ptp) {
  5720. newbc->fac_out.Function = Fac_CCBS_T_Call;
  5721. newbc->fac_out.u.CCBS_T_Call.InvokeID = ++misdn_invoke_id;
  5722. } else {
  5723. newbc->fac_out.Function = Fac_CCBSCall;
  5724. newbc->fac_out.u.CCBSCall.InvokeID = ++misdn_invoke_id;
  5725. newbc->fac_out.u.CCBSCall.CCBSReference = cc_record->mode.ptmp.reference_id;
  5726. }
  5727. AST_LIST_UNLOCK(&misdn_cc_records_db);
  5728. ast_copy_string(ast->exten, newbc->dialed.number, sizeof(ast->exten));
  5729. chan_misdn_log(1, port, "* Call completion to: %s\n", newbc->dialed.number);
  5730. chan_misdn_log(2, port, " --> * tech:%s context:%s\n", ast->name, ast->context);
  5731. } else
  5732. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5733. {
  5734. /*
  5735. * dest is ---v
  5736. * Dial(mISDN/g:group_name[/extension[/options]])
  5737. * Dial(mISDN/port[:preselected_channel][/extension[/options]])
  5738. *
  5739. * The dial extension could be empty if you are using MISDN_KEYPAD
  5740. * to control ISDN provider features.
  5741. */
  5742. dest_cp = ast_strdupa(dest);
  5743. AST_NONSTANDARD_APP_ARGS(args, dest_cp, '/');
  5744. if (!args.ext) {
  5745. args.ext = "";
  5746. }
  5747. chan_misdn_log(1, port, "* CALL: %s\n", dest);
  5748. chan_misdn_log(2, port, " --> * dialed:%s tech:%s context:%s\n", args.ext, ast->name, ast->context);
  5749. ast_copy_string(ast->exten, args.ext, sizeof(ast->exten));
  5750. ast_copy_string(newbc->dialed.number, args.ext, sizeof(newbc->dialed.number));
  5751. if (ast_strlen_zero(newbc->caller.name)
  5752. && ast->connected.id.name.valid
  5753. && !ast_strlen_zero(ast->connected.id.name.str)) {
  5754. ast_copy_string(newbc->caller.name, ast->connected.id.name.str, sizeof(newbc->caller.name));
  5755. chan_misdn_log(3, port, " --> * set caller:\"%s\" <%s>\n", newbc->caller.name, newbc->caller.number);
  5756. }
  5757. if (ast_strlen_zero(newbc->caller.number)
  5758. && ast->connected.id.number.valid
  5759. && !ast_strlen_zero(ast->connected.id.number.str)) {
  5760. ast_copy_string(newbc->caller.number, ast->connected.id.number.str, sizeof(newbc->caller.number));
  5761. chan_misdn_log(3, port, " --> * set caller:\"%s\" <%s>\n", newbc->caller.name, newbc->caller.number);
  5762. }
  5763. misdn_cfg_get(port, MISDN_CFG_APPEND_MSN_TO_CALLERID_TAG, &append_msn, sizeof(append_msn));
  5764. if (append_msn) {
  5765. strncat(newbc->incoming_cid_tag, "_", sizeof(newbc->incoming_cid_tag) - strlen(newbc->incoming_cid_tag) - 1);
  5766. strncat(newbc->incoming_cid_tag, newbc->caller.number, sizeof(newbc->incoming_cid_tag) - strlen(newbc->incoming_cid_tag) - 1);
  5767. }
  5768. ast->caller.id.tag = ast_strdup(newbc->incoming_cid_tag);
  5769. misdn_cfg_get(port, MISDN_CFG_LOCALDIALPLAN, &number_type, sizeof(number_type));
  5770. if (number_type < 0) {
  5771. if (ast->connected.id.number.valid) {
  5772. newbc->caller.number_type = ast_to_misdn_ton(ast->connected.id.number.plan);
  5773. newbc->caller.number_plan = ast_to_misdn_plan(ast->connected.id.number.plan);
  5774. } else {
  5775. newbc->caller.number_type = NUMTYPE_UNKNOWN;
  5776. newbc->caller.number_plan = NUMPLAN_ISDN;
  5777. }
  5778. } else {
  5779. /* Force us to send in SETUP message */
  5780. newbc->caller.number_type = number_type;
  5781. newbc->caller.number_plan = NUMPLAN_ISDN;
  5782. }
  5783. debug_numtype(port, newbc->caller.number_type, "LTON");
  5784. newbc->capability = ast->transfercapability;
  5785. pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY", ast_transfercapability2str(newbc->capability));
  5786. if (ast->transfercapability == INFO_CAPABILITY_DIGITAL_UNRESTRICTED) {
  5787. chan_misdn_log(2, port, " --> * Call with flag Digital\n");
  5788. }
  5789. /* update caller screening and presentation */
  5790. update_config(ch);
  5791. /* fill in some ies from channel dialplan variables */
  5792. import_ch(ast, newbc, ch);
  5793. /* Finally The Options Override Everything */
  5794. if (!ast_strlen_zero(args.opts)) {
  5795. misdn_set_opt_exec(ast, args.opts);
  5796. } else {
  5797. chan_misdn_log(2, port, "NO OPTS GIVEN\n");
  5798. }
  5799. if (newbc->set_presentation) {
  5800. newbc->caller.presentation = newbc->presentation;
  5801. }
  5802. misdn_copy_redirecting_from_ast(newbc, ast);
  5803. switch (newbc->outgoing_colp) {
  5804. case 1:/* restricted */
  5805. case 2:/* blocked */
  5806. newbc->redirecting.from.presentation = 1;/* restricted */
  5807. break;
  5808. default:
  5809. break;
  5810. }
  5811. #if defined(AST_MISDN_ENHANCEMENTS)
  5812. if (newbc->redirecting.from.number[0] && misdn_lib_is_ptp(port)) {
  5813. if (newbc->redirecting.count < 1) {
  5814. newbc->redirecting.count = 1;
  5815. }
  5816. /* Create DivertingLegInformation2 facility */
  5817. newbc->fac_out.Function = Fac_DivertingLegInformation2;
  5818. newbc->fac_out.u.DivertingLegInformation2.InvokeID = ++misdn_invoke_id;
  5819. newbc->fac_out.u.DivertingLegInformation2.DivertingPresent = 1;
  5820. misdn_PresentedNumberUnscreened_fill(
  5821. &newbc->fac_out.u.DivertingLegInformation2.Diverting,
  5822. &newbc->redirecting.from);
  5823. switch (newbc->outgoing_colp) {
  5824. case 2:/* blocked */
  5825. /* Block the number going out */
  5826. newbc->fac_out.u.DivertingLegInformation2.Diverting.Type = 1;/* presentationRestricted */
  5827. /* Don't tell about any previous diversions or why for that matter. */
  5828. newbc->fac_out.u.DivertingLegInformation2.DiversionCounter = 1;
  5829. newbc->fac_out.u.DivertingLegInformation2.DiversionReason = 0;/* unknown */
  5830. break;
  5831. default:
  5832. newbc->fac_out.u.DivertingLegInformation2.DiversionCounter =
  5833. newbc->redirecting.count;
  5834. newbc->fac_out.u.DivertingLegInformation2.DiversionReason =
  5835. misdn_to_diversion_reason(newbc->redirecting.reason);
  5836. break;
  5837. }
  5838. newbc->fac_out.u.DivertingLegInformation2.OriginalCalledPresent = 0;
  5839. if (1 < newbc->fac_out.u.DivertingLegInformation2.DiversionCounter) {
  5840. newbc->fac_out.u.DivertingLegInformation2.OriginalCalledPresent = 1;
  5841. newbc->fac_out.u.DivertingLegInformation2.OriginalCalled.Type = 2;/* numberNotAvailableDueToInterworking */
  5842. }
  5843. /*
  5844. * Expect a DivertingLegInformation3 to update the COLR of the
  5845. * redirecting-to party we are attempting to call now.
  5846. */
  5847. newbc->div_leg_3_rx_wanted = 1;
  5848. }
  5849. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5850. }
  5851. exceed = add_out_calls(port);
  5852. if (exceed != 0) {
  5853. char tmp[16];
  5854. snprintf(tmp, sizeof(tmp), "%d", exceed);
  5855. pbx_builtin_setvar_helper(ast, "MAX_OVERFLOW", tmp);
  5856. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  5857. ast_setstate(ast, AST_STATE_DOWN);
  5858. return -1;
  5859. }
  5860. #if defined(AST_MISDN_ENHANCEMENTS)
  5861. if (newbc->fac_out.Function != Fac_None) {
  5862. print_facility(&newbc->fac_out, newbc);
  5863. }
  5864. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5865. r = misdn_lib_send_event(newbc, EVENT_SETUP);
  5866. /** we should have l3id after sending setup **/
  5867. ch->l3id = newbc->l3_id;
  5868. if (r == -ENOCHAN) {
  5869. chan_misdn_log(0, port, " --> * Theres no Channel at the moment .. !\n");
  5870. chan_misdn_log(1, port, " --> * SEND: State Down pid:%d\n", newbc ? newbc->pid : -1);
  5871. ast->hangupcause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
  5872. ast_setstate(ast, AST_STATE_DOWN);
  5873. return -1;
  5874. }
  5875. chan_misdn_log(2, port, " --> * SEND: State Dialing pid:%d\n", newbc ? newbc->pid : 1);
  5876. ast_setstate(ast, AST_STATE_DIALING);
  5877. ast->hangupcause = AST_CAUSE_NORMAL_CLEARING;
  5878. if (newbc->nt) {
  5879. stop_bc_tones(ch);
  5880. }
  5881. ch->state = MISDN_CALLING;
  5882. return 0;
  5883. }
  5884. static int misdn_answer(struct ast_channel *ast)
  5885. {
  5886. struct chan_list *p;
  5887. const char *tmp;
  5888. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  5889. return -1;
  5890. }
  5891. chan_misdn_log(1, p ? (p->bc ? p->bc->port : 0) : 0, "* ANSWER:\n");
  5892. if (!p) {
  5893. ast_log(LOG_WARNING, " --> Channel not connected ??\n");
  5894. ast_queue_hangup_with_cause(ast, AST_CAUSE_NETWORK_OUT_OF_ORDER);
  5895. }
  5896. if (!p->bc) {
  5897. chan_misdn_log(1, 0, " --> Got Answer, but there is no bc obj ??\n");
  5898. ast_queue_hangup_with_cause(ast, AST_CAUSE_PROTOCOL_ERROR);
  5899. }
  5900. ast_channel_lock(ast);
  5901. tmp = pbx_builtin_getvar_helper(ast, "CRYPT_KEY");
  5902. if (!ast_strlen_zero(tmp)) {
  5903. chan_misdn_log(1, p->bc->port, " --> Connection will be BF crypted\n");
  5904. ast_copy_string(p->bc->crypt_key, tmp, sizeof(p->bc->crypt_key));
  5905. } else {
  5906. chan_misdn_log(3, p->bc->port, " --> Connection is without BF encryption\n");
  5907. }
  5908. tmp = pbx_builtin_getvar_helper(ast, "MISDN_DIGITAL_TRANS");
  5909. if (!ast_strlen_zero(tmp) && ast_true(tmp)) {
  5910. chan_misdn_log(1, p->bc->port, " --> Connection is transparent digital\n");
  5911. p->bc->nodsp = 1;
  5912. p->bc->hdlc = 0;
  5913. p->bc->nojitter = 1;
  5914. }
  5915. ast_channel_unlock(ast);
  5916. p->state = MISDN_CONNECTED;
  5917. stop_indicate(p);
  5918. if (ast_strlen_zero(p->bc->connected.number)) {
  5919. chan_misdn_log(2,p->bc->port," --> empty connected number using dialed number\n");
  5920. ast_copy_string(p->bc->connected.number, p->bc->dialed.number, sizeof(p->bc->connected.number));
  5921. /*
  5922. * Use the misdn_set_opt() application to set the presentation
  5923. * before we answer or you can use the CONECTEDLINE() function
  5924. * to set everything before using the Answer() application.
  5925. */
  5926. p->bc->connected.presentation = p->bc->presentation;
  5927. p->bc->connected.screening = 0; /* unscreened */
  5928. p->bc->connected.number_type = p->bc->dialed.number_type;
  5929. p->bc->connected.number_plan = p->bc->dialed.number_plan;
  5930. }
  5931. switch (p->bc->outgoing_colp) {
  5932. case 1:/* restricted */
  5933. case 2:/* blocked */
  5934. p->bc->connected.presentation = 1;/* restricted */
  5935. break;
  5936. default:
  5937. break;
  5938. }
  5939. #if defined(AST_MISDN_ENHANCEMENTS)
  5940. if (p->bc->div_leg_3_tx_pending) {
  5941. p->bc->div_leg_3_tx_pending = 0;
  5942. /* Send DivertingLegInformation3 */
  5943. p->bc->fac_out.Function = Fac_DivertingLegInformation3;
  5944. p->bc->fac_out.u.DivertingLegInformation3.InvokeID = ++misdn_invoke_id;
  5945. p->bc->fac_out.u.DivertingLegInformation3.PresentationAllowedIndicator =
  5946. (p->bc->connected.presentation == 0) ? 1 : 0;
  5947. print_facility(&p->bc->fac_out, p->bc);
  5948. }
  5949. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5950. misdn_lib_send_event(p->bc, EVENT_CONNECT);
  5951. start_bc_tones(p);
  5952. return 0;
  5953. }
  5954. static int misdn_digit_begin(struct ast_channel *chan, char digit)
  5955. {
  5956. /* XXX Modify this callback to support Asterisk controlling the length of DTMF */
  5957. return 0;
  5958. }
  5959. static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  5960. {
  5961. struct chan_list *p;
  5962. struct misdn_bchannel *bc;
  5963. char buf[2] = { digit, 0 };
  5964. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  5965. return -1;
  5966. }
  5967. bc = p->bc;
  5968. chan_misdn_log(1, bc ? bc->port : 0, "* IND : Digit %c\n", digit);
  5969. if (!bc) {
  5970. ast_log(LOG_WARNING, " --> !! Got Digit Event without having bchannel Object\n");
  5971. return -1;
  5972. }
  5973. switch (p->state) {
  5974. case MISDN_CALLING:
  5975. if (strlen(bc->infos_pending) < sizeof(bc->infos_pending) - 1) {
  5976. strncat(bc->infos_pending, buf, sizeof(bc->infos_pending) - strlen(bc->infos_pending) - 1);
  5977. }
  5978. break;
  5979. case MISDN_CALLING_ACKNOWLEDGE:
  5980. ast_copy_string(bc->info_dad, buf, sizeof(bc->info_dad));
  5981. if (strlen(bc->dialed.number) < sizeof(bc->dialed.number) - 1) {
  5982. strncat(bc->dialed.number, buf, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  5983. }
  5984. ast_copy_string(p->ast->exten, bc->dialed.number, sizeof(p->ast->exten));
  5985. misdn_lib_send_event(bc, EVENT_INFORMATION);
  5986. break;
  5987. default:
  5988. if (bc->send_dtmf) {
  5989. send_digit_to_chan(p, digit);
  5990. }
  5991. break;
  5992. }
  5993. return 0;
  5994. }
  5995. static int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast)
  5996. {
  5997. struct chan_list *p;
  5998. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  5999. return -1;
  6000. }
  6001. 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);
  6002. p->ast = ast;
  6003. return 0;
  6004. }
  6005. static int misdn_indication(struct ast_channel *ast, int cond, const void *data, size_t datalen)
  6006. {
  6007. struct chan_list *p;
  6008. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  6009. ast_log(LOG_WARNING, "Returned -1 in misdn_indication\n");
  6010. return -1;
  6011. }
  6012. if (!p->bc) {
  6013. if (p->hold.state == MISDN_HOLD_IDLE) {
  6014. chan_misdn_log(1, 0, "* IND : Indication [%d] ignored on %s\n", cond,
  6015. ast->name);
  6016. ast_log(LOG_WARNING, "Private Pointer but no bc ?\n");
  6017. } else {
  6018. chan_misdn_log(1, 0, "* IND : Indication [%d] ignored on hold %s\n",
  6019. cond, ast->name);
  6020. }
  6021. return -1;
  6022. }
  6023. chan_misdn_log(5, p->bc->port, "* IND : Indication [%d] on %s\n", cond, ast->name);
  6024. switch (cond) {
  6025. case AST_CONTROL_BUSY:
  6026. chan_misdn_log(1, p->bc->port, "* IND :\tbusy pid:%d\n", p->bc->pid);
  6027. ast_setstate(ast, AST_STATE_BUSY);
  6028. p->bc->out_cause = AST_CAUSE_USER_BUSY;
  6029. if (p->state != MISDN_CONNECTED) {
  6030. start_bc_tones(p);
  6031. misdn_lib_send_event(p->bc, EVENT_DISCONNECT);
  6032. }
  6033. return -1;
  6034. case AST_CONTROL_RING:
  6035. chan_misdn_log(1, p->bc->port, "* IND :\tring pid:%d\n", p->bc->pid);
  6036. return -1;
  6037. case AST_CONTROL_RINGING:
  6038. chan_misdn_log(1, p->bc->port, "* IND :\tringing pid:%d\n", p->bc->pid);
  6039. switch (p->state) {
  6040. case MISDN_ALERTING:
  6041. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but I was Ringing before, so ignoring it\n", p->bc->pid);
  6042. break;
  6043. case MISDN_CONNECTED:
  6044. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but Connected, so just send TONE_ALERTING without state changes \n", p->bc->pid);
  6045. return -1;
  6046. default:
  6047. p->state = MISDN_ALERTING;
  6048. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d\n", p->bc->pid);
  6049. misdn_lib_send_event(p->bc, EVENT_ALERTING);
  6050. chan_misdn_log(3, p->bc->port, " --> * SEND: State Ring pid:%d\n", p->bc->pid);
  6051. ast_setstate(ast, AST_STATE_RING);
  6052. if (!p->bc->nt && (p->originator == ORG_MISDN) && !p->incoming_early_audio) {
  6053. chan_misdn_log(2, p->bc->port, " --> incoming_early_audio off\n");
  6054. } else {
  6055. return -1;
  6056. }
  6057. }
  6058. break;
  6059. case AST_CONTROL_ANSWER:
  6060. chan_misdn_log(1, p->bc->port, " --> * IND :\tanswer pid:%d\n", p->bc->pid);
  6061. start_bc_tones(p);
  6062. break;
  6063. case AST_CONTROL_TAKEOFFHOOK:
  6064. chan_misdn_log(1, p->bc->port, " --> *\ttakeoffhook pid:%d\n", p->bc->pid);
  6065. return -1;
  6066. case AST_CONTROL_OFFHOOK:
  6067. chan_misdn_log(1, p->bc->port, " --> *\toffhook pid:%d\n", p->bc->pid);
  6068. return -1;
  6069. case AST_CONTROL_FLASH:
  6070. chan_misdn_log(1, p->bc->port, " --> *\tflash pid:%d\n", p->bc->pid);
  6071. break;
  6072. case AST_CONTROL_PROGRESS:
  6073. chan_misdn_log(1, p->bc->port, " --> * IND :\tprogress pid:%d\n", p->bc->pid);
  6074. misdn_lib_send_event(p->bc, EVENT_PROGRESS);
  6075. break;
  6076. case AST_CONTROL_PROCEEDING:
  6077. chan_misdn_log(1, p->bc->port, " --> * IND :\tproceeding pid:%d\n", p->bc->pid);
  6078. misdn_lib_send_event(p->bc, EVENT_PROCEEDING);
  6079. break;
  6080. case AST_CONTROL_INCOMPLETE:
  6081. chan_misdn_log(1, p->bc->port, " --> *\tincomplete pid:%d\n", p->bc->pid);
  6082. if (!p->overlap_dial) {
  6083. /* Overlapped dialing not enabled - send hangup */
  6084. p->bc->out_cause = AST_CAUSE_INVALID_NUMBER_FORMAT;
  6085. start_bc_tones(p);
  6086. misdn_lib_send_event(p->bc, EVENT_DISCONNECT);
  6087. if (p->bc->nt) {
  6088. hanguptone_indicate(p);
  6089. }
  6090. }
  6091. break;
  6092. case AST_CONTROL_CONGESTION:
  6093. chan_misdn_log(1, p->bc->port, " --> * IND :\tcongestion pid:%d\n", p->bc->pid);
  6094. p->bc->out_cause = AST_CAUSE_SWITCH_CONGESTION;
  6095. start_bc_tones(p);
  6096. misdn_lib_send_event(p->bc, EVENT_DISCONNECT);
  6097. if (p->bc->nt) {
  6098. hanguptone_indicate(p);
  6099. }
  6100. break;
  6101. case -1 :
  6102. chan_misdn_log(1, p->bc->port, " --> * IND :\t-1! (stop indication) pid:%d\n", p->bc->pid);
  6103. stop_indicate(p);
  6104. if (p->state == MISDN_CONNECTED) {
  6105. start_bc_tones(p);
  6106. }
  6107. break;
  6108. case AST_CONTROL_HOLD:
  6109. ast_moh_start(ast, data, p->mohinterpret);
  6110. chan_misdn_log(1, p->bc->port, " --> *\tHOLD pid:%d\n", p->bc->pid);
  6111. break;
  6112. case AST_CONTROL_UNHOLD:
  6113. ast_moh_stop(ast);
  6114. chan_misdn_log(1, p->bc->port, " --> *\tUNHOLD pid:%d\n", p->bc->pid);
  6115. break;
  6116. case AST_CONTROL_CONNECTED_LINE:
  6117. chan_misdn_log(1, p->bc->port, "* IND :\tconnected line update pid:%d\n", p->bc->pid);
  6118. misdn_update_connected_line(ast, p->bc, p->originator);
  6119. break;
  6120. case AST_CONTROL_REDIRECTING:
  6121. chan_misdn_log(1, p->bc->port, "* IND :\tredirecting info update pid:%d\n", p->bc->pid);
  6122. misdn_update_redirecting(ast, p->bc, p->originator);
  6123. break;
  6124. default:
  6125. chan_misdn_log(1, p->bc->port, " --> * Unknown Indication:%d pid:%d\n", cond, p->bc->pid);
  6126. return -1;
  6127. }
  6128. return 0;
  6129. }
  6130. static int misdn_hangup(struct ast_channel *ast)
  6131. {
  6132. struct chan_list *p;
  6133. struct misdn_bchannel *bc;
  6134. const char *var;
  6135. if (!ast) {
  6136. return -1;
  6137. }
  6138. ast_debug(1, "misdn_hangup(%s)\n", ast->name);
  6139. /* Take the ast_channel's tech_pvt reference. */
  6140. ast_mutex_lock(&release_lock);
  6141. p = MISDN_ASTERISK_TECH_PVT(ast);
  6142. if (!p) {
  6143. ast_mutex_unlock(&release_lock);
  6144. return -1;
  6145. }
  6146. MISDN_ASTERISK_TECH_PVT(ast) = NULL;
  6147. if (!misdn_chan_is_valid(p)) {
  6148. ast_mutex_unlock(&release_lock);
  6149. chan_list_unref(p, "Release ast_channel reference. Was not active?");
  6150. return 0;
  6151. }
  6152. if (p->hold.state == MISDN_HOLD_IDLE) {
  6153. bc = p->bc;
  6154. } else {
  6155. p->hold.state = MISDN_HOLD_DISCONNECT;
  6156. bc = misdn_lib_find_held_bc(p->hold.port, p->l3id);
  6157. if (!bc) {
  6158. chan_misdn_log(4, p->hold.port,
  6159. "misdn_hangup: Could not find held bc for (%s)\n", ast->name);
  6160. release_chan_early(p);
  6161. ast_mutex_unlock(&release_lock);
  6162. chan_list_unref(p, "Release ast_channel reference");
  6163. return 0;
  6164. }
  6165. }
  6166. if (ast->_state == AST_STATE_RESERVED || p->state == MISDN_NOTHING) {
  6167. /* between request and call */
  6168. ast_debug(1, "State Reserved (or nothing) => chanIsAvail\n");
  6169. release_chan_early(p);
  6170. if (bc) {
  6171. misdn_lib_release(bc);
  6172. }
  6173. ast_mutex_unlock(&release_lock);
  6174. chan_list_unref(p, "Release ast_channel reference");
  6175. return 0;
  6176. }
  6177. if (!bc) {
  6178. ast_log(LOG_WARNING, "Hangup with private but no bc ? state:%s l3id:%x\n",
  6179. misdn_get_ch_state(p), p->l3id);
  6180. release_chan_early(p);
  6181. ast_mutex_unlock(&release_lock);
  6182. chan_list_unref(p, "Release ast_channel reference");
  6183. return 0;
  6184. }
  6185. p->ast = NULL;
  6186. p->need_hangup = 0;
  6187. p->need_queue_hangup = 0;
  6188. p->need_busy = 0;
  6189. if (!bc->nt) {
  6190. stop_bc_tones(p);
  6191. }
  6192. bc->out_cause = ast->hangupcause ? ast->hangupcause : AST_CAUSE_NORMAL_CLEARING;
  6193. /* Channel lock is already held when we are called. */
  6194. //ast_channel_lock(ast);
  6195. var = pbx_builtin_getvar_helper(ast, "HANGUPCAUSE");
  6196. if (!var) {
  6197. var = pbx_builtin_getvar_helper(ast, "PRI_CAUSE");
  6198. }
  6199. if (var) {
  6200. int tmpcause;
  6201. tmpcause = atoi(var);
  6202. bc->out_cause = tmpcause ? tmpcause : AST_CAUSE_NORMAL_CLEARING;
  6203. }
  6204. var = pbx_builtin_getvar_helper(ast, "MISDN_USERUSER");
  6205. if (var) {
  6206. ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", var);
  6207. ast_copy_string(bc->uu, var, sizeof(bc->uu));
  6208. bc->uulen = strlen(bc->uu);
  6209. }
  6210. //ast_channel_unlock(ast);
  6211. chan_misdn_log(1, bc->port,
  6212. "* IND : HANGUP\tpid:%d context:%s dialed:%s caller:\"%s\" <%s> State:%s\n",
  6213. bc->pid,
  6214. ast->context,
  6215. ast->exten,
  6216. (ast->caller.id.name.valid && ast->caller.id.name.str)
  6217. ? ast->caller.id.name.str : "",
  6218. (ast->caller.id.number.valid && ast->caller.id.number.str)
  6219. ? ast->caller.id.number.str : "",
  6220. misdn_get_ch_state(p));
  6221. chan_misdn_log(3, bc->port, " --> l3id:%x\n", p->l3id);
  6222. chan_misdn_log(3, bc->port, " --> cause:%d\n", bc->cause);
  6223. chan_misdn_log(2, bc->port, " --> out_cause:%d\n", bc->out_cause);
  6224. switch (p->state) {
  6225. case MISDN_INCOMING_SETUP:
  6226. /*
  6227. * This is the only place in misdn_hangup, where we
  6228. * can call release_chan, else it might create a lot of trouble.
  6229. */
  6230. ast_log(LOG_NOTICE, "release channel, in INCOMING_SETUP state.. no other events happened\n");
  6231. release_chan(p, bc);
  6232. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  6233. ast_mutex_unlock(&release_lock);
  6234. chan_list_unref(p, "Release ast_channel reference");
  6235. return 0;
  6236. case MISDN_DIALING:
  6237. if (p->hold.state == MISDN_HOLD_IDLE) {
  6238. start_bc_tones(p);
  6239. hanguptone_indicate(p);
  6240. }
  6241. if (bc->need_disconnect) {
  6242. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6243. }
  6244. break;
  6245. case MISDN_CALLING_ACKNOWLEDGE:
  6246. if (p->hold.state == MISDN_HOLD_IDLE) {
  6247. start_bc_tones(p);
  6248. hanguptone_indicate(p);
  6249. }
  6250. if (bc->need_disconnect) {
  6251. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6252. }
  6253. break;
  6254. case MISDN_CALLING:
  6255. case MISDN_ALERTING:
  6256. case MISDN_PROGRESS:
  6257. case MISDN_PROCEEDING:
  6258. if (p->originator != ORG_AST && p->hold.state == MISDN_HOLD_IDLE) {
  6259. hanguptone_indicate(p);
  6260. }
  6261. if (bc->need_disconnect) {
  6262. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6263. }
  6264. break;
  6265. case MISDN_CONNECTED:
  6266. /* Alerting or Disconnect */
  6267. if (bc->nt && p->hold.state == MISDN_HOLD_IDLE) {
  6268. start_bc_tones(p);
  6269. hanguptone_indicate(p);
  6270. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  6271. }
  6272. if (bc->need_disconnect) {
  6273. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6274. }
  6275. break;
  6276. case MISDN_DISCONNECTED:
  6277. if (bc->need_release) {
  6278. misdn_lib_send_event(bc, EVENT_RELEASE);
  6279. }
  6280. break;
  6281. case MISDN_CLEANING:
  6282. ast_mutex_unlock(&release_lock);
  6283. chan_list_unref(p, "Release ast_channel reference");
  6284. return 0;
  6285. case MISDN_BUSY:
  6286. break;
  6287. default:
  6288. if (bc->nt) {
  6289. bc->out_cause = -1;
  6290. if (bc->need_release) {
  6291. misdn_lib_send_event(bc, EVENT_RELEASE);
  6292. }
  6293. } else {
  6294. if (bc->need_disconnect) {
  6295. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6296. }
  6297. }
  6298. break;
  6299. }
  6300. p->state = MISDN_CLEANING;
  6301. chan_misdn_log(3, bc->port, " --> Channel: %s hungup new state:%s\n", ast->name,
  6302. misdn_get_ch_state(p));
  6303. ast_mutex_unlock(&release_lock);
  6304. chan_list_unref(p, "Release ast_channel reference");
  6305. return 0;
  6306. }
  6307. static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame)
  6308. {
  6309. struct ast_frame *f;
  6310. if (tmp->dsp) {
  6311. f = ast_dsp_process(tmp->ast, tmp->dsp, frame);
  6312. } else {
  6313. chan_misdn_log(0, tmp->bc->port, "No DSP-Path found\n");
  6314. return NULL;
  6315. }
  6316. if (!f || (f->frametype != AST_FRAME_DTMF)) {
  6317. return f;
  6318. }
  6319. ast_debug(1, "Detected inband DTMF digit: %c\n", f->subclass.integer);
  6320. if (tmp->faxdetect && (f->subclass.integer == 'f')) {
  6321. /* Fax tone -- Handle and return NULL */
  6322. if (!tmp->faxhandled) {
  6323. struct ast_channel *ast = tmp->ast;
  6324. tmp->faxhandled++;
  6325. chan_misdn_log(0, tmp->bc->port, "Fax detected, preparing %s for fax transfer.\n", ast->name);
  6326. tmp->bc->rxgain = 0;
  6327. isdn_lib_update_rxgain(tmp->bc);
  6328. tmp->bc->txgain = 0;
  6329. isdn_lib_update_txgain(tmp->bc);
  6330. #ifdef MISDN_1_2
  6331. *tmp->bc->pipeline = 0;
  6332. #else
  6333. tmp->bc->ec_enable = 0;
  6334. #endif
  6335. isdn_lib_update_ec(tmp->bc);
  6336. isdn_lib_stop_dtmf(tmp->bc);
  6337. switch (tmp->faxdetect) {
  6338. case 1:
  6339. if (strcmp(ast->exten, "fax")) {
  6340. char *context;
  6341. char context_tmp[BUFFERSIZE];
  6342. misdn_cfg_get(tmp->bc->port, MISDN_CFG_FAXDETECT_CONTEXT, &context_tmp, sizeof(context_tmp));
  6343. context = ast_strlen_zero(context_tmp) ? (ast_strlen_zero(ast->macrocontext) ? ast->context : ast->macrocontext) : context_tmp;
  6344. if (ast_exists_extension(ast, context, "fax", 1,
  6345. S_COR(ast->caller.id.number.valid, ast->caller.id.number.str, NULL))) {
  6346. ast_verb(3, "Redirecting %s to fax extension (context:%s)\n", ast->name, context);
  6347. /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
  6348. pbx_builtin_setvar_helper(ast,"FAXEXTEN",ast->exten);
  6349. if (ast_async_goto(ast, context, "fax", 1)) {
  6350. ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, context);
  6351. }
  6352. } else {
  6353. ast_log(LOG_NOTICE, "Fax detected but no fax extension, context:%s exten:%s\n", context, ast->exten);
  6354. }
  6355. } else {
  6356. ast_debug(1, "Already in a fax extension, not redirecting\n");
  6357. }
  6358. break;
  6359. case 2:
  6360. ast_verb(3, "Not redirecting %s to fax extension, nojump is set.\n", ast->name);
  6361. break;
  6362. default:
  6363. break;
  6364. }
  6365. } else {
  6366. ast_debug(1, "Fax already handled\n");
  6367. }
  6368. }
  6369. if (tmp->ast_dsp && (f->subclass.integer != 'f')) {
  6370. chan_misdn_log(2, tmp->bc->port, " --> * SEND: DTMF (AST_DSP) :%c\n", f->subclass.integer);
  6371. }
  6372. return f;
  6373. }
  6374. static struct ast_frame *misdn_read(struct ast_channel *ast)
  6375. {
  6376. struct chan_list *tmp;
  6377. int len, t;
  6378. struct pollfd pfd = { .fd = -1, .events = POLLIN };
  6379. if (!ast) {
  6380. chan_misdn_log(1, 0, "misdn_read called without ast\n");
  6381. return NULL;
  6382. }
  6383. if (!(tmp = MISDN_ASTERISK_TECH_PVT(ast))) {
  6384. chan_misdn_log(1, 0, "misdn_read called without ast->pvt\n");
  6385. return NULL;
  6386. }
  6387. if (!tmp->bc && tmp->hold.state == MISDN_HOLD_IDLE) {
  6388. chan_misdn_log(1, 0, "misdn_read called without bc\n");
  6389. return NULL;
  6390. }
  6391. pfd.fd = tmp->pipe[0];
  6392. t = ast_poll(&pfd, 1, 20);
  6393. if (t < 0) {
  6394. chan_misdn_log(-1, tmp->bc->port, "poll() error (err=%s)\n", strerror(errno));
  6395. return NULL;
  6396. }
  6397. if (!t) {
  6398. chan_misdn_log(3, tmp->bc->port, "poll() timed out\n");
  6399. len = 160;
  6400. } else if (pfd.revents & POLLIN) {
  6401. len = read(tmp->pipe[0], tmp->ast_rd_buf, sizeof(tmp->ast_rd_buf));
  6402. if (len <= 0) {
  6403. /* we hangup here, since our pipe is closed */
  6404. chan_misdn_log(2, tmp->bc->port, "misdn_read: Pipe closed, hanging up\n");
  6405. return NULL;
  6406. }
  6407. } else {
  6408. return NULL;
  6409. }
  6410. tmp->frame.frametype = AST_FRAME_VOICE;
  6411. tmp->frame.subclass.codec = AST_FORMAT_ALAW;
  6412. tmp->frame.datalen = len;
  6413. tmp->frame.samples = len;
  6414. tmp->frame.mallocd = 0;
  6415. tmp->frame.offset = 0;
  6416. tmp->frame.delivery = ast_tv(0, 0);
  6417. tmp->frame.src = NULL;
  6418. tmp->frame.data.ptr = tmp->ast_rd_buf;
  6419. if (tmp->faxdetect && !tmp->faxhandled) {
  6420. if (tmp->faxdetect_timeout) {
  6421. if (ast_tvzero(tmp->faxdetect_tv)) {
  6422. tmp->faxdetect_tv = ast_tvnow();
  6423. chan_misdn_log(2, tmp->bc->port, "faxdetect: starting detection with timeout: %ds ...\n", tmp->faxdetect_timeout);
  6424. return process_ast_dsp(tmp, &tmp->frame);
  6425. } else {
  6426. struct timeval tv_now = ast_tvnow();
  6427. int diff = ast_tvdiff_ms(tv_now, tmp->faxdetect_tv);
  6428. if (diff <= (tmp->faxdetect_timeout * 1000)) {
  6429. chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ...\n");
  6430. return process_ast_dsp(tmp, &tmp->frame);
  6431. } else {
  6432. chan_misdn_log(2, tmp->bc->port, "faxdetect: stopping detection (time ran out) ...\n");
  6433. tmp->faxdetect = 0;
  6434. return &tmp->frame;
  6435. }
  6436. }
  6437. } else {
  6438. chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ... (no timeout)\n");
  6439. return process_ast_dsp(tmp, &tmp->frame);
  6440. }
  6441. } else {
  6442. if (tmp->ast_dsp) {
  6443. return process_ast_dsp(tmp, &tmp->frame);
  6444. } else {
  6445. return &tmp->frame;
  6446. }
  6447. }
  6448. }
  6449. static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
  6450. {
  6451. struct chan_list *ch;
  6452. if (!ast || !(ch = MISDN_ASTERISK_TECH_PVT(ast))) {
  6453. return -1;
  6454. }
  6455. if (ch->hold.state != MISDN_HOLD_IDLE) {
  6456. chan_misdn_log(7, 0, "misdn_write: Returning because hold active\n");
  6457. return 0;
  6458. }
  6459. if (!ch->bc) {
  6460. ast_log(LOG_WARNING, "private but no bc\n");
  6461. return -1;
  6462. }
  6463. if (ch->notxtone) {
  6464. chan_misdn_log(7, ch->bc->port, "misdn_write: Returning because notxtone\n");
  6465. return 0;
  6466. }
  6467. if (!frame->subclass.codec) {
  6468. chan_misdn_log(4, ch->bc->port, "misdn_write: * prods us\n");
  6469. return 0;
  6470. }
  6471. if (!(frame->subclass.codec & prefformat)) {
  6472. chan_misdn_log(-1, ch->bc->port, "Got Unsupported Frame with Format:%s\n", ast_getformatname(frame->subclass.codec));
  6473. return 0;
  6474. }
  6475. if (!frame->samples) {
  6476. chan_misdn_log(4, ch->bc->port, "misdn_write: zero write\n");
  6477. if (!strcmp(frame->src,"ast_prod")) {
  6478. chan_misdn_log(1, ch->bc->port, "misdn_write: state (%s) prodded.\n", misdn_get_ch_state(ch));
  6479. if (ch->ts) {
  6480. chan_misdn_log(4, ch->bc->port, "Starting Playtones\n");
  6481. misdn_lib_tone_generator_start(ch->bc);
  6482. }
  6483. return 0;
  6484. }
  6485. return -1;
  6486. }
  6487. if (!ch->bc->addr) {
  6488. chan_misdn_log(8, ch->bc->port, "misdn_write: no addr for bc dropping:%d\n", frame->samples);
  6489. return 0;
  6490. }
  6491. #ifdef MISDN_DEBUG
  6492. {
  6493. int i;
  6494. int max = 5 > frame->samples ? frame->samples : 5;
  6495. ast_debug(1, "write2mISDN %p %d bytes: ", p, frame->samples);
  6496. for (i = 0; i < max; i++) {
  6497. ast_debug(1, "%2.2x ", ((char *) frame->data.ptr)[i]);
  6498. }
  6499. }
  6500. #endif
  6501. switch (ch->bc->bc_state) {
  6502. case BCHAN_ACTIVATED:
  6503. case BCHAN_BRIDGED:
  6504. break;
  6505. default:
  6506. if (!ch->dropped_frame_cnt) {
  6507. chan_misdn_log(5, ch->bc->port,
  6508. "BC not active (nor bridged) dropping: %d frames addr:%x exten:%s cid:%s ch->state:%s bc_state:%d l3id:%x\n",
  6509. frame->samples, ch->bc->addr, ast->exten,
  6510. S_COR(ast->caller.id.number.valid, ast->caller.id.number.str, ""),
  6511. misdn_get_ch_state(ch), ch->bc->bc_state, ch->bc->l3_id);
  6512. }
  6513. if (++ch->dropped_frame_cnt > 100) {
  6514. ch->dropped_frame_cnt = 0;
  6515. 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);
  6516. }
  6517. return 0;
  6518. }
  6519. chan_misdn_log(9, ch->bc->port, "Sending :%d bytes to MISDN\n", frame->samples);
  6520. if (!ch->bc->nojitter && misdn_cap_is_speech(ch->bc->capability)) {
  6521. /* Buffered Transmit (triggered by read from isdn side)*/
  6522. if (misdn_jb_fill(ch->jb, frame->data.ptr, frame->samples) < 0) {
  6523. if (ch->bc->active) {
  6524. cb_log(0, ch->bc->port, "Misdn Jitterbuffer Overflow.\n");
  6525. }
  6526. }
  6527. } else {
  6528. /* transmit without jitterbuffer */
  6529. misdn_lib_tx2misdn_frm(ch->bc, frame->data.ptr, frame->samples);
  6530. }
  6531. return 0;
  6532. }
  6533. static enum ast_bridge_result misdn_bridge(struct ast_channel *c0,
  6534. struct ast_channel *c1, int flags,
  6535. struct ast_frame **fo,
  6536. struct ast_channel **rc,
  6537. int timeoutms)
  6538. {
  6539. struct chan_list *ch1, *ch2;
  6540. struct ast_channel *carr[2], *who;
  6541. int to = -1;
  6542. struct ast_frame *f;
  6543. int p1_b, p2_b;
  6544. int bridging;
  6545. ch1 = get_chan_by_ast(c0);
  6546. if (!ch1) {
  6547. return -1;
  6548. }
  6549. ch2 = get_chan_by_ast(c1);
  6550. if (!ch2) {
  6551. chan_list_unref(ch1, "Failed to find ch2");
  6552. return -1;
  6553. }
  6554. carr[0] = c0;
  6555. carr[1] = c1;
  6556. misdn_cfg_get(ch1->bc->port, MISDN_CFG_BRIDGING, &p1_b, sizeof(p1_b));
  6557. misdn_cfg_get(ch2->bc->port, MISDN_CFG_BRIDGING, &p2_b, sizeof(p2_b));
  6558. if (! p1_b || ! p2_b) {
  6559. ast_log(LOG_NOTICE, "Falling back to Asterisk bridging\n");
  6560. chan_list_unref(ch1, "Bridge fallback ch1");
  6561. chan_list_unref(ch2, "Bridge fallback ch2");
  6562. return AST_BRIDGE_FAILED;
  6563. }
  6564. misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(bridging));
  6565. if (bridging) {
  6566. /* trying to make a mISDN_dsp conference */
  6567. chan_misdn_log(1, ch1->bc->port, "I SEND: Making conference with Number:%d\n", ch1->bc->pid + 1);
  6568. misdn_lib_bridge(ch1->bc, ch2->bc);
  6569. }
  6570. ast_verb(3, "Native bridging %s and %s\n", c0->name, c1->name);
  6571. chan_misdn_log(1, ch1->bc->port, "* Making Native Bridge between \"%s\" <%s> and \"%s\" <%s>\n",
  6572. ch1->bc->caller.name,
  6573. ch1->bc->caller.number,
  6574. ch2->bc->caller.name,
  6575. ch2->bc->caller.number);
  6576. if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
  6577. ch1->ignore_dtmf = 1;
  6578. }
  6579. if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1)) {
  6580. ch2->ignore_dtmf = 1;
  6581. }
  6582. for (;/*ever*/;) {
  6583. to = -1;
  6584. who = ast_waitfor_n(carr, 2, &to);
  6585. if (!who) {
  6586. ast_log(LOG_NOTICE, "misdn_bridge: empty read, breaking out\n");
  6587. break;
  6588. }
  6589. f = ast_read(who);
  6590. if (!f || f->frametype == AST_FRAME_CONTROL) {
  6591. /* got hangup .. */
  6592. if (!f) {
  6593. chan_misdn_log(4, ch1->bc->port, "Read Null Frame\n");
  6594. } else {
  6595. chan_misdn_log(4, ch1->bc->port, "Read Frame Control class:%d\n", f->subclass.integer);
  6596. }
  6597. *fo = f;
  6598. *rc = who;
  6599. break;
  6600. }
  6601. if (f->frametype == AST_FRAME_DTMF) {
  6602. chan_misdn_log(1, 0, "Read DTMF %d from %s\n", f->subclass.integer, who->exten);
  6603. *fo = f;
  6604. *rc = who;
  6605. break;
  6606. }
  6607. #if 0
  6608. if (f->frametype == AST_FRAME_VOICE) {
  6609. chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid +1);
  6610. continue;
  6611. }
  6612. #endif
  6613. ast_write((who == c0) ? c1 : c0, f);
  6614. }
  6615. chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid + 1);
  6616. misdn_lib_split_bridge(ch1->bc, ch2->bc);
  6617. chan_list_unref(ch1, "Bridge complete ch1");
  6618. chan_list_unref(ch2, "Bridge complete ch2");
  6619. return AST_BRIDGE_COMPLETE;
  6620. }
  6621. /** AST INDICATIONS END **/
  6622. static int dialtone_indicate(struct chan_list *cl)
  6623. {
  6624. struct ast_channel *ast = cl->ast;
  6625. int nd = 0;
  6626. if (!ast) {
  6627. chan_misdn_log(0, cl->bc->port, "No Ast in dialtone_indicate\n");
  6628. return -1;
  6629. }
  6630. misdn_cfg_get(cl->bc->port, MISDN_CFG_NODIALTONE, &nd, sizeof(nd));
  6631. if (nd) {
  6632. chan_misdn_log(1, cl->bc->port, "Not sending Dialtone, because config wants it\n");
  6633. return 0;
  6634. }
  6635. chan_misdn_log(3, cl->bc->port, " --> Dial\n");
  6636. cl->ts = ast_get_indication_tone(ast->zone, "dial");
  6637. if (cl->ts) {
  6638. cl->notxtone = 0;
  6639. cl->norxtone = 0;
  6640. /* This prods us in misdn_write */
  6641. ast_playtones_start(ast, 0, cl->ts->data, 0);
  6642. }
  6643. return 0;
  6644. }
  6645. static void hanguptone_indicate(struct chan_list *cl)
  6646. {
  6647. misdn_lib_send_tone(cl->bc, TONE_HANGUP);
  6648. }
  6649. static int stop_indicate(struct chan_list *cl)
  6650. {
  6651. struct ast_channel *ast = cl->ast;
  6652. if (!ast) {
  6653. chan_misdn_log(0, cl->bc->port, "No Ast in stop_indicate\n");
  6654. return -1;
  6655. }
  6656. chan_misdn_log(3, cl->bc->port, " --> None\n");
  6657. misdn_lib_tone_generator_stop(cl->bc);
  6658. ast_playtones_stop(ast);
  6659. if (cl->ts) {
  6660. cl->ts = ast_tone_zone_sound_unref(cl->ts);
  6661. }
  6662. return 0;
  6663. }
  6664. static int start_bc_tones(struct chan_list* cl)
  6665. {
  6666. misdn_lib_tone_generator_stop(cl->bc);
  6667. cl->notxtone = 0;
  6668. cl->norxtone = 0;
  6669. return 0;
  6670. }
  6671. static int stop_bc_tones(struct chan_list *cl)
  6672. {
  6673. if (!cl) {
  6674. return -1;
  6675. }
  6676. cl->notxtone = 1;
  6677. cl->norxtone = 1;
  6678. return 0;
  6679. }
  6680. /*!
  6681. * \internal
  6682. * \brief Destroy the chan_list object.
  6683. *
  6684. * \param obj chan_list object to destroy.
  6685. *
  6686. * \return Nothing
  6687. */
  6688. static void chan_list_destructor(void *obj)
  6689. {
  6690. struct chan_list *ch = obj;
  6691. #if defined(AST_MISDN_ENHANCEMENTS)
  6692. if (ch->peer) {
  6693. ao2_ref(ch->peer, -1);
  6694. ch->peer = NULL;
  6695. }
  6696. #endif /* AST_MISDN_ENHANCEMENTS */
  6697. if (ch->dsp) {
  6698. ast_dsp_free(ch->dsp);
  6699. ch->dsp = NULL;
  6700. }
  6701. /* releasing jitterbuffer */
  6702. if (ch->jb) {
  6703. misdn_jb_destroy(ch->jb);
  6704. ch->jb = NULL;
  6705. }
  6706. if (ch->overlap_dial) {
  6707. if (ch->overlap_dial_task != -1) {
  6708. misdn_tasks_remove(ch->overlap_dial_task);
  6709. ch->overlap_dial_task = -1;
  6710. }
  6711. ast_mutex_destroy(&ch->overlap_tv_lock);
  6712. }
  6713. if (-1 < ch->pipe[0]) {
  6714. close(ch->pipe[0]);
  6715. }
  6716. if (-1 < ch->pipe[1]) {
  6717. close(ch->pipe[1]);
  6718. }
  6719. }
  6720. /*! Returns a reference to the new chan_list. */
  6721. static struct chan_list *chan_list_init(int orig)
  6722. {
  6723. struct chan_list *cl;
  6724. cl = ao2_alloc(sizeof(*cl), chan_list_destructor);
  6725. if (!cl) {
  6726. chan_misdn_log(-1, 0, "misdn_request: malloc failed!");
  6727. return NULL;
  6728. }
  6729. cl->originator = orig;
  6730. cl->need_queue_hangup = 1;
  6731. cl->need_hangup = 1;
  6732. cl->need_busy = 1;
  6733. cl->overlap_dial_task = -1;
  6734. #if defined(AST_MISDN_ENHANCEMENTS)
  6735. cl->record_id = -1;
  6736. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6737. cl->pipe[0] = -1;
  6738. cl->pipe[1] = -1;
  6739. return cl;
  6740. }
  6741. static struct ast_channel *misdn_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
  6742. {
  6743. struct ast_channel *ast;
  6744. char group[BUFFERSIZE + 1] = "";
  6745. char dial_str[128];
  6746. char *dest_cp;
  6747. char *p = NULL;
  6748. int channel = 0;
  6749. int port = 0;
  6750. struct misdn_bchannel *newbc = NULL;
  6751. int dec = 0;
  6752. #if defined(AST_MISDN_ENHANCEMENTS)
  6753. int cc_retry_call = 0; /* TRUE if this is a call completion retry call */
  6754. long record_id = -1;
  6755. struct misdn_cc_record *cc_record;
  6756. const char *err_msg;
  6757. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6758. struct chan_list *cl;
  6759. AST_DECLARE_APP_ARGS(args,
  6760. AST_APP_ARG(intf); /* interface token */
  6761. AST_APP_ARG(ext); /* extension token */
  6762. AST_APP_ARG(opts); /* options token */
  6763. );
  6764. snprintf(dial_str, sizeof(dial_str), "%s/%s", misdn_type, (char *) data);
  6765. /*
  6766. * data is ---v
  6767. * Dial(mISDN/g:group_name[/extension[/options]])
  6768. * Dial(mISDN/port[:preselected_channel][/extension[/options]])
  6769. * Dial(mISDN/cc/cc-record-id)
  6770. *
  6771. * The dial extension could be empty if you are using MISDN_KEYPAD
  6772. * to control ISDN provider features.
  6773. */
  6774. dest_cp = ast_strdupa(data);
  6775. AST_NONSTANDARD_APP_ARGS(args, dest_cp, '/');
  6776. if (!args.ext) {
  6777. args.ext = "";
  6778. }
  6779. if (!ast_strlen_zero(args.intf)) {
  6780. if (args.intf[0] == 'g' && args.intf[1] == ':') {
  6781. /* We make a group call lets checkout which ports are in my group */
  6782. args.intf += 2;
  6783. ast_copy_string(group, args.intf, sizeof(group));
  6784. chan_misdn_log(2, 0, " --> Group Call group: %s\n", group);
  6785. #if defined(AST_MISDN_ENHANCEMENTS)
  6786. } else if (strcmp(args.intf, "cc") == 0) {
  6787. cc_retry_call = 1;
  6788. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6789. } else if ((p = strchr(args.intf, ':'))) {
  6790. /* we have a preselected channel */
  6791. *p++ = 0;
  6792. channel = atoi(p);
  6793. port = atoi(args.intf);
  6794. chan_misdn_log(2, port, " --> Call on preselected Channel (%d).\n", channel);
  6795. } else {
  6796. port = atoi(args.intf);
  6797. }
  6798. } else {
  6799. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) WITHOUT Port or Group, check extensions.conf\n", dial_str);
  6800. return NULL;
  6801. }
  6802. #if defined(AST_MISDN_ENHANCEMENTS)
  6803. if (cc_retry_call) {
  6804. if (ast_strlen_zero(args.ext)) {
  6805. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) WITHOUT cc-record-id, check extensions.conf\n", dial_str);
  6806. return NULL;
  6807. }
  6808. if (!isdigit(*args.ext)) {
  6809. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) cc-record-id must be a number.\n", dial_str);
  6810. return NULL;
  6811. }
  6812. record_id = atol(args.ext);
  6813. AST_LIST_LOCK(&misdn_cc_records_db);
  6814. cc_record = misdn_cc_find_by_id(record_id);
  6815. if (!cc_record) {
  6816. AST_LIST_UNLOCK(&misdn_cc_records_db);
  6817. err_msg = misdn_cc_record_not_found;
  6818. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) %s.\n", dial_str, err_msg);
  6819. return NULL;
  6820. }
  6821. if (!cc_record->activated) {
  6822. AST_LIST_UNLOCK(&misdn_cc_records_db);
  6823. err_msg = "Call completion has not been activated";
  6824. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) %s.\n", dial_str, err_msg);
  6825. return NULL;
  6826. }
  6827. port = cc_record->port;
  6828. AST_LIST_UNLOCK(&misdn_cc_records_db);
  6829. }
  6830. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6831. if (misdn_cfg_is_group_method(group, METHOD_STANDARD_DEC)) {
  6832. chan_misdn_log(4, port, " --> STARTING STANDARD DEC...\n");
  6833. dec = 1;
  6834. }
  6835. if (!ast_strlen_zero(group)) {
  6836. char cfg_group[BUFFERSIZE + 1];
  6837. struct robin_list *rr = NULL;
  6838. /* Group dial */
  6839. if (misdn_cfg_is_group_method(group, METHOD_ROUND_ROBIN)) {
  6840. chan_misdn_log(4, port, " --> STARTING ROUND ROBIN...\n");
  6841. rr = get_robin_position(group);
  6842. }
  6843. if (rr) {
  6844. int port_start;
  6845. int bchan_start;
  6846. int port_up;
  6847. int check;
  6848. int maxbchans;
  6849. int wraped = 0;
  6850. if (!rr->port) {
  6851. rr->port = misdn_cfg_get_next_port_spin(0);
  6852. }
  6853. if (!rr->channel) {
  6854. rr->channel = 1;
  6855. }
  6856. bchan_start = rr->channel;
  6857. port_start = rr->port;
  6858. do {
  6859. misdn_cfg_get(rr->port, MISDN_CFG_GROUPNAME, cfg_group, sizeof(cfg_group));
  6860. if (strcasecmp(cfg_group, group)) {
  6861. wraped = 1;
  6862. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6863. rr->channel = 1;
  6864. continue;
  6865. }
  6866. misdn_cfg_get(rr->port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(check));
  6867. port_up = misdn_lib_port_up(rr->port, check);
  6868. if (!port_up) {
  6869. chan_misdn_log(1, rr->port, "L1 is not Up on this Port\n");
  6870. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6871. rr->channel = 1;
  6872. } else if (port_up < 0) {
  6873. ast_log(LOG_WARNING, "This port (%d) is blocked\n", rr->port);
  6874. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6875. rr->channel = 1;
  6876. } else {
  6877. chan_misdn_log(4, rr->port, "portup\n");
  6878. maxbchans = misdn_lib_get_maxchans(rr->port);
  6879. for (;rr->channel <= maxbchans;rr->channel++) {
  6880. /* ive come full circle and can stop now */
  6881. if (wraped && (rr->port == port_start) && (rr->channel == bchan_start)) {
  6882. break;
  6883. }
  6884. chan_misdn_log(4, rr->port, "Checking channel %d\n", rr->channel);
  6885. if ((newbc = misdn_lib_get_free_bc(rr->port, rr->channel, 0, 0))) {
  6886. chan_misdn_log(4, rr->port, " Success! Found port:%d channel:%d\n", newbc->port, newbc->channel);
  6887. rr->channel++;
  6888. break;
  6889. }
  6890. }
  6891. if (wraped && (rr->port == port_start) && (rr->channel <= bchan_start)) {
  6892. break;
  6893. } else if (!newbc || (rr->channel == maxbchans)) {
  6894. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6895. rr->channel = 1;
  6896. }
  6897. }
  6898. wraped = 1;
  6899. } while (!newbc && (rr->port > 0));
  6900. } else {
  6901. for (port = misdn_cfg_get_next_port(0); port > 0;
  6902. port = misdn_cfg_get_next_port(port)) {
  6903. misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, sizeof(cfg_group));
  6904. chan_misdn_log(3, port, "Group [%s] Port [%d]\n", group, port);
  6905. if (!strcasecmp(cfg_group, group)) {
  6906. int port_up;
  6907. int check;
  6908. misdn_cfg_get(port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(check));
  6909. port_up = misdn_lib_port_up(port, check);
  6910. chan_misdn_log(4, port, "portup:%d\n", port_up);
  6911. if (port_up > 0) {
  6912. newbc = misdn_lib_get_free_bc(port, 0, 0, dec);
  6913. if (newbc) {
  6914. break;
  6915. }
  6916. }
  6917. }
  6918. }
  6919. }
  6920. /* Group dial failed ?*/
  6921. if (!newbc) {
  6922. ast_log(LOG_WARNING,
  6923. "Could not Dial out on group '%s'.\n"
  6924. "\tEither the L2 and L1 on all of these ports where DOWN (see 'show application misdn_check_l2l1')\n"
  6925. "\tOr there was no free channel on none of the ports\n\n",
  6926. group);
  6927. return NULL;
  6928. }
  6929. } else {
  6930. /* 'Normal' Port dial * Port dial */
  6931. if (channel) {
  6932. chan_misdn_log(1, port, " --> preselected_channel: %d\n", channel);
  6933. }
  6934. newbc = misdn_lib_get_free_bc(port, channel, 0, dec);
  6935. if (!newbc) {
  6936. ast_log(LOG_WARNING, "Could not create channel on port:%d for Dial(%s)\n", port, dial_str);
  6937. return NULL;
  6938. }
  6939. }
  6940. /* create ast_channel and link all the objects together */
  6941. cl = chan_list_init(ORG_AST);
  6942. if (!cl) {
  6943. misdn_lib_release(newbc);
  6944. ast_log(LOG_ERROR, "Could not create call record for Dial(%s)\n", dial_str);
  6945. return NULL;
  6946. }
  6947. cl->bc = newbc;
  6948. ast = misdn_new(cl, AST_STATE_RESERVED, args.ext, NULL, format, requestor ? requestor->linkedid : NULL, port, channel);
  6949. if (!ast) {
  6950. chan_list_unref(cl, "Failed to create a new channel");
  6951. misdn_lib_release(newbc);
  6952. ast_log(LOG_ERROR, "Could not create Asterisk channel for Dial(%s)\n", dial_str);
  6953. return NULL;
  6954. }
  6955. #if defined(AST_MISDN_ENHANCEMENTS)
  6956. cl->record_id = record_id;
  6957. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6958. /* register chan in local list */
  6959. cl_queue_chan(cl);
  6960. /* fill in the config into the objects */
  6961. read_config(cl);
  6962. /* important */
  6963. cl->need_hangup = 0;
  6964. chan_list_unref(cl, "Successful misdn_request()");
  6965. return ast;
  6966. }
  6967. static int misdn_send_text(struct ast_channel *chan, const char *text)
  6968. {
  6969. struct chan_list *tmp = MISDN_ASTERISK_TECH_PVT(chan);
  6970. if (tmp && tmp->bc) {
  6971. ast_copy_string(tmp->bc->display, text, sizeof(tmp->bc->display));
  6972. misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
  6973. } else {
  6974. ast_log(LOG_WARNING, "No chan_list but send_text request?\n");
  6975. return -1;
  6976. }
  6977. return 0;
  6978. }
  6979. static struct ast_channel_tech misdn_tech = {
  6980. .type = misdn_type,
  6981. .description = "Channel driver for mISDN Support (Bri/Pri)",
  6982. .capabilities = AST_FORMAT_ALAW ,
  6983. .requester = misdn_request,
  6984. .send_digit_begin = misdn_digit_begin,
  6985. .send_digit_end = misdn_digit_end,
  6986. .call = misdn_call,
  6987. .bridge = misdn_bridge,
  6988. .hangup = misdn_hangup,
  6989. .answer = misdn_answer,
  6990. .read = misdn_read,
  6991. .write = misdn_write,
  6992. .indicate = misdn_indication,
  6993. .fixup = misdn_fixup,
  6994. .send_text = misdn_send_text,
  6995. .properties = 0,
  6996. };
  6997. static struct ast_channel_tech misdn_tech_wo_bridge = {
  6998. .type = misdn_type,
  6999. .description = "Channel driver for mISDN Support (Bri/Pri)",
  7000. .capabilities = AST_FORMAT_ALAW ,
  7001. .requester = misdn_request,
  7002. .send_digit_begin = misdn_digit_begin,
  7003. .send_digit_end = misdn_digit_end,
  7004. .call = misdn_call,
  7005. .hangup = misdn_hangup,
  7006. .answer = misdn_answer,
  7007. .read = misdn_read,
  7008. .write = misdn_write,
  7009. .indicate = misdn_indication,
  7010. .fixup = misdn_fixup,
  7011. .send_text = misdn_send_text,
  7012. .properties = 0,
  7013. };
  7014. static int glob_channel = 0;
  7015. static void update_name(struct ast_channel *tmp, int port, int c)
  7016. {
  7017. int chan_offset = 0;
  7018. int tmp_port = misdn_cfg_get_next_port(0);
  7019. char newname[255];
  7020. for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
  7021. if (tmp_port == port) {
  7022. break;
  7023. }
  7024. chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
  7025. }
  7026. if (c < 0) {
  7027. c = 0;
  7028. }
  7029. snprintf(newname, sizeof(newname), "%s/%d-", misdn_type, chan_offset + c);
  7030. if (strncmp(tmp->name, newname, strlen(newname))) {
  7031. snprintf(newname, sizeof(newname), "%s/%d-u%d", misdn_type, chan_offset + c, glob_channel++);
  7032. ast_change_name(tmp, newname);
  7033. chan_misdn_log(3, port, " --> updating channel name to [%s]\n", tmp->name);
  7034. }
  7035. }
  7036. static struct ast_channel *misdn_new(struct chan_list *chlist, int state, char *exten, char *callerid, int format, const char *linkedid, int port, int c)
  7037. {
  7038. struct ast_channel *tmp;
  7039. char *cid_name = NULL;
  7040. char *cid_num = NULL;
  7041. int chan_offset = 0;
  7042. int tmp_port = misdn_cfg_get_next_port(0);
  7043. int bridging;
  7044. for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
  7045. if (tmp_port == port) {
  7046. break;
  7047. }
  7048. chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
  7049. }
  7050. if (c < 0) {
  7051. c = 0;
  7052. }
  7053. if (callerid) {
  7054. ast_callerid_parse(callerid, &cid_name, &cid_num);
  7055. }
  7056. tmp = ast_channel_alloc(1, state, cid_num, cid_name, "", exten, "", linkedid, 0, "%s/%s%d-u%d", misdn_type, c ? "" : "tmp", chan_offset + c, glob_channel++);
  7057. if (tmp) {
  7058. chan_misdn_log(2, port, " --> * NEW CHANNEL dialed:%s caller:%s\n", exten, callerid);
  7059. tmp->nativeformats = prefformat;
  7060. tmp->readformat = format;
  7061. tmp->rawreadformat = format;
  7062. tmp->writeformat = format;
  7063. tmp->rawwriteformat = format;
  7064. /* Link the channel and private together */
  7065. chan_list_ref(chlist, "Give a reference to ast_channel");
  7066. MISDN_ASTERISK_TECH_PVT(tmp) = chlist;
  7067. chlist->ast = tmp;
  7068. misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(bridging));
  7069. tmp->tech = bridging ? &misdn_tech : &misdn_tech_wo_bridge;
  7070. tmp->writeformat = format;
  7071. tmp->readformat = format;
  7072. tmp->priority = 1;
  7073. if (exten) {
  7074. ast_copy_string(tmp->exten, exten, sizeof(tmp->exten));
  7075. } else {
  7076. chan_misdn_log(1, 0, "misdn_new: no exten given.\n");
  7077. }
  7078. if (!ast_strlen_zero(cid_num)) {
  7079. /* Don't use ast_set_callerid() here because it will
  7080. * generate a needless NewCallerID event */
  7081. tmp->caller.ani.number.valid = 1;
  7082. tmp->caller.ani.number.str = ast_strdup(cid_num);
  7083. }
  7084. if (pipe(chlist->pipe) < 0) {
  7085. ast_log(LOG_ERROR, "Pipe failed\n");
  7086. }
  7087. ast_channel_set_fd(tmp, 0, chlist->pipe[0]);
  7088. tmp->rings = (state == AST_STATE_RING) ? 1 : 0;
  7089. ast_jb_configure(tmp, misdn_get_global_jbconf());
  7090. } else {
  7091. chan_misdn_log(-1, 0, "Unable to allocate channel structure\n");
  7092. }
  7093. return tmp;
  7094. }
  7095. /*! Returns a reference to the found chan_list. */
  7096. static struct chan_list *find_chan_by_bc(struct misdn_bchannel *bc)
  7097. {
  7098. struct chan_list *help;
  7099. ast_mutex_lock(&cl_te_lock);
  7100. for (help = cl_te; help; help = help->next) {
  7101. if (help->bc == bc) {
  7102. chan_list_ref(help, "Found chan_list by bc");
  7103. ast_mutex_unlock(&cl_te_lock);
  7104. return help;
  7105. }
  7106. }
  7107. ast_mutex_unlock(&cl_te_lock);
  7108. chan_misdn_log(6, bc->port,
  7109. "$$$ find_chan_by_bc: No channel found for dialed:%s caller:\"%s\" <%s>\n",
  7110. bc->dialed.number,
  7111. bc->caller.name,
  7112. bc->caller.number);
  7113. return NULL;
  7114. }
  7115. /*! Returns a reference to the found chan_list. */
  7116. static struct chan_list *find_hold_call(struct misdn_bchannel *bc)
  7117. {
  7118. struct chan_list *help;
  7119. if (bc->pri) {
  7120. return NULL;
  7121. }
  7122. chan_misdn_log(6, bc->port, "$$$ find_hold_call: channel:%d dialed:%s caller:\"%s\" <%s>\n",
  7123. bc->channel,
  7124. bc->dialed.number,
  7125. bc->caller.name,
  7126. bc->caller.number);
  7127. ast_mutex_lock(&cl_te_lock);
  7128. for (help = cl_te; help; help = help->next) {
  7129. chan_misdn_log(4, bc->port, "$$$ find_hold_call: --> hold:%d channel:%d\n", help->hold.state, help->hold.channel);
  7130. if (help->hold.state == MISDN_HOLD_ACTIVE && help->hold.port == bc->port) {
  7131. chan_list_ref(help, "Found chan_list hold call");
  7132. ast_mutex_unlock(&cl_te_lock);
  7133. return help;
  7134. }
  7135. }
  7136. ast_mutex_unlock(&cl_te_lock);
  7137. chan_misdn_log(6, bc->port,
  7138. "$$$ find_hold_call: No channel found for dialed:%s caller:\"%s\" <%s>\n",
  7139. bc->dialed.number,
  7140. bc->caller.name,
  7141. bc->caller.number);
  7142. return NULL;
  7143. }
  7144. /*! Returns a reference to the found chan_list. */
  7145. static struct chan_list *find_hold_call_l3(unsigned long l3_id)
  7146. {
  7147. struct chan_list *help;
  7148. ast_mutex_lock(&cl_te_lock);
  7149. for (help = cl_te; help; help = help->next) {
  7150. if (help->hold.state != MISDN_HOLD_IDLE && help->l3id == l3_id) {
  7151. chan_list_ref(help, "Found chan_list hold call l3");
  7152. ast_mutex_unlock(&cl_te_lock);
  7153. return help;
  7154. }
  7155. }
  7156. ast_mutex_unlock(&cl_te_lock);
  7157. return NULL;
  7158. }
  7159. #define TRANSFER_ON_HELD_CALL_HANGUP 1
  7160. #if defined(TRANSFER_ON_HELD_CALL_HANGUP)
  7161. /*!
  7162. * \internal
  7163. * \brief Find a suitable active call to go with a held call so we could try a transfer.
  7164. *
  7165. * \param bc B channel record.
  7166. *
  7167. * \return Found call record or NULL.
  7168. *
  7169. * \note Returns a reference to the found chan_list.
  7170. *
  7171. * \note There could be a possibility where we find the wrong active call to transfer.
  7172. * This concern is mitigated by the fact that there could be at most one other call
  7173. * on a PTMP BRI link to another device. Maybe the l3_id could help in locating an
  7174. * active call on the same TEI?
  7175. */
  7176. static struct chan_list *find_hold_active_call(struct misdn_bchannel *bc)
  7177. {
  7178. struct chan_list *list;
  7179. ast_mutex_lock(&cl_te_lock);
  7180. for (list = cl_te; list; list = list->next) {
  7181. if (list->hold.state == MISDN_HOLD_IDLE && list->bc && list->bc->port == bc->port
  7182. && list->ast) {
  7183. switch (list->state) {
  7184. case MISDN_PROCEEDING:
  7185. case MISDN_PROGRESS:
  7186. case MISDN_ALERTING:
  7187. case MISDN_CONNECTED:
  7188. chan_list_ref(list, "Found chan_list hold active call");
  7189. ast_mutex_unlock(&cl_te_lock);
  7190. return list;
  7191. default:
  7192. break;
  7193. }
  7194. }
  7195. }
  7196. ast_mutex_unlock(&cl_te_lock);
  7197. return NULL;
  7198. }
  7199. #endif /* defined(TRANSFER_ON_HELD_CALL_HANGUP) */
  7200. static void cl_queue_chan(struct chan_list *chan)
  7201. {
  7202. chan_misdn_log(4, chan->bc ? chan->bc->port : 0, "* Queuing chan %p\n", chan);
  7203. chan_list_ref(chan, "Adding chan_list to list");
  7204. ast_mutex_lock(&cl_te_lock);
  7205. chan->next = NULL;
  7206. if (!cl_te) {
  7207. /* List is empty, make head of list. */
  7208. cl_te = chan;
  7209. } else {
  7210. struct chan_list *help;
  7211. /* Put at end of list. */
  7212. for (help = cl_te; help->next; help = help->next) {
  7213. }
  7214. help->next = chan;
  7215. }
  7216. ast_mutex_unlock(&cl_te_lock);
  7217. }
  7218. static int cl_dequeue_chan(struct chan_list *chan)
  7219. {
  7220. int found_it;
  7221. struct chan_list *help;
  7222. ast_mutex_lock(&cl_te_lock);
  7223. if (!cl_te) {
  7224. /* List is empty. */
  7225. ast_mutex_unlock(&cl_te_lock);
  7226. return 0;
  7227. }
  7228. if (cl_te == chan) {
  7229. /* What we want is the head of the list. */
  7230. cl_te = cl_te->next;
  7231. ast_mutex_unlock(&cl_te_lock);
  7232. chan_list_unref(chan, "Removed chan_list from list head");
  7233. return 1;
  7234. }
  7235. found_it = 0;
  7236. for (help = cl_te; help->next; help = help->next) {
  7237. if (help->next == chan) {
  7238. /* Found it in the list. */
  7239. help->next = help->next->next;
  7240. found_it = 1;
  7241. break;
  7242. }
  7243. }
  7244. ast_mutex_unlock(&cl_te_lock);
  7245. if (found_it) {
  7246. chan_list_unref(chan, "Removed chan_list from list");
  7247. }
  7248. return found_it;
  7249. }
  7250. /** Channel Queue End **/
  7251. static int pbx_start_chan(struct chan_list *ch)
  7252. {
  7253. int ret = ast_pbx_start(ch->ast);
  7254. ch->need_hangup = (ret >= 0) ? 0 : 1;
  7255. return ret;
  7256. }
  7257. static void hangup_chan(struct chan_list *ch, struct misdn_bchannel *bc)
  7258. {
  7259. int port = bc->port;
  7260. if (!ch) {
  7261. cb_log(1, port, "Cannot hangup chan, no ch\n");
  7262. return;
  7263. }
  7264. cb_log(5, port, "hangup_chan called\n");
  7265. if (ch->need_hangup) {
  7266. cb_log(2, port, " --> hangup\n");
  7267. ch->need_hangup = 0;
  7268. ch->need_queue_hangup = 0;
  7269. if (ch->ast && send_cause2ast(ch->ast, bc, ch)) {
  7270. ast_hangup(ch->ast);
  7271. }
  7272. return;
  7273. }
  7274. if (!ch->need_queue_hangup) {
  7275. cb_log(2, port, " --> No need to queue hangup\n");
  7276. return;
  7277. }
  7278. ch->need_queue_hangup = 0;
  7279. if (ch->ast) {
  7280. if (send_cause2ast(ch->ast, bc, ch)) {
  7281. ast_queue_hangup_with_cause(ch->ast, bc->cause);
  7282. cb_log(2, port, " --> queue_hangup\n");
  7283. }
  7284. } else {
  7285. cb_log(1, port, "Cannot hangup chan, no ast\n");
  7286. }
  7287. }
  7288. /*!
  7289. * \internal
  7290. * \brief ISDN asked us to release channel, pendant to misdn_hangup.
  7291. *
  7292. * \param ch Call channel record to release.
  7293. * \param bc Current B channel record associated with ch.
  7294. *
  7295. * \return Nothing
  7296. *
  7297. * \note The only valid thing to do with ch after calling is to chan_list_unref(ch, "").
  7298. */
  7299. static void release_chan(struct chan_list *ch, struct misdn_bchannel *bc)
  7300. {
  7301. struct ast_channel *ast;
  7302. chan_misdn_log(5, bc->port, "release_chan: bc with pid:%d l3id: %x\n", bc->pid, bc->l3_id);
  7303. ast_mutex_lock(&release_lock);
  7304. for (;;) {
  7305. ast = ch->ast;
  7306. if (!ast || !ast_channel_trylock(ast)) {
  7307. break;
  7308. }
  7309. DEADLOCK_AVOIDANCE(&release_lock);
  7310. }
  7311. if (!cl_dequeue_chan(ch)) {
  7312. /* Someone already released it. */
  7313. if (ast) {
  7314. ast_channel_unlock(ast);
  7315. }
  7316. ast_mutex_unlock(&release_lock);
  7317. return;
  7318. }
  7319. ch->state = MISDN_CLEANING;
  7320. ch->ast = NULL;
  7321. if (ast) {
  7322. struct chan_list *ast_ch;
  7323. ast_ch = MISDN_ASTERISK_TECH_PVT(ast);
  7324. MISDN_ASTERISK_TECH_PVT(ast) = NULL;
  7325. chan_misdn_log(1, bc->port,
  7326. "* RELEASING CHANNEL pid:%d context:%s dialed:%s caller:\"%s\" <%s>\n",
  7327. bc->pid,
  7328. ast->context,
  7329. ast->exten,
  7330. S_COR(ast->caller.id.name.valid, ast->caller.id.name.str, ""),
  7331. S_COR(ast->caller.id.number.valid, ast->caller.id.number.str, ""));
  7332. if (ast->_state != AST_STATE_RESERVED) {
  7333. chan_misdn_log(3, bc->port, " --> Setting AST State to down\n");
  7334. ast_setstate(ast, AST_STATE_DOWN);
  7335. }
  7336. ast_channel_unlock(ast);
  7337. if (ast_ch) {
  7338. chan_list_unref(ast_ch, "Release ast_channel reference.");
  7339. }
  7340. }
  7341. if (ch->originator == ORG_AST) {
  7342. --misdn_out_calls[bc->port];
  7343. } else {
  7344. --misdn_in_calls[bc->port];
  7345. }
  7346. ast_mutex_unlock(&release_lock);
  7347. }
  7348. /*!
  7349. * \internal
  7350. * \brief Do everything in release_chan() that makes sense without a bc.
  7351. *
  7352. * \param ch Call channel record to release.
  7353. *
  7354. * \return Nothing
  7355. *
  7356. * \note The only valid thing to do with ch after calling is to chan_list_unref(ch, "").
  7357. */
  7358. static void release_chan_early(struct chan_list *ch)
  7359. {
  7360. struct ast_channel *ast;
  7361. ast_mutex_lock(&release_lock);
  7362. for (;;) {
  7363. ast = ch->ast;
  7364. if (!ast || !ast_channel_trylock(ast)) {
  7365. break;
  7366. }
  7367. DEADLOCK_AVOIDANCE(&release_lock);
  7368. }
  7369. if (!cl_dequeue_chan(ch)) {
  7370. /* Someone already released it. */
  7371. if (ast) {
  7372. ast_channel_unlock(ast);
  7373. }
  7374. ast_mutex_unlock(&release_lock);
  7375. return;
  7376. }
  7377. ch->state = MISDN_CLEANING;
  7378. ch->ast = NULL;
  7379. if (ast) {
  7380. struct chan_list *ast_ch;
  7381. ast_ch = MISDN_ASTERISK_TECH_PVT(ast);
  7382. MISDN_ASTERISK_TECH_PVT(ast) = NULL;
  7383. if (ast->_state != AST_STATE_RESERVED) {
  7384. ast_setstate(ast, AST_STATE_DOWN);
  7385. }
  7386. ast_channel_unlock(ast);
  7387. if (ast_ch) {
  7388. chan_list_unref(ast_ch, "Release ast_channel reference.");
  7389. }
  7390. }
  7391. if (ch->hold.state != MISDN_HOLD_IDLE) {
  7392. if (ch->originator == ORG_AST) {
  7393. --misdn_out_calls[ch->hold.port];
  7394. } else {
  7395. --misdn_in_calls[ch->hold.port];
  7396. }
  7397. }
  7398. ast_mutex_unlock(&release_lock);
  7399. }
  7400. /*!
  7401. * \internal
  7402. * \brief Attempt to transfer the active channel party to the held channel party.
  7403. *
  7404. * \param active_ch Channel currently connected.
  7405. * \param held_ch Channel currently on hold.
  7406. *
  7407. * \retval 0 on success.
  7408. * \retval -1 on error.
  7409. */
  7410. static int misdn_attempt_transfer(struct chan_list *active_ch, struct chan_list *held_ch)
  7411. {
  7412. int retval;
  7413. struct ast_channel *target;
  7414. struct ast_channel *transferee;
  7415. struct ast_party_connected_line target_colp;
  7416. struct ast_party_connected_line transferee_colp;
  7417. switch (active_ch->state) {
  7418. case MISDN_PROCEEDING:
  7419. case MISDN_PROGRESS:
  7420. case MISDN_ALERTING:
  7421. case MISDN_CONNECTED:
  7422. break;
  7423. default:
  7424. return -1;
  7425. }
  7426. ast_channel_lock(held_ch->ast);
  7427. while (ast_channel_trylock(active_ch->ast)) {
  7428. CHANNEL_DEADLOCK_AVOIDANCE(held_ch->ast);
  7429. }
  7430. transferee = ast_bridged_channel(held_ch->ast);
  7431. if (!transferee) {
  7432. /*
  7433. * Could not transfer. Held channel is not bridged anymore.
  7434. * Held party probably got tired of waiting and hung up.
  7435. */
  7436. ast_channel_unlock(held_ch->ast);
  7437. ast_channel_unlock(active_ch->ast);
  7438. return -1;
  7439. }
  7440. target = active_ch->ast;
  7441. chan_misdn_log(1, held_ch->hold.port, "TRANSFERRING %s to %s\n",
  7442. held_ch->ast->name, target->name);
  7443. ast_party_connected_line_init(&target_colp);
  7444. ast_party_connected_line_copy(&target_colp, &target->connected);
  7445. ast_party_connected_line_init(&transferee_colp);
  7446. ast_party_connected_line_copy(&transferee_colp, &held_ch->ast->connected);
  7447. held_ch->hold.state = MISDN_HOLD_TRANSFER;
  7448. /*
  7449. * Before starting a masquerade, all channel and pvt locks must
  7450. * be unlocked. Any recursive channel locks held before
  7451. * ast_channel_transfer_masquerade() invalidates deadlock
  7452. * avoidance. Since we are unlocking both the pvt and its owner
  7453. * channel it is possible for "target" and "transferee" to be
  7454. * destroyed by their pbx threads. To prevent this we must give
  7455. * "target" and "transferee" a reference before any unlocking
  7456. * takes place.
  7457. */
  7458. ao2_ref(target, +1);
  7459. ao2_ref(transferee, +1);
  7460. ast_channel_unlock(held_ch->ast);
  7461. ast_channel_unlock(active_ch->ast);
  7462. /* Setup transfer masquerade. */
  7463. retval = ast_channel_transfer_masquerade(target, &target_colp, 0,
  7464. transferee, &transferee_colp, 1);
  7465. ast_party_connected_line_free(&target_colp);
  7466. ast_party_connected_line_free(&transferee_colp);
  7467. ao2_ref(target, -1);
  7468. ao2_ref(transferee, -1);
  7469. return retval;
  7470. }
  7471. static void do_immediate_setup(struct misdn_bchannel *bc, struct chan_list *ch, struct ast_channel *ast)
  7472. {
  7473. char *predial;
  7474. struct ast_frame fr;
  7475. predial = ast_strdupa(ast->exten);
  7476. ch->state = MISDN_DIALING;
  7477. if (!ch->noautorespond_on_setup) {
  7478. if (bc->nt) {
  7479. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  7480. } else {
  7481. if (misdn_lib_is_ptp(bc->port)) {
  7482. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  7483. } else {
  7484. misdn_lib_send_event(bc, EVENT_PROCEEDING);
  7485. }
  7486. }
  7487. } else {
  7488. ch->state = MISDN_INCOMING_SETUP;
  7489. }
  7490. chan_misdn_log(1, bc->port,
  7491. "* Starting Ast context:%s dialed:%s caller:\"%s\" <%s> with 's' extension\n",
  7492. ast->context,
  7493. ast->exten,
  7494. (ast->caller.id.name.valid && ast->caller.id.name.str)
  7495. ? ast->caller.id.name.str : "",
  7496. (ast->caller.id.number.valid && ast->caller.id.number.str)
  7497. ? ast->caller.id.number.str : "");
  7498. strcpy(ast->exten, "s");
  7499. if (!ast_canmatch_extension(ast, ast->context, ast->exten, 1, bc->caller.number) || pbx_start_chan(ch) < 0) {
  7500. ast = NULL;
  7501. bc->out_cause = AST_CAUSE_UNALLOCATED;
  7502. hangup_chan(ch, bc);
  7503. hanguptone_indicate(ch);
  7504. misdn_lib_send_event(bc, bc->nt ? EVENT_RELEASE_COMPLETE : EVENT_DISCONNECT);
  7505. }
  7506. while (!ast_strlen_zero(predial)) {
  7507. fr.frametype = AST_FRAME_DTMF;
  7508. fr.subclass.integer = *predial;
  7509. fr.src = NULL;
  7510. fr.data.ptr = NULL;
  7511. fr.datalen = 0;
  7512. fr.samples = 0;
  7513. fr.mallocd = 0;
  7514. fr.offset = 0;
  7515. fr.delivery = ast_tv(0,0);
  7516. if (ch->ast && MISDN_ASTERISK_TECH_PVT(ch->ast)) {
  7517. ast_queue_frame(ch->ast, &fr);
  7518. }
  7519. predial++;
  7520. }
  7521. }
  7522. /*!
  7523. * \retval -1 if can hangup after calling.
  7524. * \retval 0 if cannot hangup after calling.
  7525. */
  7526. static int send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, struct chan_list *ch)
  7527. {
  7528. int can_hangup;
  7529. if (!ast) {
  7530. chan_misdn_log(1, 0, "send_cause2ast: No Ast\n");
  7531. return 0;
  7532. }
  7533. if (!bc) {
  7534. chan_misdn_log(1, 0, "send_cause2ast: No BC\n");
  7535. return 0;
  7536. }
  7537. if (!ch) {
  7538. chan_misdn_log(1, 0, "send_cause2ast: No Ch\n");
  7539. return 0;
  7540. }
  7541. ast->hangupcause = bc->cause;
  7542. can_hangup = -1;
  7543. switch (bc->cause) {
  7544. case AST_CAUSE_UNALLOCATED:
  7545. case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
  7546. case AST_CAUSE_NO_ROUTE_DESTINATION:
  7547. case 4: /* Send special information tone */
  7548. case AST_CAUSE_NUMBER_CHANGED:
  7549. case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
  7550. /* Congestion Cases */
  7551. /*
  7552. * Not Queueing the Congestion anymore, since we want to hear
  7553. * the inband message
  7554. *
  7555. chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Congestion pid:%d\n", bc ? bc->pid : -1);
  7556. ch->state = MISDN_BUSY;
  7557. ast_queue_control(ast, AST_CONTROL_CONGESTION);
  7558. */
  7559. break;
  7560. case AST_CAUSE_CALL_REJECTED:
  7561. case AST_CAUSE_USER_BUSY:
  7562. ch->state = MISDN_BUSY;
  7563. if (!ch->need_busy) {
  7564. chan_misdn_log(1, bc ? bc->port : 0, "Queued busy already\n");
  7565. break;
  7566. }
  7567. ch->need_busy = 0;
  7568. chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Busy pid:%d\n", bc ? bc->pid : -1);
  7569. ast_queue_control(ast, AST_CONTROL_BUSY);
  7570. /* The BUSY is likely to cause a hangup or the user needs to hear it. */
  7571. can_hangup = 0;
  7572. break;
  7573. }
  7574. return can_hangup;
  7575. }
  7576. /*! \brief Import parameters from the dialplan environment variables */
  7577. void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch)
  7578. {
  7579. const char *tmp;
  7580. ast_channel_lock(chan);
  7581. tmp = pbx_builtin_getvar_helper(chan, "MISDN_ADDRESS_COMPLETE");
  7582. if (tmp && (atoi(tmp) == 1)) {
  7583. bc->sending_complete = 1;
  7584. }
  7585. tmp = pbx_builtin_getvar_helper(chan, "MISDN_USERUSER");
  7586. if (tmp) {
  7587. ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", tmp);
  7588. ast_copy_string(bc->uu, tmp, sizeof(bc->uu));
  7589. bc->uulen = strlen(bc->uu);
  7590. }
  7591. tmp = pbx_builtin_getvar_helper(chan, "MISDN_KEYPAD");
  7592. if (tmp) {
  7593. ast_copy_string(bc->keypad, tmp, sizeof(bc->keypad));
  7594. }
  7595. ast_channel_unlock(chan);
  7596. }
  7597. /*! \brief Export parameters to the dialplan environment variables */
  7598. void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch)
  7599. {
  7600. char tmp[32];
  7601. /*
  7602. * The only use for MISDN_PID is if there is a problem and you
  7603. * have to use the "misdn restart pid" CLI command. Otherwise,
  7604. * the pid is not used by anyone. The internal use of MISDN_PID
  7605. * has been deleted.
  7606. */
  7607. chan_misdn_log(3, bc->port, " --> EXPORT_PID: pid:%d\n", bc->pid);
  7608. snprintf(tmp, sizeof(tmp), "%d", bc->pid);
  7609. pbx_builtin_setvar_helper(chan, "_MISDN_PID", tmp);
  7610. if (bc->sending_complete) {
  7611. snprintf(tmp, sizeof(tmp), "%d", bc->sending_complete);
  7612. pbx_builtin_setvar_helper(chan, "MISDN_ADDRESS_COMPLETE", tmp);
  7613. }
  7614. if (bc->urate) {
  7615. snprintf(tmp, sizeof(tmp), "%d", bc->urate);
  7616. pbx_builtin_setvar_helper(chan, "MISDN_URATE", tmp);
  7617. }
  7618. if (bc->uulen) {
  7619. pbx_builtin_setvar_helper(chan, "MISDN_USERUSER", bc->uu);
  7620. }
  7621. if (!ast_strlen_zero(bc->keypad)) {
  7622. pbx_builtin_setvar_helper(chan, "MISDN_KEYPAD", bc->keypad);
  7623. }
  7624. }
  7625. int add_in_calls(int port)
  7626. {
  7627. int max_in_calls;
  7628. misdn_cfg_get(port, MISDN_CFG_MAX_IN, &max_in_calls, sizeof(max_in_calls));
  7629. misdn_in_calls[port]++;
  7630. if (max_in_calls >= 0 && max_in_calls < misdn_in_calls[port]) {
  7631. ast_log(LOG_NOTICE, "Marking Incoming Call on port[%d]\n", port);
  7632. return misdn_in_calls[port] - max_in_calls;
  7633. }
  7634. return 0;
  7635. }
  7636. int add_out_calls(int port)
  7637. {
  7638. int max_out_calls;
  7639. misdn_cfg_get(port, MISDN_CFG_MAX_OUT, &max_out_calls, sizeof(max_out_calls));
  7640. if (max_out_calls >= 0 && max_out_calls <= misdn_out_calls[port]) {
  7641. ast_log(LOG_NOTICE, "Rejecting Outgoing Call on port[%d]\n", port);
  7642. return (misdn_out_calls[port] + 1) - max_out_calls;
  7643. }
  7644. misdn_out_calls[port]++;
  7645. return 0;
  7646. }
  7647. static void start_pbx(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan)
  7648. {
  7649. if (pbx_start_chan(ch) < 0) {
  7650. hangup_chan(ch, bc);
  7651. chan_misdn_log(-1, bc->port, "ast_pbx_start returned <0 in SETUP\n");
  7652. if (bc->nt) {
  7653. hanguptone_indicate(ch);
  7654. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  7655. } else {
  7656. misdn_lib_send_event(bc, EVENT_RELEASE);
  7657. }
  7658. }
  7659. }
  7660. static void wait_for_digits(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan)
  7661. {
  7662. ch->state = MISDN_WAITING4DIGS;
  7663. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  7664. if (bc->nt && !bc->dialed.number[0]) {
  7665. dialtone_indicate(ch);
  7666. }
  7667. }
  7668. #if defined(AST_MISDN_ENHANCEMENTS)
  7669. /*!
  7670. * \internal
  7671. * \brief Handle the FACILITY CCBSStatusRequest message.
  7672. *
  7673. * \param port Logical port number.
  7674. * \param facility Facility ie contents.
  7675. *
  7676. * \return Nothing
  7677. */
  7678. static void misdn_cc_handle_ccbs_status_request(int port, const struct FacParm *facility)
  7679. {
  7680. struct misdn_cc_record *cc_record;
  7681. struct misdn_bchannel dummy;
  7682. switch (facility->u.CCBSStatusRequest.ComponentType) {
  7683. case FacComponent_Invoke:
  7684. /* Build message */
  7685. misdn_make_dummy(&dummy, port, 0, misdn_lib_port_is_nt(port), 0);
  7686. dummy.fac_out.Function = Fac_CCBSStatusRequest;
  7687. dummy.fac_out.u.CCBSStatusRequest.InvokeID = facility->u.CCBSStatusRequest.InvokeID;
  7688. dummy.fac_out.u.CCBSStatusRequest.ComponentType = FacComponent_Result;
  7689. /* Answer User-A free question */
  7690. AST_LIST_LOCK(&misdn_cc_records_db);
  7691. cc_record = misdn_cc_find_by_reference(port, facility->u.CCBSStatusRequest.Component.Invoke.CCBSReference);
  7692. if (cc_record) {
  7693. dummy.fac_out.u.CCBSStatusRequest.Component.Result.Free = cc_record->party_a_free;
  7694. } else {
  7695. /* No record so say User-A is free */
  7696. dummy.fac_out.u.CCBSStatusRequest.Component.Result.Free = 1;
  7697. }
  7698. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7699. /* Send message */
  7700. print_facility(&dummy.fac_out, &dummy);
  7701. misdn_lib_send_event(&dummy, EVENT_FACILITY);
  7702. break;
  7703. default:
  7704. chan_misdn_log(0, port, " --> not yet handled: facility type:0x%04X\n", facility->Function);
  7705. break;
  7706. }
  7707. }
  7708. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7709. #if defined(AST_MISDN_ENHANCEMENTS)
  7710. /*!
  7711. * \internal
  7712. * \brief Start a PBX to notify that User-B is available.
  7713. *
  7714. * \param record_id Call completion record ID
  7715. * \param notify Dialplan location to start processing.
  7716. *
  7717. * \return Nothing
  7718. */
  7719. static void misdn_cc_pbx_notify(long record_id, const struct misdn_cc_notify *notify)
  7720. {
  7721. struct ast_channel *chan;
  7722. char id_str[32];
  7723. static unsigned short sequence = 0;
  7724. /* Create a channel to notify with */
  7725. snprintf(id_str, sizeof(id_str), "%ld", record_id);
  7726. chan = ast_channel_alloc(0, AST_STATE_DOWN, id_str, NULL, NULL,
  7727. notify->exten, notify->context, NULL, 0,
  7728. "mISDN-CC/%ld-%X", record_id, (unsigned) ++sequence);
  7729. if (!chan) {
  7730. ast_log(LOG_ERROR, "Unable to allocate channel!\n");
  7731. return;
  7732. }
  7733. chan->priority = notify->priority;
  7734. ast_free(chan->dialed.number.str);
  7735. chan->dialed.number.str = ast_strdup(notify->exten);
  7736. if (ast_pbx_start(chan)) {
  7737. ast_log(LOG_WARNING, "Unable to start pbx channel %s!\n", chan->name);
  7738. ast_channel_release(chan);
  7739. } else {
  7740. ast_verb(1, "Started pbx for call completion notify channel %s\n", chan->name);
  7741. }
  7742. }
  7743. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7744. #if defined(AST_MISDN_ENHANCEMENTS)
  7745. /*!
  7746. * \internal
  7747. * \brief Handle the FACILITY CCBS_T_RemoteUserFree message.
  7748. *
  7749. * \param bc B channel control structure message came in on
  7750. *
  7751. * \return Nothing
  7752. */
  7753. static void misdn_cc_handle_T_remote_user_free(struct misdn_bchannel *bc)
  7754. {
  7755. struct misdn_cc_record *cc_record;
  7756. struct misdn_cc_notify notify;
  7757. long record_id;
  7758. AST_LIST_LOCK(&misdn_cc_records_db);
  7759. cc_record = misdn_cc_find_by_bc(bc);
  7760. if (cc_record) {
  7761. if (cc_record->party_a_free) {
  7762. notify = cc_record->remote_user_free;
  7763. } else {
  7764. /* Send CCBS_T_Suspend message */
  7765. bc->fac_out.Function = Fac_CCBS_T_Suspend;
  7766. bc->fac_out.u.CCBS_T_Suspend.InvokeID = ++misdn_invoke_id;
  7767. print_facility(&bc->fac_out, bc);
  7768. misdn_lib_send_event(bc, EVENT_FACILITY);
  7769. notify = cc_record->b_free;
  7770. }
  7771. record_id = cc_record->record_id;
  7772. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7773. if (notify.context[0]) {
  7774. /* Party A is free or B-Free notify has been setup. */
  7775. misdn_cc_pbx_notify(record_id, &notify);
  7776. }
  7777. } else {
  7778. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7779. }
  7780. }
  7781. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7782. #if defined(AST_MISDN_ENHANCEMENTS)
  7783. /*!
  7784. * \internal
  7785. * \brief Handle the FACILITY CCBSRemoteUserFree message.
  7786. *
  7787. * \param port Logical port number.
  7788. * \param facility Facility ie contents.
  7789. *
  7790. * \return Nothing
  7791. */
  7792. static void misdn_cc_handle_remote_user_free(int port, const struct FacParm *facility)
  7793. {
  7794. struct misdn_cc_record *cc_record;
  7795. struct misdn_cc_notify notify;
  7796. long record_id;
  7797. AST_LIST_LOCK(&misdn_cc_records_db);
  7798. cc_record = misdn_cc_find_by_reference(port, facility->u.CCBSRemoteUserFree.CCBSReference);
  7799. if (cc_record) {
  7800. notify = cc_record->remote_user_free;
  7801. record_id = cc_record->record_id;
  7802. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7803. misdn_cc_pbx_notify(record_id, &notify);
  7804. } else {
  7805. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7806. }
  7807. }
  7808. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7809. #if defined(AST_MISDN_ENHANCEMENTS)
  7810. /*!
  7811. * \internal
  7812. * \brief Handle the FACILITY CCBSBFree message.
  7813. *
  7814. * \param port Logical port number.
  7815. * \param facility Facility ie contents.
  7816. *
  7817. * \return Nothing
  7818. */
  7819. static void misdn_cc_handle_b_free(int port, const struct FacParm *facility)
  7820. {
  7821. struct misdn_cc_record *cc_record;
  7822. struct misdn_cc_notify notify;
  7823. long record_id;
  7824. AST_LIST_LOCK(&misdn_cc_records_db);
  7825. cc_record = misdn_cc_find_by_reference(port, facility->u.CCBSBFree.CCBSReference);
  7826. if (cc_record && cc_record->b_free.context[0]) {
  7827. /* B-Free notify has been setup. */
  7828. notify = cc_record->b_free;
  7829. record_id = cc_record->record_id;
  7830. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7831. misdn_cc_pbx_notify(record_id, &notify);
  7832. } else {
  7833. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7834. }
  7835. }
  7836. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7837. /*!
  7838. * \internal
  7839. * \brief Handle the incoming facility ie contents
  7840. *
  7841. * \param event Message type facility ie came in on
  7842. * \param bc B channel control structure message came in on
  7843. * \param ch Associated channel call record
  7844. *
  7845. * \return Nothing
  7846. */
  7847. static void misdn_facility_ie_handler(enum event_e event, struct misdn_bchannel *bc, struct chan_list *ch)
  7848. {
  7849. #if defined(AST_MISDN_ENHANCEMENTS)
  7850. const char *diagnostic_msg;
  7851. struct misdn_cc_record *cc_record;
  7852. char buf[32];
  7853. struct misdn_party_id party_id;
  7854. long new_record_id;
  7855. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7856. print_facility(&bc->fac_in, bc);
  7857. switch (bc->fac_in.Function) {
  7858. #if defined(AST_MISDN_ENHANCEMENTS)
  7859. case Fac_ActivationDiversion:
  7860. switch (bc->fac_in.u.ActivationDiversion.ComponentType) {
  7861. case FacComponent_Result:
  7862. /* Positive ACK to activation */
  7863. /* We don't handle this yet */
  7864. break;
  7865. default:
  7866. chan_misdn_log(0, bc->port," --> not yet handled: facility type:0x%04X\n",
  7867. bc->fac_in.Function);
  7868. break;
  7869. }
  7870. break;
  7871. case Fac_DeactivationDiversion:
  7872. switch (bc->fac_in.u.DeactivationDiversion.ComponentType) {
  7873. case FacComponent_Result:
  7874. /* Positive ACK to deactivation */
  7875. /* We don't handle this yet */
  7876. break;
  7877. default:
  7878. chan_misdn_log(0, bc->port," --> not yet handled: facility type:0x%04X\n",
  7879. bc->fac_in.Function);
  7880. break;
  7881. }
  7882. break;
  7883. case Fac_ActivationStatusNotificationDiv:
  7884. /* Sent to other MSN numbers on the line when a user activates call forwarding. */
  7885. /* Sent in the first call control message of an outgoing call from the served user. */
  7886. /* We do not have anything to do for this message. */
  7887. break;
  7888. case Fac_DeactivationStatusNotificationDiv:
  7889. /* Sent to other MSN numbers on the line when a user deactivates call forwarding. */
  7890. /* We do not have anything to do for this message. */
  7891. break;
  7892. #if 0 /* We don't handle this yet */
  7893. case Fac_InterrogationDiversion:
  7894. /* We don't handle this yet */
  7895. break;
  7896. case Fac_InterrogateServedUserNumbers:
  7897. /* We don't handle this yet */
  7898. break;
  7899. #endif /* We don't handle this yet */
  7900. case Fac_DiversionInformation:
  7901. /* Sent to the served user when a call is forwarded. */
  7902. /* We do not have anything to do for this message. */
  7903. break;
  7904. case Fac_CallDeflection:
  7905. if (ch && ch->ast) {
  7906. switch (bc->fac_in.u.CallDeflection.ComponentType) {
  7907. case FacComponent_Invoke:
  7908. ast_copy_string(bc->redirecting.from.number, bc->dialed.number,
  7909. sizeof(bc->redirecting.from.number));
  7910. bc->redirecting.from.name[0] = 0;
  7911. bc->redirecting.from.number_plan = bc->dialed.number_plan;
  7912. bc->redirecting.from.number_type = bc->dialed.number_type;
  7913. bc->redirecting.from.screening = 0;/* Unscreened */
  7914. if (bc->fac_in.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent) {
  7915. bc->redirecting.from.presentation =
  7916. bc->fac_in.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser
  7917. ? 0 /* Allowed */ : 1 /* Restricted */;
  7918. } else {
  7919. bc->redirecting.from.presentation = 0;/* Allowed */
  7920. }
  7921. /* Add configured prefix to the call deflection number */
  7922. memset(&party_id, 0, sizeof(party_id));
  7923. misdn_PartyNumber_extract(&party_id,
  7924. &bc->fac_in.u.CallDeflection.Component.Invoke.Deflection.Party);
  7925. misdn_add_number_prefix(bc->port, party_id.number_type,
  7926. party_id.number, sizeof(party_id.number));
  7927. //party_id.presentation = 0;/* Allowed */
  7928. //party_id.screening = 0;/* Unscreened */
  7929. bc->redirecting.to = party_id;
  7930. ++bc->redirecting.count;
  7931. bc->redirecting.reason = mISDN_REDIRECTING_REASON_DEFLECTION;
  7932. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  7933. ast_string_field_set(ch->ast, call_forward, bc->redirecting.to.number);
  7934. /* Send back positive ACK */
  7935. #if 1
  7936. /*
  7937. * Since there are no return result arguments it must be a
  7938. * generic result message. ETSI 300-196
  7939. */
  7940. bc->fac_out.Function = Fac_RESULT;
  7941. bc->fac_out.u.RESULT.InvokeID = bc->fac_in.u.CallDeflection.InvokeID;
  7942. #else
  7943. bc->fac_out.Function = Fac_CallDeflection;
  7944. bc->fac_out.u.CallDeflection.InvokeID = bc->fac_in.u.CallDeflection.InvokeID;
  7945. bc->fac_out.u.CallDeflection.ComponentType = FacComponent_Result;
  7946. #endif
  7947. print_facility(&bc->fac_out, bc);
  7948. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  7949. /* This line is BUSY to further attempts by this dialing attempt. */
  7950. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  7951. break;
  7952. case FacComponent_Result:
  7953. /* Positive ACK to call deflection */
  7954. /*
  7955. * Sent in DISCONNECT or FACILITY message depending upon network option.
  7956. * It is in the FACILITY message if the call is still offered to the user
  7957. * while trying to alert the deflected to party.
  7958. */
  7959. /* Ignore the ACK */
  7960. break;
  7961. default:
  7962. break;
  7963. }
  7964. }
  7965. break;
  7966. #if 0 /* We don't handle this yet */
  7967. case Fac_CallRerouteing:
  7968. /* Private-Public ISDN interworking message */
  7969. /* We don't handle this yet */
  7970. break;
  7971. #endif /* We don't handle this yet */
  7972. case Fac_DivertingLegInformation1:
  7973. /* Private-Public ISDN interworking message */
  7974. bc->div_leg_3_rx_wanted = 0;
  7975. if (ch && ch->ast) {
  7976. bc->redirecting.reason =
  7977. diversion_reason_to_misdn(bc->fac_in.u.DivertingLegInformation1.DiversionReason);
  7978. if (bc->fac_in.u.DivertingLegInformation1.DivertedToPresent) {
  7979. misdn_PresentedNumberUnscreened_extract(&bc->redirecting.to,
  7980. &bc->fac_in.u.DivertingLegInformation1.DivertedTo);
  7981. /* Add configured prefix to redirecting.to.number */
  7982. misdn_add_number_prefix(bc->port, bc->redirecting.to.number_type,
  7983. bc->redirecting.to.number, sizeof(bc->redirecting.to.number));
  7984. } else {
  7985. bc->redirecting.to.number[0] = '\0';
  7986. bc->redirecting.to.number_plan = NUMPLAN_ISDN;
  7987. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  7988. bc->redirecting.to.presentation = 1;/* restricted */
  7989. bc->redirecting.to.screening = 0;/* unscreened */
  7990. }
  7991. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  7992. bc->div_leg_3_rx_wanted = 1;
  7993. }
  7994. break;
  7995. case Fac_DivertingLegInformation2:
  7996. /* Private-Public ISDN interworking message */
  7997. switch (event) {
  7998. case EVENT_SETUP:
  7999. /* Comes in on a SETUP with redirecting.from information */
  8000. bc->div_leg_3_tx_pending = 1;
  8001. if (ch && ch->ast) {
  8002. /*
  8003. * Setup the redirecting.to informtion so we can identify
  8004. * if the user wants to manually supply the COLR for this
  8005. * redirected to number if further redirects could happen.
  8006. *
  8007. * All the user needs to do is set the REDIRECTING(to-pres)
  8008. * to the COLR and REDIRECTING(to-num) = ${EXTEN} to be safe
  8009. * after determining that the incoming call was redirected by
  8010. * checking if there is a REDIRECTING(from-num).
  8011. */
  8012. ast_copy_string(bc->redirecting.to.number, bc->dialed.number,
  8013. sizeof(bc->redirecting.to.number));
  8014. bc->redirecting.to.number_plan = bc->dialed.number_plan;
  8015. bc->redirecting.to.number_type = bc->dialed.number_type;
  8016. bc->redirecting.to.presentation = 1;/* restricted */
  8017. bc->redirecting.to.screening = 0;/* unscreened */
  8018. bc->redirecting.reason =
  8019. diversion_reason_to_misdn(bc->fac_in.u.DivertingLegInformation2.DiversionReason);
  8020. bc->redirecting.count = bc->fac_in.u.DivertingLegInformation2.DiversionCounter;
  8021. if (bc->fac_in.u.DivertingLegInformation2.DivertingPresent) {
  8022. /* This information is redundant if there was a redirecting ie in the SETUP. */
  8023. misdn_PresentedNumberUnscreened_extract(&bc->redirecting.from,
  8024. &bc->fac_in.u.DivertingLegInformation2.Diverting);
  8025. /* Add configured prefix to redirecting.from.number */
  8026. misdn_add_number_prefix(bc->port, bc->redirecting.from.number_type,
  8027. bc->redirecting.from.number, sizeof(bc->redirecting.from.number));
  8028. }
  8029. #if 0
  8030. if (bc->fac_in.u.DivertingLegInformation2.OriginalCalledPresent) {
  8031. /* We have no place to put the OriginalCalled number */
  8032. }
  8033. #endif
  8034. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  8035. }
  8036. break;
  8037. default:
  8038. chan_misdn_log(0, bc->port," --> Expected in a SETUP message: facility type:0x%04X\n",
  8039. bc->fac_in.Function);
  8040. break;
  8041. }
  8042. break;
  8043. case Fac_DivertingLegInformation3:
  8044. /* Private-Public ISDN interworking message */
  8045. if (bc->div_leg_3_rx_wanted) {
  8046. bc->div_leg_3_rx_wanted = 0;
  8047. if (ch && ch->ast) {
  8048. ch->ast->redirecting.to.number.presentation =
  8049. bc->fac_in.u.DivertingLegInformation3.PresentationAllowedIndicator
  8050. ? AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED
  8051. : AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
  8052. ast_channel_queue_redirecting_update(ch->ast, &ch->ast->redirecting, NULL);
  8053. }
  8054. }
  8055. break;
  8056. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  8057. case Fac_CD:
  8058. if (ch && ch->ast) {
  8059. ast_copy_string(bc->redirecting.from.number, bc->dialed.number,
  8060. sizeof(bc->redirecting.from.number));
  8061. bc->redirecting.from.name[0] = 0;
  8062. bc->redirecting.from.number_plan = bc->dialed.number_plan;
  8063. bc->redirecting.from.number_type = bc->dialed.number_type;
  8064. bc->redirecting.from.screening = 0;/* Unscreened */
  8065. bc->redirecting.from.presentation =
  8066. bc->fac_in.u.CDeflection.PresentationAllowed
  8067. ? 0 /* Allowed */ : 1 /* Restricted */;
  8068. ast_copy_string(bc->redirecting.to.number,
  8069. (char *) bc->fac_in.u.CDeflection.DeflectedToNumber,
  8070. sizeof(bc->redirecting.to.number));
  8071. bc->redirecting.to.name[0] = 0;
  8072. bc->redirecting.to.number_plan = NUMPLAN_UNKNOWN;
  8073. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  8074. bc->redirecting.to.presentation = 0;/* Allowed */
  8075. bc->redirecting.to.screening = 0;/* Unscreened */
  8076. ++bc->redirecting.count;
  8077. bc->redirecting.reason = mISDN_REDIRECTING_REASON_DEFLECTION;
  8078. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  8079. ast_string_field_set(ch->ast, call_forward, bc->redirecting.to.number);
  8080. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  8081. /* This line is BUSY to further attempts by this dialing attempt. */
  8082. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  8083. }
  8084. break;
  8085. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  8086. case Fac_AOCDCurrency:
  8087. if (ch && ch->ast) {
  8088. bc->AOCDtype = Fac_AOCDCurrency;
  8089. memcpy(&bc->AOCD.currency, &bc->fac_in.u.AOCDcur, sizeof(bc->AOCD.currency));
  8090. bc->AOCD_need_export = 1;
  8091. export_aoc_vars(ch->originator, ch->ast, bc);
  8092. }
  8093. break;
  8094. case Fac_AOCDChargingUnit:
  8095. if (ch && ch->ast) {
  8096. bc->AOCDtype = Fac_AOCDChargingUnit;
  8097. memcpy(&bc->AOCD.chargingUnit, &bc->fac_in.u.AOCDchu, sizeof(bc->AOCD.chargingUnit));
  8098. bc->AOCD_need_export = 1;
  8099. export_aoc_vars(ch->originator, ch->ast, bc);
  8100. }
  8101. break;
  8102. #if defined(AST_MISDN_ENHANCEMENTS)
  8103. case Fac_ERROR:
  8104. diagnostic_msg = misdn_to_str_error_code(bc->fac_in.u.ERROR.errorValue);
  8105. chan_misdn_log(1, bc->port, " --> Facility error code: %s\n", diagnostic_msg);
  8106. switch (event) {
  8107. case EVENT_DISCONNECT:
  8108. case EVENT_RELEASE:
  8109. case EVENT_RELEASE_COMPLETE:
  8110. /* Possible call failure as a result of Fac_CCBSCall/Fac_CCBS_T_Call */
  8111. if (ch && ch->peer) {
  8112. misdn_cc_set_peer_var(ch->peer, MISDN_ERROR_MSG, diagnostic_msg);
  8113. }
  8114. break;
  8115. default:
  8116. break;
  8117. }
  8118. AST_LIST_LOCK(&misdn_cc_records_db);
  8119. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.ERROR.invokeId);
  8120. if (cc_record) {
  8121. cc_record->outstanding_message = 0;
  8122. cc_record->error_code = bc->fac_in.u.ERROR.errorValue;
  8123. }
  8124. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8125. break;
  8126. case Fac_REJECT:
  8127. diagnostic_msg = misdn_to_str_reject_code(bc->fac_in.u.REJECT.Code);
  8128. chan_misdn_log(1, bc->port, " --> Facility reject code: %s\n", diagnostic_msg);
  8129. switch (event) {
  8130. case EVENT_DISCONNECT:
  8131. case EVENT_RELEASE:
  8132. case EVENT_RELEASE_COMPLETE:
  8133. /* Possible call failure as a result of Fac_CCBSCall/Fac_CCBS_T_Call */
  8134. if (ch && ch->peer) {
  8135. misdn_cc_set_peer_var(ch->peer, MISDN_ERROR_MSG, diagnostic_msg);
  8136. }
  8137. break;
  8138. default:
  8139. break;
  8140. }
  8141. if (bc->fac_in.u.REJECT.InvokeIDPresent) {
  8142. AST_LIST_LOCK(&misdn_cc_records_db);
  8143. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.REJECT.InvokeID);
  8144. if (cc_record) {
  8145. cc_record->outstanding_message = 0;
  8146. cc_record->reject_code = bc->fac_in.u.REJECT.Code;
  8147. }
  8148. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8149. }
  8150. break;
  8151. case Fac_RESULT:
  8152. AST_LIST_LOCK(&misdn_cc_records_db);
  8153. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.RESULT.InvokeID);
  8154. if (cc_record) {
  8155. cc_record->outstanding_message = 0;
  8156. }
  8157. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8158. break;
  8159. #if 0 /* We don't handle this yet */
  8160. case Fac_EctExecute:
  8161. /* We don't handle this yet */
  8162. break;
  8163. case Fac_ExplicitEctExecute:
  8164. /* We don't handle this yet */
  8165. break;
  8166. case Fac_EctLinkIdRequest:
  8167. /* We don't handle this yet */
  8168. break;
  8169. #endif /* We don't handle this yet */
  8170. case Fac_SubaddressTransfer:
  8171. /* We do not have anything to do for this message since we do not handle subaddreses. */
  8172. break;
  8173. case Fac_RequestSubaddress:
  8174. /*
  8175. * We do not have anything to do for this message since we do not handle subaddreses.
  8176. * However, we do care about some other ie's that should be present.
  8177. */
  8178. if (bc->redirecting.to_changed) {
  8179. /* Add configured prefix to redirecting.to.number */
  8180. misdn_add_number_prefix(bc->port, bc->redirecting.to.number_type,
  8181. bc->redirecting.to.number, sizeof(bc->redirecting.to.number));
  8182. }
  8183. switch (bc->notify_description_code) {
  8184. case mISDN_NOTIFY_CODE_INVALID:
  8185. /* Notify ie was not present. */
  8186. bc->redirecting.to_changed = 0;
  8187. break;
  8188. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ALERTING:
  8189. /*
  8190. * It would be preferable to update the connected line information
  8191. * only when the message callStatus is active. However, the
  8192. * optional redirection number may not be present in the active
  8193. * message if an alerting message were received earlier.
  8194. *
  8195. * The consequences if we wind up sending two updates is benign.
  8196. * The other end will think that it got transferred twice.
  8197. */
  8198. if (!bc->redirecting.to_changed) {
  8199. break;
  8200. }
  8201. bc->redirecting.to_changed = 0;
  8202. if (!ch || !ch->ast) {
  8203. break;
  8204. }
  8205. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  8206. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING,
  8207. bc->incoming_cid_tag);
  8208. break;
  8209. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE:
  8210. if (!bc->redirecting.to_changed) {
  8211. break;
  8212. }
  8213. bc->redirecting.to_changed = 0;
  8214. if (!ch || !ch->ast) {
  8215. break;
  8216. }
  8217. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  8218. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER, bc->incoming_cid_tag);
  8219. break;
  8220. default:
  8221. bc->redirecting.to_changed = 0;
  8222. chan_misdn_log(0, bc->port," --> not yet handled: notify code:0x%02X\n",
  8223. bc->notify_description_code);
  8224. break;
  8225. }
  8226. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  8227. break;
  8228. case Fac_EctInform:
  8229. /* Private-Public ISDN interworking message */
  8230. if (ch && ch->ast && bc->fac_in.u.EctInform.RedirectionPresent) {
  8231. /* Add configured prefix to the redirection number */
  8232. memset(&party_id, 0, sizeof(party_id));
  8233. misdn_PresentedNumberUnscreened_extract(&party_id,
  8234. &bc->fac_in.u.EctInform.Redirection);
  8235. misdn_add_number_prefix(bc->port, party_id.number_type,
  8236. party_id.number, sizeof(party_id.number));
  8237. /*
  8238. * It would be preferable to update the connected line information
  8239. * only when the message callStatus is active. However, the
  8240. * optional redirection number may not be present in the active
  8241. * message if an alerting message were received earlier.
  8242. *
  8243. * The consequences if we wind up sending two updates is benign.
  8244. * The other end will think that it got transferred twice.
  8245. */
  8246. misdn_update_remote_party(ch->ast, &party_id,
  8247. (bc->fac_in.u.EctInform.Status == 0 /* alerting */)
  8248. ? AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING
  8249. : AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER,
  8250. bc->incoming_cid_tag);
  8251. }
  8252. break;
  8253. #if 0 /* We don't handle this yet */
  8254. case Fac_EctLoopTest:
  8255. /* The use of this message is unclear on how it works to detect loops. */
  8256. /* We don't handle this yet */
  8257. break;
  8258. #endif /* We don't handle this yet */
  8259. case Fac_CallInfoRetain:
  8260. switch (event) {
  8261. case EVENT_ALERTING:
  8262. case EVENT_DISCONNECT:
  8263. /* CCBS/CCNR is available */
  8264. if (ch && ch->peer) {
  8265. AST_LIST_LOCK(&misdn_cc_records_db);
  8266. if (ch->record_id == -1) {
  8267. cc_record = misdn_cc_new();
  8268. } else {
  8269. /*
  8270. * We are doing a call-completion attempt
  8271. * or the switch is sending us extra call-completion
  8272. * availability indications (erroneously?).
  8273. *
  8274. * Assume that the network request retention option
  8275. * is not on and that the current call-completion
  8276. * request is disabled.
  8277. */
  8278. cc_record = misdn_cc_find_by_id(ch->record_id);
  8279. if (cc_record) {
  8280. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  8281. /*
  8282. * What? We are getting mixed messages from the
  8283. * switch. We are currently setup for
  8284. * point-to-point. Now we are switching to
  8285. * point-to-multipoint.
  8286. *
  8287. * Close the call-completion signaling link
  8288. */
  8289. cc_record->mode.ptp.bc->fac_out.Function = Fac_None;
  8290. cc_record->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  8291. misdn_lib_send_event(cc_record->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  8292. }
  8293. /*
  8294. * Resetup the existing record for a possible new
  8295. * call-completion request.
  8296. */
  8297. new_record_id = misdn_cc_record_id_new();
  8298. if (new_record_id < 0) {
  8299. /* Looks like we must keep the old id anyway. */
  8300. } else {
  8301. cc_record->record_id = new_record_id;
  8302. ch->record_id = new_record_id;
  8303. }
  8304. cc_record->ptp = 0;
  8305. cc_record->port = bc->port;
  8306. memset(&cc_record->mode, 0, sizeof(cc_record->mode));
  8307. cc_record->mode.ptmp.linkage_id = bc->fac_in.u.CallInfoRetain.CallLinkageID;
  8308. cc_record->invoke_id = ++misdn_invoke_id;
  8309. cc_record->activated = 0;
  8310. cc_record->outstanding_message = 0;
  8311. cc_record->activation_requested = 0;
  8312. cc_record->error_code = FacError_None;
  8313. cc_record->reject_code = FacReject_None;
  8314. memset(&cc_record->remote_user_free, 0, sizeof(cc_record->remote_user_free));
  8315. memset(&cc_record->b_free, 0, sizeof(cc_record->b_free));
  8316. cc_record->time_created = time(NULL);
  8317. cc_record = NULL;
  8318. } else {
  8319. /*
  8320. * Where did the record go? We will have to recapture
  8321. * the call setup information. Unfortunately, some
  8322. * setup information may have been changed.
  8323. */
  8324. ch->record_id = -1;
  8325. cc_record = misdn_cc_new();
  8326. }
  8327. }
  8328. if (cc_record) {
  8329. ch->record_id = cc_record->record_id;
  8330. cc_record->ptp = 0;
  8331. cc_record->port = bc->port;
  8332. cc_record->mode.ptmp.linkage_id = bc->fac_in.u.CallInfoRetain.CallLinkageID;
  8333. /* Record call information for possible call-completion attempt. */
  8334. cc_record->redial.caller = bc->caller;
  8335. cc_record->redial.dialed = bc->dialed;
  8336. cc_record->redial.setup_bc_hlc_llc = bc->setup_bc_hlc_llc;
  8337. cc_record->redial.capability = bc->capability;
  8338. cc_record->redial.hdlc = bc->hdlc;
  8339. }
  8340. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8341. /* Set MISDN_CC_RECORD_ID in original channel */
  8342. if (ch->record_id != -1) {
  8343. snprintf(buf, sizeof(buf), "%ld", ch->record_id);
  8344. } else {
  8345. buf[0] = 0;
  8346. }
  8347. misdn_cc_set_peer_var(ch->peer, MISDN_CC_RECORD_ID, buf);
  8348. }
  8349. break;
  8350. default:
  8351. chan_misdn_log(0, bc->port,
  8352. " --> Expected in a DISCONNECT or ALERTING message: facility type:0x%04X\n",
  8353. bc->fac_in.Function);
  8354. break;
  8355. }
  8356. break;
  8357. case Fac_CCBS_T_Call:
  8358. case Fac_CCBSCall:
  8359. switch (event) {
  8360. case EVENT_SETUP:
  8361. /*
  8362. * This is a call completion retry call.
  8363. * If we had anything to do we would do it here.
  8364. */
  8365. break;
  8366. default:
  8367. chan_misdn_log(0, bc->port, " --> Expected in a SETUP message: facility type:0x%04X\n",
  8368. bc->fac_in.Function);
  8369. break;
  8370. }
  8371. break;
  8372. case Fac_CCBSDeactivate:
  8373. switch (bc->fac_in.u.CCBSDeactivate.ComponentType) {
  8374. case FacComponent_Result:
  8375. AST_LIST_LOCK(&misdn_cc_records_db);
  8376. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.CCBSDeactivate.InvokeID);
  8377. if (cc_record) {
  8378. cc_record->outstanding_message = 0;
  8379. }
  8380. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8381. break;
  8382. default:
  8383. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8384. bc->fac_in.Function);
  8385. break;
  8386. }
  8387. break;
  8388. case Fac_CCBSErase:
  8389. AST_LIST_LOCK(&misdn_cc_records_db);
  8390. cc_record = misdn_cc_find_by_reference(bc->port, bc->fac_in.u.CCBSErase.CCBSReference);
  8391. if (cc_record) {
  8392. misdn_cc_delete(cc_record);
  8393. }
  8394. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8395. break;
  8396. case Fac_CCBSRemoteUserFree:
  8397. misdn_cc_handle_remote_user_free(bc->port, &bc->fac_in);
  8398. break;
  8399. case Fac_CCBSBFree:
  8400. misdn_cc_handle_b_free(bc->port, &bc->fac_in);
  8401. break;
  8402. case Fac_CCBSStatusRequest:
  8403. misdn_cc_handle_ccbs_status_request(bc->port, &bc->fac_in);
  8404. break;
  8405. case Fac_EraseCallLinkageID:
  8406. AST_LIST_LOCK(&misdn_cc_records_db);
  8407. cc_record = misdn_cc_find_by_linkage(bc->port,
  8408. bc->fac_in.u.EraseCallLinkageID.CallLinkageID);
  8409. if (cc_record && !cc_record->activation_requested) {
  8410. /*
  8411. * The T-RETENTION timer expired before we requested
  8412. * call completion activation. Call completion is no
  8413. * longer available.
  8414. */
  8415. misdn_cc_delete(cc_record);
  8416. }
  8417. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8418. break;
  8419. case Fac_CCBSStopAlerting:
  8420. /* We do not have anything to do for this message. */
  8421. break;
  8422. case Fac_CCBSRequest:
  8423. case Fac_CCNRRequest:
  8424. switch (bc->fac_in.u.CCBSRequest.ComponentType) {
  8425. case FacComponent_Result:
  8426. AST_LIST_LOCK(&misdn_cc_records_db);
  8427. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.CCBSRequest.InvokeID);
  8428. if (cc_record && !cc_record->ptp) {
  8429. cc_record->outstanding_message = 0;
  8430. cc_record->activated = 1;
  8431. cc_record->mode.ptmp.recall_mode = bc->fac_in.u.CCBSRequest.Component.Result.RecallMode;
  8432. cc_record->mode.ptmp.reference_id = bc->fac_in.u.CCBSRequest.Component.Result.CCBSReference;
  8433. }
  8434. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8435. break;
  8436. default:
  8437. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8438. bc->fac_in.Function);
  8439. break;
  8440. }
  8441. break;
  8442. #if 0 /* We don't handle this yet */
  8443. case Fac_CCBSInterrogate:
  8444. case Fac_CCNRInterrogate:
  8445. /* We don't handle this yet */
  8446. break;
  8447. case Fac_StatusRequest:
  8448. /* We don't handle this yet */
  8449. break;
  8450. #endif /* We don't handle this yet */
  8451. #if 0 /* We don't handle this yet */
  8452. case Fac_CCBS_T_Suspend:
  8453. case Fac_CCBS_T_Resume:
  8454. /* We don't handle this yet */
  8455. break;
  8456. #endif /* We don't handle this yet */
  8457. case Fac_CCBS_T_RemoteUserFree:
  8458. misdn_cc_handle_T_remote_user_free(bc);
  8459. break;
  8460. case Fac_CCBS_T_Available:
  8461. switch (event) {
  8462. case EVENT_ALERTING:
  8463. case EVENT_DISCONNECT:
  8464. /* CCBS-T/CCNR-T is available */
  8465. if (ch && ch->peer) {
  8466. int set_id = 1;
  8467. AST_LIST_LOCK(&misdn_cc_records_db);
  8468. if (ch->record_id == -1) {
  8469. cc_record = misdn_cc_new();
  8470. } else {
  8471. /*
  8472. * We are doing a call-completion attempt
  8473. * or the switch is sending us extra call-completion
  8474. * availability indications (erroneously?).
  8475. */
  8476. cc_record = misdn_cc_find_by_id(ch->record_id);
  8477. if (cc_record) {
  8478. if (cc_record->ptp && cc_record->mode.ptp.retention_enabled) {
  8479. /*
  8480. * Call-completion is still activated.
  8481. * The user does not have to request it again.
  8482. */
  8483. chan_misdn_log(1, bc->port, " --> Call-completion request retention option is enabled\n");
  8484. set_id = 0;
  8485. } else {
  8486. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  8487. /*
  8488. * The network request retention option
  8489. * is not on and the current call-completion
  8490. * request is to be disabled.
  8491. *
  8492. * We should get here only if EVENT_DISCONNECT
  8493. *
  8494. * Close the call-completion signaling link
  8495. */
  8496. cc_record->mode.ptp.bc->fac_out.Function = Fac_None;
  8497. cc_record->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  8498. misdn_lib_send_event(cc_record->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  8499. }
  8500. /*
  8501. * Resetup the existing record for a possible new
  8502. * call-completion request.
  8503. */
  8504. new_record_id = misdn_cc_record_id_new();
  8505. if (new_record_id < 0) {
  8506. /* Looks like we must keep the old id anyway. */
  8507. } else {
  8508. cc_record->record_id = new_record_id;
  8509. ch->record_id = new_record_id;
  8510. }
  8511. cc_record->ptp = 1;
  8512. cc_record->port = bc->port;
  8513. memset(&cc_record->mode, 0, sizeof(cc_record->mode));
  8514. cc_record->invoke_id = ++misdn_invoke_id;
  8515. cc_record->activated = 0;
  8516. cc_record->outstanding_message = 0;
  8517. cc_record->activation_requested = 0;
  8518. cc_record->error_code = FacError_None;
  8519. cc_record->reject_code = FacReject_None;
  8520. memset(&cc_record->remote_user_free, 0, sizeof(cc_record->remote_user_free));
  8521. memset(&cc_record->b_free, 0, sizeof(cc_record->b_free));
  8522. cc_record->time_created = time(NULL);
  8523. }
  8524. cc_record = NULL;
  8525. } else {
  8526. /*
  8527. * Where did the record go? We will have to recapture
  8528. * the call setup information. Unfortunately, some
  8529. * setup information may have been changed.
  8530. */
  8531. ch->record_id = -1;
  8532. cc_record = misdn_cc_new();
  8533. }
  8534. }
  8535. if (cc_record) {
  8536. ch->record_id = cc_record->record_id;
  8537. cc_record->ptp = 1;
  8538. cc_record->port = bc->port;
  8539. /* Record call information for possible call-completion attempt. */
  8540. cc_record->redial.caller = bc->caller;
  8541. cc_record->redial.dialed = bc->dialed;
  8542. cc_record->redial.setup_bc_hlc_llc = bc->setup_bc_hlc_llc;
  8543. cc_record->redial.capability = bc->capability;
  8544. cc_record->redial.hdlc = bc->hdlc;
  8545. }
  8546. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8547. /* Set MISDN_CC_RECORD_ID in original channel */
  8548. if (ch->record_id != -1 && set_id) {
  8549. snprintf(buf, sizeof(buf), "%ld", ch->record_id);
  8550. } else {
  8551. buf[0] = 0;
  8552. }
  8553. misdn_cc_set_peer_var(ch->peer, MISDN_CC_RECORD_ID, buf);
  8554. }
  8555. break;
  8556. default:
  8557. chan_misdn_log(0, bc->port,
  8558. " --> Expected in a DISCONNECT or ALERTING message: facility type:0x%04X\n",
  8559. bc->fac_in.Function);
  8560. break;
  8561. }
  8562. break;
  8563. case Fac_CCBS_T_Request:
  8564. case Fac_CCNR_T_Request:
  8565. switch (bc->fac_in.u.CCBS_T_Request.ComponentType) {
  8566. case FacComponent_Result:
  8567. AST_LIST_LOCK(&misdn_cc_records_db);
  8568. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.CCBS_T_Request.InvokeID);
  8569. if (cc_record && cc_record->ptp) {
  8570. cc_record->outstanding_message = 0;
  8571. cc_record->activated = 1;
  8572. cc_record->mode.ptp.retention_enabled =
  8573. cc_record->mode.ptp.requested_retention
  8574. ? bc->fac_in.u.CCBS_T_Request.Component.Result.RetentionSupported
  8575. ? 1 : 0
  8576. : 0;
  8577. }
  8578. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8579. break;
  8580. case FacComponent_Invoke:
  8581. /* We cannot be User-B in ptp mode. */
  8582. default:
  8583. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8584. bc->fac_in.Function);
  8585. break;
  8586. }
  8587. break;
  8588. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  8589. case Fac_None:
  8590. break;
  8591. default:
  8592. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8593. bc->fac_in.Function);
  8594. break;
  8595. }
  8596. }
  8597. /*!
  8598. * \internal
  8599. * \brief Determine if the given dialed party matches our MSN.
  8600. * \since 1.8
  8601. *
  8602. * \param port ISDN port
  8603. * \param dialed Dialed party information of incoming call.
  8604. *
  8605. * \retval non-zero if MSN is valid.
  8606. * \retval 0 if MSN invalid.
  8607. */
  8608. static int misdn_is_msn_valid(int port, const struct misdn_party_dialing *dialed)
  8609. {
  8610. char number[sizeof(dialed->number)];
  8611. ast_copy_string(number, dialed->number, sizeof(number));
  8612. misdn_add_number_prefix(port, dialed->number_type, number, sizeof(number));
  8613. return misdn_cfg_is_msn_valid(port, number);
  8614. }
  8615. /************************************************************/
  8616. /* Receive Events from isdn_lib here */
  8617. /************************************************************/
  8618. static enum event_response_e
  8619. cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
  8620. {
  8621. #if defined(AST_MISDN_ENHANCEMENTS)
  8622. struct misdn_cc_record *cc_record;
  8623. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  8624. struct chan_list *held_ch;
  8625. struct chan_list *ch = find_chan_by_bc(bc);
  8626. if (event != EVENT_BCHAN_DATA && event != EVENT_TONE_GENERATE) {
  8627. int debuglevel = 1;
  8628. /* Debug Only Non-Bchan */
  8629. if (event == EVENT_CLEANUP && !user_data) {
  8630. debuglevel = 5;
  8631. }
  8632. chan_misdn_log(debuglevel, bc->port,
  8633. "I IND :%s caller:\"%s\" <%s> dialed:%s pid:%d state:%s\n",
  8634. manager_isdn_get_info(event),
  8635. bc->caller.name,
  8636. bc->caller.number,
  8637. bc->dialed.number,
  8638. bc->pid,
  8639. ch ? misdn_get_ch_state(ch) : "none");
  8640. if (debuglevel == 1) {
  8641. misdn_lib_log_ies(bc);
  8642. chan_misdn_log(4, bc->port, " --> bc_state:%s\n", bc_state2str(bc->bc_state));
  8643. }
  8644. }
  8645. if (!ch) {
  8646. switch(event) {
  8647. case EVENT_SETUP:
  8648. case EVENT_DISCONNECT:
  8649. case EVENT_RELEASE:
  8650. case EVENT_RELEASE_COMPLETE:
  8651. case EVENT_PORT_ALARM:
  8652. case EVENT_RETRIEVE:
  8653. case EVENT_NEW_BC:
  8654. case EVENT_FACILITY:
  8655. case EVENT_REGISTER:
  8656. break;
  8657. case EVENT_CLEANUP:
  8658. case EVENT_TONE_GENERATE:
  8659. case EVENT_BCHAN_DATA:
  8660. return -1;
  8661. default:
  8662. 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);
  8663. return -1;
  8664. }
  8665. } else {
  8666. switch (event) {
  8667. case EVENT_TONE_GENERATE:
  8668. break;
  8669. case EVENT_DISCONNECT:
  8670. case EVENT_RELEASE:
  8671. case EVENT_RELEASE_COMPLETE:
  8672. case EVENT_CLEANUP:
  8673. case EVENT_TIMEOUT:
  8674. if (!ch->ast) {
  8675. 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));
  8676. }
  8677. break;
  8678. default:
  8679. if (!ch->ast || !MISDN_ASTERISK_TECH_PVT(ch->ast)) {
  8680. if (event != EVENT_BCHAN_DATA) {
  8681. ast_log(LOG_NOTICE, "No Ast or No private Pointer in Event (%d:%s)\n", event, manager_isdn_get_info(event));
  8682. }
  8683. chan_list_unref(ch, "No Ast or Ast private pointer");
  8684. return -1;
  8685. }
  8686. break;
  8687. }
  8688. }
  8689. switch (event) {
  8690. case EVENT_PORT_ALARM:
  8691. {
  8692. int boa = 0;
  8693. misdn_cfg_get(bc->port, MISDN_CFG_ALARM_BLOCK, &boa, sizeof(boa));
  8694. if (boa) {
  8695. cb_log(1, bc->port, " --> blocking\n");
  8696. misdn_lib_port_block(bc->port);
  8697. }
  8698. }
  8699. break;
  8700. case EVENT_BCHAN_ACTIVATED:
  8701. break;
  8702. case EVENT_NEW_CHANNEL:
  8703. update_name(ch->ast,bc->port,bc->channel);
  8704. break;
  8705. case EVENT_NEW_L3ID:
  8706. ch->l3id=bc->l3_id;
  8707. ch->addr=bc->addr;
  8708. break;
  8709. case EVENT_NEW_BC:
  8710. if (!ch) {
  8711. ch = find_hold_call(bc);
  8712. }
  8713. if (!ch) {
  8714. ast_log(LOG_WARNING, "NEW_BC without chan_list?\n");
  8715. break;
  8716. }
  8717. if (bc) {
  8718. ch->bc = (struct misdn_bchannel *) user_data;
  8719. }
  8720. break;
  8721. case EVENT_DTMF_TONE:
  8722. {
  8723. /* sending INFOS as DTMF-Frames :) */
  8724. struct ast_frame fr;
  8725. memset(&fr, 0, sizeof(fr));
  8726. fr.frametype = AST_FRAME_DTMF;
  8727. fr.subclass.integer = bc->dtmf ;
  8728. fr.src = NULL;
  8729. fr.data.ptr = NULL;
  8730. fr.datalen = 0;
  8731. fr.samples = 0;
  8732. fr.mallocd = 0;
  8733. fr.offset = 0;
  8734. fr.delivery = ast_tv(0,0);
  8735. if (!ch->ignore_dtmf) {
  8736. chan_misdn_log(2, bc->port, " --> DTMF:%c\n", bc->dtmf);
  8737. ast_queue_frame(ch->ast, &fr);
  8738. } else {
  8739. chan_misdn_log(2, bc->port, " --> Ignoring DTMF:%c due to bridge flags\n", bc->dtmf);
  8740. }
  8741. break;
  8742. }
  8743. case EVENT_STATUS:
  8744. break;
  8745. case EVENT_INFORMATION:
  8746. if (ch->state != MISDN_CONNECTED) {
  8747. stop_indicate(ch);
  8748. }
  8749. if (!ch->ast) {
  8750. break;
  8751. }
  8752. if (ch->state == MISDN_WAITING4DIGS) {
  8753. /* Ok, incomplete Setup, waiting till extension exists */
  8754. if (ast_strlen_zero(bc->info_dad) && ! ast_strlen_zero(bc->keypad)) {
  8755. chan_misdn_log(1, bc->port, " --> using keypad as info\n");
  8756. ast_copy_string(bc->info_dad, bc->keypad, sizeof(bc->info_dad));
  8757. }
  8758. strncat(bc->dialed.number, bc->info_dad, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  8759. ast_copy_string(ch->ast->exten, bc->dialed.number, sizeof(ch->ast->exten));
  8760. /* Check for Pickup Request first */
  8761. if (!strcmp(ch->ast->exten, ast_pickup_ext())) {
  8762. if (ast_pickup_call(ch->ast)) {
  8763. hangup_chan(ch, bc);
  8764. } else {
  8765. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  8766. hangup_chan(ch, bc);
  8767. ch->ast = NULL;
  8768. break;
  8769. }
  8770. }
  8771. if (!ast_canmatch_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  8772. if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->caller.number)) {
  8773. ast_log(LOG_WARNING,
  8774. "Extension '%s@%s' can never match. Jumping to 'i' extension. port:%d\n",
  8775. bc->dialed.number, ch->context, bc->port);
  8776. pbx_builtin_setvar_helper(ch->ast, "INVALID_EXTEN", bc->dialed.number);
  8777. strcpy(ch->ast->exten, "i");
  8778. ch->state = MISDN_DIALING;
  8779. start_pbx(ch, bc, ch->ast);
  8780. break;
  8781. }
  8782. ast_log(LOG_WARNING,
  8783. "Extension '%s@%s' can never match. Disconnecting. port:%d\n"
  8784. "\tMaybe you want to add an 'i' extension to catch this case.\n",
  8785. bc->dialed.number, ch->context, bc->port);
  8786. if (bc->nt) {
  8787. hanguptone_indicate(ch);
  8788. }
  8789. ch->state = MISDN_EXTCANTMATCH;
  8790. bc->out_cause = AST_CAUSE_UNALLOCATED;
  8791. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  8792. break;
  8793. }
  8794. if (ch->overlap_dial) {
  8795. ast_mutex_lock(&ch->overlap_tv_lock);
  8796. ch->overlap_tv = ast_tvnow();
  8797. ast_mutex_unlock(&ch->overlap_tv_lock);
  8798. if (ch->overlap_dial_task == -1) {
  8799. ch->overlap_dial_task =
  8800. misdn_tasks_add_variable(ch->overlap_dial, misdn_overlap_dial_task, ch);
  8801. }
  8802. break;
  8803. }
  8804. if (ast_exists_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  8805. ch->state = MISDN_DIALING;
  8806. start_pbx(ch, bc, ch->ast);
  8807. }
  8808. } else {
  8809. /* sending INFOS as DTMF-Frames :) */
  8810. struct ast_frame fr;
  8811. int digits;
  8812. memset(&fr, 0, sizeof(fr));
  8813. fr.frametype = AST_FRAME_DTMF;
  8814. fr.subclass.integer = bc->info_dad[0] ;
  8815. fr.src = NULL;
  8816. fr.data.ptr = NULL;
  8817. fr.datalen = 0;
  8818. fr.samples = 0;
  8819. fr.mallocd = 0;
  8820. fr.offset = 0;
  8821. fr.delivery = ast_tv(0,0);
  8822. misdn_cfg_get(0, MISDN_GEN_APPEND_DIGITS2EXTEN, &digits, sizeof(digits));
  8823. if (ch->state != MISDN_CONNECTED) {
  8824. if (digits) {
  8825. strncat(bc->dialed.number, bc->info_dad, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  8826. ast_copy_string(ch->ast->exten, bc->dialed.number, sizeof(ch->ast->exten));
  8827. ast_cdr_update(ch->ast);
  8828. }
  8829. ast_queue_frame(ch->ast, &fr);
  8830. }
  8831. }
  8832. break;
  8833. case EVENT_SETUP:
  8834. {
  8835. struct ast_channel *chan;
  8836. int exceed;
  8837. int ai;
  8838. int im;
  8839. int append_msn = 0;
  8840. if (ch) {
  8841. switch (ch->state) {
  8842. case MISDN_NOTHING:
  8843. chan_list_unref(ch, "Ignore found ch. Is it for an outgoing call?");
  8844. ch = NULL;
  8845. break;
  8846. default:
  8847. chan_list_unref(ch, "Already have a call.");
  8848. chan_misdn_log(1, bc->port, " --> Ignoring Call we have already one\n");
  8849. return RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE; /* Ignore MSNs which are not in our List */
  8850. }
  8851. }
  8852. if (!bc->nt && !misdn_is_msn_valid(bc->port, &bc->dialed)) {
  8853. chan_misdn_log(1, bc->port, " --> Ignoring Call, its not in our MSN List\n");
  8854. return RESPONSE_IGNORE_SETUP; /* Ignore MSNs which are not in our List */
  8855. }
  8856. if (bc->cw) {
  8857. int cause;
  8858. chan_misdn_log(0, bc->port, " --> Call Waiting on PMP sending RELEASE_COMPLETE\n");
  8859. misdn_cfg_get(bc->port, MISDN_CFG_REJECT_CAUSE, &cause, sizeof(cause));
  8860. bc->out_cause = cause ? cause : AST_CAUSE_NORMAL_CLEARING;
  8861. return RESPONSE_RELEASE_SETUP;
  8862. }
  8863. print_bearer(bc);
  8864. ch = chan_list_init(ORG_MISDN);
  8865. if (!ch) {
  8866. chan_misdn_log(-1, bc->port, "cb_events: malloc for chan_list failed!\n");
  8867. return RESPONSE_RELEASE_SETUP;
  8868. }
  8869. ch->bc = bc;
  8870. ch->l3id = bc->l3_id;
  8871. ch->addr = bc->addr;
  8872. chan = misdn_new(ch, AST_STATE_RESERVED, bc->dialed.number, bc->caller.number, AST_FORMAT_ALAW, NULL, bc->port, bc->channel);
  8873. if (!chan) {
  8874. chan_list_unref(ch, "Failed to create a new channel");
  8875. ast_log(LOG_ERROR, "cb_events: misdn_new failed!\n");
  8876. return RESPONSE_RELEASE_SETUP;
  8877. }
  8878. if ((exceed = add_in_calls(bc->port))) {
  8879. char tmp[16];
  8880. snprintf(tmp, sizeof(tmp), "%d", exceed);
  8881. pbx_builtin_setvar_helper(chan, "MAX_OVERFLOW", tmp);
  8882. }
  8883. read_config(ch);
  8884. export_ch(chan, bc, ch);
  8885. ch->ast->rings = 1;
  8886. ast_setstate(ch->ast, AST_STATE_RINGING);
  8887. /* Update asterisk channel caller information */
  8888. chan_misdn_log(2, bc->port, " --> TON: %s(%d)\n", misdn_to_str_ton(bc->caller.number_type), bc->caller.number_type);
  8889. chan_misdn_log(2, bc->port, " --> PLAN: %s(%d)\n", misdn_to_str_plan(bc->caller.number_plan), bc->caller.number_plan);
  8890. chan->caller.id.number.plan = misdn_to_ast_ton(bc->caller.number_type)
  8891. | misdn_to_ast_plan(bc->caller.number_plan);
  8892. chan_misdn_log(2, bc->port, " --> PRES: %s(%d)\n", misdn_to_str_pres(bc->caller.presentation), bc->caller.presentation);
  8893. chan_misdn_log(2, bc->port, " --> SCREEN: %s(%d)\n", misdn_to_str_screen(bc->caller.screening), bc->caller.screening);
  8894. chan->caller.id.number.presentation = misdn_to_ast_pres(bc->caller.presentation)
  8895. | misdn_to_ast_screen(bc->caller.screening);
  8896. ast_set_callerid(chan, bc->caller.number, NULL, bc->caller.number);
  8897. misdn_cfg_get(bc->port, MISDN_CFG_APPEND_MSN_TO_CALLERID_TAG, &append_msn, sizeof(append_msn));
  8898. if (append_msn) {
  8899. strncat(bc->incoming_cid_tag, "_", sizeof(bc->incoming_cid_tag) - strlen(bc->incoming_cid_tag) - 1);
  8900. strncat(bc->incoming_cid_tag, bc->dialed.number, sizeof(bc->incoming_cid_tag) - strlen(bc->incoming_cid_tag) - 1);
  8901. }
  8902. ast_channel_lock(chan);
  8903. chan->caller.id.tag = ast_strdup(bc->incoming_cid_tag);
  8904. ast_channel_unlock(chan);
  8905. if (!ast_strlen_zero(bc->redirecting.from.number)) {
  8906. /* Add configured prefix to redirecting.from.number */
  8907. misdn_add_number_prefix(bc->port, bc->redirecting.from.number_type, bc->redirecting.from.number, sizeof(bc->redirecting.from.number));
  8908. /* Update asterisk channel redirecting information */
  8909. misdn_copy_redirecting_to_ast(chan, &bc->redirecting, bc->incoming_cid_tag);
  8910. }
  8911. pbx_builtin_setvar_helper(chan, "TRANSFERCAPABILITY", ast_transfercapability2str(bc->capability));
  8912. chan->transfercapability = bc->capability;
  8913. switch (bc->capability) {
  8914. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  8915. pbx_builtin_setvar_helper(chan, "CALLTYPE", "DIGITAL");
  8916. break;
  8917. default:
  8918. pbx_builtin_setvar_helper(chan, "CALLTYPE", "SPEECH");
  8919. break;
  8920. }
  8921. if (!strstr(ch->allowed_bearers, "all")) {
  8922. int i;
  8923. for (i = 0; i < ARRAY_LEN(allowed_bearers_array); ++i) {
  8924. if (allowed_bearers_array[i].cap == bc->capability) {
  8925. if (strstr(ch->allowed_bearers, allowed_bearers_array[i].name)) {
  8926. /* The bearer capability is allowed */
  8927. if (allowed_bearers_array[i].deprecated) {
  8928. chan_misdn_log(0, bc->port, "%s in allowed_bearers list is deprecated\n",
  8929. allowed_bearers_array[i].name);
  8930. }
  8931. break;
  8932. }
  8933. }
  8934. }
  8935. if (i == ARRAY_LEN(allowed_bearers_array)) {
  8936. /* We did not find the bearer capability */
  8937. chan_misdn_log(0, bc->port, "Bearer capability not allowed: %s(%d)\n",
  8938. bearer2str(bc->capability), bc->capability);
  8939. ch->state = MISDN_EXTCANTMATCH;
  8940. chan_list_unref(ch, "BC not allowed, releasing call");
  8941. bc->out_cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
  8942. return RESPONSE_RELEASE_SETUP;
  8943. }
  8944. }
  8945. /** queue new chan **/
  8946. cl_queue_chan(ch);
  8947. if (bc->fac_in.Function != Fac_None) {
  8948. misdn_facility_ie_handler(event, bc, ch);
  8949. }
  8950. /* Check for Pickup Request first */
  8951. if (!strcmp(chan->exten, ast_pickup_ext())) {
  8952. if (!ch->noautorespond_on_setup) {
  8953. /* Sending SETUP_ACK */
  8954. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  8955. } else {
  8956. ch->state = MISDN_INCOMING_SETUP;
  8957. }
  8958. if (ast_pickup_call(chan)) {
  8959. hangup_chan(ch, bc);
  8960. } else {
  8961. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  8962. hangup_chan(ch, bc);
  8963. ch->ast = NULL;
  8964. break;
  8965. }
  8966. }
  8967. /*
  8968. * added support for s extension hope it will help those poor cretains
  8969. * which haven't overlap dial.
  8970. */
  8971. misdn_cfg_get(bc->port, MISDN_CFG_ALWAYS_IMMEDIATE, &ai, sizeof(ai));
  8972. if (ai) {
  8973. do_immediate_setup(bc, ch, chan);
  8974. break;
  8975. }
  8976. /* check if we should jump into s when we have no dialed.number */
  8977. misdn_cfg_get(bc->port, MISDN_CFG_IMMEDIATE, &im, sizeof(im));
  8978. if (im && ast_strlen_zero(bc->dialed.number)) {
  8979. do_immediate_setup(bc, ch, chan);
  8980. break;
  8981. }
  8982. chan_misdn_log(5, bc->port, "CONTEXT:%s\n", ch->context);
  8983. if (!ast_canmatch_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  8984. if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->caller.number)) {
  8985. ast_log(LOG_WARNING,
  8986. "Extension '%s@%s' can never match. Jumping to 'i' extension. port:%d\n",
  8987. bc->dialed.number, ch->context, bc->port);
  8988. pbx_builtin_setvar_helper(ch->ast, "INVALID_EXTEN", bc->dialed.number);
  8989. strcpy(ch->ast->exten, "i");
  8990. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  8991. ch->state = MISDN_DIALING;
  8992. start_pbx(ch, bc, chan);
  8993. break;
  8994. }
  8995. ast_log(LOG_WARNING,
  8996. "Extension '%s@%s' can never match. Disconnecting. port:%d\n"
  8997. "\tMaybe you want to add an 'i' extension to catch this case.\n",
  8998. bc->dialed.number, ch->context, bc->port);
  8999. if (bc->nt) {
  9000. hanguptone_indicate(ch);
  9001. }
  9002. ch->state = MISDN_EXTCANTMATCH;
  9003. bc->out_cause = AST_CAUSE_UNALLOCATED;
  9004. misdn_lib_send_event(bc, bc->nt ? EVENT_RELEASE_COMPLETE : EVENT_RELEASE);
  9005. break;
  9006. }
  9007. /* Whatever happens, when sending_complete is set or we are PTMP TE, we will definitely
  9008. * jump into the dialplan, when the dialed extension does not exist, the 's' extension
  9009. * will be used by Asterisk automatically. */
  9010. if (bc->sending_complete || (!bc->nt && !misdn_lib_is_ptp(bc->port))) {
  9011. if (!ch->noautorespond_on_setup) {
  9012. ch->state=MISDN_DIALING;
  9013. misdn_lib_send_event(bc, EVENT_PROCEEDING);
  9014. } else {
  9015. ch->state = MISDN_INCOMING_SETUP;
  9016. }
  9017. start_pbx(ch, bc, chan);
  9018. break;
  9019. }
  9020. /*
  9021. * When we are NT and overlapdial is set and if
  9022. * the number is empty, we wait for the ISDN timeout
  9023. * instead of our own timer.
  9024. */
  9025. if (ch->overlap_dial && bc->nt && !bc->dialed.number[0]) {
  9026. wait_for_digits(ch, bc, chan);
  9027. break;
  9028. }
  9029. /*
  9030. * If overlapdial we will definitely send a SETUP_ACKNOWLEDGE and wait for more
  9031. * Infos with a Interdigit Timeout.
  9032. * */
  9033. if (ch->overlap_dial) {
  9034. ast_mutex_lock(&ch->overlap_tv_lock);
  9035. ch->overlap_tv = ast_tvnow();
  9036. ast_mutex_unlock(&ch->overlap_tv_lock);
  9037. wait_for_digits(ch, bc, chan);
  9038. if (ch->overlap_dial_task == -1) {
  9039. ch->overlap_dial_task =
  9040. misdn_tasks_add_variable(ch->overlap_dial, misdn_overlap_dial_task, ch);
  9041. }
  9042. break;
  9043. }
  9044. /* If the extension does not exist and we're not TE_PTMP we wait for more digits
  9045. * without interdigit timeout.
  9046. * */
  9047. if (!ast_exists_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  9048. wait_for_digits(ch, bc, chan);
  9049. break;
  9050. }
  9051. /*
  9052. * If the extension exists let's just jump into it.
  9053. * */
  9054. if (ast_exists_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  9055. misdn_lib_send_event(bc, bc->need_more_infos ? EVENT_SETUP_ACKNOWLEDGE : EVENT_PROCEEDING);
  9056. ch->state = MISDN_DIALING;
  9057. start_pbx(ch, bc, chan);
  9058. break;
  9059. }
  9060. break;
  9061. }
  9062. #if defined(AST_MISDN_ENHANCEMENTS)
  9063. case EVENT_REGISTER:
  9064. if (bc->fac_in.Function != Fac_None) {
  9065. misdn_facility_ie_handler(event, bc, ch);
  9066. }
  9067. /*
  9068. * Shut down this connection immediately.
  9069. * The current design of chan_misdn data structures
  9070. * does not allow the proper handling of inbound call records
  9071. * without an assigned B channel. Therefore, we cannot
  9072. * be the CCBS User-B party in a point-to-point setup.
  9073. */
  9074. bc->fac_out.Function = Fac_None;
  9075. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  9076. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  9077. break;
  9078. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9079. case EVENT_SETUP_ACKNOWLEDGE:
  9080. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  9081. if (bc->channel) {
  9082. update_name(ch->ast,bc->port,bc->channel);
  9083. }
  9084. if (bc->fac_in.Function != Fac_None) {
  9085. misdn_facility_ie_handler(event, bc, ch);
  9086. }
  9087. if (!ast_strlen_zero(bc->infos_pending)) {
  9088. /* TX Pending Infos */
  9089. strncat(bc->dialed.number, bc->infos_pending, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  9090. if (!ch->ast) {
  9091. break;
  9092. }
  9093. ast_copy_string(ch->ast->exten, bc->dialed.number, sizeof(ch->ast->exten));
  9094. ast_copy_string(bc->info_dad, bc->infos_pending, sizeof(bc->info_dad));
  9095. ast_copy_string(bc->infos_pending, "", sizeof(bc->infos_pending));
  9096. misdn_lib_send_event(bc, EVENT_INFORMATION);
  9097. }
  9098. break;
  9099. case EVENT_PROCEEDING:
  9100. if (misdn_cap_is_speech(bc->capability) &&
  9101. misdn_inband_avail(bc)) {
  9102. start_bc_tones(ch);
  9103. }
  9104. ch->state = MISDN_PROCEEDING;
  9105. if (bc->fac_in.Function != Fac_None) {
  9106. misdn_facility_ie_handler(event, bc, ch);
  9107. }
  9108. if (!ch->ast) {
  9109. break;
  9110. }
  9111. ast_queue_control(ch->ast, AST_CONTROL_PROCEEDING);
  9112. break;
  9113. case EVENT_PROGRESS:
  9114. if (bc->channel) {
  9115. update_name(ch->ast, bc->port, bc->channel);
  9116. }
  9117. if (bc->fac_in.Function != Fac_None) {
  9118. misdn_facility_ie_handler(event, bc, ch);
  9119. }
  9120. if (!bc->nt) {
  9121. if (misdn_cap_is_speech(bc->capability) &&
  9122. misdn_inband_avail(bc)) {
  9123. start_bc_tones(ch);
  9124. }
  9125. ch->state = MISDN_PROGRESS;
  9126. if (!ch->ast) {
  9127. break;
  9128. }
  9129. ast_queue_control(ch->ast, AST_CONTROL_PROGRESS);
  9130. }
  9131. break;
  9132. case EVENT_ALERTING:
  9133. ch->state = MISDN_ALERTING;
  9134. if (!ch->ast) {
  9135. break;
  9136. }
  9137. if (bc->fac_in.Function != Fac_None) {
  9138. misdn_facility_ie_handler(event, bc, ch);
  9139. }
  9140. ast_queue_control(ch->ast, AST_CONTROL_RINGING);
  9141. ast_setstate(ch->ast, AST_STATE_RINGING);
  9142. cb_log(7, bc->port, " --> Set State Ringing\n");
  9143. if (misdn_cap_is_speech(bc->capability) && misdn_inband_avail(bc)) {
  9144. cb_log(1, bc->port, "Starting Tones, we have inband Data\n");
  9145. start_bc_tones(ch);
  9146. } else {
  9147. cb_log(3, bc->port, " --> We have no inband Data, the other end must create ringing\n");
  9148. if (ch->far_alerting) {
  9149. cb_log(1, bc->port, " --> The other end can not do ringing eh ?.. we must do all ourself..");
  9150. start_bc_tones(ch);
  9151. /*tone_indicate(ch, TONE_FAR_ALERTING);*/
  9152. }
  9153. }
  9154. break;
  9155. case EVENT_CONNECT:
  9156. if (bc->fac_in.Function != Fac_None) {
  9157. misdn_facility_ie_handler(event, bc, ch);
  9158. }
  9159. #if defined(AST_MISDN_ENHANCEMENTS)
  9160. if (bc->div_leg_3_rx_wanted) {
  9161. bc->div_leg_3_rx_wanted = 0;
  9162. if (ch->ast) {
  9163. ch->ast->redirecting.to.number.presentation =
  9164. AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
  9165. ast_channel_queue_redirecting_update(ch->ast, &ch->ast->redirecting, NULL);
  9166. }
  9167. }
  9168. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9169. /* we answer when we've got our very new L3 ID from the NT stack */
  9170. misdn_lib_send_event(bc, EVENT_CONNECT_ACKNOWLEDGE);
  9171. if (!ch->ast) {
  9172. break;
  9173. }
  9174. stop_indicate(ch);
  9175. #if defined(AST_MISDN_ENHANCEMENTS)
  9176. if (ch->record_id != -1) {
  9177. /*
  9178. * We will delete the associated call completion
  9179. * record since we now have a completed call.
  9180. * We will not wait/depend on the network to tell
  9181. * us to delete it.
  9182. */
  9183. AST_LIST_LOCK(&misdn_cc_records_db);
  9184. cc_record = misdn_cc_find_by_id(ch->record_id);
  9185. if (cc_record) {
  9186. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  9187. /* Close the call-completion signaling link */
  9188. cc_record->mode.ptp.bc->fac_out.Function = Fac_None;
  9189. cc_record->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  9190. misdn_lib_send_event(cc_record->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  9191. }
  9192. misdn_cc_delete(cc_record);
  9193. }
  9194. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9195. ch->record_id = -1;
  9196. if (ch->peer) {
  9197. misdn_cc_set_peer_var(ch->peer, MISDN_CC_RECORD_ID, "");
  9198. ao2_ref(ch->peer, -1);
  9199. ch->peer = NULL;
  9200. }
  9201. }
  9202. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9203. if (!ast_strlen_zero(bc->connected.number)) {
  9204. /* Add configured prefix to connected.number */
  9205. misdn_add_number_prefix(bc->port, bc->connected.number_type, bc->connected.number, sizeof(bc->connected.number));
  9206. /* Update the connected line information on the other channel */
  9207. misdn_update_remote_party(ch->ast, &bc->connected, AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER, bc->incoming_cid_tag);
  9208. }
  9209. ch->l3id = bc->l3_id;
  9210. ch->addr = bc->addr;
  9211. start_bc_tones(ch);
  9212. ch->state = MISDN_CONNECTED;
  9213. ast_queue_control(ch->ast, AST_CONTROL_ANSWER);
  9214. break;
  9215. case EVENT_CONNECT_ACKNOWLEDGE:
  9216. ch->l3id = bc->l3_id;
  9217. ch->addr = bc->addr;
  9218. start_bc_tones(ch);
  9219. ch->state = MISDN_CONNECTED;
  9220. break;
  9221. case EVENT_DISCONNECT:
  9222. /* we might not have an ch->ast ptr here anymore */
  9223. if (ch) {
  9224. if (bc->fac_in.Function != Fac_None) {
  9225. misdn_facility_ie_handler(event, bc, ch);
  9226. }
  9227. 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);
  9228. if (ch->originator == ORG_AST && !bc->nt && misdn_inband_avail(bc) && ch->state != MISDN_CONNECTED) {
  9229. /* If there's inband information available (e.g. a
  9230. recorded message saying what was wrong with the
  9231. dialled number, or perhaps even giving an
  9232. alternative number, then play it instead of
  9233. immediately releasing the call */
  9234. chan_misdn_log(1, bc->port, " --> Inband Info Avail, not sending RELEASE\n");
  9235. ch->state = MISDN_DISCONNECTED;
  9236. start_bc_tones(ch);
  9237. if (ch->ast) {
  9238. ch->ast->hangupcause = bc->cause;
  9239. if (bc->cause == AST_CAUSE_USER_BUSY) {
  9240. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  9241. }
  9242. }
  9243. ch->need_busy = 0;
  9244. break;
  9245. }
  9246. bc->need_disconnect = 0;
  9247. stop_bc_tones(ch);
  9248. /* Check for held channel, to implement transfer */
  9249. held_ch = find_hold_call(bc);
  9250. if (!held_ch || !ch->ast || misdn_attempt_transfer(ch, held_ch)) {
  9251. hangup_chan(ch, bc);
  9252. }
  9253. } else {
  9254. held_ch = find_hold_call_l3(bc->l3_id);
  9255. if (held_ch) {
  9256. if (bc->fac_in.Function != Fac_None) {
  9257. misdn_facility_ie_handler(event, bc, held_ch);
  9258. }
  9259. if (held_ch->hold.state == MISDN_HOLD_ACTIVE) {
  9260. bc->need_disconnect = 0;
  9261. #if defined(TRANSFER_ON_HELD_CALL_HANGUP)
  9262. /*
  9263. * Some phones disconnect the held call and the active call at the
  9264. * same time to do the transfer. Unfortunately, either call could
  9265. * be disconnected first.
  9266. */
  9267. ch = find_hold_active_call(bc);
  9268. if (!ch || misdn_attempt_transfer(ch, held_ch)) {
  9269. held_ch->hold.state = MISDN_HOLD_DISCONNECT;
  9270. hangup_chan(held_ch, bc);
  9271. }
  9272. #else
  9273. hangup_chan(held_ch, bc);
  9274. #endif /* defined(TRANSFER_ON_HELD_CALL_HANGUP) */
  9275. }
  9276. }
  9277. }
  9278. if (held_ch) {
  9279. chan_list_unref(held_ch, "Done with held call");
  9280. }
  9281. bc->out_cause = -1;
  9282. if (bc->need_release) {
  9283. misdn_lib_send_event(bc, EVENT_RELEASE);
  9284. }
  9285. break;
  9286. case EVENT_RELEASE:
  9287. if (!ch) {
  9288. ch = find_hold_call_l3(bc->l3_id);
  9289. if (!ch) {
  9290. chan_misdn_log(1, bc->port,
  9291. " --> no Ch, so we've already released. (%s)\n",
  9292. manager_isdn_get_info(event));
  9293. return -1;
  9294. }
  9295. }
  9296. if (bc->fac_in.Function != Fac_None) {
  9297. misdn_facility_ie_handler(event, bc, ch);
  9298. }
  9299. bc->need_disconnect = 0;
  9300. bc->need_release = 0;
  9301. hangup_chan(ch, bc);
  9302. release_chan(ch, bc);
  9303. break;
  9304. case EVENT_RELEASE_COMPLETE:
  9305. if (!ch) {
  9306. ch = find_hold_call_l3(bc->l3_id);
  9307. }
  9308. bc->need_disconnect = 0;
  9309. bc->need_release = 0;
  9310. bc->need_release_complete = 0;
  9311. if (ch) {
  9312. if (bc->fac_in.Function != Fac_None) {
  9313. misdn_facility_ie_handler(event, bc, ch);
  9314. }
  9315. stop_bc_tones(ch);
  9316. hangup_chan(ch, bc);
  9317. release_chan(ch, bc);
  9318. } else {
  9319. #if defined(AST_MISDN_ENHANCEMENTS)
  9320. /*
  9321. * A call-completion signaling link established with
  9322. * REGISTER does not have a struct chan_list record
  9323. * associated with it.
  9324. */
  9325. AST_LIST_LOCK(&misdn_cc_records_db);
  9326. cc_record = misdn_cc_find_by_bc(bc);
  9327. if (cc_record) {
  9328. /* The call-completion signaling link is closed. */
  9329. misdn_cc_delete(cc_record);
  9330. }
  9331. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9332. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9333. chan_misdn_log(1, bc->port,
  9334. " --> no Ch, so we've already released. (%s)\n",
  9335. manager_isdn_get_info(event));
  9336. }
  9337. break;
  9338. case EVENT_BCHAN_ERROR:
  9339. case EVENT_CLEANUP:
  9340. stop_bc_tones(ch);
  9341. switch (ch->state) {
  9342. case MISDN_CALLING:
  9343. bc->cause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
  9344. break;
  9345. default:
  9346. break;
  9347. }
  9348. hangup_chan(ch, bc);
  9349. release_chan(ch, bc);
  9350. break;
  9351. case EVENT_TONE_GENERATE:
  9352. {
  9353. int tone_len = bc->tone_cnt;
  9354. struct ast_channel *ast = ch->ast;
  9355. void *tmp;
  9356. int res;
  9357. int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
  9358. chan_misdn_log(9, bc->port, "TONE_GEN: len:%d\n", tone_len);
  9359. if (!ast) {
  9360. break;
  9361. }
  9362. if (!ast->generator) {
  9363. break;
  9364. }
  9365. tmp = ast->generatordata;
  9366. ast->generatordata = NULL;
  9367. generate = ast->generator->generate;
  9368. if (tone_len < 0 || tone_len > 512) {
  9369. ast_log(LOG_NOTICE, "TONE_GEN: len was %d, set to 128\n", tone_len);
  9370. tone_len = 128;
  9371. }
  9372. res = generate(ast, tmp, tone_len, tone_len);
  9373. ast->generatordata = tmp;
  9374. if (res) {
  9375. ast_log(LOG_WARNING, "Auto-deactivating generator\n");
  9376. ast_deactivate_generator(ast);
  9377. } else {
  9378. bc->tone_cnt = 0;
  9379. }
  9380. break;
  9381. }
  9382. case EVENT_BCHAN_DATA:
  9383. if (ch->bc->AOCD_need_export) {
  9384. export_aoc_vars(ch->originator, ch->ast, ch->bc);
  9385. }
  9386. if (!misdn_cap_is_speech(ch->bc->capability)) {
  9387. struct ast_frame frame;
  9388. /* In Data Modes we queue frames */
  9389. memset(&frame, 0, sizeof(frame));
  9390. frame.frametype = AST_FRAME_VOICE; /* we have no data frames yet */
  9391. frame.subclass.codec = AST_FORMAT_ALAW;
  9392. frame.datalen = bc->bframe_len;
  9393. frame.samples = bc->bframe_len;
  9394. frame.mallocd = 0;
  9395. frame.offset = 0;
  9396. frame.delivery = ast_tv(0, 0);
  9397. frame.src = NULL;
  9398. frame.data.ptr = bc->bframe;
  9399. if (ch->ast) {
  9400. ast_queue_frame(ch->ast, &frame);
  9401. }
  9402. } else {
  9403. struct pollfd pfd = { .fd = ch->pipe[1], .events = POLLOUT };
  9404. int t;
  9405. t = ast_poll(&pfd, 1, 0);
  9406. if (t < 0) {
  9407. chan_misdn_log(-1, bc->port, "poll() error (err=%s)\n", strerror(errno));
  9408. break;
  9409. }
  9410. if (!t) {
  9411. chan_misdn_log(9, bc->port, "poll() timed out\n");
  9412. break;
  9413. }
  9414. if (pfd.revents & POLLOUT) {
  9415. chan_misdn_log(9, bc->port, "writing %d bytes to asterisk\n", bc->bframe_len);
  9416. if (write(ch->pipe[1], bc->bframe, bc->bframe_len) <= 0) {
  9417. chan_misdn_log(0, bc->port, "Write returned <=0 (err=%s) --> hanging up channel\n", strerror(errno));
  9418. stop_bc_tones(ch);
  9419. hangup_chan(ch, bc);
  9420. release_chan(ch, bc);
  9421. }
  9422. } else {
  9423. chan_misdn_log(1, bc->port, "Write Pipe full!\n");
  9424. }
  9425. }
  9426. break;
  9427. case EVENT_TIMEOUT:
  9428. if (ch && bc) {
  9429. chan_misdn_log(1, bc->port, "--> state: %s\n", misdn_get_ch_state(ch));
  9430. }
  9431. switch (ch->state) {
  9432. case MISDN_DIALING:
  9433. case MISDN_PROGRESS:
  9434. if (bc->nt && !ch->nttimeout) {
  9435. break;
  9436. }
  9437. /* fall-through */
  9438. case MISDN_CALLING:
  9439. case MISDN_ALERTING:
  9440. case MISDN_PROCEEDING:
  9441. case MISDN_CALLING_ACKNOWLEDGE:
  9442. if (bc->nt) {
  9443. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  9444. hanguptone_indicate(ch);
  9445. }
  9446. bc->out_cause = AST_CAUSE_UNALLOCATED;
  9447. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  9448. break;
  9449. case MISDN_WAITING4DIGS:
  9450. if (bc->nt) {
  9451. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  9452. bc->out_cause = AST_CAUSE_UNALLOCATED;
  9453. hanguptone_indicate(ch);
  9454. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  9455. } else {
  9456. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  9457. misdn_lib_send_event(bc, EVENT_RELEASE);
  9458. }
  9459. break;
  9460. case MISDN_CLEANING:
  9461. chan_misdn_log(1, bc->port, " --> in state cleaning .. so ignoring, the stack should clean it for us\n");
  9462. break;
  9463. default:
  9464. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  9465. break;
  9466. }
  9467. break;
  9468. /****************************/
  9469. /** Supplementary Services **/
  9470. /****************************/
  9471. case EVENT_RETRIEVE:
  9472. if (!ch) {
  9473. chan_misdn_log(4, bc->port, " --> no CH, searching for held call\n");
  9474. ch = find_hold_call_l3(bc->l3_id);
  9475. if (!ch || ch->hold.state != MISDN_HOLD_ACTIVE) {
  9476. ast_log(LOG_WARNING, "No held call found, cannot Retrieve\n");
  9477. misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT);
  9478. break;
  9479. }
  9480. }
  9481. /* remember the channel again */
  9482. ch->bc = bc;
  9483. ch->hold.state = MISDN_HOLD_IDLE;
  9484. ch->hold.port = 0;
  9485. ch->hold.channel = 0;
  9486. ast_queue_control(ch->ast, AST_CONTROL_UNHOLD);
  9487. if (misdn_lib_send_event(bc, EVENT_RETRIEVE_ACKNOWLEDGE) < 0) {
  9488. chan_misdn_log(4, bc->port, " --> RETRIEVE_ACK failed\n");
  9489. misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT);
  9490. }
  9491. break;
  9492. case EVENT_HOLD:
  9493. {
  9494. int hold_allowed;
  9495. struct ast_channel *bridged;
  9496. misdn_cfg_get(bc->port, MISDN_CFG_HOLD_ALLOWED, &hold_allowed, sizeof(hold_allowed));
  9497. if (!hold_allowed) {
  9498. chan_misdn_log(-1, bc->port, "Hold not allowed this port.\n");
  9499. misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
  9500. break;
  9501. }
  9502. bridged = ast_bridged_channel(ch->ast);
  9503. if (bridged) {
  9504. chan_misdn_log(2, bc->port, "Bridge Partner is of type: %s\n", bridged->tech->type);
  9505. ch->l3id = bc->l3_id;
  9506. /* forget the channel now */
  9507. ch->bc = NULL;
  9508. ch->hold.state = MISDN_HOLD_ACTIVE;
  9509. ch->hold.port = bc->port;
  9510. ch->hold.channel = bc->channel;
  9511. ast_queue_control(ch->ast, AST_CONTROL_HOLD);
  9512. misdn_lib_send_event(bc, EVENT_HOLD_ACKNOWLEDGE);
  9513. } else {
  9514. misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
  9515. chan_misdn_log(0, bc->port, "We aren't bridged to anybody\n");
  9516. }
  9517. break;
  9518. }
  9519. case EVENT_NOTIFY:
  9520. if (bc->redirecting.to_changed) {
  9521. /* Add configured prefix to redirecting.to.number */
  9522. misdn_add_number_prefix(bc->port, bc->redirecting.to.number_type,
  9523. bc->redirecting.to.number, sizeof(bc->redirecting.to.number));
  9524. }
  9525. switch (bc->notify_description_code) {
  9526. case mISDN_NOTIFY_CODE_DIVERSION_ACTIVATED:
  9527. /* Ignore for now. */
  9528. bc->redirecting.to_changed = 0;
  9529. break;
  9530. case mISDN_NOTIFY_CODE_CALL_IS_DIVERTING:
  9531. if (!bc->redirecting.to_changed) {
  9532. break;
  9533. }
  9534. bc->redirecting.to_changed = 0;
  9535. if (!ch || !ch->ast) {
  9536. break;
  9537. }
  9538. switch (ch->state) {
  9539. case MISDN_ALERTING:
  9540. /* Call is deflecting after we have seen an ALERTING message */
  9541. bc->redirecting.reason = mISDN_REDIRECTING_REASON_NO_REPLY;
  9542. break;
  9543. default:
  9544. /* Call is deflecting for call forwarding unconditional or busy reason. */
  9545. bc->redirecting.reason = mISDN_REDIRECTING_REASON_UNKNOWN;
  9546. break;
  9547. }
  9548. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  9549. ast_channel_queue_redirecting_update(ch->ast, &ch->ast->redirecting, NULL);
  9550. break;
  9551. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ALERTING:
  9552. /*
  9553. * It would be preferable to update the connected line information
  9554. * only when the message callStatus is active. However, the
  9555. * optional redirection number may not be present in the active
  9556. * message if an alerting message were received earlier.
  9557. *
  9558. * The consequences if we wind up sending two updates is benign.
  9559. * The other end will think that it got transferred twice.
  9560. */
  9561. if (!bc->redirecting.to_changed) {
  9562. break;
  9563. }
  9564. bc->redirecting.to_changed = 0;
  9565. if (!ch || !ch->ast) {
  9566. break;
  9567. }
  9568. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  9569. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING,
  9570. bc->incoming_cid_tag);
  9571. break;
  9572. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE:
  9573. if (!bc->redirecting.to_changed) {
  9574. break;
  9575. }
  9576. bc->redirecting.to_changed = 0;
  9577. if (!ch || !ch->ast) {
  9578. break;
  9579. }
  9580. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  9581. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER, bc->incoming_cid_tag);
  9582. break;
  9583. default:
  9584. bc->redirecting.to_changed = 0;
  9585. chan_misdn_log(0, bc->port," --> not yet handled: notify code:0x%02X\n",
  9586. bc->notify_description_code);
  9587. break;
  9588. }
  9589. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  9590. break;
  9591. case EVENT_FACILITY:
  9592. if (bc->fac_in.Function == Fac_None) {
  9593. /* This is a FACILITY message so we MUST have a facility ie */
  9594. chan_misdn_log(0, bc->port," --> Missing facility ie or unknown facility ie contents.\n");
  9595. } else {
  9596. misdn_facility_ie_handler(event, bc, ch);
  9597. }
  9598. /* In case it came in on a FACILITY message and we did not handle it. */
  9599. bc->redirecting.to_changed = 0;
  9600. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  9601. break;
  9602. case EVENT_RESTART:
  9603. if (!bc->dummy) {
  9604. stop_bc_tones(ch);
  9605. release_chan(ch, bc);
  9606. }
  9607. break;
  9608. default:
  9609. chan_misdn_log(1, 0, "Got Unknown Event\n");
  9610. break;
  9611. }
  9612. if (ch) {
  9613. chan_list_unref(ch, "cb_event complete OK");
  9614. }
  9615. return RESPONSE_OK;
  9616. }
  9617. /** TE STUFF END **/
  9618. #if defined(AST_MISDN_ENHANCEMENTS)
  9619. /*!
  9620. * \internal
  9621. * \brief Get call completion record information.
  9622. *
  9623. * \param chan Asterisk channel to operate upon. (Not used)
  9624. * \param function_name Name of the function that called us.
  9625. * \param function_args Argument string passed to function (Could be NULL)
  9626. * \param buf Buffer to put returned string.
  9627. * \param size Size of the supplied buffer including the null terminator.
  9628. *
  9629. * \retval 0 on success.
  9630. * \retval -1 on error.
  9631. */
  9632. static int misdn_cc_read(struct ast_channel *chan, const char *function_name,
  9633. char *function_args, char *buf, size_t size)
  9634. {
  9635. char *parse;
  9636. struct misdn_cc_record *cc_record;
  9637. AST_DECLARE_APP_ARGS(args,
  9638. AST_APP_ARG(cc_id); /* Call completion record ID value. */
  9639. AST_APP_ARG(get_name); /* Name of what to get */
  9640. AST_APP_ARG(other); /* Any extraneous garbage arguments */
  9641. );
  9642. /* Ensure that the buffer is empty */
  9643. *buf = 0;
  9644. if (ast_strlen_zero(function_args)) {
  9645. ast_log(LOG_ERROR, "Function '%s' requires arguments.\n", function_name);
  9646. return -1;
  9647. }
  9648. parse = ast_strdupa(function_args);
  9649. AST_STANDARD_APP_ARGS(args, parse);
  9650. if (!args.argc || ast_strlen_zero(args.cc_id)) {
  9651. ast_log(LOG_ERROR, "Function '%s' missing call completion record ID.\n",
  9652. function_name);
  9653. return -1;
  9654. }
  9655. if (!isdigit(*args.cc_id)) {
  9656. ast_log(LOG_ERROR, "Function '%s' call completion record ID must be numeric.\n",
  9657. function_name);
  9658. return -1;
  9659. }
  9660. if (ast_strlen_zero(args.get_name)) {
  9661. ast_log(LOG_ERROR, "Function '%s' missing what-to-get parameter.\n",
  9662. function_name);
  9663. return -1;
  9664. }
  9665. AST_LIST_LOCK(&misdn_cc_records_db);
  9666. cc_record = misdn_cc_find_by_id(atoi(args.cc_id));
  9667. if (cc_record) {
  9668. if (!strcasecmp("a-all", args.get_name)) {
  9669. snprintf(buf, size, "\"%s\" <%s>", cc_record->redial.caller.name,
  9670. cc_record->redial.caller.number);
  9671. } else if (!strcasecmp("a-name", args.get_name)) {
  9672. ast_copy_string(buf, cc_record->redial.caller.name, size);
  9673. } else if (!strncasecmp("a-num", args.get_name, 5)) {
  9674. ast_copy_string(buf, cc_record->redial.caller.number, size);
  9675. } else if (!strcasecmp("a-ton", args.get_name)) {
  9676. snprintf(buf, size, "%d",
  9677. misdn_to_ast_plan(cc_record->redial.caller.number_plan)
  9678. | misdn_to_ast_ton(cc_record->redial.caller.number_type));
  9679. } else if (!strncasecmp("a-pres", args.get_name, 6)) {
  9680. ast_copy_string(buf, ast_named_caller_presentation(
  9681. misdn_to_ast_pres(cc_record->redial.caller.presentation)
  9682. | misdn_to_ast_screen(cc_record->redial.caller.screening)), size);
  9683. } else if (!strcasecmp("a-busy", args.get_name)) {
  9684. ast_copy_string(buf, cc_record->party_a_free ? "no" : "yes", size);
  9685. } else if (!strncasecmp("b-num", args.get_name, 5)) {
  9686. ast_copy_string(buf, cc_record->redial.dialed.number, size);
  9687. } else if (!strcasecmp("b-ton", args.get_name)) {
  9688. snprintf(buf, size, "%d",
  9689. misdn_to_ast_plan(cc_record->redial.dialed.number_plan)
  9690. | misdn_to_ast_ton(cc_record->redial.dialed.number_type));
  9691. } else if (!strcasecmp("port", args.get_name)) {
  9692. snprintf(buf, size, "%d", cc_record->port);
  9693. } else if (!strcasecmp("available-notify-priority", args.get_name)) {
  9694. snprintf(buf, size, "%d", cc_record->remote_user_free.priority);
  9695. } else if (!strcasecmp("available-notify-exten", args.get_name)) {
  9696. ast_copy_string(buf, cc_record->remote_user_free.exten, size);
  9697. } else if (!strcasecmp("available-notify-context", args.get_name)) {
  9698. ast_copy_string(buf, cc_record->remote_user_free.context, size);
  9699. } else if (!strcasecmp("busy-notify-priority", args.get_name)) {
  9700. snprintf(buf, size, "%d", cc_record->b_free.priority);
  9701. } else if (!strcasecmp("busy-notify-exten", args.get_name)) {
  9702. ast_copy_string(buf, cc_record->b_free.exten, size);
  9703. } else if (!strcasecmp("busy-notify-context", args.get_name)) {
  9704. ast_copy_string(buf, cc_record->b_free.context, size);
  9705. } else {
  9706. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9707. ast_log(LOG_ERROR, "Function '%s': Unknown what-to-get '%s'.\n", function_name, args.get_name);
  9708. return -1;
  9709. }
  9710. }
  9711. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9712. return 0;
  9713. }
  9714. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9715. #if defined(AST_MISDN_ENHANCEMENTS)
  9716. static struct ast_custom_function misdn_cc_function = {
  9717. .name = "mISDN_CC",
  9718. .synopsis = "Get call completion record information.",
  9719. .syntax = "mISDN_CC(${MISDN_CC_RECORD_ID},<what-to-get>)",
  9720. .desc =
  9721. "mISDN_CC(${MISDN_CC_RECORD_ID},<what-to-get>)\n"
  9722. "The following can be retrieved:\n"
  9723. "\"a-num\", \"a-name\", \"a-all\", \"a-ton\", \"a-pres\", \"a-busy\",\n"
  9724. "\"b-num\", \"b-ton\", \"port\",\n"
  9725. " User-A is available for call completion:\n"
  9726. " \"available-notify-priority\",\n"
  9727. " \"available-notify-exten\",\n"
  9728. " \"available-notify-context\",\n"
  9729. " User-A is busy:\n"
  9730. " \"busy-notify-priority\",\n"
  9731. " \"busy-notify-exten\",\n"
  9732. " \"busy-notify-context\"\n",
  9733. .read = misdn_cc_read,
  9734. };
  9735. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9736. /******************************************
  9737. *
  9738. * Asterisk Channel Endpoint END
  9739. *
  9740. *
  9741. *******************************************/
  9742. static int unload_module(void)
  9743. {
  9744. /* First, take us out of the channel loop */
  9745. ast_log(LOG_VERBOSE, "-- Unregistering mISDN Channel Driver --\n");
  9746. misdn_tasks_destroy();
  9747. if (!g_config_initialized) {
  9748. return 0;
  9749. }
  9750. ast_cli_unregister_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
  9751. /* ast_unregister_application("misdn_crypt"); */
  9752. ast_unregister_application("misdn_set_opt");
  9753. ast_unregister_application("misdn_facility");
  9754. ast_unregister_application("misdn_check_l2l1");
  9755. #if defined(AST_MISDN_ENHANCEMENTS)
  9756. ast_unregister_application(misdn_command_name);
  9757. ast_custom_function_unregister(&misdn_cc_function);
  9758. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9759. ast_channel_unregister(&misdn_tech);
  9760. free_robin_list();
  9761. misdn_cfg_destroy();
  9762. misdn_lib_destroy();
  9763. ast_free(misdn_out_calls);
  9764. ast_free(misdn_in_calls);
  9765. ast_free(misdn_debug_only);
  9766. ast_free(misdn_ports);
  9767. ast_free(misdn_debug);
  9768. #if defined(AST_MISDN_ENHANCEMENTS)
  9769. misdn_cc_destroy();
  9770. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9771. return 0;
  9772. }
  9773. static int load_module(void)
  9774. {
  9775. int i, port;
  9776. int ntflags = 0, ntkc = 0;
  9777. char ports[256] = "";
  9778. char tempbuf[BUFFERSIZE + 1];
  9779. char ntfile[BUFFERSIZE + 1];
  9780. struct misdn_lib_iface iface = {
  9781. .cb_event = cb_events,
  9782. .cb_log = chan_misdn_log,
  9783. .cb_jb_empty = chan_misdn_jb_empty,
  9784. };
  9785. max_ports = misdn_lib_maxports_get();
  9786. if (max_ports <= 0) {
  9787. ast_log(LOG_ERROR, "Unable to initialize mISDN\n");
  9788. return AST_MODULE_LOAD_DECLINE;
  9789. }
  9790. if (misdn_cfg_init(max_ports, 0)) {
  9791. ast_log(LOG_ERROR, "Unable to initialize misdn_config.\n");
  9792. return AST_MODULE_LOAD_DECLINE;
  9793. }
  9794. g_config_initialized = 1;
  9795. #if defined(AST_MISDN_ENHANCEMENTS)
  9796. misdn_cc_init();
  9797. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9798. misdn_debug = ast_malloc(sizeof(int) * (max_ports + 1));
  9799. if (!misdn_debug) {
  9800. ast_log(LOG_ERROR, "Out of memory for misdn_debug\n");
  9801. return AST_MODULE_LOAD_DECLINE;
  9802. }
  9803. misdn_ports = ast_malloc(sizeof(int) * (max_ports + 1));
  9804. if (!misdn_ports) {
  9805. ast_free(misdn_debug);
  9806. ast_log(LOG_ERROR, "Out of memory for misdn_ports\n");
  9807. return AST_MODULE_LOAD_DECLINE;
  9808. }
  9809. misdn_cfg_get(0, MISDN_GEN_DEBUG, &misdn_debug[0], sizeof(misdn_debug[0]));
  9810. for (i = 1; i <= max_ports; i++) {
  9811. misdn_debug[i] = misdn_debug[0];
  9812. misdn_ports[i] = i;
  9813. }
  9814. *misdn_ports = 0;
  9815. misdn_debug_only = ast_calloc(max_ports + 1, sizeof(int));
  9816. if (!misdn_debug_only) {
  9817. ast_free(misdn_ports);
  9818. ast_free(misdn_debug);
  9819. ast_log(LOG_ERROR, "Out of memory for misdn_debug_only\n");
  9820. return AST_MODULE_LOAD_DECLINE;
  9821. }
  9822. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, tempbuf, sizeof(tempbuf));
  9823. if (!ast_strlen_zero(tempbuf)) {
  9824. tracing = 1;
  9825. }
  9826. misdn_in_calls = ast_malloc(sizeof(int) * (max_ports + 1));
  9827. if (!misdn_in_calls) {
  9828. ast_free(misdn_debug_only);
  9829. ast_free(misdn_ports);
  9830. ast_free(misdn_debug);
  9831. ast_log(LOG_ERROR, "Out of memory for misdn_in_calls\n");
  9832. return AST_MODULE_LOAD_DECLINE;
  9833. }
  9834. misdn_out_calls = ast_malloc(sizeof(int) * (max_ports + 1));
  9835. if (!misdn_out_calls) {
  9836. ast_free(misdn_in_calls);
  9837. ast_free(misdn_debug_only);
  9838. ast_free(misdn_ports);
  9839. ast_free(misdn_debug);
  9840. ast_log(LOG_ERROR, "Out of memory for misdn_out_calls\n");
  9841. return AST_MODULE_LOAD_DECLINE;
  9842. }
  9843. for (i = 1; i <= max_ports; i++) {
  9844. misdn_in_calls[i] = 0;
  9845. misdn_out_calls[i] = 0;
  9846. }
  9847. ast_mutex_init(&cl_te_lock);
  9848. ast_mutex_init(&release_lock);
  9849. misdn_cfg_update_ptp();
  9850. misdn_cfg_get_ports_string(ports);
  9851. if (!ast_strlen_zero(ports)) {
  9852. chan_misdn_log(0, 0, "Got: %s from get_ports\n", ports);
  9853. }
  9854. if (misdn_lib_init(ports, &iface, NULL)) {
  9855. chan_misdn_log(0, 0, "No te ports initialized\n");
  9856. }
  9857. misdn_cfg_get(0, MISDN_GEN_NTDEBUGFLAGS, &ntflags, sizeof(ntflags));
  9858. misdn_cfg_get(0, MISDN_GEN_NTDEBUGFILE, &ntfile, sizeof(ntfile));
  9859. misdn_cfg_get(0, MISDN_GEN_NTKEEPCALLS, &ntkc, sizeof(ntkc));
  9860. misdn_lib_nt_keepcalls(ntkc);
  9861. misdn_lib_nt_debug_init(ntflags, ntfile);
  9862. if (ast_channel_register(&misdn_tech)) {
  9863. ast_log(LOG_ERROR, "Unable to register channel class %s\n", misdn_type);
  9864. unload_module();
  9865. return AST_MODULE_LOAD_DECLINE;
  9866. }
  9867. ast_cli_register_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
  9868. ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_opt",
  9869. "misdn_set_opt(:<opt><optarg>:<opt><optarg>...):\n"
  9870. "Sets mISDN opts. and optargs\n"
  9871. "\n"
  9872. "The available options are:\n"
  9873. " a - Have Asterisk detect DTMF tones on called channel\n"
  9874. " c - Make crypted outgoing call, optarg is keyindex\n"
  9875. " d - Send display text to called phone, text is the optarg\n"
  9876. " e - Perform echo cancellation on this channel,\n"
  9877. " takes taps as optarg (32,64,128,256)\n"
  9878. " e! - Disable echo cancellation on this channel\n"
  9879. " f - Enable fax detection\n"
  9880. " h - Make digital outgoing call\n"
  9881. " h1 - Make HDLC mode digital outgoing call\n"
  9882. " i - Ignore detected DTMF tones, don't signal them to Asterisk,\n"
  9883. " they will be transported inband.\n"
  9884. " jb - Set jitter buffer length, optarg is length\n"
  9885. " jt - Set jitter buffer upper threshold, optarg is threshold\n"
  9886. " jn - Disable jitter buffer\n"
  9887. " n - Disable mISDN DSP on channel.\n"
  9888. " Disables: echo cancel, DTMF detection, and volume control.\n"
  9889. " p - Caller ID presentation,\n"
  9890. " optarg is either 'allowed' or 'restricted'\n"
  9891. " s - Send Non-inband DTMF as inband\n"
  9892. " vr - Rx gain control, optarg is gain\n"
  9893. " vt - Tx gain control, optarg is gain\n"
  9894. );
  9895. ast_register_application("misdn_facility", misdn_facility_exec, "misdn_facility",
  9896. "misdn_facility(<FACILITY_TYPE>|<ARG1>|..)\n"
  9897. "Sends the Facility Message FACILITY_TYPE with \n"
  9898. "the given Arguments to the current ISDN Channel\n"
  9899. "Supported Facilities are:\n"
  9900. "\n"
  9901. "type=calldeflect args=Nr where to deflect\n"
  9902. #if defined(AST_MISDN_ENHANCEMENTS)
  9903. "type=callrerouting args=Nr where to deflect\n"
  9904. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9905. );
  9906. ast_register_application("misdn_check_l2l1", misdn_check_l2l1, "misdn_check_l2l1",
  9907. "misdn_check_l2l1(<port>||g:<groupname>,timeout)\n"
  9908. "Checks if the L2 and L1 are up on either the given <port> or\n"
  9909. "on the ports in the group with <groupname>\n"
  9910. "If the L1/L2 are down, check_l2l1 gets up the L1/L2 and waits\n"
  9911. "for <timeout> seconds that this happens. Otherwise, nothing happens\n"
  9912. "\n"
  9913. "This application, ensures the L1/L2 state of the Ports in a group\n"
  9914. "it is intended to make the pmp_l1_check option redundant and to\n"
  9915. "fix a buggy switch config from your provider\n"
  9916. "\n"
  9917. "a sample dialplan would look like:\n\n"
  9918. "exten => _X.,1,misdn_check_l2l1(g:out|2)\n"
  9919. "exten => _X.,n,dial(mISDN/g:out/${EXTEN})\n"
  9920. );
  9921. #if defined(AST_MISDN_ENHANCEMENTS)
  9922. ast_register_application(misdn_command_name, misdn_command_exec, misdn_command_name,
  9923. "misdn_command(<command>[,<options>])\n"
  9924. "The following commands are defined:\n"
  9925. "cc-initialize\n"
  9926. " Setup mISDN support for call completion\n"
  9927. " Must call before doing any Dial() involving call completion.\n"
  9928. "ccnr-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>\n"
  9929. " Request Call Completion No Reply activation\n"
  9930. "ccbs-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>\n"
  9931. " Request Call Completion Busy Subscriber activation\n"
  9932. "cc-b-free,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>\n"
  9933. " Set the dialplan location to notify when User-B is available but User-A is busy.\n"
  9934. " Setting this dialplan location is optional.\n"
  9935. "cc-a-busy,${MISDN_CC_RECORD_ID},<yes/no>\n"
  9936. " Set the busy status of call completion User-A\n"
  9937. "cc-deactivate,${MISDN_CC_RECORD_ID}\n"
  9938. " Deactivate the identified call completion request\n"
  9939. "\n"
  9940. "MISDN_CC_RECORD_ID is set when Dial() returns and call completion is possible\n"
  9941. "MISDN_CC_STATUS is set to ACTIVATED or ERROR after the call completion\n"
  9942. "activation request.\n"
  9943. "MISDN_ERROR_MSG is set to a descriptive message on error.\n"
  9944. );
  9945. ast_custom_function_register(&misdn_cc_function);
  9946. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9947. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, sizeof(global_tracefile));
  9948. /* start the l1 watchers */
  9949. for (port = misdn_cfg_get_next_port(0); port >= 0; port = misdn_cfg_get_next_port(port)) {
  9950. int l1timeout;
  9951. misdn_cfg_get(port, MISDN_CFG_L1_TIMEOUT, &l1timeout, sizeof(l1timeout));
  9952. if (l1timeout) {
  9953. chan_misdn_log(4, 0, "Adding L1watcher task: port:%d timeout:%ds\n", port, l1timeout);
  9954. misdn_tasks_add(l1timeout * 1000, misdn_l1_task, &misdn_ports[port]);
  9955. }
  9956. }
  9957. chan_misdn_log(0, 0, "-- mISDN Channel Driver Registered --\n");
  9958. return 0;
  9959. }
  9960. static int reload(void)
  9961. {
  9962. reload_config();
  9963. return 0;
  9964. }
  9965. /*** SOME APPS ;)***/
  9966. #if defined(AST_MISDN_ENHANCEMENTS)
  9967. /*!
  9968. * \brief misdn_command arguments container.
  9969. */
  9970. AST_DEFINE_APP_ARGS_TYPE(misdn_command_args,
  9971. AST_APP_ARG(name); /* Subcommand name */
  9972. AST_APP_ARG(arg)[10 + 1]; /* Subcommand arguments */
  9973. );
  9974. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9975. #if defined(AST_MISDN_ENHANCEMENTS)
  9976. static void misdn_cc_caller_destroy(void *obj)
  9977. {
  9978. /* oh snap! */
  9979. }
  9980. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9981. #if defined(AST_MISDN_ENHANCEMENTS)
  9982. static struct misdn_cc_caller *misdn_cc_caller_alloc(struct ast_channel *chan)
  9983. {
  9984. struct misdn_cc_caller *cc_caller;
  9985. if (!(cc_caller = ao2_alloc(sizeof(*cc_caller), misdn_cc_caller_destroy))) {
  9986. return NULL;
  9987. }
  9988. cc_caller->chan = chan;
  9989. return cc_caller;
  9990. }
  9991. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9992. #if defined(AST_MISDN_ENHANCEMENTS)
  9993. /*!
  9994. * \internal
  9995. * \brief misdn_command(cc-initialize) subcommand handler
  9996. *
  9997. * \param chan Asterisk channel to operate upon.
  9998. * \param subcommand Arguments for the subcommand
  9999. *
  10000. * \retval 0 on success.
  10001. * \retval -1 on error.
  10002. */
  10003. static int misdn_command_cc_initialize(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10004. {
  10005. struct misdn_cc_caller *cc_caller;
  10006. struct ast_datastore *datastore;
  10007. if (!(cc_caller = misdn_cc_caller_alloc(chan))) {
  10008. return -1;
  10009. }
  10010. if (!(datastore = ast_datastore_alloc(&misdn_cc_ds_info, NULL))) {
  10011. ao2_ref(cc_caller, -1);
  10012. return -1;
  10013. }
  10014. ast_channel_lock(chan);
  10015. /* Inherit reference */
  10016. datastore->data = cc_caller;
  10017. cc_caller = NULL;
  10018. datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  10019. ast_channel_datastore_add(chan, datastore);
  10020. ast_channel_unlock(chan);
  10021. return 0;
  10022. }
  10023. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10024. #if defined(AST_MISDN_ENHANCEMENTS)
  10025. /*!
  10026. * \internal
  10027. * \brief misdn_command(cc-deactivate) subcommand handler
  10028. *
  10029. * \details
  10030. * misdn_command(cc-deactivate,${MISDN_CC_RECORD_ID})
  10031. * Deactivate a call completion service instance.
  10032. *
  10033. * \param chan Asterisk channel to operate upon.
  10034. * \param subcommand Arguments for the subcommand
  10035. *
  10036. * \retval 0 on success.
  10037. * \retval -1 on error.
  10038. */
  10039. static int misdn_command_cc_deactivate(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10040. {
  10041. long record_id;
  10042. const char *error_str;
  10043. struct misdn_cc_record *cc_record;
  10044. struct misdn_bchannel *bc;
  10045. struct misdn_bchannel dummy;
  10046. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID})\n";
  10047. if (ast_strlen_zero(subcommand->arg[0]) || !isdigit(*subcommand->arg[0])) {
  10048. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10049. return -1;
  10050. }
  10051. record_id = atol(subcommand->arg[0]);
  10052. AST_LIST_LOCK(&misdn_cc_records_db);
  10053. cc_record = misdn_cc_find_by_id(record_id);
  10054. if (cc_record && 0 <= cc_record->port) {
  10055. if (cc_record->ptp) {
  10056. if (cc_record->mode.ptp.bc) {
  10057. /* Close the call-completion signaling link */
  10058. bc = cc_record->mode.ptp.bc;
  10059. bc->fac_out.Function = Fac_None;
  10060. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  10061. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  10062. }
  10063. misdn_cc_delete(cc_record);
  10064. } else if (cc_record->activated) {
  10065. cc_record->error_code = FacError_None;
  10066. cc_record->reject_code = FacReject_None;
  10067. cc_record->invoke_id = ++misdn_invoke_id;
  10068. cc_record->outstanding_message = 1;
  10069. /* Build message */
  10070. misdn_make_dummy(&dummy, cc_record->port, 0, misdn_lib_port_is_nt(cc_record->port), 0);
  10071. dummy.fac_out.Function = Fac_CCBSDeactivate;
  10072. dummy.fac_out.u.CCBSDeactivate.InvokeID = cc_record->invoke_id;
  10073. dummy.fac_out.u.CCBSDeactivate.ComponentType = FacComponent_Invoke;
  10074. dummy.fac_out.u.CCBSDeactivate.Component.Invoke.CCBSReference = cc_record->mode.ptmp.reference_id;
  10075. /* Send message */
  10076. print_facility(&dummy.fac_out, &dummy);
  10077. misdn_lib_send_event(&dummy, EVENT_FACILITY);
  10078. }
  10079. }
  10080. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10081. /* Wait for the response to the call completion deactivation request. */
  10082. misdn_cc_response_wait(chan, MISDN_CC_REQUEST_WAIT_MAX, record_id);
  10083. AST_LIST_LOCK(&misdn_cc_records_db);
  10084. cc_record = misdn_cc_find_by_id(record_id);
  10085. if (cc_record) {
  10086. if (cc_record->port < 0) {
  10087. /* The network did not tell us that call completion was available. */
  10088. error_str = NULL;
  10089. } else if (cc_record->outstanding_message) {
  10090. cc_record->outstanding_message = 0;
  10091. error_str = misdn_no_response_from_network;
  10092. } else if (cc_record->reject_code != FacReject_None) {
  10093. error_str = misdn_to_str_reject_code(cc_record->reject_code);
  10094. } else if (cc_record->error_code != FacError_None) {
  10095. error_str = misdn_to_str_error_code(cc_record->error_code);
  10096. } else {
  10097. error_str = NULL;
  10098. }
  10099. misdn_cc_delete(cc_record);
  10100. } else {
  10101. error_str = NULL;
  10102. }
  10103. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10104. if (error_str) {
  10105. ast_verb(1, "%s(%s) diagnostic '%s' on channel %s\n",
  10106. misdn_command_name, subcommand->name, error_str, chan->name);
  10107. pbx_builtin_setvar_helper(chan, MISDN_ERROR_MSG, error_str);
  10108. }
  10109. return 0;
  10110. }
  10111. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10112. #if defined(AST_MISDN_ENHANCEMENTS)
  10113. /*!
  10114. * \internal
  10115. * \brief misdn_command(cc-a-busy) subcommand handler
  10116. *
  10117. * \details
  10118. * misdn_command(cc-a-busy,${MISDN_CC_RECORD_ID},<yes/no>)
  10119. * Set the status of User-A for a call completion service instance.
  10120. *
  10121. * \param chan Asterisk channel to operate upon.
  10122. * \param subcommand Arguments for the subcommand
  10123. *
  10124. * \retval 0 on success.
  10125. * \retval -1 on error.
  10126. */
  10127. static int misdn_command_cc_a_busy(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10128. {
  10129. long record_id;
  10130. int party_a_free;
  10131. struct misdn_cc_record *cc_record;
  10132. struct misdn_bchannel *bc;
  10133. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<yes/no>)\n";
  10134. if (ast_strlen_zero(subcommand->arg[0]) || !isdigit(*subcommand->arg[0])) {
  10135. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10136. return -1;
  10137. }
  10138. record_id = atol(subcommand->arg[0]);
  10139. if (ast_true(subcommand->arg[1])) {
  10140. party_a_free = 0;
  10141. } else if (ast_false(subcommand->arg[1])) {
  10142. party_a_free = 1;
  10143. } else {
  10144. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10145. return -1;
  10146. }
  10147. AST_LIST_LOCK(&misdn_cc_records_db);
  10148. cc_record = misdn_cc_find_by_id(record_id);
  10149. if (cc_record && cc_record->party_a_free != party_a_free) {
  10150. /* User-A's status has changed */
  10151. cc_record->party_a_free = party_a_free;
  10152. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  10153. cc_record->error_code = FacError_None;
  10154. cc_record->reject_code = FacReject_None;
  10155. /* Build message */
  10156. bc = cc_record->mode.ptp.bc;
  10157. if (cc_record->party_a_free) {
  10158. bc->fac_out.Function = Fac_CCBS_T_Resume;
  10159. bc->fac_out.u.CCBS_T_Resume.InvokeID = ++misdn_invoke_id;
  10160. } else {
  10161. bc->fac_out.Function = Fac_CCBS_T_Suspend;
  10162. bc->fac_out.u.CCBS_T_Suspend.InvokeID = ++misdn_invoke_id;
  10163. }
  10164. /* Send message */
  10165. print_facility(&bc->fac_out, bc);
  10166. misdn_lib_send_event(bc, EVENT_FACILITY);
  10167. }
  10168. }
  10169. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10170. return 0;
  10171. }
  10172. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10173. #if defined(AST_MISDN_ENHANCEMENTS)
  10174. /*!
  10175. * \internal
  10176. * \brief misdn_command(cc-b-free) subcommand handler
  10177. *
  10178. * \details
  10179. * misdn_command(cc-b-free,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10180. * Set the dialplan location to notify when User-B is free and User-A is busy.
  10181. *
  10182. * \param chan Asterisk channel to operate upon.
  10183. * \param subcommand Arguments for the subcommand
  10184. *
  10185. * \retval 0 on success.
  10186. * \retval -1 on error.
  10187. */
  10188. static int misdn_command_cc_b_free(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10189. {
  10190. unsigned index;
  10191. long record_id;
  10192. int priority;
  10193. char *context;
  10194. char *exten;
  10195. struct misdn_cc_record *cc_record;
  10196. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)\n";
  10197. /* Check that all arguments are present */
  10198. for (index = 0; index < 4; ++index) {
  10199. if (ast_strlen_zero(subcommand->arg[index])) {
  10200. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10201. return -1;
  10202. }
  10203. }
  10204. /* These must be numeric */
  10205. if (!isdigit(*subcommand->arg[0]) || !isdigit(*subcommand->arg[3])) {
  10206. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10207. return -1;
  10208. }
  10209. record_id = atol(subcommand->arg[0]);
  10210. context = subcommand->arg[1];
  10211. exten = subcommand->arg[2];
  10212. priority = atoi(subcommand->arg[3]);
  10213. AST_LIST_LOCK(&misdn_cc_records_db);
  10214. cc_record = misdn_cc_find_by_id(record_id);
  10215. if (cc_record) {
  10216. /* Save User-B free information */
  10217. ast_copy_string(cc_record->b_free.context, context, sizeof(cc_record->b_free.context));
  10218. ast_copy_string(cc_record->b_free.exten, exten, sizeof(cc_record->b_free.exten));
  10219. cc_record->b_free.priority = priority;
  10220. }
  10221. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10222. return 0;
  10223. }
  10224. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10225. #if defined(AST_MISDN_ENHANCEMENTS)
  10226. struct misdn_cc_request {
  10227. enum FacFunction ptmp;
  10228. enum FacFunction ptp;
  10229. };
  10230. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10231. #if defined(AST_MISDN_ENHANCEMENTS)
  10232. /*!
  10233. * \internal
  10234. * \brief misdn_command(ccbs-request/ccnr-request) subcommand handler helper
  10235. *
  10236. * \details
  10237. * misdn_command(ccbs-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10238. * misdn_command(ccnr-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10239. * Set the dialplan location to notify when User-B is free and User-A is free.
  10240. *
  10241. * \param chan Asterisk channel to operate upon.
  10242. * \param subcommand Arguments for the subcommand
  10243. * \param request Which call-completion request message to generate.
  10244. *
  10245. * \retval 0 on success.
  10246. * \retval -1 on error.
  10247. */
  10248. static int misdn_command_cc_request(struct ast_channel *chan, struct misdn_command_args *subcommand, const struct misdn_cc_request *request)
  10249. {
  10250. unsigned index;
  10251. int request_retention;
  10252. long record_id;
  10253. int priority;
  10254. char *context;
  10255. char *exten;
  10256. const char *error_str;
  10257. struct misdn_cc_record *cc_record;
  10258. struct misdn_bchannel *bc;
  10259. struct misdn_bchannel dummy;
  10260. struct misdn_party_id id;
  10261. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)\n";
  10262. /* Check that all arguments are present */
  10263. for (index = 0; index < 4; ++index) {
  10264. if (ast_strlen_zero(subcommand->arg[index])) {
  10265. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10266. return -1;
  10267. }
  10268. }
  10269. /* These must be numeric */
  10270. if (!isdigit(*subcommand->arg[0]) || !isdigit(*subcommand->arg[3])) {
  10271. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10272. return -1;
  10273. }
  10274. record_id = atol(subcommand->arg[0]);
  10275. context = subcommand->arg[1];
  10276. exten = subcommand->arg[2];
  10277. priority = atoi(subcommand->arg[3]);
  10278. AST_LIST_LOCK(&misdn_cc_records_db);
  10279. cc_record = misdn_cc_find_by_id(record_id);
  10280. if (cc_record) {
  10281. /* Save User-B free information */
  10282. ast_copy_string(cc_record->remote_user_free.context, context,
  10283. sizeof(cc_record->remote_user_free.context));
  10284. ast_copy_string(cc_record->remote_user_free.exten, exten,
  10285. sizeof(cc_record->remote_user_free.exten));
  10286. cc_record->remote_user_free.priority = priority;
  10287. if (0 <= cc_record->port) {
  10288. if (cc_record->ptp) {
  10289. if (!cc_record->mode.ptp.bc) {
  10290. bc = misdn_lib_get_register_bc(cc_record->port);
  10291. if (bc) {
  10292. cc_record->mode.ptp.bc = bc;
  10293. cc_record->error_code = FacError_None;
  10294. cc_record->reject_code = FacReject_None;
  10295. cc_record->invoke_id = ++misdn_invoke_id;
  10296. cc_record->outstanding_message = 1;
  10297. cc_record->activation_requested = 1;
  10298. misdn_cfg_get(bc->port, MISDN_CFG_CC_REQUEST_RETENTION,
  10299. &request_retention, sizeof(request_retention));
  10300. cc_record->mode.ptp.requested_retention = request_retention ? 1 : 0;
  10301. /* Build message */
  10302. bc->fac_out.Function = request->ptp;
  10303. bc->fac_out.u.CCBS_T_Request.InvokeID = cc_record->invoke_id;
  10304. bc->fac_out.u.CCBS_T_Request.ComponentType = FacComponent_Invoke;
  10305. bc->fac_out.u.CCBS_T_Request.Component.Invoke.Q931ie =
  10306. cc_record->redial.setup_bc_hlc_llc;
  10307. memset(&id, 0, sizeof(id));
  10308. id.number_plan = cc_record->redial.dialed.number_plan;
  10309. id.number_type = cc_record->redial.dialed.number_type;
  10310. ast_copy_string(id.number, cc_record->redial.dialed.number,
  10311. sizeof(id.number));
  10312. misdn_Address_fill(
  10313. &bc->fac_out.u.CCBS_T_Request.Component.Invoke.Destination,
  10314. &id);
  10315. misdn_Address_fill(
  10316. &bc->fac_out.u.CCBS_T_Request.Component.Invoke.Originating,
  10317. &cc_record->redial.caller);
  10318. bc->fac_out.u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1;
  10319. bc->fac_out.u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator =
  10320. (cc_record->redial.caller.presentation != 0) ? 0 : 1;
  10321. bc->fac_out.u.CCBS_T_Request.Component.Invoke.RetentionSupported =
  10322. request_retention ? 1 : 0;
  10323. /* Send message */
  10324. print_facility(&bc->fac_out, bc);
  10325. misdn_lib_send_event(bc, EVENT_REGISTER);
  10326. }
  10327. }
  10328. } else {
  10329. cc_record->error_code = FacError_None;
  10330. cc_record->reject_code = FacReject_None;
  10331. cc_record->invoke_id = ++misdn_invoke_id;
  10332. cc_record->outstanding_message = 1;
  10333. cc_record->activation_requested = 1;
  10334. /* Build message */
  10335. misdn_make_dummy(&dummy, cc_record->port, 0,
  10336. misdn_lib_port_is_nt(cc_record->port), 0);
  10337. dummy.fac_out.Function = request->ptmp;
  10338. dummy.fac_out.u.CCBSRequest.InvokeID = cc_record->invoke_id;
  10339. dummy.fac_out.u.CCBSRequest.ComponentType = FacComponent_Invoke;
  10340. dummy.fac_out.u.CCBSRequest.Component.Invoke.CallLinkageID =
  10341. cc_record->mode.ptmp.linkage_id;
  10342. /* Send message */
  10343. print_facility(&dummy.fac_out, &dummy);
  10344. misdn_lib_send_event(&dummy, EVENT_FACILITY);
  10345. }
  10346. }
  10347. }
  10348. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10349. /* Wait for the response to the call completion request. */
  10350. misdn_cc_response_wait(chan, MISDN_CC_REQUEST_WAIT_MAX, record_id);
  10351. AST_LIST_LOCK(&misdn_cc_records_db);
  10352. cc_record = misdn_cc_find_by_id(record_id);
  10353. if (cc_record) {
  10354. if (!cc_record->activated) {
  10355. if (cc_record->port < 0) {
  10356. /* The network did not tell us that call completion was available. */
  10357. error_str = "No port number";
  10358. } else if (cc_record->outstanding_message) {
  10359. cc_record->outstanding_message = 0;
  10360. error_str = misdn_no_response_from_network;
  10361. } else if (cc_record->reject_code != FacReject_None) {
  10362. error_str = misdn_to_str_reject_code(cc_record->reject_code);
  10363. } else if (cc_record->error_code != FacError_None) {
  10364. error_str = misdn_to_str_error_code(cc_record->error_code);
  10365. } else if (cc_record->ptp) {
  10366. if (cc_record->mode.ptp.bc) {
  10367. error_str = "Call-completion already requested";
  10368. } else {
  10369. error_str = "Could not allocate call-completion signaling link";
  10370. }
  10371. } else {
  10372. /* Should never happen. */
  10373. error_str = "Unexpected error";
  10374. }
  10375. /* No need to keep the call completion record. */
  10376. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  10377. /* Close the call-completion signaling link */
  10378. bc = cc_record->mode.ptp.bc;
  10379. bc->fac_out.Function = Fac_None;
  10380. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  10381. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  10382. }
  10383. misdn_cc_delete(cc_record);
  10384. } else {
  10385. error_str = NULL;
  10386. }
  10387. } else {
  10388. error_str = misdn_cc_record_not_found;
  10389. }
  10390. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10391. if (error_str) {
  10392. ast_verb(1, "%s(%s) diagnostic '%s' on channel %s\n",
  10393. misdn_command_name, subcommand->name, error_str, chan->name);
  10394. pbx_builtin_setvar_helper(chan, MISDN_ERROR_MSG, error_str);
  10395. pbx_builtin_setvar_helper(chan, MISDN_CC_STATUS, "ERROR");
  10396. } else {
  10397. pbx_builtin_setvar_helper(chan, MISDN_CC_STATUS, "ACTIVATED");
  10398. }
  10399. return 0;
  10400. }
  10401. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10402. #if defined(AST_MISDN_ENHANCEMENTS)
  10403. /*!
  10404. * \internal
  10405. * \brief misdn_command(ccbs-request) subcommand handler
  10406. *
  10407. * \details
  10408. * misdn_command(ccbs-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10409. * Set the dialplan location to notify when User-B is free and User-A is free.
  10410. *
  10411. * \param chan Asterisk channel to operate upon.
  10412. * \param subcommand Arguments for the subcommand
  10413. *
  10414. * \retval 0 on success.
  10415. * \retval -1 on error.
  10416. */
  10417. static int misdn_command_ccbs_request(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10418. {
  10419. static const struct misdn_cc_request request = {
  10420. .ptmp = Fac_CCBSRequest,
  10421. .ptp = Fac_CCBS_T_Request
  10422. };
  10423. return misdn_command_cc_request(chan, subcommand, &request);
  10424. }
  10425. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10426. #if defined(AST_MISDN_ENHANCEMENTS)
  10427. /*!
  10428. * \internal
  10429. * \brief misdn_command(ccnr-request) subcommand handler
  10430. *
  10431. * \details
  10432. * misdn_command(ccnr-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10433. * Set the dialplan location to notify when User-B is free and User-A is free.
  10434. *
  10435. * \param chan Asterisk channel to operate upon.
  10436. * \param subcommand Arguments for the subcommand
  10437. *
  10438. * \retval 0 on success.
  10439. * \retval -1 on error.
  10440. */
  10441. static int misdn_command_ccnr_request(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10442. {
  10443. static const struct misdn_cc_request request = {
  10444. .ptmp = Fac_CCNRRequest,
  10445. .ptp = Fac_CCNR_T_Request
  10446. };
  10447. return misdn_command_cc_request(chan, subcommand, &request);
  10448. }
  10449. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10450. #if defined(AST_MISDN_ENHANCEMENTS)
  10451. struct misdn_command_table {
  10452. /*! \brief subcommand name */
  10453. const char *name;
  10454. /*! \brief subcommand handler */
  10455. int (*func)(struct ast_channel *chan, struct misdn_command_args *subcommand);
  10456. /*! \brief TRUE if the subcommand can only be executed on mISDN channels */
  10457. int misdn_only;
  10458. };
  10459. static const struct misdn_command_table misdn_commands[] = {
  10460. /* *INDENT-OFF* */
  10461. /* subcommand-name subcommand-handler mISDN only */
  10462. { "cc-initialize", misdn_command_cc_initialize, 0 },
  10463. { "cc-deactivate", misdn_command_cc_deactivate, 0 },
  10464. { "cc-a-busy", misdn_command_cc_a_busy, 0 },
  10465. { "cc-b-free", misdn_command_cc_b_free, 0 },
  10466. { "ccbs-request", misdn_command_ccbs_request, 0 },
  10467. { "ccnr-request", misdn_command_ccnr_request, 0 },
  10468. /* *INDENT-ON* */
  10469. };
  10470. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10471. #if defined(AST_MISDN_ENHANCEMENTS)
  10472. /*!
  10473. * \internal
  10474. * \brief misdn_command() dialplan application.
  10475. *
  10476. * \param chan Asterisk channel to operate upon.
  10477. * \param data Application options string.
  10478. *
  10479. * \retval 0 on success.
  10480. * \retval -1 on error.
  10481. */
  10482. static int misdn_command_exec(struct ast_channel *chan, const char *data)
  10483. {
  10484. char *parse;
  10485. unsigned index;
  10486. struct misdn_command_args subcommand;
  10487. if (ast_strlen_zero((char *) data)) {
  10488. ast_log(LOG_ERROR, "%s requires arguments\n", misdn_command_name);
  10489. return -1;
  10490. }
  10491. ast_log(LOG_DEBUG, "%s(%s)\n", misdn_command_name, (char *) data);
  10492. parse = ast_strdupa(data);
  10493. AST_STANDARD_APP_ARGS(subcommand, parse);
  10494. if (!subcommand.argc || ast_strlen_zero(subcommand.name)) {
  10495. ast_log(LOG_ERROR, "%s requires a subcommand\n", misdn_command_name);
  10496. return -1;
  10497. }
  10498. for (index = 0; index < ARRAY_LEN(misdn_commands); ++index) {
  10499. if (strcasecmp(misdn_commands[index].name, subcommand.name) == 0) {
  10500. strcpy(subcommand.name, misdn_commands[index].name);
  10501. if (misdn_commands[index].misdn_only
  10502. && strcasecmp(chan->tech->type, misdn_type) != 0) {
  10503. ast_log(LOG_WARNING,
  10504. "%s(%s) only makes sense with %s channels!\n",
  10505. misdn_command_name, subcommand.name, misdn_type);
  10506. return -1;
  10507. }
  10508. return misdn_commands[index].func(chan, &subcommand);
  10509. }
  10510. }
  10511. ast_log(LOG_WARNING, "%s(%s) subcommand is unknown\n", misdn_command_name,
  10512. subcommand.name);
  10513. return -1;
  10514. }
  10515. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10516. static int misdn_facility_exec(struct ast_channel *chan, const char *data)
  10517. {
  10518. struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
  10519. char *parse;
  10520. unsigned max_len;
  10521. AST_DECLARE_APP_ARGS(args,
  10522. AST_APP_ARG(facility_type);
  10523. AST_APP_ARG(arg)[99];
  10524. );
  10525. chan_misdn_log(0, 0, "TYPE: %s\n", chan->tech->type);
  10526. if (strcasecmp(chan->tech->type, misdn_type)) {
  10527. ast_log(LOG_WARNING, "misdn_facility only makes sense with %s channels!\n", misdn_type);
  10528. return -1;
  10529. }
  10530. if (ast_strlen_zero((char *) data)) {
  10531. ast_log(LOG_WARNING, "misdn_facility requires arguments: facility_type[,<args>]\n");
  10532. return -1;
  10533. }
  10534. parse = ast_strdupa(data);
  10535. AST_STANDARD_APP_ARGS(args, parse);
  10536. if (ast_strlen_zero(args.facility_type)) {
  10537. ast_log(LOG_WARNING, "misdn_facility requires arguments: facility_type[,<args>]\n");
  10538. return -1;
  10539. }
  10540. if (!strcasecmp(args.facility_type, "calldeflect")) {
  10541. if (ast_strlen_zero(args.arg[0])) {
  10542. ast_log(LOG_WARNING, "Facility: Call Deflection requires an argument: Number\n");
  10543. }
  10544. #if defined(AST_MISDN_ENHANCEMENTS)
  10545. max_len = sizeof(ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number) - 1;
  10546. if (max_len < strlen(args.arg[0])) {
  10547. ast_log(LOG_WARNING,
  10548. "Facility: Number argument too long (up to %u digits are allowed). Ignoring.\n",
  10549. max_len);
  10550. return 0;
  10551. }
  10552. ch->bc->fac_out.Function = Fac_CallDeflection;
  10553. ch->bc->fac_out.u.CallDeflection.InvokeID = ++misdn_invoke_id;
  10554. ch->bc->fac_out.u.CallDeflection.ComponentType = FacComponent_Invoke;
  10555. ch->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1;
  10556. ch->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 0;
  10557. ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Type = 0;/* unknown */
  10558. ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = strlen(args.arg[0]);
  10559. strcpy((char *) ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number, args.arg[0]);
  10560. ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Subaddress.Length = 0;
  10561. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  10562. max_len = sizeof(ch->bc->fac_out.u.CDeflection.DeflectedToNumber) - 1;
  10563. if (max_len < strlen(args.arg[0])) {
  10564. ast_log(LOG_WARNING,
  10565. "Facility: Number argument too long (up to %u digits are allowed). Ignoring.\n",
  10566. max_len);
  10567. return 0;
  10568. }
  10569. ch->bc->fac_out.Function = Fac_CD;
  10570. ch->bc->fac_out.u.CDeflection.PresentationAllowed = 0;
  10571. //ch->bc->fac_out.u.CDeflection.DeflectedToSubaddress[0] = 0;
  10572. strcpy((char *) ch->bc->fac_out.u.CDeflection.DeflectedToNumber, args.arg[0]);
  10573. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  10574. /* Send message */
  10575. print_facility(&ch->bc->fac_out, ch->bc);
  10576. misdn_lib_send_event(ch->bc, EVENT_FACILITY);
  10577. #if defined(AST_MISDN_ENHANCEMENTS)
  10578. } else if (!strcasecmp(args.facility_type, "callrerouteing")
  10579. || !strcasecmp(args.facility_type, "callrerouting")) {
  10580. if (ast_strlen_zero(args.arg[0])) {
  10581. ast_log(LOG_WARNING, "Facility: Call rerouting requires an argument: Number\n");
  10582. }
  10583. max_len = sizeof(ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number) - 1;
  10584. if (max_len < strlen(args.arg[0])) {
  10585. ast_log(LOG_WARNING,
  10586. "Facility: Number argument too long (up to %u digits are allowed). Ignoring.\n",
  10587. max_len);
  10588. return 0;
  10589. }
  10590. ch->bc->fac_out.Function = Fac_CallRerouteing;
  10591. ch->bc->fac_out.u.CallRerouteing.InvokeID = ++misdn_invoke_id;
  10592. ch->bc->fac_out.u.CallRerouteing.ComponentType = FacComponent_Invoke;
  10593. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingReason = 0;/* unknown */
  10594. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingCounter = 1;
  10595. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 0;/* unknown */
  10596. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = strlen(args.arg[0]);
  10597. strcpy((char *) ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number, args.arg[0]);
  10598. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Subaddress.Length = 0;
  10599. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length = 0;
  10600. /* 0x90 0x90 0xa3 3.1 kHz audio, circuit mode, 64kbit/sec, level1/a-Law */
  10601. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 3;
  10602. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[0] = 0x90;
  10603. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[1] = 0x90;
  10604. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[2] = 0xa3;
  10605. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Length = 0;
  10606. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Llc.Length = 0;
  10607. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Length = 0;
  10608. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1;/* presentationRestricted */
  10609. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.SubscriptionOption = 0;/* no notification to caller */
  10610. /* Send message */
  10611. print_facility(&ch->bc->fac_out, ch->bc);
  10612. misdn_lib_send_event(ch->bc, EVENT_FACILITY);
  10613. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10614. } else {
  10615. chan_misdn_log(1, ch->bc->port, "Unknown Facility: %s\n", args.facility_type);
  10616. }
  10617. return 0;
  10618. }
  10619. static int misdn_check_l2l1(struct ast_channel *chan, const char *data)
  10620. {
  10621. char *parse;
  10622. char group[BUFFERSIZE + 1];
  10623. char *port_str;
  10624. int port = 0;
  10625. int timeout;
  10626. int dowait = 0;
  10627. int port_up;
  10628. AST_DECLARE_APP_ARGS(args,
  10629. AST_APP_ARG(grouppar);
  10630. AST_APP_ARG(timeout);
  10631. );
  10632. if (ast_strlen_zero((char *) data)) {
  10633. ast_log(LOG_WARNING, "misdn_check_l2l1 Requires arguments\n");
  10634. return -1;
  10635. }
  10636. parse = ast_strdupa(data);
  10637. AST_STANDARD_APP_ARGS(args, parse);
  10638. if (args.argc != 2) {
  10639. ast_log(LOG_WARNING, "Wrong argument count\n");
  10640. return 0;
  10641. }
  10642. /*ast_log(LOG_NOTICE, "Arguments: group/port '%s' timeout '%s'\n", args.grouppar, args.timeout);*/
  10643. timeout = atoi(args.timeout);
  10644. port_str = args.grouppar;
  10645. if (port_str[0] == 'g' && port_str[1] == ':') {
  10646. /* We make a group call lets checkout which ports are in my group */
  10647. port_str += 2;
  10648. ast_copy_string(group, port_str, sizeof(group));
  10649. chan_misdn_log(2, 0, "Checking Ports in group: %s\n", group);
  10650. for (port = misdn_cfg_get_next_port(port);
  10651. port > 0;
  10652. port = misdn_cfg_get_next_port(port)) {
  10653. char cfg_group[BUFFERSIZE + 1];
  10654. chan_misdn_log(2, 0, "trying port %d\n", port);
  10655. misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, sizeof(cfg_group));
  10656. if (!strcasecmp(cfg_group, group)) {
  10657. port_up = misdn_lib_port_up(port, 1);
  10658. if (!port_up) {
  10659. chan_misdn_log(2, 0, " --> port '%d'\n", port);
  10660. misdn_lib_get_port_up(port);
  10661. dowait = 1;
  10662. }
  10663. }
  10664. }
  10665. } else {
  10666. port = atoi(port_str);
  10667. chan_misdn_log(2, 0, "Checking Port: %d\n", port);
  10668. port_up = misdn_lib_port_up(port, 1);
  10669. if (!port_up) {
  10670. misdn_lib_get_port_up(port);
  10671. dowait = 1;
  10672. }
  10673. }
  10674. if (dowait) {
  10675. chan_misdn_log(2, 0, "Waiting for '%d' seconds\n", timeout);
  10676. ast_safe_sleep(chan, timeout * 1000);
  10677. }
  10678. return 0;
  10679. }
  10680. static int misdn_set_opt_exec(struct ast_channel *chan, const char *data)
  10681. {
  10682. struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
  10683. char *tok;
  10684. char *tokb;
  10685. char *parse;
  10686. int keyidx = 0;
  10687. int rxgain = 0;
  10688. int txgain = 0;
  10689. int change_jitter = 0;
  10690. if (strcasecmp(chan->tech->type, misdn_type)) {
  10691. ast_log(LOG_WARNING, "misdn_set_opt makes sense only with %s channels!\n", misdn_type);
  10692. return -1;
  10693. }
  10694. if (ast_strlen_zero((char *) data)) {
  10695. ast_log(LOG_WARNING, "misdn_set_opt Requires arguments\n");
  10696. return -1;
  10697. }
  10698. parse = ast_strdupa(data);
  10699. for (tok = strtok_r(parse, ":", &tokb);
  10700. tok;
  10701. tok = strtok_r(NULL, ":", &tokb)) {
  10702. int neglect = 0;
  10703. if (tok[0] == '!') {
  10704. neglect = 1;
  10705. tok++;
  10706. }
  10707. switch(tok[0]) {
  10708. case 'd' :
  10709. ast_copy_string(ch->bc->display, ++tok, sizeof(ch->bc->display));
  10710. chan_misdn_log(1, ch->bc->port, "SETOPT: Display:%s\n", ch->bc->display);
  10711. break;
  10712. case 'n':
  10713. chan_misdn_log(1, ch->bc->port, "SETOPT: No DSP\n");
  10714. ch->bc->nodsp = 1;
  10715. break;
  10716. case 'j':
  10717. chan_misdn_log(1, ch->bc->port, "SETOPT: jitter\n");
  10718. tok++;
  10719. change_jitter = 1;
  10720. switch (tok[0]) {
  10721. case 'b':
  10722. ch->jb_len = atoi(++tok);
  10723. chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d\n", ch->jb_len);
  10724. break;
  10725. case 't' :
  10726. ch->jb_upper_threshold = atoi(++tok);
  10727. chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d\n", ch->jb_upper_threshold);
  10728. break;
  10729. case 'n':
  10730. ch->bc->nojitter = 1;
  10731. chan_misdn_log(1, ch->bc->port, " --> nojitter\n");
  10732. break;
  10733. default:
  10734. ch->jb_len = 4000;
  10735. ch->jb_upper_threshold = 0;
  10736. chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d (default)\n", ch->jb_len);
  10737. chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d (default)\n", ch->jb_upper_threshold);
  10738. break;
  10739. }
  10740. break;
  10741. case 'v':
  10742. tok++;
  10743. switch (tok[0]) {
  10744. case 'r' :
  10745. rxgain = atoi(++tok);
  10746. if (rxgain < -8) {
  10747. rxgain = -8;
  10748. }
  10749. if (rxgain > 8) {
  10750. rxgain = 8;
  10751. }
  10752. ch->bc->rxgain = rxgain;
  10753. chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", rxgain);
  10754. break;
  10755. case 't':
  10756. txgain = atoi(++tok);
  10757. if (txgain < -8) {
  10758. txgain = -8;
  10759. }
  10760. if (txgain > 8) {
  10761. txgain = 8;
  10762. }
  10763. ch->bc->txgain = txgain;
  10764. chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", txgain);
  10765. break;
  10766. }
  10767. break;
  10768. case 'c':
  10769. keyidx = atoi(++tok);
  10770. {
  10771. char keys[4096];
  10772. char *key = NULL;
  10773. char *tmp = keys;
  10774. int i;
  10775. misdn_cfg_get(0, MISDN_GEN_CRYPT_KEYS, keys, sizeof(keys));
  10776. for (i = 0; i < keyidx; i++) {
  10777. key = strsep(&tmp, ",");
  10778. }
  10779. if (key) {
  10780. ast_copy_string(ch->bc->crypt_key, key, sizeof(ch->bc->crypt_key));
  10781. }
  10782. chan_misdn_log(0, ch->bc->port, "SETOPT: crypt with key:%s\n", ch->bc->crypt_key);
  10783. break;
  10784. }
  10785. case 'e':
  10786. chan_misdn_log(1, ch->bc->port, "SETOPT: EchoCancel\n");
  10787. if (neglect) {
  10788. chan_misdn_log(1, ch->bc->port, " --> disabled\n");
  10789. #ifdef MISDN_1_2
  10790. *ch->bc->pipeline = 0;
  10791. #else
  10792. ch->bc->ec_enable = 0;
  10793. #endif
  10794. } else {
  10795. #ifdef MISDN_1_2
  10796. update_pipeline_config(ch->bc);
  10797. #else
  10798. ch->bc->ec_enable = 1;
  10799. ch->bc->orig = ch->originator;
  10800. tok++;
  10801. if (*tok) {
  10802. ch->bc->ec_deftaps = atoi(tok);
  10803. }
  10804. #endif
  10805. }
  10806. break;
  10807. case 'h':
  10808. chan_misdn_log(1, ch->bc->port, "SETOPT: Digital\n");
  10809. if (strlen(tok) > 1 && tok[1] == '1') {
  10810. chan_misdn_log(1, ch->bc->port, "SETOPT: HDLC \n");
  10811. if (!ch->bc->hdlc) {
  10812. ch->bc->hdlc = 1;
  10813. }
  10814. }
  10815. ch->bc->capability = INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
  10816. break;
  10817. case 's':
  10818. chan_misdn_log(1, ch->bc->port, "SETOPT: Send DTMF\n");
  10819. ch->bc->send_dtmf = 1;
  10820. break;
  10821. case 'f':
  10822. chan_misdn_log(1, ch->bc->port, "SETOPT: Faxdetect\n");
  10823. ch->faxdetect = 1;
  10824. misdn_cfg_get(ch->bc->port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout));
  10825. break;
  10826. case 'a':
  10827. chan_misdn_log(1, ch->bc->port, "SETOPT: AST_DSP (for DTMF)\n");
  10828. ch->ast_dsp = 1;
  10829. break;
  10830. case 'p':
  10831. chan_misdn_log(1, ch->bc->port, "SETOPT: callerpres: %s\n", &tok[1]);
  10832. /* CRICH: callingpres!!! */
  10833. if (strstr(tok, "allowed")) {
  10834. ch->bc->presentation = 0;
  10835. ch->bc->set_presentation = 1;
  10836. } else if (strstr(tok, "restricted")) {
  10837. ch->bc->presentation = 1;
  10838. ch->bc->set_presentation = 1;
  10839. } else if (strstr(tok, "not_screened")) {
  10840. chan_misdn_log(0, ch->bc->port, "SETOPT: callerpres: not_screened is deprecated\n");
  10841. ch->bc->presentation = 1;
  10842. ch->bc->set_presentation = 1;
  10843. }
  10844. break;
  10845. case 'i' :
  10846. chan_misdn_log(1, ch->bc->port, "Ignoring dtmf tones, just use them inband\n");
  10847. ch->ignore_dtmf = 1;
  10848. break;
  10849. default:
  10850. break;
  10851. }
  10852. }
  10853. if (change_jitter) {
  10854. config_jitterbuffer(ch);
  10855. }
  10856. if (ch->faxdetect || ch->ast_dsp) {
  10857. if (!ch->dsp) {
  10858. ch->dsp = ast_dsp_new();
  10859. }
  10860. if (ch->dsp) {
  10861. ast_dsp_set_features(ch->dsp, DSP_FEATURE_DIGIT_DETECT | DSP_FEATURE_FAX_DETECT);
  10862. }
  10863. }
  10864. if (ch->ast_dsp) {
  10865. chan_misdn_log(1, ch->bc->port, "SETOPT: with AST_DSP we deactivate mISDN_dsp\n");
  10866. ch->bc->nodsp = 1;
  10867. }
  10868. return 0;
  10869. }
  10870. int chan_misdn_jb_empty(struct misdn_bchannel *bc, char *buf, int len)
  10871. {
  10872. struct chan_list *ch;
  10873. int res;
  10874. ch = find_chan_by_bc(bc);
  10875. if (!ch) {
  10876. return 0;
  10877. }
  10878. if (ch->jb) {
  10879. res = misdn_jb_empty(ch->jb, buf, len);
  10880. } else {
  10881. res = 0;
  10882. }
  10883. chan_list_unref(ch, "Done emptying jb");
  10884. return res;
  10885. }
  10886. /*******************************************************/
  10887. /***************** JITTERBUFFER ************************/
  10888. /*******************************************************/
  10889. /* allocates the jb-structure and initialize the elements*/
  10890. struct misdn_jb *misdn_jb_init(int size, int upper_threshold)
  10891. {
  10892. struct misdn_jb *jb;
  10893. jb = ast_calloc(1, sizeof(*jb));
  10894. if (!jb) {
  10895. chan_misdn_log(-1, 0, "No free Mem for jb\n");
  10896. return NULL;
  10897. }
  10898. jb->size = size;
  10899. jb->upper_threshold = upper_threshold;
  10900. //jb->wp = 0;
  10901. //jb->rp = 0;
  10902. //jb->state_full = 0;
  10903. //jb->state_empty = 0;
  10904. //jb->bytes_wrote = 0;
  10905. jb->samples = ast_calloc(size, sizeof(*jb->samples));
  10906. if (!jb->samples) {
  10907. ast_free(jb);
  10908. chan_misdn_log(-1, 0, "No free Mem for jb->samples\n");
  10909. return NULL;
  10910. }
  10911. jb->ok = ast_calloc(size, sizeof(*jb->ok));
  10912. if (!jb->ok) {
  10913. ast_free(jb->samples);
  10914. ast_free(jb);
  10915. chan_misdn_log(-1, 0, "No free Mem for jb->ok\n");
  10916. return NULL;
  10917. }
  10918. ast_mutex_init(&jb->mutexjb);
  10919. return jb;
  10920. }
  10921. /* frees the data and destroys the given jitterbuffer struct */
  10922. void misdn_jb_destroy(struct misdn_jb *jb)
  10923. {
  10924. ast_mutex_destroy(&jb->mutexjb);
  10925. ast_free(jb->ok);
  10926. ast_free(jb->samples);
  10927. ast_free(jb);
  10928. }
  10929. /* fills the jitterbuffer with len data returns < 0 if there was an
  10930. error (buffer overflow). */
  10931. int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len)
  10932. {
  10933. int i;
  10934. int j;
  10935. int rp;
  10936. int wp;
  10937. if (!jb || ! data) {
  10938. return 0;
  10939. }
  10940. ast_mutex_lock(&jb->mutexjb);
  10941. wp = jb->wp;
  10942. rp = jb->rp;
  10943. for (i = 0; i < len; i++) {
  10944. jb->samples[wp] = data[i];
  10945. jb->ok[wp] = 1;
  10946. wp = (wp != jb->size - 1) ? wp + 1 : 0;
  10947. if (wp == jb->rp) {
  10948. jb->state_full = 1;
  10949. }
  10950. }
  10951. if (wp >= rp) {
  10952. jb->state_buffer = wp - rp;
  10953. } else {
  10954. jb->state_buffer = jb->size - rp + wp;
  10955. }
  10956. chan_misdn_log(9, 0, "misdn_jb_fill: written:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
  10957. if (jb->state_full) {
  10958. jb->wp = wp;
  10959. rp = wp;
  10960. for (j = 0; j < jb->upper_threshold; j++) {
  10961. rp = (rp != 0) ? rp - 1 : jb->size - 1;
  10962. }
  10963. jb->rp = rp;
  10964. jb->state_full = 0;
  10965. jb->state_empty = 1;
  10966. ast_mutex_unlock(&jb->mutexjb);
  10967. return -1;
  10968. }
  10969. if (!jb->state_empty) {
  10970. jb->bytes_wrote += len;
  10971. if (jb->bytes_wrote >= jb->upper_threshold) {
  10972. jb->state_empty = 1;
  10973. jb->bytes_wrote = 0;
  10974. }
  10975. }
  10976. jb->wp = wp;
  10977. ast_mutex_unlock(&jb->mutexjb);
  10978. return 0;
  10979. }
  10980. /* gets len bytes out of the jitterbuffer if available, else only the
  10981. available data is returned and the return value indicates the number
  10982. of data. */
  10983. int misdn_jb_empty(struct misdn_jb *jb, char *data, int len)
  10984. {
  10985. int i;
  10986. int wp;
  10987. int rp;
  10988. int read = 0;
  10989. ast_mutex_lock(&jb->mutexjb);
  10990. rp = jb->rp;
  10991. wp = jb->wp;
  10992. if (jb->state_empty) {
  10993. for (i = 0; i < len; i++) {
  10994. if (wp == rp) {
  10995. jb->rp = rp;
  10996. jb->state_empty = 0;
  10997. ast_mutex_unlock(&jb->mutexjb);
  10998. return read;
  10999. } else {
  11000. if (jb->ok[rp] == 1) {
  11001. data[i] = jb->samples[rp];
  11002. jb->ok[rp] = 0;
  11003. rp = (rp != jb->size - 1) ? rp + 1 : 0;
  11004. read += 1;
  11005. }
  11006. }
  11007. }
  11008. if (wp >= rp) {
  11009. jb->state_buffer = wp - rp;
  11010. } else {
  11011. jb->state_buffer = jb->size - rp + wp;
  11012. }
  11013. chan_misdn_log(9, 0, "misdn_jb_empty: read:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
  11014. jb->rp = rp;
  11015. } else {
  11016. chan_misdn_log(9, 0, "misdn_jb_empty: Wait...requested:%d p:%p\n", len, jb);
  11017. }
  11018. ast_mutex_unlock(&jb->mutexjb);
  11019. return read;
  11020. }
  11021. /*******************************************************/
  11022. /*************** JITTERBUFFER END *********************/
  11023. /*******************************************************/
  11024. static void chan_misdn_log(int level, int port, char *tmpl, ...)
  11025. {
  11026. va_list ap;
  11027. char buf[1024];
  11028. char port_buf[8];
  11029. if (!(0 <= port && port <= max_ports)) {
  11030. ast_log(LOG_WARNING, "chan_misdn_log called with out-of-range port number! (%d)\n", port);
  11031. port = 0;
  11032. level = -1;
  11033. } else if (!(level == -1
  11034. || (misdn_debug_only[port]
  11035. ? (level == 1 && misdn_debug[port]) || level == misdn_debug[port]
  11036. : level <= misdn_debug[port])
  11037. || (level <= misdn_debug[0] && !ast_strlen_zero(global_tracefile)))) {
  11038. /*
  11039. * We are not going to print anything so lets not
  11040. * go to all the work of generating a string.
  11041. */
  11042. return;
  11043. }
  11044. snprintf(port_buf, sizeof(port_buf), "P[%2d] ", port);
  11045. va_start(ap, tmpl);
  11046. vsnprintf(buf, sizeof(buf), tmpl, ap);
  11047. va_end(ap);
  11048. if (level == -1) {
  11049. ast_log(LOG_WARNING, "%s", buf);
  11050. } else if (misdn_debug_only[port]
  11051. ? (level == 1 && misdn_debug[port]) || level == misdn_debug[port]
  11052. : level <= misdn_debug[port]) {
  11053. ast_verbose("%s%s", port_buf, buf);
  11054. }
  11055. if (level <= misdn_debug[0] && !ast_strlen_zero(global_tracefile)) {
  11056. char ctimebuf[30];
  11057. time_t tm;
  11058. char *tmp;
  11059. char *p;
  11060. FILE *fp;
  11061. fp = fopen(global_tracefile, "a+");
  11062. if (!fp) {
  11063. ast_verbose("Error opening Tracefile: [ %s ] %s\n", global_tracefile, strerror(errno));
  11064. return;
  11065. }
  11066. tm = time(NULL);
  11067. tmp = ctime_r(&tm, ctimebuf);
  11068. p = strchr(tmp, '\n');
  11069. if (p) {
  11070. *p = ':';
  11071. }
  11072. fputs(tmp, fp);
  11073. fputs(" ", fp);
  11074. fputs(port_buf, fp);
  11075. fputs(" ", fp);
  11076. fputs(buf, fp);
  11077. fclose(fp);
  11078. }
  11079. }
  11080. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Channel driver for mISDN Support (BRI/PRI)",
  11081. .load = load_module,
  11082. .unload = unload_module,
  11083. .reload = reload,
  11084. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  11085. );