sig_pri.c 294 KB

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