sig_pri.c 299 KB

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