sig_pri.c 297 KB

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