sig_pri.c 253 KB

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