sig_pri.c 295 KB

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