sig_pri.c 286 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2009, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.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. /*! \file
  19. *
  20. * \brief PRI signaling module
  21. *
  22. * \author Matthew Fredrickson <creslin@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #ifdef HAVE_PRI
  29. #include <errno.h>
  30. #include <ctype.h>
  31. #include <signal.h>
  32. #include "asterisk/utils.h"
  33. #include "asterisk/options.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/app.h"
  36. #include "asterisk/file.h"
  37. #include "asterisk/callerid.h"
  38. #include "asterisk/say.h"
  39. #include "asterisk/manager.h"
  40. #include "asterisk/astdb.h"
  41. #include "asterisk/causes.h"
  42. #include "asterisk/musiconhold.h"
  43. #include "asterisk/cli.h"
  44. #include "asterisk/transcap.h"
  45. #include "asterisk/features.h"
  46. #include "asterisk/aoc.h"
  47. #include "sig_pri.h"
  48. #ifndef PRI_EVENT_FACILITY
  49. #error "Upgrade your libpri"
  50. #endif
  51. /* define this to send PRI user-user information elements */
  52. #undef SUPPORT_USERUSER
  53. /*!
  54. * Define to make always pick a channel if allowed. Useful for
  55. * testing channel shifting.
  56. */
  57. //#define ALWAYS_PICK_CHANNEL 1
  58. /*!
  59. * Define to force a RESTART on a channel that returns a cause
  60. * code of PRI_CAUSE_REQUESTED_CHAN_UNAVAIL(44). If the cause
  61. * is because of a stuck channel on the peer and the channel is
  62. * always the next channel we pick for an outgoing call then
  63. * this can help.
  64. */
  65. #define FORCE_RESTART_UNAVAIL_CHANS 1
  66. #if defined(HAVE_PRI_CCSS)
  67. struct sig_pri_cc_agent_prv {
  68. /*! Asterisk span D channel control structure. */
  69. struct sig_pri_span *pri;
  70. /*! CC id value to use with libpri. -1 if invalid. */
  71. long cc_id;
  72. /*! TRUE if CC has been requested and we are waiting for the response. */
  73. unsigned char cc_request_response_pending;
  74. };
  75. struct sig_pri_cc_monitor_instance {
  76. /*! \brief Asterisk span D channel control structure. */
  77. struct sig_pri_span *pri;
  78. /*! CC id value to use with libpri. (-1 if already canceled). */
  79. long cc_id;
  80. /*! CC core id value. */
  81. int core_id;
  82. /*! Device name(Channel name less sequence number) */
  83. char name[1];
  84. };
  85. /*! Upper level agent/monitor type name. */
  86. static const char *sig_pri_cc_type_name;
  87. /*! Container of sig_pri monitor instances. */
  88. static struct ao2_container *sig_pri_cc_monitors;
  89. #endif /* defined(HAVE_PRI_CCSS) */
  90. static int pri_matchdigittimeout = 3000;
  91. static int pri_gendigittimeout = 8000;
  92. #define DCHAN_NOTINALARM (1 << 0)
  93. #define DCHAN_UP (1 << 1)
  94. /* Defines to help decode the encoded event channel id. */
  95. #define PRI_CHANNEL(p) ((p) & 0xff)
  96. #define PRI_SPAN(p) (((p) >> 8) & 0xff)
  97. #define PRI_EXPLICIT (1 << 16)
  98. #define PRI_CIS_CALL (1 << 17) /* Call is using the D channel only. */
  99. #define PRI_HELD_CALL (1 << 18)
  100. #define DCHAN_AVAILABLE (DCHAN_NOTINALARM | DCHAN_UP)
  101. static int pri_active_dchan_index(struct sig_pri_span *pri);
  102. static const char *sig_pri_call_level2str(enum sig_pri_call_level level)
  103. {
  104. switch (level) {
  105. case SIG_PRI_CALL_LEVEL_IDLE:
  106. return "Idle";
  107. case SIG_PRI_CALL_LEVEL_SETUP:
  108. return "Setup";
  109. case SIG_PRI_CALL_LEVEL_OVERLAP:
  110. return "Overlap";
  111. case SIG_PRI_CALL_LEVEL_PROCEEDING:
  112. return "Proceeding";
  113. case SIG_PRI_CALL_LEVEL_ALERTING:
  114. return "Alerting";
  115. case SIG_PRI_CALL_LEVEL_DEFER_DIAL:
  116. return "DeferDial";
  117. case SIG_PRI_CALL_LEVEL_CONNECT:
  118. return "Connect";
  119. }
  120. return "Unknown";
  121. }
  122. static inline void pri_rel(struct sig_pri_span *pri)
  123. {
  124. ast_mutex_unlock(&pri->lock);
  125. }
  126. static unsigned int PVT_TO_CHANNEL(struct sig_pri_chan *p)
  127. {
  128. int res = (((p)->prioffset) | ((p)->logicalspan << 8) | (p->mastertrunkgroup ? PRI_EXPLICIT : 0));
  129. ast_debug(5, "prioffset: %d mastertrunkgroup: %d logicalspan: %d result: %d\n",
  130. p->prioffset, p->mastertrunkgroup, p->logicalspan, res);
  131. return res;
  132. }
  133. static void sig_pri_handle_dchan_exception(struct sig_pri_span *pri, int index)
  134. {
  135. if (pri->calls->handle_dchan_exception)
  136. pri->calls->handle_dchan_exception(pri, index);
  137. }
  138. static void sig_pri_set_dialing(struct sig_pri_chan *p, int is_dialing)
  139. {
  140. if (p->calls->set_dialing) {
  141. p->calls->set_dialing(p->chan_pvt, is_dialing);
  142. }
  143. }
  144. static void sig_pri_set_digital(struct sig_pri_chan *p, int is_digital)
  145. {
  146. p->digital = is_digital;
  147. if (p->calls->set_digital) {
  148. p->calls->set_digital(p->chan_pvt, is_digital);
  149. }
  150. }
  151. static void sig_pri_set_outgoing(struct sig_pri_chan *p, int is_outgoing)
  152. {
  153. p->outgoing = is_outgoing;
  154. if (p->calls->set_outgoing) {
  155. p->calls->set_outgoing(p->chan_pvt, is_outgoing);
  156. }
  157. }
  158. void sig_pri_set_alarm(struct sig_pri_chan *p, int in_alarm)
  159. {
  160. if (sig_pri_is_alarm_ignored(p->pri)) {
  161. /* Always set not in alarm */
  162. in_alarm = 0;
  163. }
  164. /*
  165. * Clear the channel restart state when the channel alarm
  166. * changes to prevent the state from getting stuck when the link
  167. * goes down.
  168. */
  169. p->resetting = SIG_PRI_RESET_IDLE;
  170. p->inalarm = in_alarm;
  171. if (p->calls->set_alarm) {
  172. p->calls->set_alarm(p->chan_pvt, in_alarm);
  173. }
  174. }
  175. static const char *sig_pri_get_orig_dialstring(struct sig_pri_chan *p)
  176. {
  177. if (p->calls->get_orig_dialstring) {
  178. return p->calls->get_orig_dialstring(p->chan_pvt);
  179. }
  180. ast_log(LOG_ERROR, "get_orig_dialstring callback not defined\n");
  181. return "";
  182. }
  183. #if defined(HAVE_PRI_CCSS)
  184. static void sig_pri_make_cc_dialstring(struct sig_pri_chan *p, char *buf, size_t buf_size)
  185. {
  186. if (p->calls->make_cc_dialstring) {
  187. p->calls->make_cc_dialstring(p->chan_pvt, buf, buf_size);
  188. } else {
  189. ast_log(LOG_ERROR, "make_cc_dialstring callback not defined\n");
  190. buf[0] = '\0';
  191. }
  192. }
  193. #endif /* defined(HAVE_PRI_CCSS) */
  194. static void sig_pri_dial_digits(struct sig_pri_chan *p, const char *dial_string)
  195. {
  196. if (p->calls->dial_digits) {
  197. p->calls->dial_digits(p->chan_pvt, dial_string);
  198. }
  199. }
  200. /*!
  201. * \internal
  202. * \brief Reevaluate the PRI span device state.
  203. * \since 1.8
  204. *
  205. * \param pri PRI span control structure.
  206. *
  207. * \return Nothing
  208. *
  209. * \note Assumes the pri->lock is already obtained.
  210. */
  211. static void sig_pri_span_devstate_changed(struct sig_pri_span *pri)
  212. {
  213. if (pri->calls->update_span_devstate) {
  214. pri->calls->update_span_devstate(pri);
  215. }
  216. }
  217. /*!
  218. * \internal
  219. * \brief Set the caller id information in the parent module.
  220. * \since 1.8
  221. *
  222. * \param p sig_pri channel structure.
  223. *
  224. * \return Nothing
  225. */
  226. static void sig_pri_set_caller_id(struct sig_pri_chan *p)
  227. {
  228. struct ast_party_caller caller;
  229. if (p->calls->set_callerid) {
  230. ast_party_caller_init(&caller);
  231. caller.id.name.str = p->cid_name;
  232. caller.id.name.presentation = p->callingpres;
  233. caller.id.name.valid = 1;
  234. caller.id.number.str = p->cid_num;
  235. caller.id.number.plan = p->cid_ton;
  236. caller.id.number.presentation = p->callingpres;
  237. caller.id.number.valid = 1;
  238. if (!ast_strlen_zero(p->cid_subaddr)) {
  239. caller.id.subaddress.valid = 1;
  240. //caller.id.subaddress.type = 0;/* nsap */
  241. //caller.id.subaddress.odd_even_indicator = 0;
  242. caller.id.subaddress.str = p->cid_subaddr;
  243. }
  244. caller.id.tag = p->user_tag;
  245. caller.ani.number.str = p->cid_ani;
  246. //caller.ani.number.plan = p->xxx;
  247. //caller.ani.number.presentation = p->xxx;
  248. caller.ani.number.valid = 1;
  249. caller.ani2 = p->cid_ani2;
  250. p->calls->set_callerid(p->chan_pvt, &caller);
  251. }
  252. }
  253. /*!
  254. * \internal
  255. * \brief Set the Dialed Number Identifier.
  256. * \since 1.8
  257. *
  258. * \param p sig_pri channel structure.
  259. * \param dnid Dialed Number Identifier string.
  260. *
  261. * \return Nothing
  262. */
  263. static void sig_pri_set_dnid(struct sig_pri_chan *p, const char *dnid)
  264. {
  265. if (p->calls->set_dnid) {
  266. p->calls->set_dnid(p->chan_pvt, dnid);
  267. }
  268. }
  269. /*!
  270. * \internal
  271. * \brief Set the Redirecting Directory Number Information Service (RDNIS).
  272. * \since 1.8
  273. *
  274. * \param p sig_pri channel structure.
  275. * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
  276. *
  277. * \return Nothing
  278. */
  279. static void sig_pri_set_rdnis(struct sig_pri_chan *p, const char *rdnis)
  280. {
  281. if (p->calls->set_rdnis) {
  282. p->calls->set_rdnis(p->chan_pvt, rdnis);
  283. }
  284. }
  285. static void sig_pri_unlock_private(struct sig_pri_chan *p)
  286. {
  287. if (p->calls->unlock_private)
  288. p->calls->unlock_private(p->chan_pvt);
  289. }
  290. static void sig_pri_lock_private(struct sig_pri_chan *p)
  291. {
  292. if (p->calls->lock_private)
  293. p->calls->lock_private(p->chan_pvt);
  294. }
  295. static void sig_pri_deadlock_avoidance_private(struct sig_pri_chan *p)
  296. {
  297. if (p->calls->deadlock_avoidance_private) {
  298. p->calls->deadlock_avoidance_private(p->chan_pvt);
  299. } else {
  300. /* Fallback to the old way if callback not present. */
  301. sig_pri_unlock_private(p);
  302. sched_yield();
  303. sig_pri_lock_private(p);
  304. }
  305. }
  306. static void pri_grab(struct sig_pri_chan *p, struct sig_pri_span *pri)
  307. {
  308. /* Grab the lock first */
  309. while (ast_mutex_trylock(&pri->lock)) {
  310. /* Avoid deadlock */
  311. sig_pri_deadlock_avoidance_private(p);
  312. }
  313. /* Then break the poll */
  314. if (pri->master != AST_PTHREADT_NULL) {
  315. pthread_kill(pri->master, SIGURG);
  316. }
  317. }
  318. /*!
  319. * \internal
  320. * \brief Convert PRI redirecting reason to asterisk version.
  321. * \since 1.8
  322. *
  323. * \param pri_reason PRI redirecting reason.
  324. *
  325. * \return Equivalent asterisk redirecting reason value.
  326. */
  327. static enum AST_REDIRECTING_REASON pri_to_ast_reason(int pri_reason)
  328. {
  329. enum AST_REDIRECTING_REASON ast_reason;
  330. switch (pri_reason) {
  331. case PRI_REDIR_FORWARD_ON_BUSY:
  332. ast_reason = AST_REDIRECTING_REASON_USER_BUSY;
  333. break;
  334. case PRI_REDIR_FORWARD_ON_NO_REPLY:
  335. ast_reason = AST_REDIRECTING_REASON_NO_ANSWER;
  336. break;
  337. case PRI_REDIR_DEFLECTION:
  338. ast_reason = AST_REDIRECTING_REASON_DEFLECTION;
  339. break;
  340. case PRI_REDIR_UNCONDITIONAL:
  341. ast_reason = AST_REDIRECTING_REASON_UNCONDITIONAL;
  342. break;
  343. case PRI_REDIR_UNKNOWN:
  344. default:
  345. ast_reason = AST_REDIRECTING_REASON_UNKNOWN;
  346. break;
  347. }
  348. return ast_reason;
  349. }
  350. /*!
  351. * \internal
  352. * \brief Convert asterisk redirecting reason to PRI version.
  353. * \since 1.8
  354. *
  355. * \param ast_reason Asterisk redirecting reason.
  356. *
  357. * \return Equivalent PRI redirecting reason value.
  358. */
  359. static int ast_to_pri_reason(enum AST_REDIRECTING_REASON ast_reason)
  360. {
  361. int pri_reason;
  362. switch (ast_reason) {
  363. case AST_REDIRECTING_REASON_USER_BUSY:
  364. pri_reason = PRI_REDIR_FORWARD_ON_BUSY;
  365. break;
  366. case AST_REDIRECTING_REASON_NO_ANSWER:
  367. pri_reason = PRI_REDIR_FORWARD_ON_NO_REPLY;
  368. break;
  369. case AST_REDIRECTING_REASON_UNCONDITIONAL:
  370. pri_reason = PRI_REDIR_UNCONDITIONAL;
  371. break;
  372. case AST_REDIRECTING_REASON_DEFLECTION:
  373. pri_reason = PRI_REDIR_DEFLECTION;
  374. break;
  375. case AST_REDIRECTING_REASON_UNKNOWN:
  376. default:
  377. pri_reason = PRI_REDIR_UNKNOWN;
  378. break;
  379. }
  380. return pri_reason;
  381. }
  382. /*!
  383. * \internal
  384. * \brief Convert PRI number presentation to asterisk version.
  385. * \since 1.8
  386. *
  387. * \param pri_presentation PRI number presentation.
  388. *
  389. * \return Equivalent asterisk number presentation value.
  390. */
  391. static int pri_to_ast_presentation(int pri_presentation)
  392. {
  393. int ast_presentation;
  394. switch (pri_presentation) {
  395. case PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED:
  396. ast_presentation = AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED;
  397. break;
  398. case PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_PASSED_SCREEN:
  399. ast_presentation = AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_PASSED_SCREEN;
  400. break;
  401. case PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_FAILED_SCREEN:
  402. ast_presentation = AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_FAILED_SCREEN;
  403. break;
  404. case PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER:
  405. ast_presentation = AST_PRES_ALLOWED | AST_PRES_NETWORK_NUMBER;
  406. break;
  407. case PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED:
  408. ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
  409. break;
  410. case PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_PASSED_SCREEN:
  411. ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_PASSED_SCREEN;
  412. break;
  413. case PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_FAILED_SCREEN:
  414. ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_FAILED_SCREEN;
  415. break;
  416. case PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER:
  417. ast_presentation = AST_PRES_RESTRICTED | AST_PRES_NETWORK_NUMBER;
  418. break;
  419. case PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED:
  420. case PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_PASSED_SCREEN:
  421. case PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_FAILED_SCREEN:
  422. case PRI_PRES_UNAVAILABLE | PRI_PRES_NETWORK_NUMBER:
  423. ast_presentation = AST_PRES_NUMBER_NOT_AVAILABLE;
  424. break;
  425. default:
  426. ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
  427. break;
  428. }
  429. return ast_presentation;
  430. }
  431. /*!
  432. * \internal
  433. * \brief Convert asterisk number presentation to PRI version.
  434. * \since 1.8
  435. *
  436. * \param ast_presentation Asterisk number presentation.
  437. *
  438. * \return Equivalent PRI number presentation value.
  439. */
  440. static int ast_to_pri_presentation(int ast_presentation)
  441. {
  442. int pri_presentation;
  443. switch (ast_presentation) {
  444. case AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED:
  445. pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
  446. break;
  447. case AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_PASSED_SCREEN:
  448. pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_PASSED_SCREEN;
  449. break;
  450. case AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_FAILED_SCREEN:
  451. pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_FAILED_SCREEN;
  452. break;
  453. case AST_PRES_ALLOWED | AST_PRES_NETWORK_NUMBER:
  454. pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER;
  455. break;
  456. case AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED:
  457. pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
  458. break;
  459. case AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_PASSED_SCREEN:
  460. pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_PASSED_SCREEN;
  461. break;
  462. case AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_FAILED_SCREEN:
  463. pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_FAILED_SCREEN;
  464. break;
  465. case AST_PRES_RESTRICTED | AST_PRES_NETWORK_NUMBER:
  466. pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER;
  467. break;
  468. case AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_UNSCREENED:
  469. case AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_PASSED_SCREEN:
  470. case AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_FAILED_SCREEN:
  471. case AST_PRES_UNAVAILABLE | AST_PRES_NETWORK_NUMBER:
  472. pri_presentation = PRES_NUMBER_NOT_AVAILABLE;
  473. break;
  474. default:
  475. pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
  476. break;
  477. }
  478. return pri_presentation;
  479. }
  480. /*!
  481. * \internal
  482. * \brief Convert PRI name char_set to asterisk version.
  483. * \since 1.8
  484. *
  485. * \param pri_char_set PRI name char_set.
  486. *
  487. * \return Equivalent asterisk name char_set value.
  488. */
  489. static enum AST_PARTY_CHAR_SET pri_to_ast_char_set(int pri_char_set)
  490. {
  491. enum AST_PARTY_CHAR_SET ast_char_set;
  492. switch (pri_char_set) {
  493. default:
  494. case PRI_CHAR_SET_UNKNOWN:
  495. ast_char_set = AST_PARTY_CHAR_SET_UNKNOWN;
  496. break;
  497. case PRI_CHAR_SET_ISO8859_1:
  498. ast_char_set = AST_PARTY_CHAR_SET_ISO8859_1;
  499. break;
  500. case PRI_CHAR_SET_WITHDRAWN:
  501. ast_char_set = AST_PARTY_CHAR_SET_WITHDRAWN;
  502. break;
  503. case PRI_CHAR_SET_ISO8859_2:
  504. ast_char_set = AST_PARTY_CHAR_SET_ISO8859_2;
  505. break;
  506. case PRI_CHAR_SET_ISO8859_3:
  507. ast_char_set = AST_PARTY_CHAR_SET_ISO8859_3;
  508. break;
  509. case PRI_CHAR_SET_ISO8859_4:
  510. ast_char_set = AST_PARTY_CHAR_SET_ISO8859_4;
  511. break;
  512. case PRI_CHAR_SET_ISO8859_5:
  513. ast_char_set = AST_PARTY_CHAR_SET_ISO8859_5;
  514. break;
  515. case PRI_CHAR_SET_ISO8859_7:
  516. ast_char_set = AST_PARTY_CHAR_SET_ISO8859_7;
  517. break;
  518. case PRI_CHAR_SET_ISO10646_BMPSTRING:
  519. ast_char_set = AST_PARTY_CHAR_SET_ISO10646_BMPSTRING;
  520. break;
  521. case PRI_CHAR_SET_ISO10646_UTF_8STRING:
  522. ast_char_set = AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING;
  523. break;
  524. }
  525. return ast_char_set;
  526. }
  527. /*!
  528. * \internal
  529. * \brief Convert asterisk name char_set to PRI version.
  530. * \since 1.8
  531. *
  532. * \param ast_char_set Asterisk name char_set.
  533. *
  534. * \return Equivalent PRI name char_set value.
  535. */
  536. static int ast_to_pri_char_set(enum AST_PARTY_CHAR_SET ast_char_set)
  537. {
  538. int pri_char_set;
  539. switch (ast_char_set) {
  540. default:
  541. case AST_PARTY_CHAR_SET_UNKNOWN:
  542. pri_char_set = PRI_CHAR_SET_UNKNOWN;
  543. break;
  544. case AST_PARTY_CHAR_SET_ISO8859_1:
  545. pri_char_set = PRI_CHAR_SET_ISO8859_1;
  546. break;
  547. case AST_PARTY_CHAR_SET_WITHDRAWN:
  548. pri_char_set = PRI_CHAR_SET_WITHDRAWN;
  549. break;
  550. case AST_PARTY_CHAR_SET_ISO8859_2:
  551. pri_char_set = PRI_CHAR_SET_ISO8859_2;
  552. break;
  553. case AST_PARTY_CHAR_SET_ISO8859_3:
  554. pri_char_set = PRI_CHAR_SET_ISO8859_3;
  555. break;
  556. case AST_PARTY_CHAR_SET_ISO8859_4:
  557. pri_char_set = PRI_CHAR_SET_ISO8859_4;
  558. break;
  559. case AST_PARTY_CHAR_SET_ISO8859_5:
  560. pri_char_set = PRI_CHAR_SET_ISO8859_5;
  561. break;
  562. case AST_PARTY_CHAR_SET_ISO8859_7:
  563. pri_char_set = PRI_CHAR_SET_ISO8859_7;
  564. break;
  565. case AST_PARTY_CHAR_SET_ISO10646_BMPSTRING:
  566. pri_char_set = PRI_CHAR_SET_ISO10646_BMPSTRING;
  567. break;
  568. case AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING:
  569. pri_char_set = PRI_CHAR_SET_ISO10646_UTF_8STRING;
  570. break;
  571. }
  572. return pri_char_set;
  573. }
  574. #if defined(HAVE_PRI_SUBADDR)
  575. /*!
  576. * \internal
  577. * \brief Fill in the asterisk party subaddress from the given PRI party subaddress.
  578. * \since 1.8
  579. *
  580. * \param ast_subaddress Asterisk party subaddress structure.
  581. * \param pri_subaddress PRI party subaddress structure.
  582. *
  583. * \return Nothing
  584. *
  585. */
  586. static void sig_pri_set_subaddress(struct ast_party_subaddress *ast_subaddress, const struct pri_party_subaddress *pri_subaddress)
  587. {
  588. char *cnum, *ptr;
  589. int x, len;
  590. if (ast_subaddress->str) {
  591. ast_free(ast_subaddress->str);
  592. }
  593. if (pri_subaddress->length <= 0) {
  594. ast_party_subaddress_init(ast_subaddress);
  595. return;
  596. }
  597. if (!pri_subaddress->type) {
  598. /* NSAP */
  599. ast_subaddress->str = ast_strdup((char *) pri_subaddress->data);
  600. } else {
  601. /* User Specified */
  602. if (!(cnum = ast_malloc(2 * pri_subaddress->length + 1))) {
  603. ast_party_subaddress_init(ast_subaddress);
  604. return;
  605. }
  606. ptr = cnum;
  607. len = pri_subaddress->length - 1; /* -1 account for zero based indexing */
  608. for (x = 0; x < len; ++x) {
  609. ptr += sprintf(ptr, "%02x", pri_subaddress->data[x]);
  610. }
  611. if (pri_subaddress->odd_even_indicator) {
  612. /* ODD */
  613. sprintf(ptr, "%01x", (pri_subaddress->data[len]) >> 4);
  614. } else {
  615. /* EVEN */
  616. sprintf(ptr, "%02x", pri_subaddress->data[len]);
  617. }
  618. ast_subaddress->str = cnum;
  619. }
  620. ast_subaddress->type = pri_subaddress->type;
  621. ast_subaddress->odd_even_indicator = pri_subaddress->odd_even_indicator;
  622. ast_subaddress->valid = 1;
  623. }
  624. #endif /* defined(HAVE_PRI_SUBADDR) */
  625. #if defined(HAVE_PRI_SUBADDR)
  626. static unsigned char ast_pri_pack_hex_char(char c)
  627. {
  628. unsigned char res;
  629. if (c < '0') {
  630. res = 0;
  631. } else if (c < ('9' + 1)) {
  632. res = c - '0';
  633. } else if (c < 'A') {
  634. res = 0;
  635. } else if (c < ('F' + 1)) {
  636. res = c - 'A' + 10;
  637. } else if (c < 'a') {
  638. res = 0;
  639. } else if (c < ('f' + 1)) {
  640. res = c - 'a' + 10;
  641. } else {
  642. res = 0;
  643. }
  644. return res;
  645. }
  646. #endif /* defined(HAVE_PRI_SUBADDR) */
  647. #if defined(HAVE_PRI_SUBADDR)
  648. /*!
  649. * \internal
  650. * \brief Convert a null terminated hexadecimal string to a packed hex byte array.
  651. * \details left justified, with 0 padding if odd length.
  652. * \since 1.8
  653. *
  654. * \param dst pointer to packed byte array.
  655. * \param src pointer to null terminated hexadecimal string.
  656. * \param maxlen destination array size.
  657. *
  658. * \return Length of byte array
  659. *
  660. * \note The dst is not an ASCIIz string.
  661. * \note The src is an ASCIIz hex string.
  662. */
  663. static int ast_pri_pack_hex_string(unsigned char *dst, char *src, int maxlen)
  664. {
  665. int res = 0;
  666. int len = strlen(src);
  667. if (len > (2 * maxlen)) {
  668. len = 2 * maxlen;
  669. }
  670. res = len / 2 + len % 2;
  671. while (len > 1) {
  672. *dst = ast_pri_pack_hex_char(*src) << 4;
  673. src++;
  674. *dst |= ast_pri_pack_hex_char(*src);
  675. dst++, src++;
  676. len -= 2;
  677. }
  678. if (len) { /* 1 left */
  679. *dst = ast_pri_pack_hex_char(*src) << 4;
  680. }
  681. return res;
  682. }
  683. #endif /* defined(HAVE_PRI_SUBADDR) */
  684. #if defined(HAVE_PRI_SUBADDR)
  685. /*!
  686. * \internal
  687. * \brief Fill in the PRI party subaddress from the given asterisk party subaddress.
  688. * \since 1.8
  689. *
  690. * \param pri_subaddress PRI party subaddress structure.
  691. * \param ast_subaddress Asterisk party subaddress structure.
  692. *
  693. * \return Nothing
  694. *
  695. * \note Assumes that pri_subaddress has been previously memset to zero.
  696. */
  697. static void sig_pri_party_subaddress_from_ast(struct pri_party_subaddress *pri_subaddress, const struct ast_party_subaddress *ast_subaddress)
  698. {
  699. if (ast_subaddress->valid && !ast_strlen_zero(ast_subaddress->str)) {
  700. pri_subaddress->type = ast_subaddress->type;
  701. if (!ast_subaddress->type) {
  702. /* 0 = NSAP */
  703. ast_copy_string((char *) pri_subaddress->data, ast_subaddress->str,
  704. sizeof(pri_subaddress->data));
  705. pri_subaddress->length = strlen((char *) pri_subaddress->data);
  706. pri_subaddress->odd_even_indicator = 0;
  707. pri_subaddress->valid = 1;
  708. } else {
  709. /* 2 = User Specified */
  710. /*
  711. * Copy HexString to packed HexData,
  712. * if odd length then right pad trailing byte with 0
  713. */
  714. int length = ast_pri_pack_hex_string(pri_subaddress->data,
  715. ast_subaddress->str, sizeof(pri_subaddress->data));
  716. pri_subaddress->length = length; /* packed data length */
  717. length = strlen(ast_subaddress->str);
  718. if (length > 2 * sizeof(pri_subaddress->data)) {
  719. pri_subaddress->odd_even_indicator = 0;
  720. } else {
  721. pri_subaddress->odd_even_indicator = (length & 1);
  722. }
  723. pri_subaddress->valid = 1;
  724. }
  725. }
  726. }
  727. #endif /* defined(HAVE_PRI_SUBADDR) */
  728. /*!
  729. * \internal
  730. * \brief Fill in the PRI party name from the given asterisk party name.
  731. * \since 1.8
  732. *
  733. * \param pri_name PRI party name structure.
  734. * \param ast_name Asterisk party name structure.
  735. *
  736. * \return Nothing
  737. *
  738. * \note Assumes that pri_name has been previously memset to zero.
  739. */
  740. static void sig_pri_party_name_from_ast(struct pri_party_name *pri_name, const struct ast_party_name *ast_name)
  741. {
  742. if (!ast_name->valid) {
  743. return;
  744. }
  745. pri_name->valid = 1;
  746. pri_name->presentation = ast_to_pri_presentation(ast_name->presentation);
  747. pri_name->char_set = ast_to_pri_char_set(ast_name->char_set);
  748. if (!ast_strlen_zero(ast_name->str)) {
  749. ast_copy_string(pri_name->str, ast_name->str, sizeof(pri_name->str));
  750. }
  751. }
  752. /*!
  753. * \internal
  754. * \brief Fill in the PRI party number from the given asterisk party number.
  755. * \since 1.8
  756. *
  757. * \param pri_number PRI party number structure.
  758. * \param ast_number Asterisk party number structure.
  759. *
  760. * \return Nothing
  761. *
  762. * \note Assumes that pri_number has been previously memset to zero.
  763. */
  764. static void sig_pri_party_number_from_ast(struct pri_party_number *pri_number, const struct ast_party_number *ast_number)
  765. {
  766. if (!ast_number->valid) {
  767. return;
  768. }
  769. pri_number->valid = 1;
  770. pri_number->presentation = ast_to_pri_presentation(ast_number->presentation);
  771. pri_number->plan = ast_number->plan;
  772. if (!ast_strlen_zero(ast_number->str)) {
  773. ast_copy_string(pri_number->str, ast_number->str, sizeof(pri_number->str));
  774. }
  775. }
  776. /*!
  777. * \internal
  778. * \brief Fill in the PRI party id from the given asterisk party id.
  779. * \since 1.8
  780. *
  781. * \param pri_id PRI party id structure.
  782. * \param ast_id Asterisk party id structure.
  783. *
  784. * \return Nothing
  785. *
  786. * \note Assumes that pri_id has been previously memset to zero.
  787. */
  788. static void sig_pri_party_id_from_ast(struct pri_party_id *pri_id, const struct ast_party_id *ast_id)
  789. {
  790. sig_pri_party_name_from_ast(&pri_id->name, &ast_id->name);
  791. sig_pri_party_number_from_ast(&pri_id->number, &ast_id->number);
  792. #if defined(HAVE_PRI_SUBADDR)
  793. sig_pri_party_subaddress_from_ast(&pri_id->subaddress, &ast_id->subaddress);
  794. #endif /* defined(HAVE_PRI_SUBADDR) */
  795. }
  796. /*!
  797. * \internal
  798. * \brief Update the PRI redirecting information for the current call.
  799. * \since 1.8
  800. *
  801. * \param pvt sig_pri private channel structure.
  802. * \param ast Asterisk channel
  803. *
  804. * \return Nothing
  805. *
  806. * \note Assumes that the PRI lock is already obtained.
  807. */
  808. static void sig_pri_redirecting_update(struct sig_pri_chan *pvt, struct ast_channel *ast)
  809. {
  810. struct pri_party_redirecting pri_redirecting;
  811. /*! \todo XXX Original called data can be put in a channel data store that is inherited. */
  812. memset(&pri_redirecting, 0, sizeof(pri_redirecting));
  813. sig_pri_party_id_from_ast(&pri_redirecting.from, &ast->redirecting.from);
  814. sig_pri_party_id_from_ast(&pri_redirecting.to, &ast->redirecting.to);
  815. pri_redirecting.count = ast->redirecting.count;
  816. pri_redirecting.reason = ast_to_pri_reason(ast->redirecting.reason);
  817. pri_redirecting_update(pvt->pri->pri, pvt->call, &pri_redirecting);
  818. }
  819. /*!
  820. * \internal
  821. * \brief Reset DTMF detector.
  822. * \since 1.8
  823. *
  824. * \param p sig_pri channel structure.
  825. *
  826. * \return Nothing
  827. */
  828. static void sig_pri_dsp_reset_and_flush_digits(struct sig_pri_chan *p)
  829. {
  830. if (p->calls->dsp_reset_and_flush_digits) {
  831. p->calls->dsp_reset_and_flush_digits(p->chan_pvt);
  832. }
  833. }
  834. static int sig_pri_set_echocanceller(struct sig_pri_chan *p, int enable)
  835. {
  836. if (p->calls->set_echocanceller)
  837. return p->calls->set_echocanceller(p->chan_pvt, enable);
  838. else
  839. return -1;
  840. }
  841. static void sig_pri_fixup_chans(struct sig_pri_chan *old_chan, struct sig_pri_chan *new_chan)
  842. {
  843. if (old_chan->calls->fixup_chans)
  844. old_chan->calls->fixup_chans(old_chan->chan_pvt, new_chan->chan_pvt);
  845. }
  846. static int sig_pri_play_tone(struct sig_pri_chan *p, enum sig_pri_tone tone)
  847. {
  848. if (p->calls->play_tone)
  849. return p->calls->play_tone(p->chan_pvt, tone);
  850. else
  851. return -1;
  852. }
  853. static struct ast_channel *sig_pri_new_ast_channel(struct sig_pri_chan *p, int state, int ulaw, int transfercapability, char *exten, const struct ast_channel *requestor)
  854. {
  855. struct ast_channel *c;
  856. if (p->calls->new_ast_channel) {
  857. c = p->calls->new_ast_channel(p->chan_pvt, state, ulaw, exten, requestor);
  858. } else {
  859. return NULL;
  860. }
  861. if (!c) {
  862. return NULL;
  863. }
  864. if (!p->owner)
  865. p->owner = c;
  866. p->isidlecall = 0;
  867. p->alreadyhungup = 0;
  868. c->transfercapability = transfercapability;
  869. pbx_builtin_setvar_helper(c, "TRANSFERCAPABILITY",
  870. ast_transfercapability2str(transfercapability));
  871. if (transfercapability & AST_TRANS_CAP_DIGITAL) {
  872. sig_pri_set_digital(p, 1);
  873. }
  874. if (p->pri) {
  875. ast_mutex_lock(&p->pri->lock);
  876. sig_pri_span_devstate_changed(p->pri);
  877. ast_mutex_unlock(&p->pri->lock);
  878. }
  879. return c;
  880. }
  881. /*!
  882. * \internal
  883. * \brief Open the PRI channel media path.
  884. * \since 1.8
  885. *
  886. * \param p Channel private control structure.
  887. *
  888. * \return Nothing
  889. */
  890. static void sig_pri_open_media(struct sig_pri_chan *p)
  891. {
  892. if (p->no_b_channel) {
  893. return;
  894. }
  895. if (p->calls->open_media) {
  896. p->calls->open_media(p->chan_pvt);
  897. }
  898. }
  899. /*!
  900. * \internal
  901. * \brief Post an AMI B channel association event.
  902. * \since 1.8
  903. *
  904. * \param p Channel private control structure.
  905. *
  906. * \note Assumes the private and owner are locked.
  907. *
  908. * \return Nothing
  909. */
  910. static void sig_pri_ami_channel_event(struct sig_pri_chan *p)
  911. {
  912. if (p->calls->ami_channel_event) {
  913. p->calls->ami_channel_event(p->chan_pvt, p->owner);
  914. }
  915. }
  916. struct ast_channel *sig_pri_request(struct sig_pri_chan *p, enum sig_pri_law law, const struct ast_channel *requestor, int transfercapability)
  917. {
  918. struct ast_channel *ast;
  919. ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
  920. sig_pri_set_outgoing(p, 1);
  921. ast = sig_pri_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability, p->exten, requestor);
  922. if (!ast) {
  923. sig_pri_set_outgoing(p, 0);
  924. }
  925. return ast;
  926. }
  927. int pri_is_up(struct sig_pri_span *pri)
  928. {
  929. int x;
  930. for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
  931. if (pri->dchanavail[x] == DCHAN_AVAILABLE)
  932. return 1;
  933. }
  934. return 0;
  935. }
  936. static const char *pri_order(int level)
  937. {
  938. switch (level) {
  939. case 0:
  940. return "Primary";
  941. case 1:
  942. return "Secondary";
  943. case 2:
  944. return "Tertiary";
  945. case 3:
  946. return "Quaternary";
  947. default:
  948. return "<Unknown>";
  949. }
  950. }
  951. /* Returns index of the active dchan */
  952. static int pri_active_dchan_index(struct sig_pri_span *pri)
  953. {
  954. int x;
  955. for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
  956. if ((pri->dchans[x] == pri->pri))
  957. return x;
  958. }
  959. ast_log(LOG_WARNING, "No active dchan found!\n");
  960. return -1;
  961. }
  962. static void pri_find_dchan(struct sig_pri_span *pri)
  963. {
  964. struct pri *old;
  965. int oldslot = -1;
  966. int newslot = -1;
  967. int idx;
  968. old = pri->pri;
  969. for (idx = 0; idx < SIG_PRI_NUM_DCHANS; ++idx) {
  970. if (!pri->dchans[idx]) {
  971. /* No more D channels defined on the span. */
  972. break;
  973. }
  974. if (pri->dchans[idx] == old) {
  975. oldslot = idx;
  976. }
  977. if (newslot < 0 && pri->dchanavail[idx] == DCHAN_AVAILABLE) {
  978. newslot = idx;
  979. }
  980. }
  981. /* At this point, idx is a count of how many D-channels are defined on the span. */
  982. if (1 < idx) {
  983. /* We have several D-channels defined on the span. (NFAS PRI setup) */
  984. if (newslot < 0) {
  985. /* No D-channels available. Default to the primary D-channel. */
  986. newslot = 0;
  987. if (!pri->no_d_channels) {
  988. pri->no_d_channels = 1;
  989. if (old && oldslot != newslot) {
  990. ast_log(LOG_WARNING,
  991. "Span %d: No D-channels up! Switching selected D-channel from %s to %s.\n",
  992. pri->span, pri_order(oldslot), pri_order(newslot));
  993. } else {
  994. ast_log(LOG_WARNING, "Span %d: No D-channels up!\n", pri->span);
  995. }
  996. }
  997. } else {
  998. pri->no_d_channels = 0;
  999. }
  1000. if (old && oldslot != newslot) {
  1001. ast_log(LOG_NOTICE,
  1002. "Switching selected D-channel from %s (fd %d) to %s (fd %d)!\n",
  1003. pri_order(oldslot), pri->fds[oldslot],
  1004. pri_order(newslot), pri->fds[newslot]);
  1005. }
  1006. } else {
  1007. if (newslot < 0) {
  1008. /* The only D-channel is not up. */
  1009. newslot = 0;
  1010. if (!pri->no_d_channels) {
  1011. pri->no_d_channels = 1;
  1012. /*
  1013. * This is annoying to see on non-persistent layer 2
  1014. * connections. Let's not complain in that case.
  1015. */
  1016. if (pri->sig != SIG_BRI_PTMP) {
  1017. ast_log(LOG_WARNING, "Span %d: D-channel is down!\n", pri->span);
  1018. }
  1019. }
  1020. } else {
  1021. pri->no_d_channels = 0;
  1022. }
  1023. }
  1024. pri->pri = pri->dchans[newslot];
  1025. }
  1026. /*!
  1027. * \internal
  1028. * \brief Determine if a private channel structure is in use.
  1029. * \since 1.8
  1030. *
  1031. * \param pvt Channel to determine if in use.
  1032. *
  1033. * \return TRUE if the channel is in use.
  1034. */
  1035. static int sig_pri_is_chan_in_use(struct sig_pri_chan *pvt)
  1036. {
  1037. return pvt->owner || pvt->call || pvt->allocated || pvt->inalarm
  1038. || pvt->resetting != SIG_PRI_RESET_IDLE;
  1039. }
  1040. /*!
  1041. * \brief Determine if a private channel structure is available.
  1042. * \since 1.8
  1043. *
  1044. * \param pvt Channel to determine if available.
  1045. *
  1046. * \return TRUE if the channel is available.
  1047. */
  1048. int sig_pri_is_chan_available(struct sig_pri_chan *pvt)
  1049. {
  1050. return !sig_pri_is_chan_in_use(pvt)
  1051. #if defined(HAVE_PRI_SERVICE_MESSAGES)
  1052. /* And not out-of-service */
  1053. && !pvt->service_status
  1054. #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
  1055. ;
  1056. }
  1057. /*!
  1058. * \internal
  1059. * \brief Obtain the sig_pri owner channel lock if the owner exists.
  1060. * \since 1.8
  1061. *
  1062. * \param pri PRI span control structure.
  1063. * \param chanpos Channel position in the span.
  1064. *
  1065. * \note Assumes the pri->lock is already obtained.
  1066. * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
  1067. *
  1068. * \return Nothing
  1069. */
  1070. static void sig_pri_lock_owner(struct sig_pri_span *pri, int chanpos)
  1071. {
  1072. for (;;) {
  1073. if (!pri->pvts[chanpos]->owner) {
  1074. /* There is no owner lock to get. */
  1075. break;
  1076. }
  1077. if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
  1078. /* We got the lock */
  1079. break;
  1080. }
  1081. /* Avoid deadlock */
  1082. sig_pri_unlock_private(pri->pvts[chanpos]);
  1083. DEADLOCK_AVOIDANCE(&pri->lock);
  1084. sig_pri_lock_private(pri->pvts[chanpos]);
  1085. }
  1086. }
  1087. /*!
  1088. * \internal
  1089. * \brief Queue the given frame onto the owner channel.
  1090. * \since 1.8
  1091. *
  1092. * \param pri PRI span control structure.
  1093. * \param chanpos Channel position in the span.
  1094. * \param frame Frame to queue onto the owner channel.
  1095. *
  1096. * \note Assumes the pri->lock is already obtained.
  1097. * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
  1098. *
  1099. * \return Nothing
  1100. */
  1101. static void pri_queue_frame(struct sig_pri_span *pri, int chanpos, struct ast_frame *frame)
  1102. {
  1103. sig_pri_lock_owner(pri, chanpos);
  1104. if (pri->pvts[chanpos]->owner) {
  1105. ast_queue_frame(pri->pvts[chanpos]->owner, frame);
  1106. ast_channel_unlock(pri->pvts[chanpos]->owner);
  1107. }
  1108. }
  1109. /*!
  1110. * \internal
  1111. * \brief Queue a control frame of the specified subclass onto the owner channel.
  1112. * \since 1.8
  1113. *
  1114. * \param pri PRI span control structure.
  1115. * \param chanpos Channel position in the span.
  1116. * \param subclass Control frame subclass to queue onto the owner channel.
  1117. *
  1118. * \note Assumes the pri->lock is already obtained.
  1119. * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
  1120. *
  1121. * \return Nothing
  1122. */
  1123. static void pri_queue_control(struct sig_pri_span *pri, int chanpos, int subclass)
  1124. {
  1125. struct ast_frame f = {AST_FRAME_CONTROL, };
  1126. struct sig_pri_chan *p = pri->pvts[chanpos];
  1127. if (p->calls->queue_control) {
  1128. p->calls->queue_control(p->chan_pvt, subclass);
  1129. }
  1130. f.subclass.integer = subclass;
  1131. pri_queue_frame(pri, chanpos, &f);
  1132. }
  1133. /*!
  1134. * \internal
  1135. * \brief Find the channel associated with the libpri call.
  1136. * \since 10.0
  1137. *
  1138. * \param pri PRI span control structure.
  1139. * \param call LibPRI opaque call pointer to find.
  1140. *
  1141. * \note Assumes the pri->lock is already obtained.
  1142. *
  1143. * \retval array-index into private pointer array on success.
  1144. * \retval -1 on error.
  1145. */
  1146. static int pri_find_principle_by_call(struct sig_pri_span *pri, q931_call *call)
  1147. {
  1148. int idx;
  1149. if (!call) {
  1150. /* Cannot find a call without a call. */
  1151. return -1;
  1152. }
  1153. for (idx = 0; idx < pri->numchans; ++idx) {
  1154. if (pri->pvts[idx] && pri->pvts[idx]->call == call) {
  1155. /* Found the principle */
  1156. return idx;
  1157. }
  1158. }
  1159. return -1;
  1160. }
  1161. /*!
  1162. * \internal
  1163. * \brief Kill the call.
  1164. * \since 10.0
  1165. *
  1166. * \param pri PRI span control structure.
  1167. * \param call LibPRI opaque call pointer to find.
  1168. * \param cause Reason call was killed.
  1169. *
  1170. * \note Assumes the pvt->pri->lock is already obtained.
  1171. *
  1172. * \return Nothing
  1173. */
  1174. static void sig_pri_kill_call(struct sig_pri_span *pri, q931_call *call, int cause)
  1175. {
  1176. int chanpos;
  1177. chanpos = pri_find_principle_by_call(pri, call);
  1178. if (chanpos < 0) {
  1179. pri_hangup(pri->pri, call, cause);
  1180. return;
  1181. }
  1182. sig_pri_lock_private(pri->pvts[chanpos]);
  1183. if (!pri->pvts[chanpos]->owner) {
  1184. pri_hangup(pri->pri, call, cause);
  1185. pri->pvts[chanpos]->call = NULL;
  1186. sig_pri_unlock_private(pri->pvts[chanpos]);
  1187. sig_pri_span_devstate_changed(pri);
  1188. return;
  1189. }
  1190. pri->pvts[chanpos]->owner->hangupcause = cause;
  1191. pri_queue_control(pri, chanpos, AST_CONTROL_HANGUP);
  1192. sig_pri_unlock_private(pri->pvts[chanpos]);
  1193. }
  1194. /*!
  1195. * \internal
  1196. * \brief Find the private structure for the libpri call.
  1197. *
  1198. * \param pri PRI span control structure.
  1199. * \param channel LibPRI encoded channel ID.
  1200. * \param call LibPRI opaque call pointer.
  1201. *
  1202. * \note Assumes the pri->lock is already obtained.
  1203. *
  1204. * \retval array-index into private pointer array on success.
  1205. * \retval -1 on error.
  1206. */
  1207. static int pri_find_principle(struct sig_pri_span *pri, int channel, q931_call *call)
  1208. {
  1209. int x;
  1210. int span;
  1211. int principle;
  1212. int prioffset;
  1213. if (channel < 0) {
  1214. /* Channel is not picked yet. */
  1215. return -1;
  1216. }
  1217. prioffset = PRI_CHANNEL(channel);
  1218. if (!prioffset || (channel & PRI_HELD_CALL)) {
  1219. /* Find the call waiting call or held call. */
  1220. return pri_find_principle_by_call(pri, call);
  1221. }
  1222. span = PRI_SPAN(channel);
  1223. if (!(channel & PRI_EXPLICIT)) {
  1224. int index;
  1225. index = pri_active_dchan_index(pri);
  1226. if (index == -1) {
  1227. return -1;
  1228. }
  1229. span = pri->dchan_logical_span[index];
  1230. }
  1231. principle = -1;
  1232. for (x = 0; x < pri->numchans; x++) {
  1233. if (pri->pvts[x]
  1234. && pri->pvts[x]->prioffset == prioffset
  1235. && pri->pvts[x]->logicalspan == span
  1236. && !pri->pvts[x]->no_b_channel) {
  1237. principle = x;
  1238. break;
  1239. }
  1240. }
  1241. return principle;
  1242. }
  1243. /*!
  1244. * \internal
  1245. * \brief Fixup the private structure associated with the libpri call.
  1246. *
  1247. * \param pri PRI span control structure.
  1248. * \param principle Array-index into private array to move call to if not already there.
  1249. * \param call LibPRI opaque call pointer to find if need to move call.
  1250. *
  1251. * \note Assumes the pri->lock is already obtained.
  1252. *
  1253. * \retval principle on success.
  1254. * \retval -1 on error.
  1255. */
  1256. static int pri_fixup_principle(struct sig_pri_span *pri, int principle, q931_call *call)
  1257. {
  1258. int x;
  1259. if (principle < 0 || pri->numchans <= principle) {
  1260. /* Out of rannge */
  1261. return -1;
  1262. }
  1263. if (!call) {
  1264. /* No call */
  1265. return principle;
  1266. }
  1267. if (pri->pvts[principle] && pri->pvts[principle]->call == call) {
  1268. /* Call is already on the specified principle. */
  1269. return principle;
  1270. }
  1271. /* Find the old principle location. */
  1272. for (x = 0; x < pri->numchans; x++) {
  1273. struct sig_pri_chan *new_chan;
  1274. struct sig_pri_chan *old_chan;
  1275. if (!pri->pvts[x] || pri->pvts[x]->call != call) {
  1276. continue;
  1277. }
  1278. /* Found our call */
  1279. new_chan = pri->pvts[principle];
  1280. old_chan = pri->pvts[x];
  1281. /* Get locks to safely move to the new private structure. */
  1282. sig_pri_lock_private(old_chan);
  1283. sig_pri_lock_owner(pri, x);
  1284. sig_pri_lock_private(new_chan);
  1285. ast_verb(3, "Moving call (%s) from channel %d to %d.\n",
  1286. old_chan->owner ? old_chan->owner->name : "",
  1287. old_chan->channel, new_chan->channel);
  1288. if (!sig_pri_is_chan_available(new_chan)) {
  1289. ast_log(LOG_WARNING,
  1290. "Can't move call (%s) from channel %d to %d. It is already in use.\n",
  1291. old_chan->owner ? old_chan->owner->name : "",
  1292. old_chan->channel, new_chan->channel);
  1293. sig_pri_unlock_private(new_chan);
  1294. if (old_chan->owner) {
  1295. ast_channel_unlock(old_chan->owner);
  1296. }
  1297. sig_pri_unlock_private(old_chan);
  1298. return -1;
  1299. }
  1300. sig_pri_fixup_chans(old_chan, new_chan);
  1301. /* Fix it all up now */
  1302. new_chan->owner = old_chan->owner;
  1303. old_chan->owner = NULL;
  1304. new_chan->call = old_chan->call;
  1305. old_chan->call = NULL;
  1306. /* Transfer flags from the old channel. */
  1307. #if defined(HAVE_PRI_AOC_EVENTS)
  1308. new_chan->aoc_s_request_invoke_id_valid = old_chan->aoc_s_request_invoke_id_valid;
  1309. new_chan->waiting_for_aoce = old_chan->waiting_for_aoce;
  1310. new_chan->holding_aoce = old_chan->holding_aoce;
  1311. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  1312. new_chan->alreadyhungup = old_chan->alreadyhungup;
  1313. new_chan->isidlecall = old_chan->isidlecall;
  1314. new_chan->progress = old_chan->progress;
  1315. new_chan->allocated = old_chan->allocated;
  1316. new_chan->outgoing = old_chan->outgoing;
  1317. new_chan->digital = old_chan->digital;
  1318. #if defined(HAVE_PRI_CALL_WAITING)
  1319. new_chan->is_call_waiting = old_chan->is_call_waiting;
  1320. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  1321. #if defined(HAVE_PRI_AOC_EVENTS)
  1322. old_chan->aoc_s_request_invoke_id_valid = 0;
  1323. old_chan->waiting_for_aoce = 0;
  1324. old_chan->holding_aoce = 0;
  1325. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  1326. old_chan->alreadyhungup = 0;
  1327. old_chan->isidlecall = 0;
  1328. old_chan->progress = 0;
  1329. old_chan->allocated = 0;
  1330. old_chan->outgoing = 0;
  1331. old_chan->digital = 0;
  1332. #if defined(HAVE_PRI_CALL_WAITING)
  1333. old_chan->is_call_waiting = 0;
  1334. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  1335. /* More stuff to transfer to the new channel. */
  1336. new_chan->call_level = old_chan->call_level;
  1337. old_chan->call_level = SIG_PRI_CALL_LEVEL_IDLE;
  1338. #if defined(HAVE_PRI_REVERSE_CHARGE)
  1339. new_chan->reverse_charging_indication = old_chan->reverse_charging_indication;
  1340. #endif /* defined(HAVE_PRI_REVERSE_CHARGE) */
  1341. #if defined(HAVE_PRI_SETUP_KEYPAD)
  1342. strcpy(new_chan->keypad_digits, old_chan->keypad_digits);
  1343. #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
  1344. strcpy(new_chan->deferred_digits, old_chan->deferred_digits);
  1345. strcpy(new_chan->moh_suggested, old_chan->moh_suggested);
  1346. new_chan->moh_state = old_chan->moh_state;
  1347. old_chan->moh_state = SIG_PRI_MOH_STATE_IDLE;
  1348. #if defined(HAVE_PRI_AOC_EVENTS)
  1349. new_chan->aoc_s_request_invoke_id = old_chan->aoc_s_request_invoke_id;
  1350. new_chan->aoc_e = old_chan->aoc_e;
  1351. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  1352. strcpy(new_chan->user_tag, old_chan->user_tag);
  1353. if (new_chan->no_b_channel) {
  1354. /* Copy the real channel configuration to the no B channel interface. */
  1355. new_chan->hidecallerid = old_chan->hidecallerid;
  1356. new_chan->hidecalleridname = old_chan->hidecalleridname;
  1357. new_chan->immediate = old_chan->immediate;
  1358. new_chan->priexclusive = old_chan->priexclusive;
  1359. new_chan->priindication_oob = old_chan->priindication_oob;
  1360. new_chan->use_callerid = old_chan->use_callerid;
  1361. new_chan->use_callingpres = old_chan->use_callingpres;
  1362. new_chan->stripmsd = old_chan->stripmsd;
  1363. strcpy(new_chan->context, old_chan->context);
  1364. strcpy(new_chan->mohinterpret, old_chan->mohinterpret);
  1365. /* Become a member of the old channel span/trunk-group. */
  1366. new_chan->logicalspan = old_chan->logicalspan;
  1367. new_chan->mastertrunkgroup = old_chan->mastertrunkgroup;
  1368. } else if (old_chan->no_b_channel) {
  1369. /*
  1370. * We are transitioning from a held/call-waiting channel to a
  1371. * real channel so we need to make sure that the media path is
  1372. * open. (Needed especially if the channel is natively
  1373. * bridged.)
  1374. */
  1375. sig_pri_open_media(new_chan);
  1376. }
  1377. if (new_chan->owner) {
  1378. sig_pri_ami_channel_event(new_chan);
  1379. }
  1380. sig_pri_unlock_private(old_chan);
  1381. if (new_chan->owner) {
  1382. ast_channel_unlock(new_chan->owner);
  1383. }
  1384. sig_pri_unlock_private(new_chan);
  1385. return principle;
  1386. }
  1387. ast_verb(3, "Call specified, but not found.\n");
  1388. return -1;
  1389. }
  1390. /*!
  1391. * \internal
  1392. * \brief Find and fixup the private structure associated with the libpri call.
  1393. *
  1394. * \param pri PRI span control structure.
  1395. * \param channel LibPRI encoded channel ID.
  1396. * \param call LibPRI opaque call pointer.
  1397. *
  1398. * \details
  1399. * This is a combination of pri_find_principle() and pri_fixup_principle()
  1400. * to reduce code redundancy and to make handling several PRI_EVENT_xxx's
  1401. * consistent for the current architecture.
  1402. *
  1403. * \note Assumes the pri->lock is already obtained.
  1404. *
  1405. * \retval array-index into private pointer array on success.
  1406. * \retval -1 on error.
  1407. */
  1408. static int pri_find_fixup_principle(struct sig_pri_span *pri, int channel, q931_call *call)
  1409. {
  1410. int chanpos;
  1411. chanpos = pri_find_principle(pri, channel, call);
  1412. if (chanpos < 0) {
  1413. ast_log(LOG_WARNING, "Span %d: PRI requested channel %d/%d is unconfigured.\n",
  1414. pri->span, PRI_SPAN(channel), PRI_CHANNEL(channel));
  1415. sig_pri_kill_call(pri, call, PRI_CAUSE_IDENTIFIED_CHANNEL_NOTEXIST);
  1416. return -1;
  1417. }
  1418. chanpos = pri_fixup_principle(pri, chanpos, call);
  1419. if (chanpos < 0) {
  1420. ast_log(LOG_WARNING, "Span %d: PRI requested channel %d/%d is not available.\n",
  1421. pri->span, PRI_SPAN(channel), PRI_CHANNEL(channel));
  1422. /*
  1423. * Using Q.931 section 5.2.3.1 b) as the reason for picking
  1424. * PRI_CAUSE_CHANNEL_UNACCEPTABLE. Receiving a
  1425. * PRI_CAUSE_REQUESTED_CHAN_UNAVAIL would cause us to restart
  1426. * that channel (which is not specified by Q.931) and kill some
  1427. * other call which would be bad.
  1428. */
  1429. sig_pri_kill_call(pri, call, PRI_CAUSE_CHANNEL_UNACCEPTABLE);
  1430. return -1;
  1431. }
  1432. return chanpos;
  1433. }
  1434. static char * redirectingreason2str(int redirectingreason)
  1435. {
  1436. switch (redirectingreason) {
  1437. case 0:
  1438. return "UNKNOWN";
  1439. case 1:
  1440. return "BUSY";
  1441. case 2:
  1442. return "NO_REPLY";
  1443. case 0xF:
  1444. return "UNCONDITIONAL";
  1445. default:
  1446. return "NOREDIRECT";
  1447. }
  1448. }
  1449. static char *dialplan2str(int dialplan)
  1450. {
  1451. if (dialplan == -1) {
  1452. return("Dynamically set dialplan in ISDN");
  1453. }
  1454. return (pri_plan2str(dialplan));
  1455. }
  1456. /*!
  1457. * \internal
  1458. * \brief Apply numbering plan prefix to the given number.
  1459. *
  1460. * \param buf Buffer to put number into.
  1461. * \param size Size of given buffer.
  1462. * \param pri PRI span control structure.
  1463. * \param number Number to apply numbering plan.
  1464. * \param plan Numbering plan to apply.
  1465. *
  1466. * \return Nothing
  1467. */
  1468. static void apply_plan_to_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, int plan)
  1469. {
  1470. switch (plan) {
  1471. case PRI_INTERNATIONAL_ISDN: /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */
  1472. snprintf(buf, size, "%s%s", pri->internationalprefix, number);
  1473. break;
  1474. case PRI_NATIONAL_ISDN: /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */
  1475. snprintf(buf, size, "%s%s", pri->nationalprefix, number);
  1476. break;
  1477. case PRI_LOCAL_ISDN: /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */
  1478. snprintf(buf, size, "%s%s", pri->localprefix, number);
  1479. break;
  1480. case PRI_PRIVATE: /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */
  1481. snprintf(buf, size, "%s%s", pri->privateprefix, number);
  1482. break;
  1483. case PRI_UNKNOWN: /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */
  1484. snprintf(buf, size, "%s%s", pri->unknownprefix, number);
  1485. break;
  1486. default: /* other Q.931 dialplan => don't twiddle with callingnum */
  1487. snprintf(buf, size, "%s", number);
  1488. break;
  1489. }
  1490. }
  1491. /*!
  1492. * \internal
  1493. * \brief Apply numbering plan prefix to the given number if the number exists.
  1494. *
  1495. * \param buf Buffer to put number into.
  1496. * \param size Size of given buffer.
  1497. * \param pri PRI span control structure.
  1498. * \param number Number to apply numbering plan.
  1499. * \param plan Numbering plan to apply.
  1500. *
  1501. * \return Nothing
  1502. */
  1503. static void apply_plan_to_existing_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, int plan)
  1504. {
  1505. /* Make sure a number exists so the prefix isn't placed on an empty string. */
  1506. if (ast_strlen_zero(number)) {
  1507. if (size) {
  1508. *buf = '\0';
  1509. }
  1510. return;
  1511. }
  1512. apply_plan_to_number(buf, size, pri, number, plan);
  1513. }
  1514. /*!
  1515. * \internal
  1516. * \brief Restart the next channel we think is idle on the span.
  1517. *
  1518. * \param pri PRI span control structure.
  1519. *
  1520. * \note Assumes the pri->lock is already obtained.
  1521. *
  1522. * \return Nothing
  1523. */
  1524. static void pri_check_restart(struct sig_pri_span *pri)
  1525. {
  1526. #if defined(HAVE_PRI_SERVICE_MESSAGES)
  1527. unsigned why;
  1528. #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
  1529. for (++pri->resetpos; pri->resetpos < pri->numchans; ++pri->resetpos) {
  1530. if (!pri->pvts[pri->resetpos]
  1531. || pri->pvts[pri->resetpos]->no_b_channel
  1532. || sig_pri_is_chan_in_use(pri->pvts[pri->resetpos])) {
  1533. continue;
  1534. }
  1535. #if defined(HAVE_PRI_SERVICE_MESSAGES)
  1536. why = pri->pvts[pri->resetpos]->service_status;
  1537. if (why) {
  1538. ast_log(LOG_NOTICE,
  1539. "Span %d: channel %d out-of-service (reason: %s), not sending RESTART\n",
  1540. pri->span, pri->pvts[pri->resetpos]->channel,
  1541. (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
  1542. continue;
  1543. }
  1544. #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
  1545. break;
  1546. }
  1547. if (pri->resetpos < pri->numchans) {
  1548. /* Mark the channel as resetting and restart it */
  1549. pri->pvts[pri->resetpos]->resetting = SIG_PRI_RESET_ACTIVE;
  1550. pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
  1551. } else {
  1552. pri->resetting = 0;
  1553. time(&pri->lastreset);
  1554. sig_pri_span_devstate_changed(pri);
  1555. }
  1556. }
  1557. #if defined(HAVE_PRI_CALL_WAITING)
  1558. /*!
  1559. * \internal
  1560. * \brief Init the private channel configuration using the span controller.
  1561. * \since 1.8
  1562. *
  1563. * \param pvt Channel to init the configuration.
  1564. * \param pri PRI span control structure.
  1565. *
  1566. * \note Assumes the pri->lock is already obtained.
  1567. *
  1568. * \return Nothing
  1569. */
  1570. static void sig_pri_init_config(struct sig_pri_chan *pvt, struct sig_pri_span *pri)
  1571. {
  1572. pvt->stripmsd = pri->ch_cfg.stripmsd;
  1573. pvt->hidecallerid = pri->ch_cfg.hidecallerid;
  1574. pvt->hidecalleridname = pri->ch_cfg.hidecalleridname;
  1575. pvt->immediate = pri->ch_cfg.immediate;
  1576. pvt->priexclusive = pri->ch_cfg.priexclusive;
  1577. pvt->priindication_oob = pri->ch_cfg.priindication_oob;
  1578. pvt->use_callerid = pri->ch_cfg.use_callerid;
  1579. pvt->use_callingpres = pri->ch_cfg.use_callingpres;
  1580. ast_copy_string(pvt->context, pri->ch_cfg.context, sizeof(pvt->context));
  1581. ast_copy_string(pvt->mohinterpret, pri->ch_cfg.mohinterpret, sizeof(pvt->mohinterpret));
  1582. if (pri->calls->init_config) {
  1583. pri->calls->init_config(pvt->chan_pvt, pri);
  1584. }
  1585. }
  1586. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  1587. /*!
  1588. * \internal
  1589. * \brief Find an empty B-channel interface to use.
  1590. *
  1591. * \param pri PRI span control structure.
  1592. * \param backwards TRUE if the search starts from higher channels.
  1593. *
  1594. * \note Assumes the pri->lock is already obtained.
  1595. *
  1596. * \retval array-index into private pointer array on success.
  1597. * \retval -1 on error.
  1598. */
  1599. static int pri_find_empty_chan(struct sig_pri_span *pri, int backwards)
  1600. {
  1601. int x;
  1602. if (backwards)
  1603. x = pri->numchans;
  1604. else
  1605. x = 0;
  1606. for (;;) {
  1607. if (backwards && (x < 0))
  1608. break;
  1609. if (!backwards && (x >= pri->numchans))
  1610. break;
  1611. if (pri->pvts[x]
  1612. && !pri->pvts[x]->no_b_channel
  1613. && sig_pri_is_chan_available(pri->pvts[x])) {
  1614. ast_debug(1, "Found empty available channel %d/%d\n",
  1615. pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
  1616. return x;
  1617. }
  1618. if (backwards)
  1619. x--;
  1620. else
  1621. x++;
  1622. }
  1623. return -1;
  1624. }
  1625. #if defined(HAVE_PRI_CALL_HOLD)
  1626. /*!
  1627. * \internal
  1628. * \brief Find or create an empty no-B-channel interface to use.
  1629. * \since 1.8
  1630. *
  1631. * \param pri PRI span control structure.
  1632. *
  1633. * \note Assumes the pri->lock is already obtained.
  1634. *
  1635. * \retval array-index into private pointer array on success.
  1636. * \retval -1 on error.
  1637. */
  1638. static int pri_find_empty_nobch(struct sig_pri_span *pri)
  1639. {
  1640. int idx;
  1641. for (idx = 0; idx < pri->numchans; ++idx) {
  1642. if (pri->pvts[idx]
  1643. && pri->pvts[idx]->no_b_channel
  1644. && sig_pri_is_chan_available(pri->pvts[idx])) {
  1645. ast_debug(1, "Found empty available no B channel interface\n");
  1646. return idx;
  1647. }
  1648. }
  1649. /* Need to create a new interface. */
  1650. if (pri->calls->new_nobch_intf) {
  1651. idx = pri->calls->new_nobch_intf(pri);
  1652. } else {
  1653. idx = -1;
  1654. }
  1655. return idx;
  1656. }
  1657. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  1658. static void *do_idle_thread(void *v_pvt)
  1659. {
  1660. struct sig_pri_chan *pvt = v_pvt;
  1661. struct ast_channel *chan = pvt->owner;
  1662. struct ast_frame *f;
  1663. char ex[80];
  1664. /* Wait up to 30 seconds for an answer */
  1665. int timeout_ms = 30000;
  1666. int ms;
  1667. struct timeval start;
  1668. ast_verb(3, "Initiating idle call on channel %s\n", chan->name);
  1669. snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
  1670. if (ast_call(chan, ex, 0)) {
  1671. ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
  1672. ast_hangup(chan);
  1673. return NULL;
  1674. }
  1675. start = ast_tvnow();
  1676. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  1677. if (ast_waitfor(chan, ms) <= 0) {
  1678. break;
  1679. }
  1680. f = ast_read(chan);
  1681. if (!f) {
  1682. /* Got hangup */
  1683. break;
  1684. }
  1685. if (f->frametype == AST_FRAME_CONTROL) {
  1686. switch (f->subclass.integer) {
  1687. case AST_CONTROL_ANSWER:
  1688. /* Launch the PBX */
  1689. ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
  1690. ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
  1691. chan->priority = 1;
  1692. ast_verb(4, "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
  1693. ast_pbx_run(chan);
  1694. /* It's already hungup, return immediately */
  1695. return NULL;
  1696. case AST_CONTROL_BUSY:
  1697. ast_verb(4, "Idle channel '%s' busy, waiting...\n", chan->name);
  1698. break;
  1699. case AST_CONTROL_CONGESTION:
  1700. ast_verb(4, "Idle channel '%s' congested, waiting...\n", chan->name);
  1701. break;
  1702. };
  1703. }
  1704. ast_frfree(f);
  1705. }
  1706. /* Hangup the channel since nothing happend */
  1707. ast_hangup(chan);
  1708. return NULL;
  1709. }
  1710. static void *pri_ss_thread(void *data)
  1711. {
  1712. struct sig_pri_chan *p = data;
  1713. struct ast_channel *chan = p->owner;
  1714. char exten[AST_MAX_EXTENSION];
  1715. int res;
  1716. int len;
  1717. int timeout;
  1718. if (!chan) {
  1719. /* We lost the owner before we could get started. */
  1720. return NULL;
  1721. }
  1722. /*
  1723. * In the bizarre case where the channel has become a zombie before we
  1724. * even get started here, abort safely.
  1725. */
  1726. if (!chan->tech_pvt) {
  1727. ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
  1728. ast_hangup(chan);
  1729. return NULL;
  1730. }
  1731. ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
  1732. sig_pri_dsp_reset_and_flush_digits(p);
  1733. /* Now loop looking for an extension */
  1734. ast_copy_string(exten, p->exten, sizeof(exten));
  1735. len = strlen(exten);
  1736. res = 0;
  1737. while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
  1738. if (len && !ast_ignore_pattern(chan->context, exten))
  1739. sig_pri_play_tone(p, -1);
  1740. else
  1741. sig_pri_play_tone(p, SIG_PRI_TONE_DIALTONE);
  1742. if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
  1743. timeout = pri_matchdigittimeout;
  1744. else
  1745. timeout = pri_gendigittimeout;
  1746. res = ast_waitfordigit(chan, timeout);
  1747. if (res < 0) {
  1748. ast_debug(1, "waitfordigit returned < 0...\n");
  1749. ast_hangup(chan);
  1750. return NULL;
  1751. } else if (res) {
  1752. exten[len++] = res;
  1753. exten[len] = '\0';
  1754. } else
  1755. break;
  1756. }
  1757. /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
  1758. if (ast_strlen_zero(exten)) {
  1759. ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
  1760. exten[0] = 's';
  1761. exten[1] = '\0';
  1762. } else {
  1763. ast_free(chan->dialed.number.str);
  1764. chan->dialed.number.str = ast_strdup(exten);
  1765. if (p->pri->append_msn_to_user_tag && p->pri->nodetype != PRI_NETWORK) {
  1766. /*
  1767. * Update the user tag for party id's from this device for this call
  1768. * now that we have a complete MSN from the network.
  1769. */
  1770. snprintf(p->user_tag, sizeof(p->user_tag), "%s_%s", p->pri->initial_user_tag,
  1771. exten);
  1772. ast_free(chan->caller.id.tag);
  1773. chan->caller.id.tag = ast_strdup(p->user_tag);
  1774. }
  1775. }
  1776. sig_pri_play_tone(p, -1);
  1777. if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
  1778. /* Start the real PBX */
  1779. ast_copy_string(chan->exten, exten, sizeof(chan->exten));
  1780. sig_pri_dsp_reset_and_flush_digits(p);
  1781. #if defined(ISSUE_16789)
  1782. /*
  1783. * Conditionaled out this code to effectively revert the Mantis
  1784. * issue 16789 change. It breaks overlap dialing through
  1785. * Asterisk. There is not enough information available at this
  1786. * point to know if dialing is complete. The
  1787. * ast_exists_extension(), ast_matchmore_extension(), and
  1788. * ast_canmatch_extension() calls are not adequate to detect a
  1789. * dial through extension pattern of "_9!".
  1790. *
  1791. * Workaround is to use the dialplan Proceeding() application
  1792. * early on non-dial through extensions.
  1793. */
  1794. if ((p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
  1795. && !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
  1796. sig_pri_lock_private(p);
  1797. if (p->pri->pri) {
  1798. pri_grab(p, p->pri);
  1799. if (p->call_level < SIG_PRI_CALL_LEVEL_PROCEEDING) {
  1800. p->call_level = SIG_PRI_CALL_LEVEL_PROCEEDING;
  1801. }
  1802. pri_proceeding(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 0);
  1803. pri_rel(p->pri);
  1804. }
  1805. sig_pri_unlock_private(p);
  1806. }
  1807. #endif /* defined(ISSUE_16789) */
  1808. sig_pri_set_echocanceller(p, 1);
  1809. ast_setstate(chan, AST_STATE_RING);
  1810. res = ast_pbx_run(chan);
  1811. if (res) {
  1812. ast_log(LOG_WARNING, "PBX exited non-zero!\n");
  1813. }
  1814. } else {
  1815. ast_debug(1, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
  1816. chan->hangupcause = AST_CAUSE_UNALLOCATED;
  1817. ast_hangup(chan);
  1818. p->exten[0] = '\0';
  1819. /* Since we send release complete here, we won't get one */
  1820. p->call = NULL;
  1821. ast_mutex_lock(&p->pri->lock);
  1822. sig_pri_span_devstate_changed(p->pri);
  1823. ast_mutex_unlock(&p->pri->lock);
  1824. }
  1825. return NULL;
  1826. }
  1827. void pri_event_alarm(struct sig_pri_span *pri, int index, int before_start_pri)
  1828. {
  1829. pri->dchanavail[index] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
  1830. if (!before_start_pri) {
  1831. pri_find_dchan(pri);
  1832. }
  1833. }
  1834. void pri_event_noalarm(struct sig_pri_span *pri, int index, int before_start_pri)
  1835. {
  1836. pri->dchanavail[index] |= DCHAN_NOTINALARM;
  1837. if (!before_start_pri)
  1838. pri_restart(pri->dchans[index]);
  1839. }
  1840. /*!
  1841. * \internal
  1842. * \brief Convert libpri party name into asterisk party name.
  1843. * \since 1.8
  1844. *
  1845. * \param ast_name Asterisk party name structure to fill. Must already be set initialized.
  1846. * \param pri_name libpri party name structure containing source information.
  1847. *
  1848. * \note The filled in ast_name structure needs to be destroyed by
  1849. * ast_party_name_free() when it is no longer needed.
  1850. *
  1851. * \return Nothing
  1852. */
  1853. static void sig_pri_party_name_convert(struct ast_party_name *ast_name, const struct pri_party_name *pri_name)
  1854. {
  1855. ast_name->str = ast_strdup(pri_name->str);
  1856. ast_name->char_set = pri_to_ast_char_set(pri_name->char_set);
  1857. ast_name->presentation = pri_to_ast_presentation(pri_name->presentation);
  1858. ast_name->valid = 1;
  1859. }
  1860. /*!
  1861. * \internal
  1862. * \brief Convert libpri party number into asterisk party number.
  1863. * \since 1.8
  1864. *
  1865. * \param ast_number Asterisk party number structure to fill. Must already be set initialized.
  1866. * \param pri_number libpri party number structure containing source information.
  1867. * \param pri PRI span control structure.
  1868. *
  1869. * \note The filled in ast_number structure needs to be destroyed by
  1870. * ast_party_number_free() when it is no longer needed.
  1871. *
  1872. * \return Nothing
  1873. */
  1874. static void sig_pri_party_number_convert(struct ast_party_number *ast_number, const struct pri_party_number *pri_number, struct sig_pri_span *pri)
  1875. {
  1876. char number[AST_MAX_EXTENSION];
  1877. apply_plan_to_existing_number(number, sizeof(number), pri, pri_number->str,
  1878. pri_number->plan);
  1879. ast_number->str = ast_strdup(number);
  1880. ast_number->plan = pri_number->plan;
  1881. ast_number->presentation = pri_to_ast_presentation(pri_number->presentation);
  1882. ast_number->valid = 1;
  1883. }
  1884. /*!
  1885. * \internal
  1886. * \brief Convert libpri party id into asterisk party id.
  1887. * \since 1.8
  1888. *
  1889. * \param ast_id Asterisk party id structure to fill. Must already be set initialized.
  1890. * \param pri_id libpri party id structure containing source information.
  1891. * \param pri PRI span control structure.
  1892. *
  1893. * \note The filled in ast_id structure needs to be destroyed by
  1894. * ast_party_id_free() when it is no longer needed.
  1895. *
  1896. * \return Nothing
  1897. */
  1898. static void sig_pri_party_id_convert(struct ast_party_id *ast_id, const struct pri_party_id *pri_id, struct sig_pri_span *pri)
  1899. {
  1900. if (pri_id->name.valid) {
  1901. sig_pri_party_name_convert(&ast_id->name, &pri_id->name);
  1902. }
  1903. if (pri_id->number.valid) {
  1904. sig_pri_party_number_convert(&ast_id->number, &pri_id->number, pri);
  1905. }
  1906. #if defined(HAVE_PRI_SUBADDR)
  1907. if (pri_id->subaddress.valid) {
  1908. sig_pri_set_subaddress(&ast_id->subaddress, &pri_id->subaddress);
  1909. }
  1910. #endif /* defined(HAVE_PRI_SUBADDR) */
  1911. }
  1912. /*!
  1913. * \internal
  1914. * \brief Convert libpri redirecting information into asterisk redirecting information.
  1915. * \since 1.8
  1916. *
  1917. * \param ast_redirecting Asterisk redirecting structure to fill.
  1918. * \param pri_redirecting libpri redirecting structure containing source information.
  1919. * \param ast_guide Asterisk redirecting structure to use as an initialization guide.
  1920. * \param pri PRI span control structure.
  1921. *
  1922. * \note The filled in ast_redirecting structure needs to be destroyed by
  1923. * ast_party_redirecting_free() when it is no longer needed.
  1924. *
  1925. * \return Nothing
  1926. */
  1927. static void sig_pri_redirecting_convert(struct ast_party_redirecting *ast_redirecting,
  1928. const struct pri_party_redirecting *pri_redirecting,
  1929. const struct ast_party_redirecting *ast_guide,
  1930. struct sig_pri_span *pri)
  1931. {
  1932. ast_party_redirecting_set_init(ast_redirecting, ast_guide);
  1933. sig_pri_party_id_convert(&ast_redirecting->from, &pri_redirecting->from, pri);
  1934. sig_pri_party_id_convert(&ast_redirecting->to, &pri_redirecting->to, pri);
  1935. ast_redirecting->count = pri_redirecting->count;
  1936. ast_redirecting->reason = pri_to_ast_reason(pri_redirecting->reason);
  1937. }
  1938. /*!
  1939. * \internal
  1940. * \brief Determine if the given extension matches one of the MSNs in the pattern list.
  1941. * \since 1.8
  1942. *
  1943. * \param msn_patterns Comma separated list of MSN patterns to match.
  1944. * \param exten Extension to match in the MSN list.
  1945. *
  1946. * \retval 1 if matches.
  1947. * \retval 0 if no match.
  1948. */
  1949. static int sig_pri_msn_match(const char *msn_patterns, const char *exten)
  1950. {
  1951. char *pattern;
  1952. char *msn_list;
  1953. char *list_tail;
  1954. msn_list = ast_strdupa(msn_patterns);
  1955. list_tail = NULL;
  1956. pattern = strtok_r(msn_list, ",", &list_tail);
  1957. while (pattern) {
  1958. pattern = ast_strip(pattern);
  1959. if (!ast_strlen_zero(pattern) && ast_extension_match(pattern, exten)) {
  1960. /* Extension matched the pattern. */
  1961. return 1;
  1962. }
  1963. pattern = strtok_r(NULL, ",", &list_tail);
  1964. }
  1965. /* Did not match any pattern in the list. */
  1966. return 0;
  1967. }
  1968. #if defined(HAVE_PRI_MCID)
  1969. /*!
  1970. * \internal
  1971. * \brief Append the given party id to the event string.
  1972. * \since 1.8
  1973. *
  1974. * \param msg Event message string being built.
  1975. * \param prefix Prefix to add to the party id lines.
  1976. * \param party Party information to encode.
  1977. *
  1978. * \return Nothing
  1979. */
  1980. static void sig_pri_event_party_id(struct ast_str **msg, const char *prefix, struct ast_party_id *party)
  1981. {
  1982. int pres;
  1983. /* Combined party presentation */
  1984. pres = ast_party_id_presentation(party);
  1985. ast_str_append(msg, 0, "%sPres: %d (%s)\r\n", prefix, pres,
  1986. ast_describe_caller_presentation(pres));
  1987. /* Party number */
  1988. ast_str_append(msg, 0, "%sNumValid: %d\r\n", prefix,
  1989. (unsigned) party->number.valid);
  1990. ast_str_append(msg, 0, "%sNum: %s\r\n", prefix,
  1991. S_COR(party->number.valid, party->number.str, ""));
  1992. ast_str_append(msg, 0, "%ston: %d\r\n", prefix, party->number.plan);
  1993. if (party->number.valid) {
  1994. ast_str_append(msg, 0, "%sNumPlan: %d\r\n", prefix, party->number.plan);
  1995. ast_str_append(msg, 0, "%sNumPres: %d (%s)\r\n", prefix,
  1996. party->number.presentation,
  1997. ast_describe_caller_presentation(party->number.presentation));
  1998. }
  1999. /* Party name */
  2000. ast_str_append(msg, 0, "%sNameValid: %d\r\n", prefix,
  2001. (unsigned) party->name.valid);
  2002. ast_str_append(msg, 0, "%sName: %s\r\n", prefix,
  2003. S_COR(party->name.valid, party->name.str, ""));
  2004. if (party->name.valid) {
  2005. ast_str_append(msg, 0, "%sNameCharSet: %s\r\n", prefix,
  2006. ast_party_name_charset_describe(party->name.char_set));
  2007. ast_str_append(msg, 0, "%sNamePres: %d (%s)\r\n", prefix,
  2008. party->name.presentation,
  2009. ast_describe_caller_presentation(party->name.presentation));
  2010. }
  2011. #if defined(HAVE_PRI_SUBADDR)
  2012. /* Party subaddress */
  2013. if (party->subaddress.valid) {
  2014. static const char subaddress[] = "Subaddr";
  2015. ast_str_append(msg, 0, "%s%s: %s\r\n", prefix, subaddress,
  2016. S_OR(party->subaddress.str, ""));
  2017. ast_str_append(msg, 0, "%s%sType: %d\r\n", prefix, subaddress,
  2018. party->subaddress.type);
  2019. ast_str_append(msg, 0, "%s%sOdd: %d\r\n", prefix, subaddress,
  2020. party->subaddress.odd_even_indicator);
  2021. }
  2022. #endif /* defined(HAVE_PRI_SUBADDR) */
  2023. }
  2024. #endif /* defined(HAVE_PRI_MCID) */
  2025. #if defined(HAVE_PRI_MCID)
  2026. /*!
  2027. * \internal
  2028. * \brief Handle the MCID event.
  2029. * \since 1.8
  2030. *
  2031. * \param pri PRI span control structure.
  2032. * \param mcid MCID event parameters.
  2033. * \param owner Asterisk channel associated with the call.
  2034. * NULL if Asterisk no longer has the ast_channel struct.
  2035. *
  2036. * \note Assumes the pri->lock is already obtained.
  2037. * \note Assumes the owner channel lock is already obtained if still present.
  2038. *
  2039. * \return Nothing
  2040. */
  2041. static void sig_pri_mcid_event(struct sig_pri_span *pri, const struct pri_subcmd_mcid_req *mcid, struct ast_channel *owner)
  2042. {
  2043. struct ast_channel *chans[1];
  2044. struct ast_str *msg;
  2045. struct ast_party_id party;
  2046. msg = ast_str_create(4096);
  2047. if (!msg) {
  2048. return;
  2049. }
  2050. if (owner) {
  2051. /*
  2052. * The owner channel is present.
  2053. * Pass the event to the peer as well.
  2054. */
  2055. ast_queue_control(owner, AST_CONTROL_MCID);
  2056. ast_str_append(&msg, 0, "Channel: %s\r\n", owner->name);
  2057. ast_str_append(&msg, 0, "UniqueID: %s\r\n", owner->uniqueid);
  2058. sig_pri_event_party_id(&msg, "CallerID", &owner->connected.id);
  2059. } else {
  2060. /*
  2061. * Since we no longer have an owner channel,
  2062. * we have to use the caller information supplied by libpri.
  2063. */
  2064. ast_party_id_init(&party);
  2065. sig_pri_party_id_convert(&party, &mcid->originator, pri);
  2066. sig_pri_event_party_id(&msg, "CallerID", &party);
  2067. ast_party_id_free(&party);
  2068. }
  2069. /* Always use libpri's called party information. */
  2070. ast_party_id_init(&party);
  2071. sig_pri_party_id_convert(&party, &mcid->answerer, pri);
  2072. sig_pri_event_party_id(&msg, "ConnectedID", &party);
  2073. ast_party_id_free(&party);
  2074. chans[0] = owner;
  2075. ast_manager_event_multichan(EVENT_FLAG_CALL, "MCID", owner ? 1 : 0, chans, "%s",
  2076. ast_str_buffer(msg));
  2077. ast_free(msg);
  2078. }
  2079. #endif /* defined(HAVE_PRI_MCID) */
  2080. #if defined(HAVE_PRI_TRANSFER)
  2081. struct xfer_rsp_data {
  2082. struct sig_pri_span *pri;
  2083. /*! Call to send transfer success/fail response over. */
  2084. q931_call *call;
  2085. /*! Invocation ID to use when sending a reply to the transfer request. */
  2086. int invoke_id;
  2087. };
  2088. #endif /* defined(HAVE_PRI_TRANSFER) */
  2089. #if defined(HAVE_PRI_TRANSFER)
  2090. /*!
  2091. * \internal
  2092. * \brief Send the transfer success/fail response message.
  2093. * \since 1.8
  2094. *
  2095. * \param data Callback user data pointer
  2096. * \param is_successful TRUE if the transfer was successful.
  2097. *
  2098. * \return Nothing
  2099. */
  2100. static void sig_pri_transfer_rsp(void *data, int is_successful)
  2101. {
  2102. struct xfer_rsp_data *rsp = data;
  2103. pri_transfer_rsp(rsp->pri->pri, rsp->call, rsp->invoke_id, is_successful);
  2104. }
  2105. #endif /* defined(HAVE_PRI_TRANSFER) */
  2106. #if defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER)
  2107. /*!
  2108. * \brief Protocol callback to indicate if transfer will happen.
  2109. * \since 1.8
  2110. *
  2111. * \param data Callback user data pointer
  2112. * \param is_successful TRUE if the transfer will happen.
  2113. *
  2114. * \return Nothing
  2115. */
  2116. typedef void (*xfer_rsp_callback)(void *data, int is_successful);
  2117. #endif /* defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER) */
  2118. #if defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER)
  2119. /*!
  2120. * \internal
  2121. * \brief Attempt to transfer the two calls to each other.
  2122. * \since 1.8
  2123. *
  2124. * \param pri PRI span control structure.
  2125. * \param call_1_pri First call involved in the transfer. (transferee; usually on hold)
  2126. * \param call_1_held TRUE if call_1_pri is on hold.
  2127. * \param call_2_pri Second call involved in the transfer. (target; usually active/ringing)
  2128. * \param call_2_held TRUE if call_2_pri is on hold.
  2129. * \param rsp_callback Protocol callback to indicate if transfer will happen. NULL if not used.
  2130. * \param data Callback user data pointer
  2131. *
  2132. * \note Assumes the pri->lock is already obtained.
  2133. *
  2134. * \retval 0 on success.
  2135. * \retval -1 on error.
  2136. */
  2137. static int sig_pri_attempt_transfer(struct sig_pri_span *pri, q931_call *call_1_pri, int call_1_held, q931_call *call_2_pri, int call_2_held, xfer_rsp_callback rsp_callback, void *data)
  2138. {
  2139. struct attempt_xfer_call {
  2140. q931_call *pri;
  2141. struct ast_channel *ast;
  2142. int held;
  2143. int chanpos;
  2144. };
  2145. int retval;
  2146. struct ast_channel *transferee;
  2147. struct attempt_xfer_call *call_1;
  2148. struct attempt_xfer_call *call_2;
  2149. struct attempt_xfer_call *swap_call;
  2150. struct attempt_xfer_call c1;
  2151. struct attempt_xfer_call c2;
  2152. c1.pri = call_1_pri;
  2153. c1.held = call_1_held;
  2154. call_1 = &c1;
  2155. c2.pri = call_2_pri;
  2156. c2.held = call_2_held;
  2157. call_2 = &c2;
  2158. call_1->chanpos = pri_find_principle_by_call(pri, call_1->pri);
  2159. call_2->chanpos = pri_find_principle_by_call(pri, call_2->pri);
  2160. if (call_1->chanpos < 0 || call_2->chanpos < 0) {
  2161. /* Calls not found in span control. */
  2162. if (rsp_callback) {
  2163. /* Transfer failed. */
  2164. rsp_callback(data, 0);
  2165. }
  2166. return -1;
  2167. }
  2168. /* Attempt to make transferee and target consistent. */
  2169. if (!call_1->held && call_2->held) {
  2170. /*
  2171. * Swap call_1 and call_2 to make call_1 the transferee(held call)
  2172. * and call_2 the target(active call).
  2173. */
  2174. swap_call = call_1;
  2175. call_1 = call_2;
  2176. call_2 = swap_call;
  2177. }
  2178. /* Deadlock avoidance is attempted. */
  2179. sig_pri_lock_private(pri->pvts[call_1->chanpos]);
  2180. sig_pri_lock_owner(pri, call_1->chanpos);
  2181. sig_pri_lock_private(pri->pvts[call_2->chanpos]);
  2182. sig_pri_lock_owner(pri, call_2->chanpos);
  2183. call_1->ast = pri->pvts[call_1->chanpos]->owner;
  2184. call_2->ast = pri->pvts[call_2->chanpos]->owner;
  2185. if (!call_1->ast || !call_2->ast) {
  2186. /* At least one owner is not present. */
  2187. if (call_1->ast) {
  2188. ast_channel_unlock(call_1->ast);
  2189. }
  2190. if (call_2->ast) {
  2191. ast_channel_unlock(call_2->ast);
  2192. }
  2193. sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
  2194. sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
  2195. if (rsp_callback) {
  2196. /* Transfer failed. */
  2197. rsp_callback(data, 0);
  2198. }
  2199. return -1;
  2200. }
  2201. for (;;) {
  2202. transferee = ast_bridged_channel(call_1->ast);
  2203. if (transferee) {
  2204. break;
  2205. }
  2206. /* Try masquerading the other way. */
  2207. swap_call = call_1;
  2208. call_1 = call_2;
  2209. call_2 = swap_call;
  2210. transferee = ast_bridged_channel(call_1->ast);
  2211. if (transferee) {
  2212. break;
  2213. }
  2214. /* Could not transfer. Neither call is bridged. */
  2215. ast_channel_unlock(call_1->ast);
  2216. ast_channel_unlock(call_2->ast);
  2217. sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
  2218. sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
  2219. if (rsp_callback) {
  2220. /* Transfer failed. */
  2221. rsp_callback(data, 0);
  2222. }
  2223. return -1;
  2224. }
  2225. ast_verb(3, "TRANSFERRING %s to %s\n", call_1->ast->name, call_2->ast->name);
  2226. /*
  2227. * Setup transfer masquerade.
  2228. *
  2229. * Note: There is an extremely nasty deadlock avoidance issue
  2230. * with ast_channel_transfer_masquerade(). Deadlock may be possible if
  2231. * the channels involved are proxies (chan_agent channels) and
  2232. * it is called with locks. Unfortunately, there is no simple
  2233. * or even merely difficult way to guarantee deadlock avoidance
  2234. * and still be able to send an ECT success response without the
  2235. * possibility of the bridged channel hanging up on us.
  2236. */
  2237. ast_mutex_unlock(&pri->lock);
  2238. retval = ast_channel_transfer_masquerade(
  2239. call_2->ast,
  2240. &call_2->ast->connected,
  2241. call_2->held,
  2242. transferee,
  2243. &call_1->ast->connected,
  2244. call_1->held);
  2245. /* Reacquire the pri->lock to hold off completion of the transfer masquerade. */
  2246. ast_mutex_lock(&pri->lock);
  2247. ast_channel_unlock(call_1->ast);
  2248. ast_channel_unlock(call_2->ast);
  2249. sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
  2250. sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
  2251. if (rsp_callback) {
  2252. /*
  2253. * Report transfer status.
  2254. *
  2255. * Must do the callback before the masquerade completes to ensure
  2256. * that the protocol message goes out before the call leg is
  2257. * disconnected.
  2258. */
  2259. rsp_callback(data, retval ? 0 : 1);
  2260. }
  2261. return retval;
  2262. }
  2263. #endif /* defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER) */
  2264. #if defined(HAVE_PRI_CCSS)
  2265. /*!
  2266. * \internal
  2267. * \brief Compare the CC agent private data by libpri cc_id.
  2268. * \since 1.8
  2269. *
  2270. * \param obj pointer to the (user-defined part) of an object.
  2271. * \param arg callback argument from ao2_callback()
  2272. * \param flags flags from ao2_callback()
  2273. *
  2274. * \return values are a combination of enum _cb_results.
  2275. */
  2276. static int sig_pri_cc_agent_cmp_cc_id(void *obj, void *arg, int flags)
  2277. {
  2278. struct ast_cc_agent *agent_1 = obj;
  2279. struct sig_pri_cc_agent_prv *agent_prv_1 = agent_1->private_data;
  2280. struct sig_pri_cc_agent_prv *agent_prv_2 = arg;
  2281. return (agent_prv_1 && agent_prv_1->pri == agent_prv_2->pri
  2282. && agent_prv_1->cc_id == agent_prv_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
  2283. }
  2284. #endif /* defined(HAVE_PRI_CCSS) */
  2285. #if defined(HAVE_PRI_CCSS)
  2286. /*!
  2287. * \internal
  2288. * \brief Find the CC agent by libpri cc_id.
  2289. * \since 1.8
  2290. *
  2291. * \param pri PRI span control structure.
  2292. * \param cc_id CC record ID to find.
  2293. *
  2294. * \note
  2295. * Since agents are refcounted, and this function returns
  2296. * a reference to the agent, it is imperative that you decrement
  2297. * the refcount of the agent once you have finished using it.
  2298. *
  2299. * \retval agent on success.
  2300. * \retval NULL not found.
  2301. */
  2302. static struct ast_cc_agent *sig_pri_find_cc_agent_by_cc_id(struct sig_pri_span *pri, long cc_id)
  2303. {
  2304. struct sig_pri_cc_agent_prv finder = {
  2305. .pri = pri,
  2306. .cc_id = cc_id,
  2307. };
  2308. return ast_cc_agent_callback(0, sig_pri_cc_agent_cmp_cc_id, &finder,
  2309. sig_pri_cc_type_name);
  2310. }
  2311. #endif /* defined(HAVE_PRI_CCSS) */
  2312. #if defined(HAVE_PRI_CCSS)
  2313. /*!
  2314. * \internal
  2315. * \brief Compare the CC monitor instance by libpri cc_id.
  2316. * \since 1.8
  2317. *
  2318. * \param obj pointer to the (user-defined part) of an object.
  2319. * \param arg callback argument from ao2_callback()
  2320. * \param flags flags from ao2_callback()
  2321. *
  2322. * \return values are a combination of enum _cb_results.
  2323. */
  2324. static int sig_pri_cc_monitor_cmp_cc_id(void *obj, void *arg, int flags)
  2325. {
  2326. struct sig_pri_cc_monitor_instance *monitor_1 = obj;
  2327. struct sig_pri_cc_monitor_instance *monitor_2 = arg;
  2328. return (monitor_1->pri == monitor_2->pri
  2329. && monitor_1->cc_id == monitor_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
  2330. }
  2331. #endif /* defined(HAVE_PRI_CCSS) */
  2332. #if defined(HAVE_PRI_CCSS)
  2333. /*!
  2334. * \internal
  2335. * \brief Find the CC monitor instance by libpri cc_id.
  2336. * \since 1.8
  2337. *
  2338. * \param pri PRI span control structure.
  2339. * \param cc_id CC record ID to find.
  2340. *
  2341. * \note
  2342. * Since monitor_instances are refcounted, and this function returns
  2343. * a reference to the instance, it is imperative that you decrement
  2344. * the refcount of the instance once you have finished using it.
  2345. *
  2346. * \retval monitor_instance on success.
  2347. * \retval NULL not found.
  2348. */
  2349. static struct sig_pri_cc_monitor_instance *sig_pri_find_cc_monitor_by_cc_id(struct sig_pri_span *pri, long cc_id)
  2350. {
  2351. struct sig_pri_cc_monitor_instance finder = {
  2352. .pri = pri,
  2353. .cc_id = cc_id,
  2354. };
  2355. return ao2_callback(sig_pri_cc_monitors, 0, sig_pri_cc_monitor_cmp_cc_id, &finder);
  2356. }
  2357. #endif /* defined(HAVE_PRI_CCSS) */
  2358. #if defined(HAVE_PRI_CCSS)
  2359. /*!
  2360. * \internal
  2361. * \brief Destroy the given monitor instance.
  2362. * \since 1.8
  2363. *
  2364. * \param data Monitor instance to destroy.
  2365. *
  2366. * \return Nothing
  2367. */
  2368. static void sig_pri_cc_monitor_instance_destroy(void *data)
  2369. {
  2370. struct sig_pri_cc_monitor_instance *monitor_instance = data;
  2371. if (monitor_instance->cc_id != -1) {
  2372. ast_mutex_lock(&monitor_instance->pri->lock);
  2373. pri_cc_cancel(monitor_instance->pri->pri, monitor_instance->cc_id);
  2374. ast_mutex_unlock(&monitor_instance->pri->lock);
  2375. }
  2376. monitor_instance->pri->calls->module_unref();
  2377. }
  2378. #endif /* defined(HAVE_PRI_CCSS) */
  2379. #if defined(HAVE_PRI_CCSS)
  2380. /*!
  2381. * \internal
  2382. * \brief Construct a new monitor instance.
  2383. * \since 1.8
  2384. *
  2385. * \param core_id CC core ID.
  2386. * \param pri PRI span control structure.
  2387. * \param cc_id CC record ID.
  2388. * \param device_name Name of device (Asterisk channel name less sequence number).
  2389. *
  2390. * \note
  2391. * Since monitor_instances are refcounted, and this function returns
  2392. * a reference to the instance, it is imperative that you decrement
  2393. * the refcount of the instance once you have finished using it.
  2394. *
  2395. * \retval monitor_instance on success.
  2396. * \retval NULL on error.
  2397. */
  2398. static struct sig_pri_cc_monitor_instance *sig_pri_cc_monitor_instance_init(int core_id, struct sig_pri_span *pri, long cc_id, const char *device_name)
  2399. {
  2400. struct sig_pri_cc_monitor_instance *monitor_instance;
  2401. if (!pri->calls->module_ref || !pri->calls->module_unref) {
  2402. return NULL;
  2403. }
  2404. monitor_instance = ao2_alloc(sizeof(*monitor_instance) + strlen(device_name),
  2405. sig_pri_cc_monitor_instance_destroy);
  2406. if (!monitor_instance) {
  2407. return NULL;
  2408. }
  2409. monitor_instance->cc_id = cc_id;
  2410. monitor_instance->pri = pri;
  2411. monitor_instance->core_id = core_id;
  2412. strcpy(monitor_instance->name, device_name);
  2413. pri->calls->module_ref();
  2414. ao2_link(sig_pri_cc_monitors, monitor_instance);
  2415. return monitor_instance;
  2416. }
  2417. #endif /* defined(HAVE_PRI_CCSS) */
  2418. #if defined(HAVE_PRI_CCSS)
  2419. /*!
  2420. * \internal
  2421. * \brief Announce to the CC core that protocol CC monitor is available for this call.
  2422. * \since 1.8
  2423. *
  2424. * \param pri PRI span control structure.
  2425. * \param chanpos Channel position in the span.
  2426. * \param cc_id CC record ID.
  2427. * \param service CCBS/CCNR indication.
  2428. *
  2429. * \note Assumes the pri->lock is already obtained.
  2430. * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
  2431. * \note Assumes the sig_pri_lock_owner(pri, chanpos) is already obtained.
  2432. *
  2433. * \retval 0 on success.
  2434. * \retval -1 on error.
  2435. */
  2436. static int sig_pri_cc_available(struct sig_pri_span *pri, int chanpos, long cc_id, enum ast_cc_service_type service)
  2437. {
  2438. struct sig_pri_chan *pvt;
  2439. struct ast_cc_config_params *cc_params;
  2440. struct sig_pri_cc_monitor_instance *monitor;
  2441. enum ast_cc_monitor_policies monitor_policy;
  2442. int core_id;
  2443. int res;
  2444. char device_name[AST_CHANNEL_NAME];
  2445. char dialstring[AST_CHANNEL_NAME];
  2446. pvt = pri->pvts[chanpos];
  2447. core_id = ast_cc_get_current_core_id(pvt->owner);
  2448. if (core_id == -1) {
  2449. return -1;
  2450. }
  2451. cc_params = ast_channel_get_cc_config_params(pvt->owner);
  2452. if (!cc_params) {
  2453. return -1;
  2454. }
  2455. res = -1;
  2456. monitor_policy = ast_get_cc_monitor_policy(cc_params);
  2457. switch (monitor_policy) {
  2458. case AST_CC_MONITOR_NEVER:
  2459. /* CCSS is not enabled. */
  2460. break;
  2461. case AST_CC_MONITOR_NATIVE:
  2462. case AST_CC_MONITOR_ALWAYS:
  2463. /*
  2464. * If it is AST_CC_MONITOR_ALWAYS and native fails we will attempt the fallback
  2465. * later in the call to sig_pri_cc_generic_check().
  2466. */
  2467. ast_channel_get_device_name(pvt->owner, device_name, sizeof(device_name));
  2468. sig_pri_make_cc_dialstring(pvt, dialstring, sizeof(dialstring));
  2469. monitor = sig_pri_cc_monitor_instance_init(core_id, pri, cc_id, device_name);
  2470. if (!monitor) {
  2471. break;
  2472. }
  2473. res = ast_queue_cc_frame(pvt->owner, sig_pri_cc_type_name, dialstring, service,
  2474. monitor);
  2475. if (res) {
  2476. monitor->cc_id = -1;
  2477. ao2_unlink(sig_pri_cc_monitors, monitor);
  2478. ao2_ref(monitor, -1);
  2479. }
  2480. break;
  2481. case AST_CC_MONITOR_GENERIC:
  2482. ast_queue_cc_frame(pvt->owner, AST_CC_GENERIC_MONITOR_TYPE,
  2483. sig_pri_get_orig_dialstring(pvt), service, NULL);
  2484. /* Say it failed to force caller to cancel native CC. */
  2485. break;
  2486. }
  2487. return res;
  2488. }
  2489. #endif /* defined(HAVE_PRI_CCSS) */
  2490. /*!
  2491. * \internal
  2492. * \brief Check if generic CC monitor is needed and request it.
  2493. * \since 1.8
  2494. *
  2495. * \param pri PRI span control structure.
  2496. * \param chanpos Channel position in the span.
  2497. * \param service CCBS/CCNR indication.
  2498. *
  2499. * \note Assumes the pri->lock is already obtained.
  2500. * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
  2501. *
  2502. * \return Nothing
  2503. */
  2504. static void sig_pri_cc_generic_check(struct sig_pri_span *pri, int chanpos, enum ast_cc_service_type service)
  2505. {
  2506. struct ast_channel *owner;
  2507. struct ast_cc_config_params *cc_params;
  2508. #if defined(HAVE_PRI_CCSS)
  2509. struct ast_cc_monitor *monitor;
  2510. char device_name[AST_CHANNEL_NAME];
  2511. #endif /* defined(HAVE_PRI_CCSS) */
  2512. enum ast_cc_monitor_policies monitor_policy;
  2513. int core_id;
  2514. if (!pri->pvts[chanpos]->outgoing) {
  2515. /* This is not an outgoing call so it cannot be CC monitor. */
  2516. return;
  2517. }
  2518. sig_pri_lock_owner(pri, chanpos);
  2519. owner = pri->pvts[chanpos]->owner;
  2520. if (!owner) {
  2521. return;
  2522. }
  2523. core_id = ast_cc_get_current_core_id(owner);
  2524. if (core_id == -1) {
  2525. /* No CC core setup */
  2526. goto done;
  2527. }
  2528. cc_params = ast_channel_get_cc_config_params(owner);
  2529. if (!cc_params) {
  2530. /* Could not get CC config parameters. */
  2531. goto done;
  2532. }
  2533. #if defined(HAVE_PRI_CCSS)
  2534. ast_channel_get_device_name(owner, device_name, sizeof(device_name));
  2535. monitor = ast_cc_get_monitor_by_recall_core_id(core_id, device_name);
  2536. if (monitor) {
  2537. /* CC monitor is already present so no need for generic CC. */
  2538. ao2_ref(monitor, -1);
  2539. goto done;
  2540. }
  2541. #endif /* defined(HAVE_PRI_CCSS) */
  2542. monitor_policy = ast_get_cc_monitor_policy(cc_params);
  2543. switch (monitor_policy) {
  2544. case AST_CC_MONITOR_NEVER:
  2545. /* CCSS is not enabled. */
  2546. break;
  2547. case AST_CC_MONITOR_NATIVE:
  2548. if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
  2549. /* Request generic CC monitor. */
  2550. ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
  2551. sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
  2552. }
  2553. break;
  2554. case AST_CC_MONITOR_ALWAYS:
  2555. if (pri->sig == SIG_BRI_PTMP && pri->nodetype != PRI_NETWORK) {
  2556. /*
  2557. * Cannot monitor PTMP TE side since this is not defined.
  2558. * We are playing the roll of a phone in this case and
  2559. * a phone cannot monitor a party over the network without
  2560. * protocol help.
  2561. */
  2562. break;
  2563. }
  2564. /*
  2565. * We are either falling back or this is a PTMP NT span.
  2566. * Request generic CC monitor.
  2567. */
  2568. ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
  2569. sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
  2570. break;
  2571. case AST_CC_MONITOR_GENERIC:
  2572. if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
  2573. /* Request generic CC monitor. */
  2574. ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
  2575. sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
  2576. }
  2577. break;
  2578. }
  2579. done:
  2580. ast_channel_unlock(owner);
  2581. }
  2582. #if defined(HAVE_PRI_CCSS)
  2583. /*!
  2584. * \internal
  2585. * \brief The CC link canceled the CC instance.
  2586. * \since 1.8
  2587. *
  2588. * \param pri PRI span control structure.
  2589. * \param cc_id CC record ID.
  2590. * \param is_agent TRUE if the cc_id is for an agent.
  2591. *
  2592. * \return Nothing
  2593. */
  2594. static void sig_pri_cc_link_canceled(struct sig_pri_span *pri, long cc_id, int is_agent)
  2595. {
  2596. if (is_agent) {
  2597. struct ast_cc_agent *agent;
  2598. agent = sig_pri_find_cc_agent_by_cc_id(pri, cc_id);
  2599. if (!agent) {
  2600. return;
  2601. }
  2602. ast_cc_failed(agent->core_id, "%s agent got canceled by link",
  2603. sig_pri_cc_type_name);
  2604. ao2_ref(agent, -1);
  2605. } else {
  2606. struct sig_pri_cc_monitor_instance *monitor;
  2607. monitor = sig_pri_find_cc_monitor_by_cc_id(pri, cc_id);
  2608. if (!monitor) {
  2609. return;
  2610. }
  2611. monitor->cc_id = -1;
  2612. ast_cc_monitor_failed(monitor->core_id, monitor->name,
  2613. "%s monitor got canceled by link", sig_pri_cc_type_name);
  2614. ao2_ref(monitor, -1);
  2615. }
  2616. }
  2617. #endif /* defined(HAVE_PRI_CCSS) */
  2618. #if defined(HAVE_PRI_AOC_EVENTS)
  2619. /*!
  2620. * \internal
  2621. * \brief Convert ast_aoc_charged_item to PRI_AOC_CHARGED_ITEM .
  2622. * \since 1.8
  2623. *
  2624. * \param value Value to convert to string.
  2625. *
  2626. * \return PRI_AOC_CHARGED_ITEM
  2627. */
  2628. static enum PRI_AOC_CHARGED_ITEM sig_pri_aoc_charged_item_to_pri(enum PRI_AOC_CHARGED_ITEM value)
  2629. {
  2630. switch (value) {
  2631. case AST_AOC_CHARGED_ITEM_NA:
  2632. return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
  2633. case AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
  2634. return PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
  2635. case AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
  2636. return PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
  2637. case AST_AOC_CHARGED_ITEM_CALL_ATTEMPT:
  2638. return PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT;
  2639. case AST_AOC_CHARGED_ITEM_CALL_SETUP:
  2640. return PRI_AOC_CHARGED_ITEM_CALL_SETUP;
  2641. case AST_AOC_CHARGED_ITEM_USER_USER_INFO:
  2642. return PRI_AOC_CHARGED_ITEM_USER_USER_INFO;
  2643. case AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
  2644. return PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
  2645. }
  2646. return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
  2647. }
  2648. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  2649. #if defined(HAVE_PRI_AOC_EVENTS)
  2650. /*!
  2651. * \internal
  2652. * \brief Convert PRI_AOC_CHARGED_ITEM to ast_aoc_charged_item.
  2653. * \since 1.8
  2654. *
  2655. * \param value Value to convert to string.
  2656. *
  2657. * \return ast_aoc_charged_item
  2658. */
  2659. static enum ast_aoc_s_charged_item sig_pri_aoc_charged_item_to_ast(enum PRI_AOC_CHARGED_ITEM value)
  2660. {
  2661. switch (value) {
  2662. case PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE:
  2663. return AST_AOC_CHARGED_ITEM_NA;
  2664. case PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
  2665. return AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
  2666. case PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
  2667. return AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
  2668. case PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT:
  2669. return AST_AOC_CHARGED_ITEM_CALL_ATTEMPT;
  2670. case PRI_AOC_CHARGED_ITEM_CALL_SETUP:
  2671. return AST_AOC_CHARGED_ITEM_CALL_SETUP;
  2672. case PRI_AOC_CHARGED_ITEM_USER_USER_INFO:
  2673. return AST_AOC_CHARGED_ITEM_USER_USER_INFO;
  2674. case PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
  2675. return AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
  2676. }
  2677. return AST_AOC_CHARGED_ITEM_NA;
  2678. }
  2679. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  2680. #if defined(HAVE_PRI_AOC_EVENTS)
  2681. /*!
  2682. * \internal
  2683. * \brief Convert AST_AOC_MULTIPLER to PRI_AOC_MULTIPLIER.
  2684. * \since 1.8
  2685. *
  2686. * \return pri enum equivalent.
  2687. */
  2688. static int sig_pri_aoc_multiplier_from_ast(enum ast_aoc_currency_multiplier mult)
  2689. {
  2690. switch (mult) {
  2691. case AST_AOC_MULT_ONETHOUSANDTH:
  2692. return PRI_AOC_MULTIPLIER_THOUSANDTH;
  2693. case AST_AOC_MULT_ONEHUNDREDTH:
  2694. return PRI_AOC_MULTIPLIER_HUNDREDTH;
  2695. case AST_AOC_MULT_ONETENTH:
  2696. return PRI_AOC_MULTIPLIER_TENTH;
  2697. case AST_AOC_MULT_ONE:
  2698. return PRI_AOC_MULTIPLIER_ONE;
  2699. case AST_AOC_MULT_TEN:
  2700. return PRI_AOC_MULTIPLIER_TEN;
  2701. case AST_AOC_MULT_HUNDRED:
  2702. return PRI_AOC_MULTIPLIER_HUNDRED;
  2703. case AST_AOC_MULT_THOUSAND:
  2704. return PRI_AOC_MULTIPLIER_THOUSAND;
  2705. default:
  2706. return PRI_AOC_MULTIPLIER_ONE;
  2707. }
  2708. }
  2709. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  2710. #if defined(HAVE_PRI_AOC_EVENTS)
  2711. /*!
  2712. * \internal
  2713. * \brief Convert PRI_AOC_MULTIPLIER to AST_AOC_MULTIPLIER
  2714. * \since 1.8
  2715. *
  2716. * \return ast enum equivalent.
  2717. */
  2718. static int sig_pri_aoc_multiplier_from_pri(const int mult)
  2719. {
  2720. switch (mult) {
  2721. case PRI_AOC_MULTIPLIER_THOUSANDTH:
  2722. return AST_AOC_MULT_ONETHOUSANDTH;
  2723. case PRI_AOC_MULTIPLIER_HUNDREDTH:
  2724. return AST_AOC_MULT_ONEHUNDREDTH;
  2725. case PRI_AOC_MULTIPLIER_TENTH:
  2726. return AST_AOC_MULT_ONETENTH;
  2727. case PRI_AOC_MULTIPLIER_ONE:
  2728. return AST_AOC_MULT_ONE;
  2729. case PRI_AOC_MULTIPLIER_TEN:
  2730. return AST_AOC_MULT_TEN;
  2731. case PRI_AOC_MULTIPLIER_HUNDRED:
  2732. return AST_AOC_MULT_HUNDRED;
  2733. case PRI_AOC_MULTIPLIER_THOUSAND:
  2734. return AST_AOC_MULT_THOUSAND;
  2735. default:
  2736. return AST_AOC_MULT_ONE;
  2737. }
  2738. }
  2739. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  2740. #if defined(HAVE_PRI_AOC_EVENTS)
  2741. /*!
  2742. * \internal
  2743. * \brief Convert ast_aoc_time_scale representation to PRI_AOC_TIME_SCALE
  2744. * \since 1.8
  2745. *
  2746. * \param value Value to convert to ast representation
  2747. *
  2748. * \return PRI_AOC_TIME_SCALE
  2749. */
  2750. static enum PRI_AOC_TIME_SCALE sig_pri_aoc_scale_to_pri(enum ast_aoc_time_scale value)
  2751. {
  2752. switch (value) {
  2753. default:
  2754. case AST_AOC_TIME_SCALE_HUNDREDTH_SECOND:
  2755. return PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND;
  2756. case AST_AOC_TIME_SCALE_TENTH_SECOND:
  2757. return PRI_AOC_TIME_SCALE_TENTH_SECOND;
  2758. case AST_AOC_TIME_SCALE_SECOND:
  2759. return PRI_AOC_TIME_SCALE_SECOND;
  2760. case AST_AOC_TIME_SCALE_TEN_SECOND:
  2761. return PRI_AOC_TIME_SCALE_TEN_SECOND;
  2762. case AST_AOC_TIME_SCALE_MINUTE:
  2763. return PRI_AOC_TIME_SCALE_MINUTE;
  2764. case AST_AOC_TIME_SCALE_HOUR:
  2765. return PRI_AOC_TIME_SCALE_HOUR;
  2766. case AST_AOC_TIME_SCALE_DAY:
  2767. return PRI_AOC_TIME_SCALE_DAY;
  2768. }
  2769. }
  2770. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  2771. #if defined(HAVE_PRI_AOC_EVENTS)
  2772. /*!
  2773. * \internal
  2774. * \brief Convert PRI_AOC_TIME_SCALE to ast aoc representation
  2775. * \since 1.8
  2776. *
  2777. * \param value Value to convert to ast representation
  2778. *
  2779. * \return ast aoc time scale
  2780. */
  2781. static enum ast_aoc_time_scale sig_pri_aoc_scale_to_ast(enum PRI_AOC_TIME_SCALE value)
  2782. {
  2783. switch (value) {
  2784. default:
  2785. case PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND:
  2786. return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
  2787. case PRI_AOC_TIME_SCALE_TENTH_SECOND:
  2788. return AST_AOC_TIME_SCALE_TENTH_SECOND;
  2789. case PRI_AOC_TIME_SCALE_SECOND:
  2790. return AST_AOC_TIME_SCALE_SECOND;
  2791. case PRI_AOC_TIME_SCALE_TEN_SECOND:
  2792. return AST_AOC_TIME_SCALE_TEN_SECOND;
  2793. case PRI_AOC_TIME_SCALE_MINUTE:
  2794. return AST_AOC_TIME_SCALE_MINUTE;
  2795. case PRI_AOC_TIME_SCALE_HOUR:
  2796. return AST_AOC_TIME_SCALE_HOUR;
  2797. case PRI_AOC_TIME_SCALE_DAY:
  2798. return AST_AOC_TIME_SCALE_DAY;
  2799. }
  2800. return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
  2801. }
  2802. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  2803. #if defined(HAVE_PRI_AOC_EVENTS)
  2804. /*!
  2805. * \internal
  2806. * \brief Handle AOC-S control frame
  2807. * \since 1.8
  2808. *
  2809. * \param aoc_s AOC-S event parameters.
  2810. * \param owner Asterisk channel associated with the call.
  2811. * \param passthrough indicating if this message should be queued on the ast channel
  2812. *
  2813. * \note Assumes the pri->lock is already obtained.
  2814. * \note Assumes the sig_pri private is locked
  2815. * \note Assumes the owner channel lock is already obtained.
  2816. *
  2817. * \return Nothing
  2818. */
  2819. static void sig_pri_aoc_s_from_pri(const struct pri_subcmd_aoc_s *aoc_s, struct ast_channel *owner, int passthrough)
  2820. {
  2821. struct ast_aoc_decoded *decoded = NULL;
  2822. struct ast_aoc_encoded *encoded = NULL;
  2823. size_t encoded_size = 0;
  2824. int idx;
  2825. if (!owner || !aoc_s) {
  2826. return;
  2827. }
  2828. if (!(decoded = ast_aoc_create(AST_AOC_S, 0, 0))) {
  2829. return;
  2830. }
  2831. for (idx = 0; idx < aoc_s->num_items; ++idx) {
  2832. enum ast_aoc_s_charged_item charged_item;
  2833. charged_item = sig_pri_aoc_charged_item_to_ast(aoc_s->item[idx].chargeable);
  2834. if (charged_item == AST_AOC_CHARGED_ITEM_NA) {
  2835. /* Delete the unknown charged item from the list. */
  2836. continue;
  2837. }
  2838. switch (aoc_s->item[idx].rate_type) {
  2839. case PRI_AOC_RATE_TYPE_DURATION:
  2840. ast_aoc_s_add_rate_duration(decoded,
  2841. charged_item,
  2842. aoc_s->item[idx].rate.duration.amount.cost,
  2843. sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.duration.amount.multiplier),
  2844. aoc_s->item[idx].rate.duration.currency,
  2845. aoc_s->item[idx].rate.duration.time.length,
  2846. sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.time.scale),
  2847. aoc_s->item[idx].rate.duration.granularity.length,
  2848. sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.granularity.scale),
  2849. aoc_s->item[idx].rate.duration.charging_type);
  2850. break;
  2851. case PRI_AOC_RATE_TYPE_FLAT:
  2852. ast_aoc_s_add_rate_flat(decoded,
  2853. charged_item,
  2854. aoc_s->item[idx].rate.flat.amount.cost,
  2855. sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.flat.amount.multiplier),
  2856. aoc_s->item[idx].rate.flat.currency);
  2857. break;
  2858. case PRI_AOC_RATE_TYPE_VOLUME:
  2859. ast_aoc_s_add_rate_volume(decoded,
  2860. charged_item,
  2861. aoc_s->item[idx].rate.volume.unit,
  2862. aoc_s->item[idx].rate.volume.amount.cost,
  2863. sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.volume.amount.multiplier),
  2864. aoc_s->item[idx].rate.volume.currency);
  2865. break;
  2866. case PRI_AOC_RATE_TYPE_SPECIAL_CODE:
  2867. ast_aoc_s_add_rate_special_charge_code(decoded,
  2868. charged_item,
  2869. aoc_s->item[idx].rate.special);
  2870. break;
  2871. case PRI_AOC_RATE_TYPE_FREE:
  2872. ast_aoc_s_add_rate_free(decoded, charged_item, 0);
  2873. break;
  2874. case PRI_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
  2875. ast_aoc_s_add_rate_free(decoded, charged_item, 1);
  2876. break;
  2877. default:
  2878. ast_aoc_s_add_rate_na(decoded, charged_item);
  2879. break;
  2880. }
  2881. }
  2882. if (passthrough && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
  2883. ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
  2884. }
  2885. ast_aoc_manager_event(decoded, owner);
  2886. ast_aoc_destroy_decoded(decoded);
  2887. ast_aoc_destroy_encoded(encoded);
  2888. }
  2889. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  2890. #if defined(HAVE_PRI_AOC_EVENTS)
  2891. /*!
  2892. * \internal
  2893. * \brief Generate AOC Request Response
  2894. * \since 1.8
  2895. *
  2896. * \param aoc_request
  2897. *
  2898. * \note Assumes the pri->lock is already obtained.
  2899. * \note Assumes the sig_pri private is locked
  2900. * \note Assumes the owner channel lock is already obtained.
  2901. *
  2902. * \return Nothing
  2903. */
  2904. static void sig_pri_aoc_request_from_pri(const struct pri_subcmd_aoc_request *aoc_request, struct sig_pri_chan *pvt, q931_call *call)
  2905. {
  2906. int request;
  2907. if (!aoc_request) {
  2908. return;
  2909. }
  2910. request = aoc_request->charging_request;
  2911. if (request & PRI_AOC_REQUEST_S) {
  2912. if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S) {
  2913. /* An AOC-S response must come from the other side, so save off this invoke_id
  2914. * and see if an AOC-S message comes in before the call is answered. */
  2915. pvt->aoc_s_request_invoke_id = aoc_request->invoke_id;
  2916. pvt->aoc_s_request_invoke_id_valid = 1;
  2917. } else {
  2918. pri_aoc_s_request_response_send(pvt->pri->pri,
  2919. call,
  2920. aoc_request->invoke_id,
  2921. NULL);
  2922. }
  2923. }
  2924. if (request & PRI_AOC_REQUEST_D) {
  2925. if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_D) {
  2926. pri_aoc_de_request_response_send(pvt->pri->pri,
  2927. call,
  2928. PRI_AOC_REQ_RSP_CHARGING_INFO_FOLLOWS,
  2929. aoc_request->invoke_id);
  2930. } else {
  2931. pri_aoc_de_request_response_send(pvt->pri->pri,
  2932. call,
  2933. PRI_AOC_REQ_RSP_ERROR_NOT_AVAILABLE,
  2934. aoc_request->invoke_id);
  2935. }
  2936. }
  2937. if (request & PRI_AOC_REQUEST_E) {
  2938. if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_E) {
  2939. pri_aoc_de_request_response_send(pvt->pri->pri,
  2940. call,
  2941. PRI_AOC_REQ_RSP_CHARGING_INFO_FOLLOWS,
  2942. aoc_request->invoke_id);
  2943. } else {
  2944. pri_aoc_de_request_response_send(pvt->pri->pri,
  2945. call,
  2946. PRI_AOC_REQ_RSP_ERROR_NOT_AVAILABLE,
  2947. aoc_request->invoke_id);
  2948. }
  2949. }
  2950. }
  2951. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  2952. #if defined(HAVE_PRI_AOC_EVENTS)
  2953. /*!
  2954. * \internal
  2955. * \brief Generate AOC-D AST_CONTROL_AOC frame
  2956. * \since 1.8
  2957. *
  2958. * \param aoc_e AOC-D event parameters.
  2959. * \param owner Asterisk channel associated with the call.
  2960. * \param passthrough indicating if this message should be queued on the ast channel
  2961. *
  2962. * \note Assumes the pri->lock is already obtained.
  2963. * \note Assumes the sig_pri private is locked
  2964. * \note Assumes the owner channel lock is already obtained.
  2965. *
  2966. * \return Nothing
  2967. */
  2968. static void sig_pri_aoc_d_from_pri(const struct pri_subcmd_aoc_d *aoc_d, struct ast_channel *owner, int passthrough)
  2969. {
  2970. struct ast_aoc_decoded *decoded = NULL;
  2971. struct ast_aoc_encoded *encoded = NULL;
  2972. size_t encoded_size = 0;
  2973. enum ast_aoc_charge_type type;
  2974. if (!owner || !aoc_d) {
  2975. return;
  2976. }
  2977. switch (aoc_d->charge) {
  2978. case PRI_AOC_DE_CHARGE_CURRENCY:
  2979. type = AST_AOC_CHARGE_CURRENCY;
  2980. break;
  2981. case PRI_AOC_DE_CHARGE_UNITS:
  2982. type = AST_AOC_CHARGE_UNIT;
  2983. break;
  2984. case PRI_AOC_DE_CHARGE_FREE:
  2985. type = AST_AOC_CHARGE_FREE;
  2986. break;
  2987. default:
  2988. type = AST_AOC_CHARGE_NA;
  2989. break;
  2990. }
  2991. if (!(decoded = ast_aoc_create(AST_AOC_D, type, 0))) {
  2992. return;
  2993. }
  2994. switch (aoc_d->billing_accumulation) {
  2995. default:
  2996. ast_debug(1, "AOC-D billing accumulation has unknown value: %d\n",
  2997. aoc_d->billing_accumulation);
  2998. /* Fall through */
  2999. case 0:/* subTotal */
  3000. ast_aoc_set_total_type(decoded, AST_AOC_SUBTOTAL);
  3001. break;
  3002. case 1:/* total */
  3003. ast_aoc_set_total_type(decoded, AST_AOC_TOTAL);
  3004. break;
  3005. }
  3006. switch (aoc_d->billing_id) {
  3007. case PRI_AOC_D_BILLING_ID_NORMAL:
  3008. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL);
  3009. break;
  3010. case PRI_AOC_D_BILLING_ID_REVERSE:
  3011. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_REVERSE_CHARGE);
  3012. break;
  3013. case PRI_AOC_D_BILLING_ID_CREDIT_CARD:
  3014. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
  3015. break;
  3016. case PRI_AOC_D_BILLING_ID_NOT_AVAILABLE:
  3017. default:
  3018. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NA);
  3019. break;
  3020. }
  3021. switch (aoc_d->charge) {
  3022. case PRI_AOC_DE_CHARGE_CURRENCY:
  3023. ast_aoc_set_currency_info(decoded,
  3024. aoc_d->recorded.money.amount.cost,
  3025. sig_pri_aoc_multiplier_from_pri(aoc_d->recorded.money.amount.multiplier),
  3026. aoc_d->recorded.money.currency);
  3027. break;
  3028. case PRI_AOC_DE_CHARGE_UNITS:
  3029. {
  3030. int i;
  3031. for (i = 0; i < aoc_d->recorded.unit.num_items; ++i) {
  3032. /* if type or number are negative, then they are not present */
  3033. ast_aoc_add_unit_entry(decoded,
  3034. (aoc_d->recorded.unit.item[i].number >= 0 ? 1 : 0),
  3035. aoc_d->recorded.unit.item[i].number,
  3036. (aoc_d->recorded.unit.item[i].type >= 0 ? 1 : 0),
  3037. aoc_d->recorded.unit.item[i].type);
  3038. }
  3039. }
  3040. break;
  3041. }
  3042. if (passthrough && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
  3043. ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
  3044. }
  3045. ast_aoc_manager_event(decoded, owner);
  3046. ast_aoc_destroy_decoded(decoded);
  3047. ast_aoc_destroy_encoded(encoded);
  3048. }
  3049. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  3050. #if defined(HAVE_PRI_AOC_EVENTS)
  3051. /*!
  3052. * \internal
  3053. * \brief Generate AOC-E AST_CONTROL_AOC frame
  3054. * \since 1.8
  3055. *
  3056. * \param aoc_e AOC-E event parameters.
  3057. * \param owner Asterisk channel associated with the call.
  3058. * \param passthrough indicating if this message should be queued on the ast channel
  3059. *
  3060. * \note Assumes the pri->lock is already obtained.
  3061. * \note Assumes the sig_pri private is locked
  3062. * \note Assumes the owner channel lock is already obtained.
  3063. * \note owner channel may be NULL. In that case, generate event only
  3064. *
  3065. * \return Nothing
  3066. */
  3067. static void sig_pri_aoc_e_from_pri(const struct pri_subcmd_aoc_e *aoc_e, struct ast_channel *owner, int passthrough)
  3068. {
  3069. struct ast_aoc_decoded *decoded = NULL;
  3070. struct ast_aoc_encoded *encoded = NULL;
  3071. size_t encoded_size = 0;
  3072. enum ast_aoc_charge_type type;
  3073. if (!aoc_e) {
  3074. return;
  3075. }
  3076. switch (aoc_e->charge) {
  3077. case PRI_AOC_DE_CHARGE_CURRENCY:
  3078. type = AST_AOC_CHARGE_CURRENCY;
  3079. break;
  3080. case PRI_AOC_DE_CHARGE_UNITS:
  3081. type = AST_AOC_CHARGE_UNIT;
  3082. break;
  3083. case PRI_AOC_DE_CHARGE_FREE:
  3084. type = AST_AOC_CHARGE_FREE;
  3085. break;
  3086. default:
  3087. type = AST_AOC_CHARGE_NA;
  3088. break;
  3089. }
  3090. if (!(decoded = ast_aoc_create(AST_AOC_E, type, 0))) {
  3091. return;
  3092. }
  3093. switch (aoc_e->associated.charging_type) {
  3094. case PRI_AOC_E_CHARGING_ASSOCIATION_NUMBER:
  3095. if (!aoc_e->associated.charge.number.valid) {
  3096. break;
  3097. }
  3098. ast_aoc_set_association_number(decoded, aoc_e->associated.charge.number.str, aoc_e->associated.charge.number.plan);
  3099. break;
  3100. case PRI_AOC_E_CHARGING_ASSOCIATION_ID:
  3101. ast_aoc_set_association_id(decoded, aoc_e->associated.charge.id);
  3102. break;
  3103. default:
  3104. break;
  3105. }
  3106. switch (aoc_e->billing_id) {
  3107. case PRI_AOC_E_BILLING_ID_NORMAL:
  3108. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL);
  3109. break;
  3110. case PRI_AOC_E_BILLING_ID_REVERSE:
  3111. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_REVERSE_CHARGE);
  3112. break;
  3113. case PRI_AOC_E_BILLING_ID_CREDIT_CARD:
  3114. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
  3115. break;
  3116. case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_UNCONDITIONAL:
  3117. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL);
  3118. break;
  3119. case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_BUSY:
  3120. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_BUSY);
  3121. break;
  3122. case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_NO_REPLY:
  3123. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_NO_REPLY);
  3124. break;
  3125. case PRI_AOC_E_BILLING_ID_CALL_DEFLECTION:
  3126. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_DEFLECTION);
  3127. break;
  3128. case PRI_AOC_E_BILLING_ID_CALL_TRANSFER:
  3129. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_TRANSFER);
  3130. break;
  3131. case PRI_AOC_E_BILLING_ID_NOT_AVAILABLE:
  3132. default:
  3133. ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NA);
  3134. break;
  3135. }
  3136. switch (aoc_e->charge) {
  3137. case PRI_AOC_DE_CHARGE_CURRENCY:
  3138. ast_aoc_set_currency_info(decoded,
  3139. aoc_e->recorded.money.amount.cost,
  3140. sig_pri_aoc_multiplier_from_pri(aoc_e->recorded.money.amount.multiplier),
  3141. aoc_e->recorded.money.currency);
  3142. break;
  3143. case PRI_AOC_DE_CHARGE_UNITS:
  3144. {
  3145. int i;
  3146. for (i = 0; i < aoc_e->recorded.unit.num_items; ++i) {
  3147. /* if type or number are negative, then they are not present */
  3148. ast_aoc_add_unit_entry(decoded,
  3149. (aoc_e->recorded.unit.item[i].number >= 0 ? 1 : 0),
  3150. aoc_e->recorded.unit.item[i].number,
  3151. (aoc_e->recorded.unit.item[i].type >= 0 ? 1 : 0),
  3152. aoc_e->recorded.unit.item[i].type);
  3153. }
  3154. }
  3155. }
  3156. if (passthrough && owner && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
  3157. ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
  3158. }
  3159. ast_aoc_manager_event(decoded, owner);
  3160. ast_aoc_destroy_decoded(decoded);
  3161. ast_aoc_destroy_encoded(encoded);
  3162. }
  3163. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  3164. #if defined(HAVE_PRI_AOC_EVENTS)
  3165. /*!
  3166. * \internal
  3167. * \brief send an AOC-S message on the current call
  3168. *
  3169. * \param pvt sig_pri private channel structure.
  3170. * \param generic decoded ast AOC message
  3171. *
  3172. * \return Nothing
  3173. *
  3174. * \note Assumes that the PRI lock is already obtained.
  3175. */
  3176. static void sig_pri_aoc_s_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
  3177. {
  3178. struct pri_subcmd_aoc_s aoc_s = { 0, };
  3179. const struct ast_aoc_s_entry *entry;
  3180. int idx;
  3181. for (idx = 0; idx < ast_aoc_s_get_count(decoded); idx++) {
  3182. if (!(entry = ast_aoc_s_get_rate_info(decoded, idx))) {
  3183. break;
  3184. }
  3185. aoc_s.item[idx].chargeable = sig_pri_aoc_charged_item_to_pri(entry->charged_item);
  3186. switch (entry->rate_type) {
  3187. case AST_AOC_RATE_TYPE_DURATION:
  3188. aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_DURATION;
  3189. aoc_s.item[idx].rate.duration.amount.cost = entry->rate.duration.amount;
  3190. aoc_s.item[idx].rate.duration.amount.multiplier =
  3191. sig_pri_aoc_multiplier_from_ast(entry->rate.duration.multiplier);
  3192. aoc_s.item[idx].rate.duration.time.length = entry->rate.duration.time;
  3193. aoc_s.item[idx].rate.duration.time.scale =
  3194. sig_pri_aoc_scale_to_pri(entry->rate.duration.time_scale);
  3195. aoc_s.item[idx].rate.duration.granularity.length = entry->rate.duration.granularity_time;
  3196. aoc_s.item[idx].rate.duration.granularity.scale =
  3197. sig_pri_aoc_scale_to_pri(entry->rate.duration.granularity_time_scale);
  3198. aoc_s.item[idx].rate.duration.charging_type = entry->rate.duration.charging_type;
  3199. if (!ast_strlen_zero(entry->rate.duration.currency_name)) {
  3200. ast_copy_string(aoc_s.item[idx].rate.duration.currency,
  3201. entry->rate.duration.currency_name,
  3202. sizeof(aoc_s.item[idx].rate.duration.currency));
  3203. }
  3204. break;
  3205. case AST_AOC_RATE_TYPE_FLAT:
  3206. aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FLAT;
  3207. aoc_s.item[idx].rate.flat.amount.cost = entry->rate.flat.amount;
  3208. aoc_s.item[idx].rate.flat.amount.multiplier =
  3209. sig_pri_aoc_multiplier_from_ast(entry->rate.flat.multiplier);
  3210. if (!ast_strlen_zero(entry->rate.flat.currency_name)) {
  3211. ast_copy_string(aoc_s.item[idx].rate.flat.currency,
  3212. entry->rate.flat.currency_name,
  3213. sizeof(aoc_s.item[idx].rate.flat.currency));
  3214. }
  3215. break;
  3216. case AST_AOC_RATE_TYPE_VOLUME:
  3217. aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_VOLUME;
  3218. aoc_s.item[idx].rate.volume.unit = entry->rate.volume.volume_unit;
  3219. aoc_s.item[idx].rate.volume.amount.cost = entry->rate.volume.amount;
  3220. aoc_s.item[idx].rate.volume.amount.multiplier =
  3221. sig_pri_aoc_multiplier_from_ast(entry->rate.volume.multiplier);
  3222. if (!ast_strlen_zero(entry->rate.volume.currency_name)) {
  3223. ast_copy_string(aoc_s.item[idx].rate.volume.currency,
  3224. entry->rate.volume.currency_name,
  3225. sizeof(aoc_s.item[idx].rate.volume.currency));
  3226. }
  3227. break;
  3228. case AST_AOC_RATE_TYPE_SPECIAL_CODE:
  3229. aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_SPECIAL_CODE;
  3230. aoc_s.item[idx].rate.special = entry->rate.special_code;
  3231. break;
  3232. case AST_AOC_RATE_TYPE_FREE:
  3233. aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FREE;
  3234. break;
  3235. case AST_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
  3236. aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FREE_FROM_BEGINNING;
  3237. break;
  3238. default:
  3239. case AST_AOC_RATE_TYPE_NA:
  3240. aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_NOT_AVAILABLE;
  3241. break;
  3242. }
  3243. }
  3244. aoc_s.num_items = idx;
  3245. /* if this rate should be sent as a response to an AOC-S request we will
  3246. * have an aoc_s_request_invoke_id associated with this pvt */
  3247. if (pvt->aoc_s_request_invoke_id_valid) {
  3248. pri_aoc_s_request_response_send(pvt->pri->pri, pvt->call, pvt->aoc_s_request_invoke_id, &aoc_s);
  3249. pvt->aoc_s_request_invoke_id_valid = 0;
  3250. } else {
  3251. pri_aoc_s_send(pvt->pri->pri, pvt->call, &aoc_s);
  3252. }
  3253. }
  3254. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  3255. #if defined(HAVE_PRI_AOC_EVENTS)
  3256. /*!
  3257. * \internal
  3258. * \brief send an AOC-D message on the current call
  3259. *
  3260. * \param pvt sig_pri private channel structure.
  3261. * \param generic decoded ast AOC message
  3262. *
  3263. * \return Nothing
  3264. *
  3265. * \note Assumes that the PRI lock is already obtained.
  3266. */
  3267. static void sig_pri_aoc_d_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
  3268. {
  3269. struct pri_subcmd_aoc_d aoc_d = { 0, };
  3270. aoc_d.billing_accumulation = (ast_aoc_get_total_type(decoded) == AST_AOC_TOTAL) ? 1 : 0;
  3271. switch (ast_aoc_get_billing_id(decoded)) {
  3272. case AST_AOC_BILLING_NORMAL:
  3273. aoc_d.billing_id = PRI_AOC_D_BILLING_ID_NORMAL;
  3274. break;
  3275. case AST_AOC_BILLING_REVERSE_CHARGE:
  3276. aoc_d.billing_id = PRI_AOC_D_BILLING_ID_REVERSE;
  3277. break;
  3278. case AST_AOC_BILLING_CREDIT_CARD:
  3279. aoc_d.billing_id = PRI_AOC_D_BILLING_ID_CREDIT_CARD;
  3280. break;
  3281. case AST_AOC_BILLING_NA:
  3282. default:
  3283. aoc_d.billing_id = PRI_AOC_D_BILLING_ID_NOT_AVAILABLE;
  3284. break;
  3285. }
  3286. switch (ast_aoc_get_charge_type(decoded)) {
  3287. case AST_AOC_CHARGE_FREE:
  3288. aoc_d.charge = PRI_AOC_DE_CHARGE_FREE;
  3289. break;
  3290. case AST_AOC_CHARGE_CURRENCY:
  3291. {
  3292. const char *currency_name = ast_aoc_get_currency_name(decoded);
  3293. aoc_d.charge = PRI_AOC_DE_CHARGE_CURRENCY;
  3294. aoc_d.recorded.money.amount.cost = ast_aoc_get_currency_amount(decoded);
  3295. aoc_d.recorded.money.amount.multiplier = sig_pri_aoc_multiplier_from_ast(ast_aoc_get_currency_multiplier(decoded));
  3296. if (!ast_strlen_zero(currency_name)) {
  3297. ast_copy_string(aoc_d.recorded.money.currency, currency_name, sizeof(aoc_d.recorded.money.currency));
  3298. }
  3299. }
  3300. break;
  3301. case AST_AOC_CHARGE_UNIT:
  3302. {
  3303. const struct ast_aoc_unit_entry *entry;
  3304. int i;
  3305. aoc_d.charge = PRI_AOC_DE_CHARGE_UNITS;
  3306. for (i = 0; i < ast_aoc_get_unit_count(decoded); i++) {
  3307. if ((entry = ast_aoc_get_unit_info(decoded, i)) && i < ARRAY_LEN(aoc_d.recorded.unit.item)) {
  3308. if (entry->valid_amount) {
  3309. aoc_d.recorded.unit.item[i].number = entry->amount;
  3310. } else {
  3311. aoc_d.recorded.unit.item[i].number = -1;
  3312. }
  3313. if (entry->valid_type) {
  3314. aoc_d.recorded.unit.item[i].type = entry->type;
  3315. } else {
  3316. aoc_d.recorded.unit.item[i].type = -1;
  3317. }
  3318. aoc_d.recorded.unit.num_items++;
  3319. } else {
  3320. break;
  3321. }
  3322. }
  3323. }
  3324. break;
  3325. case AST_AOC_CHARGE_NA:
  3326. default:
  3327. aoc_d.charge = PRI_AOC_DE_CHARGE_NOT_AVAILABLE;
  3328. break;
  3329. }
  3330. pri_aoc_d_send(pvt->pri->pri, pvt->call, &aoc_d);
  3331. }
  3332. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  3333. #if defined(HAVE_PRI_AOC_EVENTS)
  3334. /*!
  3335. * \internal
  3336. * \brief send an AOC-E message on the current call
  3337. *
  3338. * \param pvt sig_pri private channel structure.
  3339. * \param generic decoded ast AOC message
  3340. *
  3341. * \return Nothing
  3342. *
  3343. * \note Assumes that the PRI lock is already obtained.
  3344. */
  3345. static void sig_pri_aoc_e_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
  3346. {
  3347. struct pri_subcmd_aoc_e *aoc_e = &pvt->aoc_e;
  3348. const struct ast_aoc_charging_association *ca = ast_aoc_get_association_info(decoded);
  3349. memset(aoc_e, 0, sizeof(*aoc_e));
  3350. pvt->holding_aoce = 1;
  3351. switch (ca->charging_type) {
  3352. case AST_AOC_CHARGING_ASSOCIATION_NUMBER:
  3353. aoc_e->associated.charge.number.valid = 1;
  3354. ast_copy_string(aoc_e->associated.charge.number.str,
  3355. ca->charge.number.number,
  3356. sizeof(aoc_e->associated.charge.number.str));
  3357. aoc_e->associated.charge.number.plan = ca->charge.number.plan;
  3358. aoc_e->associated.charging_type = PRI_AOC_E_CHARGING_ASSOCIATION_NUMBER;
  3359. break;
  3360. case AST_AOC_CHARGING_ASSOCIATION_ID:
  3361. aoc_e->associated.charge.id = ca->charge.id;
  3362. aoc_e->associated.charging_type = PRI_AOC_E_CHARGING_ASSOCIATION_ID;
  3363. break;
  3364. case AST_AOC_CHARGING_ASSOCIATION_NA:
  3365. default:
  3366. break;
  3367. }
  3368. switch (ast_aoc_get_billing_id(decoded)) {
  3369. case AST_AOC_BILLING_NORMAL:
  3370. aoc_e->billing_id = PRI_AOC_E_BILLING_ID_NORMAL;
  3371. break;
  3372. case AST_AOC_BILLING_REVERSE_CHARGE:
  3373. aoc_e->billing_id = PRI_AOC_E_BILLING_ID_REVERSE;
  3374. break;
  3375. case AST_AOC_BILLING_CREDIT_CARD:
  3376. aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CREDIT_CARD;
  3377. break;
  3378. case AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL:
  3379. aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_UNCONDITIONAL;
  3380. break;
  3381. case AST_AOC_BILLING_CALL_FWD_BUSY:
  3382. aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_BUSY;
  3383. break;
  3384. case AST_AOC_BILLING_CALL_FWD_NO_REPLY:
  3385. aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_NO_REPLY;
  3386. break;
  3387. case AST_AOC_BILLING_CALL_DEFLECTION:
  3388. aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_DEFLECTION;
  3389. break;
  3390. case AST_AOC_BILLING_CALL_TRANSFER:
  3391. aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_TRANSFER;
  3392. break;
  3393. case AST_AOC_BILLING_NA:
  3394. default:
  3395. aoc_e->billing_id = PRI_AOC_E_BILLING_ID_NOT_AVAILABLE;
  3396. break;
  3397. }
  3398. switch (ast_aoc_get_charge_type(decoded)) {
  3399. case AST_AOC_CHARGE_FREE:
  3400. aoc_e->charge = PRI_AOC_DE_CHARGE_FREE;
  3401. break;
  3402. case AST_AOC_CHARGE_CURRENCY:
  3403. {
  3404. const char *currency_name = ast_aoc_get_currency_name(decoded);
  3405. aoc_e->charge = PRI_AOC_DE_CHARGE_CURRENCY;
  3406. aoc_e->recorded.money.amount.cost = ast_aoc_get_currency_amount(decoded);
  3407. aoc_e->recorded.money.amount.multiplier = sig_pri_aoc_multiplier_from_ast(ast_aoc_get_currency_multiplier(decoded));
  3408. if (!ast_strlen_zero(currency_name)) {
  3409. ast_copy_string(aoc_e->recorded.money.currency, currency_name, sizeof(aoc_e->recorded.money.currency));
  3410. }
  3411. }
  3412. break;
  3413. case AST_AOC_CHARGE_UNIT:
  3414. {
  3415. const struct ast_aoc_unit_entry *entry;
  3416. int i;
  3417. aoc_e->charge = PRI_AOC_DE_CHARGE_UNITS;
  3418. for (i = 0; i < ast_aoc_get_unit_count(decoded); i++) {
  3419. if ((entry = ast_aoc_get_unit_info(decoded, i)) && i < ARRAY_LEN(aoc_e->recorded.unit.item)) {
  3420. if (entry->valid_amount) {
  3421. aoc_e->recorded.unit.item[i].number = entry->amount;
  3422. } else {
  3423. aoc_e->recorded.unit.item[i].number = -1;
  3424. }
  3425. if (entry->valid_type) {
  3426. aoc_e->recorded.unit.item[i].type = entry->type;
  3427. } else {
  3428. aoc_e->recorded.unit.item[i].type = -1;
  3429. }
  3430. aoc_e->recorded.unit.num_items++;
  3431. }
  3432. }
  3433. }
  3434. break;
  3435. case AST_AOC_CHARGE_NA:
  3436. default:
  3437. aoc_e->charge = PRI_AOC_DE_CHARGE_NOT_AVAILABLE;
  3438. break;
  3439. }
  3440. }
  3441. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  3442. #if defined(HAVE_PRI_AOC_EVENTS)
  3443. /*!
  3444. * \internal
  3445. * \brief send an AOC-E termination request on ast_channel and set
  3446. * hangup delay.
  3447. *
  3448. * \param pri PRI span control structure.
  3449. * \param chanpos Channel position in the span.
  3450. * \param ms to delay hangup
  3451. *
  3452. * \note Assumes the pri->lock is already obtained.
  3453. * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
  3454. *
  3455. * \return Nothing
  3456. */
  3457. static void sig_pri_send_aoce_termination_request(struct sig_pri_span *pri, int chanpos, unsigned int ms)
  3458. {
  3459. struct sig_pri_chan *pvt;
  3460. struct ast_aoc_decoded *decoded = NULL;
  3461. struct ast_aoc_encoded *encoded = NULL;
  3462. size_t encoded_size;
  3463. struct timeval whentohangup = { 0, };
  3464. sig_pri_lock_owner(pri, chanpos);
  3465. pvt = pri->pvts[chanpos];
  3466. if (!pvt->owner) {
  3467. return;
  3468. }
  3469. if (!(decoded = ast_aoc_create(AST_AOC_REQUEST, 0, AST_AOC_REQUEST_E))) {
  3470. ast_softhangup_nolock(pvt->owner, AST_SOFTHANGUP_DEV);
  3471. goto cleanup_termination_request;
  3472. }
  3473. ast_aoc_set_termination_request(decoded);
  3474. if (!(encoded = ast_aoc_encode(decoded, &encoded_size, pvt->owner))) {
  3475. ast_softhangup_nolock(pvt->owner, AST_SOFTHANGUP_DEV);
  3476. goto cleanup_termination_request;
  3477. }
  3478. /* convert ms to timeval */
  3479. whentohangup.tv_usec = (ms % 1000) * 1000;
  3480. whentohangup.tv_sec = ms / 1000;
  3481. if (ast_queue_control_data(pvt->owner, AST_CONTROL_AOC, encoded, encoded_size)) {
  3482. ast_softhangup_nolock(pvt->owner, AST_SOFTHANGUP_DEV);
  3483. goto cleanup_termination_request;
  3484. }
  3485. pvt->waiting_for_aoce = 1;
  3486. ast_channel_setwhentohangup_tv(pvt->owner, whentohangup);
  3487. ast_debug(1, "Delaying hangup on %s for aoc-e msg\n", pvt->owner->name);
  3488. cleanup_termination_request:
  3489. ast_channel_unlock(pvt->owner);
  3490. ast_aoc_destroy_decoded(decoded);
  3491. ast_aoc_destroy_encoded(encoded);
  3492. }
  3493. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  3494. /*!
  3495. * \internal
  3496. * \brief TRUE if PRI event came in on a CIS call.
  3497. * \since 1.8
  3498. *
  3499. * \param channel PRI encoded span/channel
  3500. *
  3501. * \retval non-zero if CIS call.
  3502. */
  3503. static int sig_pri_is_cis_call(int channel)
  3504. {
  3505. return channel != -1 && (channel & PRI_CIS_CALL);
  3506. }
  3507. /*!
  3508. * \internal
  3509. * \brief Handle the CIS associated PRI subcommand events.
  3510. * \since 1.8
  3511. *
  3512. * \param pri PRI span control structure.
  3513. * \param event_id PRI event id
  3514. * \param subcmds Subcommands to process if any. (Could be NULL).
  3515. * \param call_rsp libpri opaque call structure to send any responses toward.
  3516. * Could be NULL either because it is not available or the call is for the
  3517. * dummy call reference. However, this should not be NULL in the cases that
  3518. * need to use the pointer to send a response message back.
  3519. *
  3520. * \note Assumes the pri->lock is already obtained.
  3521. *
  3522. * \return Nothing
  3523. */
  3524. static void sig_pri_handle_cis_subcmds(struct sig_pri_span *pri, int event_id,
  3525. const struct pri_subcommands *subcmds, q931_call *call_rsp)
  3526. {
  3527. int index;
  3528. #if defined(HAVE_PRI_CCSS)
  3529. struct ast_cc_agent *agent;
  3530. struct sig_pri_cc_agent_prv *agent_prv;
  3531. struct sig_pri_cc_monitor_instance *monitor;
  3532. #endif /* defined(HAVE_PRI_CCSS) */
  3533. if (!subcmds) {
  3534. return;
  3535. }
  3536. for (index = 0; index < subcmds->counter_subcmd; ++index) {
  3537. const struct pri_subcommand *subcmd = &subcmds->subcmd[index];
  3538. switch (subcmd->cmd) {
  3539. #if defined(STATUS_REQUEST_PLACE_HOLDER)
  3540. case PRI_SUBCMD_STATUS_REQ:
  3541. case PRI_SUBCMD_STATUS_REQ_RSP:
  3542. /* Ignore for now. */
  3543. break;
  3544. #endif /* defined(STATUS_REQUEST_PLACE_HOLDER) */
  3545. #if defined(HAVE_PRI_CCSS)
  3546. case PRI_SUBCMD_CC_REQ:
  3547. agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_request.cc_id);
  3548. if (!agent) {
  3549. pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
  3550. break;
  3551. }
  3552. if (!ast_cc_request_is_within_limits()) {
  3553. if (pri_cc_req_rsp(pri->pri, subcmd->u.cc_request.cc_id,
  3554. 5/* queue_full */)) {
  3555. pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
  3556. }
  3557. ast_cc_failed(agent->core_id, "%s agent system CC queue full",
  3558. sig_pri_cc_type_name);
  3559. ao2_ref(agent, -1);
  3560. break;
  3561. }
  3562. agent_prv = agent->private_data;
  3563. agent_prv->cc_request_response_pending = 1;
  3564. if (ast_cc_agent_accept_request(agent->core_id,
  3565. "%s caller accepted CC offer.", sig_pri_cc_type_name)) {
  3566. agent_prv->cc_request_response_pending = 0;
  3567. if (pri_cc_req_rsp(pri->pri, subcmd->u.cc_request.cc_id,
  3568. 2/* short_term_denial */)) {
  3569. pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
  3570. }
  3571. ast_cc_failed(agent->core_id, "%s agent CC core request accept failed",
  3572. sig_pri_cc_type_name);
  3573. }
  3574. ao2_ref(agent, -1);
  3575. break;
  3576. #endif /* defined(HAVE_PRI_CCSS) */
  3577. #if defined(HAVE_PRI_CCSS)
  3578. case PRI_SUBCMD_CC_REQ_RSP:
  3579. monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
  3580. subcmd->u.cc_request_rsp.cc_id);
  3581. if (!monitor) {
  3582. pri_cc_cancel(pri->pri, subcmd->u.cc_request_rsp.cc_id);
  3583. break;
  3584. }
  3585. switch (subcmd->u.cc_request_rsp.status) {
  3586. case 0:/* success */
  3587. ast_cc_monitor_request_acked(monitor->core_id,
  3588. "%s far end accepted CC request", sig_pri_cc_type_name);
  3589. break;
  3590. case 1:/* timeout */
  3591. ast_verb(2, "core_id:%d %s CC request timeout\n", monitor->core_id,
  3592. sig_pri_cc_type_name);
  3593. ast_cc_monitor_failed(monitor->core_id, monitor->name,
  3594. "%s CC request timeout", sig_pri_cc_type_name);
  3595. break;
  3596. case 2:/* error */
  3597. ast_verb(2, "core_id:%d %s CC request error: %s\n", monitor->core_id,
  3598. sig_pri_cc_type_name,
  3599. pri_facility_error2str(subcmd->u.cc_request_rsp.fail_code));
  3600. ast_cc_monitor_failed(monitor->core_id, monitor->name,
  3601. "%s CC request error", sig_pri_cc_type_name);
  3602. break;
  3603. case 3:/* reject */
  3604. ast_verb(2, "core_id:%d %s CC request reject: %s\n", monitor->core_id,
  3605. sig_pri_cc_type_name,
  3606. pri_facility_reject2str(subcmd->u.cc_request_rsp.fail_code));
  3607. ast_cc_monitor_failed(monitor->core_id, monitor->name,
  3608. "%s CC request reject", sig_pri_cc_type_name);
  3609. break;
  3610. default:
  3611. ast_verb(2, "core_id:%d %s CC request unknown status %d\n",
  3612. monitor->core_id, sig_pri_cc_type_name,
  3613. subcmd->u.cc_request_rsp.status);
  3614. ast_cc_monitor_failed(monitor->core_id, monitor->name,
  3615. "%s CC request unknown status", sig_pri_cc_type_name);
  3616. break;
  3617. }
  3618. ao2_ref(monitor, -1);
  3619. break;
  3620. #endif /* defined(HAVE_PRI_CCSS) */
  3621. #if defined(HAVE_PRI_CCSS)
  3622. case PRI_SUBCMD_CC_REMOTE_USER_FREE:
  3623. monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
  3624. subcmd->u.cc_remote_user_free.cc_id);
  3625. if (!monitor) {
  3626. pri_cc_cancel(pri->pri, subcmd->u.cc_remote_user_free.cc_id);
  3627. break;
  3628. }
  3629. ast_cc_monitor_callee_available(monitor->core_id,
  3630. "%s callee has become available", sig_pri_cc_type_name);
  3631. ao2_ref(monitor, -1);
  3632. break;
  3633. #endif /* defined(HAVE_PRI_CCSS) */
  3634. #if defined(HAVE_PRI_CCSS)
  3635. case PRI_SUBCMD_CC_B_FREE:
  3636. monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
  3637. subcmd->u.cc_b_free.cc_id);
  3638. if (!monitor) {
  3639. pri_cc_cancel(pri->pri, subcmd->u.cc_b_free.cc_id);
  3640. break;
  3641. }
  3642. ast_cc_monitor_party_b_free(monitor->core_id);
  3643. ao2_ref(monitor, -1);
  3644. break;
  3645. #endif /* defined(HAVE_PRI_CCSS) */
  3646. #if defined(HAVE_PRI_CCSS)
  3647. case PRI_SUBCMD_CC_STATUS_REQ:
  3648. monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
  3649. subcmd->u.cc_status_req.cc_id);
  3650. if (!monitor) {
  3651. pri_cc_cancel(pri->pri, subcmd->u.cc_status_req.cc_id);
  3652. break;
  3653. }
  3654. ast_cc_monitor_status_request(monitor->core_id);
  3655. ao2_ref(monitor, -1);
  3656. break;
  3657. #endif /* defined(HAVE_PRI_CCSS) */
  3658. #if defined(HAVE_PRI_CCSS)
  3659. case PRI_SUBCMD_CC_STATUS_REQ_RSP:
  3660. agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_status_req_rsp.cc_id);
  3661. if (!agent) {
  3662. pri_cc_cancel(pri->pri, subcmd->u.cc_status_req_rsp.cc_id);
  3663. break;
  3664. }
  3665. ast_cc_agent_status_response(agent->core_id,
  3666. subcmd->u.cc_status_req_rsp.status ? AST_DEVICE_INUSE
  3667. : AST_DEVICE_NOT_INUSE);
  3668. ao2_ref(agent, -1);
  3669. break;
  3670. #endif /* defined(HAVE_PRI_CCSS) */
  3671. #if defined(HAVE_PRI_CCSS)
  3672. case PRI_SUBCMD_CC_STATUS:
  3673. agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_status.cc_id);
  3674. if (!agent) {
  3675. pri_cc_cancel(pri->pri, subcmd->u.cc_status.cc_id);
  3676. break;
  3677. }
  3678. if (subcmd->u.cc_status.status) {
  3679. ast_cc_agent_caller_busy(agent->core_id, "%s agent caller is busy",
  3680. sig_pri_cc_type_name);
  3681. } else {
  3682. ast_cc_agent_caller_available(agent->core_id,
  3683. "%s agent caller is available", sig_pri_cc_type_name);
  3684. }
  3685. ao2_ref(agent, -1);
  3686. break;
  3687. #endif /* defined(HAVE_PRI_CCSS) */
  3688. #if defined(HAVE_PRI_CCSS)
  3689. case PRI_SUBCMD_CC_CANCEL:
  3690. sig_pri_cc_link_canceled(pri, subcmd->u.cc_cancel.cc_id,
  3691. subcmd->u.cc_cancel.is_agent);
  3692. break;
  3693. #endif /* defined(HAVE_PRI_CCSS) */
  3694. #if defined(HAVE_PRI_CCSS)
  3695. case PRI_SUBCMD_CC_STOP_ALERTING:
  3696. monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
  3697. subcmd->u.cc_stop_alerting.cc_id);
  3698. if (!monitor) {
  3699. pri_cc_cancel(pri->pri, subcmd->u.cc_stop_alerting.cc_id);
  3700. break;
  3701. }
  3702. ast_cc_monitor_stop_ringing(monitor->core_id);
  3703. ao2_ref(monitor, -1);
  3704. break;
  3705. #endif /* defined(HAVE_PRI_CCSS) */
  3706. #if defined(HAVE_PRI_AOC_EVENTS)
  3707. case PRI_SUBCMD_AOC_E:
  3708. /* Queue AST_CONTROL_AOC frame */
  3709. sig_pri_aoc_e_from_pri(&subcmd->u.aoc_e, NULL, 0);
  3710. break;
  3711. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  3712. default:
  3713. ast_debug(2, "Span %d: Unknown CIS subcommand(%d) in %s event.\n", pri->span,
  3714. subcmd->cmd, pri_event2str(event_id));
  3715. break;
  3716. }
  3717. }
  3718. }
  3719. #if defined(HAVE_PRI_AOC_EVENTS)
  3720. /*!
  3721. * \internal
  3722. * \brief detect if AOC-S subcmd is present.
  3723. * \since 1.8
  3724. *
  3725. * \param subcmds Subcommands to process if any. (Could be NULL).
  3726. *
  3727. * \note Knowing whether or not an AOC-E subcmd is present on certain
  3728. * PRI hangup events is necessary to determine what method to use to hangup
  3729. * the ast_channel. If an AOC-E subcmd just came in, then a new AOC-E was queued
  3730. * on the ast_channel. If a soft hangup is used, the AOC-E msg will never make it
  3731. * across the bridge, but if a AST_CONTROL_HANGUP frame is queued behind it
  3732. * we can ensure the AOC-E frame makes it to it's destination before the hangup
  3733. * frame is read.
  3734. *
  3735. *
  3736. * \retval 0 AOC-E is not present in subcmd list
  3737. * \retval 1 AOC-E is present in subcmd list
  3738. */
  3739. static int detect_aoc_e_subcmd(const struct pri_subcommands *subcmds)
  3740. {
  3741. int i;
  3742. if (!subcmds) {
  3743. return 0;
  3744. }
  3745. for (i = 0; i < subcmds->counter_subcmd; ++i) {
  3746. const struct pri_subcommand *subcmd = &subcmds->subcmd[i];
  3747. if (subcmd->cmd == PRI_SUBCMD_AOC_E) {
  3748. return 1;
  3749. }
  3750. }
  3751. return 0;
  3752. }
  3753. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  3754. /*!
  3755. * \internal
  3756. * \brief Handle the call associated PRI subcommand events.
  3757. * \since 1.8
  3758. *
  3759. * \param pri PRI span control structure.
  3760. * \param chanpos Channel position in the span.
  3761. * \param event_id PRI event id
  3762. * \param subcmds Subcommands to process if any. (Could be NULL).
  3763. * \param call_rsp libpri opaque call structure to send any responses toward.
  3764. * Could be NULL either because it is not available or the call is for the
  3765. * dummy call reference. However, this should not be NULL in the cases that
  3766. * need to use the pointer to send a response message back.
  3767. *
  3768. * \note Assumes the pri->lock is already obtained.
  3769. * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
  3770. *
  3771. * \return Nothing
  3772. */
  3773. static void sig_pri_handle_subcmds(struct sig_pri_span *pri, int chanpos, int event_id,
  3774. const struct pri_subcommands *subcmds, q931_call *call_rsp)
  3775. {
  3776. int index;
  3777. struct ast_channel *owner;
  3778. struct ast_party_redirecting ast_redirecting;
  3779. #if defined(HAVE_PRI_TRANSFER)
  3780. struct xfer_rsp_data xfer_rsp;
  3781. #endif /* defined(HAVE_PRI_TRANSFER) */
  3782. if (!subcmds) {
  3783. return;
  3784. }
  3785. for (index = 0; index < subcmds->counter_subcmd; ++index) {
  3786. const struct pri_subcommand *subcmd = &subcmds->subcmd[index];
  3787. switch (subcmd->cmd) {
  3788. case PRI_SUBCMD_CONNECTED_LINE:
  3789. sig_pri_lock_owner(pri, chanpos);
  3790. owner = pri->pvts[chanpos]->owner;
  3791. if (owner) {
  3792. struct ast_party_connected_line ast_connected;
  3793. int caller_id_update;
  3794. /* Extract the connected line information */
  3795. ast_party_connected_line_init(&ast_connected);
  3796. sig_pri_party_id_convert(&ast_connected.id, &subcmd->u.connected_line.id,
  3797. pri);
  3798. ast_connected.id.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
  3799. caller_id_update = 0;
  3800. if (ast_connected.id.name.str) {
  3801. /* Save name for Caller-ID update */
  3802. ast_copy_string(pri->pvts[chanpos]->cid_name,
  3803. ast_connected.id.name.str, sizeof(pri->pvts[chanpos]->cid_name));
  3804. caller_id_update = 1;
  3805. }
  3806. if (ast_connected.id.number.str) {
  3807. /* Save number for Caller-ID update */
  3808. ast_copy_string(pri->pvts[chanpos]->cid_num,
  3809. ast_connected.id.number.str, sizeof(pri->pvts[chanpos]->cid_num));
  3810. pri->pvts[chanpos]->cid_ton = ast_connected.id.number.plan;
  3811. caller_id_update = 1;
  3812. }
  3813. ast_connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  3814. pri->pvts[chanpos]->cid_subaddr[0] = '\0';
  3815. #if defined(HAVE_PRI_SUBADDR)
  3816. if (ast_connected.id.subaddress.valid) {
  3817. ast_party_subaddress_set(&owner->caller.id.subaddress,
  3818. &ast_connected.id.subaddress);
  3819. if (ast_connected.id.subaddress.str) {
  3820. ast_copy_string(pri->pvts[chanpos]->cid_subaddr,
  3821. ast_connected.id.subaddress.str,
  3822. sizeof(pri->pvts[chanpos]->cid_subaddr));
  3823. }
  3824. }
  3825. #endif /* defined(HAVE_PRI_SUBADDR) */
  3826. if (caller_id_update) {
  3827. struct ast_party_caller ast_caller;
  3828. pri->pvts[chanpos]->callingpres =
  3829. ast_party_id_presentation(&ast_connected.id);
  3830. sig_pri_set_caller_id(pri->pvts[chanpos]);
  3831. ast_party_caller_set_init(&ast_caller, &owner->caller);
  3832. ast_caller.id = ast_connected.id;
  3833. ast_caller.ani = ast_connected.id;
  3834. ast_channel_set_caller_event(owner, &ast_caller, NULL);
  3835. }
  3836. /* Update the connected line information on the other channel */
  3837. if (event_id != PRI_EVENT_RING) {
  3838. /* This connected_line update was not from a SETUP message. */
  3839. ast_channel_queue_connected_line_update(owner, &ast_connected, NULL);
  3840. }
  3841. ast_party_connected_line_free(&ast_connected);
  3842. ast_channel_unlock(owner);
  3843. }
  3844. break;
  3845. case PRI_SUBCMD_REDIRECTING:
  3846. sig_pri_lock_owner(pri, chanpos);
  3847. owner = pri->pvts[chanpos]->owner;
  3848. if (owner) {
  3849. sig_pri_redirecting_convert(&ast_redirecting, &subcmd->u.redirecting,
  3850. &owner->redirecting, pri);
  3851. ast_redirecting.from.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
  3852. ast_redirecting.to.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
  3853. /*! \todo XXX Original called data can be put in a channel data store that is inherited. */
  3854. ast_channel_set_redirecting(owner, &ast_redirecting, NULL);
  3855. if (event_id != PRI_EVENT_RING) {
  3856. /* This redirection was not from a SETUP message. */
  3857. ast_channel_queue_redirecting_update(owner, &ast_redirecting, NULL);
  3858. }
  3859. ast_party_redirecting_free(&ast_redirecting);
  3860. ast_channel_unlock(owner);
  3861. }
  3862. break;
  3863. #if defined(HAVE_PRI_CALL_REROUTING)
  3864. case PRI_SUBCMD_REROUTING:
  3865. sig_pri_lock_owner(pri, chanpos);
  3866. owner = pri->pvts[chanpos]->owner;
  3867. if (owner) {
  3868. struct pri_party_redirecting pri_deflection;
  3869. if (!call_rsp) {
  3870. ast_log(LOG_WARNING,
  3871. "Span %d: %s tried CallRerouting/CallDeflection to '%s' without call!\n",
  3872. pri->span, owner->name, subcmd->u.rerouting.deflection.to.number.str);
  3873. ast_channel_unlock(owner);
  3874. break;
  3875. }
  3876. if (ast_strlen_zero(subcmd->u.rerouting.deflection.to.number.str)) {
  3877. ast_log(LOG_WARNING,
  3878. "Span %d: %s tried CallRerouting/CallDeflection to empty number!\n",
  3879. pri->span, owner->name);
  3880. pri_rerouting_rsp(pri->pri, call_rsp, subcmd->u.rerouting.invoke_id,
  3881. PRI_REROUTING_RSP_INVALID_NUMBER);
  3882. ast_channel_unlock(owner);
  3883. break;
  3884. }
  3885. ast_verb(3, "Span %d: %s is CallRerouting/CallDeflection to '%s'.\n",
  3886. pri->span, owner->name, subcmd->u.rerouting.deflection.to.number.str);
  3887. /*
  3888. * Send back positive ACK to CallRerouting/CallDeflection.
  3889. *
  3890. * Note: This call will be hungup by the core when it processes
  3891. * the call_forward string.
  3892. */
  3893. pri_rerouting_rsp(pri->pri, call_rsp, subcmd->u.rerouting.invoke_id,
  3894. PRI_REROUTING_RSP_OK_CLEAR);
  3895. pri_deflection = subcmd->u.rerouting.deflection;
  3896. /* Adjust the deflecting to number based upon the subscription option. */
  3897. switch (subcmd->u.rerouting.subscription_option) {
  3898. case 0: /* noNotification */
  3899. case 1: /* notificationWithoutDivertedToNr */
  3900. /* Delete the number because the far end is not supposed to see it. */
  3901. pri_deflection.to.number.presentation =
  3902. PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
  3903. pri_deflection.to.number.plan =
  3904. (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
  3905. pri_deflection.to.number.str[0] = '\0';
  3906. break;
  3907. case 2: /* notificationWithDivertedToNr */
  3908. break;
  3909. case 3: /* notApplicable */
  3910. default:
  3911. break;
  3912. }
  3913. sig_pri_redirecting_convert(&ast_redirecting, &pri_deflection,
  3914. &owner->redirecting, pri);
  3915. ast_redirecting.from.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
  3916. ast_redirecting.to.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
  3917. ast_channel_set_redirecting(owner, &ast_redirecting, NULL);
  3918. ast_party_redirecting_free(&ast_redirecting);
  3919. /* Request the core to forward to the new number. */
  3920. ast_string_field_set(owner, call_forward,
  3921. subcmd->u.rerouting.deflection.to.number.str);
  3922. /* Wake up the channel. */
  3923. ast_queue_frame(owner, &ast_null_frame);
  3924. ast_channel_unlock(owner);
  3925. }
  3926. break;
  3927. #endif /* defined(HAVE_PRI_CALL_REROUTING) */
  3928. #if defined(HAVE_PRI_CCSS)
  3929. case PRI_SUBCMD_CC_AVAILABLE:
  3930. sig_pri_lock_owner(pri, chanpos);
  3931. owner = pri->pvts[chanpos]->owner;
  3932. if (owner) {
  3933. enum ast_cc_service_type service;
  3934. switch (event_id) {
  3935. case PRI_EVENT_RINGING:
  3936. service = AST_CC_CCNR;
  3937. break;
  3938. case PRI_EVENT_HANGUP_REQ:
  3939. /* We will assume that the cause was busy/congestion. */
  3940. service = AST_CC_CCBS;
  3941. break;
  3942. default:
  3943. service = AST_CC_NONE;
  3944. break;
  3945. }
  3946. if (service == AST_CC_NONE
  3947. || sig_pri_cc_available(pri, chanpos, subcmd->u.cc_available.cc_id,
  3948. service)) {
  3949. pri_cc_cancel(pri->pri, subcmd->u.cc_available.cc_id);
  3950. }
  3951. ast_channel_unlock(owner);
  3952. } else {
  3953. /* No asterisk channel. */
  3954. pri_cc_cancel(pri->pri, subcmd->u.cc_available.cc_id);
  3955. }
  3956. break;
  3957. #endif /* defined(HAVE_PRI_CCSS) */
  3958. #if defined(HAVE_PRI_CCSS)
  3959. case PRI_SUBCMD_CC_CALL:
  3960. sig_pri_lock_owner(pri, chanpos);
  3961. owner = pri->pvts[chanpos]->owner;
  3962. if (owner) {
  3963. struct ast_cc_agent *agent;
  3964. agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_call.cc_id);
  3965. if (agent) {
  3966. ast_setup_cc_recall_datastore(owner, agent->core_id);
  3967. ast_cc_agent_set_interfaces_chanvar(owner);
  3968. ast_cc_agent_recalling(agent->core_id,
  3969. "%s caller is attempting recall", sig_pri_cc_type_name);
  3970. ao2_ref(agent, -1);
  3971. }
  3972. ast_channel_unlock(owner);
  3973. }
  3974. break;
  3975. #endif /* defined(HAVE_PRI_CCSS) */
  3976. #if defined(HAVE_PRI_CCSS)
  3977. case PRI_SUBCMD_CC_CANCEL:
  3978. sig_pri_cc_link_canceled(pri, subcmd->u.cc_cancel.cc_id,
  3979. subcmd->u.cc_cancel.is_agent);
  3980. break;
  3981. #endif /* defined(HAVE_PRI_CCSS) */
  3982. #if defined(HAVE_PRI_TRANSFER)
  3983. case PRI_SUBCMD_TRANSFER_CALL:
  3984. if (!call_rsp) {
  3985. /* Should never happen. */
  3986. ast_log(LOG_ERROR,
  3987. "Call transfer subcommand without call to send response!\n");
  3988. break;
  3989. }
  3990. sig_pri_unlock_private(pri->pvts[chanpos]);
  3991. xfer_rsp.pri = pri;
  3992. xfer_rsp.call = call_rsp;
  3993. xfer_rsp.invoke_id = subcmd->u.transfer.invoke_id;
  3994. sig_pri_attempt_transfer(pri,
  3995. subcmd->u.transfer.call_1, subcmd->u.transfer.is_call_1_held,
  3996. subcmd->u.transfer.call_2, subcmd->u.transfer.is_call_2_held,
  3997. sig_pri_transfer_rsp, &xfer_rsp);
  3998. sig_pri_lock_private(pri->pvts[chanpos]);
  3999. break;
  4000. #endif /* defined(HAVE_PRI_TRANSFER) */
  4001. #if defined(HAVE_PRI_AOC_EVENTS)
  4002. case PRI_SUBCMD_AOC_S:
  4003. sig_pri_lock_owner(pri, chanpos);
  4004. owner = pri->pvts[chanpos]->owner;
  4005. if (owner) {
  4006. sig_pri_aoc_s_from_pri(&subcmd->u.aoc_s, owner,
  4007. (pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S));
  4008. ast_channel_unlock(owner);
  4009. }
  4010. break;
  4011. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  4012. #if defined(HAVE_PRI_AOC_EVENTS)
  4013. case PRI_SUBCMD_AOC_D:
  4014. sig_pri_lock_owner(pri, chanpos);
  4015. owner = pri->pvts[chanpos]->owner;
  4016. if (owner) {
  4017. /* Queue AST_CONTROL_AOC frame on channel */
  4018. sig_pri_aoc_d_from_pri(&subcmd->u.aoc_d, owner,
  4019. (pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_D));
  4020. ast_channel_unlock(owner);
  4021. }
  4022. break;
  4023. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  4024. #if defined(HAVE_PRI_AOC_EVENTS)
  4025. case PRI_SUBCMD_AOC_E:
  4026. sig_pri_lock_owner(pri, chanpos);
  4027. owner = pri->pvts[chanpos]->owner;
  4028. /* Queue AST_CONTROL_AOC frame */
  4029. sig_pri_aoc_e_from_pri(&subcmd->u.aoc_e, owner,
  4030. (pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_E));
  4031. if (owner) {
  4032. ast_channel_unlock(owner);
  4033. }
  4034. break;
  4035. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  4036. #if defined(HAVE_PRI_AOC_EVENTS)
  4037. case PRI_SUBCMD_AOC_CHARGING_REQ:
  4038. sig_pri_lock_owner(pri, chanpos);
  4039. owner = pri->pvts[chanpos]->owner;
  4040. if (owner) {
  4041. sig_pri_aoc_request_from_pri(&subcmd->u.aoc_request, pri->pvts[chanpos],
  4042. call_rsp);
  4043. ast_channel_unlock(owner);
  4044. }
  4045. break;
  4046. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  4047. #if defined(HAVE_PRI_AOC_EVENTS)
  4048. case PRI_SUBCMD_AOC_CHARGING_REQ_RSP:
  4049. /*
  4050. * An AOC request response may contain an AOC-S rate list.
  4051. * If this is the case handle this just like we
  4052. * would an incoming AOC-S msg.
  4053. */
  4054. if (subcmd->u.aoc_request_response.valid_aoc_s) {
  4055. sig_pri_lock_owner(pri, chanpos);
  4056. owner = pri->pvts[chanpos]->owner;
  4057. if (owner) {
  4058. sig_pri_aoc_s_from_pri(&subcmd->u.aoc_request_response.aoc_s, owner,
  4059. (pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S));
  4060. ast_channel_unlock(owner);
  4061. }
  4062. }
  4063. break;
  4064. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  4065. #if defined(HAVE_PRI_MCID)
  4066. case PRI_SUBCMD_MCID_REQ:
  4067. sig_pri_lock_owner(pri, chanpos);
  4068. owner = pri->pvts[chanpos]->owner;
  4069. sig_pri_mcid_event(pri, &subcmd->u.mcid_req, owner);
  4070. if (owner) {
  4071. ast_channel_unlock(owner);
  4072. }
  4073. break;
  4074. #endif /* defined(HAVE_PRI_MCID) */
  4075. #if defined(HAVE_PRI_MCID)
  4076. case PRI_SUBCMD_MCID_RSP:
  4077. /* Ignore for now. */
  4078. break;
  4079. #endif /* defined(HAVE_PRI_MCID) */
  4080. #if defined(HAVE_PRI_DISPLAY_TEXT)
  4081. case PRI_SUBCMD_DISPLAY_TEXT:
  4082. if (event_id != PRI_EVENT_RING) {
  4083. /*
  4084. * This display text was not from a SETUP message. We can do
  4085. * something with this display text string.
  4086. */
  4087. sig_pri_lock_owner(pri, chanpos);
  4088. owner = pri->pvts[chanpos]->owner;
  4089. if (owner) {
  4090. struct ast_frame f;
  4091. /* Pass the display text to the peer channel. */
  4092. memset(&f, 0, sizeof(f));
  4093. f.frametype = AST_FRAME_TEXT;
  4094. f.subclass.integer = 0;
  4095. f.offset = 0;
  4096. f.data.ptr = &subcmd->u.display.text;
  4097. f.datalen = subcmd->u.display.length + 1;
  4098. ast_queue_frame(owner, &f);
  4099. ast_channel_unlock(owner);
  4100. }
  4101. }
  4102. break;
  4103. #endif /* defined(HAVE_PRI_DISPLAY_TEXT) */
  4104. default:
  4105. ast_debug(2, "Span %d: Unknown call subcommand(%d) in %s event.\n",
  4106. pri->span, subcmd->cmd, pri_event2str(event_id));
  4107. break;
  4108. }
  4109. }
  4110. }
  4111. /*!
  4112. * \internal
  4113. * \brief Convert the MOH state to string.
  4114. * \since 10.0
  4115. *
  4116. * \param state MOH state to process.
  4117. *
  4118. * \return String version of MOH state.
  4119. */
  4120. static const char *sig_pri_moh_state_str(enum sig_pri_moh_state state)
  4121. {
  4122. const char *str;
  4123. str = "Unknown";
  4124. switch (state) {
  4125. case SIG_PRI_MOH_STATE_IDLE:
  4126. str = "SIG_PRI_MOH_STATE_IDLE";
  4127. break;
  4128. case SIG_PRI_MOH_STATE_NOTIFY:
  4129. str = "SIG_PRI_MOH_STATE_NOTIFY";
  4130. break;
  4131. case SIG_PRI_MOH_STATE_MOH:
  4132. str = "SIG_PRI_MOH_STATE_MOH";
  4133. break;
  4134. #if defined(HAVE_PRI_CALL_HOLD)
  4135. case SIG_PRI_MOH_STATE_HOLD_REQ:
  4136. str = "SIG_PRI_MOH_STATE_HOLD_REQ";
  4137. break;
  4138. case SIG_PRI_MOH_STATE_PEND_UNHOLD:
  4139. str = "SIG_PRI_MOH_STATE_PEND_UNHOLD";
  4140. break;
  4141. case SIG_PRI_MOH_STATE_HOLD:
  4142. str = "SIG_PRI_MOH_STATE_HOLD";
  4143. break;
  4144. case SIG_PRI_MOH_STATE_RETRIEVE_REQ:
  4145. str = "SIG_PRI_MOH_STATE_RETRIEVE_REQ";
  4146. break;
  4147. case SIG_PRI_MOH_STATE_PEND_HOLD:
  4148. str = "SIG_PRI_MOH_STATE_PEND_HOLD";
  4149. break;
  4150. case SIG_PRI_MOH_STATE_RETRIEVE_FAIL:
  4151. str = "SIG_PRI_MOH_STATE_RETRIEVE_FAIL";
  4152. break;
  4153. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4154. case SIG_PRI_MOH_STATE_NUM:
  4155. /* Not a real state. */
  4156. break;
  4157. }
  4158. return str;
  4159. }
  4160. /*!
  4161. * \internal
  4162. * \brief Convert the MOH event to string.
  4163. * \since 10.0
  4164. *
  4165. * \param event MOH event to process.
  4166. *
  4167. * \return String version of MOH event.
  4168. */
  4169. static const char *sig_pri_moh_event_str(enum sig_pri_moh_event event)
  4170. {
  4171. const char *str;
  4172. str = "Unknown";
  4173. switch (event) {
  4174. case SIG_PRI_MOH_EVENT_RESET:
  4175. str = "SIG_PRI_MOH_EVENT_RESET";
  4176. break;
  4177. case SIG_PRI_MOH_EVENT_HOLD:
  4178. str = "SIG_PRI_MOH_EVENT_HOLD";
  4179. break;
  4180. case SIG_PRI_MOH_EVENT_UNHOLD:
  4181. str = "SIG_PRI_MOH_EVENT_UNHOLD";
  4182. break;
  4183. #if defined(HAVE_PRI_CALL_HOLD)
  4184. case SIG_PRI_MOH_EVENT_HOLD_ACK:
  4185. str = "SIG_PRI_MOH_EVENT_HOLD_ACK";
  4186. break;
  4187. case SIG_PRI_MOH_EVENT_HOLD_REJ:
  4188. str = "SIG_PRI_MOH_EVENT_HOLD_REJ";
  4189. break;
  4190. case SIG_PRI_MOH_EVENT_RETRIEVE_ACK:
  4191. str = "SIG_PRI_MOH_EVENT_RETRIEVE_ACK";
  4192. break;
  4193. case SIG_PRI_MOH_EVENT_RETRIEVE_REJ:
  4194. str = "SIG_PRI_MOH_EVENT_RETRIEVE_REJ";
  4195. break;
  4196. case SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK:
  4197. str = "SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK";
  4198. break;
  4199. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4200. case SIG_PRI_MOH_EVENT_NUM:
  4201. /* Not a real event. */
  4202. break;
  4203. }
  4204. return str;
  4205. }
  4206. #if defined(HAVE_PRI_CALL_HOLD)
  4207. /*!
  4208. * \internal
  4209. * \brief Retrieve a call that was placed on hold by the HOLD message.
  4210. * \since 10.0
  4211. *
  4212. * \param pvt Channel private control structure.
  4213. *
  4214. * \note Assumes the pvt->pri->lock is already obtained.
  4215. * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
  4216. *
  4217. * \return Next MOH state
  4218. */
  4219. static enum sig_pri_moh_state sig_pri_moh_retrieve_call(struct sig_pri_chan *pvt)
  4220. {
  4221. int chanpos;
  4222. int channel;
  4223. if (pvt->pri->nodetype == PRI_NETWORK) {
  4224. /* Find an available channel to propose */
  4225. chanpos = pri_find_empty_chan(pvt->pri, 1);
  4226. if (chanpos < 0) {
  4227. /* No channels available. */
  4228. return SIG_PRI_MOH_STATE_RETRIEVE_FAIL;
  4229. }
  4230. channel = PVT_TO_CHANNEL(pvt->pri->pvts[chanpos]);
  4231. /*
  4232. * We cannot occupy or reserve this channel at this time because
  4233. * the retrieve may fail or we could have a RETRIEVE collision.
  4234. */
  4235. } else {
  4236. /* Let the network pick the channel. */
  4237. channel = 0;
  4238. }
  4239. if (pri_retrieve(pvt->pri->pri, pvt->call, channel)) {
  4240. return SIG_PRI_MOH_STATE_RETRIEVE_FAIL;
  4241. }
  4242. return SIG_PRI_MOH_STATE_RETRIEVE_REQ;
  4243. }
  4244. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4245. /*!
  4246. * \internal
  4247. * \brief MOH FSM state idle.
  4248. * \since 10.0
  4249. *
  4250. * \param chan Channel to post event to (Usually pvt->owner)
  4251. * \param pvt Channel private control structure.
  4252. * \param event MOH event to process.
  4253. *
  4254. * \note Assumes the pvt->pri->lock is already obtained.
  4255. * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
  4256. *
  4257. * \return Next MOH state
  4258. */
  4259. static enum sig_pri_moh_state sig_pri_moh_fsm_idle(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
  4260. {
  4261. enum sig_pri_moh_state next_state;
  4262. next_state = pvt->moh_state;
  4263. switch (event) {
  4264. case SIG_PRI_MOH_EVENT_HOLD:
  4265. if (!strcasecmp(pvt->mohinterpret, "passthrough")) {
  4266. /*
  4267. * This config setting is deprecated.
  4268. * The old way did not send MOH just in case the notification was ignored.
  4269. */
  4270. pri_notify(pvt->pri->pri, pvt->call, pvt->prioffset, PRI_NOTIFY_REMOTE_HOLD);
  4271. next_state = SIG_PRI_MOH_STATE_NOTIFY;
  4272. break;
  4273. }
  4274. switch (pvt->pri->moh_signaling) {
  4275. default:
  4276. case SIG_PRI_MOH_SIGNALING_MOH:
  4277. ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
  4278. next_state = SIG_PRI_MOH_STATE_MOH;
  4279. break;
  4280. case SIG_PRI_MOH_SIGNALING_NOTIFY:
  4281. /* Send MOH anyway in case the far end does not interpret the notification. */
  4282. ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
  4283. pri_notify(pvt->pri->pri, pvt->call, pvt->prioffset, PRI_NOTIFY_REMOTE_HOLD);
  4284. next_state = SIG_PRI_MOH_STATE_NOTIFY;
  4285. break;
  4286. #if defined(HAVE_PRI_CALL_HOLD)
  4287. case SIG_PRI_MOH_SIGNALING_HOLD:
  4288. if (pri_hold(pvt->pri->pri, pvt->call)) {
  4289. /* Fall back to MOH instead */
  4290. ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
  4291. next_state = SIG_PRI_MOH_STATE_MOH;
  4292. } else {
  4293. next_state = SIG_PRI_MOH_STATE_HOLD_REQ;
  4294. }
  4295. break;
  4296. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4297. }
  4298. break;
  4299. default:
  4300. break;
  4301. }
  4302. pvt->moh_state = next_state;
  4303. return next_state;
  4304. }
  4305. /*!
  4306. * \internal
  4307. * \brief MOH FSM state notify remote party.
  4308. * \since 10.0
  4309. *
  4310. * \param chan Channel to post event to (Usually pvt->owner)
  4311. * \param pvt Channel private control structure.
  4312. * \param event MOH event to process.
  4313. *
  4314. * \note Assumes the pvt->pri->lock is already obtained.
  4315. * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
  4316. *
  4317. * \return Next MOH state
  4318. */
  4319. static enum sig_pri_moh_state sig_pri_moh_fsm_notify(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
  4320. {
  4321. enum sig_pri_moh_state next_state;
  4322. next_state = pvt->moh_state;
  4323. switch (event) {
  4324. case SIG_PRI_MOH_EVENT_HOLD:
  4325. if (strcasecmp(pvt->mohinterpret, "passthrough")) {
  4326. /* Restart MOH in case it was stopped by other means. */
  4327. ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
  4328. }
  4329. break;
  4330. case SIG_PRI_MOH_EVENT_UNHOLD:
  4331. pri_notify(pvt->pri->pri, pvt->call, pvt->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
  4332. /* Fall through */
  4333. case SIG_PRI_MOH_EVENT_RESET:
  4334. ast_moh_stop(chan);
  4335. next_state = SIG_PRI_MOH_STATE_IDLE;
  4336. break;
  4337. default:
  4338. break;
  4339. }
  4340. pvt->moh_state = next_state;
  4341. return next_state;
  4342. }
  4343. /*!
  4344. * \internal
  4345. * \brief MOH FSM state generate moh.
  4346. * \since 10.0
  4347. *
  4348. * \param chan Channel to post event to (Usually pvt->owner)
  4349. * \param pvt Channel private control structure.
  4350. * \param event MOH event to process.
  4351. *
  4352. * \note Assumes the pvt->pri->lock is already obtained.
  4353. * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
  4354. *
  4355. * \return Next MOH state
  4356. */
  4357. static enum sig_pri_moh_state sig_pri_moh_fsm_moh(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
  4358. {
  4359. enum sig_pri_moh_state next_state;
  4360. next_state = pvt->moh_state;
  4361. switch (event) {
  4362. case SIG_PRI_MOH_EVENT_HOLD:
  4363. /* Restart MOH in case it was stopped by other means. */
  4364. ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
  4365. break;
  4366. case SIG_PRI_MOH_EVENT_RESET:
  4367. case SIG_PRI_MOH_EVENT_UNHOLD:
  4368. ast_moh_stop(chan);
  4369. next_state = SIG_PRI_MOH_STATE_IDLE;
  4370. break;
  4371. default:
  4372. break;
  4373. }
  4374. pvt->moh_state = next_state;
  4375. return next_state;
  4376. }
  4377. #if defined(HAVE_PRI_CALL_HOLD)
  4378. /*!
  4379. * \internal
  4380. * \brief MOH FSM state hold requested.
  4381. * \since 10.0
  4382. *
  4383. * \param chan Channel to post event to (Usually pvt->owner)
  4384. * \param pvt Channel private control structure.
  4385. * \param event MOH event to process.
  4386. *
  4387. * \note Assumes the pvt->pri->lock is already obtained.
  4388. * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
  4389. *
  4390. * \return Next MOH state
  4391. */
  4392. static enum sig_pri_moh_state sig_pri_moh_fsm_hold_req(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
  4393. {
  4394. enum sig_pri_moh_state next_state;
  4395. next_state = pvt->moh_state;
  4396. switch (event) {
  4397. case SIG_PRI_MOH_EVENT_RESET:
  4398. next_state = SIG_PRI_MOH_STATE_IDLE;
  4399. break;
  4400. case SIG_PRI_MOH_EVENT_UNHOLD:
  4401. next_state = SIG_PRI_MOH_STATE_PEND_UNHOLD;
  4402. break;
  4403. case SIG_PRI_MOH_EVENT_HOLD_REJ:
  4404. /* Fall back to MOH */
  4405. if (chan) {
  4406. ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
  4407. }
  4408. next_state = SIG_PRI_MOH_STATE_MOH;
  4409. break;
  4410. case SIG_PRI_MOH_EVENT_HOLD_ACK:
  4411. next_state = SIG_PRI_MOH_STATE_HOLD;
  4412. break;
  4413. default:
  4414. break;
  4415. }
  4416. pvt->moh_state = next_state;
  4417. return next_state;
  4418. }
  4419. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4420. #if defined(HAVE_PRI_CALL_HOLD)
  4421. /*!
  4422. * \internal
  4423. * \brief MOH FSM state hold requested with pending unhold.
  4424. * \since 10.0
  4425. *
  4426. * \param chan Channel to post event to (Usually pvt->owner)
  4427. * \param pvt Channel private control structure.
  4428. * \param event MOH event to process.
  4429. *
  4430. * \note Assumes the pvt->pri->lock is already obtained.
  4431. * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
  4432. *
  4433. * \return Next MOH state
  4434. */
  4435. static enum sig_pri_moh_state sig_pri_moh_fsm_pend_unhold(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
  4436. {
  4437. enum sig_pri_moh_state next_state;
  4438. next_state = pvt->moh_state;
  4439. switch (event) {
  4440. case SIG_PRI_MOH_EVENT_RESET:
  4441. next_state = SIG_PRI_MOH_STATE_IDLE;
  4442. break;
  4443. case SIG_PRI_MOH_EVENT_HOLD:
  4444. next_state = SIG_PRI_MOH_STATE_HOLD_REQ;
  4445. break;
  4446. case SIG_PRI_MOH_EVENT_HOLD_REJ:
  4447. next_state = SIG_PRI_MOH_STATE_IDLE;
  4448. break;
  4449. case SIG_PRI_MOH_EVENT_HOLD_ACK:
  4450. next_state = sig_pri_moh_retrieve_call(pvt);
  4451. break;
  4452. default:
  4453. break;
  4454. }
  4455. pvt->moh_state = next_state;
  4456. return next_state;
  4457. }
  4458. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4459. #if defined(HAVE_PRI_CALL_HOLD)
  4460. /*!
  4461. * \internal
  4462. * \brief MOH FSM state hold.
  4463. * \since 10.0
  4464. *
  4465. * \param chan Channel to post event to (Usually pvt->owner)
  4466. * \param pvt Channel private control structure.
  4467. * \param event MOH event to process.
  4468. *
  4469. * \note Assumes the pvt->pri->lock is already obtained.
  4470. * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
  4471. *
  4472. * \return Next MOH state
  4473. */
  4474. static enum sig_pri_moh_state sig_pri_moh_fsm_hold(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
  4475. {
  4476. enum sig_pri_moh_state next_state;
  4477. next_state = pvt->moh_state;
  4478. switch (event) {
  4479. case SIG_PRI_MOH_EVENT_RESET:
  4480. next_state = SIG_PRI_MOH_STATE_IDLE;
  4481. break;
  4482. case SIG_PRI_MOH_EVENT_UNHOLD:
  4483. next_state = sig_pri_moh_retrieve_call(pvt);
  4484. break;
  4485. case SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK:
  4486. /* Fall back to MOH */
  4487. if (chan) {
  4488. ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
  4489. }
  4490. next_state = SIG_PRI_MOH_STATE_MOH;
  4491. break;
  4492. default:
  4493. break;
  4494. }
  4495. pvt->moh_state = next_state;
  4496. return next_state;
  4497. }
  4498. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4499. #if defined(HAVE_PRI_CALL_HOLD)
  4500. /*!
  4501. * \internal
  4502. * \brief MOH FSM state retrieve requested.
  4503. * \since 10.0
  4504. *
  4505. * \param chan Channel to post event to (Usually pvt->owner)
  4506. * \param pvt Channel private control structure.
  4507. * \param event MOH event to process.
  4508. *
  4509. * \note Assumes the pvt->pri->lock is already obtained.
  4510. * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
  4511. *
  4512. * \return Next MOH state
  4513. */
  4514. static enum sig_pri_moh_state sig_pri_moh_fsm_retrieve_req(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
  4515. {
  4516. enum sig_pri_moh_state next_state;
  4517. next_state = pvt->moh_state;
  4518. switch (event) {
  4519. case SIG_PRI_MOH_EVENT_RESET:
  4520. next_state = SIG_PRI_MOH_STATE_IDLE;
  4521. break;
  4522. case SIG_PRI_MOH_EVENT_HOLD:
  4523. next_state = SIG_PRI_MOH_STATE_PEND_HOLD;
  4524. break;
  4525. case SIG_PRI_MOH_EVENT_RETRIEVE_ACK:
  4526. case SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK:
  4527. next_state = SIG_PRI_MOH_STATE_IDLE;
  4528. break;
  4529. case SIG_PRI_MOH_EVENT_RETRIEVE_REJ:
  4530. next_state = SIG_PRI_MOH_STATE_RETRIEVE_FAIL;
  4531. break;
  4532. default:
  4533. break;
  4534. }
  4535. pvt->moh_state = next_state;
  4536. return next_state;
  4537. }
  4538. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4539. #if defined(HAVE_PRI_CALL_HOLD)
  4540. /*!
  4541. * \internal
  4542. * \brief MOH FSM state retrieve requested with pending hold.
  4543. * \since 10.0
  4544. *
  4545. * \param chan Channel to post event to (Usually pvt->owner)
  4546. * \param pvt Channel private control structure.
  4547. * \param event MOH event to process.
  4548. *
  4549. * \note Assumes the pvt->pri->lock is already obtained.
  4550. * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
  4551. *
  4552. * \return Next MOH state
  4553. */
  4554. static enum sig_pri_moh_state sig_pri_moh_fsm_pend_hold(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
  4555. {
  4556. enum sig_pri_moh_state next_state;
  4557. next_state = pvt->moh_state;
  4558. switch (event) {
  4559. case SIG_PRI_MOH_EVENT_RESET:
  4560. next_state = SIG_PRI_MOH_STATE_IDLE;
  4561. break;
  4562. case SIG_PRI_MOH_EVENT_UNHOLD:
  4563. next_state = SIG_PRI_MOH_STATE_RETRIEVE_REQ;
  4564. break;
  4565. case SIG_PRI_MOH_EVENT_RETRIEVE_ACK:
  4566. case SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK:
  4567. /*
  4568. * Successfully came off of hold. Now we can reinterpret the
  4569. * MOH signaling option to handle the pending HOLD request.
  4570. */
  4571. switch (pvt->pri->moh_signaling) {
  4572. default:
  4573. case SIG_PRI_MOH_SIGNALING_MOH:
  4574. if (chan) {
  4575. ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
  4576. }
  4577. next_state = SIG_PRI_MOH_STATE_MOH;
  4578. break;
  4579. case SIG_PRI_MOH_SIGNALING_NOTIFY:
  4580. /* Send MOH anyway in case the far end does not interpret the notification. */
  4581. if (chan) {
  4582. ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
  4583. }
  4584. pri_notify(pvt->pri->pri, pvt->call, pvt->prioffset, PRI_NOTIFY_REMOTE_HOLD);
  4585. next_state = SIG_PRI_MOH_STATE_NOTIFY;
  4586. break;
  4587. case SIG_PRI_MOH_SIGNALING_HOLD:
  4588. if (pri_hold(pvt->pri->pri, pvt->call)) {
  4589. /* Fall back to MOH instead */
  4590. if (chan) {
  4591. ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
  4592. }
  4593. next_state = SIG_PRI_MOH_STATE_MOH;
  4594. } else {
  4595. next_state = SIG_PRI_MOH_STATE_HOLD_REQ;
  4596. }
  4597. break;
  4598. }
  4599. break;
  4600. case SIG_PRI_MOH_EVENT_RETRIEVE_REJ:
  4601. /*
  4602. * We cannot reinterpret the MOH signaling option because we
  4603. * failed to come off of hold.
  4604. */
  4605. next_state = SIG_PRI_MOH_STATE_HOLD;
  4606. break;
  4607. default:
  4608. break;
  4609. }
  4610. pvt->moh_state = next_state;
  4611. return next_state;
  4612. }
  4613. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4614. #if defined(HAVE_PRI_CALL_HOLD)
  4615. /*!
  4616. * \internal
  4617. * \brief MOH FSM state retrieve failed.
  4618. * \since 10.0
  4619. *
  4620. * \param chan Channel to post event to (Usually pvt->owner)
  4621. * \param pvt Channel private control structure.
  4622. * \param event MOH event to process.
  4623. *
  4624. * \note Assumes the pvt->pri->lock is already obtained.
  4625. * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
  4626. *
  4627. * \return Next MOH state
  4628. */
  4629. static enum sig_pri_moh_state sig_pri_moh_fsm_retrieve_fail(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
  4630. {
  4631. enum sig_pri_moh_state next_state;
  4632. next_state = pvt->moh_state;
  4633. switch (event) {
  4634. case SIG_PRI_MOH_EVENT_RESET:
  4635. next_state = SIG_PRI_MOH_STATE_IDLE;
  4636. break;
  4637. case SIG_PRI_MOH_EVENT_HOLD:
  4638. next_state = SIG_PRI_MOH_STATE_HOLD;
  4639. break;
  4640. case SIG_PRI_MOH_EVENT_UNHOLD:
  4641. next_state = sig_pri_moh_retrieve_call(pvt);
  4642. break;
  4643. case SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK:
  4644. next_state = SIG_PRI_MOH_STATE_IDLE;
  4645. break;
  4646. default:
  4647. break;
  4648. }
  4649. pvt->moh_state = next_state;
  4650. return next_state;
  4651. }
  4652. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4653. /*!
  4654. * \internal
  4655. * \brief MOH FSM state function type.
  4656. * \since 10.0
  4657. *
  4658. * \param chan Channel to post event to (Usually pvt->owner)
  4659. * \param pvt Channel private control structure.
  4660. * \param event MOH event to process.
  4661. *
  4662. * \note Assumes the pvt->pri->lock is already obtained.
  4663. * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
  4664. *
  4665. * \return Next MOH state
  4666. */
  4667. typedef enum sig_pri_moh_state (*sig_pri_moh_fsm_state)(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event);
  4668. /*! MOH FSM state table. */
  4669. static const sig_pri_moh_fsm_state sig_pri_moh_fsm[SIG_PRI_MOH_STATE_NUM] = {
  4670. /* *INDENT-OFF* */
  4671. [SIG_PRI_MOH_STATE_IDLE] = sig_pri_moh_fsm_idle,
  4672. [SIG_PRI_MOH_STATE_NOTIFY] = sig_pri_moh_fsm_notify,
  4673. [SIG_PRI_MOH_STATE_MOH] = sig_pri_moh_fsm_moh,
  4674. #if defined(HAVE_PRI_CALL_HOLD)
  4675. [SIG_PRI_MOH_STATE_HOLD_REQ] = sig_pri_moh_fsm_hold_req,
  4676. [SIG_PRI_MOH_STATE_PEND_UNHOLD] = sig_pri_moh_fsm_pend_unhold,
  4677. [SIG_PRI_MOH_STATE_HOLD] = sig_pri_moh_fsm_hold,
  4678. [SIG_PRI_MOH_STATE_RETRIEVE_REQ] = sig_pri_moh_fsm_retrieve_req,
  4679. [SIG_PRI_MOH_STATE_PEND_HOLD] = sig_pri_moh_fsm_pend_hold,
  4680. [SIG_PRI_MOH_STATE_RETRIEVE_FAIL] = sig_pri_moh_fsm_retrieve_fail,
  4681. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4682. /* *INDENT-ON* */
  4683. };
  4684. /*!
  4685. * \internal
  4686. * \brief Send an event to the MOH FSM.
  4687. * \since 10.0
  4688. *
  4689. * \param chan Channel to post event to (Usually pvt->owner)
  4690. * \param pvt Channel private control structure.
  4691. * \param event MOH event to process.
  4692. *
  4693. * \note Assumes the pvt->pri->lock is already obtained.
  4694. * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
  4695. *
  4696. * \return Nothing
  4697. */
  4698. static void sig_pri_moh_fsm_event(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
  4699. {
  4700. enum sig_pri_moh_state orig_state;
  4701. enum sig_pri_moh_state next_state;
  4702. const char *chan_name;
  4703. if (chan) {
  4704. chan_name = ast_strdupa(chan->name);
  4705. } else {
  4706. chan_name = "Unknown";
  4707. }
  4708. orig_state = pvt->moh_state;
  4709. ast_debug(2, "Channel '%s' MOH-Event: %s in state %s\n", chan_name,
  4710. sig_pri_moh_event_str(event), sig_pri_moh_state_str(orig_state));
  4711. if (orig_state < SIG_PRI_MOH_STATE_IDLE || SIG_PRI_MOH_STATE_NUM <= orig_state
  4712. || !sig_pri_moh_fsm[orig_state]) {
  4713. /* Programming error: State not implemented. */
  4714. ast_log(LOG_ERROR, "MOH state not implemented: %s(%d)\n",
  4715. sig_pri_moh_state_str(orig_state), orig_state);
  4716. return;
  4717. }
  4718. /* Execute the state. */
  4719. next_state = sig_pri_moh_fsm[orig_state](chan, pvt, event);
  4720. ast_debug(2, "Channel '%s' MOH-Next-State: %s\n", chan_name,
  4721. (orig_state == next_state) ? "$" : sig_pri_moh_state_str(next_state));
  4722. }
  4723. #if defined(HAVE_PRI_CALL_HOLD)
  4724. /*!
  4725. * \internal
  4726. * \brief Post an AMI hold event.
  4727. * \since 10.0
  4728. *
  4729. * \param chan Channel to post event to
  4730. * \param is_held TRUE if the call was placed on hold.
  4731. *
  4732. * \return Nothing
  4733. */
  4734. static void sig_pri_ami_hold_event(struct ast_channel *chan, int is_held)
  4735. {
  4736. ast_manager_event(chan, EVENT_FLAG_CALL, "Hold",
  4737. "Status: %s\r\n"
  4738. "Channel: %s\r\n"
  4739. "Uniqueid: %s\r\n",
  4740. is_held ? "On" : "Off",
  4741. chan->name,
  4742. chan->uniqueid);
  4743. }
  4744. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4745. #if defined(HAVE_PRI_CALL_HOLD)
  4746. /*!
  4747. * \internal
  4748. * \brief Handle the hold event from libpri.
  4749. * \since 1.8
  4750. *
  4751. * \param pri PRI span control structure.
  4752. * \param ev Hold event received.
  4753. *
  4754. * \note Assumes the pri->lock is already obtained.
  4755. *
  4756. * \retval 0 on success.
  4757. * \retval -1 on error.
  4758. */
  4759. static int sig_pri_handle_hold(struct sig_pri_span *pri, pri_event *ev)
  4760. {
  4761. int retval;
  4762. int chanpos_old;
  4763. int chanpos_new;
  4764. struct ast_channel *owner;
  4765. chanpos_old = pri_find_principle_by_call(pri, ev->hold.call);
  4766. if (chanpos_old < 0) {
  4767. ast_log(LOG_WARNING, "Span %d: Received HOLD for unknown call.\n", pri->span);
  4768. return -1;
  4769. }
  4770. if (pri->pvts[chanpos_old]->no_b_channel) {
  4771. /* Call is already on hold or is call waiting call. */
  4772. return -1;
  4773. }
  4774. chanpos_new = -1;
  4775. sig_pri_lock_private(pri->pvts[chanpos_old]);
  4776. sig_pri_lock_owner(pri, chanpos_old);
  4777. owner = pri->pvts[chanpos_old]->owner;
  4778. if (!owner) {
  4779. goto done_with_private;
  4780. }
  4781. if (pri->pvts[chanpos_old]->call_level != SIG_PRI_CALL_LEVEL_CONNECT) {
  4782. /*
  4783. * Make things simple. Don't allow placing a call on hold that
  4784. * is not connected.
  4785. */
  4786. goto done_with_owner;
  4787. }
  4788. chanpos_new = pri_find_empty_nobch(pri);
  4789. if (chanpos_new < 0) {
  4790. /* No hold channel available. */
  4791. goto done_with_owner;
  4792. }
  4793. sig_pri_handle_subcmds(pri, chanpos_old, ev->e, ev->hold.subcmds, ev->hold.call);
  4794. pri_queue_control(pri, chanpos_old, AST_CONTROL_HOLD);
  4795. chanpos_new = pri_fixup_principle(pri, chanpos_new, ev->hold.call);
  4796. if (chanpos_new < 0) {
  4797. /* Should never happen. */
  4798. pri_queue_control(pri, chanpos_old, AST_CONTROL_UNHOLD);
  4799. } else {
  4800. sig_pri_ami_hold_event(owner, 1);
  4801. }
  4802. done_with_owner:;
  4803. ast_channel_unlock(owner);
  4804. done_with_private:;
  4805. sig_pri_unlock_private(pri->pvts[chanpos_old]);
  4806. if (chanpos_new < 0) {
  4807. retval = -1;
  4808. } else {
  4809. sig_pri_span_devstate_changed(pri);
  4810. retval = 0;
  4811. }
  4812. return retval;
  4813. }
  4814. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4815. #if defined(HAVE_PRI_CALL_HOLD)
  4816. /*!
  4817. * \internal
  4818. * \brief Handle the hold acknowledge event from libpri.
  4819. * \since 10.0
  4820. *
  4821. * \param pri PRI span control structure.
  4822. * \param ev Hold acknowledge event received.
  4823. *
  4824. * \note Assumes the pri->lock is already obtained.
  4825. *
  4826. * \return Nothing
  4827. */
  4828. static void sig_pri_handle_hold_ack(struct sig_pri_span *pri, pri_event *ev)
  4829. {
  4830. int chanpos;
  4831. /*
  4832. * We were successfully put on hold by the remote party
  4833. * so we just need to switch to a no_b_channel channel.
  4834. */
  4835. chanpos = pri_find_empty_nobch(pri);
  4836. if (chanpos < 0) {
  4837. /* Very bad news. No hold channel available. */
  4838. ast_log(LOG_ERROR,
  4839. "Span %d: No hold channel available for held call that is on %d/%d\n",
  4840. pri->span, PRI_SPAN(ev->hold_ack.channel), PRI_CHANNEL(ev->hold_ack.channel));
  4841. sig_pri_kill_call(pri, ev->hold_ack.call, PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED);
  4842. return;
  4843. }
  4844. chanpos = pri_fixup_principle(pri, chanpos, ev->hold_ack.call);
  4845. if (chanpos < 0) {
  4846. /* Should never happen. */
  4847. sig_pri_kill_call(pri, ev->hold_ack.call, PRI_CAUSE_NORMAL_TEMPORARY_FAILURE);
  4848. return;
  4849. }
  4850. sig_pri_lock_private(pri->pvts[chanpos]);
  4851. sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->hold_ack.subcmds, ev->hold_ack.call);
  4852. sig_pri_moh_fsm_event(pri->pvts[chanpos]->owner, pri->pvts[chanpos],
  4853. SIG_PRI_MOH_EVENT_HOLD_ACK);
  4854. sig_pri_unlock_private(pri->pvts[chanpos]);
  4855. sig_pri_span_devstate_changed(pri);
  4856. }
  4857. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4858. #if defined(HAVE_PRI_CALL_HOLD)
  4859. /*!
  4860. * \internal
  4861. * \brief Handle the hold reject event from libpri.
  4862. * \since 10.0
  4863. *
  4864. * \param pri PRI span control structure.
  4865. * \param ev Hold reject event received.
  4866. *
  4867. * \note Assumes the pri->lock is already obtained.
  4868. *
  4869. * \return Nothing
  4870. */
  4871. static void sig_pri_handle_hold_rej(struct sig_pri_span *pri, pri_event *ev)
  4872. {
  4873. int chanpos;
  4874. chanpos = pri_find_principle(pri, ev->hold_rej.channel, ev->hold_rej.call);
  4875. if (chanpos < 0) {
  4876. ast_log(LOG_WARNING, "Span %d: Could not find principle for HOLD_REJECT\n",
  4877. pri->span);
  4878. sig_pri_kill_call(pri, ev->hold_rej.call, PRI_CAUSE_NORMAL_TEMPORARY_FAILURE);
  4879. return;
  4880. }
  4881. chanpos = pri_fixup_principle(pri, chanpos, ev->hold_rej.call);
  4882. if (chanpos < 0) {
  4883. /* Should never happen. */
  4884. sig_pri_kill_call(pri, ev->hold_rej.call, PRI_CAUSE_NORMAL_TEMPORARY_FAILURE);
  4885. return;
  4886. }
  4887. ast_debug(1, "Span %d: HOLD_REJECT cause: %d(%s)\n", pri->span,
  4888. ev->hold_rej.cause, pri_cause2str(ev->hold_rej.cause));
  4889. sig_pri_lock_private(pri->pvts[chanpos]);
  4890. sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->hold_rej.subcmds, ev->hold_rej.call);
  4891. sig_pri_moh_fsm_event(pri->pvts[chanpos]->owner, pri->pvts[chanpos],
  4892. SIG_PRI_MOH_EVENT_HOLD_REJ);
  4893. sig_pri_unlock_private(pri->pvts[chanpos]);
  4894. }
  4895. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4896. #if defined(HAVE_PRI_CALL_HOLD)
  4897. /*!
  4898. * \internal
  4899. * \brief Handle the retrieve event from libpri.
  4900. * \since 1.8
  4901. *
  4902. * \param pri PRI span control structure.
  4903. * \param ev Retrieve event received.
  4904. *
  4905. * \note Assumes the pri->lock is already obtained.
  4906. *
  4907. * \return Nothing
  4908. */
  4909. static void sig_pri_handle_retrieve(struct sig_pri_span *pri, pri_event *ev)
  4910. {
  4911. int chanpos;
  4912. if (!(ev->retrieve.channel & PRI_HELD_CALL)) {
  4913. /* The call is not currently held. */
  4914. pri_retrieve_rej(pri->pri, ev->retrieve.call,
  4915. PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED);
  4916. return;
  4917. }
  4918. if (pri_find_principle_by_call(pri, ev->retrieve.call) < 0) {
  4919. ast_log(LOG_WARNING, "Span %d: Received RETRIEVE for unknown call.\n", pri->span);
  4920. pri_retrieve_rej(pri->pri, ev->retrieve.call,
  4921. PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED);
  4922. return;
  4923. }
  4924. if (PRI_CHANNEL(ev->retrieve.channel) == 0xFF) {
  4925. chanpos = pri_find_empty_chan(pri, 1);
  4926. } else {
  4927. chanpos = pri_find_principle(pri,
  4928. ev->retrieve.channel & ~PRI_HELD_CALL, ev->retrieve.call);
  4929. if (ev->retrieve.flexible
  4930. && (chanpos < 0 || !sig_pri_is_chan_available(pri->pvts[chanpos]))) {
  4931. /*
  4932. * Channel selection is flexible and the requested channel
  4933. * is bad or not available. Pick another channel.
  4934. */
  4935. chanpos = pri_find_empty_chan(pri, 1);
  4936. }
  4937. }
  4938. if (chanpos < 0) {
  4939. pri_retrieve_rej(pri->pri, ev->retrieve.call,
  4940. ev->retrieve.flexible ? PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION
  4941. : PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
  4942. return;
  4943. }
  4944. chanpos = pri_fixup_principle(pri, chanpos, ev->retrieve.call);
  4945. if (chanpos < 0) {
  4946. /* Channel is already in use. */
  4947. pri_retrieve_rej(pri->pri, ev->retrieve.call,
  4948. PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
  4949. return;
  4950. }
  4951. sig_pri_lock_private(pri->pvts[chanpos]);
  4952. sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->retrieve.subcmds, ev->retrieve.call);
  4953. sig_pri_lock_owner(pri, chanpos);
  4954. pri_queue_control(pri, chanpos, AST_CONTROL_UNHOLD);
  4955. if (pri->pvts[chanpos]->owner) {
  4956. sig_pri_ami_hold_event(pri->pvts[chanpos]->owner, 0);
  4957. ast_channel_unlock(pri->pvts[chanpos]->owner);
  4958. }
  4959. pri_retrieve_ack(pri->pri, ev->retrieve.call,
  4960. PVT_TO_CHANNEL(pri->pvts[chanpos]));
  4961. sig_pri_moh_fsm_event(pri->pvts[chanpos]->owner, pri->pvts[chanpos],
  4962. SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK);
  4963. sig_pri_unlock_private(pri->pvts[chanpos]);
  4964. sig_pri_span_devstate_changed(pri);
  4965. }
  4966. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4967. #if defined(HAVE_PRI_CALL_HOLD)
  4968. /*!
  4969. * \internal
  4970. * \brief Handle the retrieve acknowledge event from libpri.
  4971. * \since 10.0
  4972. *
  4973. * \param pri PRI span control structure.
  4974. * \param ev Retrieve acknowledge event received.
  4975. *
  4976. * \note Assumes the pri->lock is already obtained.
  4977. *
  4978. * \return Nothing
  4979. */
  4980. static void sig_pri_handle_retrieve_ack(struct sig_pri_span *pri, pri_event *ev)
  4981. {
  4982. int chanpos;
  4983. chanpos = pri_find_fixup_principle(pri, ev->retrieve_ack.channel,
  4984. ev->retrieve_ack.call);
  4985. if (chanpos < 0) {
  4986. return;
  4987. }
  4988. sig_pri_lock_private(pri->pvts[chanpos]);
  4989. sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->retrieve_ack.subcmds,
  4990. ev->retrieve_ack.call);
  4991. sig_pri_moh_fsm_event(pri->pvts[chanpos]->owner, pri->pvts[chanpos],
  4992. SIG_PRI_MOH_EVENT_RETRIEVE_ACK);
  4993. sig_pri_unlock_private(pri->pvts[chanpos]);
  4994. sig_pri_span_devstate_changed(pri);
  4995. }
  4996. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  4997. #if defined(HAVE_PRI_CALL_HOLD)
  4998. /*!
  4999. * \internal
  5000. * \brief Handle the retrieve reject event from libpri.
  5001. * \since 10.0
  5002. *
  5003. * \param pri PRI span control structure.
  5004. * \param ev Retrieve reject event received.
  5005. *
  5006. * \note Assumes the pri->lock is already obtained.
  5007. *
  5008. * \return Nothing
  5009. */
  5010. static void sig_pri_handle_retrieve_rej(struct sig_pri_span *pri, pri_event *ev)
  5011. {
  5012. int chanpos;
  5013. chanpos = pri_find_principle(pri, ev->retrieve_rej.channel, ev->retrieve_rej.call);
  5014. if (chanpos < 0) {
  5015. ast_log(LOG_WARNING, "Span %d: Could not find principle for RETRIEVE_REJECT\n",
  5016. pri->span);
  5017. sig_pri_kill_call(pri, ev->retrieve_rej.call, PRI_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5018. return;
  5019. }
  5020. chanpos = pri_fixup_principle(pri, chanpos, ev->retrieve_rej.call);
  5021. if (chanpos < 0) {
  5022. /* Should never happen. */
  5023. sig_pri_kill_call(pri, ev->retrieve_rej.call, PRI_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5024. return;
  5025. }
  5026. ast_debug(1, "Span %d: RETRIEVE_REJECT cause: %d(%s)\n", pri->span,
  5027. ev->retrieve_rej.cause, pri_cause2str(ev->retrieve_rej.cause));
  5028. sig_pri_lock_private(pri->pvts[chanpos]);
  5029. sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->retrieve_rej.subcmds,
  5030. ev->retrieve_rej.call);
  5031. sig_pri_moh_fsm_event(pri->pvts[chanpos]->owner, pri->pvts[chanpos],
  5032. SIG_PRI_MOH_EVENT_RETRIEVE_REJ);
  5033. sig_pri_unlock_private(pri->pvts[chanpos]);
  5034. }
  5035. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  5036. static void *pri_dchannel(void *vpri)
  5037. {
  5038. struct sig_pri_span *pri = vpri;
  5039. pri_event *e;
  5040. struct pollfd fds[SIG_PRI_NUM_DCHANS];
  5041. int res;
  5042. int chanpos = 0;
  5043. int x;
  5044. int law;
  5045. struct ast_channel *c;
  5046. struct timeval tv, lowest, *next;
  5047. int doidling=0;
  5048. char *cc;
  5049. time_t t;
  5050. int i, which=-1;
  5051. int numdchans;
  5052. pthread_t threadid;
  5053. char ani2str[6];
  5054. char plancallingnum[AST_MAX_EXTENSION];
  5055. char plancallingani[AST_MAX_EXTENSION];
  5056. char calledtonstr[10];
  5057. struct timeval lastidle = { 0, 0 };
  5058. pthread_t p;
  5059. struct ast_channel *idle;
  5060. char idlen[80];
  5061. int nextidle = -1;
  5062. int haveidles;
  5063. int activeidles;
  5064. unsigned int len;
  5065. gettimeofday(&lastidle, NULL);
  5066. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  5067. if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
  5068. /* Need to do idle dialing, check to be sure though */
  5069. cc = strchr(pri->idleext, '@');
  5070. if (cc) {
  5071. *cc = '\0';
  5072. cc++;
  5073. ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
  5074. #if 0
  5075. /* Extensions may not be loaded yet */
  5076. if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
  5077. ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
  5078. else
  5079. #endif
  5080. doidling = 1;
  5081. } else
  5082. ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
  5083. }
  5084. for (;;) {
  5085. for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
  5086. if (!pri->dchans[i])
  5087. break;
  5088. fds[i].fd = pri->fds[i];
  5089. fds[i].events = POLLIN | POLLPRI;
  5090. fds[i].revents = 0;
  5091. }
  5092. numdchans = i;
  5093. time(&t);
  5094. ast_mutex_lock(&pri->lock);
  5095. if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->sig != SIG_BRI_PTMP) && (pri->resetinterval > 0)) {
  5096. if (pri->resetting && pri_is_up(pri)) {
  5097. if (pri->resetpos < 0) {
  5098. pri_check_restart(pri);
  5099. if (pri->resetting) {
  5100. sig_pri_span_devstate_changed(pri);
  5101. }
  5102. }
  5103. } else {
  5104. if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) {
  5105. pri->resetting = 1;
  5106. pri->resetpos = -1;
  5107. }
  5108. }
  5109. }
  5110. /* Look for any idle channels if appropriate */
  5111. if (doidling && pri_is_up(pri)) {
  5112. nextidle = -1;
  5113. haveidles = 0;
  5114. activeidles = 0;
  5115. for (x = pri->numchans; x >= 0; x--) {
  5116. if (pri->pvts[x] && !pri->pvts[x]->no_b_channel) {
  5117. if (sig_pri_is_chan_available(pri->pvts[x])) {
  5118. if (haveidles < pri->minunused) {
  5119. haveidles++;
  5120. } else {
  5121. nextidle = x;
  5122. break;
  5123. }
  5124. } else if (pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
  5125. activeidles++;
  5126. }
  5127. }
  5128. }
  5129. if (nextidle > -1) {
  5130. if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
  5131. /* Don't create a new idle call more than once per second */
  5132. snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
  5133. pri->pvts[nextidle]->allocated = 1;
  5134. /*
  5135. * Release the PRI lock while we create the channel so other
  5136. * threads can send D channel messages.
  5137. */
  5138. ast_mutex_unlock(&pri->lock);
  5139. /*
  5140. * We already have the B channel reserved for this call. We
  5141. * just need to make sure that sig_pri_hangup() has completed
  5142. * cleaning up before continuing.
  5143. */
  5144. sig_pri_lock_private(pri->pvts[nextidle]);
  5145. sig_pri_unlock_private(pri->pvts[nextidle]);
  5146. idle = sig_pri_request(pri->pvts[nextidle], AST_FORMAT_ULAW, NULL, 0);
  5147. ast_mutex_lock(&pri->lock);
  5148. if (idle) {
  5149. pri->pvts[nextidle]->isidlecall = 1;
  5150. if (ast_pthread_create_background(&p, NULL, do_idle_thread, pri->pvts[nextidle])) {
  5151. ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
  5152. ast_mutex_unlock(&pri->lock);
  5153. ast_hangup(idle);
  5154. ast_mutex_lock(&pri->lock);
  5155. }
  5156. } else {
  5157. pri->pvts[nextidle]->allocated = 0;
  5158. ast_log(LOG_WARNING, "Unable to request channel 'DAHDI/%s' for idle call\n", idlen);
  5159. }
  5160. gettimeofday(&lastidle, NULL);
  5161. }
  5162. } else if ((haveidles < pri->minunused) &&
  5163. (activeidles > pri->minidle)) {
  5164. /* Mark something for hangup if there is something
  5165. that can be hungup */
  5166. for (x = pri->numchans; x >= 0; x--) {
  5167. /* find a candidate channel */
  5168. if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
  5169. pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  5170. haveidles++;
  5171. /* Stop if we have enough idle channels or
  5172. can't spare any more active idle ones */
  5173. if ((haveidles >= pri->minunused) ||
  5174. (activeidles <= pri->minidle))
  5175. break;
  5176. }
  5177. }
  5178. }
  5179. }
  5180. /* Start with reasonable max */
  5181. if (doidling || pri->resetting) {
  5182. /*
  5183. * Make sure we stop at least once per second if we're
  5184. * monitoring idle channels
  5185. */
  5186. lowest = ast_tv(1, 0);
  5187. } else {
  5188. /* Don't poll for more than 60 seconds */
  5189. lowest = ast_tv(60, 0);
  5190. }
  5191. for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
  5192. if (!pri->dchans[i]) {
  5193. /* We scanned all D channels on this span. */
  5194. break;
  5195. }
  5196. next = pri_schedule_next(pri->dchans[i]);
  5197. if (next) {
  5198. /* We need relative time here */
  5199. tv = ast_tvsub(*next, ast_tvnow());
  5200. if (tv.tv_sec < 0) {
  5201. /*
  5202. * A timer has already expired.
  5203. * By definition zero time is the lowest so we can quit early.
  5204. */
  5205. lowest = ast_tv(0, 0);
  5206. break;
  5207. }
  5208. if (ast_tvcmp(tv, lowest) < 0) {
  5209. lowest = tv;
  5210. }
  5211. }
  5212. }
  5213. ast_mutex_unlock(&pri->lock);
  5214. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  5215. pthread_testcancel();
  5216. e = NULL;
  5217. res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
  5218. pthread_testcancel();
  5219. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  5220. ast_mutex_lock(&pri->lock);
  5221. if (!res) {
  5222. for (which = 0; which < SIG_PRI_NUM_DCHANS; which++) {
  5223. if (!pri->dchans[which])
  5224. break;
  5225. /* Just a timeout, run the scheduler */
  5226. e = pri_schedule_run(pri->dchans[which]);
  5227. if (e)
  5228. break;
  5229. }
  5230. } else if (res > -1) {
  5231. for (which = 0; which < SIG_PRI_NUM_DCHANS; which++) {
  5232. if (!pri->dchans[which])
  5233. break;
  5234. if (fds[which].revents & POLLPRI) {
  5235. sig_pri_handle_dchan_exception(pri, which);
  5236. } else if (fds[which].revents & POLLIN) {
  5237. e = pri_check_event(pri->dchans[which]);
  5238. }
  5239. if (e)
  5240. break;
  5241. }
  5242. } else if (errno != EINTR)
  5243. ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
  5244. if (e) {
  5245. if (pri->debug) {
  5246. ast_verbose("Span %d: Processing event %s(%d)\n",
  5247. pri->span, pri_event2str(e->e), e->e);
  5248. }
  5249. if (e->e != PRI_EVENT_DCHAN_DOWN) {
  5250. if (!(pri->dchanavail[which] & DCHAN_UP)) {
  5251. ast_verb(2, "%s D-Channel on span %d up\n", pri_order(which), pri->span);
  5252. }
  5253. pri->dchanavail[which] |= DCHAN_UP;
  5254. } else {
  5255. if (pri->dchanavail[which] & DCHAN_UP) {
  5256. ast_verb(2, "%s D-Channel on span %d down\n", pri_order(which), pri->span);
  5257. }
  5258. pri->dchanavail[which] &= ~DCHAN_UP;
  5259. }
  5260. if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which]))
  5261. /* Must be an NFAS group that has the secondary dchan active */
  5262. pri->pri = pri->dchans[which];
  5263. switch (e->e) {
  5264. case PRI_EVENT_DCHAN_UP:
  5265. pri->no_d_channels = 0;
  5266. if (!pri->pri) {
  5267. pri_find_dchan(pri);
  5268. }
  5269. /* Note presense of D-channel */
  5270. time(&pri->lastreset);
  5271. /* Restart in 5 seconds */
  5272. if (pri->resetinterval > -1) {
  5273. pri->lastreset -= pri->resetinterval;
  5274. pri->lastreset += 5;
  5275. }
  5276. /* Take the channels from inalarm condition */
  5277. pri->resetting = 0;
  5278. for (i = 0; i < pri->numchans; i++) {
  5279. if (pri->pvts[i]) {
  5280. sig_pri_set_alarm(pri->pvts[i], 0);
  5281. }
  5282. }
  5283. sig_pri_span_devstate_changed(pri);
  5284. break;
  5285. case PRI_EVENT_DCHAN_DOWN:
  5286. pri_find_dchan(pri);
  5287. if (!pri_is_up(pri)) {
  5288. if (pri->sig == SIG_BRI_PTMP) {
  5289. /*
  5290. * For PTMP connections with non-persistent layer 2 we want to
  5291. * *not* declare inalarm unless there actually is an alarm.
  5292. */
  5293. break;
  5294. }
  5295. /* Hangup active channels and put them in alarm mode */
  5296. pri->resetting = 0;
  5297. for (i = 0; i < pri->numchans; i++) {
  5298. struct sig_pri_chan *p = pri->pvts[i];
  5299. if (p) {
  5300. if (pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
  5301. /* T309 is not enabled : destroy calls when alarm occurs */
  5302. if (p->call) {
  5303. pri_destroycall(p->pri->pri, p->call);
  5304. p->call = NULL;
  5305. }
  5306. if (p->owner)
  5307. p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  5308. }
  5309. sig_pri_set_alarm(p, 1);
  5310. }
  5311. }
  5312. sig_pri_span_devstate_changed(pri);
  5313. }
  5314. break;
  5315. case PRI_EVENT_RESTART:
  5316. if (e->restart.channel > -1 && PRI_CHANNEL(e->restart.channel) != 0xFF) {
  5317. chanpos = pri_find_principle(pri, e->restart.channel, NULL);
  5318. if (chanpos < 0)
  5319. ast_log(LOG_WARNING,
  5320. "Span %d: Restart requested on odd/unavailable channel number %d/%d\n",
  5321. pri->span, PRI_SPAN(e->restart.channel),
  5322. PRI_CHANNEL(e->restart.channel));
  5323. else {
  5324. int skipit = 0;
  5325. #if defined(HAVE_PRI_SERVICE_MESSAGES)
  5326. unsigned why;
  5327. why = pri->pvts[chanpos]->service_status;
  5328. if (why) {
  5329. ast_log(LOG_NOTICE,
  5330. "Span %d: Channel %d/%d out-of-service (reason: %s), ignoring RESTART\n",
  5331. pri->span, PRI_SPAN(e->restart.channel),
  5332. PRI_CHANNEL(e->restart.channel),
  5333. (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
  5334. skipit = 1;
  5335. }
  5336. #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
  5337. sig_pri_lock_private(pri->pvts[chanpos]);
  5338. if (!skipit) {
  5339. ast_verb(3, "Span %d: Channel %d/%d restarted\n", pri->span,
  5340. PRI_SPAN(e->restart.channel),
  5341. PRI_CHANNEL(e->restart.channel));
  5342. if (pri->pvts[chanpos]->call) {
  5343. pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
  5344. pri->pvts[chanpos]->call = NULL;
  5345. }
  5346. }
  5347. /* Force soft hangup if appropriate */
  5348. if (pri->pvts[chanpos]->owner)
  5349. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  5350. sig_pri_unlock_private(pri->pvts[chanpos]);
  5351. }
  5352. } else {
  5353. ast_verb(3, "Restart requested on entire span %d\n", pri->span);
  5354. for (x = 0; x < pri->numchans; x++)
  5355. if (pri->pvts[x]) {
  5356. sig_pri_lock_private(pri->pvts[x]);
  5357. if (pri->pvts[x]->call) {
  5358. pri_destroycall(pri->pri, pri->pvts[x]->call);
  5359. pri->pvts[x]->call = NULL;
  5360. }
  5361. if (pri->pvts[x]->owner)
  5362. pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  5363. sig_pri_unlock_private(pri->pvts[x]);
  5364. }
  5365. }
  5366. sig_pri_span_devstate_changed(pri);
  5367. break;
  5368. case PRI_EVENT_KEYPAD_DIGIT:
  5369. if (sig_pri_is_cis_call(e->digit.channel)) {
  5370. sig_pri_handle_cis_subcmds(pri, e->e, e->digit.subcmds,
  5371. e->digit.call);
  5372. break;
  5373. }
  5374. chanpos = pri_find_principle_by_call(pri, e->digit.call);
  5375. if (chanpos < 0) {
  5376. ast_log(LOG_WARNING,
  5377. "Span %d: Received keypad digits for unknown call.\n", pri->span);
  5378. break;
  5379. }
  5380. sig_pri_lock_private(pri->pvts[chanpos]);
  5381. sig_pri_handle_subcmds(pri, chanpos, e->e, e->digit.subcmds,
  5382. e->digit.call);
  5383. /* queue DTMF frame if the PBX for this call was already started (we're forwarding KEYPAD_DIGITs further on */
  5384. if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
  5385. && pri->pvts[chanpos]->owner) {
  5386. /* how to do that */
  5387. int digitlen = strlen(e->digit.digits);
  5388. int i;
  5389. for (i = 0; i < digitlen; i++) {
  5390. struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = e->digit.digits[i], };
  5391. pri_queue_frame(pri, chanpos, &f);
  5392. }
  5393. }
  5394. sig_pri_unlock_private(pri->pvts[chanpos]);
  5395. break;
  5396. case PRI_EVENT_INFO_RECEIVED:
  5397. if (sig_pri_is_cis_call(e->ring.channel)) {
  5398. sig_pri_handle_cis_subcmds(pri, e->e, e->ring.subcmds,
  5399. e->ring.call);
  5400. break;
  5401. }
  5402. chanpos = pri_find_principle_by_call(pri, e->ring.call);
  5403. if (chanpos < 0) {
  5404. ast_log(LOG_WARNING,
  5405. "Span %d: Received INFORMATION for unknown call.\n", pri->span);
  5406. break;
  5407. }
  5408. sig_pri_lock_private(pri->pvts[chanpos]);
  5409. sig_pri_handle_subcmds(pri, chanpos, e->e, e->ring.subcmds, e->ring.call);
  5410. /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */
  5411. if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
  5412. && pri->pvts[chanpos]->owner) {
  5413. /* how to do that */
  5414. int digitlen = strlen(e->ring.callednum);
  5415. int i;
  5416. for (i = 0; i < digitlen; i++) {
  5417. struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = e->ring.callednum[i], };
  5418. pri_queue_frame(pri, chanpos, &f);
  5419. }
  5420. }
  5421. sig_pri_unlock_private(pri->pvts[chanpos]);
  5422. break;
  5423. #if defined(HAVE_PRI_SERVICE_MESSAGES)
  5424. case PRI_EVENT_SERVICE:
  5425. chanpos = pri_find_principle(pri, e->service.channel, NULL);
  5426. if (chanpos < 0) {
  5427. ast_log(LOG_WARNING, "Received service change status %d on unconfigured channel %d/%d span %d\n",
  5428. e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
  5429. } else {
  5430. char db_chan_name[20];
  5431. char db_answer[5];
  5432. int ch;
  5433. unsigned *why;
  5434. ch = pri->pvts[chanpos]->channel;
  5435. snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->span, ch);
  5436. why = &pri->pvts[chanpos]->service_status;
  5437. switch (e->service.changestatus) {
  5438. case 0: /* in-service */
  5439. /* Far end wants to be in service now. */
  5440. ast_db_del(db_chan_name, SRVST_DBKEY);
  5441. *why &= ~SRVST_FAREND;
  5442. if (*why) {
  5443. snprintf(db_answer, sizeof(db_answer), "%s:%u",
  5444. SRVST_TYPE_OOS, *why);
  5445. ast_db_put(db_chan_name, SRVST_DBKEY, db_answer);
  5446. } else {
  5447. sig_pri_span_devstate_changed(pri);
  5448. }
  5449. break;
  5450. case 2: /* out-of-service */
  5451. /* Far end wants to be out-of-service now. */
  5452. ast_db_del(db_chan_name, SRVST_DBKEY);
  5453. *why |= SRVST_FAREND;
  5454. snprintf(db_answer, sizeof(db_answer), "%s:%u", SRVST_TYPE_OOS,
  5455. *why);
  5456. ast_db_put(db_chan_name, SRVST_DBKEY, db_answer);
  5457. sig_pri_span_devstate_changed(pri);
  5458. break;
  5459. default:
  5460. ast_log(LOG_ERROR, "Huh? changestatus is: %d\n", e->service.changestatus);
  5461. break;
  5462. }
  5463. ast_log(LOG_NOTICE, "Channel %d/%d span %d (logical: %d) received a change of service message, status '%d'\n",
  5464. PRI_SPAN(e->service.channel), PRI_CHANNEL(e->service.channel), pri->span, ch, e->service.changestatus);
  5465. }
  5466. break;
  5467. case PRI_EVENT_SERVICE_ACK:
  5468. chanpos = pri_find_principle(pri, e->service_ack.channel, NULL);
  5469. if (chanpos < 0) {
  5470. ast_log(LOG_WARNING, "Received service acknowledge change status '%d' on unconfigured channel %d/%d span %d\n",
  5471. e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
  5472. } else {
  5473. ast_debug(2, "Channel %d/%d span %d received a change os service acknowledgement message, status '%d'\n",
  5474. PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span, e->service_ack.changestatus);
  5475. }
  5476. break;
  5477. #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
  5478. case PRI_EVENT_RING:
  5479. if (!ast_strlen_zero(pri->msn_list)
  5480. && !sig_pri_msn_match(pri->msn_list, e->ring.callednum)) {
  5481. /* The call is not for us so ignore it. */
  5482. ast_verb(3,
  5483. "Ignoring call to '%s' on span %d. Its not in the MSN list: %s\n",
  5484. e->ring.callednum, pri->span, pri->msn_list);
  5485. pri_destroycall(pri->pri, e->ring.call);
  5486. break;
  5487. }
  5488. if (sig_pri_is_cis_call(e->ring.channel)) {
  5489. sig_pri_handle_cis_subcmds(pri, e->e, e->ring.subcmds,
  5490. e->ring.call);
  5491. break;
  5492. }
  5493. chanpos = pri_find_principle_by_call(pri, e->ring.call);
  5494. if (-1 < chanpos) {
  5495. /* Libpri has already filtered out duplicate SETUPs. */
  5496. ast_log(LOG_WARNING,
  5497. "Span %d: Got SETUP with duplicate call ptr (%p). Dropping call.\n",
  5498. pri->span, e->ring.call);
  5499. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5500. break;
  5501. }
  5502. if (e->ring.channel == -1 || PRI_CHANNEL(e->ring.channel) == 0xFF) {
  5503. /* Any channel requested. */
  5504. chanpos = pri_find_empty_chan(pri, 1);
  5505. } else if (PRI_CHANNEL(e->ring.channel) == 0x00) {
  5506. /* No channel specified. */
  5507. #if defined(HAVE_PRI_CALL_WAITING)
  5508. if (!pri->allow_call_waiting_calls)
  5509. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  5510. {
  5511. /* We will not accept incoming call waiting calls. */
  5512. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
  5513. break;
  5514. }
  5515. #if defined(HAVE_PRI_CALL_WAITING)
  5516. chanpos = pri_find_empty_nobch(pri);
  5517. if (chanpos < 0) {
  5518. /* We could not find/create a call interface. */
  5519. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
  5520. break;
  5521. }
  5522. /* Setup the call interface to use. */
  5523. sig_pri_init_config(pri->pvts[chanpos], pri);
  5524. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  5525. } else {
  5526. /* A channel is specified. */
  5527. chanpos = pri_find_principle(pri, e->ring.channel, e->ring.call);
  5528. if (chanpos < 0) {
  5529. ast_log(LOG_WARNING,
  5530. "Span %d: SETUP on unconfigured channel %d/%d\n",
  5531. pri->span, PRI_SPAN(e->ring.channel),
  5532. PRI_CHANNEL(e->ring.channel));
  5533. } else {
  5534. switch (pri->pvts[chanpos]->resetting) {
  5535. case SIG_PRI_RESET_IDLE:
  5536. break;
  5537. case SIG_PRI_RESET_ACTIVE:
  5538. /*
  5539. * The peer may have lost the expected ack or not received the
  5540. * RESTART yet.
  5541. */
  5542. pri->pvts[chanpos]->resetting = SIG_PRI_RESET_NO_ACK;
  5543. break;
  5544. case SIG_PRI_RESET_NO_ACK:
  5545. /* The peer likely is not going to ack the RESTART. */
  5546. ast_debug(1,
  5547. "Span %d: Second SETUP while waiting for RESTART ACKNOWLEDGE on channel %d/%d\n",
  5548. pri->span, PRI_SPAN(e->ring.channel),
  5549. PRI_CHANNEL(e->ring.channel));
  5550. /* Assume we got the ack. */
  5551. pri->pvts[chanpos]->resetting = SIG_PRI_RESET_IDLE;
  5552. if (pri->resetting) {
  5553. /* Go on to the next idle channel to RESTART. */
  5554. pri_check_restart(pri);
  5555. }
  5556. break;
  5557. }
  5558. if (!sig_pri_is_chan_available(pri->pvts[chanpos])) {
  5559. /* This is where we handle initial glare */
  5560. ast_debug(1,
  5561. "Span %d: SETUP requested unavailable channel %d/%d. Attempting to renegotiate.\n",
  5562. pri->span, PRI_SPAN(e->ring.channel),
  5563. PRI_CHANNEL(e->ring.channel));
  5564. chanpos = -1;
  5565. }
  5566. }
  5567. #if defined(ALWAYS_PICK_CHANNEL)
  5568. if (e->ring.flexible) {
  5569. chanpos = -1;
  5570. }
  5571. #endif /* defined(ALWAYS_PICK_CHANNEL) */
  5572. if (chanpos < 0 && e->ring.flexible) {
  5573. /* We can try to pick another channel. */
  5574. chanpos = pri_find_empty_chan(pri, 1);
  5575. }
  5576. }
  5577. if (chanpos < 0) {
  5578. if (e->ring.flexible) {
  5579. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
  5580. } else {
  5581. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
  5582. }
  5583. break;
  5584. }
  5585. sig_pri_lock_private(pri->pvts[chanpos]);
  5586. /* Mark channel as in use so noone else will steal it. */
  5587. pri->pvts[chanpos]->call = e->ring.call;
  5588. /* Use plancallingnum as a scratch buffer since it is initialized next. */
  5589. apply_plan_to_existing_number(plancallingnum, sizeof(plancallingnum), pri,
  5590. e->ring.redirectingnum, e->ring.callingplanrdnis);
  5591. sig_pri_set_rdnis(pri->pvts[chanpos], plancallingnum);
  5592. /* Setup caller-id info */
  5593. apply_plan_to_existing_number(plancallingnum, sizeof(plancallingnum), pri,
  5594. e->ring.callingnum, e->ring.callingplan);
  5595. pri->pvts[chanpos]->cid_ani2 = 0;
  5596. if (pri->pvts[chanpos]->use_callerid) {
  5597. ast_shrink_phone_number(plancallingnum);
  5598. ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
  5599. #ifdef PRI_ANI
  5600. apply_plan_to_existing_number(plancallingani, sizeof(plancallingani),
  5601. pri, e->ring.callingani, e->ring.callingplanani);
  5602. ast_shrink_phone_number(plancallingani);
  5603. ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani,
  5604. sizeof(pri->pvts[chanpos]->cid_ani));
  5605. #endif
  5606. pri->pvts[chanpos]->cid_subaddr[0] = '\0';
  5607. #if defined(HAVE_PRI_SUBADDR)
  5608. if (e->ring.calling.subaddress.valid) {
  5609. struct ast_party_subaddress calling_subaddress;
  5610. ast_party_subaddress_init(&calling_subaddress);
  5611. sig_pri_set_subaddress(&calling_subaddress,
  5612. &e->ring.calling.subaddress);
  5613. if (calling_subaddress.str) {
  5614. ast_copy_string(pri->pvts[chanpos]->cid_subaddr,
  5615. calling_subaddress.str,
  5616. sizeof(pri->pvts[chanpos]->cid_subaddr));
  5617. }
  5618. ast_party_subaddress_free(&calling_subaddress);
  5619. }
  5620. #endif /* defined(HAVE_PRI_SUBADDR) */
  5621. ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
  5622. pri->pvts[chanpos]->cid_ton = e->ring.callingplan; /* this is the callingplan (TON/NPI), e->ring.callingplan>>4 would be the TON */
  5623. pri->pvts[chanpos]->callingpres = e->ring.callingpres;
  5624. if (e->ring.ani2 >= 0) {
  5625. pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
  5626. }
  5627. } else {
  5628. pri->pvts[chanpos]->cid_num[0] = '\0';
  5629. pri->pvts[chanpos]->cid_subaddr[0] = '\0';
  5630. pri->pvts[chanpos]->cid_ani[0] = '\0';
  5631. pri->pvts[chanpos]->cid_name[0] = '\0';
  5632. pri->pvts[chanpos]->cid_ton = 0;
  5633. pri->pvts[chanpos]->callingpres = 0;
  5634. }
  5635. /* Setup the user tag for party id's from this device for this call. */
  5636. if (pri->append_msn_to_user_tag) {
  5637. snprintf(pri->pvts[chanpos]->user_tag,
  5638. sizeof(pri->pvts[chanpos]->user_tag), "%s_%s",
  5639. pri->initial_user_tag,
  5640. pri->nodetype == PRI_NETWORK
  5641. ? plancallingnum : e->ring.callednum);
  5642. } else {
  5643. ast_copy_string(pri->pvts[chanpos]->user_tag,
  5644. pri->initial_user_tag, sizeof(pri->pvts[chanpos]->user_tag));
  5645. }
  5646. sig_pri_set_caller_id(pri->pvts[chanpos]);
  5647. /* Set DNID on all incoming calls -- even immediate */
  5648. sig_pri_set_dnid(pri->pvts[chanpos], e->ring.callednum);
  5649. /* If immediate=yes go to s|1 */
  5650. if (pri->pvts[chanpos]->immediate) {
  5651. ast_verb(3, "Going to extension s|1 because of immediate=yes\n");
  5652. pri->pvts[chanpos]->exten[0] = 's';
  5653. pri->pvts[chanpos]->exten[1] = '\0';
  5654. }
  5655. /* Get called number */
  5656. else if (!ast_strlen_zero(e->ring.callednum)) {
  5657. ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
  5658. } else if (pri->overlapdial)
  5659. pri->pvts[chanpos]->exten[0] = '\0';
  5660. else {
  5661. /* Some PRI circuits are set up to send _no_ digits. Handle them as 's'. */
  5662. pri->pvts[chanpos]->exten[0] = 's';
  5663. pri->pvts[chanpos]->exten[1] = '\0';
  5664. }
  5665. /* No number yet, but received "sending complete"? */
  5666. if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
  5667. ast_verb(3, "Going to extension s|1 because of Complete received\n");
  5668. pri->pvts[chanpos]->exten[0] = 's';
  5669. pri->pvts[chanpos]->exten[1] = '\0';
  5670. }
  5671. /* Make sure extension exists (or in overlap dial mode, can exist) */
  5672. if (((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
  5673. ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
  5674. /* Select audio companding mode. */
  5675. switch (e->ring.layer1) {
  5676. case PRI_LAYER_1_ALAW:
  5677. law = SIG_PRI_ALAW;
  5678. break;
  5679. case PRI_LAYER_1_ULAW:
  5680. law = SIG_PRI_ULAW;
  5681. break;
  5682. default:
  5683. /* This is a data call to us. */
  5684. law = SIG_PRI_DEFLAW;
  5685. break;
  5686. }
  5687. if (e->ring.complete || !(pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
  5688. /* Just announce proceeding */
  5689. pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_PROCEEDING;
  5690. pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
  5691. } else if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
  5692. pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_CONNECT;
  5693. pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
  5694. } else {
  5695. pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_OVERLAP;
  5696. pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
  5697. }
  5698. /* Start PBX */
  5699. if (!e->ring.complete
  5700. && (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
  5701. && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
  5702. /*
  5703. * Release the PRI lock while we create the channel so other
  5704. * threads can send D channel messages. We must also release
  5705. * the private lock to prevent deadlock while creating the
  5706. * channel.
  5707. */
  5708. sig_pri_unlock_private(pri->pvts[chanpos]);
  5709. ast_mutex_unlock(&pri->lock);
  5710. c = sig_pri_new_ast_channel(pri->pvts[chanpos],
  5711. AST_STATE_RESERVED, law, e->ring.ctype,
  5712. pri->pvts[chanpos]->exten, NULL);
  5713. ast_mutex_lock(&pri->lock);
  5714. sig_pri_lock_private(pri->pvts[chanpos]);
  5715. if (c) {
  5716. #if defined(HAVE_PRI_SUBADDR)
  5717. if (e->ring.calling.subaddress.valid) {
  5718. /* Set Calling Subaddress */
  5719. sig_pri_lock_owner(pri, chanpos);
  5720. sig_pri_set_subaddress(
  5721. &pri->pvts[chanpos]->owner->caller.id.subaddress,
  5722. &e->ring.calling.subaddress);
  5723. if (!e->ring.calling.subaddress.type
  5724. && !ast_strlen_zero(
  5725. (char *) e->ring.calling.subaddress.data)) {
  5726. /* NSAP */
  5727. pbx_builtin_setvar_helper(c, "CALLINGSUBADDR",
  5728. (char *) e->ring.calling.subaddress.data);
  5729. }
  5730. ast_channel_unlock(c);
  5731. }
  5732. if (e->ring.called_subaddress.valid) {
  5733. /* Set Called Subaddress */
  5734. sig_pri_lock_owner(pri, chanpos);
  5735. sig_pri_set_subaddress(
  5736. &pri->pvts[chanpos]->owner->dialed.subaddress,
  5737. &e->ring.called_subaddress);
  5738. if (!e->ring.called_subaddress.type
  5739. && !ast_strlen_zero(
  5740. (char *) e->ring.called_subaddress.data)) {
  5741. /* NSAP */
  5742. pbx_builtin_setvar_helper(c, "CALLEDSUBADDR",
  5743. (char *) e->ring.called_subaddress.data);
  5744. }
  5745. ast_channel_unlock(c);
  5746. }
  5747. #else
  5748. if (!ast_strlen_zero(e->ring.callingsubaddr)) {
  5749. pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
  5750. }
  5751. #endif /* !defined(HAVE_PRI_SUBADDR) */
  5752. if (e->ring.ani2 >= 0) {
  5753. snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
  5754. pbx_builtin_setvar_helper(c, "ANI2", ani2str);
  5755. }
  5756. #ifdef SUPPORT_USERUSER
  5757. if (!ast_strlen_zero(e->ring.useruserinfo)) {
  5758. pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
  5759. }
  5760. #endif
  5761. snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
  5762. pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
  5763. if (e->ring.redirectingreason >= 0) {
  5764. /* This is now just a status variable. Use REDIRECTING() dialplan function. */
  5765. pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
  5766. }
  5767. #if defined(HAVE_PRI_REVERSE_CHARGE)
  5768. pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
  5769. #endif
  5770. #if defined(HAVE_PRI_SETUP_KEYPAD)
  5771. ast_copy_string(pri->pvts[chanpos]->keypad_digits,
  5772. e->ring.keypad_digits,
  5773. sizeof(pri->pvts[chanpos]->keypad_digits));
  5774. #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
  5775. sig_pri_handle_subcmds(pri, chanpos, e->e, e->ring.subcmds,
  5776. e->ring.call);
  5777. if (!pri->pvts[chanpos]->digital
  5778. && !pri->pvts[chanpos]->no_b_channel) {
  5779. /*
  5780. * Call has a channel.
  5781. * Indicate that we are providing dialtone.
  5782. */
  5783. pri->pvts[chanpos]->progress = 1;/* No need to send plain PROGRESS again. */
  5784. #ifdef HAVE_PRI_PROG_W_CAUSE
  5785. pri_progress_with_cause(pri->pri, e->ring.call,
  5786. PVT_TO_CHANNEL(pri->pvts[chanpos]), 1, -1);/* no cause at all */
  5787. #else
  5788. pri_progress(pri->pri, e->ring.call,
  5789. PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
  5790. #endif
  5791. }
  5792. }
  5793. if (c && !ast_pthread_create_detached(&threadid, NULL, pri_ss_thread, pri->pvts[chanpos])) {
  5794. ast_verb(3, "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
  5795. plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
  5796. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
  5797. } else {
  5798. ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
  5799. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
  5800. if (c) {
  5801. /* Avoid deadlock while destroying channel */
  5802. sig_pri_unlock_private(pri->pvts[chanpos]);
  5803. ast_mutex_unlock(&pri->lock);
  5804. ast_hangup(c);
  5805. ast_mutex_lock(&pri->lock);
  5806. } else {
  5807. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
  5808. pri->pvts[chanpos]->call = NULL;
  5809. sig_pri_unlock_private(pri->pvts[chanpos]);
  5810. sig_pri_span_devstate_changed(pri);
  5811. }
  5812. break;
  5813. }
  5814. } else {
  5815. /*
  5816. * Release the PRI lock while we create the channel so other
  5817. * threads can send D channel messages. We must also release
  5818. * the private lock to prevent deadlock while creating the
  5819. * channel.
  5820. */
  5821. sig_pri_unlock_private(pri->pvts[chanpos]);
  5822. ast_mutex_unlock(&pri->lock);
  5823. c = sig_pri_new_ast_channel(pri->pvts[chanpos],
  5824. AST_STATE_RING, law, e->ring.ctype,
  5825. pri->pvts[chanpos]->exten, NULL);
  5826. ast_mutex_lock(&pri->lock);
  5827. sig_pri_lock_private(pri->pvts[chanpos]);
  5828. if (c) {
  5829. /*
  5830. * It is reasonably safe to set the following
  5831. * channel variables while the PRI and DAHDI private
  5832. * structures are locked. The PBX has not been
  5833. * started yet and it is unlikely that any other task
  5834. * will do anything with the channel we have just
  5835. * created.
  5836. */
  5837. #if defined(HAVE_PRI_SUBADDR)
  5838. if (e->ring.calling.subaddress.valid) {
  5839. /* Set Calling Subaddress */
  5840. sig_pri_lock_owner(pri, chanpos);
  5841. sig_pri_set_subaddress(
  5842. &pri->pvts[chanpos]->owner->caller.id.subaddress,
  5843. &e->ring.calling.subaddress);
  5844. if (!e->ring.calling.subaddress.type
  5845. && !ast_strlen_zero(
  5846. (char *) e->ring.calling.subaddress.data)) {
  5847. /* NSAP */
  5848. pbx_builtin_setvar_helper(c, "CALLINGSUBADDR",
  5849. (char *) e->ring.calling.subaddress.data);
  5850. }
  5851. ast_channel_unlock(c);
  5852. }
  5853. if (e->ring.called_subaddress.valid) {
  5854. /* Set Called Subaddress */
  5855. sig_pri_lock_owner(pri, chanpos);
  5856. sig_pri_set_subaddress(
  5857. &pri->pvts[chanpos]->owner->dialed.subaddress,
  5858. &e->ring.called_subaddress);
  5859. if (!e->ring.called_subaddress.type
  5860. && !ast_strlen_zero(
  5861. (char *) e->ring.called_subaddress.data)) {
  5862. /* NSAP */
  5863. pbx_builtin_setvar_helper(c, "CALLEDSUBADDR",
  5864. (char *) e->ring.called_subaddress.data);
  5865. }
  5866. ast_channel_unlock(c);
  5867. }
  5868. #else
  5869. if (!ast_strlen_zero(e->ring.callingsubaddr)) {
  5870. pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
  5871. }
  5872. #endif /* !defined(HAVE_PRI_SUBADDR) */
  5873. if (e->ring.ani2 >= 0) {
  5874. snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
  5875. pbx_builtin_setvar_helper(c, "ANI2", ani2str);
  5876. }
  5877. #ifdef SUPPORT_USERUSER
  5878. if (!ast_strlen_zero(e->ring.useruserinfo)) {
  5879. pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
  5880. }
  5881. #endif
  5882. if (e->ring.redirectingreason >= 0) {
  5883. /* This is now just a status variable. Use REDIRECTING() dialplan function. */
  5884. pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
  5885. }
  5886. #if defined(HAVE_PRI_REVERSE_CHARGE)
  5887. pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
  5888. #endif
  5889. #if defined(HAVE_PRI_SETUP_KEYPAD)
  5890. ast_copy_string(pri->pvts[chanpos]->keypad_digits,
  5891. e->ring.keypad_digits,
  5892. sizeof(pri->pvts[chanpos]->keypad_digits));
  5893. #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
  5894. snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
  5895. pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
  5896. sig_pri_handle_subcmds(pri, chanpos, e->e, e->ring.subcmds,
  5897. e->ring.call);
  5898. }
  5899. if (c && !ast_pbx_start(c)) {
  5900. ast_verb(3, "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
  5901. plancallingnum, pri->pvts[chanpos]->exten,
  5902. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
  5903. sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
  5904. } else {
  5905. ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
  5906. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
  5907. if (c) {
  5908. /* Avoid deadlock while destroying channel */
  5909. sig_pri_unlock_private(pri->pvts[chanpos]);
  5910. ast_mutex_unlock(&pri->lock);
  5911. ast_hangup(c);
  5912. ast_mutex_lock(&pri->lock);
  5913. } else {
  5914. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
  5915. pri->pvts[chanpos]->call = NULL;
  5916. sig_pri_unlock_private(pri->pvts[chanpos]);
  5917. sig_pri_span_devstate_changed(pri);
  5918. }
  5919. break;
  5920. }
  5921. }
  5922. } else {
  5923. ast_verb(3,
  5924. "Span %d: Extension %s@%s does not exist. Rejecting call from '%s'.\n",
  5925. pri->span, pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context,
  5926. pri->pvts[chanpos]->cid_num);
  5927. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
  5928. pri->pvts[chanpos]->call = NULL;
  5929. pri->pvts[chanpos]->exten[0] = '\0';
  5930. sig_pri_unlock_private(pri->pvts[chanpos]);
  5931. sig_pri_span_devstate_changed(pri);
  5932. break;
  5933. }
  5934. sig_pri_unlock_private(pri->pvts[chanpos]);
  5935. break;
  5936. case PRI_EVENT_RINGING:
  5937. if (sig_pri_is_cis_call(e->ringing.channel)) {
  5938. sig_pri_handle_cis_subcmds(pri, e->e, e->ringing.subcmds,
  5939. e->ringing.call);
  5940. break;
  5941. }
  5942. chanpos = pri_find_fixup_principle(pri, e->ringing.channel,
  5943. e->ringing.call);
  5944. if (chanpos < 0) {
  5945. break;
  5946. }
  5947. sig_pri_lock_private(pri->pvts[chanpos]);
  5948. sig_pri_handle_subcmds(pri, chanpos, e->e, e->ringing.subcmds,
  5949. e->ringing.call);
  5950. sig_pri_cc_generic_check(pri, chanpos, AST_CC_CCNR);
  5951. sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
  5952. sig_pri_lock_owner(pri, chanpos);
  5953. if (pri->pvts[chanpos]->owner) {
  5954. ast_setstate(pri->pvts[chanpos]->owner, AST_STATE_RINGING);
  5955. ast_channel_unlock(pri->pvts[chanpos]->owner);
  5956. }
  5957. pri_queue_control(pri, chanpos, AST_CONTROL_RINGING);
  5958. if (pri->pvts[chanpos]->call_level < SIG_PRI_CALL_LEVEL_ALERTING) {
  5959. pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_ALERTING;
  5960. }
  5961. if (!pri->pvts[chanpos]->progress
  5962. && !pri->pvts[chanpos]->no_b_channel
  5963. #ifdef PRI_PROGRESS_MASK
  5964. && (e->ringing.progressmask
  5965. & (PRI_PROG_CALL_NOT_E2E_ISDN | PRI_PROG_INBAND_AVAILABLE))
  5966. #else
  5967. && e->ringing.progress == 8
  5968. #endif
  5969. ) {
  5970. /* Bring voice path up */
  5971. pri_queue_control(pri, chanpos, AST_CONTROL_PROGRESS);
  5972. pri->pvts[chanpos]->progress = 1;
  5973. sig_pri_set_dialing(pri->pvts[chanpos], 0);
  5974. sig_pri_open_media(pri->pvts[chanpos]);
  5975. }
  5976. #ifdef SUPPORT_USERUSER
  5977. if (!ast_strlen_zero(e->ringing.useruserinfo)) {
  5978. struct ast_channel *owner;
  5979. sig_pri_lock_owner(pri, chanpos);
  5980. owner = pri->pvts[chanpos]->owner;
  5981. if (owner) {
  5982. pbx_builtin_setvar_helper(owner, "USERUSERINFO",
  5983. e->ringing.useruserinfo);
  5984. ast_channel_unlock(owner);
  5985. }
  5986. }
  5987. #endif
  5988. sig_pri_unlock_private(pri->pvts[chanpos]);
  5989. break;
  5990. case PRI_EVENT_PROGRESS:
  5991. if (sig_pri_is_cis_call(e->proceeding.channel)) {
  5992. sig_pri_handle_cis_subcmds(pri, e->e, e->proceeding.subcmds,
  5993. e->proceeding.call);
  5994. break;
  5995. }
  5996. chanpos = pri_find_fixup_principle(pri, e->proceeding.channel,
  5997. e->proceeding.call);
  5998. if (chanpos < 0) {
  5999. break;
  6000. }
  6001. sig_pri_lock_private(pri->pvts[chanpos]);
  6002. sig_pri_handle_subcmds(pri, chanpos, e->e, e->proceeding.subcmds,
  6003. e->proceeding.call);
  6004. if (e->proceeding.cause > -1) {
  6005. ast_verb(3, "PROGRESS with cause code %d received\n", e->proceeding.cause);
  6006. /* Work around broken, out of spec USER_BUSY cause in a progress message */
  6007. if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
  6008. if (pri->pvts[chanpos]->owner) {
  6009. ast_verb(3, "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
  6010. pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
  6011. pri_queue_control(pri, chanpos, AST_CONTROL_BUSY);
  6012. }
  6013. }
  6014. }
  6015. if (!pri->pvts[chanpos]->progress
  6016. && !pri->pvts[chanpos]->no_b_channel
  6017. #ifdef PRI_PROGRESS_MASK
  6018. && (e->proceeding.progressmask
  6019. & (PRI_PROG_CALL_NOT_E2E_ISDN | PRI_PROG_INBAND_AVAILABLE))
  6020. #else
  6021. && e->proceeding.progress == 8
  6022. #endif
  6023. ) {
  6024. /* Bring voice path up */
  6025. ast_debug(1,
  6026. "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
  6027. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,
  6028. pri->span);
  6029. pri_queue_control(pri, chanpos, AST_CONTROL_PROGRESS);
  6030. pri->pvts[chanpos]->progress = 1;
  6031. sig_pri_set_dialing(pri->pvts[chanpos], 0);
  6032. sig_pri_open_media(pri->pvts[chanpos]);
  6033. }
  6034. sig_pri_unlock_private(pri->pvts[chanpos]);
  6035. break;
  6036. case PRI_EVENT_PROCEEDING:
  6037. if (sig_pri_is_cis_call(e->proceeding.channel)) {
  6038. sig_pri_handle_cis_subcmds(pri, e->e, e->proceeding.subcmds,
  6039. e->proceeding.call);
  6040. break;
  6041. }
  6042. chanpos = pri_find_fixup_principle(pri, e->proceeding.channel,
  6043. e->proceeding.call);
  6044. if (chanpos < 0) {
  6045. break;
  6046. }
  6047. sig_pri_lock_private(pri->pvts[chanpos]);
  6048. sig_pri_handle_subcmds(pri, chanpos, e->e, e->proceeding.subcmds,
  6049. e->proceeding.call);
  6050. if (pri->pvts[chanpos]->call_level < SIG_PRI_CALL_LEVEL_PROCEEDING) {
  6051. pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_PROCEEDING;
  6052. ast_debug(1,
  6053. "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
  6054. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,
  6055. pri->span);
  6056. pri_queue_control(pri, chanpos, AST_CONTROL_PROCEEDING);
  6057. }
  6058. if (!pri->pvts[chanpos]->progress
  6059. && !pri->pvts[chanpos]->no_b_channel
  6060. #ifdef PRI_PROGRESS_MASK
  6061. && (e->proceeding.progressmask
  6062. & (PRI_PROG_CALL_NOT_E2E_ISDN | PRI_PROG_INBAND_AVAILABLE))
  6063. #else
  6064. && e->proceeding.progress == 8
  6065. #endif
  6066. ) {
  6067. /* Bring voice path up */
  6068. pri_queue_control(pri, chanpos, AST_CONTROL_PROGRESS);
  6069. pri->pvts[chanpos]->progress = 1;
  6070. sig_pri_open_media(pri->pvts[chanpos]);
  6071. }
  6072. sig_pri_set_dialing(pri->pvts[chanpos], 0);
  6073. sig_pri_unlock_private(pri->pvts[chanpos]);
  6074. break;
  6075. case PRI_EVENT_FACILITY:
  6076. if (!e->facility.call || sig_pri_is_cis_call(e->facility.channel)) {
  6077. /* Event came in on the dummy channel or a CIS call. */
  6078. #if defined(HAVE_PRI_CALL_REROUTING)
  6079. sig_pri_handle_cis_subcmds(pri, e->e, e->facility.subcmds,
  6080. e->facility.subcall);
  6081. #else
  6082. sig_pri_handle_cis_subcmds(pri, e->e, e->facility.subcmds,
  6083. e->facility.call);
  6084. #endif /* !defined(HAVE_PRI_CALL_REROUTING) */
  6085. break;
  6086. }
  6087. chanpos = pri_find_principle_by_call(pri, e->facility.call);
  6088. if (chanpos < 0) {
  6089. ast_log(LOG_WARNING, "Span %d: Received facility for unknown call.\n",
  6090. pri->span);
  6091. break;
  6092. }
  6093. sig_pri_lock_private(pri->pvts[chanpos]);
  6094. #if defined(HAVE_PRI_CALL_REROUTING)
  6095. sig_pri_handle_subcmds(pri, chanpos, e->e, e->facility.subcmds,
  6096. e->facility.subcall);
  6097. #else
  6098. sig_pri_handle_subcmds(pri, chanpos, e->e, e->facility.subcmds,
  6099. e->facility.call);
  6100. #endif /* !defined(HAVE_PRI_CALL_REROUTING) */
  6101. sig_pri_unlock_private(pri->pvts[chanpos]);
  6102. break;
  6103. case PRI_EVENT_ANSWER:
  6104. if (sig_pri_is_cis_call(e->answer.channel)) {
  6105. #if defined(HAVE_PRI_CALL_WAITING)
  6106. /* Call is CIS so do normal CONNECT_ACKNOWLEDGE. */
  6107. pri_connect_ack(pri->pri, e->answer.call, 0);
  6108. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  6109. sig_pri_handle_cis_subcmds(pri, e->e, e->answer.subcmds,
  6110. e->answer.call);
  6111. break;
  6112. }
  6113. chanpos = pri_find_fixup_principle(pri, e->answer.channel, e->answer.call);
  6114. if (chanpos < 0) {
  6115. break;
  6116. }
  6117. #if defined(HAVE_PRI_CALL_WAITING)
  6118. if (pri->pvts[chanpos]->is_call_waiting) {
  6119. if (pri->pvts[chanpos]->no_b_channel) {
  6120. int new_chanpos;
  6121. /*
  6122. * Need to find a free channel now or
  6123. * kill the call with PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION.
  6124. */
  6125. new_chanpos = pri_find_empty_chan(pri, 1);
  6126. if (0 <= new_chanpos) {
  6127. new_chanpos = pri_fixup_principle(pri, new_chanpos,
  6128. e->answer.call);
  6129. }
  6130. if (new_chanpos < 0) {
  6131. /*
  6132. * Either no channel was available or someone stole
  6133. * the channel!
  6134. */
  6135. ast_verb(3,
  6136. "Span %d: Channel not available for call waiting call.\n",
  6137. pri->span);
  6138. sig_pri_lock_private(pri->pvts[chanpos]);
  6139. sig_pri_handle_subcmds(pri, chanpos, e->e, e->answer.subcmds,
  6140. e->answer.call);
  6141. sig_pri_cc_generic_check(pri, chanpos, AST_CC_CCBS);
  6142. sig_pri_lock_owner(pri, chanpos);
  6143. if (pri->pvts[chanpos]->owner) {
  6144. pri->pvts[chanpos]->owner->hangupcause = PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION;
  6145. switch (pri->pvts[chanpos]->owner->_state) {
  6146. case AST_STATE_BUSY:
  6147. case AST_STATE_UP:
  6148. ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
  6149. break;
  6150. default:
  6151. pri_queue_control(pri, chanpos, AST_CONTROL_CONGESTION);
  6152. break;
  6153. }
  6154. ast_channel_unlock(pri->pvts[chanpos]->owner);
  6155. } else {
  6156. pri->pvts[chanpos]->is_call_waiting = 0;
  6157. ast_atomic_fetchadd_int(&pri->num_call_waiting_calls, -1);
  6158. pri_hangup(pri->pri, e->answer.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
  6159. pri->pvts[chanpos]->call = NULL;
  6160. }
  6161. sig_pri_unlock_private(pri->pvts[chanpos]);
  6162. sig_pri_span_devstate_changed(pri);
  6163. break;
  6164. }
  6165. chanpos = new_chanpos;
  6166. }
  6167. pri_connect_ack(pri->pri, e->answer.call, PVT_TO_CHANNEL(pri->pvts[chanpos]));
  6168. sig_pri_span_devstate_changed(pri);
  6169. } else {
  6170. /* Call is normal so do normal CONNECT_ACKNOWLEDGE. */
  6171. pri_connect_ack(pri->pri, e->answer.call, 0);
  6172. }
  6173. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  6174. sig_pri_lock_private(pri->pvts[chanpos]);
  6175. #if defined(HAVE_PRI_CALL_WAITING)
  6176. if (pri->pvts[chanpos]->is_call_waiting) {
  6177. pri->pvts[chanpos]->is_call_waiting = 0;
  6178. ast_atomic_fetchadd_int(&pri->num_call_waiting_calls, -1);
  6179. }
  6180. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  6181. sig_pri_handle_subcmds(pri, chanpos, e->e, e->answer.subcmds,
  6182. e->answer.call);
  6183. if (!ast_strlen_zero(pri->pvts[chanpos]->deferred_digits)) {
  6184. /* We have some 'w' deferred digits to dial now. */
  6185. ast_verb(3,
  6186. "Span %d: Channel %d/%d dialing deferred digit string: %s\n",
  6187. pri->span, pri->pvts[chanpos]->logicalspan,
  6188. pri->pvts[chanpos]->prioffset,
  6189. pri->pvts[chanpos]->deferred_digits);
  6190. if (pri->pvts[chanpos]->call_level < SIG_PRI_CALL_LEVEL_DEFER_DIAL) {
  6191. pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_DEFER_DIAL;
  6192. }
  6193. sig_pri_dial_digits(pri->pvts[chanpos],
  6194. pri->pvts[chanpos]->deferred_digits);
  6195. } else {
  6196. if (pri->pvts[chanpos]->call_level < SIG_PRI_CALL_LEVEL_CONNECT) {
  6197. pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_CONNECT;
  6198. }
  6199. sig_pri_open_media(pri->pvts[chanpos]);
  6200. pri_queue_control(pri, chanpos, AST_CONTROL_ANSWER);
  6201. sig_pri_set_dialing(pri->pvts[chanpos], 0);
  6202. /* Enable echo cancellation if it's not on already */
  6203. sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
  6204. }
  6205. #ifdef SUPPORT_USERUSER
  6206. if (!ast_strlen_zero(e->answer.useruserinfo)) {
  6207. struct ast_channel *owner;
  6208. sig_pri_lock_owner(pri, chanpos);
  6209. owner = pri->pvts[chanpos]->owner;
  6210. if (owner) {
  6211. pbx_builtin_setvar_helper(owner, "USERUSERINFO",
  6212. e->answer.useruserinfo);
  6213. ast_channel_unlock(owner);
  6214. }
  6215. }
  6216. #endif
  6217. sig_pri_unlock_private(pri->pvts[chanpos]);
  6218. break;
  6219. #if defined(HAVE_PRI_CALL_WAITING)
  6220. case PRI_EVENT_CONNECT_ACK:
  6221. if (sig_pri_is_cis_call(e->connect_ack.channel)) {
  6222. sig_pri_handle_cis_subcmds(pri, e->e, e->connect_ack.subcmds,
  6223. e->connect_ack.call);
  6224. break;
  6225. }
  6226. chanpos = pri_find_fixup_principle(pri, e->connect_ack.channel,
  6227. e->connect_ack.call);
  6228. if (chanpos < 0) {
  6229. break;
  6230. }
  6231. sig_pri_lock_private(pri->pvts[chanpos]);
  6232. sig_pri_handle_subcmds(pri, chanpos, e->e, e->connect_ack.subcmds,
  6233. e->connect_ack.call);
  6234. sig_pri_open_media(pri->pvts[chanpos]);
  6235. sig_pri_unlock_private(pri->pvts[chanpos]);
  6236. sig_pri_span_devstate_changed(pri);
  6237. break;
  6238. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  6239. case PRI_EVENT_HANGUP:
  6240. if (sig_pri_is_cis_call(e->hangup.channel)) {
  6241. sig_pri_handle_cis_subcmds(pri, e->e, e->hangup.subcmds,
  6242. e->hangup.call);
  6243. pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
  6244. break;
  6245. }
  6246. chanpos = pri_find_principle_by_call(pri, e->hangup.call);
  6247. if (chanpos < 0) {
  6248. /*
  6249. * Continue hanging up the call even though
  6250. * we do not remember it (if we ever did).
  6251. */
  6252. pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
  6253. break;
  6254. }
  6255. sig_pri_lock_private(pri->pvts[chanpos]);
  6256. sig_pri_handle_subcmds(pri, chanpos, e->e, e->hangup.subcmds,
  6257. e->hangup.call);
  6258. switch (e->hangup.cause) {
  6259. case PRI_CAUSE_INVALID_CALL_REFERENCE:
  6260. /*
  6261. * The peer denies the existence of this call so we must
  6262. * continue hanging it up and forget about it.
  6263. */
  6264. pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
  6265. pri->pvts[chanpos]->call = NULL;
  6266. break;
  6267. default:
  6268. break;
  6269. }
  6270. if (!pri->pvts[chanpos]->alreadyhungup) {
  6271. /* we're calling here dahdi_hangup so once we get there we need to clear p->call after calling pri_hangup */
  6272. pri->pvts[chanpos]->alreadyhungup = 1;
  6273. switch (e->hangup.cause) {
  6274. case PRI_CAUSE_USER_BUSY:
  6275. case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
  6276. sig_pri_cc_generic_check(pri, chanpos, AST_CC_CCBS);
  6277. break;
  6278. default:
  6279. break;
  6280. }
  6281. if (pri->pvts[chanpos]->owner) {
  6282. int do_hangup = 0;
  6283. /* Queue a BUSY instead of a hangup if our cause is appropriate */
  6284. pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
  6285. switch (pri->pvts[chanpos]->owner->_state) {
  6286. case AST_STATE_BUSY:
  6287. case AST_STATE_UP:
  6288. do_hangup = 1;
  6289. break;
  6290. default:
  6291. if (!pri->pvts[chanpos]->outgoing) {
  6292. /*
  6293. * The incoming call leg hung up before getting
  6294. * connected so just hangup the call.
  6295. */
  6296. do_hangup = 1;
  6297. break;
  6298. }
  6299. switch (e->hangup.cause) {
  6300. case PRI_CAUSE_USER_BUSY:
  6301. pri_queue_control(pri, chanpos, AST_CONTROL_BUSY);
  6302. break;
  6303. case PRI_CAUSE_CALL_REJECTED:
  6304. case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
  6305. case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
  6306. case PRI_CAUSE_SWITCH_CONGESTION:
  6307. case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
  6308. case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
  6309. pri_queue_control(pri, chanpos, AST_CONTROL_CONGESTION);
  6310. break;
  6311. default:
  6312. do_hangup = 1;
  6313. break;
  6314. }
  6315. break;
  6316. }
  6317. if (do_hangup) {
  6318. #if defined(HAVE_PRI_AOC_EVENTS)
  6319. if (detect_aoc_e_subcmd(e->hangup.subcmds)) {
  6320. /* If a AOC-E msg was sent during the release, we must use a
  6321. * AST_CONTROL_HANGUP frame to guarantee that frame gets read before hangup */
  6322. pri_queue_control(pri, chanpos, AST_CONTROL_HANGUP);
  6323. } else {
  6324. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  6325. }
  6326. #else
  6327. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  6328. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  6329. }
  6330. } else {
  6331. /*
  6332. * Continue hanging up the call even though
  6333. * we do not have an owner.
  6334. */
  6335. pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
  6336. pri->pvts[chanpos]->call = NULL;
  6337. }
  6338. ast_verb(3, "Span %d: Channel %d/%d got hangup, cause %d\n",
  6339. pri->span, pri->pvts[chanpos]->logicalspan,
  6340. pri->pvts[chanpos]->prioffset, e->hangup.cause);
  6341. } else {
  6342. /* Continue hanging up the call. */
  6343. pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
  6344. pri->pvts[chanpos]->call = NULL;
  6345. }
  6346. #if defined(FORCE_RESTART_UNAVAIL_CHANS)
  6347. if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL
  6348. && pri->sig != SIG_BRI_PTMP && !pri->resetting
  6349. && pri->pvts[chanpos]->resetting == SIG_PRI_RESET_IDLE) {
  6350. ast_verb(3,
  6351. "Span %d: Forcing restart of channel %d/%d since channel reported in use\n",
  6352. pri->span, pri->pvts[chanpos]->logicalspan,
  6353. pri->pvts[chanpos]->prioffset);
  6354. pri->pvts[chanpos]->resetting = SIG_PRI_RESET_ACTIVE;
  6355. pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
  6356. }
  6357. #endif /* defined(FORCE_RESTART_UNAVAIL_CHANS) */
  6358. if (e->hangup.aoc_units > -1)
  6359. ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
  6360. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
  6361. #ifdef SUPPORT_USERUSER
  6362. if (!ast_strlen_zero(e->hangup.useruserinfo)) {
  6363. struct ast_channel *owner;
  6364. sig_pri_lock_owner(pri, chanpos);
  6365. owner = pri->pvts[chanpos]->owner;
  6366. if (owner) {
  6367. pbx_builtin_setvar_helper(owner, "USERUSERINFO",
  6368. e->hangup.useruserinfo);
  6369. ast_channel_unlock(owner);
  6370. }
  6371. }
  6372. #endif
  6373. sig_pri_unlock_private(pri->pvts[chanpos]);
  6374. sig_pri_span_devstate_changed(pri);
  6375. break;
  6376. case PRI_EVENT_HANGUP_REQ:
  6377. if (sig_pri_is_cis_call(e->hangup.channel)) {
  6378. sig_pri_handle_cis_subcmds(pri, e->e, e->hangup.subcmds,
  6379. e->hangup.call);
  6380. pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
  6381. break;
  6382. }
  6383. chanpos = pri_find_principle_by_call(pri, e->hangup.call);
  6384. if (chanpos < 0) {
  6385. /*
  6386. * Continue hanging up the call even though
  6387. * we do not remember it (if we ever did).
  6388. */
  6389. pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
  6390. break;
  6391. }
  6392. sig_pri_lock_private(pri->pvts[chanpos]);
  6393. sig_pri_handle_subcmds(pri, chanpos, e->e, e->hangup.subcmds,
  6394. e->hangup.call);
  6395. #if defined(HAVE_PRI_CALL_HOLD)
  6396. if (e->hangup.call_active && e->hangup.call_held
  6397. && pri->hold_disconnect_transfer) {
  6398. /* We are to transfer the call instead of simply hanging up. */
  6399. sig_pri_unlock_private(pri->pvts[chanpos]);
  6400. if (!sig_pri_attempt_transfer(pri, e->hangup.call_held, 1,
  6401. e->hangup.call_active, 0, NULL, NULL)) {
  6402. break;
  6403. }
  6404. sig_pri_lock_private(pri->pvts[chanpos]);
  6405. }
  6406. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  6407. switch (e->hangup.cause) {
  6408. case PRI_CAUSE_USER_BUSY:
  6409. case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
  6410. sig_pri_cc_generic_check(pri, chanpos, AST_CC_CCBS);
  6411. break;
  6412. case PRI_CAUSE_INVALID_CALL_REFERENCE:
  6413. /*
  6414. * The peer denies the existence of this call so we must
  6415. * continue hanging it up and forget about it. We should not
  6416. * get this cause here, but for completeness we will handle it
  6417. * anyway.
  6418. */
  6419. pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
  6420. pri->pvts[chanpos]->call = NULL;
  6421. break;
  6422. default:
  6423. break;
  6424. }
  6425. if (pri->pvts[chanpos]->owner) {
  6426. int do_hangup = 0;
  6427. pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
  6428. switch (pri->pvts[chanpos]->owner->_state) {
  6429. case AST_STATE_BUSY:
  6430. case AST_STATE_UP:
  6431. do_hangup = 1;
  6432. break;
  6433. default:
  6434. if (!pri->pvts[chanpos]->outgoing) {
  6435. /*
  6436. * The incoming call leg hung up before getting
  6437. * connected so just hangup the call.
  6438. */
  6439. do_hangup = 1;
  6440. break;
  6441. }
  6442. switch (e->hangup.cause) {
  6443. case PRI_CAUSE_USER_BUSY:
  6444. pri_queue_control(pri, chanpos, AST_CONTROL_BUSY);
  6445. break;
  6446. case PRI_CAUSE_CALL_REJECTED:
  6447. case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
  6448. case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
  6449. case PRI_CAUSE_SWITCH_CONGESTION:
  6450. case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
  6451. case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
  6452. pri_queue_control(pri, chanpos, AST_CONTROL_CONGESTION);
  6453. break;
  6454. default:
  6455. do_hangup = 1;
  6456. break;
  6457. }
  6458. break;
  6459. }
  6460. if (do_hangup) {
  6461. #if defined(HAVE_PRI_AOC_EVENTS)
  6462. if (!pri->pvts[chanpos]->holding_aoce
  6463. && pri->aoce_delayhangup
  6464. && ast_bridged_channel(pri->pvts[chanpos]->owner)) {
  6465. sig_pri_send_aoce_termination_request(pri, chanpos,
  6466. pri_get_timer(pri->pri, PRI_TIMER_T305) / 2);
  6467. } else if (detect_aoc_e_subcmd(e->hangup.subcmds)) {
  6468. /* If a AOC-E msg was sent during the Disconnect, we must use a AST_CONTROL_HANGUP frame
  6469. * to guarantee that frame gets read before hangup */
  6470. pri_queue_control(pri, chanpos, AST_CONTROL_HANGUP);
  6471. } else {
  6472. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  6473. }
  6474. #else
  6475. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  6476. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  6477. }
  6478. ast_verb(3, "Span %d: Channel %d/%d got hangup request, cause %d\n",
  6479. pri->span, pri->pvts[chanpos]->logicalspan,
  6480. pri->pvts[chanpos]->prioffset, e->hangup.cause);
  6481. } else {
  6482. /*
  6483. * Continue hanging up the call even though
  6484. * we do not have an owner.
  6485. */
  6486. pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
  6487. pri->pvts[chanpos]->call = NULL;
  6488. }
  6489. #if defined(FORCE_RESTART_UNAVAIL_CHANS)
  6490. if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL
  6491. && pri->sig != SIG_BRI_PTMP && !pri->resetting
  6492. && pri->pvts[chanpos]->resetting == SIG_PRI_RESET_IDLE) {
  6493. ast_verb(3,
  6494. "Span %d: Forcing restart of channel %d/%d since channel reported in use\n",
  6495. pri->span, pri->pvts[chanpos]->logicalspan,
  6496. pri->pvts[chanpos]->prioffset);
  6497. pri->pvts[chanpos]->resetting = SIG_PRI_RESET_ACTIVE;
  6498. pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
  6499. }
  6500. #endif /* defined(FORCE_RESTART_UNAVAIL_CHANS) */
  6501. #ifdef SUPPORT_USERUSER
  6502. if (!ast_strlen_zero(e->hangup.useruserinfo)) {
  6503. struct ast_channel *owner;
  6504. sig_pri_lock_owner(pri, chanpos);
  6505. owner = pri->pvts[chanpos]->owner;
  6506. if (owner) {
  6507. pbx_builtin_setvar_helper(owner, "USERUSERINFO",
  6508. e->hangup.useruserinfo);
  6509. ast_channel_unlock(owner);
  6510. }
  6511. }
  6512. #endif
  6513. sig_pri_unlock_private(pri->pvts[chanpos]);
  6514. sig_pri_span_devstate_changed(pri);
  6515. break;
  6516. case PRI_EVENT_HANGUP_ACK:
  6517. if (sig_pri_is_cis_call(e->hangup.channel)) {
  6518. sig_pri_handle_cis_subcmds(pri, e->e, e->hangup.subcmds,
  6519. e->hangup.call);
  6520. break;
  6521. }
  6522. chanpos = pri_find_principle_by_call(pri, e->hangup.call);
  6523. if (chanpos < 0) {
  6524. break;
  6525. }
  6526. sig_pri_lock_private(pri->pvts[chanpos]);
  6527. pri->pvts[chanpos]->call = NULL;
  6528. if (pri->pvts[chanpos]->owner) {
  6529. ast_verb(3, "Span %d: Channel %d/%d got hangup ACK\n", pri->span,
  6530. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset);
  6531. }
  6532. #ifdef SUPPORT_USERUSER
  6533. if (!ast_strlen_zero(e->hangup.useruserinfo)) {
  6534. struct ast_channel *owner;
  6535. sig_pri_lock_owner(pri, chanpos);
  6536. owner = pri->pvts[chanpos]->owner;
  6537. if (owner) {
  6538. pbx_builtin_setvar_helper(owner, "USERUSERINFO",
  6539. e->hangup.useruserinfo);
  6540. ast_channel_unlock(owner);
  6541. }
  6542. }
  6543. #endif
  6544. sig_pri_unlock_private(pri->pvts[chanpos]);
  6545. sig_pri_span_devstate_changed(pri);
  6546. break;
  6547. case PRI_EVENT_CONFIG_ERR:
  6548. ast_log(LOG_WARNING, "PRI Error on span %d: %s\n", pri->span, e->err.err);
  6549. break;
  6550. case PRI_EVENT_RESTART_ACK:
  6551. chanpos = pri_find_principle(pri, e->restartack.channel, NULL);
  6552. if (chanpos < 0) {
  6553. /* Sometime switches (e.g. I421 / British Telecom) don't give us the
  6554. channel number, so we have to figure it out... This must be why
  6555. everybody resets exactly a channel at a time. */
  6556. for (x = 0; x < pri->numchans; x++) {
  6557. if (pri->pvts[x]
  6558. && pri->pvts[x]->resetting != SIG_PRI_RESET_IDLE) {
  6559. chanpos = x;
  6560. sig_pri_lock_private(pri->pvts[chanpos]);
  6561. ast_debug(1,
  6562. "Span %d: Assuming restart ack is for channel %d/%d\n",
  6563. pri->span, pri->pvts[chanpos]->logicalspan,
  6564. pri->pvts[chanpos]->prioffset);
  6565. if (pri->pvts[chanpos]->owner) {
  6566. ast_log(LOG_WARNING,
  6567. "Span %d: Got restart ack on channel %d/%d with owner\n",
  6568. pri->span, pri->pvts[chanpos]->logicalspan,
  6569. pri->pvts[chanpos]->prioffset);
  6570. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  6571. }
  6572. pri->pvts[chanpos]->resetting = SIG_PRI_RESET_IDLE;
  6573. ast_verb(3,
  6574. "Span %d: Channel %d/%d successfully restarted\n",
  6575. pri->span, pri->pvts[chanpos]->logicalspan,
  6576. pri->pvts[chanpos]->prioffset);
  6577. sig_pri_unlock_private(pri->pvts[chanpos]);
  6578. if (pri->resetting)
  6579. pri_check_restart(pri);
  6580. break;
  6581. }
  6582. }
  6583. if (chanpos < 0) {
  6584. ast_log(LOG_WARNING,
  6585. "Span %d: Restart ACK on strange channel %d/%d\n",
  6586. pri->span, PRI_SPAN(e->restartack.channel),
  6587. PRI_CHANNEL(e->restartack.channel));
  6588. }
  6589. } else {
  6590. sig_pri_lock_private(pri->pvts[chanpos]);
  6591. if (pri->pvts[chanpos]->resetting == SIG_PRI_RESET_IDLE) {
  6592. /* The channel is not in the resetting state. */
  6593. ast_debug(1,
  6594. "Span %d: Unexpected or late restart ack on channel %d/%d (Ignoring)\n",
  6595. pri->span, pri->pvts[chanpos]->logicalspan,
  6596. pri->pvts[chanpos]->prioffset);
  6597. sig_pri_unlock_private(pri->pvts[chanpos]);
  6598. break;
  6599. }
  6600. if (pri->pvts[chanpos]->owner) {
  6601. ast_log(LOG_WARNING,
  6602. "Span %d: Got restart ack on channel %d/%d with owner\n",
  6603. pri->span, pri->pvts[chanpos]->logicalspan,
  6604. pri->pvts[chanpos]->prioffset);
  6605. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  6606. }
  6607. pri->pvts[chanpos]->resetting = SIG_PRI_RESET_IDLE;
  6608. ast_verb(3,
  6609. "Span %d: Channel %d/%d successfully restarted\n",
  6610. pri->span, pri->pvts[chanpos]->logicalspan,
  6611. pri->pvts[chanpos]->prioffset);
  6612. sig_pri_unlock_private(pri->pvts[chanpos]);
  6613. if (pri->resetting)
  6614. pri_check_restart(pri);
  6615. }
  6616. break;
  6617. case PRI_EVENT_SETUP_ACK:
  6618. if (sig_pri_is_cis_call(e->setup_ack.channel)) {
  6619. sig_pri_handle_cis_subcmds(pri, e->e, e->setup_ack.subcmds,
  6620. e->setup_ack.call);
  6621. break;
  6622. }
  6623. chanpos = pri_find_fixup_principle(pri, e->setup_ack.channel,
  6624. e->setup_ack.call);
  6625. if (chanpos < 0) {
  6626. break;
  6627. }
  6628. sig_pri_lock_private(pri->pvts[chanpos]);
  6629. sig_pri_handle_subcmds(pri, chanpos, e->e, e->setup_ack.subcmds,
  6630. e->setup_ack.call);
  6631. if (pri->pvts[chanpos]->call_level < SIG_PRI_CALL_LEVEL_OVERLAP) {
  6632. pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_OVERLAP;
  6633. }
  6634. /* Send any queued digits */
  6635. len = strlen(pri->pvts[chanpos]->dialdest);
  6636. for (x = 0; x < len; ++x) {
  6637. ast_debug(1, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
  6638. pri_information(pri->pri, pri->pvts[chanpos]->call,
  6639. pri->pvts[chanpos]->dialdest[x]);
  6640. }
  6641. if (!pri->pvts[chanpos]->progress
  6642. && (pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING)
  6643. && !pri->pvts[chanpos]->digital
  6644. && !pri->pvts[chanpos]->no_b_channel) {
  6645. /*
  6646. * Call has a channel.
  6647. * Indicate for overlap dialing that dialtone may be present.
  6648. */
  6649. pri_queue_control(pri, chanpos, AST_CONTROL_PROGRESS);
  6650. pri->pvts[chanpos]->progress = 1;/* Claim to have seen inband-information */
  6651. sig_pri_set_dialing(pri->pvts[chanpos], 0);
  6652. sig_pri_open_media(pri->pvts[chanpos]);
  6653. }
  6654. sig_pri_unlock_private(pri->pvts[chanpos]);
  6655. break;
  6656. case PRI_EVENT_NOTIFY:
  6657. if (sig_pri_is_cis_call(e->notify.channel)) {
  6658. #if defined(HAVE_PRI_CALL_HOLD)
  6659. sig_pri_handle_cis_subcmds(pri, e->e, e->notify.subcmds,
  6660. e->notify.call);
  6661. #else
  6662. sig_pri_handle_cis_subcmds(pri, e->e, e->notify.subcmds, NULL);
  6663. #endif /* !defined(HAVE_PRI_CALL_HOLD) */
  6664. break;
  6665. }
  6666. #if defined(HAVE_PRI_CALL_HOLD)
  6667. chanpos = pri_find_principle_by_call(pri, e->notify.call);
  6668. if (chanpos < 0) {
  6669. ast_log(LOG_WARNING, "Span %d: Received NOTIFY for unknown call.\n",
  6670. pri->span);
  6671. break;
  6672. }
  6673. #else
  6674. /*
  6675. * This version of libpri does not supply a call pointer for
  6676. * this message. We are just going to have to trust that the
  6677. * correct principle is found.
  6678. */
  6679. chanpos = pri_find_principle(pri, e->notify.channel, NULL);
  6680. if (chanpos < 0) {
  6681. ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
  6682. PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
  6683. break;
  6684. }
  6685. #endif /* !defined(HAVE_PRI_CALL_HOLD) */
  6686. sig_pri_lock_private(pri->pvts[chanpos]);
  6687. #if defined(HAVE_PRI_CALL_HOLD)
  6688. sig_pri_handle_subcmds(pri, chanpos, e->e, e->notify.subcmds,
  6689. e->notify.call);
  6690. #else
  6691. sig_pri_handle_subcmds(pri, chanpos, e->e, e->notify.subcmds, NULL);
  6692. #endif /* !defined(HAVE_PRI_CALL_HOLD) */
  6693. switch (e->notify.info) {
  6694. case PRI_NOTIFY_REMOTE_HOLD:
  6695. if (!pri->discardremoteholdretrieval) {
  6696. pri_queue_control(pri, chanpos, AST_CONTROL_HOLD);
  6697. }
  6698. break;
  6699. case PRI_NOTIFY_REMOTE_RETRIEVAL:
  6700. if (!pri->discardremoteholdretrieval) {
  6701. pri_queue_control(pri, chanpos, AST_CONTROL_UNHOLD);
  6702. }
  6703. break;
  6704. }
  6705. sig_pri_unlock_private(pri->pvts[chanpos]);
  6706. break;
  6707. #if defined(HAVE_PRI_CALL_HOLD)
  6708. case PRI_EVENT_HOLD:
  6709. /* We should not be getting any CIS calls with this message type. */
  6710. if (sig_pri_handle_hold(pri, e)) {
  6711. pri_hold_rej(pri->pri, e->hold.call,
  6712. PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED);
  6713. } else {
  6714. pri_hold_ack(pri->pri, e->hold.call);
  6715. }
  6716. break;
  6717. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  6718. #if defined(HAVE_PRI_CALL_HOLD)
  6719. case PRI_EVENT_HOLD_ACK:
  6720. /* We should not be getting any CIS calls with this message type. */
  6721. sig_pri_handle_hold_ack(pri, e);
  6722. break;
  6723. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  6724. #if defined(HAVE_PRI_CALL_HOLD)
  6725. case PRI_EVENT_HOLD_REJ:
  6726. /* We should not be getting any CIS calls with this message type. */
  6727. sig_pri_handle_hold_rej(pri, e);
  6728. break;
  6729. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  6730. #if defined(HAVE_PRI_CALL_HOLD)
  6731. case PRI_EVENT_RETRIEVE:
  6732. /* We should not be getting any CIS calls with this message type. */
  6733. sig_pri_handle_retrieve(pri, e);
  6734. break;
  6735. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  6736. #if defined(HAVE_PRI_CALL_HOLD)
  6737. case PRI_EVENT_RETRIEVE_ACK:
  6738. /* We should not be getting any CIS calls with this message type. */
  6739. sig_pri_handle_retrieve_ack(pri, e);
  6740. break;
  6741. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  6742. #if defined(HAVE_PRI_CALL_HOLD)
  6743. case PRI_EVENT_RETRIEVE_REJ:
  6744. /* We should not be getting any CIS calls with this message type. */
  6745. sig_pri_handle_retrieve_rej(pri, e);
  6746. break;
  6747. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  6748. default:
  6749. ast_debug(1, "Span: %d Unhandled event: %s(%d)\n",
  6750. pri->span, pri_event2str(e->e), e->e);
  6751. break;
  6752. }
  6753. }
  6754. ast_mutex_unlock(&pri->lock);
  6755. }
  6756. /* Never reached */
  6757. return NULL;
  6758. }
  6759. /*!
  6760. * \brief Output AMI show spans response events for the given PRI span.
  6761. * \since 10.0
  6762. *
  6763. * \param show_cmd AMI command name
  6764. * \param s AMI session to output span information.
  6765. * \param pri PRI span control structure.
  6766. * \param dchannels Array of D channel channel numbers.
  6767. * \param action_id Action ID line to use.
  6768. *
  6769. * \return Number of D channels on this span.
  6770. */
  6771. int sig_pri_ami_show_spans(struct mansession *s, const char *show_cmd, struct sig_pri_span *pri, const int *dchannels, const char *action_id)
  6772. {
  6773. int count;
  6774. int x;
  6775. count = 0;
  6776. for (x = 0; x < ARRAY_LEN(pri->dchans); ++x) {
  6777. if (pri->dchans[x]) {
  6778. ++count;
  6779. astman_append(s,
  6780. "Event: %s\r\n"
  6781. "Span: %d\r\n"
  6782. "DChannel: %d\r\n"
  6783. "Order: %s\r\n"
  6784. "Active: %s\r\n"
  6785. "Alarm: %s\r\n"
  6786. "Up: %s\r\n"
  6787. "%s"
  6788. "\r\n",
  6789. show_cmd,
  6790. pri->span,
  6791. dchannels[x],
  6792. pri_order(x),
  6793. (pri->dchans[x] == pri->pri) ? "Yes" : "No",
  6794. (pri->dchanavail[x] & DCHAN_NOTINALARM) ? "No" : "Yes",
  6795. (pri->dchanavail[x] & DCHAN_UP) ? "Yes" : "No",
  6796. action_id
  6797. );
  6798. }
  6799. }
  6800. return count;
  6801. }
  6802. void sig_pri_init_pri(struct sig_pri_span *pri)
  6803. {
  6804. int i;
  6805. memset(pri, 0, sizeof(*pri));
  6806. ast_mutex_init(&pri->lock);
  6807. pri->master = AST_PTHREADT_NULL;
  6808. for (i = 0; i < SIG_PRI_NUM_DCHANS; i++)
  6809. pri->fds[i] = -1;
  6810. }
  6811. int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast)
  6812. {
  6813. ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
  6814. if (!ast->tech_pvt) {
  6815. ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
  6816. return 0;
  6817. }
  6818. sig_pri_set_outgoing(p, 0);
  6819. sig_pri_set_digital(p, 0); /* push up to parent for EC*/
  6820. #if defined(HAVE_PRI_CALL_WAITING)
  6821. if (p->is_call_waiting) {
  6822. p->is_call_waiting = 0;
  6823. ast_atomic_fetchadd_int(&p->pri->num_call_waiting_calls, -1);
  6824. }
  6825. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  6826. p->call_level = SIG_PRI_CALL_LEVEL_IDLE;
  6827. p->progress = 0;
  6828. p->cid_num[0] = '\0';
  6829. p->cid_subaddr[0] = '\0';
  6830. p->cid_name[0] = '\0';
  6831. p->user_tag[0] = '\0';
  6832. p->exten[0] = '\0';
  6833. sig_pri_set_dialing(p, 0);
  6834. /* Make sure we really have a call */
  6835. pri_grab(p, p->pri);
  6836. sig_pri_moh_fsm_event(ast, p, SIG_PRI_MOH_EVENT_RESET);
  6837. if (p->call) {
  6838. #if defined(SUPPORT_USERUSER)
  6839. const char *useruser = pbx_builtin_getvar_helper(ast, "USERUSERINFO");
  6840. if (!ast_strlen_zero(useruser)) {
  6841. pri_call_set_useruser(p->call, useruser);
  6842. }
  6843. #endif /* defined(SUPPORT_USERUSER) */
  6844. #if defined(HAVE_PRI_AOC_EVENTS)
  6845. if (p->holding_aoce) {
  6846. pri_aoc_e_send(p->pri->pri, p->call, &p->aoc_e);
  6847. }
  6848. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  6849. if (p->alreadyhungup) {
  6850. ast_debug(1, "Already hungup... Calling hangup once, and clearing call\n");
  6851. pri_hangup(p->pri->pri, p->call, -1);
  6852. p->call = NULL;
  6853. } else {
  6854. const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
  6855. int icause = ast->hangupcause ? ast->hangupcause : -1;
  6856. p->alreadyhungup = 1;
  6857. if (!ast_strlen_zero(cause)) {
  6858. if (atoi(cause)) {
  6859. icause = atoi(cause);
  6860. }
  6861. }
  6862. ast_debug(1,
  6863. "Not yet hungup... Calling hangup with cause %d, and clearing call\n",
  6864. icause);
  6865. pri_hangup(p->pri->pri, p->call, icause);
  6866. }
  6867. }
  6868. #if defined(HAVE_PRI_AOC_EVENTS)
  6869. p->aoc_s_request_invoke_id_valid = 0;
  6870. p->holding_aoce = 0;
  6871. p->waiting_for_aoce = 0;
  6872. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  6873. p->allocated = 0;
  6874. p->owner = NULL;
  6875. sig_pri_span_devstate_changed(p->pri);
  6876. pri_rel(p->pri);
  6877. return 0;
  6878. }
  6879. /*!
  6880. * \brief Extract the called number and subaddress from the dial string.
  6881. * \since 1.8
  6882. *
  6883. * \param p sig_pri channel structure.
  6884. * \param rdest Dial string buffer to extract called number and subaddress.
  6885. * \param called Buffer to fill with extracted <number>[:<subaddress>]
  6886. * \param called_buff_size Size of buffer to fill.
  6887. *
  6888. * \note Parsing must remain in sync with sig_pri_call().
  6889. *
  6890. * \return Nothing
  6891. */
  6892. void sig_pri_extract_called_num_subaddr(struct sig_pri_chan *p, const char *rdest, char *called, size_t called_buff_size)
  6893. {
  6894. char *dial;
  6895. char *number;
  6896. char *subaddr;
  6897. AST_DECLARE_APP_ARGS(args,
  6898. AST_APP_ARG(group); /* channel/group token */
  6899. AST_APP_ARG(ext); /* extension token */
  6900. //AST_APP_ARG(opts); /* options token */
  6901. AST_APP_ARG(other); /* Any remining unused arguments */
  6902. );
  6903. /* Get private copy of dial string and break it up. */
  6904. dial = ast_strdupa(rdest);
  6905. AST_NONSTANDARD_APP_ARGS(args, dial, '/');
  6906. number = args.ext;
  6907. if (!number) {
  6908. number = "";
  6909. }
  6910. /* Find and extract dialed_subaddress */
  6911. subaddr = strchr(number, ':');
  6912. if (subaddr) {
  6913. *subaddr++ = '\0';
  6914. /* Skip subaddress type prefix. */
  6915. switch (*subaddr) {
  6916. case 'U':
  6917. case 'u':
  6918. case 'N':
  6919. case 'n':
  6920. ++subaddr;
  6921. break;
  6922. default:
  6923. break;
  6924. }
  6925. }
  6926. /* Skip type-of-number/dial-plan prefix characters. */
  6927. if (strlen(number) < p->stripmsd) {
  6928. number = "";
  6929. } else {
  6930. char *deferred;
  6931. number += p->stripmsd;
  6932. deferred = strchr(number, 'w');
  6933. if (deferred) {
  6934. /* Remove any 'w' deferred digits. */
  6935. *deferred = '\0';
  6936. }
  6937. while (isalpha(*number)) {
  6938. ++number;
  6939. }
  6940. }
  6941. /* Fill buffer with extracted number and subaddress. */
  6942. if (ast_strlen_zero(subaddr)) {
  6943. /* Put in called number only since there is no subaddress. */
  6944. snprintf(called, called_buff_size, "%s", number);
  6945. } else {
  6946. /* Put in called number and subaddress. */
  6947. snprintf(called, called_buff_size, "%s:%s", number, subaddr);
  6948. }
  6949. }
  6950. enum SIG_PRI_CALL_OPT_FLAGS {
  6951. OPT_KEYPAD = (1 << 0),
  6952. OPT_REVERSE_CHARGE = (1 << 1), /* Collect call */
  6953. OPT_AOC_REQUEST = (1 << 2), /* AOC Request */
  6954. };
  6955. enum SIG_PRI_CALL_OPT_ARGS {
  6956. OPT_ARG_KEYPAD = 0,
  6957. OPT_ARG_AOC_REQUEST,
  6958. /* note: this entry _MUST_ be the last one in the enum */
  6959. OPT_ARG_ARRAY_SIZE,
  6960. };
  6961. AST_APP_OPTIONS(sig_pri_call_opts, BEGIN_OPTIONS
  6962. AST_APP_OPTION_ARG('K', OPT_KEYPAD, OPT_ARG_KEYPAD),
  6963. AST_APP_OPTION('R', OPT_REVERSE_CHARGE),
  6964. AST_APP_OPTION_ARG('A', OPT_AOC_REQUEST, OPT_ARG_AOC_REQUEST),
  6965. END_OPTIONS);
  6966. /*! \note Parsing must remain in sync with sig_pri_extract_called_num_subaddr(). */
  6967. int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, int timeout, int layer1)
  6968. {
  6969. char dest[256]; /* must be same length as p->dialdest */
  6970. struct ast_party_subaddress dialed_subaddress; /* Called subaddress */
  6971. struct pri_sr *sr;
  6972. char *c, *l, *n, *s;
  6973. #ifdef SUPPORT_USERUSER
  6974. const char *useruser;
  6975. #endif
  6976. int core_id;
  6977. int pridialplan;
  6978. int dp_strip;
  6979. int prilocaldialplan;
  6980. int ldp_strip;
  6981. int exclusive;
  6982. #if defined(HAVE_PRI_SETUP_KEYPAD)
  6983. const char *keypad;
  6984. #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
  6985. AST_DECLARE_APP_ARGS(args,
  6986. AST_APP_ARG(group); /* channel/group token */
  6987. AST_APP_ARG(ext); /* extension token */
  6988. AST_APP_ARG(opts); /* options token */
  6989. AST_APP_ARG(other); /* Any remining unused arguments */
  6990. );
  6991. struct ast_flags opts;
  6992. char *opt_args[OPT_ARG_ARRAY_SIZE];
  6993. ast_debug(1, "CALLER NAME: %s NUM: %s\n",
  6994. S_COR(ast->connected.id.name.valid, ast->connected.id.name.str, ""),
  6995. S_COR(ast->connected.id.number.valid, ast->connected.id.number.str, ""));
  6996. if (!p->pri) {
  6997. ast_log(LOG_ERROR, "Could not find pri on channel %d\n", p->channel);
  6998. return -1;
  6999. }
  7000. if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
  7001. ast_log(LOG_WARNING, "sig_pri_call called on %s, neither down nor reserved\n", ast->name);
  7002. return -1;
  7003. }
  7004. p->dialdest[0] = '\0';
  7005. sig_pri_set_outgoing(p, 1);
  7006. ast_copy_string(dest, rdest, sizeof(dest));
  7007. AST_NONSTANDARD_APP_ARGS(args, dest, '/');
  7008. if (ast_app_parse_options(sig_pri_call_opts, &opts, opt_args, args.opts)) {
  7009. /* General invalid option syntax. */
  7010. return -1;
  7011. }
  7012. c = args.ext;
  7013. if (!c) {
  7014. c = "";
  7015. }
  7016. /* setup dialed_subaddress if found */
  7017. ast_party_subaddress_init(&dialed_subaddress);
  7018. s = strchr(c, ':');
  7019. if (s) {
  7020. *s = '\0';
  7021. s++;
  7022. /* prefix */
  7023. /* 'n' = NSAP */
  7024. /* 'u' = User Specified */
  7025. /* Default = NSAP */
  7026. switch (*s) {
  7027. case 'U':
  7028. case 'u':
  7029. s++;
  7030. dialed_subaddress.type = 2;
  7031. break;
  7032. case 'N':
  7033. case 'n':
  7034. s++;
  7035. /* default already covered with ast_party_subaddress_init */
  7036. break;
  7037. }
  7038. dialed_subaddress.str = s;
  7039. dialed_subaddress.valid = 1;
  7040. }
  7041. l = NULL;
  7042. n = NULL;
  7043. if (!p->hidecallerid) {
  7044. if (ast->connected.id.number.valid) {
  7045. /* If we get to the end of this loop without breaking, there's no
  7046. * calleridnum. This is done instead of testing for "unknown" or
  7047. * the thousands of other ways that the calleridnum could be
  7048. * invalid. */
  7049. for (l = ast->connected.id.number.str; l && *l; l++) {
  7050. if (strchr("0123456789", *l)) {
  7051. l = ast->connected.id.number.str;
  7052. break;
  7053. }
  7054. }
  7055. } else {
  7056. l = NULL;
  7057. }
  7058. if (!p->hidecalleridname) {
  7059. n = ast->connected.id.name.valid ? ast->connected.id.name.str : NULL;
  7060. }
  7061. }
  7062. if (strlen(c) < p->stripmsd) {
  7063. ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
  7064. return -1;
  7065. }
  7066. /* Extract any 'w' deferred digits. */
  7067. s = strchr(c + p->stripmsd, 'w');
  7068. if (s) {
  7069. *s++ = '\0';
  7070. ast_copy_string(p->deferred_digits, s, sizeof(p->deferred_digits));
  7071. /*
  7072. * Since we have a 'w', this means that there will not be any
  7073. * more normal dialed digits. Therefore, the sending complete
  7074. * ie needs to be sent with any normal digits.
  7075. */
  7076. } else {
  7077. p->deferred_digits[0] = '\0';
  7078. }
  7079. pri_grab(p, p->pri);
  7080. if (!(p->call = pri_new_call(p->pri->pri))) {
  7081. ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
  7082. pri_rel(p->pri);
  7083. return -1;
  7084. }
  7085. if (!(sr = pri_sr_new())) {
  7086. ast_log(LOG_WARNING, "Failed to allocate setup request on channel %d\n",
  7087. p->channel);
  7088. pri_destroycall(p->pri->pri, p->call);
  7089. p->call = NULL;
  7090. pri_rel(p->pri);
  7091. return -1;
  7092. }
  7093. sig_pri_set_digital(p, IS_DIGITAL(ast->transfercapability)); /* push up to parent for EC */
  7094. #if defined(HAVE_PRI_CALL_WAITING)
  7095. if (p->is_call_waiting) {
  7096. /*
  7097. * Indicate that this is a call waiting call.
  7098. * i.e., Normal call but with no B channel.
  7099. */
  7100. pri_sr_set_channel(sr, 0, 0, 1);
  7101. } else
  7102. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  7103. {
  7104. /* Should the picked channel be used exclusively? */
  7105. if (p->priexclusive || p->pri->nodetype == PRI_NETWORK) {
  7106. exclusive = 1;
  7107. } else {
  7108. exclusive = 0;
  7109. }
  7110. pri_sr_set_channel(sr, PVT_TO_CHANNEL(p), exclusive, 1);
  7111. }
  7112. pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
  7113. (p->digital ? -1 : layer1));
  7114. if (p->pri->facilityenable)
  7115. pri_facility_enable(p->pri->pri);
  7116. ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
  7117. dp_strip = 0;
  7118. pridialplan = p->pri->dialplan - 1;
  7119. if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
  7120. if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
  7121. if (pridialplan == -2) {
  7122. dp_strip = strlen(p->pri->internationalprefix);
  7123. }
  7124. pridialplan = PRI_INTERNATIONAL_ISDN;
  7125. } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
  7126. if (pridialplan == -2) {
  7127. dp_strip = strlen(p->pri->nationalprefix);
  7128. }
  7129. pridialplan = PRI_NATIONAL_ISDN;
  7130. } else {
  7131. pridialplan = PRI_LOCAL_ISDN;
  7132. }
  7133. }
  7134. while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
  7135. switch (c[p->stripmsd]) {
  7136. case 'U':
  7137. pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
  7138. break;
  7139. case 'I':
  7140. pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
  7141. break;
  7142. case 'N':
  7143. pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
  7144. break;
  7145. case 'L':
  7146. pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
  7147. break;
  7148. case 'S':
  7149. pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
  7150. break;
  7151. case 'V':
  7152. pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
  7153. break;
  7154. case 'R':
  7155. pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
  7156. break;
  7157. case 'u':
  7158. pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
  7159. break;
  7160. case 'e':
  7161. pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
  7162. break;
  7163. case 'x':
  7164. pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
  7165. break;
  7166. case 'f':
  7167. pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
  7168. break;
  7169. case 'n':
  7170. pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
  7171. break;
  7172. case 'p':
  7173. pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
  7174. break;
  7175. case 'r':
  7176. pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
  7177. break;
  7178. default:
  7179. if (isalpha(c[p->stripmsd])) {
  7180. ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n",
  7181. c[p->stripmsd] > 'Z' ? "NPI" : "TON", c[p->stripmsd]);
  7182. }
  7183. break;
  7184. }
  7185. c++;
  7186. }
  7187. #if defined(HAVE_PRI_SETUP_KEYPAD)
  7188. if (ast_test_flag(&opts, OPT_KEYPAD)
  7189. && !ast_strlen_zero(opt_args[OPT_ARG_KEYPAD])) {
  7190. /* We have a keypad facility digits option with digits. */
  7191. keypad = opt_args[OPT_ARG_KEYPAD];
  7192. pri_sr_set_keypad_digits(sr, keypad);
  7193. } else {
  7194. keypad = NULL;
  7195. }
  7196. if (!keypad || !ast_strlen_zero(c + p->stripmsd + dp_strip))
  7197. #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
  7198. {
  7199. pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
  7200. }
  7201. #if defined(HAVE_PRI_SUBADDR)
  7202. if (dialed_subaddress.valid) {
  7203. struct pri_party_subaddress subaddress;
  7204. memset(&subaddress, 0, sizeof(subaddress));
  7205. sig_pri_party_subaddress_from_ast(&subaddress, &dialed_subaddress);
  7206. pri_sr_set_called_subaddress(sr, &subaddress);
  7207. }
  7208. #endif /* defined(HAVE_PRI_SUBADDR) */
  7209. #if defined(HAVE_PRI_REVERSE_CHARGE)
  7210. if (ast_test_flag(&opts, OPT_REVERSE_CHARGE)) {
  7211. pri_sr_set_reversecharge(sr, PRI_REVERSECHARGE_REQUESTED);
  7212. }
  7213. #endif /* defined(HAVE_PRI_REVERSE_CHARGE) */
  7214. #if defined(HAVE_PRI_AOC_EVENTS)
  7215. if (ast_test_flag(&opts, OPT_AOC_REQUEST)
  7216. && !ast_strlen_zero(opt_args[OPT_ARG_AOC_REQUEST])) {
  7217. if (strchr(opt_args[OPT_ARG_AOC_REQUEST], 's')) {
  7218. pri_sr_set_aoc_charging_request(sr, PRI_AOC_REQUEST_S);
  7219. }
  7220. if (strchr(opt_args[OPT_ARG_AOC_REQUEST], 'd')) {
  7221. pri_sr_set_aoc_charging_request(sr, PRI_AOC_REQUEST_D);
  7222. }
  7223. if (strchr(opt_args[OPT_ARG_AOC_REQUEST], 'e')) {
  7224. pri_sr_set_aoc_charging_request(sr, PRI_AOC_REQUEST_E);
  7225. }
  7226. }
  7227. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  7228. /* Setup the user tag for party id's from this device for this call. */
  7229. if (p->pri->append_msn_to_user_tag) {
  7230. snprintf(p->user_tag, sizeof(p->user_tag), "%s_%s", p->pri->initial_user_tag,
  7231. p->pri->nodetype == PRI_NETWORK
  7232. ? c + p->stripmsd + dp_strip
  7233. : S_COR(ast->connected.id.number.valid,
  7234. ast->connected.id.number.str, ""));
  7235. } else {
  7236. ast_copy_string(p->user_tag, p->pri->initial_user_tag, sizeof(p->user_tag));
  7237. }
  7238. /*
  7239. * Replace the caller id tag from the channel creation
  7240. * with the actual tag value.
  7241. */
  7242. ast_free(ast->caller.id.tag);
  7243. ast->caller.id.tag = ast_strdup(p->user_tag);
  7244. ldp_strip = 0;
  7245. prilocaldialplan = p->pri->localdialplan - 1;
  7246. if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
  7247. if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
  7248. if (prilocaldialplan == -2) {
  7249. ldp_strip = strlen(p->pri->internationalprefix);
  7250. }
  7251. prilocaldialplan = PRI_INTERNATIONAL_ISDN;
  7252. } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
  7253. if (prilocaldialplan == -2) {
  7254. ldp_strip = strlen(p->pri->nationalprefix);
  7255. }
  7256. prilocaldialplan = PRI_NATIONAL_ISDN;
  7257. } else {
  7258. prilocaldialplan = PRI_LOCAL_ISDN;
  7259. }
  7260. } else if (prilocaldialplan == -1) {
  7261. /* Use the numbering plan passed in. */
  7262. prilocaldialplan = ast->connected.id.number.plan;
  7263. }
  7264. if (l != NULL) {
  7265. while (*l > '9' && *l != '*' && *l != '#') {
  7266. switch (*l) {
  7267. case 'U':
  7268. prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
  7269. break;
  7270. case 'I':
  7271. prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
  7272. break;
  7273. case 'N':
  7274. prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
  7275. break;
  7276. case 'L':
  7277. prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
  7278. break;
  7279. case 'S':
  7280. prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
  7281. break;
  7282. case 'V':
  7283. prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
  7284. break;
  7285. case 'R':
  7286. prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
  7287. break;
  7288. case 'u':
  7289. prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
  7290. break;
  7291. case 'e':
  7292. prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
  7293. break;
  7294. case 'x':
  7295. prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
  7296. break;
  7297. case 'f':
  7298. prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
  7299. break;
  7300. case 'n':
  7301. prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
  7302. break;
  7303. case 'p':
  7304. prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
  7305. break;
  7306. case 'r':
  7307. prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
  7308. break;
  7309. default:
  7310. if (isalpha(*l)) {
  7311. ast_log(LOG_WARNING,
  7312. "Unrecognized prilocaldialplan %s modifier: %c\n",
  7313. *l > 'Z' ? "NPI" : "TON", *l);
  7314. }
  7315. break;
  7316. }
  7317. l++;
  7318. }
  7319. }
  7320. pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
  7321. p->use_callingpres ? ast->connected.id.number.presentation : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
  7322. #if defined(HAVE_PRI_SUBADDR)
  7323. if (ast->connected.id.subaddress.valid) {
  7324. struct pri_party_subaddress subaddress;
  7325. memset(&subaddress, 0, sizeof(subaddress));
  7326. sig_pri_party_subaddress_from_ast(&subaddress, &ast->connected.id.subaddress);
  7327. pri_sr_set_caller_subaddress(sr, &subaddress);
  7328. }
  7329. #endif /* defined(HAVE_PRI_SUBADDR) */
  7330. sig_pri_redirecting_update(p, ast);
  7331. #ifdef SUPPORT_USERUSER
  7332. /* User-user info */
  7333. useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
  7334. if (useruser)
  7335. pri_sr_set_useruser(sr, useruser);
  7336. #endif
  7337. #if defined(HAVE_PRI_CCSS)
  7338. if (ast_cc_is_recall(ast, &core_id, sig_pri_cc_type_name)) {
  7339. struct ast_cc_monitor *monitor;
  7340. char device_name[AST_CHANNEL_NAME];
  7341. /* This is a CC recall call. */
  7342. ast_channel_get_device_name(ast, device_name, sizeof(device_name));
  7343. monitor = ast_cc_get_monitor_by_recall_core_id(core_id, device_name);
  7344. if (monitor) {
  7345. struct sig_pri_cc_monitor_instance *instance;
  7346. instance = monitor->private_data;
  7347. /* If this fails then we have monitor instance ambiguity. */
  7348. ast_assert(p->pri == instance->pri);
  7349. if (pri_cc_call(p->pri->pri, instance->cc_id, p->call, sr)) {
  7350. /* The CC recall call failed for some reason. */
  7351. ast_log(LOG_WARNING, "Unable to setup CC recall call to device %s\n",
  7352. device_name);
  7353. ao2_ref(monitor, -1);
  7354. pri_destroycall(p->pri->pri, p->call);
  7355. p->call = NULL;
  7356. pri_rel(p->pri);
  7357. pri_sr_free(sr);
  7358. return -1;
  7359. }
  7360. ao2_ref(monitor, -1);
  7361. } else {
  7362. core_id = -1;
  7363. }
  7364. } else
  7365. #endif /* defined(HAVE_PRI_CCSS) */
  7366. {
  7367. core_id = -1;
  7368. }
  7369. if (core_id == -1 && pri_setup(p->pri->pri, p->call, sr)) {
  7370. ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
  7371. c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
  7372. pri_destroycall(p->pri->pri, p->call);
  7373. p->call = NULL;
  7374. pri_rel(p->pri);
  7375. pri_sr_free(sr);
  7376. return -1;
  7377. }
  7378. p->call_level = SIG_PRI_CALL_LEVEL_SETUP;
  7379. pri_sr_free(sr);
  7380. ast_setstate(ast, AST_STATE_DIALING);
  7381. sig_pri_set_dialing(p, 1);
  7382. pri_rel(p->pri);
  7383. return 0;
  7384. }
  7385. int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
  7386. {
  7387. int res = -1;
  7388. switch (condition) {
  7389. case AST_CONTROL_BUSY:
  7390. if (p->priindication_oob || p->no_b_channel) {
  7391. chan->hangupcause = AST_CAUSE_USER_BUSY;
  7392. chan->_softhangup |= AST_SOFTHANGUP_DEV;
  7393. res = 0;
  7394. break;
  7395. }
  7396. res = sig_pri_play_tone(p, SIG_PRI_TONE_BUSY);
  7397. if (p->call_level < SIG_PRI_CALL_LEVEL_ALERTING && !p->outgoing) {
  7398. chan->hangupcause = AST_CAUSE_USER_BUSY;
  7399. p->progress = 1;/* No need to send plain PROGRESS after this. */
  7400. if (p->pri && p->pri->pri) {
  7401. pri_grab(p, p->pri);
  7402. #ifdef HAVE_PRI_PROG_W_CAUSE
  7403. pri_progress_with_cause(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 1, chan->hangupcause);
  7404. #else
  7405. pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
  7406. #endif
  7407. pri_rel(p->pri);
  7408. }
  7409. }
  7410. break;
  7411. case AST_CONTROL_RINGING:
  7412. if (p->call_level < SIG_PRI_CALL_LEVEL_ALERTING && !p->outgoing) {
  7413. p->call_level = SIG_PRI_CALL_LEVEL_ALERTING;
  7414. if (p->pri && p->pri->pri) {
  7415. pri_grab(p, p->pri);
  7416. pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p),
  7417. p->no_b_channel || p->digital ? 0 : 1);
  7418. pri_rel(p->pri);
  7419. }
  7420. }
  7421. res = sig_pri_play_tone(p, SIG_PRI_TONE_RINGTONE);
  7422. if (chan->_state != AST_STATE_UP) {
  7423. if (chan->_state != AST_STATE_RING)
  7424. ast_setstate(chan, AST_STATE_RINGING);
  7425. }
  7426. break;
  7427. case AST_CONTROL_PROCEEDING:
  7428. ast_debug(1, "Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
  7429. if (p->call_level < SIG_PRI_CALL_LEVEL_PROCEEDING && !p->outgoing) {
  7430. p->call_level = SIG_PRI_CALL_LEVEL_PROCEEDING;
  7431. if (p->pri && p->pri->pri) {
  7432. pri_grab(p, p->pri);
  7433. pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p),
  7434. p->no_b_channel || p->digital ? 0 : 1);
  7435. if (!p->no_b_channel && !p->digital) {
  7436. sig_pri_set_dialing(p, 0);
  7437. }
  7438. pri_rel(p->pri);
  7439. }
  7440. }
  7441. /* don't continue in ast_indicate */
  7442. res = 0;
  7443. break;
  7444. case AST_CONTROL_PROGRESS:
  7445. ast_debug(1, "Received AST_CONTROL_PROGRESS on %s\n",chan->name);
  7446. sig_pri_set_digital(p, 0); /* Digital-only calls isn't allowing any inband progress messages */
  7447. if (!p->progress && p->call_level < SIG_PRI_CALL_LEVEL_ALERTING && !p->outgoing
  7448. && !p->no_b_channel) {
  7449. p->progress = 1;/* No need to send plain PROGRESS again. */
  7450. if (p->pri && p->pri->pri) {
  7451. pri_grab(p, p->pri);
  7452. #ifdef HAVE_PRI_PROG_W_CAUSE
  7453. pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, -1); /* no cause at all */
  7454. #else
  7455. pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
  7456. #endif
  7457. pri_rel(p->pri);
  7458. }
  7459. }
  7460. /* don't continue in ast_indicate */
  7461. res = 0;
  7462. break;
  7463. case AST_CONTROL_INCOMPLETE:
  7464. /* If we are connected or if we support overlap dialing, wait for additional digits */
  7465. if (p->call_level == SIG_PRI_CALL_LEVEL_CONNECT || (p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
  7466. res = 0;
  7467. break;
  7468. }
  7469. /* Otherwise, treat as congestion */
  7470. chan->hangupcause = AST_CAUSE_INVALID_NUMBER_FORMAT;
  7471. /* Falls through */
  7472. case AST_CONTROL_CONGESTION:
  7473. if (p->priindication_oob || p->no_b_channel) {
  7474. /* There are many cause codes that generate an AST_CONTROL_CONGESTION. */
  7475. switch (chan->hangupcause) {
  7476. case AST_CAUSE_USER_BUSY:
  7477. case AST_CAUSE_NORMAL_CLEARING:
  7478. case 0:/* Cause has not been set. */
  7479. /* Supply a more appropriate cause. */
  7480. chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
  7481. break;
  7482. default:
  7483. break;
  7484. }
  7485. chan->_softhangup |= AST_SOFTHANGUP_DEV;
  7486. res = 0;
  7487. break;
  7488. }
  7489. res = sig_pri_play_tone(p, SIG_PRI_TONE_CONGESTION);
  7490. if (p->call_level < SIG_PRI_CALL_LEVEL_ALERTING && !p->outgoing) {
  7491. /* There are many cause codes that generate an AST_CONTROL_CONGESTION. */
  7492. switch (chan->hangupcause) {
  7493. case AST_CAUSE_USER_BUSY:
  7494. case AST_CAUSE_NORMAL_CLEARING:
  7495. case 0:/* Cause has not been set. */
  7496. /* Supply a more appropriate cause. */
  7497. chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
  7498. break;
  7499. default:
  7500. break;
  7501. }
  7502. p->progress = 1;/* No need to send plain PROGRESS after this. */
  7503. if (p->pri && p->pri->pri) {
  7504. pri_grab(p, p->pri);
  7505. #ifdef HAVE_PRI_PROG_W_CAUSE
  7506. pri_progress_with_cause(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 1, chan->hangupcause);
  7507. #else
  7508. pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
  7509. #endif
  7510. pri_rel(p->pri);
  7511. }
  7512. }
  7513. break;
  7514. case AST_CONTROL_HOLD:
  7515. ast_copy_string(p->moh_suggested, S_OR(data, ""), sizeof(p->moh_suggested));
  7516. if (p->pri) {
  7517. pri_grab(p, p->pri);
  7518. sig_pri_moh_fsm_event(chan, p, SIG_PRI_MOH_EVENT_HOLD);
  7519. pri_rel(p->pri);
  7520. } else {
  7521. /* Something is wrong here. A PRI channel without the pri pointer? */
  7522. ast_moh_start(chan, data, p->mohinterpret);
  7523. }
  7524. break;
  7525. case AST_CONTROL_UNHOLD:
  7526. if (p->pri) {
  7527. pri_grab(p, p->pri);
  7528. sig_pri_moh_fsm_event(chan, p, SIG_PRI_MOH_EVENT_UNHOLD);
  7529. pri_rel(p->pri);
  7530. } else {
  7531. /* Something is wrong here. A PRI channel without the pri pointer? */
  7532. ast_moh_stop(chan);
  7533. }
  7534. break;
  7535. case AST_CONTROL_SRCUPDATE:
  7536. res = 0;
  7537. break;
  7538. case -1:
  7539. res = sig_pri_play_tone(p, -1);
  7540. break;
  7541. case AST_CONTROL_CONNECTED_LINE:
  7542. ast_debug(1, "Received AST_CONTROL_CONNECTED_LINE on %s\n", chan->name);
  7543. if (p->pri) {
  7544. struct pri_party_connected_line connected;
  7545. int dialplan;
  7546. int prefix_strip;
  7547. pri_grab(p, p->pri);
  7548. memset(&connected, 0, sizeof(connected));
  7549. sig_pri_party_id_from_ast(&connected.id, &chan->connected.id);
  7550. /* Determine the connected line numbering plan to actually use. */
  7551. switch (p->pri->cpndialplan) {
  7552. case -2:/* redundant */
  7553. case -1:/* dynamic */
  7554. /* compute dynamically */
  7555. prefix_strip = 0;
  7556. if (!strncmp(connected.id.number.str, p->pri->internationalprefix,
  7557. strlen(p->pri->internationalprefix))) {
  7558. prefix_strip = strlen(p->pri->internationalprefix);
  7559. dialplan = PRI_INTERNATIONAL_ISDN;
  7560. } else if (!strncmp(connected.id.number.str, p->pri->nationalprefix,
  7561. strlen(p->pri->nationalprefix))) {
  7562. prefix_strip = strlen(p->pri->nationalprefix);
  7563. dialplan = PRI_NATIONAL_ISDN;
  7564. } else {
  7565. dialplan = PRI_LOCAL_ISDN;
  7566. }
  7567. connected.id.number.plan = dialplan;
  7568. if (prefix_strip && p->pri->cpndialplan != -2) {
  7569. /* Strip the prefix from the connected line number. */
  7570. memmove(connected.id.number.str,
  7571. connected.id.number.str + prefix_strip,
  7572. strlen(connected.id.number.str + prefix_strip) + 1);
  7573. }
  7574. break;
  7575. case 0:/* from_channel */
  7576. /* Use the numbering plan passed in. */
  7577. break;
  7578. default:
  7579. connected.id.number.plan = p->pri->cpndialplan - 1;
  7580. break;
  7581. }
  7582. pri_connected_line_update(p->pri->pri, p->call, &connected);
  7583. pri_rel(p->pri);
  7584. }
  7585. break;
  7586. case AST_CONTROL_REDIRECTING:
  7587. ast_debug(1, "Received AST_CONTROL_REDIRECTING on %s\n", chan->name);
  7588. if (p->pri) {
  7589. pri_grab(p, p->pri);
  7590. sig_pri_redirecting_update(p, chan);
  7591. pri_rel(p->pri);
  7592. }
  7593. break;
  7594. case AST_CONTROL_AOC:
  7595. #if defined(HAVE_PRI_AOC_EVENTS)
  7596. {
  7597. struct ast_aoc_decoded *decoded
  7598. = ast_aoc_decode((struct ast_aoc_encoded *) data, datalen, chan);
  7599. ast_debug(1, "Received AST_CONTROL_AOC on %s\n", chan->name);
  7600. if (decoded && p->pri) {
  7601. pri_grab(p, p->pri);
  7602. switch (ast_aoc_get_msg_type(decoded)) {
  7603. case AST_AOC_S:
  7604. if (p->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S) {
  7605. sig_pri_aoc_s_from_ast(p, decoded);
  7606. }
  7607. break;
  7608. case AST_AOC_D:
  7609. if (p->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_D) {
  7610. sig_pri_aoc_d_from_ast(p, decoded);
  7611. }
  7612. break;
  7613. case AST_AOC_E:
  7614. if (p->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_E) {
  7615. sig_pri_aoc_e_from_ast(p, decoded);
  7616. }
  7617. /* if hangup was delayed for this AOC-E msg, waiting_for_aoc
  7618. * will be set. A hangup is already occuring via a timeout during
  7619. * this delay. Instead of waiting for that timeout to occur, go ahead
  7620. * and initiate the softhangup since the delay is no longer necessary */
  7621. if (p->waiting_for_aoce) {
  7622. p->waiting_for_aoce = 0;
  7623. ast_debug(1,
  7624. "Received final AOC-E msg, continue with hangup on %s\n",
  7625. chan->name);
  7626. ast_softhangup_nolock(chan, AST_SOFTHANGUP_DEV);
  7627. }
  7628. break;
  7629. case AST_AOC_REQUEST:
  7630. /* We do not pass through AOC requests, So unless this
  7631. * is an AOC termination request it will be ignored */
  7632. if (ast_aoc_get_termination_request(decoded)) {
  7633. pri_hangup(p->pri->pri, p->call, -1);
  7634. }
  7635. break;
  7636. default:
  7637. break;
  7638. }
  7639. pri_rel(p->pri);
  7640. }
  7641. ast_aoc_destroy_decoded(decoded);
  7642. }
  7643. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  7644. break;
  7645. #if defined(HAVE_PRI_MCID)
  7646. case AST_CONTROL_MCID:
  7647. if (p->pri && p->pri->pri && p->pri->mcid_send) {
  7648. pri_grab(p, p->pri);
  7649. pri_mcid_req_send(p->pri->pri, p->call);
  7650. pri_rel(p->pri);
  7651. }
  7652. break;
  7653. #endif /* defined(HAVE_PRI_MCID) */
  7654. }
  7655. return res;
  7656. }
  7657. int sig_pri_answer(struct sig_pri_chan *p, struct ast_channel *ast)
  7658. {
  7659. int res;
  7660. /* Send a pri acknowledge */
  7661. pri_grab(p, p->pri);
  7662. #if defined(HAVE_PRI_AOC_EVENTS)
  7663. if (p->aoc_s_request_invoke_id_valid) {
  7664. /* if AOC-S was requested and the invoke id is still present on answer. That means
  7665. * no AOC-S rate list was provided, so send a NULL response which will indicate that
  7666. * AOC-S is not available */
  7667. pri_aoc_s_request_response_send(p->pri->pri, p->call,
  7668. p->aoc_s_request_invoke_id, NULL);
  7669. p->aoc_s_request_invoke_id_valid = 0;
  7670. }
  7671. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  7672. if (p->call_level < SIG_PRI_CALL_LEVEL_CONNECT) {
  7673. p->call_level = SIG_PRI_CALL_LEVEL_CONNECT;
  7674. }
  7675. sig_pri_set_dialing(p, 0);
  7676. sig_pri_open_media(p);
  7677. res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
  7678. pri_rel(p->pri);
  7679. ast_setstate(ast, AST_STATE_UP);
  7680. return res;
  7681. }
  7682. /*!
  7683. * \internal
  7684. * \brief Simple check if the channel is available to use.
  7685. * \since 1.8
  7686. *
  7687. * \param pvt Private channel control structure.
  7688. *
  7689. * \retval 0 Interface not available.
  7690. * \retval 1 Interface is available.
  7691. */
  7692. static int sig_pri_available_check(struct sig_pri_chan *pvt)
  7693. {
  7694. /*
  7695. * If interface has a B channel and is available for use
  7696. * then the channel is available.
  7697. */
  7698. if (!pvt->no_b_channel && sig_pri_is_chan_available(pvt)) {
  7699. return 1;
  7700. }
  7701. return 0;
  7702. }
  7703. #if defined(HAVE_PRI_CALL_WAITING)
  7704. /*!
  7705. * \internal
  7706. * \brief Get an available call waiting interface.
  7707. * \since 1.8
  7708. *
  7709. * \param pri PRI span control structure.
  7710. *
  7711. * \note Assumes the pri->lock is already obtained.
  7712. *
  7713. * \retval cw Call waiting interface to use.
  7714. * \retval NULL if no call waiting interface available.
  7715. */
  7716. static struct sig_pri_chan *sig_pri_cw_available(struct sig_pri_span *pri)
  7717. {
  7718. struct sig_pri_chan *cw;
  7719. int idx;
  7720. cw = NULL;
  7721. if (pri->num_call_waiting_calls < pri->max_call_waiting_calls) {
  7722. if (!pri->num_call_waiting_calls) {
  7723. /*
  7724. * There are no outstanding call waiting calls. Check to see
  7725. * if the span is in a congested state for the first call
  7726. * waiting call.
  7727. */
  7728. for (idx = 0; idx < pri->numchans; ++idx) {
  7729. if (pri->pvts[idx] && sig_pri_available_check(pri->pvts[idx])) {
  7730. /* There is another channel that is available on this span. */
  7731. return cw;
  7732. }
  7733. }
  7734. }
  7735. idx = pri_find_empty_nobch(pri);
  7736. if (0 <= idx) {
  7737. /* Setup the call waiting interface to use. */
  7738. cw = pri->pvts[idx];
  7739. cw->is_call_waiting = 1;
  7740. sig_pri_init_config(cw, pri);
  7741. ast_atomic_fetchadd_int(&pri->num_call_waiting_calls, 1);
  7742. }
  7743. }
  7744. return cw;
  7745. }
  7746. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  7747. int sig_pri_available(struct sig_pri_chan **pvt, int is_specific_channel)
  7748. {
  7749. struct sig_pri_chan *p = *pvt;
  7750. struct sig_pri_span *pri;
  7751. if (!p->pri) {
  7752. /* Something is wrong here. A PRI channel without the pri pointer? */
  7753. return 0;
  7754. }
  7755. pri = p->pri;
  7756. ast_mutex_lock(&pri->lock);
  7757. if (
  7758. #if defined(HAVE_PRI_CALL_WAITING)
  7759. /*
  7760. * Only do call waiting calls if we have any
  7761. * call waiting call outstanding. We do not
  7762. * want new calls to steal a B channel
  7763. * freed for an earlier call waiting call.
  7764. */
  7765. !pri->num_call_waiting_calls &&
  7766. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  7767. sig_pri_available_check(p)) {
  7768. p->allocated = 1;
  7769. ast_mutex_unlock(&pri->lock);
  7770. return 1;
  7771. }
  7772. #if defined(HAVE_PRI_CALL_WAITING)
  7773. if (!is_specific_channel) {
  7774. struct sig_pri_chan *cw;
  7775. cw = sig_pri_cw_available(pri);
  7776. if (cw) {
  7777. /* We have a call waiting interface to use instead. */
  7778. cw->allocated = 1;
  7779. *pvt = cw;
  7780. ast_mutex_unlock(&pri->lock);
  7781. return 1;
  7782. }
  7783. }
  7784. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  7785. ast_mutex_unlock(&pri->lock);
  7786. return 0;
  7787. }
  7788. /* If return 0, it means this function was able to handle it (pre setup digits). If non zero, the user of this
  7789. * functions should handle it normally (generate inband DTMF) */
  7790. int sig_pri_digit_begin(struct sig_pri_chan *pvt, struct ast_channel *ast, char digit)
  7791. {
  7792. if (ast->_state == AST_STATE_DIALING) {
  7793. if (pvt->call_level < SIG_PRI_CALL_LEVEL_OVERLAP) {
  7794. unsigned int len;
  7795. len = strlen(pvt->dialdest);
  7796. if (len < sizeof(pvt->dialdest) - 1) {
  7797. ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n",
  7798. digit);
  7799. pvt->dialdest[len++] = digit;
  7800. pvt->dialdest[len] = '\0';
  7801. } else {
  7802. ast_log(LOG_WARNING,
  7803. "Span %d: Deferred digit buffer overflow for digit '%c'.\n",
  7804. pvt->pri->span, digit);
  7805. }
  7806. return 0;
  7807. }
  7808. if (pvt->call_level < SIG_PRI_CALL_LEVEL_PROCEEDING) {
  7809. pri_grab(pvt, pvt->pri);
  7810. pri_information(pvt->pri->pri, pvt->call, digit);
  7811. pri_rel(pvt->pri);
  7812. return 0;
  7813. }
  7814. if (pvt->call_level < SIG_PRI_CALL_LEVEL_CONNECT) {
  7815. ast_log(LOG_WARNING,
  7816. "Span %d: Digit '%c' may be ignored by peer. (Call level:%d(%s))\n",
  7817. pvt->pri->span, digit, pvt->call_level,
  7818. sig_pri_call_level2str(pvt->call_level));
  7819. }
  7820. }
  7821. return 1;
  7822. }
  7823. /*!
  7824. * \brief DTMF dial string complete.
  7825. * \since 1.8.11
  7826. *
  7827. * \param pvt sig_pri private channel structure.
  7828. * \param ast Asterisk channel
  7829. *
  7830. * \note Channel and private lock are already held.
  7831. *
  7832. * \return Nothing
  7833. */
  7834. void sig_pri_dial_complete(struct sig_pri_chan *pvt, struct ast_channel *ast)
  7835. {
  7836. /* If we just completed 'w' deferred dialing digits, we need to answer now. */
  7837. if (pvt->call_level == SIG_PRI_CALL_LEVEL_DEFER_DIAL) {
  7838. pvt->call_level = SIG_PRI_CALL_LEVEL_CONNECT;
  7839. sig_pri_open_media(pvt);
  7840. {
  7841. struct ast_frame f = {AST_FRAME_CONTROL, };
  7842. if (pvt->calls->queue_control) {
  7843. pvt->calls->queue_control(pvt->chan_pvt, AST_CONTROL_ANSWER);
  7844. }
  7845. f.subclass.integer = AST_CONTROL_ANSWER;
  7846. ast_queue_frame(ast, &f);
  7847. }
  7848. sig_pri_set_dialing(pvt, 0);
  7849. /* Enable echo cancellation if it's not on already */
  7850. sig_pri_set_echocanceller(pvt, 1);
  7851. }
  7852. }
  7853. #if defined(HAVE_PRI_MWI)
  7854. /*!
  7855. * \internal
  7856. * \brief Send a MWI indication to the given span.
  7857. * \since 1.8
  7858. *
  7859. * \param pri PRI span control structure.
  7860. * \param vm_number Voicemail controlling number (NULL if not present).
  7861. * \param mbox_number Mailbox number
  7862. * \param mbox_context Mailbox context
  7863. * \param num_messages Number of messages waiting.
  7864. *
  7865. * \return Nothing
  7866. */
  7867. static void sig_pri_send_mwi_indication(struct sig_pri_span *pri, const char *vm_number, const char *mbox_number, const char *mbox_context, int num_messages)
  7868. {
  7869. struct pri_party_id voicemail;
  7870. struct pri_party_id mailbox;
  7871. ast_debug(1, "Send MWI indication for %s@%s vm_number:%s num_messages:%d\n",
  7872. mbox_number, mbox_context, S_OR(vm_number, "<not-present>"), num_messages);
  7873. memset(&mailbox, 0, sizeof(mailbox));
  7874. mailbox.number.valid = 1;
  7875. mailbox.number.presentation = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
  7876. mailbox.number.plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_UNKNOWN;
  7877. ast_copy_string(mailbox.number.str, mbox_number, sizeof(mailbox.number.str));
  7878. memset(&voicemail, 0, sizeof(voicemail));
  7879. voicemail.number.valid = 1;
  7880. voicemail.number.presentation = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
  7881. voicemail.number.plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_UNKNOWN;
  7882. if (vm_number) {
  7883. ast_copy_string(voicemail.number.str, vm_number, sizeof(voicemail.number.str));
  7884. }
  7885. ast_mutex_lock(&pri->lock);
  7886. #if defined(HAVE_PRI_MWI_V2)
  7887. pri_mwi_indicate_v2(pri->pri, &mailbox, &voicemail, 1 /* speech */, num_messages,
  7888. NULL, NULL, -1, 0);
  7889. #else /* !defined(HAVE_PRI_MWI_V2) */
  7890. pri_mwi_indicate(pri->pri, &mailbox, 1 /* speech */, num_messages, NULL, NULL, -1, 0);
  7891. #endif /* !defined(HAVE_PRI_MWI_V2) */
  7892. ast_mutex_unlock(&pri->lock);
  7893. }
  7894. #endif /* defined(HAVE_PRI_MWI) */
  7895. #if defined(HAVE_PRI_MWI)
  7896. /*!
  7897. * \internal
  7898. * \brief MWI subscription event callback.
  7899. * \since 1.8
  7900. *
  7901. * \param event the event being passed to the subscriber
  7902. * \param userdata the data provider in the call to ast_event_subscribe()
  7903. *
  7904. * \return Nothing
  7905. */
  7906. static void sig_pri_mwi_event_cb(const struct ast_event *event, void *userdata)
  7907. {
  7908. struct sig_pri_span *pri = userdata;
  7909. const char *mbox_context;
  7910. const char *mbox_number;
  7911. int num_messages;
  7912. int idx;
  7913. mbox_number = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
  7914. if (ast_strlen_zero(mbox_number)) {
  7915. return;
  7916. }
  7917. mbox_context = ast_event_get_ie_str(event, AST_EVENT_IE_CONTEXT);
  7918. if (ast_strlen_zero(mbox_context)) {
  7919. return;
  7920. }
  7921. num_messages = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
  7922. for (idx = 0; idx < ARRAY_LEN(pri->mbox); ++idx) {
  7923. if (!pri->mbox[idx].sub) {
  7924. /* Mailbox slot is empty */
  7925. continue;
  7926. }
  7927. if (!strcmp(pri->mbox[idx].number, mbox_number)
  7928. && !strcmp(pri->mbox[idx].context, mbox_context)) {
  7929. /* Found the mailbox. */
  7930. sig_pri_send_mwi_indication(pri, pri->mbox[idx].vm_number, mbox_number,
  7931. mbox_context, num_messages);
  7932. break;
  7933. }
  7934. }
  7935. }
  7936. #endif /* defined(HAVE_PRI_MWI) */
  7937. #if defined(HAVE_PRI_MWI)
  7938. /*!
  7939. * \internal
  7940. * \brief Send update MWI indications from the event cache.
  7941. * \since 1.8
  7942. *
  7943. * \param pri PRI span control structure.
  7944. *
  7945. * \return Nothing
  7946. */
  7947. static void sig_pri_mwi_cache_update(struct sig_pri_span *pri)
  7948. {
  7949. int idx;
  7950. int num_messages;
  7951. struct ast_event *event;
  7952. for (idx = 0; idx < ARRAY_LEN(pri->mbox); ++idx) {
  7953. if (!pri->mbox[idx].sub) {
  7954. /* Mailbox slot is empty */
  7955. continue;
  7956. }
  7957. event = ast_event_get_cached(AST_EVENT_MWI,
  7958. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, pri->mbox[idx].number,
  7959. AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, pri->mbox[idx].context,
  7960. AST_EVENT_IE_END);
  7961. if (!event) {
  7962. /* No cached event for this mailbox. */
  7963. continue;
  7964. }
  7965. num_messages = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
  7966. sig_pri_send_mwi_indication(pri, pri->mbox[idx].vm_number, pri->mbox[idx].number,
  7967. pri->mbox[idx].context, num_messages);
  7968. ast_event_destroy(event);
  7969. }
  7970. }
  7971. #endif /* defined(HAVE_PRI_MWI) */
  7972. /*!
  7973. * \brief Stop PRI span.
  7974. * \since 1.8
  7975. *
  7976. * \param pri PRI span control structure.
  7977. *
  7978. * \return Nothing
  7979. */
  7980. void sig_pri_stop_pri(struct sig_pri_span *pri)
  7981. {
  7982. #if defined(HAVE_PRI_MWI)
  7983. int idx;
  7984. #endif /* defined(HAVE_PRI_MWI) */
  7985. #if defined(HAVE_PRI_MWI)
  7986. for (idx = 0; idx < ARRAY_LEN(pri->mbox); ++idx) {
  7987. if (pri->mbox[idx].sub) {
  7988. pri->mbox[idx].sub = ast_event_unsubscribe(pri->mbox[idx].sub);
  7989. }
  7990. }
  7991. #endif /* defined(HAVE_PRI_MWI) */
  7992. }
  7993. /*!
  7994. * \internal
  7995. * \brief qsort comparison function.
  7996. * \since 1.8
  7997. *
  7998. * \param left Ptr to sig_pri_chan ptr to compare.
  7999. * \param right Ptr to sig_pri_chan ptr to compare.
  8000. *
  8001. * \retval <0 if left < right.
  8002. * \retval =0 if left == right.
  8003. * \retval >0 if left > right.
  8004. */
  8005. static int sig_pri_cmp_pri_chans(const void *left, const void *right)
  8006. {
  8007. const struct sig_pri_chan *pvt_left;
  8008. const struct sig_pri_chan *pvt_right;
  8009. pvt_left = *(struct sig_pri_chan **) left;
  8010. pvt_right = *(struct sig_pri_chan **) right;
  8011. if (!pvt_left) {
  8012. if (!pvt_right) {
  8013. return 0;
  8014. }
  8015. return 1;
  8016. }
  8017. if (!pvt_right) {
  8018. return -1;
  8019. }
  8020. return pvt_left->channel - pvt_right->channel;
  8021. }
  8022. /*!
  8023. * \internal
  8024. * \brief Sort the PRI B channel private pointer array.
  8025. * \since 1.8
  8026. *
  8027. * \param pri PRI span control structure.
  8028. *
  8029. * \details
  8030. * Since the chan_dahdi.conf file can declare channels in any order, we need to sort
  8031. * the private channel pointer array.
  8032. *
  8033. * \return Nothing
  8034. */
  8035. static void sig_pri_sort_pri_chans(struct sig_pri_span *pri)
  8036. {
  8037. qsort(&pri->pvts, pri->numchans, sizeof(pri->pvts[0]), sig_pri_cmp_pri_chans);
  8038. }
  8039. int sig_pri_start_pri(struct sig_pri_span *pri)
  8040. {
  8041. int x;
  8042. int i;
  8043. #if defined(HAVE_PRI_MWI)
  8044. char *saveptr;
  8045. char *prev_vm_number;
  8046. struct ast_str *mwi_description = ast_str_alloca(64);
  8047. #endif /* defined(HAVE_PRI_MWI) */
  8048. #if defined(HAVE_PRI_MWI)
  8049. /* Prepare the mbox[] for use. */
  8050. for (i = 0; i < ARRAY_LEN(pri->mbox); ++i) {
  8051. if (pri->mbox[i].sub) {
  8052. pri->mbox[i].sub = ast_event_unsubscribe(pri->mbox[i].sub);
  8053. }
  8054. }
  8055. #endif /* defined(HAVE_PRI_MWI) */
  8056. ast_mutex_init(&pri->lock);
  8057. sig_pri_sort_pri_chans(pri);
  8058. #if defined(HAVE_PRI_MWI)
  8059. /*
  8060. * Split the mwi_vm_numbers configuration string into the mbox[].vm_number:
  8061. * vm_number{,vm_number}
  8062. */
  8063. prev_vm_number = NULL;
  8064. saveptr = pri->mwi_vm_numbers;
  8065. for (i = 0; i < ARRAY_LEN(pri->mbox); ++i) {
  8066. char *vm_number;
  8067. vm_number = strsep(&saveptr, ",");
  8068. if (vm_number) {
  8069. vm_number = ast_strip(vm_number);
  8070. }
  8071. if (ast_strlen_zero(vm_number)) {
  8072. /* There was no number so reuse the previous number. */
  8073. vm_number = prev_vm_number;
  8074. } else {
  8075. /* We have a new number. */
  8076. prev_vm_number = vm_number;
  8077. }
  8078. pri->mbox[i].vm_number = vm_number;
  8079. }
  8080. /*
  8081. * Split the mwi_mailboxes configuration string into the mbox[]:
  8082. * mailbox_number[@context]{,mailbox_number[@context]}
  8083. */
  8084. saveptr = pri->mwi_mailboxes;
  8085. for (i = 0; i < ARRAY_LEN(pri->mbox); ++i) {
  8086. char *mbox_number;
  8087. char *mbox_context;
  8088. mbox_number = strsep(&saveptr, ",");
  8089. if (!mbox_number) {
  8090. /* No more defined mailboxes. */
  8091. break;
  8092. }
  8093. /* Split the mailbox_number and context */
  8094. mbox_context = strchr(mbox_number, '@');
  8095. if (mbox_context) {
  8096. *mbox_context++ = '\0';
  8097. mbox_context = ast_strip(mbox_context);
  8098. }
  8099. mbox_number = ast_strip(mbox_number);
  8100. if (ast_strlen_zero(mbox_number)) {
  8101. /* There is no mailbox number. Skip it. */
  8102. continue;
  8103. }
  8104. if (ast_strlen_zero(mbox_context)) {
  8105. /* There was no context so use the default. */
  8106. mbox_context = "default";
  8107. }
  8108. /* Fill the mbox[] element. */
  8109. pri->mbox[i].number = mbox_number;
  8110. pri->mbox[i].context = mbox_context;
  8111. ast_str_set(&mwi_description, -1, "%s span %d[%d] MWI mailbox %s@%s",
  8112. sig_pri_cc_type_name, pri->span, i, mbox_number, mbox_context);
  8113. pri->mbox[i].sub = ast_event_subscribe(AST_EVENT_MWI, sig_pri_mwi_event_cb,
  8114. ast_str_buffer(mwi_description), pri,
  8115. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mbox_number,
  8116. AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, mbox_context,
  8117. AST_EVENT_IE_END);
  8118. if (!pri->mbox[i].sub) {
  8119. ast_log(LOG_ERROR, "%s span %d could not subscribe to MWI events for %s@%s.",
  8120. sig_pri_cc_type_name, pri->span, mbox_number, mbox_context);
  8121. }
  8122. #if defined(HAVE_PRI_MWI_V2)
  8123. if (ast_strlen_zero(pri->mbox[i].vm_number)) {
  8124. ast_log(LOG_WARNING, "%s span %d MWI voicemail number for %s@%s is empty.\n",
  8125. sig_pri_cc_type_name, pri->span, mbox_number, mbox_context);
  8126. }
  8127. #endif /* defined(HAVE_PRI_MWI_V2) */
  8128. }
  8129. #endif /* defined(HAVE_PRI_MWI) */
  8130. for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
  8131. if (pri->fds[i] == -1) {
  8132. break;
  8133. }
  8134. switch (pri->sig) {
  8135. case SIG_BRI:
  8136. pri->dchans[i] = pri_new_bri(pri->fds[i], 1, pri->nodetype, pri->switchtype);
  8137. break;
  8138. case SIG_BRI_PTMP:
  8139. pri->dchans[i] = pri_new_bri(pri->fds[i], 0, pri->nodetype, pri->switchtype);
  8140. break;
  8141. default:
  8142. pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
  8143. #if defined(HAVE_PRI_SERVICE_MESSAGES)
  8144. if (pri->enable_service_message_support) {
  8145. pri_set_service_message_support(pri->dchans[i], 1);
  8146. }
  8147. #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
  8148. break;
  8149. }
  8150. pri_set_overlapdial(pri->dchans[i], (pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING) ? 1 : 0);
  8151. #ifdef HAVE_PRI_PROG_W_CAUSE
  8152. pri_set_chan_mapping_logical(pri->dchans[i], pri->qsigchannelmapping == DAHDI_CHAN_MAPPING_LOGICAL);
  8153. #endif
  8154. #ifdef HAVE_PRI_INBANDDISCONNECT
  8155. pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect);
  8156. #endif
  8157. /* Enslave to master if appropriate */
  8158. if (i)
  8159. pri_enslave(pri->dchans[0], pri->dchans[i]);
  8160. if (!pri->dchans[i]) {
  8161. if (pri->fds[i] > 0)
  8162. close(pri->fds[i]);
  8163. pri->fds[i] = -1;
  8164. ast_log(LOG_ERROR, "Unable to create PRI structure\n");
  8165. return -1;
  8166. }
  8167. pri_set_debug(pri->dchans[i], SIG_PRI_DEBUG_DEFAULT);
  8168. pri_set_nsf(pri->dchans[i], pri->nsf);
  8169. #ifdef PRI_GETSET_TIMERS
  8170. for (x = 0; x < PRI_MAX_TIMERS; x++) {
  8171. if (pri->pritimers[x] != 0)
  8172. pri_set_timer(pri->dchans[i], x, pri->pritimers[x]);
  8173. }
  8174. #endif
  8175. }
  8176. /* Assume primary is the one we use */
  8177. pri->pri = pri->dchans[0];
  8178. #if defined(HAVE_PRI_CALL_HOLD)
  8179. pri_hold_enable(pri->pri, 1);
  8180. #endif /* defined(HAVE_PRI_CALL_HOLD) */
  8181. #if defined(HAVE_PRI_CALL_REROUTING)
  8182. pri_reroute_enable(pri->pri, 1);
  8183. #endif /* defined(HAVE_PRI_CALL_REROUTING) */
  8184. #if defined(HAVE_PRI_HANGUP_FIX)
  8185. pri_hangup_fix_enable(pri->pri, 1);
  8186. #endif /* defined(HAVE_PRI_HANGUP_FIX) */
  8187. #if defined(HAVE_PRI_CCSS)
  8188. pri_cc_enable(pri->pri, 1);
  8189. pri_cc_recall_mode(pri->pri, pri->cc_ptmp_recall_mode);
  8190. pri_cc_retain_signaling_req(pri->pri, pri->cc_qsig_signaling_link_req);
  8191. pri_cc_retain_signaling_rsp(pri->pri, pri->cc_qsig_signaling_link_rsp);
  8192. #endif /* defined(HAVE_PRI_CCSS) */
  8193. #if defined(HAVE_PRI_TRANSFER)
  8194. pri_transfer_enable(pri->pri, 1);
  8195. #endif /* defined(HAVE_PRI_TRANSFER) */
  8196. #if defined(HAVE_PRI_AOC_EVENTS)
  8197. pri_aoc_events_enable(pri->pri, 1);
  8198. #endif /* defined(HAVE_PRI_AOC_EVENTS) */
  8199. #if defined(HAVE_PRI_CALL_WAITING)
  8200. pri_connect_ack_enable(pri->pri, 1);
  8201. #endif /* defined(HAVE_PRI_CALL_WAITING) */
  8202. #if defined(HAVE_PRI_MCID)
  8203. pri_mcid_enable(pri->pri, 1);
  8204. #endif /* defined(HAVE_PRI_MCID) */
  8205. #if defined(HAVE_PRI_DISPLAY_TEXT)
  8206. pri_display_options_send(pri->pri, pri->display_flags_send);
  8207. pri_display_options_receive(pri->pri, pri->display_flags_receive);
  8208. #endif /* defined(HAVE_PRI_DISPLAY_TEXT) */
  8209. #if defined(HAVE_PRI_DATETIME_SEND)
  8210. pri_date_time_send_option(pri->pri, pri->datetime_send);
  8211. #endif /* defined(HAVE_PRI_DATETIME_SEND) */
  8212. #if defined(HAVE_PRI_L2_PERSISTENCE)
  8213. pri_persistent_layer2_option(pri->pri, pri->l2_persistence);
  8214. #endif /* defined(HAVE_PRI_L2_PERSISTENCE) */
  8215. pri->resetpos = -1;
  8216. if (ast_pthread_create_background(&pri->master, NULL, pri_dchannel, pri)) {
  8217. for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
  8218. if (!pri->dchans[i])
  8219. break;
  8220. if (pri->fds[i] > 0)
  8221. close(pri->fds[i]);
  8222. pri->fds[i] = -1;
  8223. }
  8224. ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
  8225. return -1;
  8226. }
  8227. #if defined(HAVE_PRI_MWI)
  8228. /*
  8229. * Send the initial MWI indications from the event cache for this span.
  8230. *
  8231. * If we were loaded after app_voicemail the event would already be in
  8232. * the cache. If we were loaded before app_voicemail the event would not
  8233. * be in the cache yet and app_voicemail will send the event when it
  8234. * gets loaded.
  8235. */
  8236. sig_pri_mwi_cache_update(pri);
  8237. #endif /* defined(HAVE_PRI_MWI) */
  8238. return 0;
  8239. }
  8240. /*!
  8241. * \brief Notify new alarm status.
  8242. *
  8243. * \param p Channel private pointer.
  8244. * \param noalarm Non-zero if not in alarm mode.
  8245. *
  8246. * \note Assumes the sig_pri_lock_private(p) is already obtained.
  8247. *
  8248. * \return Nothing
  8249. */
  8250. void sig_pri_chan_alarm_notify(struct sig_pri_chan *p, int noalarm)
  8251. {
  8252. pri_grab(p, p->pri);
  8253. sig_pri_set_alarm(p, !noalarm);
  8254. if (!noalarm) {
  8255. if (pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
  8256. /* T309 is not enabled : destroy calls when alarm occurs */
  8257. if (p->call) {
  8258. pri_destroycall(p->pri->pri, p->call);
  8259. p->call = NULL;
  8260. }
  8261. if (p->owner)
  8262. p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  8263. }
  8264. }
  8265. sig_pri_span_devstate_changed(p->pri);
  8266. pri_rel(p->pri);
  8267. }
  8268. /*!
  8269. * \brief Determine if layer 1 alarms are ignored.
  8270. *
  8271. * \param p Channel private pointer.
  8272. *
  8273. * \return TRUE if the alarm is ignored.
  8274. */
  8275. int sig_pri_is_alarm_ignored(struct sig_pri_span *pri)
  8276. {
  8277. return pri->layer1_ignored;
  8278. }
  8279. struct sig_pri_chan *sig_pri_chan_new(void *pvt_data, struct sig_pri_callback *callback, struct sig_pri_span *pri, int logicalspan, int channo, int trunkgroup)
  8280. {
  8281. struct sig_pri_chan *p;
  8282. p = ast_calloc(1, sizeof(*p));
  8283. if (!p)
  8284. return p;
  8285. p->logicalspan = logicalspan;
  8286. p->prioffset = channo;
  8287. p->mastertrunkgroup = trunkgroup;
  8288. p->calls = callback;
  8289. p->chan_pvt = pvt_data;
  8290. p->pri = pri;
  8291. return p;
  8292. }
  8293. /*!
  8294. * \brief Delete the sig_pri private channel structure.
  8295. * \since 1.8
  8296. *
  8297. * \param doomed sig_pri private channel structure to delete.
  8298. *
  8299. * \return Nothing
  8300. */
  8301. void sig_pri_chan_delete(struct sig_pri_chan *doomed)
  8302. {
  8303. ast_free(doomed);
  8304. }
  8305. #define SIG_PRI_SC_HEADER "%-4s %4s %-4s %-4s %-10s %-4s %s\n"
  8306. #define SIG_PRI_SC_LINE "%4d %4d %-4s %-4s %-10s %-4s %s"
  8307. void sig_pri_cli_show_channels_header(int fd)
  8308. {
  8309. ast_cli(fd, SIG_PRI_SC_HEADER, "PRI", "", "B", "Chan", "Call", "PRI", "Channel");
  8310. ast_cli(fd, SIG_PRI_SC_HEADER, "Span", "Chan", "Chan", "Idle", "Level", "Call", "Name");
  8311. }
  8312. void sig_pri_cli_show_channels(int fd, struct sig_pri_span *pri)
  8313. {
  8314. char line[256];
  8315. int idx;
  8316. struct sig_pri_chan *pvt;
  8317. ast_mutex_lock(&pri->lock);
  8318. for (idx = 0; idx < pri->numchans; ++idx) {
  8319. if (!pri->pvts[idx]) {
  8320. continue;
  8321. }
  8322. pvt = pri->pvts[idx];
  8323. sig_pri_lock_private(pvt);
  8324. sig_pri_lock_owner(pri, idx);
  8325. if (pvt->no_b_channel && sig_pri_is_chan_available(pvt)) {
  8326. /* Don't show held/call-waiting channels if they are not in use. */
  8327. sig_pri_unlock_private(pvt);
  8328. continue;
  8329. }
  8330. snprintf(line, sizeof(line), SIG_PRI_SC_LINE,
  8331. pri->span,
  8332. pvt->channel,
  8333. pvt->no_b_channel ? "No" : "Yes",/* Has media */
  8334. sig_pri_is_chan_available(pvt) ? "Yes" : "No",
  8335. sig_pri_call_level2str(pvt->call_level),
  8336. pvt->call ? "Yes" : "No",
  8337. pvt->owner ? pvt->owner->name : "");
  8338. if (pvt->owner) {
  8339. ast_channel_unlock(pvt->owner);
  8340. }
  8341. sig_pri_unlock_private(pvt);
  8342. ast_mutex_unlock(&pri->lock);
  8343. ast_cli(fd, "%s\n", line);
  8344. ast_mutex_lock(&pri->lock);
  8345. }
  8346. ast_mutex_unlock(&pri->lock);
  8347. }
  8348. static void build_status(char *s, size_t len, int status, int active)
  8349. {
  8350. if (!s || len < 1) {
  8351. return;
  8352. }
  8353. snprintf(s, len, "%s%s, %s",
  8354. (status & DCHAN_NOTINALARM) ? "" : "In Alarm, ",
  8355. (status & DCHAN_UP) ? "Up" : "Down",
  8356. (active) ? "Active" : "Standby");
  8357. }
  8358. void sig_pri_cli_show_spans(int fd, int span, struct sig_pri_span *pri)
  8359. {
  8360. char status[256];
  8361. int x;
  8362. for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
  8363. if (pri->dchans[x]) {
  8364. build_status(status, sizeof(status), pri->dchanavail[x], pri->dchans[x] == pri->pri);
  8365. ast_cli(fd, "PRI span %d/%d: %s\n", span, x, status);
  8366. }
  8367. }
  8368. }
  8369. void sig_pri_cli_show_span(int fd, int *dchannels, struct sig_pri_span *pri)
  8370. {
  8371. int x;
  8372. char status[256];
  8373. for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
  8374. if (pri->dchans[x]) {
  8375. #ifdef PRI_DUMP_INFO_STR
  8376. char *info_str = NULL;
  8377. #endif
  8378. ast_cli(fd, "%s D-channel: %d\n", pri_order(x), dchannels[x]);
  8379. build_status(status, sizeof(status), pri->dchanavail[x], pri->dchans[x] == pri->pri);
  8380. ast_cli(fd, "Status: %s\n", status);
  8381. ast_mutex_lock(&pri->lock);
  8382. #ifdef PRI_DUMP_INFO_STR
  8383. info_str = pri_dump_info_str(pri->pri);
  8384. if (info_str) {
  8385. ast_cli(fd, "%s", info_str);
  8386. free(info_str);
  8387. }
  8388. #else
  8389. pri_dump_info(pri->pri);
  8390. #endif
  8391. ast_mutex_unlock(&pri->lock);
  8392. ast_cli(fd, "Overlap Recv: %s\n\n", (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)?"Yes":"No");
  8393. ast_cli(fd, "\n");
  8394. }
  8395. }
  8396. }
  8397. int pri_send_keypad_facility_exec(struct sig_pri_chan *p, const char *digits)
  8398. {
  8399. sig_pri_lock_private(p);
  8400. if (!p->pri || !p->call) {
  8401. ast_debug(1, "Unable to find pri or call on channel!\n");
  8402. sig_pri_unlock_private(p);
  8403. return -1;
  8404. }
  8405. pri_grab(p, p->pri);
  8406. pri_keypad_facility(p->pri->pri, p->call, digits);
  8407. pri_rel(p->pri);
  8408. sig_pri_unlock_private(p);
  8409. return 0;
  8410. }
  8411. int pri_send_callrerouting_facility_exec(struct sig_pri_chan *p, enum ast_channel_state chanstate, const char *destination, const char *original, const char *reason)
  8412. {
  8413. int res;
  8414. sig_pri_lock_private(p);
  8415. if (!p->pri || !p->call) {
  8416. ast_debug(1, "Unable to find pri or call on channel!\n");
  8417. sig_pri_unlock_private(p);
  8418. return -1;
  8419. }
  8420. pri_grab(p, p->pri);
  8421. res = pri_callrerouting_facility(p->pri->pri, p->call, destination, original, reason);
  8422. pri_rel(p->pri);
  8423. sig_pri_unlock_private(p);
  8424. return res;
  8425. }
  8426. #if defined(HAVE_PRI_SERVICE_MESSAGES)
  8427. int pri_maintenance_bservice(struct pri *pri, struct sig_pri_chan *p, int changestatus)
  8428. {
  8429. int channel = PVT_TO_CHANNEL(p);
  8430. int span = PRI_SPAN(channel);
  8431. return pri_maintenance_service(pri, span, channel, changestatus);
  8432. }
  8433. #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
  8434. void sig_pri_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_pri_chan *pchan)
  8435. {
  8436. if (pchan->owner == oldchan) {
  8437. pchan->owner = newchan;
  8438. }
  8439. }
  8440. #if defined(HAVE_PRI_DISPLAY_TEXT)
  8441. /*!
  8442. * \brief Send display text.
  8443. * \since 10.0
  8444. *
  8445. * \param p Channel to send text over
  8446. * \param text Text to send.
  8447. *
  8448. * \return Nothing
  8449. */
  8450. void sig_pri_sendtext(struct sig_pri_chan *p, const char *text)
  8451. {
  8452. struct pri_subcmd_display_txt display;
  8453. if (p->pri && p->pri->pri) {
  8454. ast_copy_string(display.text, text, sizeof(display.text));
  8455. display.length = strlen(display.text);
  8456. display.char_set = 0;/* unknown(0) */
  8457. pri_grab(p, p->pri);
  8458. pri_display_text(p->pri->pri, p->call, &display);
  8459. pri_rel(p->pri);
  8460. }
  8461. }
  8462. #endif /* defined(HAVE_PRI_DISPLAY_TEXT) */
  8463. #if defined(HAVE_PRI_CCSS)
  8464. /*!
  8465. * \brief PRI CC agent initialization.
  8466. * \since 1.8
  8467. *
  8468. * \param agent CC core agent control.
  8469. * \param pvt_chan Original channel the agent will attempt to recall.
  8470. *
  8471. * \details
  8472. * This callback is called when the CC core is initialized. Agents should allocate
  8473. * any private data necessary for the call and assign it to the private_data
  8474. * on the agent. Additionally, if any ast_cc_agent_flags are pertinent to the
  8475. * specific agent type, they should be set in this function as well.
  8476. *
  8477. * \retval 0 on success.
  8478. * \retval -1 on error.
  8479. */
  8480. int sig_pri_cc_agent_init(struct ast_cc_agent *agent, struct sig_pri_chan *pvt_chan)
  8481. {
  8482. struct sig_pri_cc_agent_prv *cc_pvt;
  8483. cc_pvt = ast_calloc(1, sizeof(*cc_pvt));
  8484. if (!cc_pvt) {
  8485. return -1;
  8486. }
  8487. ast_mutex_lock(&pvt_chan->pri->lock);
  8488. cc_pvt->pri = pvt_chan->pri;
  8489. cc_pvt->cc_id = pri_cc_available(pvt_chan->pri->pri, pvt_chan->call);
  8490. ast_mutex_unlock(&pvt_chan->pri->lock);
  8491. if (cc_pvt->cc_id == -1) {
  8492. ast_free(cc_pvt);
  8493. return -1;
  8494. }
  8495. agent->private_data = cc_pvt;
  8496. return 0;
  8497. }
  8498. #endif /* defined(HAVE_PRI_CCSS) */
  8499. #if defined(HAVE_PRI_CCSS)
  8500. /*!
  8501. * \brief Start the offer timer.
  8502. * \since 1.8
  8503. *
  8504. * \param agent CC core agent control.
  8505. *
  8506. * \details
  8507. * This is called by the core when the caller hangs up after
  8508. * a call for which CC may be requested. The agent should
  8509. * begin the timer as configured.
  8510. *
  8511. * The primary reason why this functionality is left to
  8512. * the specific agent implementations is due to the differing
  8513. * use of schedulers throughout the code. Some channel drivers
  8514. * may already have a scheduler context they wish to use, and
  8515. * amongst those, some may use the ast_sched API while others
  8516. * may use the ast_sched_thread API, which are incompatible.
  8517. *
  8518. * \retval 0 on success.
  8519. * \retval -1 on error.
  8520. */
  8521. int sig_pri_cc_agent_start_offer_timer(struct ast_cc_agent *agent)
  8522. {
  8523. /* libpri maintains it's own offer timer in the form of T_RETENTION. */
  8524. return 0;
  8525. }
  8526. #endif /* defined(HAVE_PRI_CCSS) */
  8527. #if defined(HAVE_PRI_CCSS)
  8528. /*!
  8529. * \brief Stop the offer timer.
  8530. * \since 1.8
  8531. *
  8532. * \param agent CC core agent control.
  8533. *
  8534. * \details
  8535. * This callback is called by the CC core when the caller
  8536. * has requested CC.
  8537. *
  8538. * \retval 0 on success.
  8539. * \retval -1 on error.
  8540. */
  8541. int sig_pri_cc_agent_stop_offer_timer(struct ast_cc_agent *agent)
  8542. {
  8543. /* libpri maintains it's own offer timer in the form of T_RETENTION. */
  8544. return 0;
  8545. }
  8546. #endif /* defined(HAVE_PRI_CCSS) */
  8547. #if defined(HAVE_PRI_CCSS)
  8548. /*!
  8549. * \brief Response to a CC request.
  8550. * \since 1.8
  8551. *
  8552. * \param agent CC core agent control.
  8553. * \param reason CC request response status.
  8554. *
  8555. * \details
  8556. * When the core receives knowledge that a called
  8557. * party has accepted a CC request, it will call
  8558. * this callback. The core may also call this
  8559. * if there is some error when attempting to process
  8560. * the incoming CC request.
  8561. *
  8562. * The duty of this is to issue a propper response to a
  8563. * CC request from the caller by acknowledging receipt
  8564. * of that request or rejecting it.
  8565. *
  8566. * \return Nothing
  8567. */
  8568. void sig_pri_cc_agent_req_rsp(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason)
  8569. {
  8570. struct sig_pri_cc_agent_prv *cc_pvt;
  8571. int res;
  8572. int status;
  8573. const char *failed_msg;
  8574. static const char *failed_to_send = "Failed to send the CC request response.";
  8575. static const char *not_accepted = "The core declined the CC request.";
  8576. cc_pvt = agent->private_data;
  8577. ast_mutex_lock(&cc_pvt->pri->lock);
  8578. if (cc_pvt->cc_request_response_pending) {
  8579. cc_pvt->cc_request_response_pending = 0;
  8580. /* Convert core response reason to ISDN response status. */
  8581. status = 2;/* short_term_denial */
  8582. switch (reason) {
  8583. case AST_CC_AGENT_RESPONSE_SUCCESS:
  8584. status = 0;/* success */
  8585. break;
  8586. case AST_CC_AGENT_RESPONSE_FAILURE_INVALID:
  8587. status = 2;/* short_term_denial */
  8588. break;
  8589. case AST_CC_AGENT_RESPONSE_FAILURE_TOO_MANY:
  8590. status = 5;/* queue_full */
  8591. break;
  8592. }
  8593. res = pri_cc_req_rsp(cc_pvt->pri->pri, cc_pvt->cc_id, status);
  8594. if (!status) {
  8595. /* CC core request was accepted. */
  8596. if (res) {
  8597. failed_msg = failed_to_send;
  8598. } else {
  8599. failed_msg = NULL;
  8600. }
  8601. } else {
  8602. /* CC core request was declined. */
  8603. if (res) {
  8604. failed_msg = failed_to_send;
  8605. } else {
  8606. failed_msg = not_accepted;
  8607. }
  8608. }
  8609. } else {
  8610. failed_msg = NULL;
  8611. }
  8612. ast_mutex_unlock(&cc_pvt->pri->lock);
  8613. if (failed_msg) {
  8614. ast_cc_failed(agent->core_id, "%s agent: %s", sig_pri_cc_type_name, failed_msg);
  8615. }
  8616. }
  8617. #endif /* defined(HAVE_PRI_CCSS) */
  8618. #if defined(HAVE_PRI_CCSS)
  8619. /*!
  8620. * \brief Request the status of the agent's device.
  8621. * \since 1.8
  8622. *
  8623. * \param agent CC core agent control.
  8624. *
  8625. * \details
  8626. * Asynchronous request for the status of any caller
  8627. * which may be a valid caller for the CC transaction.
  8628. * Status responses should be made using the
  8629. * ast_cc_status_response function.
  8630. *
  8631. * \retval 0 on success.
  8632. * \retval -1 on error.
  8633. */
  8634. int sig_pri_cc_agent_status_req(struct ast_cc_agent *agent)
  8635. {
  8636. struct sig_pri_cc_agent_prv *cc_pvt;
  8637. cc_pvt = agent->private_data;
  8638. ast_mutex_lock(&cc_pvt->pri->lock);
  8639. pri_cc_status_req(cc_pvt->pri->pri, cc_pvt->cc_id);
  8640. ast_mutex_unlock(&cc_pvt->pri->lock);
  8641. return 0;
  8642. }
  8643. #endif /* defined(HAVE_PRI_CCSS) */
  8644. #if defined(HAVE_PRI_CCSS)
  8645. /*!
  8646. * \brief Request for an agent's phone to stop ringing.
  8647. * \since 1.8
  8648. *
  8649. * \param agent CC core agent control.
  8650. *
  8651. * \details
  8652. * The usefulness of this is quite limited. The only specific
  8653. * known case for this is if Asterisk requests CC over an ISDN
  8654. * PTMP link as the TE side. If other phones are in the same
  8655. * recall group as the Asterisk server, and one of those phones
  8656. * picks up the recall notice, then Asterisk will receive a
  8657. * "stop ringing" notification from the NT side of the PTMP
  8658. * link. This indication needs to be passed to the phone
  8659. * on the other side of the Asterisk server which originally
  8660. * placed the call so that it will stop ringing. Since the
  8661. * phone may be of any type, it is necessary to have a callback
  8662. * that the core can know about.
  8663. *
  8664. * \retval 0 on success.
  8665. * \retval -1 on error.
  8666. */
  8667. int sig_pri_cc_agent_stop_ringing(struct ast_cc_agent *agent)
  8668. {
  8669. struct sig_pri_cc_agent_prv *cc_pvt;
  8670. cc_pvt = agent->private_data;
  8671. ast_mutex_lock(&cc_pvt->pri->lock);
  8672. pri_cc_stop_alerting(cc_pvt->pri->pri, cc_pvt->cc_id);
  8673. ast_mutex_unlock(&cc_pvt->pri->lock);
  8674. return 0;
  8675. }
  8676. #endif /* defined(HAVE_PRI_CCSS) */
  8677. #if defined(HAVE_PRI_CCSS)
  8678. /*!
  8679. * \brief Let the caller know that the callee has become free
  8680. * but that the caller cannot attempt to call back because
  8681. * he is either busy or there is congestion on his line.
  8682. * \since 1.8
  8683. *
  8684. * \param agent CC core agent control.
  8685. *
  8686. * \details
  8687. * This is something that really only affects a scenario where
  8688. * a phone places a call over ISDN PTMP to Asterisk, who then
  8689. * connects over PTMP again to the ISDN network. For most agent
  8690. * types, there is no need to implement this callback at all
  8691. * because they don't really need to actually do anything in
  8692. * this situation. If you're having trouble understanding what
  8693. * the purpose of this callback is, then you can be safe simply
  8694. * not implementing it.
  8695. *
  8696. * \retval 0 on success.
  8697. * \retval -1 on error.
  8698. */
  8699. int sig_pri_cc_agent_party_b_free(struct ast_cc_agent *agent)
  8700. {
  8701. struct sig_pri_cc_agent_prv *cc_pvt;
  8702. cc_pvt = agent->private_data;
  8703. ast_mutex_lock(&cc_pvt->pri->lock);
  8704. pri_cc_b_free(cc_pvt->pri->pri, cc_pvt->cc_id);
  8705. ast_mutex_unlock(&cc_pvt->pri->lock);
  8706. return 0;
  8707. }
  8708. #endif /* defined(HAVE_PRI_CCSS) */
  8709. #if defined(HAVE_PRI_CCSS)
  8710. /*!
  8711. * \brief Begin monitoring a busy device.
  8712. * \since 1.8
  8713. *
  8714. * \param agent CC core agent control.
  8715. *
  8716. * \details
  8717. * The core will call this callback if the callee becomes
  8718. * available but the caller has reported that he is busy.
  8719. * The agent should begin monitoring the caller's device.
  8720. * When the caller becomes available again, the agent should
  8721. * call ast_cc_agent_caller_available.
  8722. *
  8723. * \retval 0 on success.
  8724. * \retval -1 on error.
  8725. */
  8726. int sig_pri_cc_agent_start_monitoring(struct ast_cc_agent *agent)
  8727. {
  8728. /* libpri already knows when and how it needs to monitor Party A. */
  8729. return 0;
  8730. }
  8731. #endif /* defined(HAVE_PRI_CCSS) */
  8732. #if defined(HAVE_PRI_CCSS)
  8733. /*!
  8734. * \brief Alert the caller that it is time to try recalling.
  8735. * \since 1.8
  8736. *
  8737. * \param agent CC core agent control.
  8738. *
  8739. * \details
  8740. * The core will call this function when it receives notice
  8741. * that a monitored party has become available.
  8742. *
  8743. * The agent's job is to send a message to the caller to
  8744. * notify it of such a change. If the agent is able to
  8745. * discern that the caller is currently unavailable, then
  8746. * the agent should react by calling the ast_cc_caller_unavailable
  8747. * function.
  8748. *
  8749. * \retval 0 on success.
  8750. * \retval -1 on error.
  8751. */
  8752. int sig_pri_cc_agent_callee_available(struct ast_cc_agent *agent)
  8753. {
  8754. struct sig_pri_cc_agent_prv *cc_pvt;
  8755. cc_pvt = agent->private_data;
  8756. ast_mutex_lock(&cc_pvt->pri->lock);
  8757. pri_cc_remote_user_free(cc_pvt->pri->pri, cc_pvt->cc_id);
  8758. ast_mutex_unlock(&cc_pvt->pri->lock);
  8759. return 0;
  8760. }
  8761. #endif /* defined(HAVE_PRI_CCSS) */
  8762. #if defined(HAVE_PRI_CCSS)
  8763. /*!
  8764. * \brief Destroy private data on the agent.
  8765. * \since 1.8
  8766. *
  8767. * \param agent CC core agent control.
  8768. *
  8769. * \details
  8770. * The core will call this function upon completion
  8771. * or failure of CC.
  8772. *
  8773. * \note
  8774. * The agent private_data pointer may be NULL if the agent
  8775. * constructor failed.
  8776. *
  8777. * \return Nothing
  8778. */
  8779. void sig_pri_cc_agent_destructor(struct ast_cc_agent *agent)
  8780. {
  8781. struct sig_pri_cc_agent_prv *cc_pvt;
  8782. int res;
  8783. cc_pvt = agent->private_data;
  8784. if (!cc_pvt) {
  8785. /* The agent constructor probably failed. */
  8786. return;
  8787. }
  8788. ast_mutex_lock(&cc_pvt->pri->lock);
  8789. res = -1;
  8790. if (cc_pvt->cc_request_response_pending) {
  8791. res = pri_cc_req_rsp(cc_pvt->pri->pri, cc_pvt->cc_id, 2/* short_term_denial */);
  8792. }
  8793. if (res) {
  8794. pri_cc_cancel(cc_pvt->pri->pri, cc_pvt->cc_id);
  8795. }
  8796. ast_mutex_unlock(&cc_pvt->pri->lock);
  8797. ast_free(cc_pvt);
  8798. }
  8799. #endif /* defined(HAVE_PRI_CCSS) */
  8800. #if defined(HAVE_PRI_CCSS)
  8801. /*!
  8802. * \internal
  8803. * \brief Return the hash value of the given CC monitor instance object.
  8804. * \since 1.8
  8805. *
  8806. * \param obj pointer to the (user-defined part) of an object.
  8807. * \param flags flags from ao2_callback(). Ignored at the moment.
  8808. *
  8809. * \retval core_id
  8810. */
  8811. static int sig_pri_cc_monitor_instance_hash_fn(const void *obj, const int flags)
  8812. {
  8813. const struct sig_pri_cc_monitor_instance *monitor_instance = obj;
  8814. return monitor_instance->core_id;
  8815. }
  8816. #endif /* defined(HAVE_PRI_CCSS) */
  8817. #if defined(HAVE_PRI_CCSS)
  8818. /*!
  8819. * \internal
  8820. * \brief Compere the monitor instance core_id key value.
  8821. * \since 1.8
  8822. *
  8823. * \param obj pointer to the (user-defined part) of an object.
  8824. * \param arg callback argument from ao2_callback()
  8825. * \param flags flags from ao2_callback()
  8826. *
  8827. * \return values are a combination of enum _cb_results.
  8828. */
  8829. static int sig_pri_cc_monitor_instance_cmp_fn(void *obj, void *arg, int flags)
  8830. {
  8831. struct sig_pri_cc_monitor_instance *monitor_1 = obj;
  8832. struct sig_pri_cc_monitor_instance *monitor_2 = arg;
  8833. return monitor_1->core_id == monitor_2->core_id ? CMP_MATCH | CMP_STOP : 0;
  8834. }
  8835. #endif /* defined(HAVE_PRI_CCSS) */
  8836. #if defined(HAVE_PRI_CCSS)
  8837. /*!
  8838. * \brief Request CCSS.
  8839. * \since 1.8
  8840. *
  8841. * \param monitor CC core monitor control.
  8842. * \param available_timer_id Where to put the available timer scheduler id.
  8843. * Will never be NULL for a device monitor.
  8844. *
  8845. * \details
  8846. * Perform whatever steps are necessary in order to request CC.
  8847. * In addition, the monitor implementation is responsible for
  8848. * starting the available timer in this callback. The scheduler
  8849. * ID for the callback must be stored in the parent_link's child_avail_id
  8850. * field.
  8851. *
  8852. * \retval 0 on success
  8853. * \retval -1 on failure.
  8854. */
  8855. int sig_pri_cc_monitor_req_cc(struct ast_cc_monitor *monitor, int *available_timer_id)
  8856. {
  8857. struct sig_pri_cc_monitor_instance *instance;
  8858. int cc_mode;
  8859. int res;
  8860. switch (monitor->service_offered) {
  8861. case AST_CC_CCBS:
  8862. cc_mode = 0;/* CCBS */
  8863. break;
  8864. case AST_CC_CCNR:
  8865. cc_mode = 1;/* CCNR */
  8866. break;
  8867. default:
  8868. /* CC service not supported by ISDN. */
  8869. return -1;
  8870. }
  8871. instance = monitor->private_data;
  8872. /* libpri handles it's own available timer. */
  8873. ast_mutex_lock(&instance->pri->lock);
  8874. res = pri_cc_req(instance->pri->pri, instance->cc_id, cc_mode);
  8875. ast_mutex_unlock(&instance->pri->lock);
  8876. return res;
  8877. }
  8878. #endif /* defined(HAVE_PRI_CCSS) */
  8879. #if defined(HAVE_PRI_CCSS)
  8880. /*!
  8881. * \brief Suspend monitoring.
  8882. * \since 1.8
  8883. *
  8884. * \param monitor CC core monitor control.
  8885. *
  8886. * \details
  8887. * Implementers must perform the necessary steps to suspend
  8888. * monitoring.
  8889. *
  8890. * \retval 0 on success
  8891. * \retval -1 on failure.
  8892. */
  8893. int sig_pri_cc_monitor_suspend(struct ast_cc_monitor *monitor)
  8894. {
  8895. struct sig_pri_cc_monitor_instance *instance;
  8896. instance = monitor->private_data;
  8897. ast_mutex_lock(&instance->pri->lock);
  8898. pri_cc_status(instance->pri->pri, instance->cc_id, 1/* busy */);
  8899. ast_mutex_unlock(&instance->pri->lock);
  8900. return 0;
  8901. }
  8902. #endif /* defined(HAVE_PRI_CCSS) */
  8903. #if defined(HAVE_PRI_CCSS)
  8904. /*!
  8905. * \brief Unsuspend monitoring.
  8906. * \since 1.8
  8907. *
  8908. * \param monitor CC core monitor control.
  8909. *
  8910. * \details
  8911. * Perform the necessary steps to unsuspend monitoring.
  8912. *
  8913. * \retval 0 on success
  8914. * \retval -1 on failure.
  8915. */
  8916. int sig_pri_cc_monitor_unsuspend(struct ast_cc_monitor *monitor)
  8917. {
  8918. struct sig_pri_cc_monitor_instance *instance;
  8919. instance = monitor->private_data;
  8920. ast_mutex_lock(&instance->pri->lock);
  8921. pri_cc_status(instance->pri->pri, instance->cc_id, 0/* free */);
  8922. ast_mutex_unlock(&instance->pri->lock);
  8923. return 0;
  8924. }
  8925. #endif /* defined(HAVE_PRI_CCSS) */
  8926. #if defined(HAVE_PRI_CCSS)
  8927. /*!
  8928. * \brief Status response to an ast_cc_monitor_status_request().
  8929. * \since 1.8
  8930. *
  8931. * \param monitor CC core monitor control.
  8932. * \param devstate Current status of a Party A device.
  8933. *
  8934. * \details
  8935. * Alert a monitor as to the status of the agent for which
  8936. * the monitor had previously requested a status request.
  8937. *
  8938. * \note Zero or more responses may come as a result.
  8939. *
  8940. * \retval 0 on success
  8941. * \retval -1 on failure.
  8942. */
  8943. int sig_pri_cc_monitor_status_rsp(struct ast_cc_monitor *monitor, enum ast_device_state devstate)
  8944. {
  8945. struct sig_pri_cc_monitor_instance *instance;
  8946. int cc_status;
  8947. switch (devstate) {
  8948. case AST_DEVICE_UNKNOWN:
  8949. case AST_DEVICE_NOT_INUSE:
  8950. cc_status = 0;/* free */
  8951. break;
  8952. case AST_DEVICE_BUSY:
  8953. case AST_DEVICE_INUSE:
  8954. cc_status = 1;/* busy */
  8955. break;
  8956. default:
  8957. /* Don't know how to interpret this device state into free/busy status. */
  8958. return 0;
  8959. }
  8960. instance = monitor->private_data;
  8961. ast_mutex_lock(&instance->pri->lock);
  8962. pri_cc_status_req_rsp(instance->pri->pri, instance->cc_id, cc_status);
  8963. ast_mutex_unlock(&instance->pri->lock);
  8964. return 0;
  8965. }
  8966. #endif /* defined(HAVE_PRI_CCSS) */
  8967. #if defined(HAVE_PRI_CCSS)
  8968. /*!
  8969. * \brief Cancel the running available timer.
  8970. * \since 1.8
  8971. *
  8972. * \param monitor CC core monitor control.
  8973. * \param sched_id Available timer scheduler id to cancel.
  8974. * Will never be NULL for a device monitor.
  8975. *
  8976. * \details
  8977. * In most cases, this function will likely consist of just a
  8978. * call to AST_SCHED_DEL. It might have been possible to do this
  8979. * within the core, but unfortunately the mixture of sched_thread
  8980. * and sched usage in Asterisk prevents such usage.
  8981. *
  8982. * \retval 0 on success
  8983. * \retval -1 on failure.
  8984. */
  8985. int sig_pri_cc_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id)
  8986. {
  8987. /*
  8988. * libpri maintains it's own available timer as one of:
  8989. * T_CCBS2/T_CCBS5/T_CCBS6/QSIG_CCBS_T2
  8990. * T_CCNR2/T_CCNR5/T_CCNR6/QSIG_CCNR_T2
  8991. */
  8992. return 0;
  8993. }
  8994. #endif /* defined(HAVE_PRI_CCSS) */
  8995. #if defined(HAVE_PRI_CCSS)
  8996. /*!
  8997. * \brief Destroy PRI private data on the monitor.
  8998. * \since 1.8
  8999. *
  9000. * \param monitor_pvt CC device monitor private data pointer.
  9001. *
  9002. * \details
  9003. * Implementers of this callback are responsible for destroying
  9004. * all heap-allocated data in the monitor's private_data pointer, including
  9005. * the private_data itself.
  9006. */
  9007. void sig_pri_cc_monitor_destructor(void *monitor_pvt)
  9008. {
  9009. struct sig_pri_cc_monitor_instance *instance;
  9010. instance = monitor_pvt;
  9011. if (!instance) {
  9012. return;
  9013. }
  9014. ao2_unlink(sig_pri_cc_monitors, instance);
  9015. ao2_ref(instance, -1);
  9016. }
  9017. #endif /* defined(HAVE_PRI_CCSS) */
  9018. /*!
  9019. * \brief Load the sig_pri submodule.
  9020. * \since 1.8
  9021. *
  9022. * \param cc_type_name CC type name to use when looking up agent/monitor.
  9023. *
  9024. * \retval 0 on success.
  9025. * \retval -1 on error.
  9026. */
  9027. int sig_pri_load(const char *cc_type_name)
  9028. {
  9029. #if defined(HAVE_PRI_CCSS)
  9030. sig_pri_cc_type_name = cc_type_name;
  9031. sig_pri_cc_monitors = ao2_container_alloc(37, sig_pri_cc_monitor_instance_hash_fn,
  9032. sig_pri_cc_monitor_instance_cmp_fn);
  9033. if (!sig_pri_cc_monitors) {
  9034. return -1;
  9035. }
  9036. #endif /* defined(HAVE_PRI_CCSS) */
  9037. return 0;
  9038. }
  9039. /*!
  9040. * \brief Unload the sig_pri submodule.
  9041. * \since 1.8
  9042. *
  9043. * \return Nothing
  9044. */
  9045. void sig_pri_unload(void)
  9046. {
  9047. #if defined(HAVE_PRI_CCSS)
  9048. if (sig_pri_cc_monitors) {
  9049. ao2_ref(sig_pri_cc_monitors, -1);
  9050. sig_pri_cc_monitors = NULL;
  9051. }
  9052. #endif /* defined(HAVE_PRI_CCSS) */
  9053. }
  9054. #endif /* HAVE_PRI */